##// END OF EJS Templates
Refactor boxplottest...
Miikka Heikkinen -
r2562:a6cb445167fe
parent child
Show More
@@ -47,16 +47,15
47
47
48 QTCOMMERCIALCHART_USE_NAMESPACE
48 QTCOMMERCIALCHART_USE_NAMESPACE
49
49
50 QString addCategories[] = {"Jul", "Aug", "Sep", "Nov", "Dec"};
50 static const QString allCategories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
51 static const int maxAddCategories = 5;
51 static const int maxCategories = 12;
52
52
53 MainWidget::MainWidget(QWidget *parent) :
53 MainWidget::MainWidget(QWidget *parent) :
54 QWidget(parent),
54 QWidget(parent),
55 m_chart(0),
55 m_chart(0),
56 m_axis(0),
56 m_axis(0),
57 rowPos(0),
57 m_rowPos(0),
58 nSeries(0),
58 m_seriesCount(0)
59 nNewBoxes(0)
60 {
59 {
61 m_chart = new QChart();
60 m_chart = new QChart();
62
61
@@ -66,43 +65,43 MainWidget::MainWidget(QWidget *parent) :
66 // Create add a series button
65 // Create add a series button
67 QPushButton *addSeriesButton = new QPushButton("Add a series");
66 QPushButton *addSeriesButton = new QPushButton("Add a series");
68 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
67 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
69 grid->addWidget(addSeriesButton, rowPos++, 1);
68 grid->addWidget(addSeriesButton, m_rowPos++, 1);
70
69
71 // Create remove a series button
70 // Create remove a series button
72 QPushButton *removeSeriesButton = new QPushButton("Remove a series");
71 QPushButton *removeSeriesButton = new QPushButton("Remove a series");
73 connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
72 connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
74 grid->addWidget(removeSeriesButton, rowPos++, 1);
73 grid->addWidget(removeSeriesButton, m_rowPos++, 1);
75
74
76 // Create add a single box button
75 // Create add a single box button
77 QPushButton *addBoxButton = new QPushButton("Add a box");
76 QPushButton *addBoxButton = new QPushButton("Add a box");
78 connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox()));
77 connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox()));
79 grid->addWidget(addBoxButton, rowPos++, 1);
78 grid->addWidget(addBoxButton, m_rowPos++, 1);
80
79
81 // Create insert a box button
80 // Create insert a box button
82 QPushButton *insertBoxButton = new QPushButton("Insert a box");
81 QPushButton *insertBoxButton = new QPushButton("Insert a box");
83 connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox()));
82 connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox()));
84 grid->addWidget(insertBoxButton, rowPos++, 1);
83 grid->addWidget(insertBoxButton, m_rowPos++, 1);
85
84
86 // Create add a single box button
85 // Create add a single box button
87 QPushButton *removeBoxButton = new QPushButton("Remove a box");
86 QPushButton *removeBoxButton = new QPushButton("Remove a box");
88 connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox()));
87 connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox()));
89 grid->addWidget(removeBoxButton, rowPos++, 1);
88 grid->addWidget(removeBoxButton, m_rowPos++, 1);
90
89
91 // Create clear button
90 // Create clear button
92 QPushButton *clearButton = new QPushButton("Clear");
91 QPushButton *clearButton = new QPushButton("Clear");
93 connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
92 connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
94 grid->addWidget(clearButton, rowPos++, 1);
93 grid->addWidget(clearButton, m_rowPos++, 1);
95
94
96 // Create clear button
95 // Create clear button
97 QPushButton *clearBoxButton = new QPushButton("ClearBox");
96 QPushButton *clearBoxButton = new QPushButton("ClearBox");
98 connect(clearBoxButton, SIGNAL(clicked()), this, SLOT(clearBox()));
97 connect(clearBoxButton, SIGNAL(clicked()), this, SLOT(clearBox()));
99 grid->addWidget(clearBoxButton, rowPos++, 1);
98 grid->addWidget(clearBoxButton, m_rowPos++, 1);
100
99
101
100
102 // Create set brush button
101 // Create set brush button
103 QPushButton *setBrushButton = new QPushButton("Set brush");
102 QPushButton *setBrushButton = new QPushButton("Set brush");
104 connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush()));
103 connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush()));
105 grid->addWidget(setBrushButton, rowPos++, 1);
104 grid->addWidget(setBrushButton, m_rowPos++, 1);
106
105
107 initThemeCombo(grid);
106 initThemeCombo(grid);
108 initCheckboxes(grid);
107 initCheckboxes(grid);
@@ -111,7 +110,7 MainWidget::MainWidget(QWidget *parent) :
111 QTableView *tableView = new QTableView;
110 QTableView *tableView = new QTableView;
112 tableView->setModel(m_model);
111 tableView->setModel(m_model);
113 tableView->setMaximumWidth(200);
112 tableView->setMaximumWidth(200);
114 grid->addWidget(tableView, rowPos++, 0, 3, 2, Qt::AlignLeft);
113 grid->addWidget(tableView, m_rowPos++, 0, 3, 2, Qt::AlignLeft);
115 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
114 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
116 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
115 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
117 tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
116 tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
@@ -151,8 +150,8 void MainWidget::initThemeCombo(QGridLayout *grid)
151 chartTheme->addItem("Blue Icy");
150 chartTheme->addItem("Blue Icy");
152 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
151 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
153 this, SLOT(changeChartTheme(int)));
152 this, SLOT(changeChartTheme(int)));
154 grid->addWidget(new QLabel("Chart theme:"), rowPos, 0);
153 grid->addWidget(new QLabel("Chart theme:"), m_rowPos, 0);
155 grid->addWidget(chartTheme, rowPos++, 1);
154 grid->addWidget(chartTheme, m_rowPos++, 1);
156 }
155 }
157
156
158 // Different check boxes for customizing chart
157 // Different check boxes for customizing chart
@@ -161,30 +160,42 void MainWidget::initCheckboxes(QGridLayout *grid)
161 QCheckBox *animationCheckBox = new QCheckBox("Animation");
160 QCheckBox *animationCheckBox = new QCheckBox("Animation");
162 connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool)));
161 connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool)));
163 animationCheckBox->setChecked(false);
162 animationCheckBox->setChecked(false);
164 grid->addWidget(animationCheckBox, rowPos++, 0);
163 grid->addWidget(animationCheckBox, m_rowPos++, 0);
165
164
166 QCheckBox *legendCheckBox = new QCheckBox("Legend");
165 QCheckBox *legendCheckBox = new QCheckBox("Legend");
167 connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool)));
166 connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool)));
168 legendCheckBox->setChecked(false);
167 legendCheckBox->setChecked(false);
169 grid->addWidget(legendCheckBox, rowPos++, 0);
168 grid->addWidget(legendCheckBox, m_rowPos++, 0);
170
169
171 QCheckBox *titleCheckBox = new QCheckBox("Title");
170 QCheckBox *titleCheckBox = new QCheckBox("Title");
172 connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool)));
171 connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool)));
173 titleCheckBox->setChecked(false);
172 titleCheckBox->setChecked(false);
174 grid->addWidget(titleCheckBox, rowPos++, 0);
173 grid->addWidget(titleCheckBox, m_rowPos++, 0);
175
174
176 QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper");
175 QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper");
177 connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool)));
176 connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool)));
178 modelMapperCheckBox->setChecked(false);
177 modelMapperCheckBox->setChecked(false);
179 grid->addWidget(modelMapperCheckBox, rowPos++, 0);
178 grid->addWidget(modelMapperCheckBox, m_rowPos++, 0);
180
179
181 }
180 }
182
181
182 void MainWidget::updateAxis(int categoryCount)
183 {
184 if (!m_axis) {
185 m_chart->createDefaultAxes();
186 m_axis = new QBarCategoryAxis();
187 }
188 QStringList categories;
189 for (int i = 0; i < categoryCount; i++)
190 categories << allCategories[i];
191 m_axis->setCategories(categories);
192 }
193
183 void MainWidget::addSeries()
194 void MainWidget::addSeries()
184 {
195 {
185 qDebug() << "BoxPlotTester::MainWidget::addSeries()";
196 qDebug() << "BoxPlotTester::MainWidget::addSeries()";
186
197
187 if (nSeries > 9)
198 if (m_seriesCount > 9)
188 return;
199 return;
189
200
190 // Initial data
201 // Initial data
@@ -203,42 +214,37 void MainWidget::addSeries()
203 *set4 << 4 << 5 << 5.2 << 6 << 7;
214 *set4 << 4 << 5 << 5.2 << 6 << 7;
204 *set5 << 4 << 7 << 8.2 << 9 << 10;
215 *set5 << 4 << 7 << 8.2 << 9 << 10;
205
216
206 m_series[nSeries] = new QBoxPlotSeries();
217 m_series[m_seriesCount] = new QBoxPlotSeries();
207 m_series[nSeries]->append(set0);
218 m_series[m_seriesCount]->append(set0);
208 m_series[nSeries]->append(set1);
219 m_series[m_seriesCount]->append(set1);
209 m_series[nSeries]->append(set2);
220 m_series[m_seriesCount]->append(set2);
210 m_series[nSeries]->append(set3);
221 m_series[m_seriesCount]->append(set3);
211 m_series[nSeries]->append(set4);
222 m_series[m_seriesCount]->append(set4);
212 m_series[nSeries]->append(set5);
223 m_series[m_seriesCount]->append(set5);
213 m_series[nSeries]->setName("Box & Whiskers");
224 m_series[m_seriesCount]->setName("Box & Whiskers");
214
225
215 connect(m_series[nSeries], SIGNAL(clicked(QBoxSet*)), this, SLOT(boxClicked(QBoxSet*)));
226 connect(m_series[m_seriesCount], SIGNAL(clicked(QBoxSet*)), this, SLOT(boxClicked(QBoxSet*)));
216 connect(m_series[nSeries], SIGNAL(hovered(bool, QBoxSet*)), this, SLOT(boxHovered(bool, QBoxSet*)));
227 connect(m_series[m_seriesCount], SIGNAL(hovered(bool, QBoxSet*)), this, SLOT(boxHovered(bool, QBoxSet*)));
217 connect(set1, SIGNAL(clicked()), this, SLOT(singleBoxClicked()));
228 connect(set1, SIGNAL(clicked()), this, SLOT(singleBoxClicked()));
218 connect(set2, SIGNAL(hovered(bool)), this, SLOT(singleBoxHovered(bool)));
229 connect(set2, SIGNAL(hovered(bool)), this, SLOT(singleBoxHovered(bool)));
219
230
220 m_chart->addSeries(m_series[nSeries]);
231 m_chart->addSeries(m_series[m_seriesCount]);
221
232
222 if (!m_axis) {
233 updateAxis(m_series[0]->count());
223 QStringList categories;
234 m_chart->setAxisX(m_axis, m_series[m_seriesCount]);
224 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
225 m_axis = new QBarCategoryAxis();
226 m_axis->append(categories);
227 m_chart->createDefaultAxes();
228 }
229 m_chart->setAxisX(m_axis, m_series[nSeries]);
230
235
231 nSeries++;
236 m_seriesCount++;
232 }
237 }
233
238
234 void MainWidget::removeSeries()
239 void MainWidget::removeSeries()
235 {
240 {
236 qDebug() << "BoxPlotTester::MainWidget::removeSeries()";
241 qDebug() << "BoxPlotTester::MainWidget::removeSeries()";
237
242
238 if (nSeries > 0) {
243 if (m_seriesCount > 0) {
239 nSeries--;
244 m_seriesCount--;
240 m_chart->removeSeries(m_series[nSeries]);
245 m_chart->removeSeries(m_series[m_seriesCount]);
241 delete m_series[nSeries];
246 delete m_series[m_seriesCount];
247 m_series[m_seriesCount] = 0;
242 } else {
248 } else {
243 qDebug() << "Create a series first";
249 qDebug() << "Create a series first";
244 }
250 }
@@ -248,7 +254,7 void MainWidget::addBox()
248 {
254 {
249 qDebug() << "BoxPlotTester::MainWidget::addBox()";
255 qDebug() << "BoxPlotTester::MainWidget::addBox()";
250
256
251 if (nSeries > 0 && nNewBoxes < maxAddCategories) {
257 if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) {
252 QBoxSet *newSet = new QBoxSet();
258 QBoxSet *newSet = new QBoxSet();
253 newSet->setValue(QBoxSet::LowerExtreme, 5.0);
259 newSet->setValue(QBoxSet::LowerExtreme, 5.0);
254 newSet->setValue(QBoxSet::LowerQuartile, 6.0);
260 newSet->setValue(QBoxSet::LowerQuartile, 6.0);
@@ -256,11 +262,9 void MainWidget::addBox()
256 newSet->setValue(QBoxSet::UpperQuartile, 7.0);
262 newSet->setValue(QBoxSet::UpperQuartile, 7.0);
257 newSet->setValue(QBoxSet::UpperExtreme, 8.0);
263 newSet->setValue(QBoxSet::UpperExtreme, 8.0);
258
264
259 m_series[0]->append(newSet);
265 updateAxis(m_series[0]->count() + 1);
260
261 m_axis->append(addCategories[nNewBoxes]);
262
266
263 nNewBoxes++;
267 m_series[0]->append(newSet);
264 }
268 }
265 }
269 }
266
270
@@ -268,16 +272,13 void MainWidget::insertBox()
268 {
272 {
269 qDebug() << "BoxPlotTester::MainWidget::insertBox()";
273 qDebug() << "BoxPlotTester::MainWidget::insertBox()";
270
274
271 if (nSeries > 0) {
275 if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) {
272 QBoxSet *newSet = new QBoxSet();
276 updateAxis(m_series[0]->count() + 1);
273 *newSet << 2 << 6 << 6.8 << 7 << 10;
277 for (int i = 0; i < m_seriesCount; i++) {
274
278 QBoxSet *newSet = new QBoxSet();
275 for (int i = 0; i < nSeries; i++)
279 *newSet << 2 << 6 << 6.8 << 7 << 10;
276 m_series[i]->insert(1, newSet);
280 m_series[i]->insert(1, newSet);
277
281 }
278 m_axis->append(addCategories[nNewBoxes]);
279
280 nNewBoxes++;
281 }
282 }
282 }
283 }
283
284
@@ -285,17 +286,16 void MainWidget::removeBox()
285 {
286 {
286 qDebug() << "BoxPlotTester::MainWidget::removeBox";
287 qDebug() << "BoxPlotTester::MainWidget::removeBox";
287
288
288 if (nSeries > 0) {
289 if (m_seriesCount > 0) {
289 for (int i = 0; i < nSeries; i++) {
290 for (int i = 0; i < m_seriesCount; i++) {
290 qDebug() << "m_series[i]->count() = " << m_series[i]->count();
291 qDebug() << "m_series[i]->count() = " << m_series[i]->count();
291 if (m_series[i]->count() > 3) {
292 if (m_series[i]->count()) {
292 QList<QBoxSet *> sets = m_series[i]->boxSets();
293 QList<QBoxSet *> sets = m_series[i]->boxSets();
293 m_series[i]->remove(sets.at(m_series[i]->count() - 3));
294 m_series[i]->remove(sets.at(m_series[i]->count() - 1));
294 }
295 }
295 }
296 }
296
297
297 if (m_axis->count() > 3)
298 updateAxis(m_series[0]->count());
298 m_axis->remove(m_axis->at(1));
299 } else {
299 } else {
300 qDebug() << "Create a series first";
300 qDebug() << "Create a series first";
301 }
301 }
@@ -305,7 +305,7 void MainWidget::clear()
305 {
305 {
306 qDebug() << "BoxPlotTester::MainWidget::clear";
306 qDebug() << "BoxPlotTester::MainWidget::clear";
307
307
308 if (nSeries > 0)
308 if (m_seriesCount > 0)
309 m_series[0]->clear();
309 m_series[0]->clear();
310 else
310 else
311 qDebug() << "Create a series first";
311 qDebug() << "Create a series first";
@@ -315,7 +315,7 void MainWidget::clearBox()
315 {
315 {
316 qDebug() << "BoxPlotTester::MainWidget::clearBox";
316 qDebug() << "BoxPlotTester::MainWidget::clearBox";
317
317
318 if (nSeries > 0) {
318 if (m_seriesCount > 0) {
319 QList<QBoxSet *> sets = m_series[0]->boxSets();
319 QList<QBoxSet *> sets = m_series[0]->boxSets();
320 sets.at(1)->clear();
320 sets.at(1)->clear();
321 } else {
321 } else {
@@ -327,7 +327,7 void MainWidget::setBrush()
327 {
327 {
328 qDebug() << "BoxPlotTester::MainWidget::setBrush";
328 qDebug() << "BoxPlotTester::MainWidget::setBrush";
329
329
330 if (nSeries > 0) {
330 if (m_seriesCount > 0) {
331 QList<QBoxSet *> sets = m_series[0]->boxSets();
331 QList<QBoxSet *> sets = m_series[0]->boxSets();
332 sets.at(1)->setBrush(QBrush(QColor(Qt::yellow)));
332 sets.at(1)->setBrush(QBrush(QColor(Qt::yellow)));
333 } else {
333 } else {
@@ -364,7 +364,7 void MainWidget::titleToggled(bool enabled)
364 void MainWidget::modelMapperToggled(bool enabled)
364 void MainWidget::modelMapperToggled(bool enabled)
365 {
365 {
366 if (enabled) {
366 if (enabled) {
367 m_series[nSeries] = new QBoxPlotSeries();
367 m_series[m_seriesCount] = new QBoxPlotSeries();
368
368
369 int first = 0;
369 int first = 0;
370 int count = 5;
370 int count = 5;
@@ -373,11 +373,11 void MainWidget::modelMapperToggled(bool enabled)
373 mapper->setLastBoxSetColumn(5);
373 mapper->setLastBoxSetColumn(5);
374 mapper->setFirstRow(first);
374 mapper->setFirstRow(first);
375 mapper->setRowCount(count);
375 mapper->setRowCount(count);
376 mapper->setSeries(m_series[nSeries]);
376 mapper->setSeries(m_series[m_seriesCount]);
377 mapper->setModel(m_model);
377 mapper->setModel(m_model);
378 m_chart->addSeries(m_series[nSeries]);
378 m_chart->addSeries(m_series[m_seriesCount]);
379
379
380 nSeries++;
380 m_seriesCount++;
381 } else {
381 } else {
382 removeSeries();
382 removeSeries();
383 }
383 }
@@ -45,6 +45,7 signals:
45 private:
45 private:
46 void initThemeCombo(QGridLayout *grid);
46 void initThemeCombo(QGridLayout *grid);
47 void initCheckboxes(QGridLayout *grid);
47 void initCheckboxes(QGridLayout *grid);
48 void updateAxis(int categoryCount);
48
49
49 private slots:
50 private slots:
50 void addSeries();
51 void addSeries();
@@ -71,9 +72,8 private:
71 QGridLayout *m_scatterLayout;
72 QGridLayout *m_scatterLayout;
72 QBarCategoryAxis *m_axis;
73 QBarCategoryAxis *m_axis;
73 CustomTableModel *m_model;
74 CustomTableModel *m_model;
74 int rowPos;
75 int m_rowPos;
75 int nSeries;
76 int m_seriesCount;
76 int nNewBoxes;
77 QBoxPlotSeries *m_series[10];
77 QBoxPlotSeries *m_series[10];
78 };
78 };
79
79
General Comments 0
You need to be logged in to leave comments. Login now