##// END OF EJS Templates
Table model data example
Marek Rosa -
r519:987444b37af4
parent child
Show More
@@ -0,0 +1,123
1 #include "customtablemodel.h"
2
3 CustomTableModel::CustomTableModel(QObject *parent) :
4 QAbstractTableModel(parent)
5 {
6 m_points.append(QPointF(10, 50));
7 m_labels.append("Apples");
8 m_points.append(QPointF(60, 70));
9 m_labels.append("Oranges");
10 m_points.append(QPointF(110, 50));
11 m_labels.append("Bananas");
12 m_points.append(QPointF(140, 40));
13 m_labels.append("Lemons");
14 m_points.append(QPointF(200, 150));
15 m_labels.append("Plums");
16 }
17
18 int CustomTableModel::rowCount(const QModelIndex & parent) const
19 {
20 return m_points.count();
21 }
22
23 int CustomTableModel::columnCount(const QModelIndex & parent) const
24 {
25 return 3;
26 }
27
28 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
29 {
30 if (role != Qt::DisplayRole)
31 return QVariant();
32
33 if (orientation == Qt::Horizontal)
34 {
35 switch(section)
36 {
37 case 0:
38 return "x";
39 case 1:
40 return "y";
41 case 2:
42 return "Fruit";
43 default: "What?";
44 }
45 }
46 else
47 return QString("%1").arg(section + 1);
48 }
49
50 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
51 {
52 if (role == Qt::DisplayRole)
53 {
54 switch(index.column())
55 {
56 case 0:
57 return m_points[index.row()].x();
58 case 1:
59 return m_points[index.row()].y();
60 case 2:
61 return m_labels[index.row()];
62 default:
63 return QVariant();
64 }
65 }
66 else if (role == Qt::EditRole)
67 {
68 switch(index.column())
69 {
70 case 0:
71 return m_points[index.row()].x();
72 case 1:
73 return m_points[index.row()].y();
74 case 2:
75 return m_labels[index.row()];
76 default:
77 return QVariant();
78 }
79 }
80 }
81
82 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
83 {
84 if (index.isValid() && role == Qt::EditRole)
85 {
86 switch(index.column())
87 {
88 case 0:
89 m_points[index.row()].setX(value.toDouble());
90 break;
91 case 1:
92 m_points[index.row()].setY(value.toDouble());
93 break;
94 case 2:
95 m_labels.replace(index.row(), value.toString());
96 break;
97 default:
98 return false;
99 }
100 emit dataChanged(index, index);
101 return true;
102 }
103 return false;
104 }
105
106 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
107 {
108 if (!index.isValid())
109 return Qt::ItemIsEnabled;
110 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
111 }
112
113 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
114 {
115 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
116 for (int i = 0; i < count; i++)
117 {
118 m_points.append(QPointF());
119 m_labels.append("");
120 }
121 endInsertRows();
122 return true;
123 }
@@ -0,0 +1,32
1 #ifndef XYPOINTSMODEL_H
2 #define XYPOINTSMODEL_H
3
4 #include <QAbstractTableModel>
5 #include <QPointF>
6 #include <QStringList>
7
8 class CustomTableModel : public QAbstractTableModel
9 {
10 Q_OBJECT
11 public:
12 explicit CustomTableModel(QObject *parent = 0);
13
14 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
15 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
16 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
17 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
18 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
19 Qt::ItemFlags flags ( const QModelIndex & index ) const;
20 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21
22 //signals:
23
24 //public slots:
25 private:
26 QList<QPointF> m_points;
27 QStringList m_labels;
28
29
30 };
31
32 #endif // XYPOINTSMODEL_H
@@ -0,0 +1,11
1 #include <QtGui/QApplication>
2 #include "tablewidget.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 TableWidget w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,22
1 #-------------------------------------------------
2 #
3 # Project created by QtCreator 2012-03-08T14:30:24
4 #
5 #-------------------------------------------------
6
7 !include( ../examples.pri ) {
8 error( "Couldn't find the examples.pri file!" )
9 }
10
11 QT += core gui
12
13 TARGET = tablemodelchart
14 TEMPLATE = app
15
16
17 SOURCES += main.cpp\
18 tablewidget.cpp \
19 customtablemodel.cpp
20
21 HEADERS += tablewidget.h \
22 customtablemodel.h
@@ -0,0 +1,54
1 #include "tablewidget.h"
2 #include <QGridLayout>
3 #include <QTableView>
4 #include <QStyledItemDelegate>
5 #include "qlineseries.h"
6 #include "qsplineseries.h"
7 #include "customtablemodel.h"
8 #include "qpieseries.h"
9
10 TableWidget::TableWidget(QWidget *parent)
11 : QWidget(parent)
12 {
13
14
15 // create simple model for storing data
16 // user's table data model
17 CustomTableModel* model = new CustomTableModel;
18 QTableView* tableView = new QTableView;
19 tableView->setModel(model);
20 tableView->setMinimumSize(340, 480);
21 // tableView->setItemDelegate(new QStyledItemDelegate);
22 chartView = new QChartView;
23 chartView->setMinimumSize(640, 480);
24
25 // create
26 // QLineSeries* series = new QLineSeries;
27 QSplineSeries* series = new QSplineSeries;
28 series->setModel(model);
29 series->setModelMappingX(0);
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));
37
38 // QPieSeries* pieSeries = new QPieSeries;
39 // pieSeries->setModel(model);
40 // pieSeries
41
42 chartView->addSeries(series);
43
44 // create main layout
45 QGridLayout* mainLayout = new QGridLayout;
46 mainLayout->addWidget(tableView, 1, 1);
47 mainLayout->addWidget(chartView, 1, 2);
48 setLayout(mainLayout);
49 }
50
51 TableWidget::~TableWidget()
52 {
53
54 }
@@ -0,0 +1,23
1 #ifndef TABLEWIDGET_H
2 #define TABLEWIDGET_H
3
4 #include <QtGui/QWidget>
5 #include "qchartview.h"
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 class TableWidget : public QWidget
10 {
11 Q_OBJECT
12
13 public:
14 TableWidget(QWidget *parent = 0);
15 ~TableWidget();
16
17
18
19 private:
20 QChartView* chartView;
21 };
22
23 #endif // TABLEWIDGET_H
@@ -18,6 +18,5 SUBDIRS += linechart \
18 splinechart \
18 splinechart \
19 areachart \
19 areachart \
20 stackedbarchartdrilldown \
20 stackedbarchartdrilldown \
21 customcolors
21 customcolors \
22
22 tablemodelchart
23
@@ -43,15 +43,15 void QSplineSeries::calculateControlPoints()
43 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
43 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
44 // CPOL License
44 // CPOL License
45
45
46 int n = m_x.size() - 1;
46 int n = count() - 1;
47 if (n == 1)
47 if (n == 1)
48 { // Special case: Bezier curve should be a straight line.
48 { // Special case: Bezier curve should be a straight line.
49 // firstControlPoints = new Point[1];
49 // firstControlPoints = new Point[1];
50 // 3P1 = 2P0 + P3
50 // 3P1 = 2P0 + P3
51 m_controlPoints.append(QPointF((2 * m_x[0] + m_x[1]) / 3, (2 * m_y[0] + m_y[1]) / 3));
51 m_controlPoints.append(QPointF((2 * x(0) + x(1)) / 3, (2 * y(0) + y(1)) / 3));
52
52
53 // P2 = 2P1 P0
53 // P2 = 2P1 P0
54 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - m_x[0], 2 * m_controlPoints[0].y() - m_y[0]));
54 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - x(0), 2 * m_controlPoints[0].y() - y(0)));
55 return;
55 return;
56 }
56 }
57
57
@@ -69,35 +69,35 void QSplineSeries::calculateControlPoints()
69 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
69 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
70 //
70 //
71 QList<qreal> rhs;
71 QList<qreal> rhs;
72 rhs.append(m_x[0] + 2 * m_x[1]);
72 rhs.append(x(0) + 2 * x(1));
73
73
74 // Set right hand side X values
74 // Set right hand side X values
75 for (int i = 1; i < n - 1; ++i)
75 for (int i = 1; i < n - 1; ++i)
76 rhs.append(4 * m_x[i] + 2 * m_x[i + 1]);
76 rhs.append(4 * x(i) + 2 * x(i + 1));
77
77
78 rhs.append((8 * m_x[n - 1] + m_x[n]) / 2.0);
78 rhs.append((8 * x(n - 1) + x(n)) / 2.0);
79 // Get first control points X-values
79 // Get first control points X-values
80 QList<qreal> x = getFirstControlPoints(rhs);
80 QList<qreal> xControl = getFirstControlPoints(rhs);
81 rhs[0] = m_y[0] + 2 * m_y[1];
81 rhs[0] = y(0) + 2 * y(1);
82
82
83 // Set right hand side Y values
83 // Set right hand side Y values
84 for (int i = 1; i < n - 1; ++i)
84 for (int i = 1; i < n - 1; ++i)
85 rhs[i] = 4 * m_y[i] + 2 * m_y[i + 1];
85 rhs[i] = 4 * y(i) + 2 * y(i + 1);
86
86
87 rhs[n - 1] = (8 * m_y[n - 1] + m_y[n]) / 2.0;
87 rhs[n - 1] = (8 * y(n - 1) + y(n)) / 2.0;
88 // Get first control points Y-values
88 // Get first control points Y-values
89 QList<qreal> y = getFirstControlPoints(rhs);
89 QList<qreal> yControl = getFirstControlPoints(rhs);
90
90
91 // Fill output arrays.
91 // Fill output arrays.
92 for (int i = 0; i < n; ++i)
92 for (int i = 0; i < n; ++i)
93 {
93 {
94 // First control point
94 // First control point
95 m_controlPoints.append(QPointF(x[i], y[i]));
95 m_controlPoints.append(QPointF(xControl[i], yControl[i]));
96 // Second control point
96 // Second control point
97 if (i < n - 1)
97 if (i < n - 1)
98 m_controlPoints.append(QPointF(2 * m_x[i + 1] - x[i + 1], 2 * m_y[i + 1] - y[i + 1]));
98 m_controlPoints.append(QPointF(2 * x(i + 1) - xControl[i + 1], 2 * y(i + 1) - yControl[i + 1]));
99 else
99 else
100 m_controlPoints.append(QPointF((m_x[n] + x[n - 1]) / 2, (m_y[n] + y[n - 1]) / 2));
100 m_controlPoints.append(QPointF((x(n) + xControl[n - 1]) / 2, (y(n) + yControl[n - 1]) / 2));
101 }
101 }
102 }
102 }
103
103
@@ -130,13 +130,19 QList<qreal> QSplineSeries::getFirstControlPoints(QList<qreal> rhs)
130 */
130 */
131 void QSplineSeries::updateControlPoints()
131 void QSplineSeries::updateControlPoints()
132 {
132 {
133 if(m_x.size() > 1)
133 if(count() > 1)
134 {
134 {
135 m_controlPoints.clear();
135 m_controlPoints.clear();
136 calculateControlPoints();
136 calculateControlPoints();
137 }
137 }
138 }
138 }
139
139
140 bool QSplineSeries::setModel(QAbstractItemModel* model)
141 {
142 QXYSeries::setModel(model);
143 calculateControlPoints();
144 }
145
140 #include "moc_qsplineseries.cpp"
146 #include "moc_qsplineseries.cpp"
141
147
142 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -19,6 +19,7 public:
19
19
20 // int count() const { return m_x.size(); }
20 // int count() const { return m_x.size(); }
21 QPointF controlPoint(int index) const {return m_controlPoints[index];}
21 QPointF controlPoint(int index) const {return m_controlPoints[index];}
22 bool setModel(QAbstractItemModel* model);
22
23
23 // TODO: allow the user to set custom control points
24 // TODO: allow the user to set custom control points
24 // void setCustomControlPoints(QList<QPointsF> controlPoints);
25 // void setCustomControlPoints(QList<QPointsF> controlPoints);
@@ -45,6 +45,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45 */
45 */
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
47 {
47 {
48 m_model = 0;
49 m_mapX = 0;
50 m_mapY = 1;
48 }
51 }
49 /*!
52 /*!
50 Destroys the object. Series added to QChartView or QChart instances are owned by those,
53 Destroys the object. Series added to QChartView or QChart instances are owned by those,
@@ -102,7 +105,7 void QXYSeries::replace(qreal x,qreal y)
102 */
105 */
103 void QXYSeries::replace(const QPointF& point)
106 void QXYSeries::replace(const QPointF& point)
104 {
107 {
105 replace(point.x(),point.y());
108 replace(point.x(),point.y());
106 }
109 }
107
110
108 /*!
111 /*!
@@ -138,7 +141,10 void QXYSeries::removeAll()
138 */
141 */
139 qreal QXYSeries::x(int pos) const
142 qreal QXYSeries::x(int pos) const
140 {
143 {
141 return m_x.at(pos);
144 if (m_model)
145 return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble();
146 else
147 return m_x.at(pos);
142 }
148 }
143
149
144 /*!
150 /*!
@@ -146,7 +152,10 qreal QXYSeries::x(int pos) const
146 */
152 */
147 qreal QXYSeries::y(int pos) const
153 qreal QXYSeries::y(int pos) const
148 {
154 {
149 return m_y.at(pos);
155 if (m_model)
156 return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble();
157 else
158 return m_y.at(pos);
150 }
159 }
151
160
152 /*!
161 /*!
@@ -154,10 +163,13 qreal QXYSeries::y(int pos) const
154 */
163 */
155 int QXYSeries::count() const
164 int QXYSeries::count() const
156 {
165 {
157 Q_ASSERT(m_x.size() == m_y.size());
166 Q_ASSERT(m_x.size() == m_y.size());
158
159 return m_x.size();
160
167
168 // int k = m_model->rowCount();
169 if (m_model)
170 return m_model->rowCount();
171 else
172 return m_x.size();
161 }
173 }
162
174
163 /*!
175 /*!
@@ -224,6 +236,19 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
224 }
236 }
225
237
226
238
239 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
240 {
241 emit pointReplaced(topLeft.row());
242 }
243
244 bool QXYSeries::setModel(QAbstractItemModel* model) {
245 m_model = model;
246 for (int i = 0; i < m_model->rowCount(); i++)
247 emit pointAdded(i);
248 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
249 // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
250 }
251
227 #include "moc_qxyseries.cpp"
252 #include "moc_qxyseries.cpp"
228
253
229 QTCOMMERCIALCHART_END_NAMESPACE
254 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,6 +39,15 public:
39 void setBrush(const QBrush& pen);
39 void setBrush(const QBrush& pen);
40 QBrush brush() const {return m_brush;}
40 QBrush brush() const {return m_brush;}
41
41
42 bool setModel(QAbstractItemModel* model);
43 QAbstractItemModel* model() {return m_model;}
44
45 void setModelMappingX(int modelColumn) {m_mapX = modelColumn;}
46 void setModelMappingY(int modelColumn) {m_mapY = modelColumn;}
47
48 private slots:
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50
42 signals:
51 signals:
43 void updated();
52 void updated();
44 void pointReplaced(int index);
53 void pointReplaced(int index);
@@ -48,8 +57,13 signals:
48 protected:
57 protected:
49 QVector<qreal> m_x;
58 QVector<qreal> m_x;
50 QVector<qreal> m_y;
59 QVector<qreal> m_y;
60
51 QPen m_pen;
61 QPen m_pen;
52 QBrush m_brush;
62 QBrush m_brush;
63
64 QAbstractItemModel* m_model;
65 int m_mapX;
66 int m_mapY;
53 };
67 };
54
68
55 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -101,7 +101,7 void XYChartItem::handlePointReplaced(int index)
101 QPointF point = calculateGeometryPoint(index);
101 QPointF point = calculateGeometryPoint(index);
102 QVector<QPointF> points = m_points;
102 QVector<QPointF> points = m_points;
103 m_points.replace(index,point);
103 m_points.replace(index,point);
104 updatePoint(points);
104 updatePoint(m_points);
105 update();
105 update();
106 }
106 }
107
107
@@ -125,7 +125,7 void XYChartItem::handleGeometryChanged(const QRectF& rect)
125 m_clipRect=rect.translated(-rect.topLeft());
125 m_clipRect=rect.translated(-rect.topLeft());
126 setPos(rect.topLeft());
126 setPos(rect.topLeft());
127
127
128 if(isEmpty()) return;
128 if(isEmpty()) return;
129 QVector<QPointF> points = calculateGeometryPoints();
129 QVector<QPointF> points = calculateGeometryPoints();
130 updatePoints(points);
130 updatePoints(points);
131 update();
131 update();
General Comments 0
You need to be logged in to leave comments. Login now