From 1951d151d7af63e57171757582ae2314abb02432 2012-05-31 10:15:11 From: Marek Rosa Date: 2012-05-31 10:15:11 Subject: [PATCH] Fixed problem with qhxymodelmapper --- diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index 35d19e4..ab1958d 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -253,17 +253,25 @@ void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottom for (int row = topLeft.row(); row <= bottomRight.row(); row++) { for (int column = topLeft.column(); column <= bottomRight.column(); column++) { index = topLeft.sibling(row, column); - if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { + if (m_orientation == Qt::Vertical && (index.column() == m_xSection || index.column() == m_ySection)) { if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { - oldPoint = m_series->points().at(index.row() - m_first); - newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); - newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); + QModelIndex xIndex = xModelIndex(index.row() - m_first); + QModelIndex yIndex = yModelIndex(index.row() - m_first); + if (xIndex.isValid() && yIndex.isValid()) { + oldPoint = m_series->points().at(index.row() - m_first); + newPoint.setX(m_model->data(xIndex).toReal()); + newPoint.setY(m_model->data(yIndex).toReal()); + } } } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { - oldPoint = m_series->points().at(index.column() - m_first); - newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); - newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); + QModelIndex xIndex = xModelIndex(index.column() - m_first); + QModelIndex yIndex = yModelIndex(index.column() - m_first); + if (xIndex.isValid() && yIndex.isValid()) { + oldPoint = m_series->points().at(index.column() - m_first); + newPoint.setX(m_model->data(xIndex).toReal()); + newPoint.setY(m_model->data(yIndex).toReal()); + } } } else { continue; @@ -345,9 +353,13 @@ void QXYModelMapperPrivate::insertData(int start, int end) int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); for (int i = first; i <= last; i++) { QPointF point; - point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); - point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); - m_series->insert(i - m_first, point); + QModelIndex xIndex = xModelIndex(i - m_first); + QModelIndex yIndex = yModelIndex(i - m_first); + if (xIndex.isValid() && yIndex.isValid()) { + point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); + point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); + m_series->insert(i - m_first, point); + } } // remove excess of slices (abouve m_count) @@ -385,9 +397,13 @@ void QXYModelMapperPrivate::removeData(int start, int end) if (toBeAdded > 0) for (int i = m_series->count(); i < currentSize + toBeAdded; i++) { QPointF point; - point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); - point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); - m_series->insert(i, point); + QModelIndex xIndex = xModelIndex(i); + QModelIndex yIndex = yModelIndex(i); + if (xIndex.isValid() && yIndex.isValid()) { + point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); + point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); + m_series->insert(i, point); + } } } } diff --git a/tests/tablemodelchart/customtablemodel.cpp b/tests/tablemodelchart/customtablemodel.cpp index 68fed3d..1d26362 100644 --- a/tests/tablemodelchart/customtablemodel.cpp +++ b/tests/tablemodelchart/customtablemodel.cpp @@ -30,7 +30,7 @@ CustomTableModel::CustomTableModel(QObject *parent) : qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); m_columnCount = 7; - m_rowCount = 9; + m_rowCount = 1029; m_labels.append("Apples"); m_labels.append("Oranges"); @@ -67,7 +67,7 @@ int CustomTableModel::rowCount(const QModelIndex & parent) const int CustomTableModel::columnCount(const QModelIndex & parent) const { Q_UNUSED(parent) - return m_columnCount + 1; + return m_columnCount;// + 1; } QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const @@ -102,8 +102,8 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const { switch(index.column()) { - case 7: - return m_labels[index.row()]; +// case 7: +// return m_labels[index.row()]; default: return m_data[index.row()]->at(index.column()); break; @@ -113,8 +113,8 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const { switch(index.column()) { - case 7: - return m_labels[index.row()]; +// case 7: +// return m_labels[index.row()]; default: return m_data[index.row()]->at(index.column()); break; diff --git a/tests/tablemodelchart/tablewidget.cpp b/tests/tablemodelchart/tablewidget.cpp index c3cdac1..61196d3 100644 --- a/tests/tablemodelchart/tablewidget.cpp +++ b/tests/tablemodelchart/tablewidget.cpp @@ -236,27 +236,27 @@ void TableWidget::updateChartType(bool toggle) if (m_lineRadioButton->isChecked()) { - m_chart->setAnimationOptions(QChart::NoAnimation); +// m_chart->setAnimationOptions(QChart::NoAnimation); // series 1 m_series = new QLineSeries; - m_mapper = new QHXYModelMapper; - m_mapper->setModel(m_model); - m_mapper->setSeries(m_series); - m_mapper->setXRow(0); - m_mapper->setYRow(1); - m_mapper->setFirst(3); - m_mapper->setCount(4); - -// m_mapper = new QVXYModelMapper; +// m_mapper = new QHXYModelMapper; // m_mapper->setModel(m_model); // m_mapper->setSeries(m_series); -// m_mapper->setXColumn(0); -// m_mapper->setYColumn(1); +// m_mapper->setXRow(0); +// m_mapper->setYRow(1); // m_mapper->setFirst(3); // m_mapper->setCount(4); + m_mapper = new QVXYModelMapper; + m_mapper->setModel(m_model); + m_mapper->setSeries(m_series); + m_mapper->setXColumn(0); + m_mapper->setYColumn(1); + m_mapper->setFirst(3); +// m_mapper->setCount(4); + // m_series->setModelMapping(0,1, Qt::Vertical); // m_series->setModelMappingRange(3, 4); m_chart->addSeries(m_series); @@ -505,8 +505,8 @@ void TableWidget::updateChartType(bool toggle) if (!m_barRadioButton->isChecked()) { - m_chart->axisX()->setRange(0, 500); - m_chart->axisY()->setRange(0, 220); +// m_chart->axisX()->setRange(0, 500); +// m_chart->axisY()->setRange(0, 220); } m_chart->legend()->setVisible(true); diff --git a/tests/tablemodelchart/tablewidget.h b/tests/tablemodelchart/tablewidget.h index 63c7b50..abe7c3d 100644 --- a/tests/tablemodelchart/tablewidget.h +++ b/tests/tablemodelchart/tablewidget.h @@ -63,7 +63,7 @@ public: QChartView* m_chartView; QChart* m_chart; QXYSeries* m_series; - QHXYModelMapper *m_mapper; + QVXYModelMapper *m_mapper; CustomTableModel* m_model; QTableView* m_tableView; QRadioButton* m_lineRadioButton;