##// 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 24 for (int i = 0; i < 6; i++)
25 25 {
26 26 QVector<qreal>* dataVec = new QVector<qreal>(6);
27 QVector<QColor>* colorVec = new QVector<QColor>(6);
27 28 for (int k = 0; k < dataVec->size(); k++)
29 {
28 30 if (k%2 == 0)
29 31 dataVec->replace(k, i * 50 + qrand()%20);
30 32 else
31 33 dataVec->replace(k, qrand()%100);
34 colorVec->replace(k, QColor(Qt::white));
35 }
32 36 m_data.append(dataVec);
33 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 115 break;
111 116 }
112 117 }
118 else if (role == Qt::BackgroundRole)
119 {
120 return m_rowsColors[index.row()]->at(index.column());
121 }
113 122 return QVariant();
114 123 }
115 124
@@ -137,6 +146,11 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val
137 146 emit dataChanged(index, index);
138 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 154 return false;
141 155 }
142 156
@@ -158,35 +172,42 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare
158 172 {
159 173 // m_points.insert(row, QPointF(10,20));
160 174 QVector<qreal>* dataVec = new QVector<qreal>(6);
175 QVector<QColor>* colorVec = new QVector<QColor>(6);
161 176 for (int k = 0; k < dataVec->size(); k++)
177 {
162 178 if (k%2 == 0)
163 179 // dataVec->replace(k, i * 50 + qrand()%20);
164 180 {
165 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)));
171 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%qMax(1, difference));
186 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
187 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
172 188 }
173 189 else
174 190 dataVec->replace(k, qrand()%40 + 10);
175 191 }
176 192 else
177 if (row - 1 >= 0)
178 193 {
179 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%40 + 10);
194 if (i - 1 >= 0)
195 {
196 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
180 197 }
181 198 else
182 199 {
183 200 dataVec->replace(k, qrand()%40 + 10);
184 201 }
185 202 }
203 }
186 204 else
187 205 dataVec->replace(k, qrand()%100);
188 m_data.insert(row, dataVec);
189 m_labels.insert(row,(QString("Row: %1").arg(row + 1)));
206 colorVec->replace(k, QColor(Qt::white));
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 212 endInsertRows();
192 213 return true;
@@ -4,6 +4,7
4 4 #include <QAbstractTableModel>
5 5 #include <QPointF>
6 6 #include <QStringList>
7 #include <QColor>
7 8
8 9 class CustomTableModel : public QAbstractTableModel
9 10 {
@@ -25,6 +26,7 public:
25 26 //public slots:
26 27 private:
27 28 QList<QVector<qreal>* > m_data;
29 QList<QVector<QColor>* > m_rowsColors;
28 30 QList<QPointF> m_points;
29 31 QStringList m_labels;
30 32
@@ -11,6 +11,7
11 11 #include "qbarseries.h"
12 12 #include <QPushButton>
13 13 #include <QRadioButton>
14 #include <QSpinBox>
14 15 #include <QTime>
15 16
16 17 TableWidget::TableWidget(QWidget *parent)
@@ -53,8 +54,13 TableWidget::TableWidget(QWidget *parent)
53 54 QPushButton* removeRowButton = new QPushButton("Remove row");
54 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 61 // buttons layout
57 62 QVBoxLayout* buttonsLayout = new QVBoxLayout;
63 buttonsLayout->addWidget(linesCountSpinBox);
58 64 buttonsLayout->addWidget(addRowAboveButton);
59 65 buttonsLayout->addWidget(addRowBelowButton);
60 66 buttonsLayout->addWidget(removeRowButton);
@@ -99,21 +105,24 TableWidget::TableWidget(QWidget *parent)
99 105 void TableWidget::addRowAbove()
100 106 {
101 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 113 void TableWidget::addRowBelow()
107 114 {
108 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 121 void TableWidget::removeRow()
114 122 {
115 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 128 void TableWidget::updateChartType()
@@ -122,21 +131,46 void TableWidget::updateChartType()
122 131
123 132 if (lineRadioButton->isChecked())
124 133 {
134 QPen pen;
135 pen.setWidth(2);
136
137 QColor seriesColor("#8FBC8F");
138
125 139 // series 1
126 140 series = new QLineSeries;
127 141 series->setModel(m_model);
128 142 series->setModelMapping(0,1, Qt::Vertical);
129 143 series->setModelMappingShift(1, 4);
130 144 // series->setModelMapping(0,1, Qt::Horizontal);
145
146 pen.setColor(seriesColor);
147 series->setPen(pen);
131 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 158 // series 2
134 159 series = new QLineSeries;
135 160 series->setModel(m_model);
136 161 series->setModelMapping(2,3, Qt::Vertical);
137 162 // series->setModelMapping(2,3, Qt::Horizontal);
163 pen.setColor(seriesColor);
164 series->setPen(pen);
138 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 174 // // series 3
141 175 // series = new QLineSeries;
142 176 // series->setModel(m_model);
@@ -158,6 +192,7 void TableWidget::updateChartType()
158 192 series = new QSplineSeries;
159 193 series->setModel(m_model);
160 194 series->setModelMapping(2,3, Qt::Vertical);
195 series->setModelMappingShift(0, 0);
161 196 // series->setModelMapping(2,3, Qt::Horizontal);
162 197 chartView->addSeries(series);
163 198
@@ -165,6 +200,7 void TableWidget::updateChartType()
165 200 series = new QSplineSeries;
166 201 series->setModel(m_model);
167 202 series->setModelMapping(4,5, Qt::Vertical);
203 series->setModelMappingShift(0, 0);
168 204 // series->setModelMapping(4,5, Qt::Horizontal);
169 205 chartView->addSeries(series);
170 206 }
@@ -10,6 +10,7 QTCOMMERCIALCHART_USE_NAMESPACE
10 10 class CustomTableModel;
11 11 class QTableView;
12 12 class QRadioButton;
13 class QSpinBox;
13 14 //class QSeries;
14 15
15 16 class TableWidget : public QWidget
@@ -38,6 +39,7 public:
38 39 QRadioButton* pieRadioButton;
39 40 QRadioButton* areaRadioButton;
40 41 QRadioButton* barRadioButton;
42 QSpinBox* linesCountSpinBox;
41 43 };
42 44
43 45 #endif // TABLEWIDGET_H
@@ -262,20 +262,20 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBound
262 262 m_mapCategories = categories;
263 263 m_mapBarBottom = bottomBoundry;
264 264 m_mapBarTop = topBoundry;
265 m_mapFirst = 1;
265 // m_mapFirst = 1;
266 266 m_mapOrientation = orientation;
267 267
268 268 // connect the signals
269 269 if (m_mapOrientation == Qt::Vertical)
270 270 {
271 m_mapCount = m_model->rowCount();
271 m_mapCount = m_model->rowCount() - m_mapFirst;
272 272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 273 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
274 274 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
275 275 }
276 276 else
277 277 {
278 m_mapCount = m_model->columnCount();
278 m_mapCount = m_model->columnCount() - m_mapFirst;
279 279 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
280 280 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
281 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 330 // check how many mapped items there is currently (before new items are added)
331 331 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
332 332 // internal storage before new ones can be added
333
334 int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1);
333 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 337 emit pointRemoved(count() - 1 - i);
336 338 }
337 339 }
@@ -359,7 +361,7 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
359 361 // the added data is in the mapped area or before it
360 362 // update needed
361 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 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 389 // the removed data is in the mapped area or before it
388 390 // update needed
389 int itemsToRemove = qMin(count(), end - start + 1);
390 tempItemsRemoved = itemsToRemove;
391 int z = count();
392 z = z;
393 // if (itemsToRemove > 0)
394 // {
391
392 // check how many items need to be removed from the xychartitem storage
393 // the number equals the number of items that are removed and that lay before
394 // or in the mapped area. Items that lay beyond the map do not count
395 // the max is the current number of items in storage (count())
396 int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
395 397 for (int i = 0; i < itemsToRemove; i++)
396 398 emit pointRemoved(qMax(start - m_mapFirst, 0));
397 399 }
@@ -420,28 +422,28 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
420 422 return;
421 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 427 int extraItemsAvailable = 0;
425 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 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);
435 for (int k = 0; k < tempItemsRemoved; k++)
436 if (start - m_mapFirst + k < extraItemsAvailable)
437 {
438 emit pointAdded(count() - m_mapFirst + k);
439 }
437 // if there are excess items available (below the mapped area) use them to repopulate mapped area
438 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1);
439 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
440 for (int k = 0; k < toBeAdded; k++)
441 emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k);
440 442 }
441 443 }
442 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