|
@@
-1,227
+1,227
|
|
1
|
/*!
|
|
1
|
/*!
|
|
2
|
\class Widget
|
|
2
|
\class Widget
|
|
3
|
\brief Ui for the application.
|
|
3
|
\brief Ui for the application.
|
|
4
|
\internal
|
|
4
|
\internal
|
|
5
|
*/
|
|
5
|
*/
|
|
6
|
|
|
6
|
|
|
7
|
#include "widget.h"
|
|
7
|
#include "widget.h"
|
|
8
|
#include <QGridLayout>
|
|
8
|
#include <QGridLayout>
|
|
9
|
#include <QPushButton>
|
|
9
|
#include <QPushButton>
|
|
10
|
#include <QLabel>
|
|
10
|
#include <QLabel>
|
|
11
|
|
|
11
|
|
|
12
|
#include <QSqlQuery>
|
|
12
|
#include <QSqlQuery>
|
|
13
|
#include <qscatterseries.h>
|
|
13
|
#include <qscatterseries.h>
|
|
14
|
#include <qchartview.h>
|
|
14
|
#include <qchartview.h>
|
|
15
|
#include <qchartaxis.h>
|
|
15
|
#include <qchartaxis.h>
|
|
16
|
#include <qbarcategory.h>
|
|
16
|
#include <qbarcategory.h>
|
|
17
|
#include <qbarset.h>
|
|
17
|
#include <qbarset.h>
|
|
18
|
#include <QListWidget>
|
|
18
|
#include <QListWidget>
|
|
19
|
#include <QPrinter>
|
|
19
|
#include <QPrinter>
|
|
20
|
#include <QPrintDialog>
|
|
20
|
#include <QPrintDialog>
|
|
21
|
#include <QRadioButton>
|
|
21
|
#include <QRadioButton>
|
|
22
|
|
|
22
|
|
|
23
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
23
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
24
|
|
|
24
|
|
|
25
|
Widget::Widget(QWidget *parent)
|
|
25
|
Widget::Widget(QWidget *parent)
|
|
26
|
: QWidget(parent)
|
|
26
|
: QWidget(parent)
|
|
27
|
{
|
|
27
|
{
|
|
28
|
setGeometry(100, 100, 1000, 600);
|
|
28
|
setGeometry(100, 100, 1000, 600);
|
|
29
|
|
|
29
|
|
|
30
|
// right panel layout
|
|
30
|
// right panel layout
|
|
31
|
barChartRadioButton = new QRadioButton(tr("Bar chart"));
|
|
31
|
barChartRadioButton = new QRadioButton(tr("Bar chart"));
|
|
32
|
barChartRadioButton->setChecked(true);
|
|
32
|
barChartRadioButton->setChecked(true);
|
|
33
|
scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
|
|
33
|
scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
|
|
34
|
scatterChartRadioButton->setChecked(false);
|
|
34
|
scatterChartRadioButton->setChecked(false);
|
|
35
|
countrieslist = new QListWidget;
|
|
35
|
countrieslist = new QListWidget;
|
|
36
|
countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
|
|
36
|
countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
|
|
37
|
|
|
37
|
|
|
|
|
|
38
|
//list of years widget
|
|
38
|
yearslist = new QListWidget;
|
|
39
|
yearslist = new QListWidget;
|
|
39
|
yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
40
|
yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
40
|
for (int i = 1990; i < 2011; i++)
|
|
41
|
for (int i = 1990; i < 2011; i++)
|
|
41
|
yearslist->addItem(QString("%1").arg(i));
|
|
42
|
yearslist->addItem(QString("%1").arg(i));
|
|
42
|
|
|
43
|
|
|
43
|
QPushButton* refreshButton = new QPushButton(tr("Refresh"));
|
|
44
|
QPushButton* refreshButton = new QPushButton(tr("Refresh"));
|
|
44
|
connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
|
|
45
|
connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
|
|
45
|
|
|
46
|
|
|
46
|
QPushButton* printButton = new QPushButton(tr("Print chart"));
|
|
47
|
QPushButton* printButton = new QPushButton(tr("Print chart"));
|
|
47
|
connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
|
|
48
|
connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
|
|
48
|
|
|
49
|
|
|
49
|
QVBoxLayout* rightPanelLayout = new QVBoxLayout;
|
|
50
|
QVBoxLayout* rightPanelLayout = new QVBoxLayout;
|
|
50
|
rightPanelLayout->addWidget(barChartRadioButton);
|
|
51
|
rightPanelLayout->addWidget(barChartRadioButton);
|
|
51
|
rightPanelLayout->addWidget(scatterChartRadioButton);
|
|
52
|
rightPanelLayout->addWidget(scatterChartRadioButton);
|
|
52
|
rightPanelLayout->addWidget(countrieslist);
|
|
53
|
rightPanelLayout->addWidget(countrieslist);
|
|
53
|
rightPanelLayout->addWidget(yearslist);
|
|
54
|
rightPanelLayout->addWidget(yearslist);
|
|
54
|
rightPanelLayout->addWidget(refreshButton);
|
|
55
|
rightPanelLayout->addWidget(refreshButton);
|
|
55
|
rightPanelLayout->addWidget(printButton);
|
|
56
|
rightPanelLayout->addWidget(printButton);
|
|
56
|
rightPanelLayout->setStretch(0, 1);
|
|
57
|
rightPanelLayout->setStretch(0, 1);
|
|
57
|
rightPanelLayout->setStretch(1, 0);
|
|
58
|
rightPanelLayout->setStretch(1, 0);
|
|
58
|
|
|
59
|
|
|
59
|
// main layout
|
|
60
|
// main layout
|
|
60
|
chartArea = new QChartView(this);
|
|
61
|
chartArea = new QChartView(this);
|
|
61
|
chartArea->setChartTitle("GDP by country");
|
|
62
|
chartArea->setChartTitle("GDP by country");
|
|
62
|
QGridLayout* mainLayout = new QGridLayout;
|
|
63
|
QGridLayout* mainLayout = new QGridLayout;
|
|
63
|
mainLayout->addWidget(chartArea, 0, 0);
|
|
64
|
mainLayout->addWidget(chartArea, 0, 0);
|
|
64
|
mainLayout->addLayout(rightPanelLayout, 0, 1);
|
|
65
|
mainLayout->addLayout(rightPanelLayout, 0, 1);
|
|
65
|
mainLayout->setColumnStretch(0,1);
|
|
66
|
mainLayout->setColumnStretch(0,1);
|
|
66
|
setLayout(mainLayout);
|
|
67
|
setLayout(mainLayout);
|
|
67
|
|
|
68
|
|
|
68
|
// connect to the database
|
|
69
|
// connect to the database
|
|
69
|
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
70
|
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
70
|
db.setDatabaseName("gdpData");
|
|
71
|
db.setDatabaseName("gdpData");
|
|
71
|
if(!db.open())
|
|
72
|
if(!db.open())
|
|
72
|
{
|
|
73
|
{
|
|
73
|
qDebug() << "could not open database. SQLite db file missing (?)";
|
|
74
|
qDebug() << "could not open database. SQLite db file missing (?)";
|
|
74
|
return;
|
|
75
|
return;
|
|
75
|
}
|
|
76
|
}
|
|
76
|
|
|
77
|
|
|
77
|
// get the list of all countires and regions.
|
|
78
|
// get the list of all countires and regions.
|
|
78
|
QSqlQuery query;
|
|
79
|
QSqlQuery query;
|
|
79
|
query.exec("SELECT DISTINCT country FROM gdp2");
|
|
80
|
query.exec("SELECT DISTINCT country FROM gdp2");
|
|
80
|
|
|
81
|
|
|
81
|
// add the countries to the country filter
|
|
82
|
// add the countries to the country filter
|
|
82
|
while (query.next()) {
|
|
83
|
while (query.next()) {
|
|
83
|
countrieslist->addItem(query.value(0).toString());
|
|
84
|
countrieslist->addItem(query.value(0).toString());
|
|
84
|
}
|
|
85
|
}
|
|
85
|
|
|
86
|
|
|
86
|
// hide axis X labels
|
|
87
|
// hide axis X labels
|
|
87
|
QChartAxis* axis = chartArea->axisX();
|
|
88
|
QChartAxis* axis = chartArea->axisX();
|
|
88
|
// axis->setLabelsVisible(false);
|
|
89
|
// axis->setLabelsVisible(false);
|
|
89
|
// newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
|
|
90
|
// newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
|
|
90
|
|
|
91
|
|
|
91
|
}
|
|
92
|
}
|
|
92
|
|
|
93
|
|
|
93
|
Widget::~Widget()
|
|
94
|
Widget::~Widget()
|
|
94
|
{
|
|
95
|
{
|
|
95
|
//
|
|
96
|
//
|
|
96
|
db.close();
|
|
97
|
db.close();
|
|
97
|
}
|
|
98
|
}
|
|
98
|
|
|
99
|
|
|
99
|
/*!
|
|
100
|
/*!
|
|
100
|
refreshes the chart
|
|
101
|
refreshes the chart
|
|
101
|
*/
|
|
102
|
*/
|
|
102
|
void Widget::refreshChart()
|
|
103
|
void Widget::refreshChart()
|
|
103
|
{
|
|
104
|
{
|
|
104
|
chartArea->removeAllSeries();
|
|
105
|
chartArea->removeAllSeries();
|
|
105
|
|
|
106
|
|
|
106
|
// selected countries items list is not sorted. copy the values to QStringlist and sort them.
|
|
107
|
// selected countries items list is not sorted. copy the values to QStringlist and sort them.
|
|
107
|
QStringList selectedCountriesStrings;
|
|
108
|
QStringList selectedCountriesStrings;
|
|
108
|
QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
|
|
109
|
QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
|
|
109
|
for (int i = 0; i < selectedCountriesItems.size(); i++)
|
|
110
|
for (int i = 0; i < selectedCountriesItems.size(); i++)
|
|
110
|
selectedCountriesStrings.append(selectedCountriesItems[i]->text());
|
|
111
|
selectedCountriesStrings.append(selectedCountriesItems[i]->text());
|
|
111
|
selectedCountriesStrings.sort();
|
|
112
|
selectedCountriesStrings.sort();
|
|
112
|
|
|
113
|
|
|
113
|
QSqlQuery query;
|
|
114
|
QSqlQuery query;
|
|
114
|
// selected years items list is not sorted. copy the values to QList<Integer> and sort them.
|
|
115
|
// selected years items list is not sorted. copy the values to QList<int> and sort them.
|
|
115
|
QList<int> selectedYearsInts;
|
|
116
|
QList<int> selectedYearsInts;
|
|
116
|
QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
|
|
117
|
QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
|
|
117
|
for (int i = 0; i < selectedYearsItems.size(); i++)
|
|
118
|
for (int i = 0; i < selectedYearsItems.size(); i++)
|
|
118
|
selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
|
|
119
|
selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
|
|
119
|
qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
|
|
120
|
qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
|
|
120
|
|
|
121
|
|
|
121
|
if (barChartRadioButton->isChecked())
|
|
122
|
if (barChartRadioButton->isChecked())
|
|
122
|
{
|
|
123
|
{
|
|
123
|
// use the sorted selected coutries list to initialize BarCategory
|
|
124
|
// use the sorted selected coutries list to initialize BarCategory
|
|
124
|
QBarCategory* category = new QBarCategory;
|
|
125
|
QBarCategory* category = new QBarCategory;
|
|
125
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
126
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
126
|
*category << selectedCountriesStrings[i];
|
|
127
|
*category << selectedCountriesStrings[i];
|
|
127
|
series0 = new QBarChartSeries(category);
|
|
128
|
QBarChartSeries* series0 = new QBarChartSeries(category);
|
|
128
|
|
|
129
|
|
|
129
|
// prepare the selected counries SQL query
|
|
130
|
// prepare the selected counries SQL query
|
|
130
|
QString countriesQuery = "country IN (";
|
|
131
|
QString countriesQuery = "country IN (";
|
|
131
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
132
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
132
|
{
|
|
133
|
{
|
|
133
|
countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
|
|
134
|
countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
|
|
134
|
if ( i < selectedCountriesStrings.size() - 1)
|
|
135
|
if ( i < selectedCountriesStrings.size() - 1)
|
|
135
|
countriesQuery.append(",");
|
|
136
|
countriesQuery.append(",");
|
|
136
|
else
|
|
137
|
else
|
|
137
|
countriesQuery.append(")");
|
|
138
|
countriesQuery.append(")");
|
|
138
|
}
|
|
139
|
}
|
|
139
|
|
|
140
|
|
|
140
|
// perform a query for each selected year
|
|
141
|
// perform a query for each selected year
|
|
141
|
for (int i = 0; i < selectedYearsInts.size(); i++)
|
|
142
|
for (int i = 0; i < selectedYearsInts.size(); i++)
|
|
142
|
{
|
|
143
|
{
|
|
143
|
query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
|
|
144
|
query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
|
|
144
|
QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
|
|
145
|
QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
|
|
145
|
|
|
146
|
|
|
146
|
// while (query.next()) {
|
|
147
|
// while (query.next()) {
|
|
147
|
// qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
|
|
148
|
// qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
|
|
148
|
// }
|
|
149
|
// }
|
|
149
|
query.first();
|
|
150
|
query.first();
|
|
150
|
|
|
151
|
|
|
151
|
// the data for some of the coutries for some years might be missing.
|
|
152
|
// the data for some of the coutries for some years might be missing.
|
|
152
|
// QBarChart needs bars to have same size
|
|
153
|
// QBarChart needs bars to have same size
|
|
153
|
for (int k = 0; k < selectedCountriesStrings.size(); k++)
|
|
154
|
for (int k = 0; k < selectedCountriesStrings.size(); k++)
|
|
154
|
{
|
|
155
|
{
|
|
155
|
if (selectedCountriesStrings[k] == query.value(0).toString())
|
|
156
|
if (selectedCountriesStrings[k] == query.value(0).toString())
|
|
156
|
{
|
|
157
|
{
|
|
157
|
*barSet << query.value(1).toReal();
|
|
158
|
*barSet << query.value(1).toReal();
|
|
158
|
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
|
|
159
|
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
|
|
159
|
query.next();
|
|
160
|
query.next();
|
|
160
|
}
|
|
161
|
}
|
|
161
|
else
|
|
162
|
else
|
|
162
|
{
|
|
163
|
{
|
|
163
|
// data missing, put 0
|
|
164
|
// data missing, put 0
|
|
164
|
*barSet << 0.0f;
|
|
165
|
*barSet << 0.0f;
|
|
165
|
qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
|
|
166
|
qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
|
|
166
|
}
|
|
167
|
}
|
|
167
|
}
|
|
168
|
}
|
|
168
|
series0->addBarSet(barSet);
|
|
169
|
series0->addBarSet(barSet);
|
|
169
|
}
|
|
170
|
}
|
|
170
|
// add the serie to the chart
|
|
171
|
// add the serie to the chart
|
|
171
|
chartArea->addSeries(series0);
|
|
172
|
chartArea->addSeries(series0);
|
|
172
|
|
|
|
|
|
173
|
}
|
|
173
|
}
|
|
174
|
else if (scatterChartRadioButton->isChecked())
|
|
174
|
else if (scatterChartRadioButton->isChecked())
|
|
175
|
{
|
|
175
|
{
|
|
176
|
QString yearsQuery = "year IN (";
|
|
176
|
QString yearsQuery = "year IN (";
|
|
177
|
for (int i = 0; i < selectedYearsInts.size(); i++)
|
|
177
|
for (int i = 0; i < selectedYearsInts.size(); i++)
|
|
178
|
{
|
|
178
|
{
|
|
179
|
yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
|
|
179
|
yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
|
|
180
|
if ( i < selectedYearsInts.size() - 1)
|
|
180
|
if ( i < selectedYearsInts.size() - 1)
|
|
181
|
yearsQuery.append(",");
|
|
181
|
yearsQuery.append(",");
|
|
182
|
else
|
|
182
|
else
|
|
183
|
yearsQuery.append(")");
|
|
183
|
yearsQuery.append(")");
|
|
184
|
}
|
|
184
|
}
|
|
185
|
|
|
185
|
|
|
186
|
// perform a query for each selected year
|
|
186
|
// perform a query for each selected country
|
|
187
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
187
|
for (int i = 0; i < selectedCountriesStrings.size(); i++)
|
|
188
|
{
|
|
188
|
{
|
|
189
|
query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
|
|
189
|
query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
|
|
190
|
query.first();
|
|
190
|
query.first();
|
|
191
|
|
|
191
|
|
|
192
|
QScatterSeries* series = new QScatterSeries;
|
|
192
|
QScatterSeries* series = new QScatterSeries;
|
|
193
|
// the data for some of the coutries for some years might be missing.
|
|
193
|
// the data for some of the coutries for some years might be missing.
|
|
194
|
for (int k = 0; k < selectedYearsInts.size(); k++)
|
|
194
|
for (int k = 0; k < selectedYearsInts.size(); k++)
|
|
195
|
{
|
|
195
|
{
|
|
196
|
if (selectedYearsInts[k] == query.value(0).toInt())
|
|
196
|
if (selectedYearsInts[k] == query.value(0).toInt())
|
|
197
|
{
|
|
197
|
{
|
|
198
|
*series << QPointF(query.value(0).toInt() , query.value(1).toReal());
|
|
198
|
*series << QPointF(query.value(0).toInt() , query.value(1).toReal());
|
|
199
|
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
|
|
199
|
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
|
|
200
|
query.next();
|
|
200
|
query.next();
|
|
201
|
}
|
|
201
|
}
|
|
202
|
else
|
|
202
|
else
|
|
203
|
{
|
|
203
|
{
|
|
204
|
// data missing, put 0
|
|
204
|
// data missing, put 0
|
|
205
|
*series << QPointF(selectedYearsInts[k] , 0.0f);
|
|
205
|
*series << QPointF(selectedYearsInts[k] , 0.0f);
|
|
206
|
qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
|
|
206
|
qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
|
|
207
|
}
|
|
207
|
}
|
|
208
|
}
|
|
208
|
}
|
|
209
|
// chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
|
|
209
|
// chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
|
|
210
|
chartArea->addSeries(series);
|
|
210
|
chartArea->addSeries(series);
|
|
211
|
}
|
|
211
|
}
|
|
212
|
chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
|
|
212
|
chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
|
|
213
|
}
|
|
213
|
}
|
|
214
|
}
|
|
214
|
}
|
|
215
|
|
|
215
|
|
|
216
|
void Widget::printChart()
|
|
216
|
void Widget::printChart()
|
|
217
|
{
|
|
217
|
{
|
|
218
|
QPrinter printer;
|
|
218
|
QPrinter printer;
|
|
219
|
// QPrinter printer(QPrinter::HighResolution);
|
|
219
|
// QPrinter printer(QPrinter::HighResolution);
|
|
220
|
printer.setOutputFormat(QPrinter::PdfFormat);
|
|
220
|
printer.setOutputFormat(QPrinter::PdfFormat);
|
|
221
|
printer.setOrientation(QPrinter::Landscape);
|
|
221
|
printer.setOrientation(QPrinter::Landscape);
|
|
222
|
printer.setOutputFileName("print.pdf");
|
|
222
|
printer.setOutputFileName("print.pdf");
|
|
223
|
|
|
223
|
|
|
224
|
QPainter painter;
|
|
224
|
QPainter painter;
|
|
225
|
painter.begin(&printer);
|
|
225
|
painter.begin(&printer);
|
|
226
|
chartArea->render(&painter);
|
|
226
|
chartArea->render(&painter);
|
|
227
|
}
|
|
227
|
}
|