summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/tablemodelchart/customtablemodel.cpp26
-rw-r--r--examples/tablemodelchart/customtablemodel.h1
-rw-r--r--examples/tablemodelchart/tablewidget.cpp89
-rw-r--r--examples/tablemodelchart/tablewidget.h16
-rw-r--r--src/linechart/linechartitem.cpp6
-rw-r--r--src/scatterseries/scatterchartitem.cpp6
-rw-r--r--src/splinechart/splinechartitem.cpp6
-rw-r--r--src/xychart/qxyseries.cpp16
-rw-r--r--src/xychart/qxyseries.h4
-rw-r--r--src/xychart/xychartitem.cpp4
10 files changed, 145 insertions, 29 deletions
diff --git a/examples/tablemodelchart/customtablemodel.cpp b/examples/tablemodelchart/customtablemodel.cpp
index 6f26c1b1..e4d5c716 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 aa978a0e..09aa591f 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 c5b1e15d..2964f45e 100644
--- a/examples/tablemodelchart/tablewidget.cpp
+++ b/examples/tablemodelchart/tablewidget.cpp
@@ -4,19 +4,20 @@
#include <QStyledItemDelegate>
#include "qlineseries.h"
#include "qsplineseries.h"
+#include "qscatterseries.h"
#include "customtablemodel.h"
#include "qpieseries.h"
+#include <QPushButton>
+#include <QRadioButton>
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 feae5ca6..bb8515d8 100644
--- a/examples/tablemodelchart/tablewidget.h
+++ b/examples/tablemodelchart/tablewidget.h
@@ -3,9 +3,15 @@
#include <QtGui/QWidget>
#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 dec08812..87ef527f 100644
--- a/src/linechart/linechartitem.cpp
+++ b/src/linechart/linechartitem.cpp
@@ -31,7 +31,11 @@ QPainterPath LineChartItem::shape() const
void LineChartItem::setGeometry(QVector<QPointF>& points)
{
- if(points.size()==0) return;
+ if(points.size()==0)
+ {
+ XYChartItem::setGeometry(points);
+ return;
+ }
QList<QGraphicsItem*> items = m_items.childItems();
diff --git a/src/scatterseries/scatterchartitem.cpp b/src/scatterseries/scatterchartitem.cpp
index 5279af13..07d91ef6 100644
--- a/src/scatterseries/scatterchartitem.cpp
+++ b/src/scatterseries/scatterchartitem.cpp
@@ -92,7 +92,11 @@ void ScatterChartItem::markerSelected(Marker* marker)
void ScatterChartItem::setGeometry(QVector<QPointF>& 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 cc4d5bd0..06ee33ac 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<QPointF>& 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 40d0ed0b..52f9ae44 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 f8e48ccb..bc918db3 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 25dd43f2..9a46d0b1 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(index<m_series->count());
+ Q_ASSERT(index<m_series->count() + 1);
Q_ASSERT(index>=0);
- QPointF point = calculateGeometryPoint(index);
+// QPointF point = calculateGeometryPoint(index);
QVector<QPointF> points = m_points;
points.remove(index);
updatePoints(points);