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