##// END OF EJS Templates
tablemodelchart: axes re-enabled on toggle (pie disables them)
Marek Rosa -
r878:2ebc46308a23
parent child
Show More
@@ -1,311 +1,333
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 "tablewidget.h"
22 22 #include <QGridLayout>
23 23 #include <QTableView>
24 24 #include <QStyledItemDelegate>
25 25 #include <QLineSeries>
26 26 #include <QSplineSeries>
27 27 #include <QScatterSeries>
28 28 #include "customtablemodel.h"
29 29 #include <QPieSeries>
30 30 #include <QPieSlice>
31 31 #include <QAreaSeries>
32 32 #include <QBarSeries>
33 33 #include <QBarSet>
34 34 #include <QPushButton>
35 35 #include <QRadioButton>
36 #include <QLabel>
36 37 #include <QSpinBox>
37 38 #include <QTime>
38 39
39 40 TableWidget::TableWidget(QWidget *parent)
40 41 : QWidget(parent)
41 42 {
42 43 setGeometry(100, 100, 1000, 600);
43 44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 45 // create simple model for storing data
45 46 // user's table data model
46 47 m_model = new CustomTableModel;
47 48 m_tableView = new QTableView;
48 49 m_tableView->setModel(m_model);
49 50 m_tableView->setMinimumHeight(300);
50 51 // tableView->setMinimumSize(340, 480);
51 52 // tableView->setItemDelegate(new QStyledItemDelegate);
52 53 m_chart = new QChart;
53 54 m_chartView = new QChartView(m_chart);
54 55 m_chartView->setRenderHint(QPainter::Antialiasing);
55 56 m_chartView->setMinimumSize(640, 480);
56 57
57 58 // add, remove data buttons
58 59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
59 60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
60 61
61 62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
62 63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
63 64
64 65 QPushButton* removeRowButton = new QPushButton("Remove row");
65 66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
66 67
68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
69
67 70 // spin box for setting number of affected items (add, remove)
68 71 m_linesCountSpinBox = new QSpinBox;
69 72 m_linesCountSpinBox->setRange(1, 10);
70 73 m_linesCountSpinBox->setValue(1);
71 74
72 75 // buttons layout
73 76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
77 buttonsLayout->addWidget(spinBoxLabel);
74 78 buttonsLayout->addWidget(m_linesCountSpinBox);
75 79 buttonsLayout->addWidget(addRowAboveButton);
76 80 buttonsLayout->addWidget(addRowBelowButton);
77 81 buttonsLayout->addWidget(removeRowButton);
78 82 buttonsLayout->addStretch();
79 83
80 84 // chart type radio buttons
81 85 m_lineRadioButton = new QRadioButton("Line");
82 86 m_splineRadioButton = new QRadioButton("Spline");
83 87 m_scatterRadioButton = new QRadioButton("Scatter");
84 88 m_pieRadioButton = new QRadioButton("Pie");
85 89 m_areaRadioButton = new QRadioButton("Area");
86 90 m_barRadioButton = new QRadioButton("Bar");
87 91
88 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
89 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
90 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
91 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
92 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
93 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 98 m_lineRadioButton->setChecked(true);
95 99
96 100 // radio buttons layout
97 101 QVBoxLayout* radioLayout = new QVBoxLayout;
98 102 radioLayout->addWidget(m_lineRadioButton);
99 103 radioLayout->addWidget(m_splineRadioButton);
100 104 radioLayout->addWidget(m_scatterRadioButton);
101 105 radioLayout->addWidget(m_pieRadioButton);
102 106 radioLayout->addWidget(m_areaRadioButton);
103 107 radioLayout->addWidget(m_barRadioButton);
104 108 radioLayout->addStretch();
105 109
106 110 // create main layout
107 111 QGridLayout* mainLayout = new QGridLayout;
108 112 mainLayout->addLayout(buttonsLayout, 1, 1);
109 113 mainLayout->addLayout(radioLayout, 2, 1);
110 114 mainLayout->addWidget(m_tableView, 1, 0);
111 115 mainLayout->addWidget(m_chartView, 2, 0);
112 116 setLayout(mainLayout);
113 117 m_lineRadioButton->setFocus();
114 118 }
115 119
116 120 void TableWidget::addRowAbove()
117 121 {
118 122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
119 123
120 124 }
121 125
122 126 void TableWidget::addRowBelow()
123 127 {
124 128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
125 129
126 130 }
127 131
128 132 void TableWidget::removeRow()
129 133 {
130 134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
131 135 }
132 136
133 void TableWidget::updateChartType()
137 void TableWidget::updateChartType(bool toggle)
134 138 {
139 // this if is needed, so that the function is only called once.
140 // For the radioButton that was enabled.
141 if (toggle) {
135 142 m_chart->removeAllSeries();
143
144 // renable axes of the chart (pie hides them)
145 // x axis
146 QChartAxis *axis = m_chart->axisX();
147 axis->setAxisVisible(true);
148 axis->setGridLineVisible(true);
149 axis->setLabelsVisible(true);
150
151 // y axis
152 axis = m_chart->axisY();
153 axis->setAxisVisible(true);
154 axis->setGridLineVisible(true);
155 axis->setLabelsVisible(true);
156
136 157 m_model->clearMapping();
137 158
138 159 QString seriesColorHex = "#000000";
139 160 QPen pen;
140 161 pen.setWidth(2);
141 162
142 163 if (m_lineRadioButton->isChecked())
143 164 {
144 165 // series 1
145 166 m_series = new QLineSeries;
146 167 m_series->setModel(m_model);
147 168 m_series->setModelMapping(0,1, Qt::Vertical);
148 169 m_series->setModelMappingRange(1, 4);
149 170 m_chart->addSeries(m_series);
150 171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
151 172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
152 173
153 174 // series 2
154 175 m_series = new QLineSeries;
155 176 m_series->setModel(m_model);
156 177 m_series->setModelMapping(2,3, Qt::Vertical);
157 178 m_chart->addSeries(m_series);
158 179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
159 180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
160 181
161 182 // series 3
162 183 m_series = new QLineSeries;
163 184 m_series->setModel(m_model);
164 185 m_series->setModelMapping(4,5, Qt::Vertical);
165 186 m_series->setModelMappingRange(2, 0);
166 187 m_chart->addSeries(m_series);
167 188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
168 189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
169 190 }
170 191 else if (m_splineRadioButton->isChecked())
171 192 {
172 193 // series 1
173 194 m_series = new QSplineSeries;
174 195 m_series->setModel(m_model);
175 196 m_series->setModelMapping(0,1, Qt::Vertical);
176 197 m_series->setModelMappingRange(1, 4);
177 198 // series->setModelMapping(0,1, Qt::Horizontal);
178 199 m_chart->addSeries(m_series);
179 200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
180 201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
181 202
182 203 // series 2
183 204 m_series = new QSplineSeries;
184 205 m_series->setModel(m_model);
185 206 m_series->setModelMapping(2,3, Qt::Vertical);
186 207 m_series->setModelMappingRange(0, 0);
187 208 // series->setModelMapping(2,3, Qt::Horizontal);
188 209 m_chart->addSeries(m_series);
189 210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
190 211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
191 212
192 213 // series 3
193 214 m_series = new QSplineSeries;
194 215 m_series->setModel(m_model);
195 216 m_series->setModelMapping(4,5, Qt::Vertical);
196 217 m_series->setModelMappingRange(2, 0);
197 218 // series->setModelMapping(4,5, Qt::Horizontal);
198 219 m_chart->addSeries(m_series);
199 220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
200 221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
201 222 }
202 223 else if (m_scatterRadioButton->isChecked())
203 224 {
204 225 // series 1
205 226 m_series = new QScatterSeries;
206 227 m_series->setModel(m_model);
207 228 m_series->setModelMapping(0,1, Qt::Vertical);
208 229 m_series->setModelMappingRange(2, 0);
209 230 // series->setModelMapping(0,1, Qt::Horizontal);
210 231 m_chart->addSeries(m_series);
211 232
212 233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
213 234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
214 235
215 236 // series 2
216 237 m_series = new QScatterSeries;
217 238 m_series->setModel(m_model);
218 239 m_series->setModelMapping(2,3, Qt::Vertical);
219 240 m_series->setModelMappingRange(1, 6);
220 241 // series->setModelMapping(2,3, Qt::Horizontal);
221 242 m_chart->addSeries(m_series);
222 243
223 244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
224 245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
225 246
226 247 // series 3
227 248 m_series = new QScatterSeries;
228 249 m_series->setModel(m_model);
229 250 m_series->setModelMapping(4,5, Qt::Vertical);
230 251 // series->setModelMapping(4,5, Qt::Horizontal);
231 252 m_chart->addSeries(m_series);
232 253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
233 254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
234 255 }
235 256 else if (m_pieRadioButton->isChecked())
236 257 {
237 258 // pie 1
238 259 QPieSeries* pieSeries = new QPieSeries;
239 260 pieSeries->setModel(m_model);
240 261 pieSeries->setModelMapping(0,0, Qt::Vertical);
241 262 pieSeries->setLabelsVisible(true);
242 263 pieSeries->setPieSize(0.4);
243 264 pieSeries->setPiePosition(0.2, 0.35);
244 265
245 266 m_chart->addSeries(pieSeries);
246 267 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
247 268 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
248 269
249 270 // pie 2
250 271 pieSeries = new QPieSeries;
251 272 pieSeries->setModel(m_model);
252 273 pieSeries->setModelMapping(1,1, Qt::Vertical);
253 274 pieSeries->setLabelsVisible(true);
254 275 pieSeries->setPieSize(0.4);
255 276 pieSeries->setPiePosition(0.8, 0.35);
256 277 m_chart->addSeries(pieSeries);
257 278 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
258 279 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
259 280
260 281 // pie 3
261 282 pieSeries = new QPieSeries;
262 283 pieSeries->setModel(m_model);
263 284 pieSeries->setModelMapping(2,2, Qt::Vertical);
264 285 pieSeries->setLabelsVisible(true);
265 286 pieSeries->setPieSize(0.4);
266 287 pieSeries->setPiePosition(0.5, 0.65);
267 288 m_chart->addSeries(pieSeries);
268 289 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
269 290 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
270 291 }
271 292 else if (m_areaRadioButton->isChecked())
272 293 {
273 294 QLineSeries* upperLineSeries = new QLineSeries;
274 295 upperLineSeries->setModel(m_model);
275 296 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
276 297 upperLineSeries->setModelMappingRange(1, 5);
277 298 QLineSeries* lowerLineSeries = new QLineSeries;
278 299 lowerLineSeries->setModel(m_model);
279 300 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
280 301 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
281 302 m_chart->addSeries(areaSeries);
282 303 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
283 304 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
284 305 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
285 306 }
286 307 else if (m_barRadioButton->isChecked())
287 308 {
288 309 QBarSeries* barSeries = new QBarSeries(QStringList());
289 310 barSeries->setModel(m_model);
290 311 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
291 312 barSeries->setToolTipEnabled(true);
292 313 m_chart->addSeries(barSeries);
293 314 for (int i = 0; i < barSeries->barsetCount(); i++) {
294 315 seriesColorHex = "#" + QString::number(barSeries->barsetAt(i)->brush().color().rgb(), 16).right(6).toUpper();
295 316 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
296 317 }
297 318 }
298 319
299 320
300 321 m_chart->axisX()->setRange(0, 500);
301 322 m_chart->axisY()->setRange(0, 120);
302 323
303 324 // repaint table view colors
304 325 m_tableView->repaint();
305 326 m_tableView->setFocus();
306 327 }
328 }
307 329
308 330 TableWidget::~TableWidget()
309 331 {
310 332
311 333 }
@@ -1,66 +1,66
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 TABLEWIDGET_H
22 22 #define TABLEWIDGET_H
23 23
24 24 #include <QtGui/QWidget>
25 25 #include "qchartview.h"
26 26 #include "qxyseries.h"
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 class CustomTableModel;
31 31 class QTableView;
32 32 class QRadioButton;
33 33 class QSpinBox;
34 34 //class QSeries;
35 35
36 36 class TableWidget : public QWidget
37 37 {
38 38 Q_OBJECT
39 39
40 40 public:
41 41 TableWidget(QWidget *parent = 0);
42 42 ~TableWidget();
43 43
44 44
45 45 public slots:
46 46 void addRowAbove();
47 47 void addRowBelow();
48 48 void removeRow();
49 void updateChartType();
49 void updateChartType(bool toggle);
50 50
51 51 private:
52 52 QChartView* m_chartView;
53 53 QChart* m_chart;
54 54 QXYSeries* m_series;
55 55 CustomTableModel* m_model;
56 56 QTableView* m_tableView;
57 57 QRadioButton* m_lineRadioButton;
58 58 QRadioButton* m_splineRadioButton;
59 59 QRadioButton* m_scatterRadioButton;
60 60 QRadioButton* m_pieRadioButton;
61 61 QRadioButton* m_areaRadioButton;
62 62 QRadioButton* m_barRadioButton;
63 63 QSpinBox* m_linesCountSpinBox;
64 64 };
65 65
66 66 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now