@@ -1,430 +1,433 | |||
|
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 | 21 | #include "window.h" |
|
22 | 22 | #include "view.h" |
|
23 | 23 | #include "charts.h" |
|
24 | 24 | |
|
25 | 25 | #include <QChartView> |
|
26 | 26 | #include <QPieSeries> |
|
27 | 27 | #include <QPieSlice> |
|
28 | 28 | #include <QPercentBarSeries> |
|
29 | 29 | #include <QStackedBarSeries> |
|
30 | 30 | #include <QBarSeries> |
|
31 | 31 | #include <QBarSet> |
|
32 | 32 | #include <QLineSeries> |
|
33 | 33 | #include <QSplineSeries> |
|
34 | 34 | #include <QScatterSeries> |
|
35 | 35 | #include <QAreaSeries> |
|
36 | 36 | #include <QLegend> |
|
37 | 37 | #include <QGridLayout> |
|
38 | 38 | #include <QFormLayout> |
|
39 | 39 | #include <QComboBox> |
|
40 | 40 | #include <QSpinBox> |
|
41 | 41 | #include <QCheckBox> |
|
42 | 42 | #include <QGroupBox> |
|
43 | 43 | #include <QLabel> |
|
44 | 44 | #include <QTime> |
|
45 | 45 | #include <QBarCategoriesAxis> |
|
46 | 46 | #include <QGraphicsScene> |
|
47 | 47 | #include <QGraphicsGridLayout> |
|
48 | 48 | #include <QGraphicsLinearLayout> |
|
49 | 49 | #include <QGraphicsProxyWidget> |
|
50 | 50 | #include <QGLWidget> |
|
51 | 51 | #include <QApplication> |
|
52 | 52 | #include <QDebug> |
|
53 | 53 | |
|
54 | 54 | Window::Window(QWidget* parent) : |
|
55 | 55 | QMainWindow(parent), |
|
56 | 56 | m_listCount(3), |
|
57 | 57 | m_valueMax(10), |
|
58 | 58 | m_valueCount(7), |
|
59 | 59 | m_scene(new QGraphicsScene(this)), |
|
60 | 60 | m_view(0), |
|
61 | 61 | m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)), |
|
62 | 62 | m_form(0), |
|
63 | 63 | m_themeComboBox(0), |
|
64 | 64 | m_antialiasCheckBox(0), |
|
65 | 65 | m_animatedComboBox(0), |
|
66 | 66 | m_legendComboBox(0), |
|
67 | 67 | m_openGLCheckBox(0), |
|
68 | 68 | m_zoomCheckBox(0), |
|
69 | 69 | m_scrollCheckBox(0), |
|
70 | 70 | m_rubberBand(new QGraphicsRectItem()), |
|
71 | 71 | m_isScrolling(false), |
|
72 | 72 | m_isZooming(false), |
|
73 | 73 | m_scroll(false), |
|
74 | 74 | m_zoom(false) |
|
75 | 75 | { |
|
76 | 76 | createProxyWidgets(); |
|
77 | 77 | connectSignals(); |
|
78 | 78 | |
|
79 | 79 | // create layout |
|
80 | 80 | QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout(); |
|
81 | 81 | QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout(); |
|
82 | 82 | settingsLayout->setOrientation(Qt::Vertical); |
|
83 | 83 | settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
|
84 | 84 | settingsLayout->addItem(m_widgetHash["openGLCheckBox"]); |
|
85 | 85 | settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]); |
|
86 | 86 | settingsLayout->addItem(m_widgetHash["themeLabel"]); |
|
87 | 87 | settingsLayout->addItem(m_widgetHash["themeComboBox"]); |
|
88 | 88 | settingsLayout->addItem(m_widgetHash["animationsLabel"]); |
|
89 | 89 | settingsLayout->addItem(m_widgetHash["animatedComboBox"]); |
|
90 | 90 | settingsLayout->addItem(m_widgetHash["legendLabel"]); |
|
91 | 91 | settingsLayout->addItem(m_widgetHash["legendComboBox"]); |
|
92 | 92 | settingsLayout->addItem(m_widgetHash["scrollCheckBox"]); |
|
93 | 93 | settingsLayout->addItem(m_widgetHash["zoomCheckBox"]); |
|
94 | 94 | settingsLayout->addStretch(); |
|
95 | 95 | baseLayout->addItem(settingsLayout, 0, 3, 2, 1); |
|
96 | 96 | //create charts |
|
97 | 97 | |
|
98 | int i = m_widgetHash.count(); | |
|
99 | foreach(QGraphicsProxyWidget* widget , m_widgetHash) { | |
|
100 | widget->setZValue(i--); | |
|
101 | widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); | |
|
102 | } | |
|
103 | ||
|
104 | ||
|
105 | 98 | Charts::ChartList list = Charts::chartList(); |
|
106 | 99 | |
|
107 | 100 | for(int i = 0 ; i < 6 ; ++i) |
|
108 | 101 | { |
|
109 | 102 | if(!(i<list.size()) || list.isEmpty()) break; |
|
110 | 103 | QChart* chart = list.at(i)->createChart(m_dataTable); |
|
111 | 104 | baseLayout->addItem(chart, i/3, i%3); |
|
112 | 105 | m_chartList << chart; |
|
113 | 106 | } |
|
114 | 107 | |
|
115 | 108 | m_form = new QGraphicsWidget(); |
|
116 | 109 | m_form->setLayout(baseLayout); |
|
117 | 110 | m_scene->addItem(m_form); |
|
118 | 111 | m_scene->addItem(m_rubberBand); |
|
119 | 112 | m_rubberBand->setVisible(false); |
|
120 | 113 | |
|
121 | 114 | m_view = new View(m_scene, m_form); |
|
122 | 115 | m_view->setMinimumSize(m_form->preferredSize().toSize()); |
|
123 | 116 | |
|
124 | 117 | // Set defaults |
|
125 | 118 | m_antialiasCheckBox->setChecked(true); |
|
126 | 119 | updateUI(); |
|
127 | 120 | setCentralWidget(m_view); |
|
128 | 121 | } |
|
129 | 122 | |
|
130 | 123 | Window::~Window() |
|
131 | 124 | { |
|
132 | 125 | } |
|
133 | 126 | |
|
134 | 127 | void Window::connectSignals() |
|
135 | 128 | { |
|
136 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
137 | connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
138 | connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
139 | connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
140 | connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
141 | connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
142 | connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
129 | QObject::connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
130 | QObject::connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
131 | QObject::connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
132 | QObject::connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
133 | QObject::connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
134 | QObject::connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
135 | QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
143 | 136 | } |
|
144 | 137 | |
|
145 | 138 | DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const |
|
146 | 139 | { |
|
147 | 140 | DataTable dataTable; |
|
148 | 141 | |
|
149 | 142 | // set seed for random stuff |
|
150 | 143 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); |
|
151 | 144 | |
|
152 | 145 | // generate random data |
|
153 | 146 | for (int i(0); i < listCount; i++) { |
|
154 | 147 | DataList dataList; |
|
155 | 148 | qreal yValue(0); |
|
156 | 149 | for (int j(0); j < valueCount; j++) { |
|
157 | 150 | yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; |
|
158 | 151 | QPointF value( |
|
159 | 152 | (j + (qreal) qrand() / (qreal) RAND_MAX) |
|
160 | 153 | * ((qreal) m_valueMax / (qreal) valueCount), yValue); |
|
161 | 154 | QString label = "Slice " + QString::number(i) + ":" + QString::number(j); |
|
162 | 155 | dataList << Data(value, label); |
|
163 | 156 | } |
|
164 | 157 | dataTable << dataList; |
|
165 | 158 | } |
|
166 | 159 | |
|
167 | 160 | return dataTable; |
|
168 | 161 | } |
|
169 | 162 | |
|
170 | 163 | void Window::createProxyWidgets() |
|
171 | 164 | { |
|
172 | 165 | m_themeComboBox = createThemeBox(); |
|
173 | 166 | m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing")); |
|
174 | 167 | m_animatedComboBox = createAnimationBox(); |
|
175 | 168 | m_legendComboBox = createLegendBox(); |
|
176 | 169 | m_openGLCheckBox = new QCheckBox(tr("OpenGL")); |
|
177 | 170 | m_zoomCheckBox = new QCheckBox(tr("Zoom")); |
|
178 | 171 | m_scrollCheckBox = new QCheckBox(tr("Scroll")); |
|
179 | 172 | m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox); |
|
180 | 173 | m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox); |
|
181 | 174 | m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox); |
|
182 | 175 | m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox); |
|
183 | 176 | m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox); |
|
184 | 177 | m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme")); |
|
185 | 178 | m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations")); |
|
186 | 179 | m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend")); |
|
187 | 180 | m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox); |
|
188 | 181 | m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox); |
|
189 | 182 | } |
|
190 | 183 | |
|
191 |
QComboBox* Window::createThemeBox() |
|
|
184 | QComboBox* Window::createThemeBox() | |
|
192 | 185 | { |
|
193 | 186 | // settings layoutQGLWidgetβ |
|
194 |
QComboBox* themeComboBox = new |
|
|
187 | QComboBox* themeComboBox = new ComboBox(this); | |
|
195 | 188 | themeComboBox->addItem("Light", QChart::ChartThemeLight); |
|
196 | 189 | themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); |
|
197 | 190 | themeComboBox->addItem("Dark", QChart::ChartThemeDark); |
|
198 | 191 | themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); |
|
199 | 192 | themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); |
|
200 | 193 | themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); |
|
201 | 194 | themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); |
|
202 | 195 | return themeComboBox; |
|
203 | 196 | } |
|
204 | 197 | |
|
205 |
QComboBox* Window::createAnimationBox() |
|
|
198 | QComboBox* Window::createAnimationBox() | |
|
206 | 199 | { |
|
207 | 200 | // settings layout |
|
208 |
QComboBox* animationComboBox = new |
|
|
201 | QComboBox* animationComboBox = new ComboBox(this); | |
|
209 | 202 | animationComboBox->addItem("No Animations", QChart::NoAnimation); |
|
210 | 203 | animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); |
|
211 | 204 | animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); |
|
212 | 205 | animationComboBox->addItem("All Animations", QChart::AllAnimations); |
|
213 | 206 | return animationComboBox; |
|
214 | 207 | } |
|
215 | 208 | |
|
216 |
QComboBox* Window::createLegendBox() |
|
|
209 | QComboBox* Window::createLegendBox() | |
|
217 | 210 | { |
|
218 |
QComboBox* legendComboBox = new |
|
|
211 | QComboBox* legendComboBox = new ComboBox(this); | |
|
219 | 212 | legendComboBox->addItem("No Legend ", 0); |
|
220 | 213 | legendComboBox->addItem("Legend Top", Qt::AlignTop); |
|
221 | 214 | legendComboBox->addItem("Legend Bottom", Qt::AlignBottom); |
|
222 | 215 | legendComboBox->addItem("Legend Left", Qt::AlignLeft); |
|
223 | 216 | legendComboBox->addItem("Legend Right", Qt::AlignRight); |
|
224 | 217 | return legendComboBox; |
|
225 | 218 | } |
|
226 | 219 | |
|
227 | 220 | void Window::updateUI() |
|
228 | 221 | { |
|
229 | 222 | bool opengl = m_openGLCheckBox->isChecked(); |
|
230 | 223 | bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport()); |
|
231 | 224 | if ((isOpengl && !opengl) || (!isOpengl && opengl)) { |
|
232 | 225 | m_view->deleteLater(); |
|
233 | 226 | m_view = new View(m_scene, m_form); |
|
234 | 227 | m_view->setViewport(!opengl ? new QWidget() : new QGLWidget()); |
|
235 | 228 | setCentralWidget(m_view); |
|
236 | 229 | } |
|
237 | 230 | |
|
238 | 231 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData( |
|
239 | 232 | m_themeComboBox->currentIndex()).toInt(); |
|
240 | 233 | |
|
241 | 234 | foreach (QChart *chart, m_chartList) |
|
242 | 235 | chart->setTheme(theme); |
|
243 | 236 | |
|
244 | 237 | QPalette pal = window()->palette(); |
|
245 | 238 | if (theme == QChart::ChartThemeLight) { |
|
246 | 239 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
247 | 240 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
248 | 241 | } |
|
249 | 242 | else if (theme == QChart::ChartThemeDark) { |
|
250 | 243 | pal.setColor(QPalette::Window, QRgb(0x121218)); |
|
251 | 244 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
252 | 245 | } |
|
253 | 246 | else if (theme == QChart::ChartThemeBlueCerulean) { |
|
254 | 247 | pal.setColor(QPalette::Window, QRgb(0x40434a)); |
|
255 | 248 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
256 | 249 | } |
|
257 | 250 | else if (theme == QChart::ChartThemeBrownSand) { |
|
258 | 251 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); |
|
259 | 252 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
260 | 253 | } |
|
261 | 254 | else if (theme == QChart::ChartThemeBlueNcs) { |
|
262 | 255 | pal.setColor(QPalette::Window, QRgb(0x018bba)); |
|
263 | 256 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
264 | 257 | } |
|
265 | 258 | else if (theme == QChart::ChartThemeHighContrast) { |
|
266 | 259 | pal.setColor(QPalette::Window, QRgb(0xffab03)); |
|
267 | 260 | pal.setColor(QPalette::WindowText, QRgb(0x181818)); |
|
268 | 261 | } |
|
269 | 262 | else if (theme == QChart::ChartThemeBlueIcy) { |
|
270 | 263 | pal.setColor(QPalette::Window, QRgb(0xcee7f0)); |
|
271 | 264 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
272 | 265 | } |
|
273 | 266 | else { |
|
274 | 267 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
275 | 268 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
276 | 269 | } |
|
277 | 270 | foreach(QGraphicsProxyWidget* widget , m_widgetHash) { |
|
278 | 271 | widget->setPalette(pal); |
|
279 | 272 | } |
|
280 | 273 | m_view->setBackgroundBrush(pal.color((QPalette::Window))); |
|
281 | 274 | m_rubberBand->setPen(pal.color((QPalette::WindowText))); |
|
282 | 275 | |
|
283 | 276 | QChart::AnimationOptions options( |
|
284 | 277 | m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); |
|
285 | 278 | if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) { |
|
286 | 279 | foreach (QChart *chart, m_chartList) |
|
287 | 280 | chart->setAnimationOptions(options); |
|
288 | 281 | } |
|
289 | 282 | |
|
290 | 283 | Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); |
|
291 | 284 | |
|
292 | 285 | if (!alignment) { |
|
293 | 286 | foreach (QChart *chart, m_chartList) { |
|
294 | 287 | chart->legend()->hide(); |
|
295 | 288 | } |
|
296 | 289 | } |
|
297 | 290 | else { |
|
298 | 291 | foreach (QChart *chart, m_chartList) { |
|
299 | 292 | chart->legend()->setAlignment(alignment); |
|
300 | 293 | chart->legend()->show(); |
|
301 | 294 | } |
|
302 | 295 | } |
|
303 | 296 | |
|
304 | 297 | bool antialias = m_antialiasCheckBox->isChecked(); |
|
305 | 298 | |
|
306 | 299 | if (opengl) |
|
307 | 300 | m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias); |
|
308 | 301 | else |
|
309 | 302 | m_view->setRenderHint(QPainter::Antialiasing, antialias); |
|
310 | 303 | |
|
311 | 304 | bool scroll = m_scrollCheckBox->isChecked(); |
|
312 | 305 | |
|
313 | 306 | if(!m_scroll & scroll){ |
|
314 | 307 | m_scroll=true; |
|
315 | 308 | m_zoom=false; |
|
316 | 309 | m_zoomCheckBox->setChecked(false); |
|
317 | 310 | } |
|
318 | 311 | |
|
319 | 312 | bool zoom = m_zoomCheckBox->isChecked(); |
|
320 | 313 | |
|
321 | 314 | if(!m_zoom & zoom){ |
|
322 | 315 | m_scroll=false; |
|
323 | 316 | m_zoom=true; |
|
324 | 317 | m_scrollCheckBox->setChecked(false); |
|
325 | 318 | } |
|
326 | 319 | |
|
327 | 320 | } |
|
328 | 321 | |
|
329 | 322 | void Window::mousePressEvent(QMouseEvent *event) |
|
330 | 323 | { |
|
331 | 324 | if (event->button() == Qt::LeftButton) { |
|
332 | 325 | |
|
333 | 326 | m_origin = event->pos(); |
|
334 | 327 | m_isScrolling = false; |
|
335 | 328 | m_isZooming = false; |
|
336 | 329 | |
|
337 | 330 | foreach (QChart *chart, m_chartList) { |
|
338 | 331 | |
|
339 | 332 | QRectF geometryRect = chart->geometry(); |
|
340 | 333 | QRectF plotArea = chart->plotArea(); |
|
341 | 334 | plotArea.translate(geometryRect.topLeft()); |
|
342 | 335 | if (plotArea.contains(m_origin)) { |
|
343 | 336 | m_isScrolling = m_scroll; |
|
344 | 337 | m_isZooming = m_zoom; |
|
345 | 338 | break; |
|
346 | 339 | } |
|
347 | 340 | } |
|
348 | 341 | |
|
349 | 342 | if (m_isZooming) { |
|
350 | 343 | m_rubberBand->setRect(QRectF(m_origin, QSize())); |
|
351 | 344 | m_rubberBand->setVisible(true); |
|
352 | 345 | } |
|
353 | 346 | event->accept(); |
|
354 | 347 | } |
|
355 | 348 | |
|
356 | 349 | if (event->button() == Qt::RightButton) { |
|
357 | 350 | m_origin = event->pos(); |
|
358 | 351 | m_isZooming = m_zoom; |
|
359 | 352 | } |
|
360 | 353 | } |
|
361 | 354 | |
|
362 | 355 | void Window::mouseMoveEvent(QMouseEvent *event) |
|
363 | 356 | { |
|
364 | 357 | if (m_isScrolling || m_isZooming) { |
|
365 | 358 | |
|
366 | 359 | foreach (QChart *chart, m_chartList) { |
|
367 | 360 | |
|
368 | 361 | QRectF geometryRect = chart->geometry(); |
|
369 | 362 | QRectF plotArea = chart->plotArea(); |
|
370 | 363 | plotArea.translate(geometryRect.topLeft()); |
|
371 | 364 | |
|
372 | 365 | if (plotArea.contains(m_origin)) { |
|
373 | 366 | if (m_isScrolling) { |
|
374 | 367 | QPointF delta = m_origin - event->pos(); |
|
375 | 368 | chart->scroll(delta.x(), -delta.y()); |
|
376 | 369 | } |
|
377 | 370 | if (m_isZooming && plotArea.contains(event->pos())) { |
|
378 | 371 | m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized()); |
|
379 | 372 | } |
|
380 | 373 | break; |
|
381 | 374 | } |
|
382 | 375 | } |
|
383 | 376 | if(m_isScrolling) m_origin = event->pos(); |
|
384 | 377 | event->accept(); |
|
385 | 378 | } |
|
386 | 379 | } |
|
387 | 380 | |
|
388 | 381 | void Window::mouseReleaseEvent(QMouseEvent *event) |
|
389 | 382 | { |
|
390 | 383 | if (event->button() == Qt::LeftButton) { |
|
391 | 384 | m_isScrolling = false; |
|
392 | 385 | if (m_isZooming) { |
|
393 | 386 | m_isZooming = false; |
|
394 | 387 | m_rubberBand->setVisible(false); |
|
395 | 388 | |
|
396 | 389 | foreach (QChart *chart, m_chartList) { |
|
397 | 390 | |
|
398 | 391 | QRectF geometryRect = chart->geometry(); |
|
399 | 392 | QRectF plotArea = chart->plotArea(); |
|
400 | 393 | plotArea.translate(geometryRect.topLeft()); |
|
401 | 394 | |
|
402 | 395 | if (plotArea.contains(m_origin)) { |
|
403 | 396 | QRectF rect = m_rubberBand->rect(); |
|
404 | 397 | rect.translate(-geometryRect.topLeft()); |
|
405 | 398 | chart->zoomIn(rect); |
|
406 | 399 | break; |
|
407 | 400 | } |
|
408 | 401 | } |
|
409 | 402 | } |
|
410 | 403 | event->accept(); |
|
411 | 404 | } |
|
412 | 405 | |
|
413 | 406 | if (event->button() == Qt::RightButton) { |
|
414 | 407 | |
|
415 | 408 | if (m_isZooming) { |
|
416 | 409 | foreach (QChart *chart, m_chartList) { |
|
417 | 410 | |
|
418 | 411 | QRectF geometryRect = chart->geometry(); |
|
419 | 412 | QRectF plotArea = chart->plotArea(); |
|
420 | 413 | plotArea.translate(geometryRect.topLeft()); |
|
421 | 414 | |
|
422 | 415 | if (plotArea.contains(m_origin)) { |
|
423 | 416 | QRectF rect = m_rubberBand->rect(); |
|
424 | 417 | chart->zoomOut(); |
|
425 | 418 | break; |
|
426 | 419 | } |
|
427 | 420 | } |
|
428 | 421 | } |
|
429 | 422 | } |
|
430 | 423 | } |
|
424 | ||
|
425 | void Window::comboBoxFocused(QComboBox *combobox) | |
|
426 | { | |
|
427 | foreach(QGraphicsProxyWidget* widget , m_widgetHash) { | |
|
428 | if(widget->widget()==combobox) | |
|
429 | widget->setZValue(2.0); | |
|
430 | else | |
|
431 | widget->setZValue(0.0); | |
|
432 | } | |
|
433 | } |
@@ -1,96 +1,116 | |||
|
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 | 21 | #ifndef WINDOW_H_ |
|
22 | 22 | #define WINDOW_H_ |
|
23 | 23 | #include <QMainWindow> |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | #include <QHash> |
|
26 | #include <QComboBox> | |
|
26 | 27 | |
|
27 | class QComboBox; | |
|
28 | 28 | class QCheckBox; |
|
29 | 29 | class QGraphicsRectItem; |
|
30 | 30 | class QGraphicsScene; |
|
31 | 31 | class QGraphicsWidget; |
|
32 | 32 | class View; |
|
33 | 33 | |
|
34 | 34 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | 35 | class QChart; |
|
36 | 36 | QTCOMMERCIALCHART_END_NAMESPACE |
|
37 | 37 | |
|
38 | 38 | typedef QPair<QPointF, QString> Data; |
|
39 | 39 | typedef QList<Data> DataList; |
|
40 | 40 | typedef QList<DataList> DataTable; |
|
41 | 41 | |
|
42 | 42 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
43 | 43 | |
|
44 | ||
|
44 | 45 | class Window: public QMainWindow |
|
45 | 46 | { |
|
46 | 47 | Q_OBJECT |
|
47 | 48 | public: |
|
48 | 49 | explicit Window(QWidget *parent = 0); |
|
49 | 50 | ~Window(); |
|
50 | 51 | |
|
51 | 52 | private Q_SLOTS: |
|
52 | 53 | void updateUI(); |
|
53 | 54 | |
|
54 | 55 | |
|
55 | 56 | private: |
|
56 | 57 | DataTable generateRandomData(int listCount,int valueMax,int valueCount) const; |
|
57 |
QComboBox* createThemeBox() |
|
|
58 |
QComboBox* createAnimationBox() |
|
|
59 |
QComboBox* createLegendBox() |
|
|
58 | QComboBox* createThemeBox(); | |
|
59 | QComboBox* createAnimationBox(); | |
|
60 | QComboBox* createLegendBox(); | |
|
60 | 61 | void connectSignals(); |
|
61 | 62 | void createProxyWidgets(); |
|
63 | void comboBoxFocused(QComboBox *combox); | |
|
62 | 64 | |
|
63 | 65 | protected: |
|
64 | 66 | void mousePressEvent(QMouseEvent *event); |
|
65 | 67 | void mouseMoveEvent(QMouseEvent *event); |
|
66 | 68 | void mouseReleaseEvent(QMouseEvent *event); |
|
67 | 69 | |
|
68 | 70 | |
|
69 | 71 | private: |
|
70 | 72 | int m_listCount; |
|
71 | 73 | int m_valueMax; |
|
72 | 74 | int m_valueCount; |
|
73 | 75 | QGraphicsScene* m_scene; |
|
74 | 76 | View* m_view; |
|
75 | 77 | QHash<QString, QGraphicsProxyWidget*> m_widgetHash; |
|
76 | 78 | QList<QChart*> m_chartList; |
|
77 | 79 | DataTable m_dataTable; |
|
78 | 80 | |
|
79 | 81 | QGraphicsWidget *m_form; |
|
80 | 82 | QComboBox *m_themeComboBox; |
|
81 | 83 | QCheckBox *m_antialiasCheckBox; |
|
82 | 84 | QComboBox *m_animatedComboBox; |
|
83 | 85 | QComboBox *m_legendComboBox; |
|
84 | 86 | QCheckBox *m_openGLCheckBox; |
|
85 | 87 | QCheckBox *m_zoomCheckBox; |
|
86 | 88 | QCheckBox *m_scrollCheckBox; |
|
87 | 89 | QPoint m_origin; |
|
88 | 90 | QGraphicsRectItem* m_rubberBand; |
|
89 | 91 | |
|
90 | 92 | bool m_isScrolling; |
|
91 | 93 | bool m_isZooming; |
|
92 | 94 | bool m_scroll; |
|
93 | 95 | bool m_zoom; |
|
96 | ||
|
97 | friend class ComboBox; | |
|
98 | }; | |
|
99 | ||
|
100 | class ComboBox: public QComboBox | |
|
101 | { | |
|
102 | public: | |
|
103 | ComboBox(Window* window,QWidget *parent = 0):QComboBox(parent),m_window(window) | |
|
104 | {} | |
|
105 | ||
|
106 | protected: | |
|
107 | void focusInEvent(QFocusEvent *e) | |
|
108 | { | |
|
109 | QComboBox::focusInEvent(e); | |
|
110 | m_window->comboBoxFocused(this); | |
|
111 | } | |
|
112 | private: | |
|
113 | Window* m_window; | |
|
94 | 114 | }; |
|
95 | 115 | |
|
96 | 116 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now