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