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