##// END OF EJS Templates
Adds scroll and zoom to chartviewer
Michal Klocek -
r1748:ee59e61f224b
parent child
Show More
@@ -0,0 +1,55
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "view.h"
22 #include <QGraphicsWidget>
23 #include <QResizeEvent>
24 #include <QDebug>
25
26 View::View(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent):QGraphicsView(scene,parent),
27 m_form(form)
28 {
29 setDragMode(QGraphicsView::NoDrag);
30 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32 }
33
34 void View::resizeEvent(QResizeEvent *event)
35 {
36 if (scene())
37 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
38 if (m_form)
39 m_form->resize(QSizeF(event->size()));
40 QGraphicsView::resizeEvent(event);
41 }
42
43 void View::mouseMoveEvent(QMouseEvent *event)
44 {
45 //BugFix somehow view always eats the mouse move event;
46 event->setAccepted(false);
47 }
48
49 void View::mouseReleaseEvent(QMouseEvent *event)
50 {
51
52 QGraphicsView::mouseReleaseEvent(event);
53 //BugFix somehow view always eats the mouse release event;
54 event->setAccepted(false);
55 }
@@ -0,0 +1,41
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef VIEW_H_
22 #define VIEW_H_
23 #include <QGraphicsView>
24
25 class QGraphicsScene;
26 class QResizeEvent;
27
28 class View: public QGraphicsView
29 {
30 public:
31 View(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent = 0);
32
33 protected:
34 void resizeEvent(QResizeEvent *event);
35 void mouseMoveEvent(QMouseEvent *event);
36 void mouseReleaseEvent(QMouseEvent *event);
37 private:
38 QGraphicsWidget *m_form;
39 };
40
41 #endif
@@ -1,5 +1,5
1 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 2 TARGET = chartviewer
3 3 QT += opengl
4 SOURCES = main.cpp chartwindow.cpp
5 HEADERS = chartwindow.h
4 SOURCES = main.cpp window.cpp view.cpp
5 HEADERS = window.h view.h No newline at end of file
@@ -1,32 +1,32
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "chartwindow.h"
21 #include "window.h"
22 22 #include <QApplication>
23 23 #include <QMainWindow>
24 24
25 25 int main(int argc, char *argv[])
26 26 {
27 27 QApplication a(argc, argv);
28 ChartWindow window;
28 Window window;
29 29 window.show();
30 30 return a.exec();
31 31 }
32 32
@@ -1,438 +1,584
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "chartwindow.h"
21 #include "window.h"
22 #include "view.h"
22 23
23 24 #include <QChartView>
24 25 #include <QPieSeries>
25 26 #include <QPieSlice>
26 27 #include <QPercentBarSeries>
27 28 #include <QStackedBarSeries>
28 29 #include <QBarSeries>
29 30 #include <QBarSet>
30 31 #include <QLineSeries>
31 32 #include <QSplineSeries>
32 33 #include <QScatterSeries>
33 34 #include <QAreaSeries>
34 35 #include <QLegend>
35 36 #include <QGridLayout>
36 37 #include <QFormLayout>
37 38 #include <QComboBox>
38 39 #include <QSpinBox>
39 40 #include <QCheckBox>
40 41 #include <QGroupBox>
41 42 #include <QLabel>
42 43 #include <QTime>
43 44 #include <QBarCategoriesAxis>
44 45 #include <QGraphicsScene>
45 46 #include <QGraphicsGridLayout>
46 47 #include <QGraphicsLinearLayout>
47 48 #include <QGraphicsProxyWidget>
48 49 #include <QGLWidget>
49 50 #include <QApplication>
50 51 #include <QDebug>
51 52
52 ChartWindow::ChartWindow(QWidget* parent) :
53 QMainWindow(parent), m_listCount(3), m_valueMax(10), m_valueCount(7), m_scene(
54 new QGraphicsScene(this)), m_view(0), m_dataTable(
55 generateRandomData(m_listCount, m_valueMax, m_valueCount)), m_form(0), m_themeComboBox(0), m_antialiasCheckBox(
56 0), m_animatedComboBox(0), m_legendComboBox(0), m_openGLCheckBox(0)
53 Window::Window(QWidget* parent) :
54 QMainWindow(parent),
55 m_listCount(3),
56 m_valueMax(10),
57 m_valueCount(7),
58 m_scene(new QGraphicsScene(this)),
59 m_view(0),
60 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
61 m_form(0),
62 m_themeComboBox(0),
63 m_antialiasCheckBox(0),
64 m_animatedComboBox(0),
65 m_legendComboBox(0),
66 m_openGLCheckBox(0),
67 m_zoomCheckBox(0),
68 m_scrollCheckBox(0),
69 m_rubberBand(new QGraphicsRectItem()),
70 m_isScrolling(false),
71 m_isZooming(false),
72 m_scroll(false),
73 m_zoom(false)
57 74 {
58 75 createProxyWidgets();
59 76 connectSignals();
60 77
61 78 // create layout
62 79 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
63 80 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
64 81 settingsLayout->setOrientation(Qt::Vertical);
65 82 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
66 83 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
67 84 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
68 85 settingsLayout->addItem(m_widgetHash["themeLabel"]);
69 86 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
70 87 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
71 88 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
72 89 settingsLayout->addItem(m_widgetHash["legendLabel"]);
73 90 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
91 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
92 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
74 93 settingsLayout->addStretch();
75 94 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
76 95 //create charts
77 96
78 97 int i = m_widgetHash.count();
79 98 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
80 99 widget->setZValue(i--);
81 100 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
82 101 }
83 102
84 103 QChart* chart;
85 104
86 105 chart = createAreaChart();
87 106 baseLayout->addItem(chart, 0, 0);
88 107 m_chartList << chart;
89 108
90 109 chart = createBarChart(m_valueCount);
91 110 baseLayout->addItem(chart, 0, 1);
92 111 m_chartList << chart;
93 112
94 113 chart = createLineChart();
95 114 baseLayout->addItem(chart, 0, 2);
96 115 m_chartList << chart;
97 116
98 117 chart = createPieChart();
99 118 baseLayout->addItem(chart, 1, 0);
100 119 m_chartList << chart;
101 120
102 121 chart = createSplineChart();
103 122 baseLayout->addItem(chart, 1, 1);
104 123 m_chartList << chart;
105 124
106 125 chart = createScatterChart();
107 126 baseLayout->addItem(chart, 1, 2);
108 127 m_chartList << chart;
109 128
110 // Set defaults
111 //m_antialiasCheckBox->setChecked(true);
112
113 129 m_form = new QGraphicsWidget();
114 130 m_form->setLayout(baseLayout);
115 131 m_scene->addItem(m_form);
132 m_scene->addItem(m_rubberBand);
133 m_rubberBand->setVisible(false);
116 134
117 m_view = new GraphicsView(m_scene, m_form);
135 m_view = new View(m_scene, m_form);
118 136 m_view->setMinimumSize(m_form->preferredSize().toSize());
119 137
138 // Set defaults
139 m_antialiasCheckBox->setChecked(true);
120 140 updateUI();
121 141 setCentralWidget(m_view);
122 142 }
123 143
124 ChartWindow::~ChartWindow()
144 Window::~Window()
125 145 {
126 146 }
127 147
128 void ChartWindow::connectSignals()
148 void Window::connectSignals()
129 149 {
130 150 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
131 151 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
132 152 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
153 connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
154 connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
133 155 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
134 156 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
135 157 }
136 158
137 DataTable ChartWindow::generateRandomData(int listCount, int valueMax, int valueCount) const
159 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
138 160 {
139 161 DataTable dataTable;
140 162
141 163 // set seed for random stuff
142 164 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
143 165
144 166 // generate random data
145 167 for (int i(0); i < listCount; i++) {
146 168 DataList dataList;
147 169 qreal yValue(0);
148 170 for (int j(0); j < valueCount; j++) {
149 171 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
150 172 QPointF value(
151 173 (j + (qreal) qrand() / (qreal) RAND_MAX)
152 174 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
153 175 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
154 176 dataList << Data(value, label);
155 177 }
156 178 dataTable << dataList;
157 179 }
158 180
159 181 return dataTable;
160 182 }
161 183
162 void ChartWindow::createProxyWidgets()
184 void Window::createProxyWidgets()
163 185 {
164 186 m_themeComboBox = createThemeBox();
165 187 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
166 188 m_animatedComboBox = createAnimationBox();
167 189 m_legendComboBox = createLegendBox();
168 190 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
191 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
192 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
169 193 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
170 194 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
171 195 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
172 196 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
173 197 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
174 198 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
175 199 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
176 200 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
201 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
202 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
177 203 }
178 204
179 QComboBox* ChartWindow::createThemeBox() const
205 QComboBox* Window::createThemeBox() const
180 206 {
181 207 // settings layoutQGLWidget’
182 208 QComboBox* themeComboBox = new QComboBox();
183 209 themeComboBox->addItem("Light", QChart::ChartThemeLight);
184 210 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
185 211 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
186 212 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
187 213 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
188 214 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
189 215 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
190 216 return themeComboBox;
191 217 }
192 218
193 QComboBox* ChartWindow::createAnimationBox() const
219 QComboBox* Window::createAnimationBox() const
194 220 {
195 221 // settings layout
196 222 QComboBox* animationComboBox = new QComboBox();
197 223 animationComboBox->addItem("No Animations", QChart::NoAnimation);
198 224 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
199 225 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
200 226 animationComboBox->addItem("All Animations", QChart::AllAnimations);
201 227 return animationComboBox;
202 228 }
203 229
204 QComboBox* ChartWindow::createLegendBox() const
230 QComboBox* Window::createLegendBox() const
205 231 {
206 232 QComboBox* legendComboBox = new QComboBox();
207 233 legendComboBox->addItem("No Legend ", 0);
208 234 legendComboBox->addItem("Legend Top", Qt::AlignTop);
209 235 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
210 236 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
211 237 legendComboBox->addItem("Legend Right", Qt::AlignRight);
212 238 return legendComboBox;
213 239 }
214 240
215 QChart* ChartWindow::createAreaChart() const
241 QChart* Window::createAreaChart() const
216 242 {
217 243 QChart *chart = new QChart();
218 244 // chart->axisX()->setNiceNumbersEnabled(true);
219 245 // chart->axisY()->setNiceNumbersEnabled(true);
220 246 chart->setTitle("Area chart");
221 247
222 248 // The lower series initialized to zero values
223 249 QLineSeries *lowerSeries = 0;
224 250 QString name("Series ");
225 251 int nameIndex = 0;
226 252 for (int i(0); i < m_dataTable.count(); i++) {
227 253 QLineSeries *upperSeries = new QLineSeries(chart);
228 254 for (int j(0); j < m_dataTable[i].count(); j++) {
229 255 Data data = m_dataTable[i].at(j);
230 256 if (lowerSeries) {
231 257 const QList<QPointF>& points = lowerSeries->points();
232 258 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
233 259 }
234 260 else
235 261 upperSeries->append(QPointF(j, data.first.y()));
236 262 }
237 263 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
238 264 area->setName(name + QString::number(nameIndex));
239 265 nameIndex++;
240 266 chart->addSeries(area);
241 267 chart->createDefaultAxes();
242 268 lowerSeries = upperSeries;
243 269 }
244 270
245 271 return chart;
246 272 }
247 273
248 QChart* ChartWindow::createBarChart(int valueCount) const
274 QChart* Window::createBarChart(int valueCount) const
249 275 {
250 276 Q_UNUSED(valueCount);
251 277 QChart* chart = new QChart();
252 278 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
253 279 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
254 280 chart->setTitle("Bar chart");
255 281
256 282 QStackedBarSeries* series = new QStackedBarSeries(chart);
257 283 for (int i(0); i < m_dataTable.count(); i++) {
258 284 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
259 285 foreach (Data data, m_dataTable[i])
260 286 *set << data.first.y();
261 287 series->append(set);
262 288 }
263 289 chart->addSeries(series);
264 290 chart->createDefaultAxes();
265 291
266 292 return chart;
267 293 }
268 294
269 QChart* ChartWindow::createLineChart() const
295 QChart* Window::createLineChart() const
270 296 {
271 297 QChart* chart = new QChart();
272 298 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
273 299 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
274 300 chart->setTitle("Line chart");
275 301
276 302 QString name("Series ");
277 303 int nameIndex = 0;
278 304 foreach (DataList list, m_dataTable) {
279 305 QLineSeries *series = new QLineSeries(chart);
280 306 foreach (Data data, list)
281 307 series->append(data.first);
282 308 series->setName(name + QString::number(nameIndex));
283 309 nameIndex++;
284 310 chart->addSeries(series);
285 311 }
286 312 chart->createDefaultAxes();
287 313
288 314 return chart;
289 315 }
290 316
291 QChart* ChartWindow::createPieChart() const
317 QChart* Window::createPieChart() const
292 318 {
293 319 QChart* chart = new QChart();
294 320 chart->setTitle("Pie chart");
295 321
296 322 qreal pieSize = 1.0 / m_dataTable.count();
297 323 for (int i = 0; i < m_dataTable.count(); i++) {
298 324 QPieSeries *series = new QPieSeries(chart);
299 325 foreach (Data data, m_dataTable[i]) {
300 326 QPieSlice *slice = series->append(data.second, data.first.y());
301 327 if (data == m_dataTable[i].first()) {
302 328 slice->setLabelVisible();
303 329 slice->setExploded();
304 330 }
305 331 }
306 332 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
307 333 series->setPieSize(pieSize);
308 334 series->setHorizontalPosition(hPos);
309 335 series->setVerticalPosition(0.5);
310 336 chart->addSeries(series);
311 337 }
312 338
313 339 return chart;
314 340 }
315 341
316 QChart* ChartWindow::createSplineChart() const
342 QChart* Window::createSplineChart() const
317 343 {
318 344 QChart* chart = new QChart();
319 345 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
320 346 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
321 347 chart->setTitle("Spline chart");
322 348 QString name("Series ");
323 349 int nameIndex = 0;
324 350 foreach (DataList list, m_dataTable) {
325 351 QSplineSeries *series = new QSplineSeries(chart);
326 352 foreach (Data data, list)
327 353 series->append(data.first);
328 354 series->setName(name + QString::number(nameIndex));
329 355 nameIndex++;
330 356 chart->addSeries(series);
331 357 }
332 358 chart->createDefaultAxes();
333 359 return chart;
334 360 }
335 361
336 QChart* ChartWindow::createScatterChart() const
362 QChart* Window::createScatterChart() const
337 363 {
338 364 QChart* chart = new QChart();
339 365 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
340 366 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
341 367 chart->setTitle("Scatter chart");
342 368 QString name("Series ");
343 369 int nameIndex = 0;
344 370 foreach (DataList list, m_dataTable) {
345 371 QScatterSeries *series = new QScatterSeries(chart);
346 372 foreach (Data data, list)
347 373 series->append(data.first);
348 374 series->setName(name + QString::number(nameIndex));
349 375 nameIndex++;
350 376 chart->addSeries(series);
351 377 }
352 378 chart->createDefaultAxes();
353 379 return chart;
354 380 }
355 381
356 void ChartWindow::updateUI()
382 void Window::updateUI()
357 383 {
358 384 bool opengl = m_openGLCheckBox->isChecked();
359 385 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
360 386 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
361 387 m_view->deleteLater();
362 m_view = new GraphicsView(m_scene, m_form);
388 m_view = new View(m_scene, m_form);
363 389 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
364 390 setCentralWidget(m_view);
365 391 }
366 392
367 393 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
368 394 m_themeComboBox->currentIndex()).toInt();
369 395
370 396 foreach (QChart *chart, m_chartList)
371 397 chart->setTheme(theme);
372 398
373 399 QPalette pal = window()->palette();
374 400 if (theme == QChart::ChartThemeLight) {
375 401 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
376 402 pal.setColor(QPalette::WindowText, QRgb(0x404044));
377 403 }
378 404 else if (theme == QChart::ChartThemeDark) {
379 405 pal.setColor(QPalette::Window, QRgb(0x121218));
380 406 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
381 407 }
382 408 else if (theme == QChart::ChartThemeBlueCerulean) {
383 409 pal.setColor(QPalette::Window, QRgb(0x40434a));
384 410 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
385 411 }
386 412 else if (theme == QChart::ChartThemeBrownSand) {
387 413 pal.setColor(QPalette::Window, QRgb(0x9e8965));
388 414 pal.setColor(QPalette::WindowText, QRgb(0x404044));
389 415 }
390 416 else if (theme == QChart::ChartThemeBlueNcs) {
391 417 pal.setColor(QPalette::Window, QRgb(0x018bba));
392 418 pal.setColor(QPalette::WindowText, QRgb(0x404044));
393 419 }
394 420 else if (theme == QChart::ChartThemeHighContrast) {
395 421 pal.setColor(QPalette::Window, QRgb(0xffab03));
396 422 pal.setColor(QPalette::WindowText, QRgb(0x181818));
397 423 }
398 424 else if (theme == QChart::ChartThemeBlueIcy) {
399 425 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
400 426 pal.setColor(QPalette::WindowText, QRgb(0x404044));
401 427 }
402 428 else {
403 429 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
404 430 pal.setColor(QPalette::WindowText, QRgb(0x404044));
405 431 }
406 432 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
407 433 widget->setPalette(pal);
408 434 }
409 435 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
436 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
410 437
411 438 QChart::AnimationOptions options(
412 439 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
413 440 if (m_chartList.at(0)->animationOptions() != options) {
414 441 foreach (QChart *chart, m_chartList)
415 442 chart->setAnimationOptions(options);
416 443 }
417 444
418 445 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
419 446
420 447 if (!alignment) {
421 448 foreach (QChart *chart, m_chartList) {
422 449 chart->legend()->hide();
423 450 }
424 451 }
425 452 else {
426 453 foreach (QChart *chart, m_chartList) {
427 454 chart->legend()->setAlignment(alignment);
428 455 chart->legend()->show();
429 456 }
430 457 }
431 458
432 bool checked = m_antialiasCheckBox->isChecked();
459 bool antialias = m_antialiasCheckBox->isChecked();
460
433 461 if (opengl)
434 m_view->setRenderHint(QPainter::HighQualityAntialiasing, checked);
462 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
435 463 else
436 m_view->setRenderHint(QPainter::Antialiasing, checked);
464 m_view->setRenderHint(QPainter::Antialiasing, antialias);
465
466 bool scroll = m_scrollCheckBox->isChecked();
467
468 if(!m_scroll & scroll){
469 m_scroll=true;
470 m_zoom=false;
471 m_zoomCheckBox->setChecked(false);
472 }
473
474 bool zoom = m_zoomCheckBox->isChecked();
475
476 if(!m_zoom & zoom){
477 m_scroll=false;
478 m_zoom=true;
479 m_scrollCheckBox->setChecked(false);
480 }
481
482 }
483
484 void Window::mousePressEvent(QMouseEvent *event)
485 {
486 if (event->button() == Qt::LeftButton) {
487
488 m_origin = event->pos();
489 m_isScrolling = false;
490 m_isZooming = false;
491
492 foreach (QChart *chart, m_chartList) {
493
494 QRectF geometryRect = chart->geometry();
495 QRectF plotArea = chart->plotArea();
496 plotArea.translate(geometryRect.topLeft());
497 if (plotArea.contains(m_origin)) {
498 m_isScrolling = m_scroll;
499 m_isZooming = m_zoom;
500 break;
501 }
502 }
503
504 if (m_isZooming) {
505 m_rubberBand->setRect(QRectF(m_origin, QSize()));
506 m_rubberBand->setVisible(true);
507 }
508 event->accept();
509 }
510
511 if (event->button() == Qt::RightButton) {
512 m_origin = event->pos();
513 m_isZooming = m_zoom;
514 }
515 }
516
517 void Window::mouseMoveEvent(QMouseEvent *event)
518 {
519 if (m_isScrolling || m_isZooming) {
520
521 foreach (QChart *chart, m_chartList) {
522
523 QRectF geometryRect = chart->geometry();
524 QRectF plotArea = chart->plotArea();
525 plotArea.translate(geometryRect.topLeft());
526
527 if (plotArea.contains(m_origin)) {
528 if (m_isScrolling) {
529 QPointF delta = m_origin - event->pos();
530 chart->scroll(delta.x(), -delta.y());
531 }
532 if (m_isZooming && plotArea.contains(event->pos())) {
533 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
534 }
535 break;
536 }
537 }
538 if(m_isScrolling) m_origin = event->pos();
539 event->accept();
540 }
437 541 }
438 542
543 void Window::mouseReleaseEvent(QMouseEvent *event)
544 {
545 if (event->button() == Qt::LeftButton) {
546 m_isScrolling = false;
547 if (m_isZooming) {
548 m_isZooming = false;
549 m_rubberBand->setVisible(false);
550
551 foreach (QChart *chart, m_chartList) {
552
553 QRectF geometryRect = chart->geometry();
554 QRectF plotArea = chart->plotArea();
555 plotArea.translate(geometryRect.topLeft());
556
557 if (plotArea.contains(m_origin)) {
558 QRectF rect = m_rubberBand->rect();
559 chart->zoomIn(rect);
560 break;
561 }
562 }
563 }
564 event->accept();
565 }
566
567 if (event->button() == Qt::RightButton) {
568
569 if (m_isZooming) {
570 foreach (QChart *chart, m_chartList) {
571
572 QRectF geometryRect = chart->geometry();
573 QRectF plotArea = chart->plotArea();
574 plotArea.translate(geometryRect.topLeft());
575
576 if (plotArea.contains(m_origin)) {
577 QRectF rect = m_rubberBand->rect();
578 chart->zoomOut();
579 break;
580 }
581 }
582 }
583 }
584 }
@@ -1,110 +1,102
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #ifndef CHARTWINDOW_H_
22 #define CHARTWINDOW_H_
21 #ifndef WINDOW_H_
22 #define WINDOW_H_
23 23 #include <QMainWindow>
24 24 #include <QChartGlobal>
25 25 #include <QHash>
26 #include <QGraphicsView>
27 #include <QResizeEvent>
28 #include <QGraphicsWidget>
29 26
30 27 class QComboBox;
31 28 class QCheckBox;
29 class QGraphicsRectItem;
30 class QGraphicsScene;
31 class QGraphicsWidget;
32 class View;
32 33
33 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 35 class QChart;
35 36 QTCOMMERCIALCHART_END_NAMESPACE
36 37
37 38 typedef QPair<QPointF, QString> Data;
38 39 typedef QList<Data> DataList;
39 40 typedef QList<DataList> DataTable;
40 41
41 class QGraphicsScene;
42
43 42 QTCOMMERCIALCHART_USE_NAMESPACE
44 43
45
46 class GraphicsView: public QGraphicsView
47 {
48 public:
49 GraphicsView(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent = 0):QGraphicsView(scene,parent), m_form(form)
50 {
51 setWindowTitle(tr("Charts"));
52 }
53
54 protected:
55 void resizeEvent(QResizeEvent *event)
56 {
57 if (scene())
58 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
59 if (m_form)
60 m_form->resize(QSizeF(event->size()));
61 QGraphicsView::resizeEvent(event);
62 }
63 private:
64 QGraphicsWidget *m_form;
65 };
66
67
68 class ChartWindow: public QMainWindow
44 class Window: public QMainWindow
69 45 {
70 46 Q_OBJECT
71 47 public:
72 explicit ChartWindow(QWidget *parent = 0);
73 ~ChartWindow();
48 explicit Window(QWidget *parent = 0);
49 ~Window();
74 50
75 51 private Q_SLOTS:
76 52 void updateUI();
77 53
54
78 55 private:
79 56 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
80 57 QComboBox* createThemeBox() const;
81 58 QComboBox* createAnimationBox() const;
82 59 QComboBox* createLegendBox() const;
83 60 void connectSignals();
84 61 QChart* createAreaChart() const;
85 62 QChart* createBarChart(int valueCount) const;
86 63 QChart* createPieChart() const;
87 64 QChart* createLineChart() const;
88 65 QChart* createSplineChart() const;
89 66 QChart* createScatterChart() const;
90 67 void createProxyWidgets();
91 68
69 protected:
70 void mousePressEvent(QMouseEvent *event);
71 void mouseMoveEvent(QMouseEvent *event);
72 void mouseReleaseEvent(QMouseEvent *event);
73
74
92 75 private:
93 76 int m_listCount;
94 77 int m_valueMax;
95 78 int m_valueCount;
96 79 QGraphicsScene* m_scene;
97 GraphicsView* m_view;
80 View* m_view;
98 81 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
99 82 QList<QChart*> m_chartList;
100 83 DataTable m_dataTable;
101 84
102 85 QGraphicsWidget *m_form;
103 86 QComboBox *m_themeComboBox;
104 87 QCheckBox *m_antialiasCheckBox;
105 88 QComboBox *m_animatedComboBox;
106 89 QComboBox *m_legendComboBox;
107 90 QCheckBox *m_openGLCheckBox;
91 QCheckBox *m_zoomCheckBox;
92 QCheckBox *m_scrollCheckBox;
93 QPoint m_origin;
94 QGraphicsRectItem* m_rubberBand;
95
96 bool m_isScrolling;
97 bool m_isZooming;
98 bool m_scroll;
99 bool m_zoom;
108 100 };
109 101
110 102 #endif
General Comments 0
You need to be logged in to leave comments. Login now