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