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