##// END OF EJS Templates
Copy database to bin directory
Marek Rosa -
r242:2883e9a758d6
parent child
Show More
@@ -1,23 +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
25 COPY_DATABASE_COMMAND = "cp gdpData $$CHART_BUILD_BIN_DIR/gdpData"
26
27 win32:{
28 COPY_DATABASE_COMMAND = $$replace(COPY_DATABASE_COMMAND, "/","\\")
29 }
30
31 QMAKE_POST_LINK = $$COPY_DATABASE_COMMAND
@@ -1,155 +1,155
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, 800, 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("../example/gdpbarchart/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 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
91 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
92 QStringList selectedCountriesStrings;
92 QStringList selectedCountriesStrings;
93 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
93 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
94 for (int i = 0; i < selectedCountriesItems.size(); i++)
94 for (int i = 0; i < selectedCountriesItems.size(); i++)
95 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
95 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
96 selectedCountriesStrings.sort();
96 selectedCountriesStrings.sort();
97
97
98 // use the sorted selected coutries list to initialize BarCategory
98 // use the sorted selected coutries list to initialize BarCategory
99 QBarCategory* category = new QBarCategory;
99 QBarCategory* category = new QBarCategory;
100 for (int i = 0; i < selectedCountriesStrings.size(); i++)
100 for (int i = 0; i < selectedCountriesStrings.size(); i++)
101 *category << selectedCountriesStrings[i];
101 *category << selectedCountriesStrings[i];
102 QBarChartSeries* series0 = new QBarChartSeries(category);
102 QBarChartSeries* series0 = new QBarChartSeries(category);
103
103
104 // prepare the selected counries SQL query
104 // prepare the selected counries SQL query
105 QString countriesQuery = "country IN (";
105 QString countriesQuery = "country IN (";
106 for (int i = 0; i < selectedCountriesStrings.size(); i++)
106 for (int i = 0; i < selectedCountriesStrings.size(); i++)
107 {
107 {
108 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
108 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
109 if ( i < selectedCountriesStrings.size() - 1)
109 if ( i < selectedCountriesStrings.size() - 1)
110 countriesQuery.append(",");
110 countriesQuery.append(",");
111 else
111 else
112 countriesQuery.append(")");
112 countriesQuery.append(")");
113 }
113 }
114
114
115 QSqlQuery query;
115 QSqlQuery query;
116 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
116 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
117 QList<int> selectedYearsInts;
117 QList<int> selectedYearsInts;
118 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
118 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
119 for (int i = 0; i < selectedYearsItems.size(); i++)
119 for (int i = 0; i < selectedYearsItems.size(); i++)
120 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
120 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
121 qSort(selectedYearsInts);
121 qSort(selectedYearsInts);
122
122
123 // perform a query for each selected year
123 // perform a query for each selected year
124 for (int i = 0; i < selectedYearsInts.size(); i++)
124 for (int i = 0; i < selectedYearsInts.size(); i++)
125 {
125 {
126 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
126 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
127 QBarSet* barSet = new QBarSet;
127 QBarSet* barSet = new QBarSet;
128 // while (query.next()) {
128 // while (query.next()) {
129 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
129 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
130 // }
130 // }
131 query.first();
131 query.first();
132
132
133 // the data for some of the coutries for some years might be missing.
133 // the data for some of the coutries for some years might be missing.
134 // QBarChart needs bars to have same size
134 // QBarChart needs bars to have same size
135 for (int k = 0; k < selectedCountriesStrings.size(); k++)
135 for (int k = 0; k < selectedCountriesStrings.size(); k++)
136 {
136 {
137 if (selectedCountriesStrings[k] == query.value(0).toString())
137 if (selectedCountriesStrings[k] == query.value(0).toString())
138 {
138 {
139 *barSet << query.value(1).toReal();
139 *barSet << query.value(1).toReal();
140 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
140 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
141 query.next();
141 query.next();
142 }
142 }
143 else
143 else
144 {
144 {
145 // data missing, put 0
145 // data missing, put 0
146 *barSet << 0.0f;
146 *barSet << 0.0f;
147 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
147 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
148 }
148 }
149 }
149 }
150 series0->addBarSet(barSet);
150 series0->addBarSet(barSet);
151 }
151 }
152
152
153 // add the serie to the chart
153 // add the serie to the chart
154 chartArea->addSeries(series0);
154 chartArea->addSeries(series0);
155 }
155 }
General Comments 0
You need to be logged in to leave comments. Login now