From 8b3d19ded3507f3c2ea20b7fb7ddd29e20e3418d 2012-03-27 06:12:44 From: Marek Rosa Date: 2012-03-27 06:12:44 Subject: [PATCH] Some more work on mapping with limits --- diff --git a/examples/tablemodelchart/customtablemodel.cpp b/examples/tablemodelchart/customtablemodel.cpp index 0918a98..0304082 100644 --- a/examples/tablemodelchart/customtablemodel.cpp +++ b/examples/tablemodelchart/customtablemodel.cpp @@ -24,13 +24,18 @@ CustomTableModel::CustomTableModel(QObject *parent) : for (int i = 0; i < 6; i++) { QVector* dataVec = new QVector(6); + QVector* colorVec = new QVector(6); for (int k = 0; k < dataVec->size(); k++) + { if (k%2 == 0) dataVec->replace(k, i * 50 + qrand()%20); else dataVec->replace(k, qrand()%100); + colorVec->replace(k, QColor(Qt::white)); + } m_data.append(dataVec); m_labels.append(QString("Row: %1").arg((i + 1))); + m_rowsColors.append(colorVec); } } @@ -110,6 +115,10 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const break; } } + else if (role == Qt::BackgroundRole) + { + return m_rowsColors[index.row()]->at(index.column()); + } return QVariant(); } @@ -137,6 +146,11 @@ bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val emit dataChanged(index, index); return true; } + else if (role == Qt::BackgroundRole) + { + m_rowsColors[index.row()]->replace(index.column(), value.value()); + return true; + } return false; } @@ -157,36 +171,43 @@ bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare for (int i = row; i < row + count; i++) { // m_points.insert(row, QPointF(10,20)); - QVector* dataVec = new QVector(6); + QVector* dataVec = new QVector(6); + QVector* colorVec = new QVector(6); for (int k = 0; k < dataVec->size(); k++) + { if (k%2 == 0) // dataVec->replace(k, i * 50 + qrand()%20); { int difference = 0; - if (row < m_data.size()) + if (i < m_data.size()) { - if (row - 1 >= 0) + if (i - 1 >= 0) { - difference = (int)(qAbs(m_data[row]->at(k) - m_data[row - 1]->at(k))); - dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%qMax(1, difference)); + difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count); + dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference)); } else dataVec->replace(k, qrand()%40 + 10); } else - if (row - 1 >= 0) + { + if (i - 1 >= 0) { - dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%40 + 10); + dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10); } else { dataVec->replace(k, qrand()%40 + 10); } + } } else dataVec->replace(k, qrand()%100); - m_data.insert(row, dataVec); - m_labels.insert(row,(QString("Row: %1").arg(row + 1))); + colorVec->replace(k, QColor(Qt::white)); + } + m_data.insert(i, dataVec); + m_labels.insert(i,(QString("Row: %1").arg(i + 1))); + m_rowsColors.insert(i, colorVec); } endInsertRows(); return true; diff --git a/examples/tablemodelchart/customtablemodel.h b/examples/tablemodelchart/customtablemodel.h index abf449e..73270d9 100644 --- a/examples/tablemodelchart/customtablemodel.h +++ b/examples/tablemodelchart/customtablemodel.h @@ -4,6 +4,7 @@ #include #include #include +#include class CustomTableModel : public QAbstractTableModel { @@ -25,6 +26,7 @@ public: //public slots: private: QList* > m_data; + QList* > m_rowsColors; QList m_points; QStringList m_labels; diff --git a/examples/tablemodelchart/tablewidget.cpp b/examples/tablemodelchart/tablewidget.cpp index 9b8d54b..49954cc 100644 --- a/examples/tablemodelchart/tablewidget.cpp +++ b/examples/tablemodelchart/tablewidget.cpp @@ -11,6 +11,7 @@ #include "qbarseries.h" #include #include +#include #include TableWidget::TableWidget(QWidget *parent) @@ -53,8 +54,13 @@ TableWidget::TableWidget(QWidget *parent) QPushButton* removeRowButton = new QPushButton("Remove row"); connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow())); + linesCountSpinBox = new QSpinBox; + linesCountSpinBox->setRange(1, 10); + linesCountSpinBox->setValue(1); + // buttons layout QVBoxLayout* buttonsLayout = new QVBoxLayout; + buttonsLayout->addWidget(linesCountSpinBox); buttonsLayout->addWidget(addRowAboveButton); buttonsLayout->addWidget(addRowBelowButton); buttonsLayout->addWidget(removeRowButton); @@ -99,21 +105,24 @@ TableWidget::TableWidget(QWidget *parent) void TableWidget::addRowAbove() { // m_model->insertRow(m_model->rowCount()); - m_model->insertRow(tableView->currentIndex().row()); +// m_model->insertRow(tableView->currentIndex().row()); + m_model->insertRows(tableView->currentIndex().row(), linesCountSpinBox->value()); } void TableWidget::addRowBelow() { // m_model->insertRow(m_model->rowCount()); - m_model->insertRow(tableView->currentIndex().row() + 1); +// m_model->insertRow(tableView->currentIndex().row() + 1); + m_model->insertRows(tableView->currentIndex().row() + 1, linesCountSpinBox->value()); } void TableWidget::removeRow() { // m_model->removeRow(m_model->rowCount() - 1); - m_model->removeRow(tableView->currentIndex().row()); +// m_model->removeRow(tableView->currentIndex().row()); + m_model->removeRows(tableView->currentIndex().row(), qMin(m_model->rowCount() - tableView->currentIndex().row(), linesCountSpinBox->value())); } void TableWidget::updateChartType() @@ -122,21 +131,46 @@ void TableWidget::updateChartType() if (lineRadioButton->isChecked()) { + QPen pen; + pen.setWidth(2); + + QColor seriesColor("#8FBC8F"); + // series 1 series = new QLineSeries; series->setModel(m_model); series->setModelMapping(0,1, Qt::Vertical); series->setModelMappingShift(1, 4); // series->setModelMapping(0,1, Qt::Horizontal); + + pen.setColor(seriesColor); + series->setPen(pen); chartView->addSeries(series); + for (int i = 1; i <=4; i++) + { + m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole); + m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole); + } +// tableView->setsetStyleSheet("QTableView { border: 2px solid red }"); + + seriesColor = QColor("#1E90FF"); // series 2 series = new QLineSeries; series->setModel(m_model); series->setModelMapping(2,3, Qt::Vertical); // series->setModelMapping(2,3, Qt::Horizontal); + pen.setColor(seriesColor); + series->setPen(pen); chartView->addSeries(series); + chartView->axisX()->setRange(0, 500); + + for (int i = 0; i < m_model->rowCount(); i++) + { + m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole); + m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole); + } // // series 3 // series = new QLineSeries; // series->setModel(m_model); @@ -158,6 +192,7 @@ void TableWidget::updateChartType() series = new QSplineSeries; series->setModel(m_model); series->setModelMapping(2,3, Qt::Vertical); + series->setModelMappingShift(0, 0); // series->setModelMapping(2,3, Qt::Horizontal); chartView->addSeries(series); @@ -165,6 +200,7 @@ void TableWidget::updateChartType() series = new QSplineSeries; series->setModel(m_model); series->setModelMapping(4,5, Qt::Vertical); + series->setModelMappingShift(0, 0); // series->setModelMapping(4,5, Qt::Horizontal); chartView->addSeries(series); } diff --git a/examples/tablemodelchart/tablewidget.h b/examples/tablemodelchart/tablewidget.h index 707b69a..38118d4 100644 --- a/examples/tablemodelchart/tablewidget.h +++ b/examples/tablemodelchart/tablewidget.h @@ -10,6 +10,7 @@ QTCOMMERCIALCHART_USE_NAMESPACE class CustomTableModel; class QTableView; class QRadioButton; +class QSpinBox; //class QSeries; class TableWidget : public QWidget @@ -38,6 +39,7 @@ public: QRadioButton* pieRadioButton; QRadioButton* areaRadioButton; QRadioButton* barRadioButton; + QSpinBox* linesCountSpinBox; }; #endif // TABLEWIDGET_H diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 80fafce..4b98264 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -262,20 +262,20 @@ void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBound m_mapCategories = categories; m_mapBarBottom = bottomBoundry; m_mapBarTop = topBoundry; - m_mapFirst = 1; +// m_mapFirst = 1; m_mapOrientation = orientation; // connect the signals if (m_mapOrientation == Qt::Vertical) { - m_mapCount = m_model->rowCount(); + m_mapCount = m_model->rowCount() - m_mapFirst; connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); } else { - m_mapCount = m_model->columnCount(); + m_mapCount = m_model->columnCount() - m_mapFirst; connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index d960877..ede15fe 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -330,8 +330,10 @@ void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end) // check how many mapped items there is currently (before new items are added) // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem // internal storage before new ones can be added + + int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1); if (m_mapCount == count()) - for (int i = 0; i < qMin(count(), end - start + 1); i++) + for (int i = 0; i < itemsToRemove; i++) emit pointRemoved(count() - 1 - i); } } @@ -359,7 +361,7 @@ void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end) // the added data is in the mapped area or before it // update needed if (count() > 0) - for (int i = 0; i < qMin(m_mapCount, end - start + 1); i++) + for (int i = 0; i < qMin(m_mapCount - (start - m_mapFirst), end - start + 1); i++) emit pointAdded(qMax(start + i - m_mapFirst, 0)); } } @@ -386,12 +388,12 @@ void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end { // the removed data is in the mapped area or before it // update needed - int itemsToRemove = qMin(count(), end - start + 1); - tempItemsRemoved = itemsToRemove; - int z = count(); - z = z; - // if (itemsToRemove > 0) - // { + + // check how many items need to be removed from the xychartitem storage + // the number equals the number of items that are removed and that lay before + // or in the mapped area. Items that lay beyond the map do not count + // the max is the current number of items in storage (count()) + int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1); for (int i = 0; i < itemsToRemove; i++) emit pointRemoved(qMax(start - m_mapFirst, 0)); } @@ -419,29 +421,29 @@ void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end) // therefore it has no relevance return; else - { - // if there are excess items available (below the map) use them to repopulate mapped area + { + // if the current items count in the whole model is bigger than the index of the last item + // that was removed than it means there are some extra items available int extraItemsAvailable = 0; if (m_mapOrientation == Qt::Vertical) { - extraItemsAvailable = qMax(m_model->rowCount() - m_mapFirst, 0); + extraItemsAvailable = qMax(m_model->rowCount() - end, 0); } else { - extraItemsAvailable = qMax(m_model->columnCount() - m_mapFirst, 0); + extraItemsAvailable = qMax(m_model->columnCount() - end, 0); } - // int extraItemsNeeded = qMin(extraItemsAvailable, tempItemsRemoved); - for (int k = 0; k < tempItemsRemoved; k++) - if (start - m_mapFirst + k < extraItemsAvailable) - { - emit pointAdded(count() - m_mapFirst + k); - } + // if there are excess items available (below the mapped area) use them to repopulate mapped area + int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1); + int toBeAdded = qMin(extraItemsAvailable, removedItemsCount); + for (int k = 0; k < toBeAdded; k++) + emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k); } } else { - // emit pointRemoved(qMax(start - m_mapFirst, 0)); + // data was removed from XYSeries interal storage. Nothing more to do } }