##// END OF EJS Templates
else clause added to QChartView mousePressEvent. Added some more docs to QChart and QChartView
Marek Rosa -
r285:46d1e061b6ff
parent child
Show More
@@ -1,225 +1,226
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 <qbarcategory.h>
16 #include <qbarcategory.h>
17 #include <qbarset.h>
17 #include <qbarset.h>
18 #include <QListWidget>
18 #include <QListWidget>
19 #include <QPrinter>
19 #include <QPrinter>
20 #include <QPrintDialog>
20 #include <QPrintDialog>
21 #include <QRadioButton>
21 #include <QRadioButton>
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 Widget::Widget(QWidget *parent)
25 Widget::Widget(QWidget *parent)
26 : QWidget(parent)
26 : QWidget(parent)
27 {
27 {
28 setGeometry(100, 100, 1000, 600);
28 setGeometry(100, 100, 1000, 600);
29
29
30 // right panel layout
30 // right panel layout
31 barChartRadioButton = new QRadioButton(tr("Bar chart"));
31 barChartRadioButton = new QRadioButton(tr("Bar chart"));
32 barChartRadioButton->setChecked(true);
32 barChartRadioButton->setChecked(true);
33 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
33 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
34 scatterChartRadioButton->setChecked(false);
34 scatterChartRadioButton->setChecked(false);
35 countrieslist = new QListWidget;
35 countrieslist = new QListWidget;
36 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
36 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
37
37
38 yearslist = new QListWidget;
38 yearslist = new QListWidget;
39 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
39 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
40 for (int i = 1990; i < 2011; i++)
40 for (int i = 1990; i < 2011; i++)
41 yearslist->addItem(QString("%1").arg(i));
41 yearslist->addItem(QString("%1").arg(i));
42
42
43 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
43 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
44 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
44 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
45
45
46 QPushButton* printButton = new QPushButton(tr("Print chart"));
46 QPushButton* printButton = new QPushButton(tr("Print chart"));
47 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
47 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
48
48
49 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
49 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
50 rightPanelLayout->addWidget(barChartRadioButton);
50 rightPanelLayout->addWidget(barChartRadioButton);
51 rightPanelLayout->addWidget(scatterChartRadioButton);
51 rightPanelLayout->addWidget(scatterChartRadioButton);
52 rightPanelLayout->addWidget(countrieslist);
52 rightPanelLayout->addWidget(countrieslist);
53 rightPanelLayout->addWidget(yearslist);
53 rightPanelLayout->addWidget(yearslist);
54 rightPanelLayout->addWidget(refreshButton);
54 rightPanelLayout->addWidget(refreshButton);
55 rightPanelLayout->addWidget(printButton);
55 rightPanelLayout->addWidget(printButton);
56 rightPanelLayout->setStretch(0, 1);
56 rightPanelLayout->setStretch(0, 1);
57 rightPanelLayout->setStretch(1, 0);
57 rightPanelLayout->setStretch(1, 0);
58
58
59 // main layout
59 // main layout
60 chartArea = new QChartView(this);
60 chartArea = new QChartView(this);
61 chartArea->setChartTitle("GDP by country");
61 chartArea->setChartTitle("GDP by country");
62 QGridLayout* mainLayout = new QGridLayout;
62 QGridLayout* mainLayout = new QGridLayout;
63 mainLayout->addWidget(chartArea, 0, 0);
63 mainLayout->addWidget(chartArea, 0, 0);
64 mainLayout->addLayout(rightPanelLayout, 0, 1);
64 mainLayout->addLayout(rightPanelLayout, 0, 1);
65 mainLayout->setColumnStretch(0,1);
65 mainLayout->setColumnStretch(0,1);
66 setLayout(mainLayout);
66 setLayout(mainLayout);
67
67
68 // connect to the database
68 // connect to the database
69 db = QSqlDatabase::addDatabase("QSQLITE");
69 db = QSqlDatabase::addDatabase("QSQLITE");
70 db.setDatabaseName("gdpData");
70 db.setDatabaseName("gdpData");
71 if(!db.open())
71 if(!db.open())
72 {
72 {
73 qDebug() << "could not open database. SQLite db file missing (?)";
73 qDebug() << "could not open database. SQLite db file missing (?)";
74 return;
74 return;
75 }
75 }
76
76
77 // get the list of all countires and regions.
77 // get the list of all countires and regions.
78 QSqlQuery query;
78 QSqlQuery query;
79 query.exec("SELECT DISTINCT country FROM gdp2");
79 query.exec("SELECT DISTINCT country FROM gdp2");
80
80
81 // add the countries to the country filter
81 // add the countries to the country filter
82 while (query.next()) {
82 while (query.next()) {
83 countrieslist->addItem(query.value(0).toString());
83 countrieslist->addItem(query.value(0).toString());
84 }
84 }
85
85
86 // hide axis X labels
86 // hide axis X labels
87 QChartAxis* axis = chartArea->axisX();
87 QChartAxis* axis = chartArea->axisX();
88 // axis->setLabelsVisible(false);
88 // axis->setLabelsVisible(false);
89 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
89 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
90
90
91 }
91 }
92
92
93 Widget::~Widget()
93 Widget::~Widget()
94 {
94 {
95 //
95 //
96 db.close();
96 db.close();
97 }
97 }
98
98
99 /*!
99 /*!
100 refreshes the chart
100 refreshes the chart
101 */
101 */
102 void Widget::refreshChart()
102 void Widget::refreshChart()
103 {
103 {
104 chartArea->removeSeries(series0);
104 chartArea->removeAllSeries();
105
105
106 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
106 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
107 QStringList selectedCountriesStrings;
107 QStringList selectedCountriesStrings;
108 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
108 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
109 for (int i = 0; i < selectedCountriesItems.size(); i++)
109 for (int i = 0; i < selectedCountriesItems.size(); i++)
110 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
110 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
111 selectedCountriesStrings.sort();
111 selectedCountriesStrings.sort();
112
112
113 QSqlQuery query;
113 QSqlQuery query;
114 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
114 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
115 QList<int> selectedYearsInts;
115 QList<int> selectedYearsInts;
116 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
116 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
117 for (int i = 0; i < selectedYearsItems.size(); i++)
117 for (int i = 0; i < selectedYearsItems.size(); i++)
118 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
118 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
119 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
119 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
120
120
121 if (barChartRadioButton->isChecked())
121 if (barChartRadioButton->isChecked())
122 {
122 {
123 // use the sorted selected coutries list to initialize BarCategory
123 // use the sorted selected coutries list to initialize BarCategory
124 QBarCategory* category = new QBarCategory;
124 QBarCategory* category = new QBarCategory;
125 for (int i = 0; i < selectedCountriesStrings.size(); i++)
125 for (int i = 0; i < selectedCountriesStrings.size(); i++)
126 *category << selectedCountriesStrings[i];
126 *category << selectedCountriesStrings[i];
127 series0 = new QBarChartSeries(category);
127 series0 = new QBarChartSeries(category);
128
128
129 // prepare the selected counries SQL query
129 // prepare the selected counries SQL query
130 QString countriesQuery = "country IN (";
130 QString countriesQuery = "country IN (";
131 for (int i = 0; i < selectedCountriesStrings.size(); i++)
131 for (int i = 0; i < selectedCountriesStrings.size(); i++)
132 {
132 {
133 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
133 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
134 if ( i < selectedCountriesStrings.size() - 1)
134 if ( i < selectedCountriesStrings.size() - 1)
135 countriesQuery.append(",");
135 countriesQuery.append(",");
136 else
136 else
137 countriesQuery.append(")");
137 countriesQuery.append(")");
138 }
138 }
139
139
140 // perform a query for each selected year
140 // perform a query for each selected year
141 for (int i = 0; i < selectedYearsInts.size(); i++)
141 for (int i = 0; i < selectedYearsInts.size(); i++)
142 {
142 {
143 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
143 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
144 QBarSet* barSet = new QBarSet;
144 QBarSet* barSet = new QBarSet(QString("GDP_%1").arg(selectedYearsInts[i]));
145 // while (query.next()) {
145 // while (query.next()) {
146 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
146 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
147 // }
147 // }
148 query.first();
148 query.first();
149
149
150 // the data for some of the coutries for some years might be missing.
150 // the data for some of the coutries for some years might be missing.
151 // QBarChart needs bars to have same size
151 // QBarChart needs bars to have same size
152 for (int k = 0; k < selectedCountriesStrings.size(); k++)
152 for (int k = 0; k < selectedCountriesStrings.size(); k++)
153 {
153 {
154 if (selectedCountriesStrings[k] == query.value(0).toString())
154 if (selectedCountriesStrings[k] == query.value(0).toString())
155 {
155 {
156 *barSet << query.value(1).toReal();
156 *barSet << query.value(1).toReal();
157 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
157 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
158 query.next();
158 query.next();
159 }
159 }
160 else
160 else
161 {
161 {
162 // data missing, put 0
162 // data missing, put 0
163 *barSet << 0.0f;
163 *barSet << 0.0f;
164 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
164 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
165 }
165 }
166 }
166 }
167 series0->addBarSet(barSet);
167 series0->addBarSet(barSet);
168 }
168 }
169 // add the serie to the chart
169 // add the serie to the chart
170 chartArea->addSeries(series0);
170 chartArea->addSeries(series0);
171
171
172 }
172 }
173 else if (scatterChartRadioButton->isChecked())
173 else if (scatterChartRadioButton->isChecked())
174 {
174 {
175 QString yearsQuery = "year IN (";
175 QString yearsQuery = "year IN (";
176 for (int i = 0; i < selectedYearsInts.size(); i++)
176 for (int i = 0; i < selectedYearsInts.size(); i++)
177 {
177 {
178 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
178 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
179 if ( i < selectedYearsInts.size() - 1)
179 if ( i < selectedYearsInts.size() - 1)
180 yearsQuery.append(",");
180 yearsQuery.append(",");
181 else
181 else
182 yearsQuery.append(")");
182 yearsQuery.append(")");
183 }
183 }
184
184
185 // perform a query for each selected year
185 // perform a query for each selected year
186 for (int i = 0; i < selectedCountriesStrings.size(); i++)
186 for (int i = 0; i < selectedCountriesStrings.size(); i++)
187 {
187 {
188 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
188 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
189 query.first();
189 query.first();
190
190
191 QScatterSeries* series = new QScatterSeries;
191 QScatterSeries* series = new QScatterSeries;
192 // the data for some of the coutries for some years might be missing.
192 // the data for some of the coutries for some years might be missing.
193 for (int k = 0; k < selectedYearsInts.size(); k++)
193 for (int k = 0; k < selectedYearsInts.size(); k++)
194 {
194 {
195 if (selectedYearsInts[k] == query.value(0).toInt())
195 if (selectedYearsInts[k] == query.value(0).toInt())
196 {
196 {
197 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
197 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
198 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
198 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
199 query.next();
199 query.next();
200 }
200 }
201 else
201 else
202 {
202 {
203 // data missing, put 0
203 // data missing, put 0
204 *series << QPointF(selectedYearsInts[k] , 0.0f);
204 *series << QPointF(selectedYearsInts[k] , 0.0f);
205 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
205 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
206 }
206 }
207 }
207 }
208 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
208 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
209 chartArea->addSeries(series);
209 chartArea->addSeries(series);
210 }
210 }
211 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
211 }
212 }
212 }
213 }
213
214
214 void Widget::printChart()
215 void Widget::printChart()
215 {
216 {
216 QPrinter printer;
217 QPrinter printer;
217 // QPrinter printer(QPrinter::HighResolution);
218 // QPrinter printer(QPrinter::HighResolution);
218 printer.setOutputFormat(QPrinter::PdfFormat);
219 printer.setOutputFormat(QPrinter::PdfFormat);
219 printer.setOrientation(QPrinter::Landscape);
220 printer.setOrientation(QPrinter::Landscape);
220 printer.setOutputFileName("print.pdf");
221 printer.setOutputFileName("print.pdf");
221
222
222 QPainter painter;
223 QPainter painter;
223 painter.begin(&printer);
224 painter.begin(&printer);
224 chartArea->render(&painter);
225 chartArea->render(&painter);
225 }
226 }
@@ -1,237 +1,246
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QGraphicsSceneResizeEvent>
6 #include <QGraphicsSceneResizeEvent>
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 /*!
11 /*!
12 \enum QChart::ChartTheme
12 \enum QChart::ChartTheme
13
13
14 This enum describes the theme used by the chart.
14 This enum describes the theme used by the chart.
15
15
16 \value ChartThemeDefault
16 \value ChartThemeDefault
17 \value ChartThemeVanilla
17 \value ChartThemeVanilla
18 \value ChartThemeIcy
18 \value ChartThemeIcy
19 \value ChartThemeGrayscale
19 \value ChartThemeGrayscale
20 \value ChartThemeScientific
20 \value ChartThemeScientific
21 */
21 */
22
22
23 /*!
23 /*!
24 \class QChart
24 \class QChart
25 \brief QtCommercial chart API.
25 \brief QtCommercial chart API.
26
26
27 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
27 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
28 representation of different types of QChartSeries and other chart related objects like
28 representation of different types of QChartSeries and other chart related objects like
29 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
29 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
30 convenience class QChartView instead of QChart.
30 convenience class QChartView instead of QChart.
31 \sa QChartView
31 \sa QChartView
32 */
32 */
33
33
34 /*!
34 /*!
35 Constructs a chart object which is a child of parent.
35 Constructs a chart object which is a child of a\a parent.
36 */
36 */
37 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
37 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
38 m_backgroundItem(0),
38 m_backgroundItem(0),
39 m_titleItem(0),
39 m_titleItem(0),
40 m_dataset(new ChartDataSet(this)),
40 m_dataset(new ChartDataSet(this)),
41 m_presenter(new ChartPresenter(this,m_dataset))
41 m_presenter(new ChartPresenter(this,m_dataset))
42 {
42 {
43 }
43 }
44
44
45 /*!
45 /*!
46 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
46 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
47 */
47 */
48 QChart::~QChart()
48 QChart::~QChart()
49 {
49 {
50 }
50 }
51
51
52 /*!
52 /*!
53 Adds the \a series and optional y axis onto the chart and takes the ownership of the objects.
53 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
54 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
54 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
55 the y axis).
55 the y axis).
56 */
56 */
57 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
57 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
58 {
58 {
59 m_dataset->addSeries(series, axisY);
59 m_dataset->addSeries(series, axisY);
60 }
60 }
61
61
62 /*!
62 /*!
63 Removes the QChartSeries specified in a perameter from the QChartView.
63 Removes the \a series specified in a perameter from the QChartView.
64 It releses its ownership of the specified QChartSeries object.
64 It releses its ownership of the specified QChartSeries object.
65 It does not delete the pointed QChartSeries data object
65 It does not delete the pointed QChartSeries data object
66 \sa removeSeries(), removeAllSeries()
66 \sa addSeries(), removeAllSeries()
67 */
67 */
68 void QChart::removeSeries(QChartSeries* series)
68 void QChart::removeSeries(QChartSeries* series)
69 {
69 {
70 m_dataset->removeSeries(series);
70 m_dataset->removeSeries(series);
71 }
71 }
72
72
73 /*!
73 /*!
74 Removes all the QChartSeries that have been added to the QChartView
74 Removes all the QChartSeries that have been added to the QChartView
75 It also deletes the pointed QChartSeries data objects
75 It also deletes the pointed QChartSeries data objects
76 \sa addSeries(), removeSeries()
76 \sa addSeries(), removeSeries()
77 */
77 */
78 void QChart::removeAllSeries()
78 void QChart::removeAllSeries()
79 {
79 {
80 m_dataset->removeAllSeries();
80 m_dataset->removeAllSeries();
81 }
81 }
82
82
83 void QChart::setChartBackgroundBrush(const QBrush& brush)
83 void QChart::setChartBackgroundBrush(const QBrush& brush)
84 {
84 {
85 createChartBackgroundItem();
85 createChartBackgroundItem();
86 m_backgroundItem->setBrush(brush);
86 m_backgroundItem->setBrush(brush);
87 m_backgroundItem->update();
87 m_backgroundItem->update();
88 }
88 }
89
89
90 void QChart::setChartBackgroundPen(const QPen& pen)
90 void QChart::setChartBackgroundPen(const QPen& pen)
91 {
91 {
92 createChartBackgroundItem();
92 createChartBackgroundItem();
93 m_backgroundItem->setPen(pen);
93 m_backgroundItem->setPen(pen);
94 m_backgroundItem->update();
94 m_backgroundItem->update();
95 }
95 }
96
96
97 /*!
97 /*!
98 Sets the chart \a title. The description text that is rendered above the chart.
98 Sets the chart \a title. The description text that is rendered above the chart.
99 */
99 */
100 void QChart::setChartTitle(const QString& title)
100 void QChart::setChartTitle(const QString& title)
101 {
101 {
102 createChartTitleItem();
102 createChartTitleItem();
103 m_titleItem->setPlainText(title);
103 m_titleItem->setPlainText(title);
104 }
104 }
105
105
106 /*!
106 /*!
107 Sets the \a font that is used for rendering the description text that is rendered above the chart.
107 Sets the \a font that is used for rendering the description text that is rendered above the chart.
108 */
108 */
109 void QChart::setChartTitleFont(const QFont& font)
109 void QChart::setChartTitleFont(const QFont& font)
110 {
110 {
111 createChartTitleItem();
111 createChartTitleItem();
112 m_titleItem->setFont(font);
112 m_titleItem->setFont(font);
113 }
113 }
114
114
115 void QChart::createChartBackgroundItem()
115 void QChart::createChartBackgroundItem()
116 {
116 {
117 if(!m_backgroundItem) {
117 if(!m_backgroundItem) {
118 m_backgroundItem = new QGraphicsRectItem(this);
118 m_backgroundItem = new QGraphicsRectItem(this);
119 m_backgroundItem->setPen(Qt::NoPen);
119 m_backgroundItem->setPen(Qt::NoPen);
120 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
120 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
121 }
121 }
122 }
122 }
123
123
124 void QChart::createChartTitleItem()
124 void QChart::createChartTitleItem()
125 {
125 {
126 if(!m_titleItem) {
126 if(!m_titleItem) {
127 m_titleItem = new QGraphicsTextItem(this);
127 m_titleItem = new QGraphicsTextItem(this);
128 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
128 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
129 }
129 }
130 }
130 }
131
131
132 /*!
132 /*!
133 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
133 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
134 \sa setMargin()
134 \sa setMargin()
135 */
135 */
136 int QChart::margin() const
136 int QChart::margin() const
137 {
137 {
138 return m_presenter->margin();
138 return m_presenter->margin();
139 }
139 }
140
140
141 /*!
141 /*!
142 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
142 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
143 \sa margin()
143 \sa margin()
144 */
144 */
145 void QChart::setMargin(int margin)
145 void QChart::setMargin(int margin)
146 {
146 {
147 m_presenter->setMargin(margin);
147 m_presenter->setMargin(margin);
148 }
148 }
149
149
150 /*!
150 /*!
151 Sets the \a theme used by the chart for rendering data graphical representation
151 Sets the \a theme used by the chart for rendering the graphical representation of the data
152 \sa ChartTheme, chartTheme()
152 \sa ChartTheme, chartTheme()
153 */
153 */
154 void QChart::setChartTheme(QChart::ChartTheme theme)
154 void QChart::setChartTheme(QChart::ChartTheme theme)
155 {
155 {
156 m_presenter->setChartTheme(theme);
156 m_presenter->setChartTheme(theme);
157 }
157 }
158
158
159 /*!
159 /*!
160 Returns the theme enum used by the chart.
160 Returns the theme enum used by the chart.
161 \sa ChartTheme, setChartTheme()
161 \sa ChartTheme, setChartTheme()
162 */
162 */
163 QChart::ChartTheme QChart::chartTheme() const
163 QChart::ChartTheme QChart::chartTheme() const
164 {
164 {
165 return m_presenter->chartTheme();
165 return m_presenter->chartTheme();
166 }
166 }
167
167
168 /*!
169 Zooms in the view by a factor of 2
170 */
168 void QChart::zoomIn()
171 void QChart::zoomIn()
169 {
172 {
170 if (!m_dataset->nextDomain()) {
173 if (!m_dataset->nextDomain()) {
171 QRectF rect = m_presenter->geometry();
174 QRectF rect = m_presenter->geometry();
172 rect.setWidth(rect.width()/2);
175 rect.setWidth(rect.width()/2);
173 rect.setHeight(rect.height()/2);
176 rect.setHeight(rect.height()/2);
174 rect.moveCenter(m_presenter->geometry().center());
177 rect.moveCenter(m_presenter->geometry().center());
175 zoomIn(rect);
178 zoomIn(rect);
176 }
179 }
177 }
180 }
178
181
182 /*!
183 Zooms in the view to a maximum level at which \a rect is still fully visible.
184 */
179 void QChart::zoomIn(const QRectF& rect)
185 void QChart::zoomIn(const QRectF& rect)
180 {
186 {
181 if(!rect.isValid()) return;
187 if(!rect.isValid()) return;
182 QRectF r = rect.normalized();
188 QRectF r = rect.normalized();
183 int margin = m_presenter->margin();
189 int margin = m_presenter->margin();
184 r.translate(-margin, -margin);
190 r.translate(-margin, -margin);
185 m_dataset->addDomain(r,m_presenter->geometry());
191 m_dataset->addDomain(r,m_presenter->geometry());
186 }
192 }
187
193
194 /*!
195 Restores the view zoom level to the previous one.
196 */
188 void QChart::zoomOut()
197 void QChart::zoomOut()
189 {
198 {
190 m_dataset->previousDomain();
199 m_dataset->previousDomain();
191 }
200 }
192
201
193 void QChart::zoomReset()
202 void QChart::zoomReset()
194 {
203 {
195 m_dataset->clearDomains();
204 m_dataset->clearDomains();
196 }
205 }
197
206
198 /*!
207 /*!
199 Returns the pointer to the x axis object of the chart
208 Returns the pointer to the x axis object of the chart
200 */
209 */
201 QChartAxis* QChart::axisX() const
210 QChartAxis* QChart::axisX() const
202 {
211 {
203 return m_dataset->axisX();
212 return m_dataset->axisX();
204 }
213 }
205
214
206 /*!
215 /*!
207 Returns the pointer to the y axis object of the chart
216 Returns the pointer to the y axis object of the chart
208 */
217 */
209 QChartAxis* QChart::axisY() const
218 QChartAxis* QChart::axisY() const
210 {
219 {
211 return m_dataset->axisY();
220 return m_dataset->axisY();
212 }
221 }
213
222
214 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
223 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
215 {
224 {
216
225
217 m_rect = QRectF(QPoint(0,0),event->newSize());
226 m_rect = QRectF(QPoint(0,0),event->newSize());
218 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
227 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
219
228
220 // recalculate title position
229 // recalculate title position
221 if (m_titleItem) {
230 if (m_titleItem) {
222 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
231 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
223 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
232 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
224 }
233 }
225
234
226 //recalculate background gradient
235 //recalculate background gradient
227 if (m_backgroundItem) {
236 if (m_backgroundItem) {
228 m_backgroundItem->setRect(rect);
237 m_backgroundItem->setRect(rect);
229 }
238 }
230
239
231 QGraphicsWidget::resizeEvent(event);
240 QGraphicsWidget::resizeEvent(event);
232 update();
241 update();
233 }
242 }
234
243
235 #include "moc_qchart.cpp"
244 #include "moc_qchart.cpp"
236
245
237 QTCOMMERCIALCHART_END_NAMESPACE
246 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,82
1 #ifndef QCHART_H
1 #ifndef QCHART_H
2 #define QCHART_H
2 #define QCHART_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 #include <qchartseries.h>
6 #include <QGraphicsWidget>
6 #include <QGraphicsWidget>
7 #include <QLinearGradient>
7 #include <QLinearGradient>
8 #include <QFont>
8 #include <QFont>
9
9
10 class QGraphicsSceneResizeEvent;
10 class QGraphicsSceneResizeEvent;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class AxisItem;
14 class AxisItem;
15 class QChartSeries;
15 class QChartSeries;
16 class PlotDomain;
16 class PlotDomain;
17 class BarPresenter;
17 class BarPresenter;
18 class QChartAxis;
18 class QChartAxis;
19 class ChartTheme;
19 class ChartTheme;
20 class ChartItem;
20 class ChartItem;
21 class ChartDataSet;
21 class ChartDataSet;
22 class ChartPresenter;
22 class ChartPresenter;
23
23
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
25 {
25 {
26 Q_OBJECT
26 Q_OBJECT
27 public:
27 public:
28 enum ChartTheme {
28 enum ChartTheme {
29 /*! The default theme follows the GUI style of the Operating System */
30 ChartThemeDefault,
29 ChartThemeDefault,
31 ChartThemeVanilla,
30 ChartThemeVanilla,
32 ChartThemeIcy,
31 ChartThemeIcy,
33 ChartThemeGrayscale,
32 ChartThemeGrayscale,
34 ChartThemeScientific
33 ChartThemeScientific
35 //ChartThemeUnnamed1
34 //ChartThemeUnnamed1
35 /*! The default theme follows the GUI style of the Operating System */
36 };
36 };
37
37
38 public:
38 public:
39 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
39 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
40 ~QChart();
40 ~QChart();
41
41
42 void addSeries(QChartSeries* series, QChartAxis* axisY = 0);
42 void addSeries(QChartSeries* series, QChartAxis* axisY = 0);
43 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
43 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
44 void removeAllSeries(); // deletes series and axis
44 void removeAllSeries(); // deletes series and axis
45
45
46 void setMargin(int margin);
46 void setMargin(int margin);
47 int margin() const;
47 int margin() const;
48 void setChartTheme(QChart::ChartTheme theme);
48 void setChartTheme(QChart::ChartTheme theme);
49 QChart::ChartTheme chartTheme() const;
49 QChart::ChartTheme chartTheme() const;
50
50
51 void setChartTitle(const QString& title);
51 void setChartTitle(const QString& title);
52 void setChartTitleFont(const QFont& font);
52 void setChartTitleFont(const QFont& font);
53 void setChartBackgroundBrush(const QBrush& brush);
53 void setChartBackgroundBrush(const QBrush& brush);
54 void setChartBackgroundPen(const QPen& pen);
54 void setChartBackgroundPen(const QPen& pen);
55
55
56 void zoomIn();
56 void zoomIn();
57 void zoomIn(const QRectF& rect);
57 void zoomIn(const QRectF& rect);
58 void zoomOut();
58 void zoomOut();
59 void zoomReset();
59 void zoomReset();
60
60
61 QChartAxis* axisX() const;
61 QChartAxis* axisX() const;
62 QChartAxis* axisY() const;
62 QChartAxis* axisY() const;
63
63
64 protected:
64 protected:
65 void resizeEvent(QGraphicsSceneResizeEvent *event);
65 void resizeEvent(QGraphicsSceneResizeEvent *event);
66
66
67 private:
67 private:
68 inline void createChartBackgroundItem();
68 inline void createChartBackgroundItem();
69 inline void createChartTitleItem();
69 inline void createChartTitleItem();
70
70
71 private:
71 private:
72 Q_DISABLE_COPY(QChart)
72 Q_DISABLE_COPY(QChart)
73 QGraphicsRectItem* m_backgroundItem;
73 QGraphicsRectItem* m_backgroundItem;
74 QGraphicsTextItem* m_titleItem;
74 QGraphicsTextItem* m_titleItem;
75 QRectF m_rect;
75 QRectF m_rect;
76 ChartDataSet *m_dataset;
76 ChartDataSet *m_dataset;
77 ChartPresenter *m_presenter;
77 ChartPresenter *m_presenter;
78 };
78 };
79
79
80 QTCOMMERCIALCHART_END_NAMESPACE
80 QTCOMMERCIALCHART_END_NAMESPACE
81
81
82 #endif
82 #endif
@@ -1,283 +1,312
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QGraphicsView>
4 #include <QGraphicsView>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QRubberBand>
6 #include <QRubberBand>
7 #include <QResizeEvent>
7 #include <QResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 /*!
10 /*!
11 \class QChartView
11 \class QChartView
12 \brief Chart widget
12 \brief Chart widget
13
13
14 QChartView is a standalone widget that can display charts. It does not require QGraphicsScene to work. It manages the graphical
14 QChartView is a standalone widget that can display charts. It does not require QGraphicsScene to work. It manages the graphical
15 representation of different types of QChartSeries and other chart related objects like
15 representation of different types of QChartSeries and other chart related objects like
16 QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead.
16 QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead.
17
17
18 \sa QChart
18 \sa QChart
19 */
19 */
20
20
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
22
23 /*!
24 Constructs a chartView object which is a child of a\a parent.
25 */
23 QChartView::QChartView(QWidget *parent) :
26 QChartView::QChartView(QWidget *parent) :
24 QGraphicsView(parent),
27 QGraphicsView(parent),
25 m_scene(new QGraphicsScene(this)),
28 m_scene(new QGraphicsScene(this)),
26 m_chart(new QChart()),
29 m_chart(new QChart()),
27 m_rubberBand(0),
30 m_rubberBand(0),
28 m_verticalRubberBand(false),
31 m_verticalRubberBand(false),
29 m_horizonalRubberBand(false)
32 m_horizonalRubberBand(false)
30 {
33 {
31 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
34 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
33 setScene(m_scene);
36 setScene(m_scene);
34 m_chart->setMargin(50);
37 m_chart->setMargin(50);
35 m_scene->addItem(m_chart);
38 m_scene->addItem(m_chart);
36 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
39 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 }
40 }
38
41
39
42
40 /*!
43 /*!
41 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
44 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
42 */
45 */
43 QChartView::~QChartView()
46 QChartView::~QChartView()
44 {
47 {
45 }
48 }
46
49
47 void QChartView::resizeEvent(QResizeEvent *event)
50 void QChartView::resizeEvent(QResizeEvent *event)
48 {
51 {
49 m_scene->setSceneRect(0,0,size().width(),size().height());
52 m_scene->setSceneRect(0,0,size().width(),size().height());
50 m_chart->resize(size());
53 m_chart->resize(size());
51 QWidget::resizeEvent(event);
54 QWidget::resizeEvent(event);
52 }
55 }
53
56
54 /*!
57 /*!
55 Adds the series and optional y axis onto the chart and takes the ownership of the objects.
58 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
56 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
59 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
57 the y axis).
60 the y axis).
58 \sa removeSeries, removeAllSeries
61 \sa removeSeries(), removeAllSeries()
59 */
62 */
60 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
63 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
61 {
64 {
62 m_chart->addSeries(series,axisY);
65 m_chart->addSeries(series,axisY);
63 }
66 }
64
67
65 /*!
68 /*!
66 Removes the QChartSeries specified in a perameter from the QChartView.
69 Removes the \a series specified in a perameter from the QChartView.
67 It releses its ownership of the specified QChartSeries object.
70 It releses its ownership of the specified QChartSeries object.
68 It does not delete the pointed QChartSeries data object
71 It does not delete the pointed QChartSeries data object
69 \sa removeSeries(), removeAllSeries()
72 \sa addSeries(), removeAllSeries()
70 */
73 */
71 void QChartView::removeSeries(QChartSeries* series)
74 void QChartView::removeSeries(QChartSeries* series)
72 {
75 {
73 m_chart->removeSeries(series);
76 m_chart->removeSeries(series);
74 }
77 }
75
78
76 /*!
79 /*!
77 Removes all the QChartSeries that have been added to the QChartView
80 Removes all the QChartSeries that have been added to the QChartView
78 It also deletes the pointed QChartSeries data objects
81 It also deletes the pointed QChartSeries data objects
79 \sa addSeries(), removeSeries()
82 \sa addSeries(), removeSeries()
80 */
83 */
81 void QChartView::removeAllSeries()
84 void QChartView::removeAllSeries()
82 {
85 {
83 m_chart->removeAllSeries();
86 m_chart->removeAllSeries();
84 }
87 }
85
88
89 /*!
90 Zooms in the view by a factor of 2
91 */
86 void QChartView::zoomIn()
92 void QChartView::zoomIn()
87 {
93 {
88 m_chart->zoomIn();
94 m_chart->zoomIn();
89 }
95 }
90
96
97 /*!
98 Zooms in the view to a maximum level at which \a rect is still fully visible.
99 */
91 void QChartView::zoomIn(const QRect& rect)
100 void QChartView::zoomIn(const QRect& rect)
92 {
101 {
93 m_chart->zoomIn(rect);
102 m_chart->zoomIn(rect);
94 }
103 }
95
104
105 /*!
106 Restores the view zoom level to the previous one.
107 */
96 void QChartView::zoomOut()
108 void QChartView::zoomOut()
97 {
109 {
98 m_chart->zoomOut();
110 m_chart->zoomOut();
99 }
111 }
100
112
101 /*!
113 /*!
102 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
114 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
103 */
115 */
104 int QChartView::margin() const
116 int QChartView::margin() const
105 {
117 {
106 return m_chart->margin();
118 return m_chart->margin();
107 }
119 }
108
120
109 /*!
121 /*!
110 Sets the chart \a tile. A description text that is rendered above the chart.
122 Sets the chart \a title. A description text that is rendered above the chart.
111 */
123 */
112 void QChartView::setChartTitle(const QString& title)
124 void QChartView::setChartTitle(const QString& title)
113 {
125 {
114 m_chart->setChartTitle(title);
126 m_chart->setChartTitle(title);
115 }
127 }
116
128
117 /*!
129 /*!
118 Sets the \a font that is used for rendering the description text that is rendered above the chart.
130 Sets the \a font that is used for rendering the description text that is rendered above the chart.
119 */
131 */
120 void QChartView::setChartTitleFont(const QFont& font)
132 void QChartView::setChartTitleFont(const QFont& font)
121 {
133 {
122 m_chart->setChartTitleFont(font);
134 m_chart->setChartTitleFont(font);
123 }
135 }
124
136
125 /*!
137 /*!
126 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
138 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
127 */
139 */
128 void QChartView::setChartBackgroundBrush(const QBrush& brush)
140 void QChartView::setChartBackgroundBrush(const QBrush& brush)
129 {
141 {
130 m_chart->setChartBackgroundBrush(brush);
142 m_chart->setChartBackgroundBrush(brush);
131 }
143 }
132
144
133 /*!
145 /*!
134 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
146 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
135 */
147 */
136 void QChartView::setChartBackgroundPen(const QPen& pen)
148 void QChartView::setChartBackgroundPen(const QPen& pen)
137 {
149 {
138 m_chart->setChartBackgroundPen(pen);
150 m_chart->setChartBackgroundPen(pen);
139 }
151 }
140
152
153 /*!
154 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
155 */
141 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
156 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
142 {
157 {
143 switch(policy) {
158 switch(policy) {
144 case VerticalRubberBand:
159 case VerticalRubberBand:
145 m_verticalRubberBand = true;
160 m_verticalRubberBand = true;
146 m_horizonalRubberBand = false;
161 m_horizonalRubberBand = false;
147 break;
162 break;
148 case HorizonalRubberBand:
163 case HorizonalRubberBand:
149 m_verticalRubberBand = false;
164 m_verticalRubberBand = false;
150 m_horizonalRubberBand = true;
165 m_horizonalRubberBand = true;
151 break;
166 break;
152 case RectangleRubberBand:
167 case RectangleRubberBand:
153 m_verticalRubberBand = true;
168 m_verticalRubberBand = true;
154 m_horizonalRubberBand = true;
169 m_horizonalRubberBand = true;
155 break;
170 break;
156 case NoRubberBand:
171 case NoRubberBand:
157 default:
172 default:
158 delete m_rubberBand;
173 delete m_rubberBand;
159 m_rubberBand=0;
174 m_rubberBand=0;
160 m_horizonalRubberBand = false;
175 m_horizonalRubberBand = false;
161 m_verticalRubberBand = false;
176 m_verticalRubberBand = false;
162 return;
177 return;
163 }
178 }
164 if(!m_rubberBand) {
179 if(!m_rubberBand) {
165 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
180 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
166 m_rubberBand->setEnabled(true);
181 m_rubberBand->setEnabled(true);
167 }
182 }
168 }
183 }
169
184
185 /*!
186 Returns the RubberBandPolicy that is currently being used by the widget.
187 */
170 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
188 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
171 {
189 {
172 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
190 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
173 if(m_horizonalRubberBand) return HorizonalRubberBand;
191 if(m_horizonalRubberBand) return HorizonalRubberBand;
174 if(m_verticalRubberBand) return VerticalRubberBand;
192 if(m_verticalRubberBand) return VerticalRubberBand;
175 return NoRubberBand;
193 return NoRubberBand;
176 }
194 }
177
195
196 /*!
197 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
198 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is not consumed.
199 */
178 void QChartView::mousePressEvent(QMouseEvent *event)
200 void QChartView::mousePressEvent(QMouseEvent *event)
179 {
201 {
180 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
202 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
181
203
182 int margin = m_chart->margin();
204 int margin = m_chart->margin();
183 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
205 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
184
206
185 if (rect.contains(event->pos())) {
207 if (rect.contains(event->pos())) {
186 m_rubberBandOrigin = event->pos();
208 m_rubberBandOrigin = event->pos();
187 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
209 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
188 m_rubberBand->show();
210 m_rubberBand->show();
189 event->accept();
211 event->accept();
190 }
212 }
191 }
213 }
214 else {
215 QGraphicsView::mousePressEvent(event);
216 }
192 }
217 }
193
218
194 void QChartView::mouseMoveEvent(QMouseEvent *event)
219 void QChartView::mouseMoveEvent(QMouseEvent *event)
195 {
220 {
196 if(m_rubberBand && m_rubberBand->isVisible()) {
221 if(m_rubberBand && m_rubberBand->isVisible()) {
197 int margin = m_chart->margin();
222 int margin = m_chart->margin();
198 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
223 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
199 int width = event->pos().x() - m_rubberBandOrigin.x();
224 int width = event->pos().x() - m_rubberBandOrigin.x();
200 int height = event->pos().y() - m_rubberBandOrigin.y();
225 int height = event->pos().y() - m_rubberBandOrigin.y();
201 if(!m_verticalRubberBand) {
226 if(!m_verticalRubberBand) {
202 m_rubberBandOrigin.setY(rect.top());
227 m_rubberBandOrigin.setY(rect.top());
203 height = rect.height();
228 height = rect.height();
204 }
229 }
205 if(!m_horizonalRubberBand) {
230 if(!m_horizonalRubberBand) {
206 m_rubberBandOrigin.setX(rect.left());
231 m_rubberBandOrigin.setX(rect.left());
207 width= rect.width();
232 width= rect.width();
208 }
233 }
209 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
234 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
210 }
235 }
211 else {
236 else {
212 QGraphicsView::mouseMoveEvent(event);
237 QGraphicsView::mouseMoveEvent(event);
213 }
238 }
214 }
239 }
215
240
241 /*!
242 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
243 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
244 */
216 void QChartView::mouseReleaseEvent(QMouseEvent *event)
245 void QChartView::mouseReleaseEvent(QMouseEvent *event)
217 {
246 {
218 if(m_rubberBand) {
247 if(m_rubberBand) {
219 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
248 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
220 m_rubberBand->hide();
249 m_rubberBand->hide();
221 QRect rect = m_rubberBand->geometry();
250 QRect rect = m_rubberBand->geometry();
222 m_chart->zoomIn(rect);
251 m_chart->zoomIn(rect);
223 event->accept();
252 event->accept();
224 }
253 }
225
254
226 if(event->button()==Qt::RightButton)
255 if(event->button()==Qt::RightButton)
227 m_chart->zoomReset();
256 m_chart->zoomReset();
228 }
257 }
229 else {
258 else {
230 QGraphicsView::mouseReleaseEvent(event);
259 QGraphicsView::mouseReleaseEvent(event);
231 }
260 }
232 }
261 }
233
262
234 void QChartView::keyPressEvent(QKeyEvent *event)
263 void QChartView::keyPressEvent(QKeyEvent *event)
235 {
264 {
236 switch (event->key()) {
265 switch (event->key()) {
237 case Qt::Key_Plus:
266 case Qt::Key_Plus:
238 zoomIn();
267 zoomIn();
239 break;
268 break;
240 case Qt::Key_Minus:
269 case Qt::Key_Minus:
241 zoomOut();
270 zoomOut();
242 break;
271 break;
243 default:
272 default:
244 QGraphicsView::keyPressEvent(event);
273 QGraphicsView::keyPressEvent(event);
245 break;
274 break;
246 }
275 }
247 }
276 }
248
277
249 /*!
278 /*!
250 Sets the \a theme used by the chart for rendering the graphical representation of the data
279 Sets the \a theme used by the chart for rendering the graphical representation of the data
251 \sa QChart::ChartTheme, chartTheme()
280 \sa QChart::ChartTheme, chartTheme()
252 */
281 */
253 void QChartView::setChartTheme(QChart::ChartTheme theme)
282 void QChartView::setChartTheme(QChart::ChartTheme theme)
254 {
283 {
255 m_chart->setChartTheme(theme);
284 m_chart->setChartTheme(theme);
256 }
285 }
257
286
258 /*!
287 /*!
259 Returns the theme enum used by the chart.
288 Returns the theme enum used by the chart.
260 \sa setChartTheme()
289 \sa setChartTheme()
261 */
290 */
262 QChart::ChartTheme QChartView::chartTheme() const
291 QChart::ChartTheme QChartView::chartTheme() const
263 {
292 {
264 return m_chart->chartTheme();
293 return m_chart->chartTheme();
265 }
294 }
266
295
267 /*!
296 /*!
268 Returns the pointer to the x axis object of the chart
297 Returns the pointer to the x axis object of the chart
269 */
298 */
270 QChartAxis* QChartView::axisX() const
299 QChartAxis* QChartView::axisX() const
271 {
300 {
272 return m_chart->axisX();
301 return m_chart->axisX();
273 }
302 }
274
303
275 /*!
304 /*!
276 Returns the pointer to the y axis object of the chart
305 Returns the pointer to the y axis object of the chart
277 */
306 */
278 QChartAxis* QChartView::axisY() const
307 QChartAxis* QChartView::axisY() const
279 {
308 {
280 return m_chart->axisY();
309 return m_chart->axisY();
281 }
310 }
282
311
283 QTCOMMERCIALCHART_END_NAMESPACE
312 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now