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