##// END OF EJS Templates
Added support for adding and removing data with model. Updated the example
Marek Rosa -
r545:366c5163e81a
parent child
Show More
@@ -24,7 +24,7 int CustomTableModel::rowCount(const QModelIndex & parent) const
24 24
25 25 int CustomTableModel::columnCount(const QModelIndex & parent) const
26 26 {
27 return 2;
27 return 3;
28 28 }
29 29
30 30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
@@ -116,11 +116,29 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
116 116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
117 117 {
118 118 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
119 for (int i = 0; i < count; i++)
119 for (int i = row; i < row + count; i++)
120 120 {
121 m_points.append(QPointF());
122 m_labels.append("");
121 m_points.insert(row, QPointF());
122 m_labels.insert(row,(""));
123 123 }
124 124 endInsertRows();
125 125 return true;
126 126 }
127
128 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
129 {
130 if (row > this->rowCount() - 1)
131 return false;
132 if (row < 0)
133 row = 0;
134 if (row + count > rowCount())
135 return false;
136 beginRemoveRows(parent, row, row + count - 1);
137 for (int i = row; i < row + count; i++)
138 {
139 m_points.removeAt(row);
140 m_labels.removeAt(row);
141 }
142 endRemoveRows();
143 return true;
144 }
@@ -18,6 +18,7 public:
18 18 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
19 19 Qt::ItemFlags flags ( const QModelIndex & index ) const;
20 20 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21 22
22 23 //signals:
23 24
@@ -4,19 +4,20
4 4 #include <QStyledItemDelegate>
5 5 #include "qlineseries.h"
6 6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
7 8 #include "customtablemodel.h"
8 9 #include "qpieseries.h"
10 #include <QPushButton>
11 #include <QRadioButton>
9 12
10 13 TableWidget::TableWidget(QWidget *parent)
11 14 : QWidget(parent)
12 15 {
13
14
15 16 // create simple model for storing data
16 17 // user's table data model
17 CustomTableModel* model = new CustomTableModel;
18 QTableView* tableView = new QTableView;
19 tableView->setModel(model);
18 m_model = new CustomTableModel;
19 tableView = new QTableView;
20 tableView->setModel(m_model);
20 21 tableView->setMinimumSize(340, 480);
21 22 // tableView->setItemDelegate(new QStyledItemDelegate);
22 23 chartView = new QChartView;
@@ -24,30 +25,84 TableWidget::TableWidget(QWidget *parent)
24 25
25 26 // create
26 27 // QLineSeries* series = new QLineSeries;
27 QSplineSeries* series = new QSplineSeries;
28 series->setModel(model);
29 series->setModelMapping(0,1, Qt::Vertical);
30 // series->setModelMappingY(1);
31
32 // series->add(QPointF(150, 100));
33 // series->add(QPointF(200, 130));
34 // series->add(QPointF(250, 120));
35 // series->add(QPointF(300, 140));
36 // series->add(QPointF(350, 160));
28 // QSplineSeries* series = new QSplineSeries;
29 // QScatterSeries* series = new QScatterSeries;
30 // series->setModel(m_model);
31 // series->setModelMapping(0,1, Qt::Vertical);
37 32
38 33 // QPieSeries* pieSeries = new QPieSeries;
39 34 // pieSeries->setModel(model);
40 35 // pieSeries
41 36
42 chartView->addSeries(series);
37 // chartView->addSeries(series);
38
39 // add, remove data buttons
40 QPushButton* addRowButton = new QPushButton("Add row");
41 connect(addRowButton, SIGNAL(clicked()), this, SLOT(addRow()));
42
43 QPushButton* removeRowButton = new QPushButton("Remove row");
44 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
45
46 // buttons layout
47 QVBoxLayout* buttonsLayout = new QVBoxLayout;
48 buttonsLayout->addWidget(addRowButton);
49 buttonsLayout->addWidget(removeRowButton);
50 buttonsLayout->addStretch();
51
52 // chart type radio buttons
53 lineRadioButton = new QRadioButton("Line");
54 splineRadioButton = new QRadioButton("Spline");
55 scatterRadioButton = new QRadioButton("Scatter");
56
57 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
58 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
59 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
60 lineRadioButton->setChecked(true);
61
62 // radio buttons layout
63 QHBoxLayout* radioLayout = new QHBoxLayout;
64 radioLayout->addWidget(lineRadioButton);
65 radioLayout->addWidget(splineRadioButton);
66 radioLayout->addWidget(scatterRadioButton);
43 67
44 68 // create main layout
45 69 QGridLayout* mainLayout = new QGridLayout;
70 mainLayout->addLayout(buttonsLayout, 0, 1);
71 mainLayout->addLayout(radioLayout, 0, 2);
46 72 mainLayout->addWidget(tableView, 1, 1);
47 73 mainLayout->addWidget(chartView, 1, 2);
48 74 setLayout(mainLayout);
49 75 }
50 76
77 void TableWidget::addRow()
78 {
79 // m_model->insertRow(m_model->rowCount());
80 m_model->insertRow(tableView->currentIndex().row() + 1);
81
82 }
83
84 void TableWidget::removeRow()
85 {
86 // m_model->removeRow(m_model->rowCount() - 1);
87 m_model->removeRow(tableView->currentIndex().row());
88 }
89
90 void TableWidget::updateChartType()
91 {
92 chartView->removeAllSeries();
93
94 if (lineRadioButton->isChecked())
95 series = new QLineSeries;
96 else if (splineRadioButton->isChecked())
97 series = new QSplineSeries;
98 else
99 series = new QScatterSeries;
100
101 series->setModel(m_model);
102 series->setModelMapping(0,1, Qt::Vertical);
103 chartView->addSeries(series);
104 }
105
51 106 TableWidget::~TableWidget()
52 107 {
53 108
@@ -3,9 +3,15
3 3
4 4 #include <QtGui/QWidget>
5 5 #include "qchartview.h"
6 #include "qxyseries.h"
6 7
7 8 QTCOMMERCIALCHART_USE_NAMESPACE
8 9
10 class CustomTableModel;
11 class QTableView;
12 class QRadioButton;
13 //class QSeries;
14
9 15 class TableWidget : public QWidget
10 16 {
11 17 Q_OBJECT
@@ -15,9 +21,19 public:
15 21 ~TableWidget();
16 22
17 23
24 public slots:
25 void addRow();
26 void removeRow();
27 void updateChartType();
18 28
19 29 private:
20 30 QChartView* chartView;
31 QXYSeries* series;
32 CustomTableModel* m_model;
33 QTableView* tableView;
34 QRadioButton* lineRadioButton;
35 QRadioButton* splineRadioButton;
36 QRadioButton* scatterRadioButton;
21 37 };
22 38
23 39 #endif // TABLEWIDGET_H
@@ -31,7 +31,11 QPainterPath LineChartItem::shape() const
31 31
32 32 void LineChartItem::setGeometry(QVector<QPointF>& points)
33 33 {
34 if(points.size()==0) return;
34 if(points.size()==0)
35 {
36 XYChartItem::setGeometry(points);
37 return;
38 }
35 39
36 40 QList<QGraphicsItem*> items = m_items.childItems();
37 41
@@ -92,7 +92,11 void ScatterChartItem::markerSelected(Marker* marker)
92 92
93 93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
94 94 {
95 if(points.size()==0) return;
95 if(points.size()==0)
96 {
97 XYChartItem::setGeometry(points);
98 return;
99 }
96 100
97 101 int diff = XYChartItem::points().size() - points.size();
98 102
@@ -31,7 +31,11 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
31 31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
32 32 {
33 33
34 if(points.size()==0) return;
34 if(points.size()==0)
35 {
36 XYChartItem::setGeometry(points);
37 return;
38 }
35 39
36 40 QPainterPath splinePath;
37 41 const QPointF& point = points.at(0);
@@ -121,9 +121,10 void QXYSeries::remove(qreal x,qreal y)
121 121 }while(index !=-1 && m_y.at(index)!=y);
122 122
123 123 if(index==-1) return;
124 emit pointRemoved(index);
124
125 125 m_x.remove(index);
126 126 m_y.remove(index);
127 emit pointRemoved(index);
127 128 }
128 129
129 130 /*!
@@ -265,12 +266,23 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
265 266 emit pointReplaced(topLeft.row());
266 267 }
267 268
269 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
270 {
271 emit pointAdded(start);
272 }
273
274 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
275 {
276 emit pointRemoved(start);
277 }
278
268 279 bool QXYSeries::setModel(QAbstractItemModel* model) {
269 280 m_model = model;
270 281 // for (int i = 0; i < m_model->rowCount(); i++)
271 282 // emit pointAdded(i);
272 283 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
284 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
285 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
274 286 }
275 287
276 288 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
@@ -42,11 +42,13 public:
42 42 bool setModel(QAbstractItemModel* model);
43 43 QAbstractItemModel* model() {return m_model;}
44 44
45 void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
45 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
46 46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
47 47
48 48 private slots:
49 49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
50 52
51 53 signals:
52 54 void updated();
@@ -99,9 +99,9 void XYChartItem::handlePointAdded(int index)
99 99 }
100 100 void XYChartItem::handlePointRemoved(int index)
101 101 {
102 Q_ASSERT(index<m_series->count());
102 Q_ASSERT(index<m_series->count() + 1);
103 103 Q_ASSERT(index>=0);
104 QPointF point = calculateGeometryPoint(index);
104 // QPointF point = calculateGeometryPoint(index);
105 105 QVector<QPointF> points = m_points;
106 106 points.remove(index);
107 107 updatePoints(points);
General Comments 0
You need to be logged in to leave comments. Login now