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