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