##// END OF EJS Templates
A few more warnings removed from examples
Tero Ahola -
r613:36ecec1e9bf9
parent child
Show More
@@ -1,230 +1,230
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 #include <qbarseries.h>
23
23
24 QTCOMMERCIALCHART_USE_NAMESPACE
24 QTCOMMERCIALCHART_USE_NAMESPACE
25
25
26 Widget::Widget(QWidget *parent)
26 Widget::Widget(QWidget *parent)
27 : QWidget(parent)
27 : QWidget(parent)
28 {
28 {
29 setGeometry(100, 100, 1000, 600);
29 setGeometry(100, 100, 1000, 600);
30
30
31 // right panel layout
31 // right panel layout
32 barChartRadioButton = new QRadioButton(tr("Bar chart"));
32 barChartRadioButton = new QRadioButton(tr("Bar chart"));
33 barChartRadioButton->setChecked(true);
33 barChartRadioButton->setChecked(true);
34 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
34 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
35 scatterChartRadioButton->setChecked(false);
35 scatterChartRadioButton->setChecked(false);
36 countrieslist = new QListWidget;
36 countrieslist = new QListWidget;
37 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
37 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
38
38
39 //list of years widget
39 //list of years widget
40 yearslist = new QListWidget;
40 yearslist = new QListWidget;
41 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
41 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
42 for (int i = 1990; i < 2011; i++)
42 for (int i = 1990; i < 2011; i++)
43 yearslist->addItem(QString("%1").arg(i));
43 yearslist->addItem(QString("%1").arg(i));
44
44
45 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
45 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
46 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
46 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
47
47
48 QPushButton* printButton = new QPushButton(tr("Print chart"));
48 QPushButton* printButton = new QPushButton(tr("Print chart"));
49 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
49 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
50
50
51 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
51 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
52 rightPanelLayout->addWidget(barChartRadioButton);
52 rightPanelLayout->addWidget(barChartRadioButton);
53 rightPanelLayout->addWidget(scatterChartRadioButton);
53 rightPanelLayout->addWidget(scatterChartRadioButton);
54 rightPanelLayout->addWidget(countrieslist);
54 rightPanelLayout->addWidget(countrieslist);
55 rightPanelLayout->addWidget(yearslist);
55 rightPanelLayout->addWidget(yearslist);
56 rightPanelLayout->addWidget(refreshButton);
56 rightPanelLayout->addWidget(refreshButton);
57 rightPanelLayout->addWidget(printButton);
57 rightPanelLayout->addWidget(printButton);
58 rightPanelLayout->setStretch(0, 1);
58 rightPanelLayout->setStretch(0, 1);
59 rightPanelLayout->setStretch(1, 0);
59 rightPanelLayout->setStretch(1, 0);
60
60
61 // main layout
61 // main layout
62 chartArea = new QChartView(this);
62 chartArea = new QChartView(this);
63 chartArea->setChartTitle("GDP by country");
63 chartArea->setChartTitle("GDP by country");
64 QGridLayout* mainLayout = new QGridLayout;
64 QGridLayout* mainLayout = new QGridLayout;
65 mainLayout->addWidget(chartArea, 0, 0);
65 mainLayout->addWidget(chartArea, 0, 0);
66 mainLayout->addLayout(rightPanelLayout, 0, 1);
66 mainLayout->addLayout(rightPanelLayout, 0, 1);
67 mainLayout->setColumnStretch(0,1);
67 mainLayout->setColumnStretch(0,1);
68 setLayout(mainLayout);
68 setLayout(mainLayout);
69
69
70 // connect to the database
70 // connect to the database
71 db = QSqlDatabase::addDatabase("QSQLITE");
71 db = QSqlDatabase::addDatabase("QSQLITE");
72 db.setDatabaseName("gdpData");
72 db.setDatabaseName("gdpData");
73 if(!db.open())
73 if(!db.open())
74 {
74 {
75 qDebug() << "could not open database. SQLite db file missing (?)";
75 qDebug() << "could not open database. SQLite db file missing (?)";
76 return;
76 return;
77 }
77 }
78
78
79 // get the list of all countires and regions.
79 // get the list of all countires and regions.
80 QSqlQuery query;
80 QSqlQuery query;
81 query.exec("SELECT DISTINCT country FROM gdp2");
81 query.exec("SELECT DISTINCT country FROM gdp2");
82
82
83 // add the countries to the country filter
83 // add the countries to the country filter
84 while (query.next()) {
84 while (query.next()) {
85 countrieslist->addItem(query.value(0).toString());
85 countrieslist->addItem(query.value(0).toString());
86 }
86 }
87
87
88 // hide axis X labels
88 // hide axis X labels
89 QChartAxis* axis = chartArea->axisX();
89 //QChartAxis* axis = chartArea->axisX();
90 // axis->
90 // axis->
91 // axis->setLabelsVisible(false);
91 // axis->setLabelsVisible(false);
92 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
92 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
93
93
94 }
94 }
95
95
96 Widget::~Widget()
96 Widget::~Widget()
97 {
97 {
98 //
98 //
99 db.close();
99 db.close();
100 }
100 }
101
101
102 /*!
102 /*!
103 refreshes the chart
103 refreshes the chart
104 */
104 */
105 void Widget::refreshChart()
105 void Widget::refreshChart()
106 {
106 {
107 chartArea->removeAllSeries();
107 chartArea->removeAllSeries();
108
108
109 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
109 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
110 QStringList selectedCountriesStrings;
110 QStringList selectedCountriesStrings;
111 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
111 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
112 for (int i = 0; i < selectedCountriesItems.size(); i++)
112 for (int i = 0; i < selectedCountriesItems.size(); i++)
113 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
113 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
114 selectedCountriesStrings.sort();
114 selectedCountriesStrings.sort();
115
115
116 QSqlQuery query;
116 QSqlQuery query;
117 // selected years items list is not sorted. copy the values to QList<int> and sort them.
117 // selected years items list is not sorted. copy the values to QList<int> and sort them.
118 QList<int> selectedYearsInts;
118 QList<int> selectedYearsInts;
119 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
119 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
120 for (int i = 0; i < selectedYearsItems.size(); i++)
120 for (int i = 0; i < selectedYearsItems.size(); i++)
121 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
121 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
122 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
122 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
123
123
124 if (barChartRadioButton->isChecked())
124 if (barChartRadioButton->isChecked())
125 {
125 {
126 // use the sorted selected coutries list to initialize BarCategory
126 // use the sorted selected coutries list to initialize BarCategory
127 QStringList category;
127 QStringList category;
128 for (int i = 0; i < selectedCountriesStrings.size(); i++)
128 for (int i = 0; i < selectedCountriesStrings.size(); i++)
129 category << selectedCountriesStrings[i];
129 category << selectedCountriesStrings[i];
130 QBarSeries* series0 = new QBarSeries(category);
130 QBarSeries* series0 = new QBarSeries(category);
131 series0 = new QBarSeries(category);
131 series0 = new QBarSeries(category);
132
132
133 // prepare the selected counries SQL query
133 // prepare the selected counries SQL query
134 QString countriesQuery = "country IN (";
134 QString countriesQuery = "country IN (";
135 for (int i = 0; i < selectedCountriesStrings.size(); i++)
135 for (int i = 0; i < selectedCountriesStrings.size(); i++)
136 {
136 {
137 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
137 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
138 if ( i < selectedCountriesStrings.size() - 1)
138 if ( i < selectedCountriesStrings.size() - 1)
139 countriesQuery.append(",");
139 countriesQuery.append(",");
140 else
140 else
141 countriesQuery.append(")");
141 countriesQuery.append(")");
142 }
142 }
143
143
144 // perform a query for each selected year
144 // perform a query for each selected year
145 for (int i = 0; i < selectedYearsInts.size(); i++)
145 for (int i = 0; i < selectedYearsInts.size(); i++)
146 {
146 {
147 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
147 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
148 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
148 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
149
149
150 // while (query.next()) {
150 // while (query.next()) {
151 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
151 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
152 // }
152 // }
153 query.first();
153 query.first();
154
154
155 // the data for some of the coutries for some years might be missing.
155 // the data for some of the coutries for some years might be missing.
156 // QBarChart needs bars to have same size
156 // QBarChart needs bars to have same size
157 for (int k = 0; k < selectedCountriesStrings.size(); k++)
157 for (int k = 0; k < selectedCountriesStrings.size(); k++)
158 {
158 {
159 if (selectedCountriesStrings[k] == query.value(0).toString())
159 if (selectedCountriesStrings[k] == query.value(0).toString())
160 {
160 {
161 *barSet << query.value(1).toReal();
161 *barSet << query.value(1).toReal();
162 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
162 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
163 query.next();
163 query.next();
164 }
164 }
165 else
165 else
166 {
166 {
167 // data missing, put 0
167 // data missing, put 0
168 *barSet << 0.0f;
168 *barSet << 0.0f;
169 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
169 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
170 }
170 }
171 }
171 }
172 series0->addBarSet(barSet);
172 series0->addBarSet(barSet);
173 }
173 }
174 // add the serie to the chart
174 // add the serie to the chart
175 chartArea->addSeries(series0);
175 chartArea->addSeries(series0);
176 }
176 }
177 else if (scatterChartRadioButton->isChecked())
177 else if (scatterChartRadioButton->isChecked())
178 {
178 {
179 QString yearsQuery = "year IN (";
179 QString yearsQuery = "year IN (";
180 for (int i = 0; i < selectedYearsInts.size(); i++)
180 for (int i = 0; i < selectedYearsInts.size(); i++)
181 {
181 {
182 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
182 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
183 if ( i < selectedYearsInts.size() - 1)
183 if ( i < selectedYearsInts.size() - 1)
184 yearsQuery.append(",");
184 yearsQuery.append(",");
185 else
185 else
186 yearsQuery.append(")");
186 yearsQuery.append(")");
187 }
187 }
188
188
189 // perform a query for each selected country
189 // perform a query for each selected country
190 for (int i = 0; i < selectedCountriesStrings.size(); i++)
190 for (int i = 0; i < selectedCountriesStrings.size(); i++)
191 {
191 {
192 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
192 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
193 query.first();
193 query.first();
194
194
195 QScatterSeries* series = new QScatterSeries;
195 QScatterSeries* series = new QScatterSeries;
196 // the data for some of the coutries for some years might be missing.
196 // the data for some of the coutries for some years might be missing.
197 for (int k = 0; k < selectedYearsInts.size(); k++)
197 for (int k = 0; k < selectedYearsInts.size(); k++)
198 {
198 {
199 if (selectedYearsInts[k] == query.value(0).toInt())
199 if (selectedYearsInts[k] == query.value(0).toInt())
200 {
200 {
201 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
201 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
202 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
202 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
203 query.next();
203 query.next();
204 }
204 }
205 else
205 else
206 {
206 {
207 // data missing, put 0
207 // data missing, put 0
208 *series << QPointF(selectedYearsInts[k] , 0.0f);
208 *series << QPointF(selectedYearsInts[k] , 0.0f);
209 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
209 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
210 }
210 }
211 }
211 }
212 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
212 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
213 chartArea->addSeries(series);
213 chartArea->addSeries(series);
214 }
214 }
215 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
215 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
216 }
216 }
217 }
217 }
218
218
219 void Widget::printChart()
219 void Widget::printChart()
220 {
220 {
221 QPrinter printer;
221 QPrinter printer;
222 // QPrinter printer(QPrinter::HighResolution);
222 // QPrinter printer(QPrinter::HighResolution);
223 printer.setOutputFormat(QPrinter::PdfFormat);
223 printer.setOutputFormat(QPrinter::PdfFormat);
224 printer.setOrientation(QPrinter::Landscape);
224 printer.setOrientation(QPrinter::Landscape);
225 printer.setOutputFileName("print.pdf");
225 printer.setOutputFileName("print.pdf");
226
226
227 QPainter painter;
227 QPainter painter;
228 painter.begin(&printer);
228 painter.begin(&printer);
229 chartArea->render(&painter);
229 chartArea->render(&painter);
230 }
230 }
@@ -1,149 +1,150
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qstackedbarseries.h>
5 #include <qstackedbarseries.h>
6 #include <qbarset.h>
6 #include <qbarset.h>
7 #include <qchartaxis.h>
7 #include <qchartaxis.h>
8 #include <qlegend.h>
8 #include <qlegend.h>
9 #include <QStringList>
9 #include <QStringList>
10 #include <QDebug>
10 #include <QDebug>
11
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
13
14 //! [1]
14 //! [1]
15 class DrilldownBarSeries : public QStackedBarSeries
15 class DrilldownBarSeries : public QStackedBarSeries
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18 public:
18 public:
19 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
19 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
20
20
21 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
21 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
22 {
22 {
23 mDrilldownSeries[category] = drilldownSeries;
23 mDrilldownSeries[category] = drilldownSeries;
24 }
24 }
25
25
26 DrilldownBarSeries* drilldownSeries(QString category)
26 DrilldownBarSeries* drilldownSeries(QString category)
27 {
27 {
28 return mDrilldownSeries[category];
28 return mDrilldownSeries[category];
29 }
29 }
30
30
31 public Q_SLOTS:
31 public Q_SLOTS:
32
32
33 private:
33 private:
34
34
35 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
35 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
36 };
36 };
37 //! [1]
37 //! [1]
38
38
39 //! [2]
39 //! [2]
40 class DrilldownChart : public QChartView
40 class DrilldownChart : public QChartView
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 public:
43 public:
44 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
44 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
45
45
46 void changeSeries(QSeries* series)
46 void changeSeries(QSeries* series)
47 {
47 {
48 if (m_currentSeries)
48 if (m_currentSeries)
49 removeSeries(m_currentSeries);
49 removeSeries(m_currentSeries);
50 m_currentSeries = series;
50 m_currentSeries = series;
51 addSeries(series);
51 addSeries(series);
52 setChartTitle(series->title());
52 setChartTitle(series->title());
53 }
53 }
54
54
55 public Q_SLOTS:
55 public Q_SLOTS:
56 void handleRightClick(QBarSet *barset, QString category)
56 void handleRightClick(QBarSet *barset, QString category)
57 {
57 {
58 Q_UNUSED(barset)
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
59 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
59 changeSeries(series->drilldownSeries(category));
60 changeSeries(series->drilldownSeries(category));
60 }
61 }
61
62
62 private:
63 private:
63 QSeries* m_currentSeries;
64 QSeries* m_currentSeries;
64 };
65 };
65 //! [2]
66 //! [2]
66
67
67 int main(int argc, char *argv[])
68 int main(int argc, char *argv[])
68 {
69 {
69 QApplication a(argc, argv);
70 QApplication a(argc, argv);
70 QMainWindow window;
71 QMainWindow window;
71
72
72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
73 DrilldownChart* drilldownChart = new DrilldownChart(&window);
73 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
74 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
74
75
75 //! [3]
76 //! [3]
76 // Define categories
77 // Define categories
77 QStringList months;
78 QStringList months;
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
79 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
79 QStringList weeks;
80 QStringList weeks;
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
81 weeks << "week 1" << "week 2" << "week 3" << "week 4";
81 QStringList plants;
82 QStringList plants;
82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
83 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
83 //! [3]
84 //! [3]
84
85
85 //! [4]
86 //! [4]
86 // Create drilldown structure
87 // Create drilldown structure
87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
88 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
88 seasonSeries->setTitle("Crop by month - Season");
89 seasonSeries->setTitle("Crop by month - Season");
89
90
90 // Each month in season series has drilldown series for weekly data
91 // Each month in season series has drilldown series for weekly data
91 foreach (QString month, months) {
92 foreach (QString month, months) {
92
93
93 // Create drilldown series for every week
94 // Create drilldown series for every week
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
95 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
96 seasonSeries->mapDrilldownSeries(month, weeklySeries);
96
97
97 // Drilling down from weekly data brings us back to season data.
98 // Drilling down from weekly data brings us back to season data.
98 foreach (QString week, weeks) {
99 foreach (QString week, weeks) {
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
100 weeklySeries->mapDrilldownSeries(week, seasonSeries);
100 weeklySeries->setTitle(QString("Crop by week - " + month));
101 weeklySeries->setTitle(QString("Crop by week - " + month));
101 }
102 }
102
103
103 // Use right click signal to implement drilldown
104 // Use right click signal to implement drilldown
104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
105 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
105 }
106 }
106
107
107 // Enable drilldown from season series using right click.
108 // Enable drilldown from season series using right click.
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
109 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
109 //! [4]
110 //! [4]
110
111
111 //! [5]
112 //! [5]
112 // Fill monthly and weekly series with data
113 // Fill monthly and weekly series with data
113 foreach (QString plant, plants) {
114 foreach (QString plant, plants) {
114 QBarSet* monthlyCrop = new QBarSet(plant);
115 QBarSet* monthlyCrop = new QBarSet(plant);
115 foreach (QString month, months) {
116 foreach (QString month, months) {
116 QBarSet* weeklyCrop = new QBarSet(plant);
117 QBarSet* weeklyCrop = new QBarSet(plant);
117 foreach (QString week, weeks ) {
118 foreach (QString week, weeks ) {
118 *weeklyCrop << (qrand() % 20);
119 *weeklyCrop << (qrand() % 20);
119 }
120 }
120 // Get the drilldown series from season series and add crop to it.
121 // Get the drilldown series from season series and add crop to it.
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
122 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
123 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
123 *monthlyCrop << weeklyCrop->total();
124 *monthlyCrop << weeklyCrop->total();
124
125
125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
126 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
126 }
127 }
127 seasonSeries->addBarSet(monthlyCrop);
128 seasonSeries->addBarSet(monthlyCrop);
128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
129 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
129 }
130 }
130 //! [5]
131 //! [5]
131
132
132 seasonSeries->setToolTipEnabled(true);
133 seasonSeries->setToolTipEnabled(true);
133
134
134 //! [6]
135 //! [6]
135 // Show season series in initial view
136 // Show season series in initial view
136 drilldownChart->changeSeries(seasonSeries);
137 drilldownChart->changeSeries(seasonSeries);
137 drilldownChart->setChartTitle(seasonSeries->title());
138 drilldownChart->setChartTitle(seasonSeries->title());
138 //! [6]
139 //! [6]
139
140
140 drilldownChart->axisX()->setGridLineVisible(false);
141 drilldownChart->axisX()->setGridLineVisible(false);
141
142
142 window.setCentralWidget(drilldownChart);
143 window.setCentralWidget(drilldownChart);
143 window.resize(400, 300);
144 window.resize(400, 300);
144 window.show();
145 window.show();
145
146
146 return a.exec();
147 return a.exec();
147 }
148 }
148
149
149 #include "main.moc"
150 #include "main.moc"
@@ -1,146 +1,151
1 #include "customtablemodel.h"
1 #include "customtablemodel.h"
2
2
3 CustomTableModel::CustomTableModel(QObject *parent) :
3 CustomTableModel::CustomTableModel(QObject *parent) :
4 QAbstractTableModel(parent)
4 QAbstractTableModel(parent)
5 {
5 {
6 m_points.append(QPointF(10, 50));
6 m_points.append(QPointF(10, 50));
7 m_labels.append("Apples");
7 m_labels.append("Apples");
8 m_points.append(QPointF(60, 70));
8 m_points.append(QPointF(60, 70));
9 m_labels.append("Oranges");
9 m_labels.append("Oranges");
10 m_points.append(QPointF(110, 50));
10 m_points.append(QPointF(110, 50));
11 m_labels.append("Bananas");
11 m_labels.append("Bananas");
12 m_points.append(QPointF(140, 40));
12 m_points.append(QPointF(140, 40));
13 m_labels.append("Lemons");
13 m_labels.append("Lemons");
14 m_points.append(QPointF(200, 150));
14 m_points.append(QPointF(200, 150));
15 m_labels.append("Plums");
15 m_labels.append("Plums");
16 m_points.append(QPointF(225, 75));
16 m_points.append(QPointF(225, 75));
17 m_labels.append("Pearls");
17 m_labels.append("Pearls");
18 }
18 }
19
19
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
21 {
21 {
22 Q_UNUSED(parent)
22 return m_points.count();
23 return m_points.count();
23 }
24 }
24
25
25 int CustomTableModel::columnCount(const QModelIndex & parent) const
26 int CustomTableModel::columnCount(const QModelIndex & parent) const
26 {
27 {
28 Q_UNUSED(parent)
27 return 3;
29 return 3;
28 }
30 }
29
31
30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
32 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
31 {
33 {
32 if (role != Qt::DisplayRole)
34 if (role != Qt::DisplayRole)
33 return QVariant();
35 return QVariant();
34
36
35 if (orientation == Qt::Horizontal)
37 if (orientation == Qt::Horizontal)
36 {
38 {
37 switch(section)
39 switch(section)
38 {
40 {
39 case 0:
41 case 0:
40 return "x";
42 return "x";
41 case 1:
43 case 1:
42 return "y";
44 return "y";
43 case 2:
45 case 2:
44 return "Fruit";
46 return "Fruit";
45 default:
47 default:
46 return "What?";
48 return "What?";
47 }
49 }
48 }
50 }
49 else
51 else
50 return QString("%1").arg(section + 1);
52 return QString("%1").arg(section + 1);
51 }
53 }
52
54
53 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
55 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
54 {
56 {
55 if (role == Qt::DisplayRole)
57 if (role == Qt::DisplayRole)
56 {
58 {
57 switch(index.column())
59 switch(index.column())
58 {
60 {
59 case 0:
61 case 0:
60 return m_points[index.row()].x();
62 return m_points[index.row()].x();
61 case 1:
63 case 1:
62 return m_points[index.row()].y();
64 return m_points[index.row()].y();
63 case 2:
65 case 2:
64 return m_labels[index.row()];
66 return m_labels[index.row()];
65 default:
67 default:
66 return QVariant();
68 break;
67 }
69 }
68 }
70 }
69 else if (role == Qt::EditRole)
71 else if (role == Qt::EditRole)
70 {
72 {
71 switch(index.column())
73 switch(index.column())
72 {
74 {
73 case 0:
75 case 0:
74 return m_points[index.row()].x();
76 return m_points[index.row()].x();
75 case 1:
77 case 1:
76 return m_points[index.row()].y();
78 return m_points[index.row()].y();
77 case 2:
79 case 2:
78 return m_labels[index.row()];
80 return m_labels[index.row()];
79 default:
81 default:
80 return QVariant();
82 break;
81 }
83 }
82 }
84 }
85 return QVariant();
83 }
86 }
84
87
85 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
88 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
86 {
89 {
87 if (index.isValid() && role == Qt::EditRole)
90 if (index.isValid() && role == Qt::EditRole)
88 {
91 {
89 switch(index.column())
92 switch(index.column())
90 {
93 {
91 case 0:
94 case 0:
92 m_points[index.row()].setX(value.toDouble());
95 m_points[index.row()].setX(value.toDouble());
93 break;
96 break;
94 case 1:
97 case 1:
95 m_points[index.row()].setY(value.toDouble());
98 m_points[index.row()].setY(value.toDouble());
96 break;
99 break;
97 case 2:
100 case 2:
98 m_labels.replace(index.row(), value.toString());
101 m_labels.replace(index.row(), value.toString());
99 break;
102 break;
100 default:
103 default:
101 return false;
104 return false;
102 }
105 }
103 emit dataChanged(index, index);
106 emit dataChanged(index, index);
104 return true;
107 return true;
105 }
108 }
106 return false;
109 return false;
107 }
110 }
108
111
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
112 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
110 {
113 {
111 // if (!index.isValid())
114 // if (!index.isValid())
112 // return Qt::ItemIsEnabled;
115 // return Qt::ItemIsEnabled;
113 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
116 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
114 }
117 }
115
118
116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
119 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
117 {
120 {
121 Q_UNUSED(parent)
122
118 if (row < 0)
123 if (row < 0)
119 row = 0;
124 row = 0;
120 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
125 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
121 for (int i = row; i < row + count; i++)
126 for (int i = row; i < row + count; i++)
122 {
127 {
123 m_points.insert(row, QPointF(10,20));
128 m_points.insert(row, QPointF(10,20));
124 m_labels.insert(row,("a"));
129 m_labels.insert(row,("a"));
125 }
130 }
126 endInsertRows();
131 endInsertRows();
127 return true;
132 return true;
128 }
133 }
129
134
130 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
135 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
131 {
136 {
132 if (row > this->rowCount() - 1)
137 if (row > this->rowCount() - 1)
133 return false;
138 return false;
134 if (row < 0)
139 if (row < 0)
135 row = 0;
140 row = 0;
136 if (row + count > rowCount())
141 if (row + count > rowCount())
137 return false;
142 return false;
138 beginRemoveRows(parent, row, row + count - 1);
143 beginRemoveRows(parent, row, row + count - 1);
139 for (int i = row; i < row + count; i++)
144 for (int i = row; i < row + count; i++)
140 {
145 {
141 m_points.removeAt(row);
146 m_points.removeAt(row);
142 m_labels.removeAt(row);
147 m_labels.removeAt(row);
143 }
148 }
144 endRemoveRows();
149 endRemoveRows();
145 return true;
150 return true;
146 }
151 }
General Comments 0
You need to be logged in to leave comments. Login now