1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QTabWidget>
#include <QPainter>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSqlQuery>
#include <QVariant>
#include <QFontMetrics>
#include <QApplication>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QLinearGradient>
#include <QPushButton>
#include <QSqlError>
#include "statisticsdialog.h"
StatisticsDialog::StatisticsDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
QVBoxLayout *mainLayout = new QVBoxLayout;
mTab = new QTabWidget;
QualityStats * qs = new QualityStats;
mTab->addTab(qs, tr("Quality Distribution"));
mainLayout->addWidget(mTab);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->setAlignment(Qt::AlignHCenter);
mOk = new QPushButton(tr("Ok"));
connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
buttonLayout->addWidget(mOk);
mainLayout->addLayout(buttonLayout);
QString title = QString(tr("%1 - Statistics")).arg(qApp->applicationName());
setLayout(mainLayout);
setWindowTitle(title);
}
QualityStats::QualityStats(QWidget *parent) : QWidget(parent), mMargin(10), mMaxValue(0) {
QSqlQuery q("SELECT DISTINCT(iquality), COUNT(iquality) FROM movies GROUP BY iquality");
while(q.next()){
mQualityDistrib[q.value(0).toInt()] = q.value(1).toInt();
if(q.value(1).toInt() > mMaxValue){
mMaxValue = q.value(1).toInt();
}
}
QFontMetrics fm(qApp->font());
mBarHeight = fm.height();
}
void QualityStats::paintEvent(QPaintEvent *){
QPainter p(this);
QMap<int, qreal> length;
QMap<int, int>::const_iterator it;
QMap<int, qreal>::const_iterator itr;
//draw background
QPen pen(Qt::red, 2);
p.setPen(pen);
QLinearGradient bgGradient(QPointF(0.0, height() / 2), QPointF(width(), height() / 2));
bgGradient.setColorAt(0, QColor(221, 107, 80));
bgGradient.setColorAt(1, QColor(251, 179, 150));
QBrush brush(bgGradient);
p.setBrush(brush);
p.drawRect(contentsRect());
//draw caption
QFont f = qApp->font();
f.setUnderline(true);
f.setBold(true);
p.setFont(f);
QString caption(tr("Quality Distribution"));
QFontMetrics fm(f);
int captionStartx = (width() - fm.width(caption)) / 2;
int captionStarty = fm.height();
p.drawText(captionStartx, captionStarty, caption);
//prepare drawing data
int barsStarty = fm.height() + 2 * mMargin;
f.setUnderline(false);
f.setBold(true);
p.setFont(f);
int maxKey = (--mQualityDistrib.end()).key();
int barsStartx = p.fontMetrics().width(QString::number(maxKey)) + 2 * mMargin;
int marginRight = p.fontMetrics().width(QString(tr("(%1")).arg(QString::number(mMaxValue))) + 2 * mMargin;
int maxWidth = width() - barsStartx - marginRight;
p.setRenderHints(QPainter::Antialiasing);
p.setPen(QPen(Qt::red, 1));
//calculate real lengths based on widget width
for(it = mQualityDistrib.constBegin(); it != mQualityDistrib.constEnd(); ++it){
qreal value = it.value();
value = static_cast<qreal>(value) / mMaxValue * maxWidth;
length[it.key()] = value;
}
//draw the bars and numbers
int ctr(0);
for(itr = length.constBegin(); itr != length.constEnd(); ++itr, ++ctr){
int starty = barsStarty + ctr * p.fontMetrics().height() + ctr * mMargin;
p.drawText(mMargin, starty, QString::number(itr.key()));
QLinearGradient barGradient(QPointF(barsStartx, starty), QPointF(width(), starty));
barGradient.setColorAt(0, QColor(0, 128, 170));
barGradient.setColorAt(1, QColor(131, 205, 214));
QBrush b(barGradient);
p.setBrush(b);
QRect bar(barsStartx, starty - p.fontMetrics().height() + p.fontMetrics().descent(), itr.value(), p.fontMetrics().height());
p.drawRect(bar);
int nStartx = barsStartx + itr.value() + mMargin;
p.drawText(nStartx, starty, QString(tr("(%1)")).arg(QString::number(mQualityDistrib[itr.key()])));
}
}
QSize QualityStats::sizeHint() const {
int height = mQualityDistrib.count() * mBarHeight;
height += mQualityDistrib.count() * mMargin;
height += mBarHeight + mMargin;
return QSize(400, height);
}
|