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