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