##// END OF EJS Templates
Removed series specific impl from chartwidgettest
Tero Ahola -
r446:0bdddf1eda83
parent child
Show More
@@ -1,392 +1,342
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include "qpieseries.h"
4 4 #include "qscatterseries.h"
5 5 #include <qlineseries.h>
6 6 #include <qbarset.h>
7 7 #include <qbarseries.h>
8 8 #include <qstackedbarseries.h>
9 9 #include <qpercentbarseries.h>
10 10 #include <QPushButton>
11 11 #include <QComboBox>
12 12 #include <QSpinBox>
13 13 #include <QCheckBox>
14 14 #include <QGridLayout>
15 15 #include <QHBoxLayout>
16 16 #include <QLabel>
17 17 #include <QSpacerItem>
18 18 #include <QMessageBox>
19 19 #include <cmath>
20 20 #include <QDebug>
21 21 #include <QStandardItemModel>
22 22
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 MainWidget::MainWidget(QWidget *parent) :
27 27 QWidget(parent),
28 28 m_addSerieDialog(0),
29 29 m_chartView(0)
30 30 {
31 31 m_chartView = new QChartView(this);
32 32 m_chartView->setRubberBandPolicy(QChartView::HorizonalRubberBand);
33 33
34 34 // Grid layout for the controls for configuring the chart widget
35 35 QGridLayout *grid = new QGridLayout();
36 36 QPushButton *addSeriesButton = new QPushButton("Add series");
37 37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
38 38 grid->addWidget(addSeriesButton, 0, 1);
39 39 initBackroundCombo(grid);
40 40 initScaleControls(grid);
41 41 initThemeCombo(grid);
42 42 initCheckboxes(grid);
43 43
44 44 // add row with empty label to make all the other rows static
45 45 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
46 46 grid->setRowStretch(grid->rowCount() - 1, 1);
47 47
48 48 // Another grid layout as a main layout
49 49 QGridLayout *mainLayout = new QGridLayout();
50 50 mainLayout->addLayout(grid, 0, 0);
51 51
52 // Init series type specific controls
53 initPieControls();
54 mainLayout->addLayout(m_pieLayout, 2, 0);
55 // Scatter series specific settings
56 // m_scatterLayout = new QGridLayout();
57 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
58 // m_scatterLayout->setEnabled(false);
59 // mainLayout->addLayout(m_scatterLayout, 1, 0);
60
61 52 // Add layouts and the chart widget to the main layout
62 53 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
63 54 setLayout(mainLayout);
64 55 }
65 56
66 57 // Combo box for selecting the chart's background
67 58 void MainWidget::initBackroundCombo(QGridLayout *grid)
68 59 {
69 60 QComboBox *backgroundCombo = new QComboBox(this);
70 61 backgroundCombo->addItem("Color");
71 62 backgroundCombo->addItem("Gradient");
72 63 backgroundCombo->addItem("Image");
73 64 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
74 65 this, SLOT(backgroundChanged(int)));
75 66
76 67 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
77 68 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
78 69 }
79 70
80 71 // Scale related controls (auto-scale vs. manual min-max values)
81 72 void MainWidget::initScaleControls(QGridLayout *grid)
82 73 {
83 74 m_autoScaleCheck = new QCheckBox("Automatic scaling");
84 75 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
85 76 // Allow setting also non-sense values (like -2147483648 and 2147483647)
86 77 m_xMinSpin = new QSpinBox();
87 78 m_xMinSpin->setMinimum(INT_MIN);
88 79 m_xMinSpin->setMaximum(INT_MAX);
89 80 m_xMinSpin->setValue(0);
90 81 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
91 82 m_xMaxSpin = new QSpinBox();
92 83 m_xMaxSpin->setMinimum(INT_MIN);
93 84 m_xMaxSpin->setMaximum(INT_MAX);
94 85 m_xMaxSpin->setValue(10);
95 86 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
96 87 m_yMinSpin = new QSpinBox();
97 88 m_yMinSpin->setMinimum(INT_MIN);
98 89 m_yMinSpin->setMaximum(INT_MAX);
99 90 m_yMinSpin->setValue(0);
100 91 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
101 92 m_yMaxSpin = new QSpinBox();
102 93 m_yMaxSpin->setMinimum(INT_MIN);
103 94 m_yMaxSpin->setMaximum(INT_MAX);
104 95 m_yMaxSpin->setValue(10);
105 96 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
106 97
107 98 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
108 99 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
109 100 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
110 101 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
111 102 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
112 103 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
113 104 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
114 105 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
115 106 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
116 107
117 108 m_autoScaleCheck->setChecked(true);
118 109 }
119 110
120 111 // Combo box for selecting theme
121 112 void MainWidget::initThemeCombo(QGridLayout *grid)
122 113 {
123 114 QComboBox *chartTheme = new QComboBox();
124 115 chartTheme->addItem("Default");
125 116 chartTheme->addItem("Vanilla");
126 117 chartTheme->addItem("Icy");
127 118 chartTheme->addItem("Grayscale");
128 119 chartTheme->addItem("Scientific");
129 120 chartTheme->addItem("Unnamed1");
130 121 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
131 122 this, SLOT(changeChartTheme(int)));
132 123 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
133 124 grid->addWidget(chartTheme, 8, 1);
134 125 }
135 126
136 127 // Different check boxes for customizing chart
137 128 void MainWidget::initCheckboxes(QGridLayout *grid)
138 129 {
139 130 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
140 131 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
141 132 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
142 133 zoomCheckBox->setChecked(true);
143 134 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
144 135
145 136 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
146 137 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
147 138 aliasCheckBox->setChecked(false);
148 139 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
149 140 }
150 141
151 142 void MainWidget::antiAliasToggled(bool enabled)
152 143 {
153 144 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
154 145 }
155 146
156 void MainWidget::initPieControls()
157 {
158 // Pie series specific settings
159 // Pie size factory
160 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
161 pieSizeSpin->setMinimum(LONG_MIN);
162 pieSizeSpin->setMaximum(LONG_MAX);
163 pieSizeSpin->setValue(1.0);
164 pieSizeSpin->setSingleStep(0.1);
165 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
166 // Pie position
167 QComboBox *piePosCombo = new QComboBox(this);
168 piePosCombo->addItem("Maximized");
169 piePosCombo->addItem("Top left");
170 piePosCombo->addItem("Top right");
171 piePosCombo->addItem("Bottom left");
172 piePosCombo->addItem("Bottom right");
173 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
174 this, SLOT(setPiePosition(int)));
175 m_pieLayout = new QGridLayout();
176 m_pieLayout->setEnabled(false);
177 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
178 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
179 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
180 m_pieLayout->addWidget(piePosCombo, 1, 1);
181 }
182
183 147 void MainWidget::addSeries()
184 148 {
185 149 if (!m_addSerieDialog) {
186 150 m_addSerieDialog = new DataSerieDialog(this);
187 151 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
188 152 this, SLOT(addSeries(QString, int, int, QString, bool)));
189 153 }
190 154 m_addSerieDialog->exec();
191 155 }
192 156
193 157 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
194 158 {
195 159 // TODO: dataCharacteristics
196 160 QList<RealList> testData;
197 161 for (int j(0); j < columnCount; j++) {
198 162 QList <qreal> newColumn;
199 163 for (int i(0); i < rowCount; i++) {
200 164 if (dataCharacteristics == "Sin") {
201 165 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
202 166 } else if (dataCharacteristics == "Sin + random") {
203 167 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
204 168 } else if (dataCharacteristics == "Random") {
205 169 newColumn.append(rand() % 5);
206 170 } else if (dataCharacteristics == "Linear") {
207 171 //newColumn.append(i * (j + 1.0));
208 172 // TODO: temporary hack to make pie work; prevent zero values:
209 173 newColumn.append(i * (j + 1.0) + 0.1);
210 174 } else { // "constant"
211 175 newColumn.append((j + 1.0));
212 176 }
213 177 }
214 178 testData.append(newColumn);
215 179 }
216 180 return testData;
217 181 }
218 182
219 183 QStringList MainWidget::generateLabels(int count)
220 184 {
221 185 QStringList result;
222 186 for (int i(0); i < count; i++)
223 187 result.append("label" + QString::number(i));
224 188 return result;
225 189 }
226 190
227 191 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
228 192 {
229 193 qDebug() << "addSeries: " << seriesName
230 194 << " columnCount: " << columnCount
231 195 << " rowCount: " << rowCount
232 196 << " dataCharacteristics: " << dataCharacteristics
233 197 << " labels enabled: " << labelsEnabled;
234 198 m_defaultSeriesName = seriesName;
235 199
236 200 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
237 201
238 202 // Line series and scatter series use similar data
239 203 if (seriesName.contains("line", Qt::CaseInsensitive)) {
240 204 for (int j(0); j < data.count(); j ++) {
241 205 QList<qreal> column = data.at(j);
242 206 QLineSeries *series = new QLineSeries();
243 207 for (int i(0); i < column.count(); i++) {
244 208 series->add(i, column.at(i));
245 209 }
246 210 m_chartView->addSeries(series);
247 211 setCurrentSeries(series);
248 212 }
249 213 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
250 214 for (int j(0); j < data.count(); j++) {
251 215 QList<qreal> column = data.at(j);
252 216 QScatterSeries *series = new QScatterSeries();
253 217 for (int i(0); i < column.count(); i++) {
254 218 (*series) << QPointF(i, column.at(i));
255 219 }
256 220 m_chartView->addSeries(series);
257 221 setCurrentSeries(series);
258 222 }
259 223 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
260 224 QStringList labels = generateLabels(rowCount);
261 225 for (int j(0); j < data.count(); j++) {
262 226 QPieSeries *series = new QPieSeries();
263 227 QList<qreal> column = data.at(j);
264 228 for (int i(0); i < column.count(); i++) {
265 229 series->add(column.at(i), labels.at(i));
266 230 }
267 231 m_chartView->addSeries(series);
268 232 setCurrentSeries(series);
269 233 }
270 234 } else if (seriesName == "Bar"
271 235 || seriesName == "Stacked bar"
272 236 || seriesName == "Percent bar") {
273 237 QStringList category;
274 238 QStringList labels = generateLabels(rowCount);
275 239 foreach(QString label, labels)
276 240 category << label;
277 241 QBarSeries* series = 0;
278 242 if (seriesName == "Bar")
279 243 series = new QBarSeries(category, this);
280 244 else if (seriesName == "Stacked bar")
281 245 series = new QStackedBarSeries(category, this);
282 246 else
283 247 series = new QPercentBarSeries(category, this);
284 248
285 249 for (int j(0); j < data.count(); j++) {
286 250 QList<qreal> column = data.at(j);
287 251 QBarSet *set = new QBarSet("set" + QString::number(j));
288 252 for (int i(0); i < column.count(); i++) {
289 253 *set << column.at(i);
290 254 }
291 255 series->addBarSet(set);
292 256 }
293 257 // TODO: new implementation of setFloatingValuesEnabled with signals
294 258 //series->setFloatingValuesEnabled(true);
295 259 series->setToolTipEnabled(true);
296 260 series->setSeparatorsEnabled(false);
297 261 m_chartView->addSeries(series);
298 262 setCurrentSeries(series);
299 263 }
300 264
301 265 // TODO: spline and area
302 266 }
303 267
304 268 void MainWidget::setCurrentSeries(QSeries *series)
305 269 {
306 270 if (series) {
307 271 m_currentSeries = series;
308 272 switch (m_currentSeries->type()) {
309 273 case QSeries::SeriesTypeLine:
310 274 break;
311 275 case QSeries::SeriesTypeScatter:
312 276 break;
313 277 case QSeries::SeriesTypePie:
314 278 break;
315 279 case QSeries::SeriesTypeBar:
316 280 qDebug() << "setCurrentSeries (bar)";
317 281 break;
318 282 case QSeries::SeriesTypeStackedBar:
319 283 qDebug() << "setCurrentSeries (Stackedbar)";
320 284 break;
321 285 case QSeries::SeriesTypePercentBar:
322 286 qDebug() << "setCurrentSeries (Percentbar)";
323 287 break;
324 288 default:
325 289 Q_ASSERT(false);
326 290 break;
327 291 }
328 292 }
329 293 }
330 294
331 295 void MainWidget::backgroundChanged(int itemIndex)
332 296 {
333 297 qDebug() << "backgroundChanged: " << itemIndex;
334 298 }
335 299
336 300 void MainWidget::autoScaleChanged(int value)
337 301 {
338 302 if (value) {
339 303 // TODO: enable auto scaling
340 304 } else {
341 305 // TODO: set scaling manually (and disable auto scaling)
342 306 }
343 307
344 308 m_xMinSpin->setEnabled(!value);
345 309 m_xMaxSpin->setEnabled(!value);
346 310 m_yMinSpin->setEnabled(!value);
347 311 m_yMaxSpin->setEnabled(!value);
348 312 }
349 313
350 314 void MainWidget::xMinChanged(int value)
351 315 {
352 316 qDebug() << "xMinChanged: " << value;
353 317 }
354 318
355 319 void MainWidget::xMaxChanged(int value)
356 320 {
357 321 qDebug() << "xMaxChanged: " << value;
358 322 }
359 323
360 324 void MainWidget::yMinChanged(int value)
361 325 {
362 326 qDebug() << "yMinChanged: " << value;
363 327 }
364 328
365 329 void MainWidget::yMaxChanged(int value)
366 330 {
367 331 qDebug() << "yMaxChanged: " << value;
368 332 }
369 333
370 334 void MainWidget::changeChartTheme(int themeIndex)
371 335 {
372 336 qDebug() << "changeChartTheme: " << themeIndex;
373 337 m_chartView->setChartTheme((QChart::ChartTheme) themeIndex);
374 338 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
375 339 QSize s = size();
376 340 s.setWidth(s.width()+1);
377 341 resize(s);
378 342 }
379
380 void MainWidget::setPieSizeFactor(double size)
381 {
382 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
383 if (pie)
384 pie->setSizeFactor(qreal(size));
385 }
386
387 void MainWidget::setPiePosition(int position)
388 {
389 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
390 if (pie)
391 pie->setPosition((QPieSeries::PiePosition) position);
392 }
@@ -1,63 +1,59
1 1 #ifndef MAINWIDGET_H
2 2 #define MAINWIDGET_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartview.h>
6 6 #include <QWidget>
7 7
8 8 class QSpinBox;
9 9 class QCheckBox;
10 10 class QGridLayout;
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 14 #define RealList QList<qreal>
15 15 class DataSerieDialog;
16 16
17 17 class MainWidget : public QWidget
18 18 {
19 19 Q_OBJECT
20 20 public:
21 21 explicit MainWidget(QWidget *parent = 0);
22 22
23 23 signals:
24 24
25 25 private:
26 26 void initBackroundCombo(QGridLayout *grid);
27 27 void initScaleControls(QGridLayout *grid);
28 28 void initThemeCombo(QGridLayout *grid);
29 29 void initCheckboxes(QGridLayout *grid);
30 void initPieControls();
31 30
32 31 private slots:
33 32 void addSeries();
34 33 void addSeries(QString series, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled);
35 34 void backgroundChanged(int itemIndex);
36 35 void autoScaleChanged(int value);
37 36 void xMinChanged(int value);
38 37 void xMaxChanged(int value);
39 38 void yMinChanged(int value);
40 39 void yMaxChanged(int value);
41 40 void antiAliasToggled(bool enabled);
42 41 void setCurrentSeries(QSeries *series);
43 42 void changeChartTheme(int themeIndex);
44 void setPieSizeFactor(double margin);
45 void setPiePosition(int position);
46 43 QList<RealList> generateTestData(int columnCount, int rowCount, QString dataCharacteristics);
47 44 QStringList generateLabels(int count);
48 45
49 46 private:
50 47 DataSerieDialog *m_addSerieDialog;
51 48 QChartView *m_chartView;
52 49 QCheckBox *m_autoScaleCheck;
53 50 QSpinBox *m_xMinSpin;
54 51 QSpinBox *m_xMaxSpin;
55 52 QSpinBox *m_yMinSpin;
56 53 QSpinBox *m_yMaxSpin;
57 54 QString m_defaultSeriesName;
58 55 QSeries *m_currentSeries;
59 56 QGridLayout *m_scatterLayout;
60 QGridLayout *m_pieLayout;
61 57 };
62 58
63 59 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now