##// END OF EJS Templates
Refactor charviewer...
Michal Klocek -
r2127:74495bad3a7b
parent child
Show More
@@ -0,0 +1,238
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 "grid.h"
22 #include "charts.h"
23 #include <qchart.h>
24 #include <QGraphicsGridLayout>
25 #include <QGraphicsSceneMouseEvent>
26 #include <QDebug>
27
28 Grid::Grid(int size,QGraphicsItem *parent):QGraphicsWidget(parent),
29 m_listCount(3),
30 m_valueMax(10),
31 m_valueCount(7),
32 m_size(size),
33 m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)),
34 m_rubberBand(new QGraphicsRectItem()),
35 m_gridLayout(new QGraphicsGridLayout())
36 {
37 setLayout(m_gridLayout);
38 m_rubberBand->setParentItem(this);
39 m_rubberBand->setVisible(false);
40 m_rubberBand->setZValue(2);
41 }
42
43 Grid::~Grid()
44 {
45
46 }
47
48 void Grid::createCharts()
49 {
50 clear();
51
52 Charts::ChartList list = Charts::chartList();
53
54 for (int i = 0; i < m_size * m_size; ++i) {
55 QChart *chart = 0;
56 if (i < list.size()) {
57 chart = list.at(i)->createChart(m_dataTable);
58 }
59 else {
60 chart = new QChart();
61 chart->setTitle(QObject::tr("Empty"));
62 }
63
64 m_gridLayout->addItem(chart, i / m_size, i % m_size);
65 m_chartHash[chart] = i;
66 }
67
68 }
69
70 void Grid::createCharts(const QString& category)
71 {
72 clear();
73
74 QChart *qchart(0);
75 Charts::ChartList list = Charts::chartList();
76
77 int j = 0;
78 for (int i = 0; i < list.size(); ++i) {
79 Chart *chart = list.at(i);
80 if (chart->category() == category && j < m_size * m_size) {
81 qchart = list.at(i)->createChart(m_dataTable);
82 m_gridLayout->addItem(qchart, j / m_size, j % m_size);
83 m_chartHash[qchart] = j;
84 j++;
85 }
86 }
87 for (; j < m_size * m_size; ++j) {
88 qchart = new QChart();
89 qchart->setTitle(QObject::tr("Empty"));
90 m_gridLayout->addItem(qchart, j / m_size, j % m_size);
91 m_chartHash[qchart] = j;
92 }
93
94 m_gridLayout->activate();
95 }
96
97 void Grid::clear()
98 {
99 for (int i = 0; i < m_gridLayout->count(); ++i) {
100 m_gridLayout->removeAt(i);
101 }
102
103 qDeleteAll(m_chartHash.keys());
104 m_chartHash.clear();
105 }
106
107 QList<QChart*> Grid::charts()
108 {
109 return m_chartHash.keys();
110 }
111
112 void Grid::setState(State state)
113 {
114 m_state = state;
115 }
116
117 void Grid::setRubberPen(const QPen& pen)
118 {
119 m_rubberBand->setPen(pen);
120 }
121
122 void Grid::replaceChart(QChart* oldChart, Chart* newChart)
123 {
124 int index = m_chartHash[oldChart];
125 //not in 4.7.2 m_baseLayout->removeItem(qchart);
126 for (int i = 0; i < m_gridLayout->count(); ++i) {
127 if (m_gridLayout->itemAt(i) == oldChart) {
128 m_gridLayout->removeAt(i);
129 break;
130 }
131 }
132 m_chartHash.remove(oldChart);
133 QChart *chart = newChart->createChart(m_dataTable);
134 m_gridLayout->addItem(chart, index / m_size, index % m_size);
135 m_chartHash[chart] = index;
136 delete oldChart;
137 }
138
139 void Grid::mousePressEvent(QGraphicsSceneMouseEvent *event)
140 {
141 if (event->button() == Qt::LeftButton) {
142
143 m_origin = event->pos();
144 m_currentState = NoState;
145
146 foreach (QChart *chart, charts()) {
147
148 QRectF geometryRect = chart->geometry();
149 QRectF plotArea = chart->plotArea();
150 plotArea.translate(geometryRect.topLeft());
151 if (plotArea.contains(m_origin)) {
152 m_currentState = m_state;
153 if (m_currentState == NoState) emit chartSelected(chart);
154 break;
155 }
156 }
157 if (m_currentState == ZoomState) {
158 m_rubberBand->setRect(QRectF(m_origin, QSize()));
159 m_rubberBand->setVisible(true);
160 }
161
162 event->accept();
163 }
164
165 if (event->button() == Qt::RightButton) {
166 m_origin = event->pos();
167 m_currentState = m_state;
168 }
169 }
170
171 void Grid::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
172 {
173 if (m_currentState != NoState) {
174
175 foreach (QChart *chart, charts()) {
176
177 QRectF geometryRect = chart->geometry();
178 QRectF plotArea = chart->plotArea();
179 plotArea.translate(geometryRect.topLeft());
180
181 if (plotArea.contains(m_origin)) {
182 if (m_currentState == ScrollState) {
183 QPointF delta = m_origin - event->pos();
184 chart->scroll(delta.x(), -delta.y());
185 }
186 if (m_currentState == ZoomState && plotArea.contains(event->pos()))
187 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
188 break;
189 }
190 }
191 if (m_currentState == ScrollState)
192 m_origin = event->pos();
193 event->accept();
194 }
195 }
196
197 void Grid::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
198 {
199 if (event->button() == Qt::LeftButton) {
200 if (m_currentState == ZoomState) {
201 m_rubberBand->setVisible(false);
202
203 foreach (QChart *chart, charts()) {
204
205 QRectF geometryRect = chart->geometry();
206 QRectF plotArea = chart->plotArea();
207 plotArea.translate(geometryRect.topLeft());
208
209 if (plotArea.contains(m_origin)) {
210 QRectF rect = m_rubberBand->rect();
211 rect.translate(-geometryRect.topLeft());
212 chart->zoomIn(rect);
213 break;
214 }
215 }
216 }
217
218 m_currentState = NoState;
219 event->accept();
220 }
221
222 if (event->button() == Qt::RightButton) {
223
224 if (m_currentState == ZoomState) {
225 foreach (QChart *chart, charts()) {
226
227 QRectF geometryRect = chart->geometry();
228 QRectF plotArea = chart->plotArea();
229 plotArea.translate(geometryRect.topLeft());
230
231 if (plotArea.contains(m_origin)) {
232 chart->zoomOut();
233 break;
234 }
235 }
236 }
237 }
238 }
@@ -0,0 +1,75
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 GRID_H_
22 #define GRID_H_
23
24 #include "model.h"
25 #include <QGraphicsWidget>
26 #include <QChartGlobal>
27
28 class QGraphicsGridLayout;
29 class GridControl;
30 class Chart;
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 class QChart;
34 QTCOMMERCIALCHART_END_NAMESPACE
35
36 QTCOMMERCIALCHART_USE_NAMESPACE
37
38 class Grid : public QGraphicsWidget
39 {
40 Q_OBJECT
41 public:
42 enum State { NoState = 0, ZoomState, ScrollState};
43 Grid(int size , QGraphicsItem *parent = 0 );
44 ~Grid();
45 QList<QChart*> charts();
46 void createCharts();
47 void createCharts(const QString& category);
48 void replaceChart(QChart* oldChart, Chart* newChart);
49 void setState(State state);
50 State state(){ return m_state; };
51 void setRubberPen(const QPen& pen);
52 Q_SIGNAL
53 void chartSelected(QChart* chart);
54 protected:
55 void mousePressEvent(QGraphicsSceneMouseEvent *event);
56 void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
57 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
58 private:
59 void clear();
60 private:
61 int m_listCount;
62 int m_valueMax;
63 int m_valueCount;
64 int m_size;
65 DataTable m_dataTable;
66 QHash<QChart *, int> m_chartHash;
67 GridControl* m_control;
68 State m_state;
69 State m_currentState;
70 QPointF m_origin;
71 QGraphicsRectItem *m_rubberBand;
72 QGraphicsGridLayout* m_gridLayout;
73 };
74
75 #endif /* GRID_H_ */
@@ -1,8 +1,8
1 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 2 include(charts/charts.pri)
3 3 TARGET = chartviewer
4 4 QT += opengl
5 5 INCLUDEPATH += .
6 SOURCES += main.cpp window.cpp view.cpp
7 HEADERS += window.h view.h charts.h model.h
6 SOURCES += main.cpp window.cpp view.cpp grid.cpp
7 HEADERS += window.h view.h charts.h model.h grid.h
8 8
@@ -1,615 +1,448
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 #include "grid.h"
23 24 #include "charts.h"
24 25 #include <QChartView>
25 26 #include <QAreaSeries>
26 27 #include <QLegend>
27 28 #include <QGridLayout>
28 29 #include <QFormLayout>
29 30 #include <QComboBox>
30 31 #include <QSpinBox>
31 32 #include <QCheckBox>
32 33 #include <QGroupBox>
33 34 #include <QLabel>
34 35 #include <QGraphicsScene>
35 #include <QGraphicsGridLayout>
36 36 #include <QGraphicsLinearLayout>
37 37 #include <QGraphicsProxyWidget>
38 38 #include <QGLWidget>
39 39 #include <QApplication>
40 40 #include <QDebug>
41 41 #include <QMenu>
42 42
43 43 Window::Window(const QVariantHash& parameters,QWidget *parent) :
44 44 QMainWindow(parent),
45 m_listCount(3),
46 m_valueMax(10),
47 m_valueCount(7),
48 45 m_scene(new QGraphicsScene(this)),
49 46 m_view(0),
50 m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)),
51 47 m_form(0),
52 48 m_themeComboBox(0),
53 49 m_antialiasCheckBox(0),
54 50 m_animatedComboBox(0),
55 51 m_legendComboBox(0),
56 52 m_templateComboBox(0),
57 53 m_openGLCheckBox(0),
58 54 m_zoomCheckBox(0),
59 55 m_scrollCheckBox(0),
60 m_rubberBand(new QGraphicsRectItem()),
61 m_baseLayout(new QGraphicsGridLayout()),
56 m_baseLayout(new QGraphicsLinearLayout()),
62 57 m_menu(createMenu()),
63 m_state(NoState),
64 m_currentState(NoState),
65 m_template(0)
58 m_template(0),
59 m_grid(new Grid(3))
66 60 {
67 61 createProxyWidgets();
68 62 // create layout
69 63 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
70 64 settingsLayout->setOrientation(Qt::Vertical);
71 65 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
72 66 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
73 67 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
74 68 settingsLayout->addItem(m_widgetHash["themeLabel"]);
75 69 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
76 70 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
77 71 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
78 72 settingsLayout->addItem(m_widgetHash["legendLabel"]);
79 73 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
80 74 settingsLayout->addItem(m_widgetHash["templateLabel"]);
81 75 settingsLayout->addItem(m_widgetHash["templateComboBox"]);
82 76 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
83 77 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
84 78 settingsLayout->addStretch();
85 m_baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
86 79
87 //create charts
88 Charts::ChartList list = Charts::chartList();
80 m_grid->createCharts();
89 81
90 for (int i = 0; i < 9; ++i) {
91 QChart *chart = 0;
92 if (i < list.size()) {
93 chart = list.at(i)->createChart(m_dataTable);
94 } else {
95 chart = new QChart();
96 chart->setTitle(tr("Empty"));
97 }
98
99 m_baseLayout->addItem(chart, i / 3, i % 3);
100 m_chartHash[chart] = i;
101 }
82 m_baseLayout->setOrientation(Qt::Horizontal);
83 m_baseLayout->addItem(m_grid);
84 m_baseLayout->addItem(settingsLayout);
102 85
103 86 m_form = new QGraphicsWidget();
104 87 m_form->setLayout(m_baseLayout);
105 88 m_scene->addItem(m_form);
106 m_scene->addItem(m_rubberBand);
107 m_rubberBand->setVisible(false);
108 89
109 90 m_view = new View(m_scene, m_form);
110 91 m_view->setMinimumSize(m_form->minimumSize().toSize());
111 92
112 93 // Set defaults
113 94 m_antialiasCheckBox->setChecked(true);
114 95 initializeFromParamaters(parameters);
115 96 updateUI();
116 97 setCentralWidget(m_view);
117 98
118 99 connectSignals();
119 100 }
120 101
121 102 Window::~Window()
122 103 {
123 104 }
124 105
125 106 void Window::connectSignals()
126 107 {
127 108 QObject::connect(m_form, SIGNAL(geometryChanged()), this , SLOT(handleGeometryChanged()));
128 109 QObject::connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
129 110 QObject::connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
130 111 QObject::connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
131 112 QObject::connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
132 113 QObject::connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
133 114 QObject::connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
134 115 QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
135 116 QObject::connect(m_templateComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
117 QObject::connect(m_grid, SIGNAL(chartSelected(QChart*)),this,SLOT(handleChartSelected(QChart*)));
136 118 }
137 119
138 120 void Window::createProxyWidgets()
139 121 {
140 122 m_themeComboBox = createThemeBox();
141 123 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
142 124 m_animatedComboBox = createAnimationBox();
143 125 m_legendComboBox = createLegendBox();
144 126 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
145 127 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
146 128 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
147 129 m_templateComboBox = createTempleteBox();
148 130 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
149 131 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
150 132 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
151 133 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
152 134 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
153 135 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
154 136 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
155 137 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
156 138 m_widgetHash["templateLabel"] = m_scene->addWidget(new QLabel("Chart template"));
157 139 m_widgetHash["templateComboBox"] = m_scene->addWidget(m_templateComboBox);
158 140 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
159 141 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
160 142
161 143 }
162 144
163 145 QComboBox *Window::createThemeBox()
164 146 {
165 147 QComboBox *themeComboBox = new ComboBox(this);
166 148 themeComboBox->addItem("Light", QChart::ChartThemeLight);
167 149 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
168 150 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
169 151 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
170 152 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
171 153 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
172 154 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
173 155 return themeComboBox;
174 156 }
175 157
176 158 QComboBox *Window::createAnimationBox()
177 159 {
178 160 QComboBox *animationComboBox = new ComboBox(this);
179 161 animationComboBox->addItem("No Animations", QChart::NoAnimation);
180 162 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
181 163 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
182 164 animationComboBox->addItem("All Animations", QChart::AllAnimations);
183 165 return animationComboBox;
184 166 }
185 167
186 168 QComboBox *Window::createLegendBox()
187 169 {
188 170 QComboBox *legendComboBox = new ComboBox(this);
189 171 legendComboBox->addItem("No Legend ", 0);
190 172 legendComboBox->addItem("Legend Top", Qt::AlignTop);
191 173 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
192 174 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
193 175 legendComboBox->addItem("Legend Right", Qt::AlignRight);
194 176 return legendComboBox;
195 177 }
196 178
197 179 QComboBox *Window::createTempleteBox()
198 180 {
199 181 QComboBox *templateComboBox = new ComboBox(this);
200 182 templateComboBox->addItem("No Template", 0);
201 183
202 184 Charts::ChartList list = Charts::chartList();
203 185 QMultiMap<QString, Chart *> categoryMap;
204 186
205 187 foreach (Chart *chart, list)
206 188 categoryMap.insertMulti(chart->category(), chart);
207 189
208 190 foreach (const QString &category, categoryMap.uniqueKeys())
209 191 templateComboBox->addItem(category, category);
210 192
211 193 return templateComboBox;
212 194 }
213 195
214 196 void Window::initializeFromParamaters(const QVariantHash& parameters)
215 197 {
216 198 if (parameters.contains("template")) {
217 199 QString t = parameters["template"].toString();
218 200 for (int i = 0; i < m_templateComboBox->count(); ++i) {
219 201 if (m_templateComboBox->itemText(i) == t) {
220 202 m_templateComboBox->setCurrentIndex(i);
221 203 break;
222 204 }
223 205 }
224 206 }
225 207 if (parameters.contains("opengl")) {
226 208 bool checked = parameters["opengl"].toBool();
227 209 m_openGLCheckBox->setChecked(checked);
228 210 }
229 211 if (parameters.contains("theme")) {
230 212 QString t = parameters["theme"].toString();
231 213 for (int i = 0; i < m_themeComboBox->count(); ++i) {
232 214 if (m_themeComboBox->itemText(i) == t) {
233 215 m_themeComboBox->setCurrentIndex(i);
234 216 break;
235 217 }
236 218 }
237 219 }
238 220 if (parameters.contains("animation")) {
239 221 QString t = parameters["animation"].toString();
240 222 for (int i = 0; i < m_animatedComboBox->count(); ++i) {
241 223 if (m_animatedComboBox->itemText(i) == t) {
242 224 m_animatedComboBox->setCurrentIndex(i);
243 225 break;
244 226 }
245 227 }
246 228 }
247 229 if (parameters.contains("legend")) {
248 230 QString t = parameters["legend"].toString();
249 231 for (int i = 0; i < m_legendComboBox->count(); ++i) {
250 232 if (m_legendComboBox->itemText(i) == t) {
251 233 m_legendComboBox->setCurrentIndex(i);
252 234 break;
253 235 }
254 236 }
255 237 }
256 238 }
257 239
258 240 void Window::updateUI()
259 241 {
260 242 checkTemplate();
261 243 checkOpenGL();
262 244 checkTheme();
263 245 checkAnimationOptions();
264 246 checkLegend();
265 247 checkState();
266 248 }
267 249
268 250 void Window::checkLegend()
269 251 {
270 252 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
271 253
272 254 if (!alignment) {
273 foreach (QChart *chart, m_chartHash.keys())
255 foreach (QChart *chart, m_grid->charts())
274 256 chart->legend()->hide();
275 257 } else {
276 foreach (QChart *chart, m_chartHash.keys()) {
258 foreach (QChart *chart, m_grid->charts()) {
277 259 chart->legend()->setAlignment(alignment);
278 260 chart->legend()->show();
279 261 }
280 262 }
281 263 }
282 264
283 265 void Window::checkOpenGL()
284 266 {
285 267 bool opengl = m_openGLCheckBox->isChecked();
286 268 bool isOpengl = qobject_cast<QGLWidget *>(m_view->viewport());
287 269 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
288 270 m_view->deleteLater();
289 271 m_view = new View(m_scene, m_form);
290 272 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
291 273 setCentralWidget(m_view);
292 274 }
293 275
294 276 bool antialias = m_antialiasCheckBox->isChecked();
295 277
296 278 if (opengl)
297 279 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
298 280 else
299 281 m_view->setRenderHint(QPainter::Antialiasing, antialias);
300 282 }
301 283
302 284 void Window::checkAnimationOptions()
303 285 {
304 286 QChart::AnimationOptions options(
305 287 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
306 288
307 if (!m_chartHash.isEmpty() && m_chartHash.keys().at(0)->animationOptions() != options) {
308 foreach (QChart *chart, m_chartHash.keys())
289 QList<QChart*> charts = m_grid->charts();
290
291 if (!charts.isEmpty() && charts.at(0)->animationOptions() != options) {
292 foreach (QChart *chart, charts)
309 293 chart->setAnimationOptions(options);
310 294 }
311 295 }
312 296
313 297 void Window::checkState()
314 298 {
315 299 bool scroll = m_scrollCheckBox->isChecked();
316 300
317 if (m_state != ScrollState && scroll) {
318 m_state = ScrollState;
301
302 if (m_grid->state() != Grid::ScrollState && scroll) {
303 m_grid->setState(Grid::ScrollState);
319 304 m_zoomCheckBox->setChecked(false);
320 } else if (!scroll && m_state == ScrollState) {
321 m_state = NoState;
305 } else if (!scroll && m_grid->state() == Grid::ScrollState) {
306 m_grid->setState(Grid::NoState);
322 307 }
323 308
324 309 bool zoom = m_zoomCheckBox->isChecked();
325 310
326 if (m_state != ZoomState && zoom) {
327 m_state = ZoomState;
311 if (m_grid->state() != Grid::ZoomState && zoom) {
312 m_grid->setState(Grid::ZoomState);
328 313 m_scrollCheckBox->setChecked(false);
329 } else if (!zoom && m_state == ZoomState) {
330 m_state = NoState;
314 } else if (!zoom && m_grid->state() == Grid::ZoomState) {
315 m_grid->setState(Grid::NoState);
331 316 }
332 317 }
333 318
334 319 void Window::checkTemplate()
335 320 {
336
337 321 int index = m_templateComboBox->currentIndex();
338 322 if (m_template == index || index == 0)
339 323 return;
340 324
341 325 m_template = index;
342
343 326 QString category = m_templateComboBox->itemData(index).toString();
344 Charts::ChartList list = Charts::chartList();
345
346 QList<QChart *> qchartList = m_chartHash.keys();
347
348 foreach (QChart *qchart, qchartList) {
349 for (int i = 0 ; i < m_baseLayout->count(); ++i) {
350 if (m_baseLayout->itemAt(i) == qchart) {
351 m_baseLayout->removeAt(i);
352 break;
353 }
354 }
355 }
356
357 m_chartHash.clear();
358 qDeleteAll(qchartList);
359
360 QChart *qchart(0);
361
362 int j = 0;
363 for (int i = 0; i < list.size(); ++i) {
364 Chart *chart = list.at(i);
365 if (chart->category() == category && j < 9) {
366 qchart = list.at(i)->createChart(m_dataTable);
367 m_baseLayout->addItem(qchart, j / 3, j % 3);
368 m_chartHash[qchart] = j;
369 j++;
370 }
371 }
372 for (; j < 9; ++j) {
373 qchart = new QChart();
374 qchart->setTitle(tr("Empty"));
375 m_baseLayout->addItem(qchart, j / 3, j % 3);
376 m_chartHash[qchart] = j;
377 }
378 m_baseLayout->activate();
327 m_grid->createCharts(category);
379 328 }
380 329
381 330 void Window::checkTheme()
382 331 {
383 332 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
384 333 m_themeComboBox->currentIndex()).toInt();
385 334
386 foreach (QChart *chart, m_chartHash.keys())
335 foreach (QChart *chart, m_grid->charts())
387 336 chart->setTheme(theme);
388 337
389 338 QPalette pal = window()->palette();
390 339 if (theme == QChart::ChartThemeLight) {
391 340 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
392 341 pal.setColor(QPalette::WindowText, QRgb(0x404044));
393 342 } else if (theme == QChart::ChartThemeDark) {
394 343 pal.setColor(QPalette::Window, QRgb(0x121218));
395 344 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
396 345 } else if (theme == QChart::ChartThemeBlueCerulean) {
397 346 pal.setColor(QPalette::Window, QRgb(0x40434a));
398 347 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
399 348 } else if (theme == QChart::ChartThemeBrownSand) {
400 349 pal.setColor(QPalette::Window, QRgb(0x9e8965));
401 350 pal.setColor(QPalette::WindowText, QRgb(0x404044));
402 351 } else if (theme == QChart::ChartThemeBlueNcs) {
403 352 pal.setColor(QPalette::Window, QRgb(0x018bba));
404 353 pal.setColor(QPalette::WindowText, QRgb(0x404044));
405 354 } else if (theme == QChart::ChartThemeHighContrast) {
406 355 pal.setColor(QPalette::Window, QRgb(0xffab03));
407 356 pal.setColor(QPalette::WindowText, QRgb(0x181818));
408 357 } else if (theme == QChart::ChartThemeBlueIcy) {
409 358 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
410 359 pal.setColor(QPalette::WindowText, QRgb(0x404044));
411 360 } else {
412 361 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
413 362 pal.setColor(QPalette::WindowText, QRgb(0x404044));
414 363 }
415 364 foreach (QGraphicsProxyWidget *widget, m_widgetHash)
416 365 widget->setPalette(pal);
417 366 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
418 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
419 }
420
421 void Window::mousePressEvent(QMouseEvent *event)
422 {
423 if (event->button() == Qt::LeftButton) {
424
425 m_origin = event->pos();
426 m_currentState = NoState;
427
428 foreach (QChart *chart, m_chartHash.keys()) {
429
430 QRectF geometryRect = chart->geometry();
431 QRectF plotArea = chart->plotArea();
432 plotArea.translate(geometryRect.topLeft());
433 if (plotArea.contains(m_origin)) {
434 m_currentState = m_state;
435 if (m_currentState == NoState && m_templateComboBox->currentIndex() == 0)
436 handleMenu(chart);
437 break;
438 }
439 }
440
441 if (m_currentState == ZoomState) {
442 m_rubberBand->setRect(QRectF(m_origin, QSize()));
443 m_rubberBand->setVisible(true);
444 }
445
446 event->accept();
447 }
448
449 if (event->button() == Qt::RightButton) {
450 m_origin = event->pos();
451 m_currentState = m_state;
452 }
453 }
454
455 void Window::mouseMoveEvent(QMouseEvent *event)
456 {
457 if (m_currentState != NoState) {
458
459 foreach (QChart *chart, m_chartHash.keys()) {
460
461 QRectF geometryRect = chart->geometry();
462 QRectF plotArea = chart->plotArea();
463 plotArea.translate(geometryRect.topLeft());
464
465 if (plotArea.contains(m_origin)) {
466 if (m_currentState == ScrollState) {
467 QPointF delta = m_origin - event->pos();
468 chart->scroll(delta.x(), -delta.y());
469 }
470 if (m_currentState == ZoomState && plotArea.contains(event->pos()))
471 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
472 break;
473 }
474 }
475 if (m_currentState == ScrollState)
476 m_origin = event->pos();
477 event->accept();
478 }
479 }
480
481 void Window::mouseReleaseEvent(QMouseEvent *event)
482 {
483 if (event->button() == Qt::LeftButton) {
484 if (m_currentState == ZoomState) {
485 m_rubberBand->setVisible(false);
486
487 foreach (QChart *chart, m_chartHash.keys()) {
488
489 QRectF geometryRect = chart->geometry();
490 QRectF plotArea = chart->plotArea();
491 plotArea.translate(geometryRect.topLeft());
492
493 if (plotArea.contains(m_origin)) {
494 QRectF rect = m_rubberBand->rect();
495 rect.translate(-geometryRect.topLeft());
496 chart->zoomIn(rect);
497 break;
498 }
499 }
500 }
501
502 m_currentState = NoState;
503 event->accept();
504 }
505
506 if (event->button() == Qt::RightButton) {
507
508 if (m_currentState == ZoomState) {
509 foreach (QChart *chart, m_chartHash.keys()) {
510
511 QRectF geometryRect = chart->geometry();
512 QRectF plotArea = chart->plotArea();
513 plotArea.translate(geometryRect.topLeft());
514
515 if (plotArea.contains(m_origin)) {
516 chart->zoomOut();
517 break;
518 }
519 }
520 }
521 }
367 m_grid->setRubberPen(pal.color((QPalette::WindowText)));
522 368 }
523 369
524 370 void Window::comboBoxFocused(QComboBox *combobox)
525 371 {
526 372 foreach (QGraphicsProxyWidget *widget , m_widgetHash) {
527 373 if (widget->widget() == combobox)
528 374 widget->setZValue(2.0);
529 375 else
530 376 widget->setZValue(0.0);
531 377 }
532 378 }
533 379
534 void Window::handleMenu(QChart *qchart)
380 void Window::handleChartSelected(QChart *qchart)
535 381 {
382 if(m_templateComboBox->currentIndex() != 0) return;
383
536 384 QAction *chosen = m_menu->exec(QCursor::pos());
537 385
538 386 if (chosen) {
539 387 Chart *chart = (Chart *) chosen->data().value<void *>();
540 int index = m_chartHash[qchart];
541 //not in 4.7.2 m_baseLayout->removeItem(qchart);
542 for (int i = 0 ; i < m_baseLayout->count(); ++i) {
543 if (m_baseLayout->itemAt(i) == qchart) {
544 m_baseLayout->removeAt(i);
545 break;
546 }
547 }
548
549 m_chartHash.remove(qchart);
550 QChart *newChart = chart->createChart(m_dataTable);
551 m_baseLayout->addItem(newChart, index / 3, index % 3);
552 m_chartHash[newChart] = index;
553 delete qchart;
388 m_grid->replaceChart(qchart,chart);
554 389 updateUI();
555 390 }
556
557 391 }
558 392
559 393 QMenu *Window::createMenu()
560 394 {
561 395 Charts::ChartList list = Charts::chartList();
562 396 QMultiMap<QString, Chart *> categoryMap;
563 397
564 398 QMenu *result = new QMenu(this);
565 399
566 400 foreach (Chart *chart, list)
567 401 categoryMap.insertMulti(chart->category(), chart);
568 402
569 403 foreach (const QString &category, categoryMap.uniqueKeys()) {
570 404 QMenu *menu(0);
571 405 QMultiMap<QString, Chart *> subCategoryMap;
572 406 if (category.isEmpty()) {
573 407 menu = result;
574 408 } else {
575 409 menu = new QMenu(category, this);
576 410 result->addMenu(menu);
577 411 }
578 412
579 413 foreach (Chart *chart, categoryMap.values(category))
580 414 subCategoryMap.insert(chart->subCategory(), chart);
581 415
582 416 foreach (const QString &subCategory, subCategoryMap.uniqueKeys()) {
583 417 QMenu *subMenu(0);
584 418 if (subCategory.isEmpty()) {
585 419 subMenu = menu;
586 420 } else {
587 421 subMenu = new QMenu(subCategory, this);
588 422 menu->addMenu(subMenu);
589 423 }
590 424
591 425 foreach (Chart *chart, subCategoryMap.values(subCategory)) {
592 426 createMenuAction(subMenu, QIcon(), chart->name(),
593 427 qVariantFromValue((void *) chart));
594 428 }
595 429 }
596 430 }
597
598 431 return result;
599 432 }
600 433
601 434 QAction *Window::createMenuAction(QMenu *menu, const QIcon &icon, const QString &text,
602 435 const QVariant &data)
603 436 {
604 437 QAction *action = menu->addAction(icon, text);
605 438 action->setCheckable(false);
606 439 action->setData(data);
607 440 return action;
608 441 }
609 442
610 443 void Window::handleGeometryChanged()
611 444 {
612 445 QSizeF size = m_baseLayout->sizeHint(Qt::MinimumSize);
613 446 m_view->scene()->setSceneRect(0, 0, this->width(), this->height());
614 447 m_view->setMinimumSize(size.toSize());
615 448 }
@@ -1,124 +1,115
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 #include "model.h"
24 23 #include <QMainWindow>
25 24 #include <QChartGlobal>
26 25 #include <QHash>
27 26 #include <QComboBox>
28 27
29 28 class QCheckBox;
30 29 class QGraphicsRectItem;
31 30 class QGraphicsScene;
32 31 class QGraphicsWidget;
33 32 class View;
34 class QGraphicsGridLayout;
33 class QGraphicsLinearLayout;
35 34 class Chart;
35 class Grid;
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38 class QChart;
39 39 QTCOMMERCIALCHART_END_NAMESPACE
40 40
41 41 QTCOMMERCIALCHART_USE_NAMESPACE
42 42
43 43
44 44 class Window: public QMainWindow
45 45 {
46 46 Q_OBJECT
47 enum State { NoState = 0, ZoomState, ScrollState};
48 47 public:
49 48 explicit Window(const QVariantHash& parameters, QWidget *parent = 0);
50 49 ~Window();
51 50
52 51 private Q_SLOTS:
53 52 void updateUI();
54 53 void handleGeometryChanged();
54 void handleChartSelected(QChart *chart);
55 55 private:
56 56 QComboBox *createThemeBox();
57 57 QComboBox *createAnimationBox();
58 58 QComboBox *createLegendBox();
59 59 QComboBox *createTempleteBox();
60 60 void connectSignals();
61 61 void createProxyWidgets();
62 62 void comboBoxFocused(QComboBox *combox);
63 63 inline void checkAnimationOptions();
64 64 inline void checkLegend();
65 65 inline void checkOpenGL();
66 66 inline void checkTheme();
67 67 inline void checkState();
68 68 inline void checkTemplate();
69 69 QMenu *createMenu();
70 void handleMenu(QChart *chart);
71 70 QAction *createMenuAction(QMenu *menu, const QIcon &icon, const QString &text, const QVariant &data);
72 71 void initializeFromParamaters(const QVariantHash& parameters);
73 72
74 73 protected:
75 void mousePressEvent(QMouseEvent *event);
76 void mouseMoveEvent(QMouseEvent *event);
77 void mouseReleaseEvent(QMouseEvent *event);
74 //void mousePressEvent(QMouseEvent *event);
75 //void mouseMoveEvent(QMouseEvent *event);
76 //void mouseReleaseEvent(QMouseEvent *event);
78 77
79 78 private:
80 int m_listCount;
81 int m_valueMax;
82 int m_valueCount;
83 79 QGraphicsScene *m_scene;
84 80 View *m_view;
85 81 QHash<QString, QGraphicsProxyWidget *> m_widgetHash;
86 QHash<QChart *, int> m_chartHash;
87 DataTable m_dataTable;
88 82
89 83 QGraphicsWidget *m_form;
90 84 QComboBox *m_themeComboBox;
91 85 QCheckBox *m_antialiasCheckBox;
92 86 QComboBox *m_animatedComboBox;
93 87 QComboBox *m_legendComboBox;
94 88 QComboBox *m_templateComboBox;
95 89 QCheckBox *m_openGLCheckBox;
96 90 QCheckBox *m_zoomCheckBox;
97 91 QCheckBox *m_scrollCheckBox;
98 QPoint m_origin;
99 QGraphicsRectItem *m_rubberBand;
100 QGraphicsGridLayout *m_baseLayout;
92 QGraphicsLinearLayout *m_baseLayout;
101 93 QMenu *m_menu;
102 State m_state;
103 State m_currentState;
104 94 int m_template;
95 Grid* m_grid;
105 96
106 97 friend class ComboBox;
107 98 };
108 99
109 100 class ComboBox: public QComboBox
110 101 {
111 102 public:
112 103 ComboBox(Window *window, QWidget *parent = 0): QComboBox(parent), m_window(window)
113 104 {}
114 105
115 106 protected:
116 107 void focusInEvent(QFocusEvent *e) {
117 108 QComboBox::focusInEvent(e);
118 109 m_window->comboBoxFocused(this);
119 110 }
120 111 private:
121 112 Window *m_window;
122 113 };
123 114
124 115 #endif
General Comments 0
You need to be logged in to leave comments. Login now