##// END OF EJS Templates
Some more work on mapping with limits
Marek Rosa -
r735:8b3d19ded350
parent child
Show More
@@ -24,13 +24,18 CustomTableModel::CustomTableModel(QObject *parent) :
24 for (int i = 0; i < 6; i++)
24 for (int i = 0; i < 6; i++)
25 {
25 {
26 QVector<qreal>* dataVec = new QVector<qreal>(6);
26 QVector<qreal>* dataVec = new QVector<qreal>(6);
27 QVector<QColor>* colorVec = new QVector<QColor>(6);
27 for (int k = 0; k < dataVec->size(); k++)
28 for (int k = 0; k < dataVec->size(); k++)
29 {
28 if (k%2 == 0)
30 if (k%2 == 0)
29 dataVec->replace(k, i * 50 + qrand()%20);
31 dataVec->replace(k, i * 50 + qrand()%20);
30 else
32 else
31 dataVec->replace(k, qrand()%100);
33 dataVec->replace(k, qrand()%100);
34 colorVec->replace(k, QColor(Qt::white));
35 }
32 m_data.append(dataVec);
36 m_data.append(dataVec);
33 m_labels.append(QString("Row: %1").arg((i + 1)));
37 m_labels.append(QString("Row: %1").arg((i + 1)));
38 m_rowsColors.append(colorVec);
34 }
39 }
35 }
40 }
36
41
@@ -110,6 +115,10 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
110 break;
115 break;
111 }
116 }
112 }
117 }
118 else if (role == Qt::BackgroundRole)
119 {
120 return m_rowsColors[index.row()]->at(index.column());
121 }
113 return QVariant();
122 return QVariant();
114 }
123 }
115
124
@@ -137,6 +146,11 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val
137 emit dataChanged(index, index);
146 emit dataChanged(index, index);
138 return true;
147 return true;
139 }
148 }
149 else if (role == Qt::BackgroundRole)
150 {
151 m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>());
152 return true;
153 }
140 return false;
154 return false;
141 }
155 }
142
156
@@ -157,36 +171,43 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare
157 for (int i = row; i < row + count; i++)
171 for (int i = row; i < row + count; i++)
158 {
172 {
159 // m_points.insert(row, QPointF(10,20));
173 // m_points.insert(row, QPointF(10,20));
160 QVector<qreal>* dataVec = new QVector<qreal>(6);
174 QVector<qreal>* dataVec = new QVector<qreal>(6);
175 QVector<QColor>* colorVec = new QVector<QColor>(6);
161 for (int k = 0; k < dataVec->size(); k++)
176 for (int k = 0; k < dataVec->size(); k++)
177 {
162 if (k%2 == 0)
178 if (k%2 == 0)
163 // dataVec->replace(k, i * 50 + qrand()%20);
179 // dataVec->replace(k, i * 50 + qrand()%20);
164 {
180 {
165 int difference = 0;
181 int difference = 0;
166 if (row < m_data.size())
182 if (i < m_data.size())
167 {
183 {
168 if (row - 1 >= 0)
184 if (i - 1 >= 0)
169 {
185 {
170 difference = (int)(qAbs(m_data[row]->at(k) - m_data[row - 1]->at(k)));
186 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
171 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%qMax(1, difference));
187 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
172 }
188 }
173 else
189 else
174 dataVec->replace(k, qrand()%40 + 10);
190 dataVec->replace(k, qrand()%40 + 10);
175 }
191 }
176 else
192 else
177 if (row - 1 >= 0)
193 {
194 if (i - 1 >= 0)
178 {
195 {
179 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%40 + 10);
196 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
180 }
197 }
181 else
198 else
182 {
199 {
183 dataVec->replace(k, qrand()%40 + 10);
200 dataVec->replace(k, qrand()%40 + 10);
184 }
201 }
202 }
185 }
203 }
186 else
204 else
187 dataVec->replace(k, qrand()%100);
205 dataVec->replace(k, qrand()%100);
188 m_data.insert(row, dataVec);
206 colorVec->replace(k, QColor(Qt::white));
189 m_labels.insert(row,(QString("Row: %1").arg(row + 1)));
207 }
208 m_data.insert(i, dataVec);
209 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
210 m_rowsColors.insert(i, colorVec);
190 }
211 }
191 endInsertRows();
212 endInsertRows();
192 return true;
213 return true;
@@ -4,6 +4,7
4 #include <QAbstractTableModel>
4 #include <QAbstractTableModel>
5 #include <QPointF>
5 #include <QPointF>
6 #include <QStringList>
6 #include <QStringList>
7 #include <QColor>
7
8
8 class CustomTableModel : public QAbstractTableModel
9 class CustomTableModel : public QAbstractTableModel
9 {
10 {
@@ -25,6 +26,7 public:
25 //public slots:
26 //public slots:
26 private:
27 private:
27 QList<QVector<qreal>* > m_data;
28 QList<QVector<qreal>* > m_data;
29 QList<QVector<QColor>* > m_rowsColors;
28 QList<QPointF> m_points;
30 QList<QPointF> m_points;
29 QStringList m_labels;
31 QStringList m_labels;
30
32
@@ -11,6 +11,7
11 #include "qbarseries.h"
11 #include "qbarseries.h"
12 #include <QPushButton>
12 #include <QPushButton>
13 #include <QRadioButton>
13 #include <QRadioButton>
14 #include <QSpinBox>
14 #include <QTime>
15 #include <QTime>
15
16
16 TableWidget::TableWidget(QWidget *parent)
17 TableWidget::TableWidget(QWidget *parent)
@@ -53,8 +54,13 TableWidget::TableWidget(QWidget *parent)
53 QPushButton* removeRowButton = new QPushButton("Remove row");
54 QPushButton* removeRowButton = new QPushButton("Remove row");
54 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
55 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
55
56
57 linesCountSpinBox = new QSpinBox;
58 linesCountSpinBox->setRange(1, 10);
59 linesCountSpinBox->setValue(1);
60
56 // buttons layout
61 // buttons layout
57 QVBoxLayout* buttonsLayout = new QVBoxLayout;
62 QVBoxLayout* buttonsLayout = new QVBoxLayout;
63 buttonsLayout->addWidget(linesCountSpinBox);
58 buttonsLayout->addWidget(addRowAboveButton);
64 buttonsLayout->addWidget(addRowAboveButton);
59 buttonsLayout->addWidget(addRowBelowButton);
65 buttonsLayout->addWidget(addRowBelowButton);
60 buttonsLayout->addWidget(removeRowButton);
66 buttonsLayout->addWidget(removeRowButton);
@@ -99,21 +105,24 TableWidget::TableWidget(QWidget *parent)
99 void TableWidget::addRowAbove()
105 void TableWidget::addRowAbove()
100 {
106 {
101 // m_model->insertRow(m_model->rowCount());
107 // m_model->insertRow(m_model->rowCount());
102 m_model->insertRow(tableView->currentIndex().row());
108 // m_model->insertRow(tableView->currentIndex().row());
109 m_model->insertRows(tableView->currentIndex().row(), linesCountSpinBox->value());
103
110
104 }
111 }
105
112
106 void TableWidget::addRowBelow()
113 void TableWidget::addRowBelow()
107 {
114 {
108 // m_model->insertRow(m_model->rowCount());
115 // m_model->insertRow(m_model->rowCount());
109 m_model->insertRow(tableView->currentIndex().row() + 1);
116 // m_model->insertRow(tableView->currentIndex().row() + 1);
117 m_model->insertRows(tableView->currentIndex().row() + 1, linesCountSpinBox->value());
110
118
111 }
119 }
112
120
113 void TableWidget::removeRow()
121 void TableWidget::removeRow()
114 {
122 {
115 // m_model->removeRow(m_model->rowCount() - 1);
123 // m_model->removeRow(m_model->rowCount() - 1);
116 m_model->removeRow(tableView->currentIndex().row());
124 // m_model->removeRow(tableView->currentIndex().row());
125 m_model->removeRows(tableView->currentIndex().row(), qMin(m_model->rowCount() - tableView->currentIndex().row(), linesCountSpinBox->value()));
117 }
126 }
118
127
119 void TableWidget::updateChartType()
128 void TableWidget::updateChartType()
@@ -122,21 +131,46 void TableWidget::updateChartType()
122
131
123 if (lineRadioButton->isChecked())
132 if (lineRadioButton->isChecked())
124 {
133 {
134 QPen pen;
135 pen.setWidth(2);
136
137 QColor seriesColor("#8FBC8F");
138
125 // series 1
139 // series 1
126 series = new QLineSeries;
140 series = new QLineSeries;
127 series->setModel(m_model);
141 series->setModel(m_model);
128 series->setModelMapping(0,1, Qt::Vertical);
142 series->setModelMapping(0,1, Qt::Vertical);
129 series->setModelMappingShift(1, 4);
143 series->setModelMappingShift(1, 4);
130 // series->setModelMapping(0,1, Qt::Horizontal);
144 // series->setModelMapping(0,1, Qt::Horizontal);
145
146 pen.setColor(seriesColor);
147 series->setPen(pen);
131 chartView->addSeries(series);
148 chartView->addSeries(series);
149 for (int i = 1; i <=4; i++)
150 {
151 m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole);
152 m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole);
153 }
154 // tableView->setsetStyleSheet("QTableView { border: 2px solid red }");
155
156 seriesColor = QColor("#1E90FF");
132
157
133 // series 2
158 // series 2
134 series = new QLineSeries;
159 series = new QLineSeries;
135 series->setModel(m_model);
160 series->setModel(m_model);
136 series->setModelMapping(2,3, Qt::Vertical);
161 series->setModelMapping(2,3, Qt::Vertical);
137 // series->setModelMapping(2,3, Qt::Horizontal);
162 // series->setModelMapping(2,3, Qt::Horizontal);
163 pen.setColor(seriesColor);
164 series->setPen(pen);
138 chartView->addSeries(series);
165 chartView->addSeries(series);
139
166
167 chartView->axisX()->setRange(0, 500);
168
169 for (int i = 0; i < m_model->rowCount(); i++)
170 {
171 m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole);
172 m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole);
173 }
140 // // series 3
174 // // series 3
141 // series = new QLineSeries;
175 // series = new QLineSeries;
142 // series->setModel(m_model);
176 // series->setModel(m_model);
@@ -158,6 +192,7 void TableWidget::updateChartType()
158 series = new QSplineSeries;
192 series = new QSplineSeries;
159 series->setModel(m_model);
193 series->setModel(m_model);
160 series->setModelMapping(2,3, Qt::Vertical);
194 series->setModelMapping(2,3, Qt::Vertical);
195 series->setModelMappingShift(0, 0);
161 // series->setModelMapping(2,3, Qt::Horizontal);
196 // series->setModelMapping(2,3, Qt::Horizontal);
162 chartView->addSeries(series);
197 chartView->addSeries(series);
163
198
@@ -165,6 +200,7 void TableWidget::updateChartType()
165 series = new QSplineSeries;
200 series = new QSplineSeries;
166 series->setModel(m_model);
201 series->setModel(m_model);
167 series->setModelMapping(4,5, Qt::Vertical);
202 series->setModelMapping(4,5, Qt::Vertical);
203 series->setModelMappingShift(0, 0);
168 // series->setModelMapping(4,5, Qt::Horizontal);
204 // series->setModelMapping(4,5, Qt::Horizontal);
169 chartView->addSeries(series);
205 chartView->addSeries(series);
170 }
206 }
@@ -10,6 +10,7 QTCOMMERCIALCHART_USE_NAMESPACE
10 class CustomTableModel;
10 class CustomTableModel;
11 class QTableView;
11 class QTableView;
12 class QRadioButton;
12 class QRadioButton;
13 class QSpinBox;
13 //class QSeries;
14 //class QSeries;
14
15
15 class TableWidget : public QWidget
16 class TableWidget : public QWidget
@@ -38,6 +39,7 public:
38 QRadioButton* pieRadioButton;
39 QRadioButton* pieRadioButton;
39 QRadioButton* areaRadioButton;
40 QRadioButton* areaRadioButton;
40 QRadioButton* barRadioButton;
41 QRadioButton* barRadioButton;
42 QSpinBox* linesCountSpinBox;
41 };
43 };
42
44
43 #endif // TABLEWIDGET_H
45 #endif // TABLEWIDGET_H
@@ -262,20 +262,20 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBound
262 m_mapCategories = categories;
262 m_mapCategories = categories;
263 m_mapBarBottom = bottomBoundry;
263 m_mapBarBottom = bottomBoundry;
264 m_mapBarTop = topBoundry;
264 m_mapBarTop = topBoundry;
265 m_mapFirst = 1;
265 // m_mapFirst = 1;
266 m_mapOrientation = orientation;
266 m_mapOrientation = orientation;
267
267
268 // connect the signals
268 // connect the signals
269 if (m_mapOrientation == Qt::Vertical)
269 if (m_mapOrientation == Qt::Vertical)
270 {
270 {
271 m_mapCount = m_model->rowCount();
271 m_mapCount = m_model->rowCount() - m_mapFirst;
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
273 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
274 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
274 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
275 }
275 }
276 else
276 else
277 {
277 {
278 m_mapCount = m_model->columnCount();
278 m_mapCount = m_model->columnCount() - m_mapFirst;
279 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
279 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
280 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
280 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
281 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
281 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
@@ -330,8 +330,10 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
330 // check how many mapped items there is currently (before new items are added)
330 // check how many mapped items there is currently (before new items are added)
331 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
331 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
332 // internal storage before new ones can be added
332 // internal storage before new ones can be added
333
334 int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1);
333 if (m_mapCount == count())
335 if (m_mapCount == count())
334 for (int i = 0; i < qMin(count(), end - start + 1); i++)
336 for (int i = 0; i < itemsToRemove; i++)
335 emit pointRemoved(count() - 1 - i);
337 emit pointRemoved(count() - 1 - i);
336 }
338 }
337 }
339 }
@@ -359,7 +361,7 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
359 // the added data is in the mapped area or before it
361 // the added data is in the mapped area or before it
360 // update needed
362 // update needed
361 if (count() > 0)
363 if (count() > 0)
362 for (int i = 0; i < qMin(m_mapCount, end - start + 1); i++)
364 for (int i = 0; i < qMin(m_mapCount - (start - m_mapFirst), end - start + 1); i++)
363 emit pointAdded(qMax(start + i - m_mapFirst, 0));
365 emit pointAdded(qMax(start + i - m_mapFirst, 0));
364 }
366 }
365 }
367 }
@@ -386,12 +388,12 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end
386 {
388 {
387 // the removed data is in the mapped area or before it
389 // the removed data is in the mapped area or before it
388 // update needed
390 // update needed
389 int itemsToRemove = qMin(count(), end - start + 1);
391
390 tempItemsRemoved = itemsToRemove;
392 // check how many items need to be removed from the xychartitem storage
391 int z = count();
393 // the number equals the number of items that are removed and that lay before
392 z = z;
394 // or in the mapped area. Items that lay beyond the map do not count
393 // if (itemsToRemove > 0)
395 // the max is the current number of items in storage (count())
394 // {
396 int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
395 for (int i = 0; i < itemsToRemove; i++)
397 for (int i = 0; i < itemsToRemove; i++)
396 emit pointRemoved(qMax(start - m_mapFirst, 0));
398 emit pointRemoved(qMax(start - m_mapFirst, 0));
397 }
399 }
@@ -419,29 +421,29 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
419 // therefore it has no relevance
421 // therefore it has no relevance
420 return;
422 return;
421 else
423 else
422 {
424 {
423 // if there are excess items available (below the map) use them to repopulate mapped area
425 // if the current items count in the whole model is bigger than the index of the last item
426 // that was removed than it means there are some extra items available
424 int extraItemsAvailable = 0;
427 int extraItemsAvailable = 0;
425 if (m_mapOrientation == Qt::Vertical)
428 if (m_mapOrientation == Qt::Vertical)
426 {
429 {
427 extraItemsAvailable = qMax(m_model->rowCount() - m_mapFirst, 0);
430 extraItemsAvailable = qMax(m_model->rowCount() - end, 0);
428 }
431 }
429 else
432 else
430 {
433 {
431 extraItemsAvailable = qMax(m_model->columnCount() - m_mapFirst, 0);
434 extraItemsAvailable = qMax(m_model->columnCount() - end, 0);
432 }
435 }
433
436
434 // int extraItemsNeeded = qMin(extraItemsAvailable, tempItemsRemoved);
437 // if there are excess items available (below the mapped area) use them to repopulate mapped area
435 for (int k = 0; k < tempItemsRemoved; k++)
438 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1);
436 if (start - m_mapFirst + k < extraItemsAvailable)
439 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
437 {
440 for (int k = 0; k < toBeAdded; k++)
438 emit pointAdded(count() - m_mapFirst + k);
441 emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k);
439 }
440 }
442 }
441 }
443 }
442 else
444 else
443 {
445 {
444 // emit pointRemoved(qMax(start - m_mapFirst, 0));
446 // data was removed from XYSeries interal storage. Nothing more to do
445 }
447 }
446 }
448 }
447
449
General Comments 0
You need to be logged in to leave comments. Login now