##// END OF EJS Templates
Some more work on mapping with limits
Marek Rosa -
r735:8b3d19ded350
parent child
Show More
@@ -1,214 +1,235
1 #include "customtablemodel.h"
1 #include "customtablemodel.h"
2 #include <QVector>
2 #include <QVector>
3 #include <QTime>
3 #include <QTime>
4
4
5 CustomTableModel::CustomTableModel(QObject *parent) :
5 CustomTableModel::CustomTableModel(QObject *parent) :
6 QAbstractTableModel(parent)
6 QAbstractTableModel(parent)
7 {
7 {
8 // m_points.append(QPointF(10, 50));
8 // m_points.append(QPointF(10, 50));
9 // m_labels.append("Apples");
9 // m_labels.append("Apples");
10 // m_points.append(QPointF(60, 70));
10 // m_points.append(QPointF(60, 70));
11 // m_labels.append("Oranges");
11 // m_labels.append("Oranges");
12 // m_points.append(QPointF(110, 50));
12 // m_points.append(QPointF(110, 50));
13 // m_labels.append("Bananas");
13 // m_labels.append("Bananas");
14 // m_points.append(QPointF(140, 40));
14 // m_points.append(QPointF(140, 40));
15 // m_labels.append("Lemons");
15 // m_labels.append("Lemons");
16 // m_points.append(QPointF(200, 150));
16 // m_points.append(QPointF(200, 150));
17 // m_labels.append("Plums");
17 // m_labels.append("Plums");
18 // m_points.append(QPointF(225, 75));
18 // m_points.append(QPointF(225, 75));
19 // m_labels.append("Pearls");
19 // m_labels.append("Pearls");
20
20
21 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
21 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
22
22
23 // m_data
23 // m_data
24 for (int i = 0; i < 6; i++)
24 for (int i = 0; i < 6; i++)
25 {
25 {
26 QVector<qreal>* dataVec = new QVector<qreal>(6);
26 QVector<qreal>* dataVec = new QVector<qreal>(6);
27 QVector<QColor>* colorVec = new QVector<QColor>(6);
27 for (int k = 0; k < dataVec->size(); k++)
28 for (int k = 0; k < dataVec->size(); k++)
29 {
28 if (k%2 == 0)
30 if (k%2 == 0)
29 dataVec->replace(k, i * 50 + qrand()%20);
31 dataVec->replace(k, i * 50 + qrand()%20);
30 else
32 else
31 dataVec->replace(k, qrand()%100);
33 dataVec->replace(k, qrand()%100);
34 colorVec->replace(k, QColor(Qt::white));
35 }
32 m_data.append(dataVec);
36 m_data.append(dataVec);
33 m_labels.append(QString("Row: %1").arg((i + 1)));
37 m_labels.append(QString("Row: %1").arg((i + 1)));
38 m_rowsColors.append(colorVec);
34 }
39 }
35 }
40 }
36
41
37 int CustomTableModel::rowCount(const QModelIndex & parent) const
42 int CustomTableModel::rowCount(const QModelIndex & parent) const
38 {
43 {
39 Q_UNUSED(parent)
44 Q_UNUSED(parent)
40 // return m_points.count();
45 // return m_points.count();
41 return m_data.count();
46 return m_data.count();
42 }
47 }
43
48
44 int CustomTableModel::columnCount(const QModelIndex & parent) const
49 int CustomTableModel::columnCount(const QModelIndex & parent) const
45 {
50 {
46 Q_UNUSED(parent)
51 Q_UNUSED(parent)
47 // return 3;
52 // return 3;
48 return 6;
53 return 6;
49 }
54 }
50
55
51 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
56 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
52 {
57 {
53 if (role != Qt::DisplayRole)
58 if (role != Qt::DisplayRole)
54 return QVariant();
59 return QVariant();
55
60
56 if (orientation == Qt::Horizontal)
61 if (orientation == Qt::Horizontal)
57 {
62 {
58 switch(section)
63 switch(section)
59 {
64 {
60 // case 0:
65 // case 0:
61 // return "x";
66 // return "x";
62 // case 1:
67 // case 1:
63 // return "y";
68 // return "y";
64 // case 2:
69 // case 2:
65 case 6:
70 case 6:
66 return "Fruit";
71 return "Fruit";
67 default:
72 default:
68 if (section%2 == 0)
73 if (section%2 == 0)
69 return "x";
74 return "x";
70 else
75 else
71 return "y";
76 return "y";
72 // return "What?";
77 // return "What?";
73 }
78 }
74 }
79 }
75 else
80 else
76 return QString("%1").arg(section + 1);
81 return QString("%1").arg(section + 1);
77 }
82 }
78
83
79 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
84 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
80 {
85 {
81 if (role == Qt::DisplayRole)
86 if (role == Qt::DisplayRole)
82 {
87 {
83 switch(index.column())
88 switch(index.column())
84 {
89 {
85 // case 0:
90 // case 0:
86 // return m_points[index.row()].x();
91 // return m_points[index.row()].x();
87 // case 1:
92 // case 1:
88 // return m_points[index.row()].y();
93 // return m_points[index.row()].y();
89 // case 2:
94 // case 2:
90 case 6:
95 case 6:
91 return m_labels[index.row()];
96 return m_labels[index.row()];
92 default:
97 default:
93 return m_data[index.row()]->at(index.column());
98 return m_data[index.row()]->at(index.column());
94 break;
99 break;
95 }
100 }
96 }
101 }
97 else if (role == Qt::EditRole)
102 else if (role == Qt::EditRole)
98 {
103 {
99 switch(index.column())
104 switch(index.column())
100 {
105 {
101 // case 0:
106 // case 0:
102 // return m_points[index.row()].x();
107 // return m_points[index.row()].x();
103 // case 1:
108 // case 1:
104 // return m_points[index.row()].y();
109 // return m_points[index.row()].y();
105 // case 2:
110 // case 2:
106 case 6:
111 case 6:
107 return m_labels[index.row()];
112 return m_labels[index.row()];
108 default:
113 default:
109 return m_data[index.row()]->at(index.column());
114 return m_data[index.row()]->at(index.column());
110 break;
115 break;
111 }
116 }
112 }
117 }
118 else if (role == Qt::BackgroundRole)
119 {
120 return m_rowsColors[index.row()]->at(index.column());
121 }
113 return QVariant();
122 return QVariant();
114 }
123 }
115
124
116 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
125 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
117 {
126 {
118 if (index.isValid() && role == Qt::EditRole)
127 if (index.isValid() && role == Qt::EditRole)
119 {
128 {
120 switch(index.column())
129 switch(index.column())
121 {
130 {
122 // case 0:
131 // case 0:
123 // m_points[index.row()].setX(value.toDouble());
132 // m_points[index.row()].setX(value.toDouble());
124 // break;
133 // break;
125 // case 1:
134 // case 1:
126 // m_points[index.row()].setY(value.toDouble());
135 // m_points[index.row()].setY(value.toDouble());
127 // break;
136 // break;
128 // case 2:
137 // case 2:
129 case 6:
138 case 6:
130 m_labels.replace(index.row(), value.toString());
139 m_labels.replace(index.row(), value.toString());
131 break;
140 break;
132 default:
141 default:
133 m_data[index.row()]->replace(index.column(), value.toDouble());
142 m_data[index.row()]->replace(index.column(), value.toDouble());
134 break;
143 break;
135 // return false;
144 // return false;
136 }
145 }
137 emit dataChanged(index, index);
146 emit dataChanged(index, index);
138 return true;
147 return true;
139 }
148 }
149 else if (role == Qt::BackgroundRole)
150 {
151 m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>());
152 return true;
153 }
140 return false;
154 return false;
141 }
155 }
142
156
143 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
157 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
144 {
158 {
145 // if (!index.isValid())
159 // if (!index.isValid())
146 // return Qt::ItemIsEnabled;
160 // return Qt::ItemIsEnabled;
147 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
161 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
148 }
162 }
149
163
150 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
164 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
151 {
165 {
152 Q_UNUSED(parent)
166 Q_UNUSED(parent)
153
167
154 if (row < 0)
168 if (row < 0)
155 row = 0;
169 row = 0;
156 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
170 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
157 for (int i = row; i < row + count; i++)
171 for (int i = row; i < row + count; i++)
158 {
172 {
159 // m_points.insert(row, QPointF(10,20));
173 // m_points.insert(row, QPointF(10,20));
160 QVector<qreal>* dataVec = new QVector<qreal>(6);
174 QVector<qreal>* dataVec = new QVector<qreal>(6);
175 QVector<QColor>* colorVec = new QVector<QColor>(6);
161 for (int k = 0; k < dataVec->size(); k++)
176 for (int k = 0; k < dataVec->size(); k++)
177 {
162 if (k%2 == 0)
178 if (k%2 == 0)
163 // dataVec->replace(k, i * 50 + qrand()%20);
179 // dataVec->replace(k, i * 50 + qrand()%20);
164 {
180 {
165 int difference = 0;
181 int difference = 0;
166 if (row < m_data.size())
182 if (i < m_data.size())
167 {
183 {
168 if (row - 1 >= 0)
184 if (i - 1 >= 0)
169 {
185 {
170 difference = (int)(qAbs(m_data[row]->at(k) - m_data[row - 1]->at(k)));
186 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
171 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%qMax(1, difference));
187 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
172 }
188 }
173 else
189 else
174 dataVec->replace(k, qrand()%40 + 10);
190 dataVec->replace(k, qrand()%40 + 10);
175 }
191 }
176 else
192 else
177 if (row - 1 >= 0)
193 {
194 if (i - 1 >= 0)
178 {
195 {
179 dataVec->replace(k, m_data[row - 1]->at(k) + qrand()%40 + 10);
196 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
180 }
197 }
181 else
198 else
182 {
199 {
183 dataVec->replace(k, qrand()%40 + 10);
200 dataVec->replace(k, qrand()%40 + 10);
184 }
201 }
202 }
185 }
203 }
186 else
204 else
187 dataVec->replace(k, qrand()%100);
205 dataVec->replace(k, qrand()%100);
188 m_data.insert(row, dataVec);
206 colorVec->replace(k, QColor(Qt::white));
189 m_labels.insert(row,(QString("Row: %1").arg(row + 1)));
207 }
208 m_data.insert(i, dataVec);
209 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
210 m_rowsColors.insert(i, colorVec);
190 }
211 }
191 endInsertRows();
212 endInsertRows();
192 return true;
213 return true;
193 }
214 }
194
215
195 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
216 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
196 {
217 {
197 if (row > this->rowCount() - 1)
218 if (row > this->rowCount() - 1)
198 return false;
219 return false;
199 if (row < 0)
220 if (row < 0)
200 row = 0;
221 row = 0;
201 if (row + count > rowCount())
222 if (row + count > rowCount())
202 return false;
223 return false;
203 beginRemoveRows(parent, row, row + count - 1);
224 beginRemoveRows(parent, row, row + count - 1);
204 for (int i = row; i < row + count; i++)
225 for (int i = row; i < row + count; i++)
205 {
226 {
206 // m_points.removeAt(row);
227 // m_points.removeAt(row);
207 QVector<qreal>* item = m_data.at(row);
228 QVector<qreal>* item = m_data.at(row);
208 m_data.removeAt(row);
229 m_data.removeAt(row);
209 delete item;
230 delete item;
210 m_labels.removeAt(row);
231 m_labels.removeAt(row);
211 }
232 }
212 endRemoveRows();
233 endRemoveRows();
213 return true;
234 return true;
214 }
235 }
@@ -1,34 +1,36
1 #ifndef XYPOINTSMODEL_H
1 #ifndef XYPOINTSMODEL_H
2 #define XYPOINTSMODEL_H
2 #define XYPOINTSMODEL_H
3
3
4 #include <QAbstractTableModel>
4 #include <QAbstractTableModel>
5 #include <QPointF>
5 #include <QPointF>
6 #include <QStringList>
6 #include <QStringList>
7 #include <QColor>
7
8
8 class CustomTableModel : public QAbstractTableModel
9 class CustomTableModel : public QAbstractTableModel
9 {
10 {
10 Q_OBJECT
11 Q_OBJECT
11 public:
12 public:
12 explicit CustomTableModel(QObject *parent = 0);
13 explicit CustomTableModel(QObject *parent = 0);
13
14
14 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
15 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
15 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
16 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
16 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
17 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
17 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
18 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
18 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
19 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
19 Qt::ItemFlags flags ( const QModelIndex & index ) const;
20 Qt::ItemFlags flags ( const QModelIndex & index ) const;
20 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
22 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
22
23
23 //signals:
24 //signals:
24
25
25 //public slots:
26 //public slots:
26 private:
27 private:
27 QList<QVector<qreal>* > m_data;
28 QList<QVector<qreal>* > m_data;
29 QList<QVector<QColor>* > m_rowsColors;
28 QList<QPointF> m_points;
30 QList<QPointF> m_points;
29 QStringList m_labels;
31 QStringList m_labels;
30
32
31
33
32 };
34 };
33
35
34 #endif // XYPOINTSMODEL_H
36 #endif // XYPOINTSMODEL_H
@@ -1,251 +1,287
1 #include "tablewidget.h"
1 #include "tablewidget.h"
2 #include <QGridLayout>
2 #include <QGridLayout>
3 #include <QTableView>
3 #include <QTableView>
4 #include <QStyledItemDelegate>
4 #include <QStyledItemDelegate>
5 #include "qlineseries.h"
5 #include "qlineseries.h"
6 #include "qsplineseries.h"
6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
7 #include "qscatterseries.h"
8 #include "customtablemodel.h"
8 #include "customtablemodel.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include "qareaseries.h"
10 #include "qareaseries.h"
11 #include "qbarseries.h"
11 #include "qbarseries.h"
12 #include <QPushButton>
12 #include <QPushButton>
13 #include <QRadioButton>
13 #include <QRadioButton>
14 #include <QSpinBox>
14 #include <QTime>
15 #include <QTime>
15
16
16 TableWidget::TableWidget(QWidget *parent)
17 TableWidget::TableWidget(QWidget *parent)
17 : QWidget(parent)
18 : QWidget(parent)
18 {
19 {
19 setGeometry(100, 100, 1000, 600);
20 setGeometry(100, 100, 1000, 600);
20 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
21 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
21 // create simple model for storing data
22 // create simple model for storing data
22 // user's table data model
23 // user's table data model
23 m_model = new CustomTableModel;
24 m_model = new CustomTableModel;
24 tableView = new QTableView;
25 tableView = new QTableView;
25 tableView->setModel(m_model);
26 tableView->setModel(m_model);
26 tableView->setMinimumHeight(240);
27 tableView->setMinimumHeight(240);
27 // tableView->setMinimumSize(340, 480);
28 // tableView->setMinimumSize(340, 480);
28 // tableView->setItemDelegate(new QStyledItemDelegate);
29 // tableView->setItemDelegate(new QStyledItemDelegate);
29 chartView = new QChartView(this);
30 chartView = new QChartView(this);
30 chartView->setRenderHint(QPainter::Antialiasing);
31 chartView->setRenderHint(QPainter::Antialiasing);
31 chartView->setMinimumSize(640, 480);
32 chartView->setMinimumSize(640, 480);
32
33
33 // create
34 // create
34 // QLineSeries* series = new QLineSeries;
35 // QLineSeries* series = new QLineSeries;
35 // QSplineSeries* series = new QSplineSeries;
36 // QSplineSeries* series = new QSplineSeries;
36 // QScatterSeries* series = new QScatterSeries;
37 // QScatterSeries* series = new QScatterSeries;
37 // series->setModel(m_model);
38 // series->setModel(m_model);
38 // series->setModelMapping(0,1, Qt::Vertical);
39 // series->setModelMapping(0,1, Qt::Vertical);
39
40
40 // QPieSeries* pieSeries = new QPieSeries;
41 // QPieSeries* pieSeries = new QPieSeries;
41 // pieSeries->setModel(model);
42 // pieSeries->setModel(model);
42 // pieSeries
43 // pieSeries
43
44
44 // chartView->addSeries(series);
45 // chartView->addSeries(series);
45
46
46 // add, remove data buttons
47 // add, remove data buttons
47 QPushButton* addRowAboveButton = new QPushButton("Add row above");
48 QPushButton* addRowAboveButton = new QPushButton("Add row above");
48 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
49 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
49
50
50 QPushButton* addRowBelowButton = new QPushButton("Add row below");
51 QPushButton* addRowBelowButton = new QPushButton("Add row below");
51 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
52 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
52
53
53 QPushButton* removeRowButton = new QPushButton("Remove row");
54 QPushButton* removeRowButton = new QPushButton("Remove row");
54 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
55 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
55
56
57 linesCountSpinBox = new QSpinBox;
58 linesCountSpinBox->setRange(1, 10);
59 linesCountSpinBox->setValue(1);
60
56 // buttons layout
61 // buttons layout
57 QVBoxLayout* buttonsLayout = new QVBoxLayout;
62 QVBoxLayout* buttonsLayout = new QVBoxLayout;
63 buttonsLayout->addWidget(linesCountSpinBox);
58 buttonsLayout->addWidget(addRowAboveButton);
64 buttonsLayout->addWidget(addRowAboveButton);
59 buttonsLayout->addWidget(addRowBelowButton);
65 buttonsLayout->addWidget(addRowBelowButton);
60 buttonsLayout->addWidget(removeRowButton);
66 buttonsLayout->addWidget(removeRowButton);
61 buttonsLayout->addStretch();
67 buttonsLayout->addStretch();
62
68
63 // chart type radio buttons
69 // chart type radio buttons
64 lineRadioButton = new QRadioButton("Line");
70 lineRadioButton = new QRadioButton("Line");
65 splineRadioButton = new QRadioButton("Spline");
71 splineRadioButton = new QRadioButton("Spline");
66 scatterRadioButton = new QRadioButton("Scatter");
72 scatterRadioButton = new QRadioButton("Scatter");
67 pieRadioButton = new QRadioButton("Pie");
73 pieRadioButton = new QRadioButton("Pie");
68 areaRadioButton = new QRadioButton("Area");
74 areaRadioButton = new QRadioButton("Area");
69 barRadioButton = new QRadioButton("Bar");
75 barRadioButton = new QRadioButton("Bar");
70
76
71 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
77 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
72 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
78 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
73 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
79 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
74 connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
80 connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
75 connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
81 connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
76 connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
82 connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
77 lineRadioButton->setChecked(true);
83 lineRadioButton->setChecked(true);
78
84
79 // radio buttons layout
85 // radio buttons layout
80 QVBoxLayout* radioLayout = new QVBoxLayout;
86 QVBoxLayout* radioLayout = new QVBoxLayout;
81 radioLayout->addWidget(lineRadioButton);
87 radioLayout->addWidget(lineRadioButton);
82 radioLayout->addWidget(splineRadioButton);
88 radioLayout->addWidget(splineRadioButton);
83 radioLayout->addWidget(scatterRadioButton);
89 radioLayout->addWidget(scatterRadioButton);
84 radioLayout->addWidget(pieRadioButton);
90 radioLayout->addWidget(pieRadioButton);
85 radioLayout->addWidget(areaRadioButton);
91 radioLayout->addWidget(areaRadioButton);
86 radioLayout->addWidget(barRadioButton);
92 radioLayout->addWidget(barRadioButton);
87 radioLayout->addStretch();
93 radioLayout->addStretch();
88
94
89 // create main layout
95 // create main layout
90 QGridLayout* mainLayout = new QGridLayout;
96 QGridLayout* mainLayout = new QGridLayout;
91 mainLayout->addLayout(buttonsLayout, 1, 0);
97 mainLayout->addLayout(buttonsLayout, 1, 0);
92 mainLayout->addLayout(radioLayout, 2, 0);
98 mainLayout->addLayout(radioLayout, 2, 0);
93 mainLayout->addWidget(tableView, 1, 1);
99 mainLayout->addWidget(tableView, 1, 1);
94 mainLayout->addWidget(chartView, 2, 1);
100 mainLayout->addWidget(chartView, 2, 1);
95 setLayout(mainLayout);
101 setLayout(mainLayout);
96 lineRadioButton->setFocus();
102 lineRadioButton->setFocus();
97 }
103 }
98
104
99 void TableWidget::addRowAbove()
105 void TableWidget::addRowAbove()
100 {
106 {
101 // m_model->insertRow(m_model->rowCount());
107 // m_model->insertRow(m_model->rowCount());
102 m_model->insertRow(tableView->currentIndex().row());
108 // m_model->insertRow(tableView->currentIndex().row());
109 m_model->insertRows(tableView->currentIndex().row(), linesCountSpinBox->value());
103
110
104 }
111 }
105
112
106 void TableWidget::addRowBelow()
113 void TableWidget::addRowBelow()
107 {
114 {
108 // m_model->insertRow(m_model->rowCount());
115 // m_model->insertRow(m_model->rowCount());
109 m_model->insertRow(tableView->currentIndex().row() + 1);
116 // m_model->insertRow(tableView->currentIndex().row() + 1);
117 m_model->insertRows(tableView->currentIndex().row() + 1, linesCountSpinBox->value());
110
118
111 }
119 }
112
120
113 void TableWidget::removeRow()
121 void TableWidget::removeRow()
114 {
122 {
115 // m_model->removeRow(m_model->rowCount() - 1);
123 // m_model->removeRow(m_model->rowCount() - 1);
116 m_model->removeRow(tableView->currentIndex().row());
124 // m_model->removeRow(tableView->currentIndex().row());
125 m_model->removeRows(tableView->currentIndex().row(), qMin(m_model->rowCount() - tableView->currentIndex().row(), linesCountSpinBox->value()));
117 }
126 }
118
127
119 void TableWidget::updateChartType()
128 void TableWidget::updateChartType()
120 {
129 {
121 chartView->removeAllSeries();
130 chartView->removeAllSeries();
122
131
123 if (lineRadioButton->isChecked())
132 if (lineRadioButton->isChecked())
124 {
133 {
134 QPen pen;
135 pen.setWidth(2);
136
137 QColor seriesColor("#8FBC8F");
138
125 // series 1
139 // series 1
126 series = new QLineSeries;
140 series = new QLineSeries;
127 series->setModel(m_model);
141 series->setModel(m_model);
128 series->setModelMapping(0,1, Qt::Vertical);
142 series->setModelMapping(0,1, Qt::Vertical);
129 series->setModelMappingShift(1, 4);
143 series->setModelMappingShift(1, 4);
130 // series->setModelMapping(0,1, Qt::Horizontal);
144 // series->setModelMapping(0,1, Qt::Horizontal);
145
146 pen.setColor(seriesColor);
147 series->setPen(pen);
131 chartView->addSeries(series);
148 chartView->addSeries(series);
149 for (int i = 1; i <=4; i++)
150 {
151 m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole);
152 m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole);
153 }
154 // tableView->setsetStyleSheet("QTableView { border: 2px solid red }");
155
156 seriesColor = QColor("#1E90FF");
132
157
133 // series 2
158 // series 2
134 series = new QLineSeries;
159 series = new QLineSeries;
135 series->setModel(m_model);
160 series->setModel(m_model);
136 series->setModelMapping(2,3, Qt::Vertical);
161 series->setModelMapping(2,3, Qt::Vertical);
137 // series->setModelMapping(2,3, Qt::Horizontal);
162 // series->setModelMapping(2,3, Qt::Horizontal);
163 pen.setColor(seriesColor);
164 series->setPen(pen);
138 chartView->addSeries(series);
165 chartView->addSeries(series);
139
166
167 chartView->axisX()->setRange(0, 500);
168
169 for (int i = 0; i < m_model->rowCount(); i++)
170 {
171 m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole);
172 m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole);
173 }
140 // // series 3
174 // // series 3
141 // series = new QLineSeries;
175 // series = new QLineSeries;
142 // series->setModel(m_model);
176 // series->setModel(m_model);
143 // series->setModelMapping(4,5, Qt::Vertical);
177 // series->setModelMapping(4,5, Qt::Vertical);
144 //// series->setModelMapping(4,5, Qt::Horizontal);
178 //// series->setModelMapping(4,5, Qt::Horizontal);
145 // chartView->addSeries(series);
179 // chartView->addSeries(series);
146 }
180 }
147 else if (splineRadioButton->isChecked())
181 else if (splineRadioButton->isChecked())
148 {
182 {
149 // series 1
183 // series 1
150 series = new QSplineSeries;
184 series = new QSplineSeries;
151 series->setModel(m_model);
185 series->setModel(m_model);
152 series->setModelMapping(0,1, Qt::Vertical);
186 series->setModelMapping(0,1, Qt::Vertical);
153 series->setModelMappingShift(1, 4);
187 series->setModelMappingShift(1, 4);
154 // series->setModelMapping(0,1, Qt::Horizontal);
188 // series->setModelMapping(0,1, Qt::Horizontal);
155 chartView->addSeries(series);
189 chartView->addSeries(series);
156
190
157 // series 2
191 // series 2
158 series = new QSplineSeries;
192 series = new QSplineSeries;
159 series->setModel(m_model);
193 series->setModel(m_model);
160 series->setModelMapping(2,3, Qt::Vertical);
194 series->setModelMapping(2,3, Qt::Vertical);
195 series->setModelMappingShift(0, 0);
161 // series->setModelMapping(2,3, Qt::Horizontal);
196 // series->setModelMapping(2,3, Qt::Horizontal);
162 chartView->addSeries(series);
197 chartView->addSeries(series);
163
198
164 // series 3
199 // series 3
165 series = new QSplineSeries;
200 series = new QSplineSeries;
166 series->setModel(m_model);
201 series->setModel(m_model);
167 series->setModelMapping(4,5, Qt::Vertical);
202 series->setModelMapping(4,5, Qt::Vertical);
203 series->setModelMappingShift(0, 0);
168 // series->setModelMapping(4,5, Qt::Horizontal);
204 // series->setModelMapping(4,5, Qt::Horizontal);
169 chartView->addSeries(series);
205 chartView->addSeries(series);
170 }
206 }
171 else if (scatterRadioButton->isChecked())
207 else if (scatterRadioButton->isChecked())
172 {
208 {
173 // series 1
209 // series 1
174 series = new QScatterSeries;
210 series = new QScatterSeries;
175 series->setModel(m_model);
211 series->setModel(m_model);
176 series->setModelMapping(0,1, Qt::Vertical);
212 series->setModelMapping(0,1, Qt::Vertical);
177 // series->setModelMapping(0,1, Qt::Horizontal);
213 // series->setModelMapping(0,1, Qt::Horizontal);
178 chartView->addSeries(series);
214 chartView->addSeries(series);
179
215
180 // series 2
216 // series 2
181 series = new QScatterSeries;
217 series = new QScatterSeries;
182 series->setModel(m_model);
218 series->setModel(m_model);
183 series->setModelMapping(2,3, Qt::Vertical);
219 series->setModelMapping(2,3, Qt::Vertical);
184 // series->setModelMapping(2,3, Qt::Horizontal);
220 // series->setModelMapping(2,3, Qt::Horizontal);
185 chartView->addSeries(series);
221 chartView->addSeries(series);
186
222
187 // series 3
223 // series 3
188 series = new QScatterSeries;
224 series = new QScatterSeries;
189 series->setModel(m_model);
225 series->setModel(m_model);
190 series->setModelMapping(4,5, Qt::Vertical);
226 series->setModelMapping(4,5, Qt::Vertical);
191 // series->setModelMapping(4,5, Qt::Horizontal);
227 // series->setModelMapping(4,5, Qt::Horizontal);
192 chartView->addSeries(series);
228 chartView->addSeries(series);
193 }
229 }
194 else if (pieRadioButton->isChecked())
230 else if (pieRadioButton->isChecked())
195 {
231 {
196 // pie 1
232 // pie 1
197 QPieSeries* pieSeries = new QPieSeries;
233 QPieSeries* pieSeries = new QPieSeries;
198 pieSeries->setModel(m_model);
234 pieSeries->setModel(m_model);
199 pieSeries->setModelMapping(0,0, Qt::Vertical);
235 pieSeries->setModelMapping(0,0, Qt::Vertical);
200 pieSeries->setLabelsVisible(true);
236 pieSeries->setLabelsVisible(true);
201 pieSeries->setPieSize(0.4);
237 pieSeries->setPieSize(0.4);
202 pieSeries->setPiePosition(0.2, 0.35);
238 pieSeries->setPiePosition(0.2, 0.35);
203 chartView->addSeries(pieSeries);
239 chartView->addSeries(pieSeries);
204
240
205 // pie 2
241 // pie 2
206 pieSeries = new QPieSeries;
242 pieSeries = new QPieSeries;
207 pieSeries->setModel(m_model);
243 pieSeries->setModel(m_model);
208 pieSeries->setModelMapping(1,1, Qt::Vertical);
244 pieSeries->setModelMapping(1,1, Qt::Vertical);
209 pieSeries->setLabelsVisible(true);
245 pieSeries->setLabelsVisible(true);
210 pieSeries->setPieSize(0.4);
246 pieSeries->setPieSize(0.4);
211 pieSeries->setPiePosition(0.8, 0.35);
247 pieSeries->setPiePosition(0.8, 0.35);
212 chartView->addSeries(pieSeries);
248 chartView->addSeries(pieSeries);
213
249
214 // pie 3
250 // pie 3
215 pieSeries = new QPieSeries;
251 pieSeries = new QPieSeries;
216 pieSeries->setModel(m_model);
252 pieSeries->setModel(m_model);
217 pieSeries->setModelMapping(2,2, Qt::Vertical);
253 pieSeries->setModelMapping(2,2, Qt::Vertical);
218 pieSeries->setLabelsVisible(true);
254 pieSeries->setLabelsVisible(true);
219 pieSeries->setPieSize(0.4);
255 pieSeries->setPieSize(0.4);
220 pieSeries->setPiePosition(0.5, 0.65);
256 pieSeries->setPiePosition(0.5, 0.65);
221 chartView->addSeries(pieSeries);
257 chartView->addSeries(pieSeries);
222 }
258 }
223 else if (areaRadioButton->isChecked())
259 else if (areaRadioButton->isChecked())
224 {
260 {
225 QLineSeries* upperLineSeries = new QLineSeries;
261 QLineSeries* upperLineSeries = new QLineSeries;
226 upperLineSeries->setModel(m_model);
262 upperLineSeries->setModel(m_model);
227 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
263 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
228 QLineSeries* lowerLineSeries = new QLineSeries;
264 QLineSeries* lowerLineSeries = new QLineSeries;
229 lowerLineSeries->setModel(m_model);
265 lowerLineSeries->setModel(m_model);
230 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
266 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
231 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
267 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
232 chartView->addSeries(areaSeries);
268 chartView->addSeries(areaSeries);
233 }
269 }
234 else if (barRadioButton->isChecked())
270 else if (barRadioButton->isChecked())
235 {
271 {
236 QBarSeries* barSeries = new QBarSeries(QStringList());
272 QBarSeries* barSeries = new QBarSeries(QStringList());
237 barSeries->setModel(m_model);
273 barSeries->setModel(m_model);
238 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
274 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
239 barSeries->setToolTipEnabled(true);
275 barSeries->setToolTipEnabled(true);
240 chartView->addSeries(barSeries);
276 chartView->addSeries(barSeries);
241 }
277 }
242
278
243 // series->setModel(m_model);
279 // series->setModel(m_model);
244 // series->setModelMapping(0,1, Qt::Vertical);
280 // series->setModelMapping(0,1, Qt::Vertical);
245 // chartView->addSeries(series);
281 // chartView->addSeries(series);
246 }
282 }
247
283
248 TableWidget::~TableWidget()
284 TableWidget::~TableWidget()
249 {
285 {
250
286
251 }
287 }
@@ -1,43 +1,45
1 #ifndef TABLEWIDGET_H
1 #ifndef TABLEWIDGET_H
2 #define TABLEWIDGET_H
2 #define TABLEWIDGET_H
3
3
4 #include <QtGui/QWidget>
4 #include <QtGui/QWidget>
5 #include "qchartview.h"
5 #include "qchartview.h"
6 #include "qxyseries.h"
6 #include "qxyseries.h"
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 class CustomTableModel;
10 class CustomTableModel;
11 class QTableView;
11 class QTableView;
12 class QRadioButton;
12 class QRadioButton;
13 class QSpinBox;
13 //class QSeries;
14 //class QSeries;
14
15
15 class TableWidget : public QWidget
16 class TableWidget : public QWidget
16 {
17 {
17 Q_OBJECT
18 Q_OBJECT
18
19
19 public:
20 public:
20 TableWidget(QWidget *parent = 0);
21 TableWidget(QWidget *parent = 0);
21 ~TableWidget();
22 ~TableWidget();
22
23
23
24
24 public slots:
25 public slots:
25 void addRowAbove();
26 void addRowAbove();
26 void addRowBelow();
27 void addRowBelow();
27 void removeRow();
28 void removeRow();
28 void updateChartType();
29 void updateChartType();
29
30
30 private:
31 private:
31 QChartView* chartView;
32 QChartView* chartView;
32 QXYSeries* series;
33 QXYSeries* series;
33 CustomTableModel* m_model;
34 CustomTableModel* m_model;
34 QTableView* tableView;
35 QTableView* tableView;
35 QRadioButton* lineRadioButton;
36 QRadioButton* lineRadioButton;
36 QRadioButton* splineRadioButton;
37 QRadioButton* splineRadioButton;
37 QRadioButton* scatterRadioButton;
38 QRadioButton* scatterRadioButton;
38 QRadioButton* pieRadioButton;
39 QRadioButton* pieRadioButton;
39 QRadioButton* areaRadioButton;
40 QRadioButton* areaRadioButton;
40 QRadioButton* barRadioButton;
41 QRadioButton* barRadioButton;
42 QSpinBox* linesCountSpinBox;
41 };
43 };
42
44
43 #endif // TABLEWIDGET_H
45 #endif // TABLEWIDGET_H
@@ -1,391 +1,391
1 #include <QDebug>
1 #include <QDebug>
2 #include "qbarseries.h"
2 #include "qbarseries.h"
3 #include "qbarset.h"
3 #include "qbarset.h"
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 /*!
8 /*!
9 \class QBarSeries
9 \class QBarSeries
10 \brief part of QtCommercial chart API.
10 \brief part of QtCommercial chart API.
11
11
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
14 by QStringList.
14 by QStringList.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn virtual QSeriesType QBarSeries::type() const
22 \fn virtual QSeriesType QBarSeries::type() const
23 \brief Returns type of series.
23 \brief Returns type of series.
24 \sa QSeries, QSeriesType
24 \sa QSeries, QSeriesType
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
29 \brief \internal \a pos \a tip
29 \brief \internal \a pos \a tip
30 */
30 */
31
31
32 /*!
32 /*!
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
34 QBarSeries is QObject which is a child of a \a parent.
34 QBarSeries is QObject which is a child of a \a parent.
35 */
35 */
36 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent)
36 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent)
37 : QSeries(parent)
37 : QSeries(parent)
38 ,mModel(new BarChartModel(categories, this))
38 ,mModel(new BarChartModel(categories, this))
39 {
39 {
40 m_model = NULL;
40 m_model = NULL;
41 m_mapCategories = -1;
41 m_mapCategories = -1;
42 m_mapBarBottom = -1;
42 m_mapBarBottom = -1;
43 m_mapBarTop = -1;
43 m_mapBarTop = -1;
44 m_mapFirst = 0;
44 m_mapFirst = 0;
45 m_mapCount = 0;
45 m_mapCount = 0;
46 m_mapOrientation = Qt::Vertical;
46 m_mapOrientation = Qt::Vertical;
47 }
47 }
48
48
49 /*!
49 /*!
50 Adds a set of bars to series. Takes ownership of \a set.
50 Adds a set of bars to series. Takes ownership of \a set.
51 Connects the clicked(QString) and rightClicked(QString) signals
51 Connects the clicked(QString) and rightClicked(QString) signals
52 of \a set to this series
52 of \a set to this series
53 */
53 */
54 void QBarSeries::addBarSet(QBarSet *set)
54 void QBarSeries::addBarSet(QBarSet *set)
55 {
55 {
56 mModel->addBarSet(set);
56 mModel->addBarSet(set);
57 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
57 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
58 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
58 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
59 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
59 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
60 emit updatedBars();
60 emit updatedBars();
61 }
61 }
62
62
63 /*!
63 /*!
64 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
64 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
65 Disconnects the clicked(QString) and rightClicked(QString) signals
65 Disconnects the clicked(QString) and rightClicked(QString) signals
66 of \a set from this series
66 of \a set from this series
67 */
67 */
68 void QBarSeries::removeBarSet(QBarSet *set)
68 void QBarSeries::removeBarSet(QBarSet *set)
69 {
69 {
70 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
70 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
71 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
71 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
72 mModel->removeBarSet(set);
72 mModel->removeBarSet(set);
73 emit updatedBars();
73 emit updatedBars();
74 }
74 }
75
75
76 void QBarSeries::insertBarSet(int i, QBarSet *set)
76 void QBarSeries::insertBarSet(int i, QBarSet *set)
77 {
77 {
78 mModel->insertBarSet(i, set);
78 mModel->insertBarSet(i, set);
79 // emit barsetChanged();
79 // emit barsetChanged();
80 }
80 }
81
81
82 void QBarSeries::insertCategory(int i, QString category)
82 void QBarSeries::insertCategory(int i, QString category)
83 {
83 {
84 mModel->insertCategory(i, category);
84 mModel->insertCategory(i, category);
85 }
85 }
86
86
87 void QBarSeries::removeCategory(int i)
87 void QBarSeries::removeCategory(int i)
88 {
88 {
89 mModel->removeCategory(i);
89 mModel->removeCategory(i);
90 }
90 }
91
91
92 /*!
92 /*!
93 Returns number of sets in series.
93 Returns number of sets in series.
94 */
94 */
95 int QBarSeries::barsetCount()
95 int QBarSeries::barsetCount()
96 {
96 {
97 // if(m_model)
97 // if(m_model)
98 // return m_mapBarTop - m_mapBarBottom;
98 // return m_mapBarTop - m_mapBarBottom;
99 // else
99 // else
100 return mModel->barsetCount();
100 return mModel->barsetCount();
101 }
101 }
102
102
103 /*!
103 /*!
104 Returns number of categories in series
104 Returns number of categories in series
105 */
105 */
106 int QBarSeries::categoryCount()
106 int QBarSeries::categoryCount()
107 {
107 {
108 return mModel->categoryCount();
108 return mModel->categoryCount();
109 }
109 }
110
110
111 /*!
111 /*!
112 Returns a list of sets in series. Keeps ownership of sets.
112 Returns a list of sets in series. Keeps ownership of sets.
113 */
113 */
114 QList<QBarSet*> QBarSeries::barSets()
114 QList<QBarSet*> QBarSeries::barSets()
115 {
115 {
116 return mModel->barSets();
116 return mModel->barSets();
117 }
117 }
118
118
119 /*!
119 /*!
120 \internal \a index
120 \internal \a index
121 */
121 */
122 QBarSet* QBarSeries::barsetAt(int index)
122 QBarSet* QBarSeries::barsetAt(int index)
123 {
123 {
124 return mModel->setAt(index);
124 return mModel->setAt(index);
125 }
125 }
126
126
127 /*!
127 /*!
128 \internal \a category
128 \internal \a category
129 */
129 */
130 QString QBarSeries::categoryName(int category)
130 QString QBarSeries::categoryName(int category)
131 {
131 {
132 return mModel->categoryName(category);
132 return mModel->categoryName(category);
133 }
133 }
134
134
135 /*!
135 /*!
136 Enables or disables tooltip depending on parameter \a enabled.
136 Enables or disables tooltip depending on parameter \a enabled.
137 Tooltip shows the name of set, when mouse is hovering on top of bar.
137 Tooltip shows the name of set, when mouse is hovering on top of bar.
138 Calling without parameter \a enabled, enables the tooltip
138 Calling without parameter \a enabled, enables the tooltip
139 */
139 */
140 void QBarSeries::setToolTipEnabled(bool enabled)
140 void QBarSeries::setToolTipEnabled(bool enabled)
141 {
141 {
142 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
142 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
143 if (enabled) {
143 if (enabled) {
144 for (int i=0; i<mModel->barsetCount(); i++) {
144 for (int i=0; i<mModel->barsetCount(); i++) {
145 QBarSet *set = mModel->setAt(i);
145 QBarSet *set = mModel->setAt(i);
146 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
146 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
147 }
147 }
148 } else {
148 } else {
149 for (int i=0; i<mModel->barsetCount(); i++) {
149 for (int i=0; i<mModel->barsetCount(); i++) {
150 QBarSet *set = mModel->setAt(i);
150 QBarSet *set = mModel->setAt(i);
151 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
151 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
152 }
152 }
153 }
153 }
154 }
154 }
155
155
156
156
157 /*!
157 /*!
158 \internal \a category
158 \internal \a category
159 */
159 */
160 void QBarSeries::barsetClicked(QString category)
160 void QBarSeries::barsetClicked(QString category)
161 {
161 {
162 emit clicked(qobject_cast<QBarSet*>(sender()), category);
162 emit clicked(qobject_cast<QBarSet*>(sender()), category);
163 }
163 }
164
164
165 /*!
165 /*!
166 \internal \a category
166 \internal \a category
167 */
167 */
168 void QBarSeries::barsetRightClicked(QString category)
168 void QBarSeries::barsetRightClicked(QString category)
169 {
169 {
170 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
170 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
171 }
171 }
172
172
173
173
174 /*!
174 /*!
175 \internal
175 \internal
176 */
176 */
177 qreal QBarSeries::min()
177 qreal QBarSeries::min()
178 {
178 {
179 return mModel->min();
179 return mModel->min();
180 }
180 }
181
181
182 /*!
182 /*!
183 \internal
183 \internal
184 */
184 */
185 qreal QBarSeries::max()
185 qreal QBarSeries::max()
186 {
186 {
187 return mModel->max();
187 return mModel->max();
188 }
188 }
189
189
190 /*!
190 /*!
191 \internal \a set \a category
191 \internal \a set \a category
192 */
192 */
193 qreal QBarSeries::valueAt(int set, int category)
193 qreal QBarSeries::valueAt(int set, int category)
194 {
194 {
195 return mModel->valueAt(set,category);
195 return mModel->valueAt(set,category);
196 }
196 }
197
197
198 /*!
198 /*!
199 \internal \a set \a category
199 \internal \a set \a category
200 */
200 */
201 qreal QBarSeries::percentageAt(int set, int category)
201 qreal QBarSeries::percentageAt(int set, int category)
202 {
202 {
203 return mModel->percentageAt(set,category);
203 return mModel->percentageAt(set,category);
204 }
204 }
205
205
206 /*!
206 /*!
207 \internal \a category
207 \internal \a category
208 */
208 */
209 qreal QBarSeries::categorySum(int category)
209 qreal QBarSeries::categorySum(int category)
210 {
210 {
211 return mModel->categorySum(category);
211 return mModel->categorySum(category);
212 }
212 }
213
213
214 /*!
214 /*!
215 \internal
215 \internal
216 */
216 */
217 qreal QBarSeries::maxCategorySum()
217 qreal QBarSeries::maxCategorySum()
218 {
218 {
219 return mModel->maxCategorySum();
219 return mModel->maxCategorySum();
220 }
220 }
221
221
222 /*!
222 /*!
223 \internal
223 \internal
224 */
224 */
225 BarChartModel& QBarSeries::model()
225 BarChartModel& QBarSeries::model()
226 {
226 {
227 return *mModel;
227 return *mModel;
228 }
228 }
229
229
230 bool QBarSeries::setModel(QAbstractItemModel* model)
230 bool QBarSeries::setModel(QAbstractItemModel* model)
231 {
231 {
232 // disconnect signals from old model
232 // disconnect signals from old model
233 if(m_model)
233 if(m_model)
234 {
234 {
235 disconnect(m_model, 0, this, 0);
235 disconnect(m_model, 0, this, 0);
236 m_mapCategories = -1;
236 m_mapCategories = -1;
237 m_mapBarBottom = -1;
237 m_mapBarBottom = -1;
238 m_mapBarTop = -1;
238 m_mapBarTop = -1;
239 m_mapFirst = 0;
239 m_mapFirst = 0;
240 m_mapCount = 0;
240 m_mapCount = 0;
241 m_mapOrientation = Qt::Vertical;
241 m_mapOrientation = Qt::Vertical;
242 }
242 }
243
243
244 // set new model
244 // set new model
245 if(model)
245 if(model)
246 {
246 {
247 m_model = model;
247 m_model = model;
248 return true;
248 return true;
249 }
249 }
250 else
250 else
251 {
251 {
252 m_model = NULL;
252 m_model = NULL;
253 return false;
253 return false;
254 }
254 }
255 }
255 }
256
256
257 // TODO
257 // TODO
258 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
258 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
259 {
259 {
260 if (m_model == NULL)
260 if (m_model == NULL)
261 return;
261 return;
262 m_mapCategories = categories;
262 m_mapCategories = categories;
263 m_mapBarBottom = bottomBoundry;
263 m_mapBarBottom = bottomBoundry;
264 m_mapBarTop = topBoundry;
264 m_mapBarTop = topBoundry;
265 m_mapFirst = 1;
265 // m_mapFirst = 1;
266 m_mapOrientation = orientation;
266 m_mapOrientation = orientation;
267
267
268 // connect the signals
268 // connect the signals
269 if (m_mapOrientation == Qt::Vertical)
269 if (m_mapOrientation == Qt::Vertical)
270 {
270 {
271 m_mapCount = m_model->rowCount();
271 m_mapCount = m_model->rowCount() - m_mapFirst;
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
273 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
274 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
274 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
275 }
275 }
276 else
276 else
277 {
277 {
278 m_mapCount = m_model->columnCount();
278 m_mapCount = m_model->columnCount() - m_mapFirst;
279 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
279 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
280 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
280 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
281 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
281 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
282 }
282 }
283
283
284
284
285 // create the initial bars
285 // create the initial bars
286 delete mModel;
286 delete mModel;
287 if (m_mapOrientation == Qt::Vertical)
287 if (m_mapOrientation == Qt::Vertical)
288 {
288 {
289 QStringList categories;
289 QStringList categories;
290 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
290 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
291 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
291 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
292 mModel = new BarChartModel(categories, this);
292 mModel = new BarChartModel(categories, this);
293
293
294 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
294 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
295 {
295 {
296 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
296 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
297 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
297 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
298 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
298 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
299 addBarSet(barSet);
299 addBarSet(barSet);
300 }
300 }
301 }
301 }
302 else
302 else
303 {
303 {
304 QStringList categories;
304 QStringList categories;
305 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
305 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
306 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
306 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
307 mModel = new BarChartModel(categories, this);
307 mModel = new BarChartModel(categories, this);
308
308
309 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
309 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
310 {
310 {
311 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
311 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
312 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
312 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
313 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
313 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
314 addBarSet(barSet);
314 addBarSet(barSet);
315 }
315 }
316 }
316 }
317 }
317 }
318
318
319 void QBarSeries::setModelMappingShift(int first, int count)
319 void QBarSeries::setModelMappingShift(int first, int count)
320 {
320 {
321 m_mapFirst = first;
321 m_mapFirst = first;
322 m_mapCount = count;
322 m_mapCount = count;
323 }
323 }
324
324
325 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
325 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
326 {
326 {
327 Q_UNUSED(bottomRight)
327 Q_UNUSED(bottomRight)
328
328
329 if (m_mapOrientation == Qt::Vertical)
329 if (m_mapOrientation == Qt::Vertical)
330 {
330 {
331 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
331 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
332 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
332 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
333 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
333 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
334 }
334 }
335 else
335 else
336 {
336 {
337 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
337 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
338 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
338 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
339 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
339 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
340 }
340 }
341 }
341 }
342
342
343 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
343 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
344 {
344 {
345 if (m_mapOrientation == Qt::Vertical)
345 if (m_mapOrientation == Qt::Vertical)
346 {
346 {
347 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
347 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
348 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
348 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
349 {
349 {
350 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
350 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
351 }
351 }
352 }
352 }
353 else
353 else
354 {
354 {
355 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
355 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
356 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
356 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
357 {
357 {
358 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
358 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
359 }
359 }
360 }
360 }
361 emit restructuredBar(1);
361 emit restructuredBar(1);
362 }
362 }
363
363
364 void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int start, int /*end*/)
364 void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int start, int /*end*/)
365 {
365 {
366 removeCategory(start - m_mapFirst);
366 removeCategory(start - m_mapFirst);
367 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
367 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
368 {
368 {
369 barsetAt(i)->removeValue(start - m_mapFirst);
369 barsetAt(i)->removeValue(start - m_mapFirst);
370 }
370 }
371 emit restructuredBar(1);
371 emit restructuredBar(1);
372 }
372 }
373
373
374 void QBarSeries::barsetChanged()
374 void QBarSeries::barsetChanged()
375 {
375 {
376 emit updatedBars();
376 emit updatedBars();
377 }
377 }
378
378
379 QBarCategories QBarSeries::categories() const
379 QBarCategories QBarSeries::categories() const
380 {
380 {
381 QBarCategories categories;
381 QBarCategories categories;
382 int count = mModel->categoryCount();
382 int count = mModel->categoryCount();
383 for (int i=1; i<=count; i++) {
383 for (int i=1; i<=count; i++) {
384 categories.insert(i, mModel->categoryName(i-1));
384 categories.insert(i, mModel->categoryName(i-1));
385 }
385 }
386 return categories;
386 return categories;
387 }
387 }
388
388
389 #include "moc_qbarseries.cpp"
389 #include "moc_qbarseries.cpp"
390
390
391 QTCOMMERCIALCHART_END_NAMESPACE
391 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,517 +1,519
1 #include "qxyseries.h"
1 #include "qxyseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QXYSeries
6 \class QXYSeries
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
8 */
8 */
9
9
10 /*!
10 /*!
11 \fn QPen QXYSeries::pen() const
11 \fn QPen QXYSeries::pen() const
12 \brief Returns pen used to draw points for series.
12 \brief Returns pen used to draw points for series.
13 \sa setPen()
13 \sa setPen()
14 */
14 */
15
15
16 /*!
16 /*!
17 \fn QBrush QXYSeries::brush() const
17 \fn QBrush QXYSeries::brush() const
18 \brief Returns brush used to draw points for series.
18 \brief Returns brush used to draw points for series.
19 \sa setBrush()
19 \sa setBrush()
20 */
20 */
21
21
22 /*!
22 /*!
23 \fn void QXYSeries::clicked(const QPointF& point)
23 \fn void QXYSeries::clicked(const QPointF& point)
24 \brief Signal is emitted when user clicks the \a point on chart.
24 \brief Signal is emitted when user clicks the \a point on chart.
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QXYSeries::pointReplaced(int index)
28 \fn void QXYSeries::pointReplaced(int index)
29 \brief \internal \a index
29 \brief \internal \a index
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn void QXYSeries::pointAdded(int index)
33 \fn void QXYSeries::pointAdded(int index)
34 \brief \internal \a index
34 \brief \internal \a index
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn void QXYSeries::pointRemoved(int index)
38 \fn void QXYSeries::pointRemoved(int index)
39 \brief \internal \a index
39 \brief \internal \a index
40 */
40 */
41
41
42 /*!
42 /*!
43 \fn void QXYSeries::updated()
43 \fn void QXYSeries::updated()
44 \brief \internal
44 \brief \internal
45 */
45 */
46
46
47 /*!
47 /*!
48 Constructs empty series object which is a child of \a parent.
48 Constructs empty series object which is a child of \a parent.
49 When series object is added to QChartView or QChart instance ownerships is transfered.
49 When series object is added to QChartView or QChart instance ownerships is transfered.
50 */
50 */
51 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
51 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
52 {
52 {
53 m_mapX = -1;
53 m_mapX = -1;
54 m_mapY = -1;
54 m_mapY = -1;
55 m_mapFirst = 0;
55 m_mapFirst = 0;
56 m_mapCount = 0;
56 m_mapCount = 0;
57 m_mapLimited = false;
57 m_mapLimited = false;
58 m_mapOrientation = Qt::Vertical;
58 m_mapOrientation = Qt::Vertical;
59 // m_mapYOrientation = Qt::Vertical;
59 // m_mapYOrientation = Qt::Vertical;
60 }
60 }
61 /*!
61 /*!
62 Destroys the object. Series added to QChartView or QChart instances are owned by those,
62 Destroys the object. Series added to QChartView or QChart instances are owned by those,
63 and are deleted when mentioned object are destroyed.
63 and are deleted when mentioned object are destroyed.
64 */
64 */
65 QXYSeries::~QXYSeries()
65 QXYSeries::~QXYSeries()
66 {
66 {
67 }
67 }
68
68
69 /*!
69 /*!
70 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
70 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
71 */
71 */
72 void QXYSeries::add(qreal x,qreal y)
72 void QXYSeries::add(qreal x,qreal y)
73 {
73 {
74 Q_ASSERT(m_x.size() == m_y.size());
74 Q_ASSERT(m_x.size() == m_y.size());
75 m_x<<x;
75 m_x<<x;
76 m_y<<y;
76 m_y<<y;
77 emit pointAdded(m_x.size()-1);
77 emit pointAdded(m_x.size()-1);
78 }
78 }
79
79
80 /*!
80 /*!
81 This is an overloaded function.
81 This is an overloaded function.
82 Adds data \a point to the series. Points are connected with lines on the chart.
82 Adds data \a point to the series. Points are connected with lines on the chart.
83 */
83 */
84 void QXYSeries::add(const QPointF& point)
84 void QXYSeries::add(const QPointF& point)
85 {
85 {
86 add(point.x(),point.y());
86 add(point.x(),point.y());
87 }
87 }
88
88
89 /*!
89 /*!
90 This is an overloaded function.
90 This is an overloaded function.
91 Adds list of data \a points to the series. Points are connected with lines on the chart.
91 Adds list of data \a points to the series. Points are connected with lines on the chart.
92 */
92 */
93 void QXYSeries::add(const QList<QPointF> points)
93 void QXYSeries::add(const QList<QPointF> points)
94 {
94 {
95 foreach(const QPointF& point , points) {
95 foreach(const QPointF& point , points) {
96 add(point.x(),point.y());
96 add(point.x(),point.y());
97 }
97 }
98 }
98 }
99
99
100 /*!
100 /*!
101 Modifies \a y value for given \a x a value.
101 Modifies \a y value for given \a x a value.
102 */
102 */
103 void QXYSeries::replace(qreal x,qreal y)
103 void QXYSeries::replace(qreal x,qreal y)
104 {
104 {
105 int index = m_x.indexOf(x);
105 int index = m_x.indexOf(x);
106 m_x[index]=x;
106 m_x[index]=x;
107 m_y[index]=y;
107 m_y[index]=y;
108 emit pointReplaced(index);
108 emit pointReplaced(index);
109 }
109 }
110
110
111 /*!
111 /*!
112 This is an overloaded function.
112 This is an overloaded function.
113 Replaces current y value of for given \a point x value with \a point y value.
113 Replaces current y value of for given \a point x value with \a point y value.
114 */
114 */
115 void QXYSeries::replace(const QPointF& point)
115 void QXYSeries::replace(const QPointF& point)
116 {
116 {
117 replace(point.x(),point.y());
117 replace(point.x(),point.y());
118 }
118 }
119
119
120 /*!
120 /*!
121 Removes first \a x value and related y value.
121 Removes first \a x value and related y value.
122 */
122 */
123 void QXYSeries::remove(qreal x)
123 void QXYSeries::remove(qreal x)
124 {
124 {
125 int index = m_x.indexOf(x);
125 int index = m_x.indexOf(x);
126
126
127 if(index==-1) return;
127 if(index==-1) return;
128
128
129 m_x.remove(index);
129 m_x.remove(index);
130 m_y.remove(index);
130 m_y.remove(index);
131
131
132 emit pointRemoved(index);
132 emit pointRemoved(index);
133 }
133 }
134
134
135 /*!
135 /*!
136 Removes current \a x and \a y value.
136 Removes current \a x and \a y value.
137 */
137 */
138 void QXYSeries::remove(qreal x,qreal y)
138 void QXYSeries::remove(qreal x,qreal y)
139 {
139 {
140 int index =-1;
140 int index =-1;
141 do{
141 do{
142 index = m_x.indexOf(x,index+1);
142 index = m_x.indexOf(x,index+1);
143 }while(index !=-1 && m_y.at(index)!=y);
143 }while(index !=-1 && m_y.at(index)!=y);
144
144
145 if(index==-1) return;
145 if(index==-1) return;
146
146
147 m_x.remove(index);
147 m_x.remove(index);
148 m_y.remove(index);
148 m_y.remove(index);
149 emit pointRemoved(index);
149 emit pointRemoved(index);
150 }
150 }
151
151
152 /*!
152 /*!
153 Removes current \a point x value. Note \a point y value is ignored.
153 Removes current \a point x value. Note \a point y value is ignored.
154 */
154 */
155 void QXYSeries::remove(const QPointF& point)
155 void QXYSeries::remove(const QPointF& point)
156 {
156 {
157 remove(point.x(),point.y());
157 remove(point.x(),point.y());
158 }
158 }
159
159
160 /*!
160 /*!
161 Removes all data points from the series.
161 Removes all data points from the series.
162 */
162 */
163 void QXYSeries::removeAll()
163 void QXYSeries::removeAll()
164 {
164 {
165 m_x.clear();
165 m_x.clear();
166 m_y.clear();
166 m_y.clear();
167 }
167 }
168
168
169 /*!
169 /*!
170 \internal \a pos
170 \internal \a pos
171 */
171 */
172 qreal QXYSeries::x(int pos) const
172 qreal QXYSeries::x(int pos) const
173 {
173 {
174 if (m_model)
174 if (m_model)
175 if (m_mapOrientation == Qt::Vertical)
175 if (m_mapOrientation == Qt::Vertical)
176 // consecutive data is read from model's column
176 // consecutive data is read from model's column
177 return m_model->data(m_model->index(pos + m_mapFirst, m_mapX), Qt::DisplayRole).toDouble();
177 return m_model->data(m_model->index(pos + m_mapFirst, m_mapX), Qt::DisplayRole).toDouble();
178 else
178 else
179 // consecutive data is read from model's row
179 // consecutive data is read from model's row
180 return m_model->data(m_model->index(m_mapX, pos + m_mapFirst), Qt::DisplayRole).toDouble();
180 return m_model->data(m_model->index(m_mapX, pos + m_mapFirst), Qt::DisplayRole).toDouble();
181 else
181 else
182 // model is not specified, return the data from series' internal data store
182 // model is not specified, return the data from series' internal data store
183 return m_x.at(pos);
183 return m_x.at(pos);
184 }
184 }
185
185
186 /*!
186 /*!
187 \internal \a pos
187 \internal \a pos
188 */
188 */
189 qreal QXYSeries::y(int pos) const
189 qreal QXYSeries::y(int pos) const
190 {
190 {
191 if (m_model)
191 if (m_model)
192 if (m_mapOrientation == Qt::Vertical)
192 if (m_mapOrientation == Qt::Vertical)
193 // consecutive data is read from model's column
193 // consecutive data is read from model's column
194 return m_model->data(m_model->index(pos + m_mapFirst, m_mapY), Qt::DisplayRole).toDouble();
194 return m_model->data(m_model->index(pos + m_mapFirst, m_mapY), Qt::DisplayRole).toDouble();
195 else
195 else
196 // consecutive data is read from model's row
196 // consecutive data is read from model's row
197 return m_model->data(m_model->index(m_mapY, pos + m_mapFirst), Qt::DisplayRole).toDouble();
197 return m_model->data(m_model->index(m_mapY, pos + m_mapFirst), Qt::DisplayRole).toDouble();
198 else
198 else
199 // model is not specified, return the data from series' internal data store
199 // model is not specified, return the data from series' internal data store
200 return m_y.at(pos);
200 return m_y.at(pos);
201 }
201 }
202
202
203 /*!
203 /*!
204 Returns number of data points within series.
204 Returns number of data points within series.
205 */
205 */
206 int QXYSeries::count() const
206 int QXYSeries::count() const
207 {
207 {
208 Q_ASSERT(m_x.size() == m_y.size());
208 Q_ASSERT(m_x.size() == m_y.size());
209
209
210 if (m_model) {
210 if (m_model) {
211 if (m_mapOrientation == Qt::Vertical)
211 if (m_mapOrientation == Qt::Vertical)
212 {
212 {
213 // data is in a column. Return the number of mapped items if the model's column have enough items
213 // data is in a column. Return the number of mapped items if the model's column have enough items
214 // or the number of items that can be mapped
214 // or the number of items that can be mapped
215 if (m_mapLimited)
215 if (m_mapLimited)
216 return qMin(m_mapCount, qMax(m_model->rowCount() - m_mapFirst, 0));
216 return qMin(m_mapCount, qMax(m_model->rowCount() - m_mapFirst, 0));
217 else
217 else
218 return qMax(m_model->rowCount() - m_mapFirst, 0);
218 return qMax(m_model->rowCount() - m_mapFirst, 0);
219 }
219 }
220 else
220 else
221 {
221 {
222 // data is in a row. Return the number of mapped items if the model's row have enough items
222 // data is in a row. Return the number of mapped items if the model's row have enough items
223 // or the number of items that can be mapped
223 // or the number of items that can be mapped
224 if (m_mapLimited)
224 if (m_mapLimited)
225 return qMin(m_mapCount, qMax(m_model->columnCount() - m_mapFirst, 0));
225 return qMin(m_mapCount, qMax(m_model->columnCount() - m_mapFirst, 0));
226 else
226 else
227 return qMax(m_model->columnCount() - m_mapFirst, 0);
227 return qMax(m_model->columnCount() - m_mapFirst, 0);
228 }
228 }
229 }
229 }
230
230
231 // model is not specified, return the number of points in the series internal data store
231 // model is not specified, return the number of points in the series internal data store
232 return m_x.size();
232 return m_x.size();
233 }
233 }
234
234
235 /*!
235 /*!
236 Returns the data points of the series.
236 Returns the data points of the series.
237 */
237 */
238 QList<QPointF> QXYSeries::data()
238 QList<QPointF> QXYSeries::data()
239 {
239 {
240 QList<QPointF> data;
240 QList<QPointF> data;
241 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
241 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
242 data.append(QPointF(m_x.at(i), m_y.at(i)));
242 data.append(QPointF(m_x.at(i), m_y.at(i)));
243 return data;
243 return data;
244 }
244 }
245
245
246
246
247 /*!
247 /*!
248 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
248 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
249 pen from chart theme is used.
249 pen from chart theme is used.
250 \sa QChart::setChartTheme()
250 \sa QChart::setChartTheme()
251 */
251 */
252 void QXYSeries::setPen(const QPen& pen)
252 void QXYSeries::setPen(const QPen& pen)
253 {
253 {
254 if(pen!=m_pen){
254 if(pen!=m_pen){
255 m_pen=pen;
255 m_pen=pen;
256 emit updated();
256 emit updated();
257 }
257 }
258 }
258 }
259
259
260 /*!
260 /*!
261 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
261 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
262 from chart theme setting is used.
262 from chart theme setting is used.
263 \sa QChart::setChartTheme()
263 \sa QChart::setChartTheme()
264 */
264 */
265
265
266 void QXYSeries::setBrush(const QBrush& brush)
266 void QXYSeries::setBrush(const QBrush& brush)
267 {
267 {
268 if(brush!=m_brush){
268 if(brush!=m_brush){
269 m_brush=brush;
269 m_brush=brush;
270 emit updated();
270 emit updated();
271 }
271 }
272 }
272 }
273
273
274
274
275 /*!
275 /*!
276 Stream operator for adding a data \a point to the series.
276 Stream operator for adding a data \a point to the series.
277 \sa add()
277 \sa add()
278 */
278 */
279
279
280 QXYSeries& QXYSeries::operator<< (const QPointF &point)
280 QXYSeries& QXYSeries::operator<< (const QPointF &point)
281 {
281 {
282 add(point);
282 add(point);
283 return *this;
283 return *this;
284 }
284 }
285
285
286
286
287 /*!
287 /*!
288 Stream operator for adding a list of \a points to the series.
288 Stream operator for adding a list of \a points to the series.
289 \sa add()
289 \sa add()
290 */
290 */
291
291
292 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
292 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
293 {
293 {
294 add(points);
294 add(points);
295 return *this;
295 return *this;
296 }
296 }
297
297
298
298
299 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
299 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
300 {
300 {
301 Q_UNUSED(bottomRight)
301 Q_UNUSED(bottomRight)
302
302
303 if (m_mapOrientation == Qt::Vertical)
303 if (m_mapOrientation == Qt::Vertical)
304 {
304 {
305 if (topLeft.row() >= m_mapFirst && (!m_mapLimited || topLeft.row() < m_mapFirst + m_mapCount))
305 if (topLeft.row() >= m_mapFirst && (!m_mapLimited || topLeft.row() < m_mapFirst + m_mapCount))
306 emit pointReplaced(topLeft.row() - m_mapFirst);
306 emit pointReplaced(topLeft.row() - m_mapFirst);
307 }
307 }
308 else
308 else
309 {
309 {
310 if (topLeft.column() >= m_mapFirst && (!m_mapLimited || topLeft.column() < m_mapFirst + m_mapCount))
310 if (topLeft.column() >= m_mapFirst && (!m_mapLimited || topLeft.column() < m_mapFirst + m_mapCount))
311 emit pointReplaced(topLeft.column() - m_mapFirst);
311 emit pointReplaced(topLeft.column() - m_mapFirst);
312 }
312 }
313 }
313 }
314
314
315 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
315 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
316 {
316 {
317 Q_UNUSED(parent)
317 Q_UNUSED(parent)
318 // Q_UNUSED(end)
318 // Q_UNUSED(end)
319
319
320 if (m_mapLimited)
320 if (m_mapLimited)
321 {
321 {
322 if (start >= m_mapFirst + m_mapCount)
322 if (start >= m_mapFirst + m_mapCount)
323 // the added data is below mapped area
323 // the added data is below mapped area
324 // therefore it has no relevance
324 // therefore it has no relevance
325 return;
325 return;
326 else
326 else
327 {
327 {
328 // the added data is in the mapped area or before it and update is needed
328 // the added data is in the mapped area or before it and update is needed
329
329
330 // check how many mapped items there is currently (before new items are added)
330 // check how many mapped items there is currently (before new items are added)
331 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
331 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
332 // internal storage before new ones can be added
332 // internal storage before new ones can be added
333
334 int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1);
333 if (m_mapCount == count())
335 if (m_mapCount == count())
334 for (int i = 0; i < qMin(count(), end - start + 1); i++)
336 for (int i = 0; i < itemsToRemove; i++)
335 emit pointRemoved(count() - 1 - i);
337 emit pointRemoved(count() - 1 - i);
336 }
338 }
337 }
339 }
338 else
340 else
339 {
341 {
340 // map is not limited (it includes all the items starting from m_mapFirst till the end of model)
342 // map is not limited (it includes all the items starting from m_mapFirst till the end of model)
341 // nothing to do
343 // nothing to do
342 // emit pointAdded(qMax(start - m_mapFirst, 0));
344 // emit pointAdded(qMax(start - m_mapFirst, 0));
343 }
345 }
344 }
346 }
345
347
346 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
348 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
347 {
349 {
348 Q_UNUSED(parent)
350 Q_UNUSED(parent)
349 // Q_UNUSED(end)
351 // Q_UNUSED(end)
350
352
351 if (m_mapLimited)
353 if (m_mapLimited)
352 {
354 {
353 if (start >= m_mapFirst + m_mapCount)
355 if (start >= m_mapFirst + m_mapCount)
354 // the added data is below mapped area
356 // the added data is below mapped area
355 // therefore it has no relevance
357 // therefore it has no relevance
356 return;
358 return;
357 else
359 else
358 {
360 {
359 // the added data is in the mapped area or before it
361 // the added data is in the mapped area or before it
360 // update needed
362 // update needed
361 if (count() > 0)
363 if (count() > 0)
362 for (int i = 0; i < qMin(m_mapCount, end - start + 1); i++)
364 for (int i = 0; i < qMin(m_mapCount - (start - m_mapFirst), end - start + 1); i++)
363 emit pointAdded(qMax(start + i - m_mapFirst, 0));
365 emit pointAdded(qMax(start + i - m_mapFirst, 0));
364 }
366 }
365 }
367 }
366 else
368 else
367 {
369 {
368 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
370 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
369 for (int i = 0; i < end - start + 1; i++)
371 for (int i = 0; i < end - start + 1; i++)
370 emit pointAdded(qMax(start + i - m_mapFirst, 0));
372 emit pointAdded(qMax(start + i - m_mapFirst, 0));
371 }
373 }
372 }
374 }
373
375
374 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end)
376 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end)
375 {
377 {
376 Q_UNUSED(parent)
378 Q_UNUSED(parent)
377 // Q_UNUSED(end)
379 // Q_UNUSED(end)
378
380
379 if (m_mapLimited)
381 if (m_mapLimited)
380 {
382 {
381 if (start >= m_mapFirst + m_mapCount)
383 if (start >= m_mapFirst + m_mapCount)
382 // the removed data is below mapped area
384 // the removed data is below mapped area
383 // therefore it has no relevance
385 // therefore it has no relevance
384 return;
386 return;
385 else
387 else
386 {
388 {
387 // the removed data is in the mapped area or before it
389 // the removed data is in the mapped area or before it
388 // update needed
390 // update needed
389 int itemsToRemove = qMin(count(), end - start + 1);
391
390 tempItemsRemoved = itemsToRemove;
392 // check how many items need to be removed from the xychartitem storage
391 int z = count();
393 // the number equals the number of items that are removed and that lay before
392 z = z;
394 // or in the mapped area. Items that lay beyond the map do not count
393 // if (itemsToRemove > 0)
395 // the max is the current number of items in storage (count())
394 // {
396 int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
395 for (int i = 0; i < itemsToRemove; i++)
397 for (int i = 0; i < itemsToRemove; i++)
396 emit pointRemoved(qMax(start - m_mapFirst, 0));
398 emit pointRemoved(qMax(start - m_mapFirst, 0));
397 }
399 }
398 }
400 }
399 else
401 else
400 {
402 {
401 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
403 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
402 for (int i = 0; i < end - start + 1; i++)
404 for (int i = 0; i < end - start + 1; i++)
403 emit pointRemoved(qMax(start - m_mapFirst, 0));
405 emit pointRemoved(qMax(start - m_mapFirst, 0));
404 }
406 }
405 }
407 }
406
408
407 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
409 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
408 {
410 {
409 Q_UNUSED(parent)
411 Q_UNUSED(parent)
410 Q_UNUSED(end)
412 Q_UNUSED(end)
411
413
412 // how many items there were before data was removed
414 // how many items there were before data was removed
413 // int oldCount = count() - 1;
415 // int oldCount = count() - 1;
414
416
415 if (m_mapLimited)
417 if (m_mapLimited)
416 {
418 {
417 if (start >= m_mapFirst + m_mapCount)
419 if (start >= m_mapFirst + m_mapCount)
418 // the removed data is below mapped area
420 // the removed data is below mapped area
419 // therefore it has no relevance
421 // therefore it has no relevance
420 return;
422 return;
421 else
423 else
422 {
424 {
423 // if there are excess items available (below the map) use them to repopulate mapped area
425 // if the current items count in the whole model is bigger than the index of the last item
426 // that was removed than it means there are some extra items available
424 int extraItemsAvailable = 0;
427 int extraItemsAvailable = 0;
425 if (m_mapOrientation == Qt::Vertical)
428 if (m_mapOrientation == Qt::Vertical)
426 {
429 {
427 extraItemsAvailable = qMax(m_model->rowCount() - m_mapFirst, 0);
430 extraItemsAvailable = qMax(m_model->rowCount() - end, 0);
428 }
431 }
429 else
432 else
430 {
433 {
431 extraItemsAvailable = qMax(m_model->columnCount() - m_mapFirst, 0);
434 extraItemsAvailable = qMax(m_model->columnCount() - end, 0);
432 }
435 }
433
436
434 // int extraItemsNeeded = qMin(extraItemsAvailable, tempItemsRemoved);
437 // if there are excess items available (below the mapped area) use them to repopulate mapped area
435 for (int k = 0; k < tempItemsRemoved; k++)
438 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1);
436 if (start - m_mapFirst + k < extraItemsAvailable)
439 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
437 {
440 for (int k = 0; k < toBeAdded; k++)
438 emit pointAdded(count() - m_mapFirst + k);
441 emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k);
439 }
440 }
442 }
441 }
443 }
442 else
444 else
443 {
445 {
444 // emit pointRemoved(qMax(start - m_mapFirst, 0));
446 // data was removed from XYSeries interal storage. Nothing more to do
445 }
447 }
446 }
448 }
447
449
448 bool QXYSeries::setModel(QAbstractItemModel* model) {
450 bool QXYSeries::setModel(QAbstractItemModel* model) {
449
451
450 // disconnect signals from old model
452 // disconnect signals from old model
451 if(m_model)
453 if(m_model)
452 {
454 {
453 disconnect(m_model, 0, this, 0);
455 disconnect(m_model, 0, this, 0);
454 m_mapX = -1;
456 m_mapX = -1;
455 m_mapY = -1;
457 m_mapY = -1;
456 m_mapFirst = 0;
458 m_mapFirst = 0;
457 m_mapCount = 0;
459 m_mapCount = 0;
458 m_mapLimited = false;
460 m_mapLimited = false;
459 m_mapOrientation = Qt::Vertical;
461 m_mapOrientation = Qt::Vertical;
460 }
462 }
461
463
462 // set new model
464 // set new model
463 if(model)
465 if(model)
464 {
466 {
465 m_model = model;
467 m_model = model;
466 return true;
468 return true;
467 }
469 }
468 else
470 else
469 {
471 {
470 m_model = NULL;
472 m_model = NULL;
471 return false;
473 return false;
472 }
474 }
473 }
475 }
474
476
475 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
477 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
476 {
478 {
477 if (m_model == NULL)
479 if (m_model == NULL)
478 return;
480 return;
479 m_mapX = modelX;
481 m_mapX = modelX;
480 m_mapY = modelY;
482 m_mapY = modelY;
481 m_mapFirst = 0;
483 m_mapFirst = 0;
482 m_mapOrientation = orientation;
484 m_mapOrientation = orientation;
483 if (m_mapOrientation == Qt::Vertical)
485 if (m_mapOrientation == Qt::Vertical)
484 {
486 {
485 // m_mapCount = m_model->rowCount();
487 // m_mapCount = m_model->rowCount();
486 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
488 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
487 connect(m_model,SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
489 connect(m_model,SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
488 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
490 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
489 connect(m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
491 connect(m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
490 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
492 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
491 }
493 }
492 else
494 else
493 {
495 {
494 // m_mapCount = m_model->columnCount();
496 // m_mapCount = m_model->columnCount();
495 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
497 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
496 connect(m_model,SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
498 connect(m_model,SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
497 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
499 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
498 connect(m_model, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
500 connect(m_model, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
499 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
501 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
500 }
502 }
501 }
503 }
502
504
503 void QXYSeries::setModelMappingShift(int first, int count)
505 void QXYSeries::setModelMappingShift(int first, int count)
504 {
506 {
505 m_mapFirst = first;
507 m_mapFirst = first;
506 if (count == 0)
508 if (count == 0)
507 m_mapLimited = false;
509 m_mapLimited = false;
508 else
510 else
509 {
511 {
510 m_mapCount = count;
512 m_mapCount = count;
511 m_mapLimited = true;
513 m_mapLimited = true;
512 }
514 }
513 }
515 }
514
516
515 #include "moc_qxyseries.cpp"
517 #include "moc_qxyseries.cpp"
516
518
517 QTCOMMERCIALCHART_END_NAMESPACE
519 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now