##// END OF EJS Templates
Adds charts create selection to chartviewer
Michal Klocek -
r1840:8077b9f83c91
parent child
Show More
@@ -34,6 +34,7 protected:
34 34 void resizeEvent(QResizeEvent *event);
35 35 void mouseMoveEvent(QMouseEvent *event);
36 36 void mouseReleaseEvent(QMouseEvent *event);
37
37 38 private:
38 39 QGraphicsWidget *m_form;
39 40 };
@@ -38,6 +38,7
38 38 #include <QGLWidget>
39 39 #include <QApplication>
40 40 #include <QDebug>
41 #include <QMenu>
41 42
42 43 Window::Window(QWidget* parent) :
43 44 QMainWindow(parent),
@@ -56,16 +57,15 Window::Window(QWidget* parent) :
56 57 m_zoomCheckBox(0),
57 58 m_scrollCheckBox(0),
58 59 m_rubberBand(new QGraphicsRectItem()),
59 m_isScrolling(false),
60 m_isZooming(false),
61 m_scroll(false),
62 m_zoom(false)
60 m_baseLayout(new QGraphicsGridLayout()),
61 m_menu(new QMenu(this)),
62 m_state(NoState),
63 m_currentState(NoState)
63 64 {
64 65 createProxyWidgets();
65 66 connectSignals();
66 67
67 68 // create layout
68 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
69 69 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
70 70 settingsLayout->setOrientation(Qt::Vertical);
71 71 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
@@ -80,19 +80,23 Window::Window(QWidget* parent) :
80 80 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
81 81 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
82 82 settingsLayout->addStretch();
83 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
83 m_baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
84
84 85 //create charts
85 86 Charts::ChartList list = Charts::chartList();
86 87
87 for(int i = 0 ; i < 9 && i<list.size() ; ++i)
88 {
88 for (int i = 0; i < 9 && i < list.size(); ++i) {
89 89 QChart* chart = list.at(i)->createChart(m_dataTable);
90 baseLayout->addItem(chart, i/3, i%3);
91 m_chartList << chart;
90 m_baseLayout->addItem(chart, i / 3, i % 3);
91 m_chartHash[chart] = i;
92 }
93
94 foreach(Chart* chart, list) {
95 createMenuAction(m_menu, QIcon(), chart->name(), qVariantFromValue((void *) chart));
92 96 }
93 97
94 98 m_form = new QGraphicsWidget();
95 m_form->setLayout(baseLayout);
99 m_form->setLayout(m_baseLayout);
96 100 m_scene->addItem(m_form);
97 101 m_scene->addItem(m_rubberBand);
98 102 m_rubberBand->setVisible(false);
@@ -178,6 +182,32 QComboBox* Window::createLegendBox()
178 182
179 183 void Window::updateUI()
180 184 {
185 checkOpenGL();
186 checkTheme();
187 checkAnimationOptions();
188 checkLegend();
189 checkState();
190 }
191
192 void Window::checkLegend()
193 {
194 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
195
196 if (!alignment) {
197 foreach (QChart *chart, m_chartHash.keys()) {
198 chart->legend()->hide();
199 }
200 }
201 else {
202 foreach (QChart *chart, m_chartHash.keys()) {
203 chart->legend()->setAlignment(alignment);
204 chart->legend()->show();
205 }
206 }
207 }
208
209 void Window::checkOpenGL()
210 {
181 211 bool opengl = m_openGLCheckBox->isChecked();
182 212 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
183 213 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
@@ -187,10 +217,53 void Window::updateUI()
187 217 setCentralWidget(m_view);
188 218 }
189 219
220 bool antialias = m_antialiasCheckBox->isChecked();
221
222 if (opengl)
223 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
224 else
225 m_view->setRenderHint(QPainter::Antialiasing, antialias);
226 }
227
228 void Window::checkAnimationOptions()
229 {
230 QChart::AnimationOptions options(
231 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
232 if (!m_chartHash.isEmpty() && m_chartHash.keys().at(0)->animationOptions() != options) {
233 foreach (QChart *chart, m_chartHash.keys())
234 chart->setAnimationOptions(options);
235 }
236 }
237
238 void Window::checkState()
239 {
240 bool scroll = m_scrollCheckBox->isChecked();
241
242 if (m_state != ScrollState && scroll) {
243 m_state = ScrollState;
244 m_zoomCheckBox->setChecked(false);
245 }
246 else if (!scroll && m_state == ScrollState) {
247 m_state = NoState;
248 }
249
250 bool zoom = m_zoomCheckBox->isChecked();
251
252 if (m_state != ZoomState && zoom) {
253 m_state = ZoomState;
254 m_scrollCheckBox->setChecked(false);
255 }
256 else if (!zoom && m_state == ZoomState) {
257 m_state = NoState;
258 }
259 }
260
261 void Window::checkTheme()
262 {
190 263 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
191 264 m_themeComboBox->currentIndex()).toInt();
192 265
193 foreach (QChart *chart, m_chartList)
266 foreach (QChart *chart, m_chartHash.keys())
194 267 chart->setTheme(theme);
195 268
196 269 QPalette pal = window()->palette();
@@ -231,51 +304,6 void Window::updateUI()
231 304 }
232 305 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
233 306 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
234
235 QChart::AnimationOptions options(
236 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
237 if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) {
238 foreach (QChart *chart, m_chartList)
239 chart->setAnimationOptions(options);
240 }
241
242 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
243
244 if (!alignment) {
245 foreach (QChart *chart, m_chartList) {
246 chart->legend()->hide();
247 }
248 }
249 else {
250 foreach (QChart *chart, m_chartList) {
251 chart->legend()->setAlignment(alignment);
252 chart->legend()->show();
253 }
254 }
255
256 bool antialias = m_antialiasCheckBox->isChecked();
257
258 if (opengl)
259 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
260 else
261 m_view->setRenderHint(QPainter::Antialiasing, antialias);
262
263 bool scroll = m_scrollCheckBox->isChecked();
264
265 if(!m_scroll & scroll){
266 m_scroll=true;
267 m_zoom=false;
268 m_zoomCheckBox->setChecked(false);
269 }
270
271 bool zoom = m_zoomCheckBox->isChecked();
272
273 if(!m_zoom & zoom){
274 m_scroll=false;
275 m_zoom=true;
276 m_scrollCheckBox->setChecked(false);
277 }
278
279 307 }
280 308
281 309 void Window::mousePressEvent(QMouseEvent *event)
@@ -283,56 +311,59 void Window::mousePressEvent(QMouseEvent *event)
283 311 if (event->button() == Qt::LeftButton) {
284 312
285 313 m_origin = event->pos();
286 m_isScrolling = false;
287 m_isZooming = false;
314 m_currentState = NoState;
288 315
289 foreach (QChart *chart, m_chartList) {
316 foreach (QChart *chart, m_chartHash.keys()) {
290 317
291 318 QRectF geometryRect = chart->geometry();
292 319 QRectF plotArea = chart->plotArea();
293 320 plotArea.translate(geometryRect.topLeft());
294 321 if (plotArea.contains(m_origin)) {
295 m_isScrolling = m_scroll;
296 m_isZooming = m_zoom;
322 m_currentState = m_state;
323
324 if (m_currentState == NoState) {
325 showMenu(chart);
326 }
297 327 break;
298 328 }
299 329 }
300 330
301 if (m_isZooming) {
331 if (m_currentState == ZoomState) {
302 332 m_rubberBand->setRect(QRectF(m_origin, QSize()));
303 333 m_rubberBand->setVisible(true);
304 334 }
335
305 336 event->accept();
306 337 }
307 338
308 339 if (event->button() == Qt::RightButton) {
309 340 m_origin = event->pos();
310 m_isZooming = m_zoom;
341 m_currentState = m_state;
311 342 }
312 343 }
313 344
314 345 void Window::mouseMoveEvent(QMouseEvent *event)
315 346 {
316 if (m_isScrolling || m_isZooming) {
347 if ( m_currentState != NoState) {
317 348
318 foreach (QChart *chart, m_chartList) {
349 foreach (QChart *chart, m_chartHash.keys()) {
319 350
320 351 QRectF geometryRect = chart->geometry();
321 352 QRectF plotArea = chart->plotArea();
322 353 plotArea.translate(geometryRect.topLeft());
323 354
324 355 if (plotArea.contains(m_origin)) {
325 if (m_isScrolling) {
356 if (m_currentState == ScrollState) {
326 357 QPointF delta = m_origin - event->pos();
327 358 chart->scroll(delta.x(), -delta.y());
328 359 }
329 if (m_isZooming && plotArea.contains(event->pos())) {
360 if (m_currentState == ZoomState && plotArea.contains(event->pos())) {
330 361 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
331 362 }
332 363 break;
333 364 }
334 365 }
335 if(m_isScrolling) m_origin = event->pos();
366 if(m_currentState == ScrollState) m_origin = event->pos();
336 367 event->accept();
337 368 }
338 369 }
@@ -340,12 +371,10 void Window::mouseMoveEvent(QMouseEvent *event)
340 371 void Window::mouseReleaseEvent(QMouseEvent *event)
341 372 {
342 373 if (event->button() == Qt::LeftButton) {
343 m_isScrolling = false;
344 if (m_isZooming) {
345 m_isZooming = false;
374 if (m_currentState == ZoomState) {
346 375 m_rubberBand->setVisible(false);
347 376
348 foreach (QChart *chart, m_chartList) {
377 foreach (QChart *chart, m_chartHash.keys()) {
349 378
350 379 QRectF geometryRect = chart->geometry();
351 380 QRectF plotArea = chart->plotArea();
@@ -359,13 +388,15 void Window::mouseReleaseEvent(QMouseEvent *event)
359 388 }
360 389 }
361 390 }
391
392 m_currentState = NoState;
362 393 event->accept();
363 394 }
364 395
365 396 if (event->button() == Qt::RightButton) {
366 397
367 if (m_isZooming) {
368 foreach (QChart *chart, m_chartList) {
398 if (m_currentState == ZoomState) {
399 foreach (QChart *chart, m_chartHash.keys()) {
369 400
370 401 QRectF geometryRect = chart->geometry();
371 402 QRectF plotArea = chart->plotArea();
@@ -390,3 +421,30 void Window::comboBoxFocused(QComboBox *combobox)
390 421 widget->setZValue(0.0);
391 422 }
392 423 }
424
425 void Window::showMenu(QChart* qchart)
426 {
427 QAction *chosen = m_menu->exec(QCursor::pos());
428
429 if (chosen) {
430 Chart* chart = (Chart *) chosen->data().value<void *>();
431 int index = m_chartHash[qchart];
432 m_baseLayout->removeItem(qchart);
433 m_chartHash.remove(qchart);
434 QChart* newChart = chart->createChart(m_dataTable);
435 m_baseLayout->addItem(newChart, index / 3, index % 3);
436 m_chartHash[newChart] = index;
437 delete qchart;
438 updateUI();
439 }
440
441 }
442
443 QAction* Window::createMenuAction(QMenu *menu, const QIcon &icon, const QString &text,
444 const QVariant &data)
445 {
446 QAction *action = menu->addAction(icon, text);
447 action->setCheckable(false);
448 action->setData(data);
449 return action;
450 }
@@ -31,6 +31,8 class QGraphicsRectItem;
31 31 class QGraphicsScene;
32 32 class QGraphicsWidget;
33 33 class View;
34 class QGraphicsGridLayout;
35 class Chart;
34 36
35 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 38 class QChart;
@@ -42,6 +44,7 QTCOMMERCIALCHART_USE_NAMESPACE
42 44 class Window: public QMainWindow
43 45 {
44 46 Q_OBJECT
47 enum State{ NoState = 0, ZoomState, ScrollState};
45 48 public:
46 49 explicit Window(QWidget *parent = 0);
47 50 ~Window();
@@ -55,13 +58,19 private:
55 58 void connectSignals();
56 59 void createProxyWidgets();
57 60 void comboBoxFocused(QComboBox *combox);
61 inline void checkAnimationOptions();
62 inline void checkLegend();
63 inline void checkOpenGL();
64 inline void checkTheme();
65 inline void checkState();
66 void showMenu(QChart * chart);
67 QAction* createMenuAction(QMenu *menu, const QIcon &icon, const QString &text, const QVariant &data);
58 68
59 69 protected:
60 70 void mousePressEvent(QMouseEvent *event);
61 71 void mouseMoveEvent(QMouseEvent *event);
62 72 void mouseReleaseEvent(QMouseEvent *event);
63 73
64
65 74 private:
66 75 int m_listCount;
67 76 int m_valueMax;
@@ -69,7 +78,7 private:
69 78 QGraphicsScene* m_scene;
70 79 View* m_view;
71 80 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
72 QList<QChart*> m_chartList;
81 QHash<QChart*, int> m_chartHash;
73 82 DataTable m_dataTable;
74 83
75 84 QGraphicsWidget *m_form;
@@ -82,11 +91,10 private:
82 91 QCheckBox *m_scrollCheckBox;
83 92 QPoint m_origin;
84 93 QGraphicsRectItem* m_rubberBand;
85
86 bool m_isScrolling;
87 bool m_isZooming;
88 bool m_scroll;
89 bool m_zoom;
94 QGraphicsGridLayout* m_baseLayout;
95 QMenu* m_menu;
96 State m_state;
97 State m_currentState;
90 98
91 99 friend class ComboBox;
92 100 };
General Comments 0
You need to be logged in to leave comments. Login now