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