##// END OF EJS Templates
XYSeries model with limits working.
Marek Rosa -
r833:16b8c9450c47
parent child
Show More
@@ -17,7 +17,7 SUBDIRS += \
17 17 splinechart \
18 18 stackedbarchart \
19 19 stackedbarchartdrilldown \
20 #tablemodelchart \
20 tablemodelchart \
21 21 zoomlinechart
22 22
23 23
@@ -98,7 +98,7 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation,
98 98 }
99 99 }
100 100 else
101 return QString("%1").arg(section + 1);
101 return QString("%1").arg(section /*+ 1*/);
102 102 }
103 103
104 104 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
@@ -203,7 +203,10 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare
203 203 {
204 204 if (i - 1 >= 0)
205 205 {
206 if (row > 0)
206 207 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
208 else
209 difference = (int)((qAbs(m_data[i]->at(k)/count)));
207 210 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
208 211 }
209 212 else
@@ -42,14 +42,15 TableWidget::TableWidget(QWidget *parent)
42 42 // create simple model for storing data
43 43 // user's table data model
44 44 m_model = new CustomTableModel;
45 tableView = new QTableView;
46 tableView->setModel(m_model);
47 tableView->setMinimumHeight(240);
45 m_tableView = new QTableView;
46 m_tableView->setModel(m_model);
47 m_tableView->setMinimumHeight(240);
48 48 // tableView->setMinimumSize(340, 480);
49 49 // tableView->setItemDelegate(new QStyledItemDelegate);
50 chartView = new QChartView(this);
51 chartView->setRenderHint(QPainter::Antialiasing);
52 chartView->setMinimumSize(640, 480);
50 m_chart = new QChart;
51 m_chartView = new QChartView(m_chart);
52 m_chartView->setRenderHint(QPainter::Antialiasing);
53 m_chartView->setMinimumSize(640, 480);
53 54
54 55 // create
55 56 // QLineSeries* series = new QLineSeries;
@@ -74,59 +75,59 TableWidget::TableWidget(QWidget *parent)
74 75 QPushButton* removeRowButton = new QPushButton("Remove row");
75 76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
76 77
77 linesCountSpinBox = new QSpinBox;
78 linesCountSpinBox->setRange(1, 10);
79 linesCountSpinBox->setValue(1);
78 m_linesCountSpinBox = new QSpinBox;
79 m_linesCountSpinBox->setRange(1, 10);
80 m_linesCountSpinBox->setValue(1);
80 81
81 82 // buttons layout
82 83 QVBoxLayout* buttonsLayout = new QVBoxLayout;
83 buttonsLayout->addWidget(linesCountSpinBox);
84 buttonsLayout->addWidget(m_linesCountSpinBox);
84 85 buttonsLayout->addWidget(addRowAboveButton);
85 86 buttonsLayout->addWidget(addRowBelowButton);
86 87 buttonsLayout->addWidget(removeRowButton);
87 88 buttonsLayout->addStretch();
88 89
89 90 // chart type radio buttons
90 lineRadioButton = new QRadioButton("Line");
91 splineRadioButton = new QRadioButton("Spline");
92 scatterRadioButton = new QRadioButton("Scatter");
93 pieRadioButton = new QRadioButton("Pie");
94 areaRadioButton = new QRadioButton("Area");
95 barRadioButton = new QRadioButton("Bar");
96
97 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
98 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
99 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
100 connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
101 connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
102 connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
103 lineRadioButton->setChecked(true);
91 m_lineRadioButton = new QRadioButton("Line");
92 m_splineRadioButton = new QRadioButton("Spline");
93 m_scatterRadioButton = new QRadioButton("Scatter");
94 m_pieRadioButton = new QRadioButton("Pie");
95 m_areaRadioButton = new QRadioButton("Area");
96 m_barRadioButton = new QRadioButton("Bar");
97
98 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
99 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
100 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
101 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
102 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
103 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
104 m_lineRadioButton->setChecked(true);
104 105
105 106 // radio buttons layout
106 107 QVBoxLayout* radioLayout = new QVBoxLayout;
107 radioLayout->addWidget(lineRadioButton);
108 radioLayout->addWidget(splineRadioButton);
109 radioLayout->addWidget(scatterRadioButton);
110 radioLayout->addWidget(pieRadioButton);
111 radioLayout->addWidget(areaRadioButton);
112 radioLayout->addWidget(barRadioButton);
108 radioLayout->addWidget(m_lineRadioButton);
109 radioLayout->addWidget(m_splineRadioButton);
110 radioLayout->addWidget(m_scatterRadioButton);
111 radioLayout->addWidget(m_pieRadioButton);
112 radioLayout->addWidget(m_areaRadioButton);
113 radioLayout->addWidget(m_barRadioButton);
113 114 radioLayout->addStretch();
114 115
115 116 // create main layout
116 117 QGridLayout* mainLayout = new QGridLayout;
117 mainLayout->addLayout(buttonsLayout, 1, 0);
118 mainLayout->addLayout(radioLayout, 2, 0);
119 mainLayout->addWidget(tableView, 1, 1);
120 mainLayout->addWidget(chartView, 2, 1);
118 mainLayout->addLayout(buttonsLayout, 1, 1);
119 mainLayout->addLayout(radioLayout, 2, 1);
120 mainLayout->addWidget(m_tableView, 1, 0);
121 mainLayout->addWidget(m_chartView, 2, 0);
121 122 setLayout(mainLayout);
122 lineRadioButton->setFocus();
123 m_lineRadioButton->setFocus();
123 124 }
124 125
125 126 void TableWidget::addRowAbove()
126 127 {
127 128 // m_model->insertRow(m_model->rowCount());
128 129 // m_model->insertRow(tableView->currentIndex().row());
129 m_model->insertRows(tableView->currentIndex().row(), linesCountSpinBox->value());
130 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
130 131
131 132 }
132 133
@@ -134,7 +135,7 void TableWidget::addRowBelow()
134 135 {
135 136 // m_model->insertRow(m_model->rowCount());
136 137 // m_model->insertRow(tableView->currentIndex().row() + 1);
137 m_model->insertRows(tableView->currentIndex().row() + 1, linesCountSpinBox->value());
138 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
138 139
139 140 }
140 141
@@ -142,14 +143,14 void TableWidget::removeRow()
142 143 {
143 144 // m_model->removeRow(m_model->rowCount() - 1);
144 145 // m_model->removeRow(tableView->currentIndex().row());
145 m_model->removeRows(tableView->currentIndex().row(), qMin(m_model->rowCount() - tableView->currentIndex().row(), linesCountSpinBox->value()));
146 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
146 147 }
147 148
148 149 void TableWidget::updateChartType()
149 150 {
150 chartView->removeAllSeries();
151 m_chart->removeAllSeries();
151 152
152 if (lineRadioButton->isChecked())
153 if (m_lineRadioButton->isChecked())
153 154 {
154 155 QPen pen;
155 156 pen.setWidth(2);
@@ -157,15 +158,15 void TableWidget::updateChartType()
157 158 QColor seriesColor("#8FBC8F");
158 159
159 160 // series 1
160 series = new QLineSeries;
161 series->setModel(m_model);
162 series->setModelMapping(0,1, Qt::Vertical);
163 series->setModelMappingShift(1, 4);
161 m_series = new QLineSeries;
162 m_series->setModel(m_model);
163 m_series->setModelMapping(0,1, Qt::Vertical);
164 m_series->setModelMappingShift(3, 3);
164 165 // series->setModelMapping(0,1, Qt::Horizontal);
165 166
166 167 pen.setColor(seriesColor);
167 series->setPen(pen);
168 chartView->addSeries(series);
168 m_series->setPen(pen);
169 m_chart->addSeries(m_series);
169 170 for (int i = 1; i <=4; i++)
170 171 {
171 172 m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole);
@@ -176,15 +177,16 void TableWidget::updateChartType()
176 177 seriesColor = QColor("#1E90FF");
177 178
178 179 // series 2
179 series = new QLineSeries;
180 series->setModel(m_model);
181 series->setModelMapping(2,3, Qt::Vertical);
180 m_series = new QLineSeries;
181 m_series->setModel(m_model);
182 m_series->setModelMapping(2,3, Qt::Vertical);
182 183 // series->setModelMapping(2,3, Qt::Horizontal);
183 184 pen.setColor(seriesColor);
184 series->setPen(pen);
185 chartView->addSeries(series);
185 m_series->setPen(pen);
186 // m_chart->addSeries(m_series);
186 187
187 chartView->axisX()->setRange(0, 500);
188 m_chart->axisX()->setRange(0, 500);
189 m_chart->axisY()->setRange(0, 120);
188 190
189 191 for (int i = 0; i < m_model->rowCount(); i++)
190 192 {
@@ -198,56 +200,56 void TableWidget::updateChartType()
198 200 //// series->setModelMapping(4,5, Qt::Horizontal);
199 201 // chartView->addSeries(series);
200 202 }
201 else if (splineRadioButton->isChecked())
203 else if (m_splineRadioButton->isChecked())
202 204 {
203 205 // series 1
204 series = new QSplineSeries;
205 series->setModel(m_model);
206 series->setModelMapping(0,1, Qt::Vertical);
207 series->setModelMappingShift(1, 4);
206 m_series = new QSplineSeries;
207 m_series->setModel(m_model);
208 m_series->setModelMapping(0,1, Qt::Vertical);
209 m_series->setModelMappingShift(1, 4);
208 210 // series->setModelMapping(0,1, Qt::Horizontal);
209 chartView->addSeries(series);
211 m_chart->addSeries(m_series);
210 212
211 213 // series 2
212 series = new QSplineSeries;
213 series->setModel(m_model);
214 series->setModelMapping(2,3, Qt::Vertical);
215 series->setModelMappingShift(0, 0);
214 m_series = new QSplineSeries;
215 m_series->setModel(m_model);
216 m_series->setModelMapping(2,3, Qt::Vertical);
217 m_series->setModelMappingShift(0, 0);
216 218 // series->setModelMapping(2,3, Qt::Horizontal);
217 chartView->addSeries(series);
219 m_chart->addSeries(m_series);
218 220
219 221 // series 3
220 series = new QSplineSeries;
221 series->setModel(m_model);
222 series->setModelMapping(4,5, Qt::Vertical);
223 series->setModelMappingShift(0, 0);
222 m_series = new QSplineSeries;
223 m_series->setModel(m_model);
224 m_series->setModelMapping(4,5, Qt::Vertical);
225 m_series->setModelMappingShift(0, 0);
224 226 // series->setModelMapping(4,5, Qt::Horizontal);
225 chartView->addSeries(series);
227 m_chart->addSeries(m_series);
226 228 }
227 else if (scatterRadioButton->isChecked())
229 else if (m_scatterRadioButton->isChecked())
228 230 {
229 231 // series 1
230 series = new QScatterSeries;
231 series->setModel(m_model);
232 series->setModelMapping(0,1, Qt::Vertical);
232 m_series = new QScatterSeries;
233 m_series->setModel(m_model);
234 m_series->setModelMapping(0,1, Qt::Vertical);
233 235 // series->setModelMapping(0,1, Qt::Horizontal);
234 chartView->addSeries(series);
236 m_chart->addSeries(m_series);
235 237
236 238 // series 2
237 series = new QScatterSeries;
238 series->setModel(m_model);
239 series->setModelMapping(2,3, Qt::Vertical);
239 m_series = new QScatterSeries;
240 m_series->setModel(m_model);
241 m_series->setModelMapping(2,3, Qt::Vertical);
240 242 // series->setModelMapping(2,3, Qt::Horizontal);
241 chartView->addSeries(series);
243 m_chart->addSeries(m_series);
242 244
243 245 // series 3
244 series = new QScatterSeries;
245 series->setModel(m_model);
246 series->setModelMapping(4,5, Qt::Vertical);
246 m_series = new QScatterSeries;
247 m_series->setModel(m_model);
248 m_series->setModelMapping(4,5, Qt::Vertical);
247 249 // series->setModelMapping(4,5, Qt::Horizontal);
248 chartView->addSeries(series);
250 m_chart->addSeries(m_series);
249 251 }
250 else if (pieRadioButton->isChecked())
252 else if (m_pieRadioButton->isChecked())
251 253 {
252 254 // pie 1
253 255 QPieSeries* pieSeries = new QPieSeries;
@@ -256,7 +258,7 void TableWidget::updateChartType()
256 258 pieSeries->setLabelsVisible(true);
257 259 pieSeries->setPieSize(0.4);
258 260 pieSeries->setPiePosition(0.2, 0.35);
259 chartView->addSeries(pieSeries);
261 m_chart->addSeries(pieSeries);
260 262
261 263 // pie 2
262 264 pieSeries = new QPieSeries;
@@ -265,7 +267,7 void TableWidget::updateChartType()
265 267 pieSeries->setLabelsVisible(true);
266 268 pieSeries->setPieSize(0.4);
267 269 pieSeries->setPiePosition(0.8, 0.35);
268 chartView->addSeries(pieSeries);
270 m_chart->addSeries(pieSeries);
269 271
270 272 // pie 3
271 273 pieSeries = new QPieSeries;
@@ -274,9 +276,9 void TableWidget::updateChartType()
274 276 pieSeries->setLabelsVisible(true);
275 277 pieSeries->setPieSize(0.4);
276 278 pieSeries->setPiePosition(0.5, 0.65);
277 chartView->addSeries(pieSeries);
279 m_chart->addSeries(pieSeries);
278 280 }
279 else if (areaRadioButton->isChecked())
281 else if (m_areaRadioButton->isChecked())
280 282 {
281 283 QLineSeries* upperLineSeries = new QLineSeries;
282 284 upperLineSeries->setModel(m_model);
@@ -285,15 +287,15 void TableWidget::updateChartType()
285 287 lowerLineSeries->setModel(m_model);
286 288 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
287 289 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
288 chartView->addSeries(areaSeries);
290 m_chart->addSeries(areaSeries);
289 291 }
290 else if (barRadioButton->isChecked())
292 else if (m_barRadioButton->isChecked())
291 293 {
292 294 QBarSeries* barSeries = new QBarSeries(QStringList());
293 295 barSeries->setModel(m_model);
294 296 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
295 297 barSeries->setToolTipEnabled(true);
296 chartView->addSeries(barSeries);
298 m_chart->addSeries(barSeries);
297 299 }
298 300
299 301 // series->setModel(m_model);
@@ -49,17 +49,18 public:
49 49 void updateChartType();
50 50
51 51 private:
52 QChartView* chartView;
53 QXYSeries* series;
52 QChartView* m_chartView;
53 QChart* m_chart;
54 QXYSeries* m_series;
54 55 CustomTableModel* m_model;
55 QTableView* tableView;
56 QRadioButton* lineRadioButton;
57 QRadioButton* splineRadioButton;
58 QRadioButton* scatterRadioButton;
59 QRadioButton* pieRadioButton;
60 QRadioButton* areaRadioButton;
61 QRadioButton* barRadioButton;
62 QSpinBox* linesCountSpinBox;
56 QTableView* m_tableView;
57 QRadioButton* m_lineRadioButton;
58 QRadioButton* m_splineRadioButton;
59 QRadioButton* m_scatterRadioButton;
60 QRadioButton* m_pieRadioButton;
61 QRadioButton* m_areaRadioButton;
62 QRadioButton* m_barRadioButton;
63 QSpinBox* m_linesCountSpinBox;
63 64 };
64 65
65 66 #endif // TABLEWIDGET_H
@@ -345,10 +345,10 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
345 345 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
346 346 // internal storage before new ones can be added
347 347
348 int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1);
348 int itemsToRemove = qMin(count() - qMax(start - m_mapFirst, 0), end - start + 1);
349 349 if (m_mapCount == count()) {
350 350 for (int i = 0; i < itemsToRemove; i++)
351 emit pointRemoved(count() - 1 - i);
351 emit pointRemoved(qMin(end, count()) - i);
352 352 }
353 353 }
354 354 } else {
@@ -372,14 +372,16 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
372 372 // the added data is in the mapped area or before it
373 373 // update needed
374 374 if (count() > 0) {
375 for (int i = 0; i < qMin(m_mapCount - (start - m_mapFirst), end - start + 1); i++)
376 emit pointAdded(qMax(start + i - m_mapFirst, 0));
375 int toBeAdded = qMin(m_mapCount - (start - m_mapFirst), end - start + 1);
376 for (int i = 0; i < toBeAdded; i++)
377 if (start + i >= m_mapFirst)
378 emit pointAdded(start + i);
377 379 }
378 380 }
379 381 } else {
380 382 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
381 383 for (int i = 0; i < end - start + 1; i++)
382 emit pointAdded(qMax(start + i - m_mapFirst, 0));
384 emit pointAdded(start + i);
383 385 }
384 386 }
385 387
@@ -403,12 +405,12 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end
403 405 // the max is the current number of items in storage (count())
404 406 int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
405 407 for (int i = 0; i < itemsToRemove; i++)
406 emit pointRemoved(qMax(start - m_mapFirst, 0));
408 emit pointRemoved(start);
407 409 }
408 410 } else {
409 411 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
410 412 for (int i = 0; i < end - start + 1; i++)
411 emit pointRemoved(qMax(start - m_mapFirst, 0));
413 emit pointRemoved(start);
412 414 }
413 415 }
414 416
@@ -428,18 +430,22 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
428 430 } else {
429 431 // if the current items count in the whole model is bigger than the index of the last item
430 432 // that was removed than it means there are some extra items available
433
434 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
431 435 int extraItemsAvailable = 0;
432 436 if (m_mapOrientation == Qt::Vertical) {
433 extraItemsAvailable = qMax(m_model->rowCount() - end, 0);
437 // int temp1 = m_model->rowCount();
438 // int temp2 = (end - start + 1);
439 // int temp3 = qMax(end + 1, m_mapFirst + m_mapCount);
440 extraItemsAvailable = qMax(m_model->rowCount() + (end - start + 1) - qMax(end + 1, m_mapFirst + m_mapCount), 0);
434 441 } else {
435 extraItemsAvailable = qMax(m_model->columnCount() - end, 0);
442 extraItemsAvailable = qMax(m_model->columnCount() + (end - start + 1) - qMax(end + 1, m_mapFirst + m_mapCount), 0);
436 443 }
437 444
438 445 // if there are excess items available (below the mapped area) use them to repopulate mapped area
439 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1);
440 446 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
441 447 for (int k = 0; k < toBeAdded; k++)
442 emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k);
448 emit pointAdded(m_mapFirst + m_mapCount - removedItemsCount + k);
443 449 }
444 450 } else {
445 451 // data was removed from XYSeries interal storage. Nothing more to do
@@ -64,6 +64,7 public:
64 64
65 65 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
66 66 virtual void setModelMappingShift(int first, int count = 0);
67 int mapFirst() const { return m_mapFirst; }
67 68
68 69 private Q_SLOTS:
69 70 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
@@ -105,21 +105,40 void XYChartItem::setLayout(QVector<QPointF> &points)
105 105
106 106 void XYChartItem::handlePointAdded(int index)
107 107 {
108 QVector<QPointF> points = m_points;
109 QPointF point;
110 if (m_series->model()) {
111 point = calculateGeometryPoint(index - m_series->mapFirst());
112 points.insert(index - m_series->mapFirst(), point);
113 updateLayout(m_points, points, index - m_series->mapFirst());
114 }
115 else {
116 // this checks do not work correctly if model is set
108 117 Q_ASSERT(index<m_series->count());
109 118 Q_ASSERT(index>=0);
110 QPointF point = calculateGeometryPoint(index);
111 QVector<QPointF> points = m_points;
119 point = calculateGeometryPoint(index);
112 120 points.insert(index,point);
113 121 updateLayout(m_points,points,index);
122 }
114 123 update();
115 124 }
116 125 void XYChartItem::handlePointRemoved(int index)
117 126 {
127 QVector<QPointF> points = m_points;
128 if (m_series->model()) {
129 if (index < m_series->mapFirst())
130 points.remove(0);
131 else
132 points.remove(index - m_series->mapFirst());
133 updateLayout(m_points, points, index - m_series->mapFirst());
134 }
135 else {
136 // this checks do not work correctly if model is set
118 137 Q_ASSERT(index<m_series->count() + 1);
119 138 Q_ASSERT(index>=0);
120 QVector<QPointF> points = m_points;
121 139 points.remove(index);
122 140 updateLayout(m_points,points,index);
141 }
123 142 update();
124 143 }
125 144
General Comments 0
You need to be logged in to leave comments. Login now