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