diff --git a/examples/tablemodelchart/customtablemodel.cpp b/examples/tablemodelchart/customtablemodel.cpp index 6f26c1b..e4d5c71 100644 --- a/examples/tablemodelchart/customtablemodel.cpp +++ b/examples/tablemodelchart/customtablemodel.cpp @@ -24,7 +24,7 @@ int CustomTableModel::rowCount(const QModelIndex & parent) const int CustomTableModel::columnCount(const QModelIndex & parent) const { - return 2; + return 3; } QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const @@ -116,11 +116,29 @@ Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) { beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); - for (int i = 0; i < count; i++) + for (int i = row; i < row + count; i++) { - m_points.append(QPointF()); - m_labels.append(""); + m_points.insert(row, QPointF()); + m_labels.insert(row,("")); } endInsertRows(); return true; } + +bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent) +{ + if (row > this->rowCount() - 1) + return false; + if (row < 0) + row = 0; + if (row + count > rowCount()) + return false; + beginRemoveRows(parent, row, row + count - 1); + for (int i = row; i < row + count; i++) + { + m_points.removeAt(row); + m_labels.removeAt(row); + } + endRemoveRows(); + return true; +} diff --git a/examples/tablemodelchart/customtablemodel.h b/examples/tablemodelchart/customtablemodel.h index aa978a0..09aa591 100644 --- a/examples/tablemodelchart/customtablemodel.h +++ b/examples/tablemodelchart/customtablemodel.h @@ -18,6 +18,7 @@ public: bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ); Qt::ItemFlags flags ( const QModelIndex & index ) const; bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); + bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); //signals: diff --git a/examples/tablemodelchart/tablewidget.cpp b/examples/tablemodelchart/tablewidget.cpp index c5b1e15..2964f45 100644 --- a/examples/tablemodelchart/tablewidget.cpp +++ b/examples/tablemodelchart/tablewidget.cpp @@ -4,19 +4,20 @@ #include #include "qlineseries.h" #include "qsplineseries.h" +#include "qscatterseries.h" #include "customtablemodel.h" #include "qpieseries.h" +#include +#include TableWidget::TableWidget(QWidget *parent) : QWidget(parent) { - - // create simple model for storing data // user's table data model - CustomTableModel* model = new CustomTableModel; - QTableView* tableView = new QTableView; - tableView->setModel(model); + m_model = new CustomTableModel; + tableView = new QTableView; + tableView->setModel(m_model); tableView->setMinimumSize(340, 480); // tableView->setItemDelegate(new QStyledItemDelegate); chartView = new QChartView; @@ -24,28 +25,82 @@ TableWidget::TableWidget(QWidget *parent) // create // QLineSeries* series = new QLineSeries; - QSplineSeries* series = new QSplineSeries; - series->setModel(model); - series->setModelMapping(0,1, Qt::Vertical); -// series->setModelMappingY(1); - -// series->add(QPointF(150, 100)); -// series->add(QPointF(200, 130)); -// series->add(QPointF(250, 120)); -// series->add(QPointF(300, 140)); -// series->add(QPointF(350, 160)); +// QSplineSeries* series = new QSplineSeries; +// QScatterSeries* series = new QScatterSeries; +// series->setModel(m_model); +// series->setModelMapping(0,1, Qt::Vertical); // QPieSeries* pieSeries = new QPieSeries; // pieSeries->setModel(model); // pieSeries - chartView->addSeries(series); +// chartView->addSeries(series); + + // add, remove data buttons + QPushButton* addRowButton = new QPushButton("Add row"); + connect(addRowButton, SIGNAL(clicked()), this, SLOT(addRow())); + + QPushButton* removeRowButton = new QPushButton("Remove row"); + connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow())); + + // buttons layout + QVBoxLayout* buttonsLayout = new QVBoxLayout; + buttonsLayout->addWidget(addRowButton); + buttonsLayout->addWidget(removeRowButton); + buttonsLayout->addStretch(); + + // chart type radio buttons + lineRadioButton = new QRadioButton("Line"); + splineRadioButton = new QRadioButton("Spline"); + scatterRadioButton = new QRadioButton("Scatter"); + + connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); + connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); + connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); + lineRadioButton->setChecked(true); + + // radio buttons layout + QHBoxLayout* radioLayout = new QHBoxLayout; + radioLayout->addWidget(lineRadioButton); + radioLayout->addWidget(splineRadioButton); + radioLayout->addWidget(scatterRadioButton); // create main layout QGridLayout* mainLayout = new QGridLayout; + mainLayout->addLayout(buttonsLayout, 0, 1); + mainLayout->addLayout(radioLayout, 0, 2); mainLayout->addWidget(tableView, 1, 1); mainLayout->addWidget(chartView, 1, 2); - setLayout(mainLayout); + setLayout(mainLayout); +} + +void TableWidget::addRow() +{ +// m_model->insertRow(m_model->rowCount()); + m_model->insertRow(tableView->currentIndex().row() + 1); + +} + +void TableWidget::removeRow() +{ +// m_model->removeRow(m_model->rowCount() - 1); + m_model->removeRow(tableView->currentIndex().row()); +} + +void TableWidget::updateChartType() +{ + chartView->removeAllSeries(); + + if (lineRadioButton->isChecked()) + series = new QLineSeries; + else if (splineRadioButton->isChecked()) + series = new QSplineSeries; + else + series = new QScatterSeries; + + series->setModel(m_model); + series->setModelMapping(0,1, Qt::Vertical); + chartView->addSeries(series); } TableWidget::~TableWidget() diff --git a/examples/tablemodelchart/tablewidget.h b/examples/tablemodelchart/tablewidget.h index feae5ca..bb8515d 100644 --- a/examples/tablemodelchart/tablewidget.h +++ b/examples/tablemodelchart/tablewidget.h @@ -3,9 +3,15 @@ #include #include "qchartview.h" +#include "qxyseries.h" QTCOMMERCIALCHART_USE_NAMESPACE +class CustomTableModel; +class QTableView; +class QRadioButton; +//class QSeries; + class TableWidget : public QWidget { Q_OBJECT @@ -15,9 +21,19 @@ public: ~TableWidget(); + public slots: + void addRow(); + void removeRow(); + void updateChartType(); private: QChartView* chartView; + QXYSeries* series; + CustomTableModel* m_model; + QTableView* tableView; + QRadioButton* lineRadioButton; + QRadioButton* splineRadioButton; + QRadioButton* scatterRadioButton; }; #endif // TABLEWIDGET_H diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index dec0881..87ef527 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -31,7 +31,11 @@ QPainterPath LineChartItem::shape() const void LineChartItem::setGeometry(QVector& points) { - if(points.size()==0) return; + if(points.size()==0) + { + XYChartItem::setGeometry(points); + return; + } QList items = m_items.childItems(); diff --git a/src/scatterseries/scatterchartitem.cpp b/src/scatterseries/scatterchartitem.cpp index 5279af1..07d91ef 100644 --- a/src/scatterseries/scatterchartitem.cpp +++ b/src/scatterseries/scatterchartitem.cpp @@ -92,7 +92,11 @@ void ScatterChartItem::markerSelected(Marker* marker) void ScatterChartItem::setGeometry(QVector& points) { - if(points.size()==0) return; + if(points.size()==0) + { + XYChartItem::setGeometry(points); + return; + } int diff = XYChartItem::points().size() - points.size(); diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index cc4d5bd..06ee33a 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -31,7 +31,11 @@ QPointF SplineChartItem::calculateGeometryControlPoint(int index) const void SplineChartItem::setGeometry(QVector& points) { - if(points.size()==0) return; + if(points.size()==0) + { + XYChartItem::setGeometry(points); + return; + } QPainterPath splinePath; const QPointF& point = points.at(0); diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 40d0ed0..52f9ae4 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -121,9 +121,10 @@ void QXYSeries::remove(qreal x,qreal y) }while(index !=-1 && m_y.at(index)!=y); if(index==-1) return; - emit pointRemoved(index); + m_x.remove(index); m_y.remove(index); + emit pointRemoved(index); } /*! @@ -265,12 +266,23 @@ void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) emit pointReplaced(topLeft.row()); } +void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end) +{ + emit pointAdded(start); +} + +void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end) +{ + emit pointRemoved(start); +} + bool QXYSeries::setModel(QAbstractItemModel* model) { m_model = model; // for (int i = 0; i < m_model->rowCount(); i++) // emit pointAdded(i); connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); - // connect(m_model,SIGNAL(), 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))); } void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation) diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index f8e48cc..bc918db 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -42,11 +42,13 @@ public: bool setModel(QAbstractItemModel* model); QAbstractItemModel* model() {return m_model;} - void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical); + virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical); // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical); private slots: void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); + void modelDataAdded(QModelIndex parent, int start, int end); + void modelDataRemoved(QModelIndex parent, int start, int end); signals: void updated(); diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index 25dd43f..9a46d0b 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -99,9 +99,9 @@ void XYChartItem::handlePointAdded(int index) } void XYChartItem::handlePointRemoved(int index) { - Q_ASSERT(indexcount()); + Q_ASSERT(indexcount() + 1); Q_ASSERT(index>=0); - QPointF point = calculateGeometryPoint(index); +// QPointF point = calculateGeometryPoint(index); QVector points = m_points; points.remove(index); updatePoints(points);