##// END OF EJS Templates
Added some tests for BarModelMapper
Marek Rosa -
r1364:7e4ecbbc6885
parent child
Show More
@@ -0,0 +1,8
1 !include( ../auto.pri ) {
2 error( "Couldn't find the auto.pri file!" )
3 }
4
5 SOURCES += \
6 tst_qbarmodelmapper.cpp
7
8 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_AUTOTESTS_BIN_DIR"
@@ -0,0 +1,266
1 #include <QtCore/QString>
2 #include <QtTest/QtTest>
3
4 #include <qchart.h>
5 #include <qchartview.h>
6 #include <qgroupedbarseries.h>
7 #include <qbarset.h>
8 #include <qvbarmodelmapper.h>
9 #include <qhbarmodelmapper.h>
10 #include <QStandardItemModel>
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 class tst_qbarmodelmapper : public QObject
15 {
16 Q_OBJECT
17
18 public:
19 tst_qbarmodelmapper();
20
21 private Q_SLOTS:
22 void initTestCase();
23 void cleanupTestCase();
24 void init();
25 void cleanup();
26 void verticalMapper_data();
27 void verticalMapper();
28 void verticalMapperCustomMapping_data();
29 void verticalMapperCustomMapping();
30 void horizontalMapper_data();
31 void horizontalMapper();
32 void horizontalMapperCustomMapping_data();
33 void horizontalMapperCustomMapping();
34
35 private:
36 QStandardItemModel *m_model;
37 int m_modelRowCount;
38 int m_modelColumnCount;
39
40 QGroupedBarSeries *m_series;
41 QChart *m_chart;
42 };
43
44 tst_qbarmodelmapper::tst_qbarmodelmapper():
45 m_model(0),
46 m_modelRowCount(10),
47 m_modelColumnCount(8)
48 {
49 }
50
51 void tst_qbarmodelmapper::init()
52 {
53 // m_series = new QGroupedBarSeries;
54 // m_chart->addSeries(m_series);
55 }
56
57 void tst_qbarmodelmapper::cleanup()
58 {
59 m_chart->removeSeries(m_series);
60 delete m_series;
61 m_series = 0;
62 }
63
64 void tst_qbarmodelmapper::initTestCase()
65 {
66 m_chart = new QChart;
67 QChartView *chartView = new QChartView(m_chart);
68 chartView->show();
69
70 m_model = new QStandardItemModel(this);
71 for (int row = 0; row < m_modelRowCount; ++row) {
72 for (int column = 0; column < m_modelColumnCount; column++) {
73 QStandardItem *item = new QStandardItem(row * column);
74 m_model->setItem(row, column, item);
75 }
76 }
77 }
78
79 void tst_qbarmodelmapper::cleanupTestCase()
80 {
81 m_model->clear();
82 }
83
84 void tst_qbarmodelmapper::verticalMapper_data()
85 {
86 QTest::addColumn<int>("firstBarSetColumn");
87 QTest::addColumn<int>("lastBarSetColumn");
88 QTest::addColumn<int>("expectedBarSetCount");
89 QTest::newRow("lastBarSetColumn greater than firstBarSetColumn") << 0 << 1 << 2;
90 QTest::newRow("lastBarSetColumn equal to firstBarSetColumn") << 1 << 1 << 1;
91 QTest::newRow("lastBarSetColumn lesser than firstBarSetColumn") << 1 << 0 << 0;
92 QTest::newRow("invalid firstBarSetColumn and correct lastBarSetColumn") << -3 << 1 << 0;
93 QTest::newRow("firstBarSetColumn beyond the size of model and correct lastBarSetColumn") << m_modelColumnCount << 1 << 0;
94 QTest::newRow("firstBarSetColumn beyond the size of model and invalid lastBarSetColumn") << m_modelColumnCount << -1 << 0;
95 }
96
97 void tst_qbarmodelmapper::verticalMapper()
98 {
99 QFETCH(int, firstBarSetColumn);
100 QFETCH(int, lastBarSetColumn);
101 QFETCH(int, expectedBarSetCount);
102
103 m_series = new QGroupedBarSeries;
104
105 QVBarModelMapper *mapper = new QVBarModelMapper;
106 mapper->setFirstBarSetColumn(firstBarSetColumn);
107 mapper->setLastBarSetColumn(lastBarSetColumn);
108 mapper->setModel(m_model);
109 mapper->setSeries(m_series);
110
111 m_chart->addSeries(m_series);
112
113 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
114 QCOMPARE(mapper->firstBarSetColumn(), qMax(-1, firstBarSetColumn));
115 QCOMPARE(mapper->lastBarSetColumn(), qMax(-1, lastBarSetColumn));
116
117 delete mapper;
118 mapper = 0;
119 }
120
121 void tst_qbarmodelmapper::verticalMapperCustomMapping_data()
122 {
123 QTest::addColumn<int>("first");
124 QTest::addColumn<int>("countLimit");
125 QTest::addColumn<int>("expectedBarSetCount");
126 QTest::addColumn<int>("expectedCount");
127 QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelRowCount;
128 QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelRowCount - 3;
129 QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelRowCount);
130 QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelRowCount - 3);
131 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0 << 0;
132 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0 << 0;
133 QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelRowCount + 3 << 2 << m_modelRowCount;
134 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelRowCount;
135 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelRowCount;
136 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelRowCount;
137 }
138
139 void tst_qbarmodelmapper::verticalMapperCustomMapping()
140 {
141 QFETCH(int, first);
142 QFETCH(int, countLimit);
143 QFETCH(int, expectedBarSetCount);
144 QFETCH(int, expectedCount);
145
146 m_series = new QGroupedBarSeries;
147
148 QCOMPARE(m_series->barsetCount(), 0);
149
150 QVBarModelMapper *mapper = new QVBarModelMapper;
151 mapper->setFirstBarSetColumn(0);
152 mapper->setLastBarSetColumn(1);
153 mapper->setModel(m_model);
154 mapper->setSeries(m_series);
155 mapper->setFirst(first);
156 mapper->setCount(countLimit);
157 m_chart->addSeries(m_series);
158
159 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
160
161 if (expectedBarSetCount > 0)
162 QCOMPARE(m_series->barSets().first()->count(), expectedCount);
163
164 // change values column mapping to invalid
165 mapper->setFirstBarSetColumn(-1);
166 mapper->setLastBarSetColumn(1);
167
168 QCOMPARE(m_series->barsetCount(), 0);
169
170 delete mapper;
171 mapper = 0;
172 }
173
174 void tst_qbarmodelmapper::horizontalMapper_data()
175 {
176 QTest::addColumn<int>("firstBarSetRow");
177 QTest::addColumn<int>("lastBarSetRow");
178 QTest::addColumn<int>("expectedBarSetCount");
179 QTest::newRow("lastBarSetRow greater than firstBarSetRow") << 0 << 1 << 2;
180 QTest::newRow("lastBarSetRow equal to firstBarSetRow") << 1 << 1 << 1;
181 QTest::newRow("lastBarSetRow lesser than firstBarSetRow") << 1 << 0 << 0;
182 QTest::newRow("invalid firstBarSetRow and correct lastBarSetRow") << -3 << 1 << 0;
183 QTest::newRow("firstBarSetRow beyond the size of model and correct lastBarSetRow") << m_modelRowCount << 1 << 0;
184 QTest::newRow("firstBarSetRow beyond the size of model and invalid lastBarSetRow") << m_modelRowCount << -1 << 0;
185 }
186
187 void tst_qbarmodelmapper::horizontalMapper()
188 {
189 QFETCH(int, firstBarSetRow);
190 QFETCH(int, lastBarSetRow);
191 QFETCH(int, expectedBarSetCount);
192
193 m_series = new QGroupedBarSeries;
194
195 QHBarModelMapper *mapper = new QHBarModelMapper;
196 mapper->setFirstBarSetRow(firstBarSetRow);
197 mapper->setLastBarSetRow(lastBarSetRow);
198 mapper->setModel(m_model);
199 mapper->setSeries(m_series);
200
201 m_chart->addSeries(m_series);
202
203 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
204 QCOMPARE(mapper->firstBarSetRow(), qMax(-1, firstBarSetRow));
205 QCOMPARE(mapper->lastBarSetRow(), qMax(-1, lastBarSetRow));
206
207 delete mapper;
208 mapper = 0;
209 }
210
211 void tst_qbarmodelmapper::horizontalMapperCustomMapping_data()
212 {
213 QTest::addColumn<int>("first");
214 QTest::addColumn<int>("countLimit");
215 QTest::addColumn<int>("expectedBarSetCount");
216 QTest::addColumn<int>("expectedCount");
217 QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelColumnCount;
218 QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelColumnCount - 3;
219 QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelColumnCount);
220 QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelColumnCount - 3);
221 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0 << 0;
222 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0 << 0;
223 QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << 2 << m_modelColumnCount;
224 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelColumnCount;
225 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelColumnCount;
226 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelColumnCount;
227 }
228
229 void tst_qbarmodelmapper::horizontalMapperCustomMapping()
230 {
231 QFETCH(int, first);
232 QFETCH(int, countLimit);
233 QFETCH(int, expectedBarSetCount);
234 QFETCH(int, expectedCount);
235
236 m_series = new QGroupedBarSeries;
237
238 QCOMPARE(m_series->barsetCount(), 0);
239
240 QHBarModelMapper *mapper = new QHBarModelMapper;
241 mapper->setFirstBarSetRow(0);
242 mapper->setLastBarSetRow(1);
243 mapper->setModel(m_model);
244 mapper->setSeries(m_series);
245 mapper->setFirst(first);
246 mapper->setCount(countLimit);
247 m_chart->addSeries(m_series);
248
249 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
250
251 if (expectedBarSetCount > 0)
252 QCOMPARE(m_series->barSets().first()->count(), expectedCount);
253
254 // change values column mapping to invalid
255 mapper->setFirstBarSetRow(-1);
256 mapper->setLastBarSetRow(1);
257
258 QCOMPARE(m_series->barsetCount(), 0);
259
260 delete mapper;
261 mapper = 0;
262 }
263
264 QTEST_MAIN(tst_qbarmodelmapper)
265
266 #include "tst_qbarmodelmapper.moc"
@@ -1,483 +1,483
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarmodelmapper.h"
21 #include "qbarmodelmapper.h"
22 #include "qbarmodelmapper_p.h"
22 #include "qbarmodelmapper_p.h"
23 #include "qbarseries.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QAbstractItemModel>
27 #include <QAbstractItemModel>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 /*!
31 /*!
32 \property QBarModelMapper::series
32 \property QBarModelMapper::series
33 \brief Defines the QPieSeries object that is used by the mapper.
33 \brief Defines the QPieSeries object that is used by the mapper.
34
34
35 All the data in the series in the series is discarded when it is set to the mapper.
35 All the data in the series in the series is discarded when it is set to the mapper.
36 When new series is specified the old series is disconnected (it preserves its data)
36 When new series is specified the old series is disconnected (it preserves its data)
37 */
37 */
38
38
39 /*!
39 /*!
40 \property QBarModelMapper::model
40 \property QBarModelMapper::model
41 \brief Defines the model that is used by the mapper.
41 \brief Defines the model that is used by the mapper.
42 */
42 */
43
43
44 /*!
44 /*!
45 \property QBarModelMapper::first
45 \property QBarModelMapper::first
46 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
46 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
47
47
48 Minimal and default value is: 0
48 Minimal and default value is: 0
49 */
49 */
50
50
51 /*!
51 /*!
52 \property QBarModelMapper::count
52 \property QBarModelMapper::count
53 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
53 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
54
54
55 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
55 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
56 */
56 */
57
57
58 /*!
58 /*!
59 \class QBarModelMapper
59 \class QBarModelMapper
60 \brief part of QtCommercial chart API.
60 \brief part of QtCommercial chart API.
61 \mainclass
61 \mainclass
62
62
63 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
63 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
64 Curently it is NOT possible to use both QAbstractItemModel and QXYSeries model API.
64 Curently it is NOT possible to use both QAbstractItemModel and QXYSeries model API.
65 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
65 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
66 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
66 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
67 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
67 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
68 */
68 */
69
69
70 /*!
70 /*!
71 Constructs a mapper object which is a child of \a parent.
71 Constructs a mapper object which is a child of \a parent.
72 */
72 */
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
74 QObject(parent),
74 QObject(parent),
75 d_ptr(new QBarModelMapperPrivate(this))
75 d_ptr(new QBarModelMapperPrivate(this))
76 {
76 {
77 }
77 }
78
78
79 QAbstractItemModel* QBarModelMapper::model() const
79 QAbstractItemModel* QBarModelMapper::model() const
80 {
80 {
81 Q_D(const QBarModelMapper);
81 Q_D(const QBarModelMapper);
82 return d->m_model;
82 return d->m_model;
83 }
83 }
84
84
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
86 {
86 {
87 if (model == 0)
87 if (model == 0)
88 return;
88 return;
89
89
90 Q_D(QBarModelMapper);
90 Q_D(QBarModelMapper);
91 if (d->m_model) {
91 if (d->m_model) {
92 disconnect(d->m_model, 0, d, 0);
92 disconnect(d->m_model, 0, d, 0);
93 }
93 }
94
94
95 d->m_model = model;
95 d->m_model = model;
96 d->initializeBarFromModel();
96 d->initializeBarFromModel();
97 // connect signals from the model
97 // connect signals from the model
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
99 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
99 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
103 }
103 }
104
104
105 QBarSeries* QBarModelMapper::series() const
105 QBarSeries* QBarModelMapper::series() const
106 {
106 {
107 Q_D(const QBarModelMapper);
107 Q_D(const QBarModelMapper);
108 return d->m_series;
108 return d->m_series;
109 }
109 }
110
110
111 void QBarModelMapper::setSeries(QBarSeries *series)
111 void QBarModelMapper::setSeries(QBarSeries *series)
112 {
112 {
113 Q_D(QBarModelMapper);
113 Q_D(QBarModelMapper);
114 if (d->m_series) {
114 if (d->m_series) {
115 disconnect(d->m_series, 0, d, 0);
115 disconnect(d->m_series, 0, d, 0);
116 }
116 }
117
117
118 if (series == 0)
118 if (series == 0)
119 return;
119 return;
120
120
121 d->m_series = series;
121 d->m_series = series;
122 d->initializeBarFromModel();
122 d->initializeBarFromModel();
123 // connect the signals from the series
123 // connect the signals from the series
124 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
124 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
125 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
125 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
126 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
126 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
127 }
127 }
128
128
129 int QBarModelMapper::first() const
129 int QBarModelMapper::first() const
130 {
130 {
131 Q_D(const QBarModelMapper);
131 Q_D(const QBarModelMapper);
132 return d->m_first;
132 return d->m_first;
133 }
133 }
134
134
135 void QBarModelMapper::setFirst(int first)
135 void QBarModelMapper::setFirst(int first)
136 {
136 {
137 Q_D(QBarModelMapper);
137 Q_D(QBarModelMapper);
138 d->m_first = qMax(first, 0);
138 d->m_first = qMax(first, 0);
139 d->initializeBarFromModel();
139 d->initializeBarFromModel();
140 }
140 }
141
141
142 int QBarModelMapper::count() const
142 int QBarModelMapper::count() const
143 {
143 {
144 Q_D(const QBarModelMapper);
144 Q_D(const QBarModelMapper);
145 return d->m_count;
145 return d->m_count;
146 }
146 }
147
147
148 void QBarModelMapper::setCount(int count)
148 void QBarModelMapper::setCount(int count)
149 {
149 {
150 Q_D(QBarModelMapper);
150 Q_D(QBarModelMapper);
151 d->m_count = qMax(count, -1);
151 d->m_count = qMax(count, -1);
152 d->initializeBarFromModel();
152 d->initializeBarFromModel();
153 }
153 }
154
154
155 /*!
155 /*!
156 Returns the orientation that is used when QBarModelMapper accesses the model.
156 Returns the orientation that is used when QBarModelMapper accesses the model.
157 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
158 or from columns (Qt::Vertical)
158 or from columns (Qt::Vertical)
159 */
159 */
160 Qt::Orientation QBarModelMapper::orientation() const
160 Qt::Orientation QBarModelMapper::orientation() const
161 {
161 {
162 Q_D(const QBarModelMapper);
162 Q_D(const QBarModelMapper);
163 return d->m_orientation;
163 return d->m_orientation;
164 }
164 }
165
165
166 /*!
166 /*!
167 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 Returns the \a orientation that is used when QBarModelMapper accesses the model.
168 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
169 or from columns (Qt::Vertical)
169 or from columns (Qt::Vertical)
170 */
170 */
171 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
172 {
172 {
173 Q_D(QBarModelMapper);
173 Q_D(QBarModelMapper);
174 d->m_orientation = orientation;
174 d->m_orientation = orientation;
175 d->initializeBarFromModel();
175 d->initializeBarFromModel();
176 }
176 }
177
177
178 /*!
178 /*!
179 Returns which section of the model is used as the data source for the first bar set
179 Returns which section of the model is used as the data source for the first bar set
180 */
180 */
181 int QBarModelMapper::firstBarSetSection() const
181 int QBarModelMapper::firstBarSetSection() const
182 {
182 {
183 Q_D(const QBarModelMapper);
183 Q_D(const QBarModelMapper);
184 return d->m_firstBarSetSection;
184 return d->m_firstBarSetSection;
185 }
185 }
186
186
187 /*!
187 /*!
188 Sets the model section that is used as the data source for the first bar set
188 Sets the model section that is used as the data source for the first bar set
189 Parameter \a firstBarSetSection specifies the section of the model.
189 Parameter \a firstBarSetSection specifies the section of the model.
190 */
190 */
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
192 {
192 {
193 Q_D(QBarModelMapper);
193 Q_D(QBarModelMapper);
194 d->m_firstBarSetSection = firstBarSetSection;
194 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
195 d->initializeBarFromModel();
195 d->initializeBarFromModel();
196 }
196 }
197
197
198 /*!
198 /*!
199 Returns which section of the model is used as the data source for the last bar set
199 Returns which section of the model is used as the data source for the last bar set
200 */
200 */
201 int QBarModelMapper::lastBarSetSection() const
201 int QBarModelMapper::lastBarSetSection() const
202 {
202 {
203 Q_D(const QBarModelMapper);
203 Q_D(const QBarModelMapper);
204 return d->m_lastBarSetSection;
204 return d->m_lastBarSetSection;
205 }
205 }
206
206
207 /*!
207 /*!
208 Sets the model section that is used as the data source for the last bar set
208 Sets the model section that is used as the data source for the last bar set
209 Parameter \a lastBarSetSection specifies the section of the model.
209 Parameter \a lastBarSetSection specifies the section of the model.
210 */
210 */
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
212 {
212 {
213 Q_D(QBarModelMapper);
213 Q_D(QBarModelMapper);
214 d->m_lastBarSetSection = lastBarSetSection;
214 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
215 d->initializeBarFromModel();
215 d->initializeBarFromModel();
216 }
216 }
217
217
218 /*!
218 /*!
219 Resets the QBarModelMapper to the default state.
219 Resets the QBarModelMapper to the default state.
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
221 */
221 */
222 void QBarModelMapper::reset()
222 void QBarModelMapper::reset()
223 {
223 {
224 Q_D(QBarModelMapper);
224 Q_D(QBarModelMapper);
225 d->m_first = 0;
225 d->m_first = 0;
226 d->m_count = -1;
226 d->m_count = -1;
227 d->m_firstBarSetSection = -1;
227 d->m_firstBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
229 d->initializeBarFromModel();
229 d->initializeBarFromModel();
230 }
230 }
231
231
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233
233
234 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
235 m_series(0),
235 m_series(0),
236 m_model(0),
236 m_model(0),
237 m_first(0),
237 m_first(0),
238 m_count(-1),
238 m_count(-1),
239 m_orientation(Qt::Vertical),
239 m_orientation(Qt::Vertical),
240 m_firstBarSetSection(-1),
240 m_firstBarSetSection(-1),
241 m_lastBarSetSection(-1),
241 m_lastBarSetSection(-1),
242 m_seriesSignalsBlock(false),
242 m_seriesSignalsBlock(false),
243 m_modelSignalsBlock(false),
243 m_modelSignalsBlock(false),
244 q_ptr(q)
244 q_ptr(q)
245 {
245 {
246 }
246 }
247
247
248 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 void QBarModelMapperPrivate::blockModelSignals(bool block)
249 {
249 {
250 m_modelSignalsBlock = block;
250 m_modelSignalsBlock = block;
251 }
251 }
252
252
253 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
254 {
254 {
255 m_seriesSignalsBlock = block;
255 m_seriesSignalsBlock = block;
256 }
256 }
257
257
258 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
259 {
259 {
260 if (!index.isValid())
260 if (!index.isValid())
261 return 0;
261 return 0;
262
262
263 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
264 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
265 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
266 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 return m_series->barSets().at(index.column() - m_firstBarSetSection);
267 // else
267 // else
268 // return 0;
268 // return 0;
269 }
269 }
270 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
271 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
272 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 return m_series->barSets().at(index.row() - m_firstBarSetSection);
273 }
273 }
274 return 0; // This part of model has not been mapped to any slice
274 return 0; // This part of model has not been mapped to any slice
275 }
275 }
276
276
277 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
278 {
278 {
279 if (m_count != -1 && posInBar >= m_count)
279 if (m_count != -1 && posInBar >= m_count)
280 return QModelIndex(); // invalid
280 return QModelIndex(); // invalid
281
281
282 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
283 return QModelIndex(); // invalid
283 return QModelIndex(); // invalid
284
284
285 if (m_orientation == Qt::Vertical)
285 if (m_orientation == Qt::Vertical)
286 return m_model->index(posInBar + m_first, barSection);
286 return m_model->index(posInBar + m_first, barSection);
287 else
287 else
288 return m_model->index(barSection, posInBar + m_first);
288 return m_model->index(barSection, posInBar + m_first);
289 }
289 }
290
290
291 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
292 {
292 {
293 Q_UNUSED(topLeft)
293 Q_UNUSED(topLeft)
294 Q_UNUSED(bottomRight)
294 Q_UNUSED(bottomRight)
295
295
296 if (m_model == 0 || m_series == 0)
296 if (m_model == 0 || m_series == 0)
297 return;
297 return;
298
298
299 if (m_modelSignalsBlock)
299 if (m_modelSignalsBlock)
300 return;
300 return;
301
301
302 blockSeriesSignals();
302 blockSeriesSignals();
303 QModelIndex index;
303 QModelIndex index;
304 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
305 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
306 index = topLeft.sibling(row, column);
306 index = topLeft.sibling(row, column);
307 QBarSet* bar = barSet(index);
307 QBarSet* bar = barSet(index);
308 if (bar) {
308 if (bar) {
309 if (m_orientation == Qt::Vertical)
309 if (m_orientation == Qt::Vertical)
310 bar->replace(row - m_first, m_model->data(index).toReal());
310 bar->replace(row - m_first, m_model->data(index).toReal());
311 else
311 else
312 bar->replace(column - m_first, m_model->data(index).toReal());
312 bar->replace(column - m_first, m_model->data(index).toReal());
313 }
313 }
314 }
314 }
315 }
315 }
316 blockSeriesSignals(false);
316 blockSeriesSignals(false);
317 }
317 }
318
318
319 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
319 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
320 {
320 {
321 Q_UNUSED(parent);
321 Q_UNUSED(parent);
322 Q_UNUSED(end)
322 Q_UNUSED(end)
323 if (m_modelSignalsBlock)
323 if (m_modelSignalsBlock)
324 return;
324 return;
325
325
326 blockSeriesSignals();
326 blockSeriesSignals();
327 if (m_orientation == Qt::Vertical)
327 if (m_orientation == Qt::Vertical)
328 // insertData(start, end);
328 // insertData(start, end);
329 initializeBarFromModel();
329 initializeBarFromModel();
330 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
330 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
331 initializeBarFromModel();
331 initializeBarFromModel();
332 blockSeriesSignals(false);
332 blockSeriesSignals(false);
333 }
333 }
334
334
335 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
335 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
336 {
336 {
337 Q_UNUSED(parent);
337 Q_UNUSED(parent);
338 Q_UNUSED(end)
338 Q_UNUSED(end)
339 if (m_modelSignalsBlock)
339 if (m_modelSignalsBlock)
340 return;
340 return;
341
341
342 blockSeriesSignals();
342 blockSeriesSignals();
343 if (m_orientation == Qt::Vertical)
343 if (m_orientation == Qt::Vertical)
344 // removeData(start, end);
344 // removeData(start, end);
345 initializeBarFromModel();
345 initializeBarFromModel();
346 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
346 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
347 initializeBarFromModel();
347 initializeBarFromModel();
348 blockSeriesSignals(false);
348 blockSeriesSignals(false);
349 }
349 }
350
350
351 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
351 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
352 {
352 {
353 Q_UNUSED(parent);
353 Q_UNUSED(parent);
354 Q_UNUSED(end)
354 Q_UNUSED(end)
355 if (m_modelSignalsBlock)
355 if (m_modelSignalsBlock)
356 return;
356 return;
357
357
358 blockSeriesSignals();
358 blockSeriesSignals();
359 if (m_orientation == Qt::Horizontal)
359 if (m_orientation == Qt::Horizontal)
360 // insertData(start, end);
360 // insertData(start, end);
361 initializeBarFromModel();
361 initializeBarFromModel();
362 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
362 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
363 initializeBarFromModel();
363 initializeBarFromModel();
364 blockSeriesSignals(false);
364 blockSeriesSignals(false);
365 }
365 }
366
366
367 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
367 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
368 {
368 {
369 Q_UNUSED(parent);
369 Q_UNUSED(parent);
370 Q_UNUSED(end)
370 Q_UNUSED(end)
371 if (m_modelSignalsBlock)
371 if (m_modelSignalsBlock)
372 return;
372 return;
373
373
374 blockSeriesSignals();
374 blockSeriesSignals();
375 if (m_orientation == Qt::Horizontal)
375 if (m_orientation == Qt::Horizontal)
376 // removeData(start, end);
376 // removeData(start, end);
377 initializeBarFromModel();
377 initializeBarFromModel();
378 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
378 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
379 initializeBarFromModel();
379 initializeBarFromModel();
380 blockSeriesSignals(false);
380 blockSeriesSignals(false);
381 }
381 }
382
382
383 void QBarModelMapperPrivate::insertData(int start, int end)
383 void QBarModelMapperPrivate::insertData(int start, int end)
384 {
384 {
385 Q_UNUSED(end)
385 Q_UNUSED(end)
386 if (m_model == 0 || m_series == 0)
386 if (m_model == 0 || m_series == 0)
387 return;
387 return;
388
388
389 if (m_count != -1 && start >= m_first + m_count) {
389 if (m_count != -1 && start >= m_first + m_count) {
390 return;
390 return;
391 } /*else {
391 } /*else {
392 int addedCount = end - start + 1;
392 int addedCount = end - start + 1;
393 if (m_count != -1 && addedCount > m_count)
393 if (m_count != -1 && addedCount > m_count)
394 addedCount = m_count;
394 addedCount = m_count;
395 int first = qMax(start, m_first);
395 int first = qMax(start, m_first);
396 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
396 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
397 for (int k = 0; k < m_series->barSets().count(); k++) {
397 for (int k = 0; k < m_series->barSets().count(); k++) {
398 for (int i = first; i <= last; i++) {
398 for (int i = first; i <= last; i++) {
399 QBar point;
399 QBar point;
400 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
400 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
401 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
401 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
402 m_series->insert(i - m_first, point);
402 m_series->insert(i - m_first, point);
403 }
403 }
404 >>>>>>> Stashed changes
404 >>>>>>> Stashed changes
405 }
405 }
406
406
407 // remove excess of slices (abouve m_count)
407 // remove excess of slices (abouve m_count)
408 if (m_count != -1 && m_series->points().size() > m_count)
408 if (m_count != -1 && m_series->points().size() > m_count)
409 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
409 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
410 m_series->remove(m_series->points().at(i));
410 m_series->remove(m_series->points().at(i));
411 }
411 }
412 }*/
412 }*/
413 }
413 }
414
414
415 void QBarModelMapperPrivate::removeData(int start, int end)
415 void QBarModelMapperPrivate::removeData(int start, int end)
416 {
416 {
417 Q_UNUSED(end)
417 Q_UNUSED(end)
418 if (m_model == 0 || m_series == 0)
418 if (m_model == 0 || m_series == 0)
419 return;
419 return;
420
420
421 // int removedCount = end - start + 1;
421 // int removedCount = end - start + 1;
422 if (m_count != -1 && start >= m_first + m_count) {
422 if (m_count != -1 && start >= m_first + m_count) {
423 return;
423 return;
424 } /*else {
424 } /*else {
425 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
425 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
427 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
427 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
428 for (int i = last; i >= first; i--) {
428 for (int i = last; i >= first; i--) {
429 m_series->remove(m_series->points().at(i - m_first));
429 m_series->remove(m_series->points().at(i - m_first));
430 }
430 }
431
431
432 if (m_count != -1) {
432 if (m_count != -1) {
433 int itemsAvailable; // check how many are available to be added
433 int itemsAvailable; // check how many are available to be added
434 if (m_orientation == Qt::Vertical)
434 if (m_orientation == Qt::Vertical)
435 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
435 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
436 else
436 else
437 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
437 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
438 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
438 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
439 int currentSize = m_series->count();
439 int currentSize = m_series->count();
440 if (toBeAdded > 0)
440 if (toBeAdded > 0)
441 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
441 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
442 QPointF point;
442 QPointF point;
443 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
443 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
444 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
444 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
445 m_series->insert(i, point);
445 m_series->insert(i, point);
446 }
446 }
447 }
447 }
448 }*/
448 }*/
449 }
449 }
450
450
451 void QBarModelMapperPrivate::initializeBarFromModel()
451 void QBarModelMapperPrivate::initializeBarFromModel()
452 {
452 {
453 if (m_model == 0 || m_series == 0)
453 if (m_model == 0 || m_series == 0)
454 return;
454 return;
455
455
456 blockSeriesSignals();
456 blockSeriesSignals();
457 // clear current content
457 // clear current content
458 m_series->clear();
458 m_series->clear();
459
459
460 // create the initial bar sets
460 // create the initial bar sets
461 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
461 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
462 int posInBar = 0;
462 int posInBar = 0;
463 QModelIndex barIndex = barModelIndex(i, posInBar);
463 QModelIndex barIndex = barModelIndex(i, posInBar);
464 // check if there is such model index
464 // check if there is such model index
465 if (barIndex.isValid()) {
465 if (barIndex.isValid()) {
466 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
466 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
467 while (barIndex.isValid()) {
467 while (barIndex.isValid()) {
468 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
468 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
469 posInBar++;
469 posInBar++;
470 barIndex = barModelIndex(i, posInBar);
470 barIndex = barModelIndex(i, posInBar);
471 }
471 }
472 m_series->append(barSet);
472 m_series->append(barSet);
473 } else {
473 } else {
474 break;
474 break;
475 }
475 }
476 }
476 }
477 blockSeriesSignals(false);
477 blockSeriesSignals(false);
478 }
478 }
479
479
480 #include "moc_qbarmodelmapper.cpp"
480 #include "moc_qbarmodelmapper.cpp"
481 #include "moc_qbarmodelmapper_p.cpp"
481 #include "moc_qbarmodelmapper_p.cpp"
482
482
483 QTCOMMERCIALCHART_END_NAMESPACE
483 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,654 +1,654
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \property QBarSeries::barMargin
50 \property QBarSeries::barMargin
51 \brief Defines the margin around bars.
51 \brief Defines the margin around bars.
52
52
53 Value is from 0 to 1 and represents
53 Value is from 0 to 1 and represents
54 percentage of margin compared to bars
54 percentage of margin compared to bars
55 */
55 */
56
56
57 /*!
57 /*!
58 \property QBarSeries::count
58 \property QBarSeries::count
59 \brief Holds the number of sets in series.
59 \brief Holds the number of sets in series.
60 */
60 */
61
61
62 /*!
62 /*!
63 \property QBarSeries::labelsVisible
63 \property QBarSeries::labelsVisible
64 \brief Defines the visibility of the labels in series
64 \brief Defines the visibility of the labels in series
65 */
65 */
66
66
67 /*!
67 /*!
68 \fn void QBarSeries::clicked(QBarSet *barset, int index)
68 \fn void QBarSeries::clicked(QBarSet *barset, int index)
69
69
70 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
70 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
71 Clicked bar inside set is indexed by \a index
71 Clicked bar inside set is indexed by \a index
72 */
72 */
73
73
74 /*!
74 /*!
75 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
75 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
76
76
77 The signal is emitted if mouse is hovered on top of series.
77 The signal is emitted if mouse is hovered on top of series.
78 Parameter \a barset is the pointer of barset, where hover happened.
78 Parameter \a barset is the pointer of barset, where hover happened.
79 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
79 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
80 */
80 */
81
81
82 /*!
82 /*!
83 \fn void QBarSeries::visibleChanged()
83 \fn void QBarSeries::visibleChanged()
84 */
84 */
85
85
86 /*!
86 /*!
87 \fn void QBarSeries::labelsVisibleChanged()
87 \fn void QBarSeries::labelsVisibleChanged()
88
88
89 This signal is emitted when labels visibility have changed.
89 This signal is emitted when labels visibility have changed.
90
90
91 \sa isLabelsVisible(), setLabelsVisible()
91 \sa isLabelsVisible(), setLabelsVisible()
92 */
92 */
93
93
94 /*!
94 /*!
95 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
95 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
96
96
97 This signal is emitted when \a sets have been added to the series.
97 This signal is emitted when \a sets have been added to the series.
98
98
99 \sa append(), insert()
99 \sa append(), insert()
100 */
100 */
101
101
102 /*!
102 /*!
103 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
103 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
104
104
105 This signal is emitted when \a sets have been removed from the series.
105 This signal is emitted when \a sets have been removed from the series.
106
106
107 \sa remove()
107 \sa remove()
108 */
108 */
109
109
110 /*!
110 /*!
111 Constructs empty QBarSeries.
111 Constructs empty QBarSeries.
112 QBarSeries is QObject which is a child of a \a parent.
112 QBarSeries is QObject which is a child of a \a parent.
113 */
113 */
114 QBarSeries::QBarSeries(QObject *parent) :
114 QBarSeries::QBarSeries(QObject *parent) :
115 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
115 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
116 {
116 {
117 }
117 }
118
118
119 /*!
119 /*!
120 Destructs barseries and owned barsets.
120 Destructs barseries and owned barsets.
121 */
121 */
122 QBarSeries::~QBarSeries()
122 QBarSeries::~QBarSeries()
123 {
123 {
124 Q_D(QBarSeries);
124 Q_D(QBarSeries);
125 if(d->m_dataset){
125 if(d->m_dataset){
126 d->m_dataset->removeSeries(this);
126 d->m_dataset->removeSeries(this);
127 }
127 }
128 }
128 }
129
129
130 /*!
130 /*!
131 \internal
131 \internal
132 */
132 */
133 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
133 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
134 QAbstractSeries(d,parent)
134 QAbstractSeries(d,parent)
135 {
135 {
136 }
136 }
137
137
138 /*!
138 /*!
139 Returns the type of series. Derived classes override this.
139 Returns the type of series. Derived classes override this.
140 */
140 */
141 QAbstractSeries::SeriesType QBarSeries::type() const
141 QAbstractSeries::SeriesType QBarSeries::type() const
142 {
142 {
143 return QAbstractSeries::SeriesTypeBar;
143 return QAbstractSeries::SeriesTypeBar;
144 }
144 }
145
145
146 /*!
146 /*!
147 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
147 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
148 percentage of margin compared to bars
148 percentage of margin compared to bars
149 */
149 */
150 void QBarSeries::setBarMargin(qreal margin)
150 void QBarSeries::setBarMargin(qreal margin)
151 {
151 {
152 Q_D(QBarSeries);
152 Q_D(QBarSeries);
153 d->setBarMargin(margin);
153 d->setBarMargin(margin);
154 }
154 }
155
155
156 /*!
156 /*!
157 Returns the margin around bars
157 Returns the margin around bars
158 */
158 */
159 qreal QBarSeries::barMargin() const
159 qreal QBarSeries::barMargin() const
160 {
160 {
161 Q_D(const QBarSeries);
161 Q_D(const QBarSeries);
162 return d->barMargin();
162 return d->barMargin();
163 }
163 }
164
164
165 /*!
165 /*!
166 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
166 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
167 Returns true, if appending succeeded.
167 Returns true, if appending succeeded.
168
168
169 */
169 */
170 bool QBarSeries::append(QBarSet *set)
170 bool QBarSeries::append(QBarSet *set)
171 {
171 {
172 Q_D(QBarSeries);
172 Q_D(QBarSeries);
173 bool success = d->append(set);
173 bool success = d->append(set);
174 if (success) {
174 if (success) {
175 QList<QBarSet*> sets;
175 QList<QBarSet*> sets;
176 sets.append(set);
176 sets.append(set);
177 emit barsetsAdded(sets);
177 emit barsetsAdded(sets);
178 }
178 }
179 return success;
179 return success;
180 }
180 }
181
181
182 /*!
182 /*!
183 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
183 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
184 Returns true, if set was removed.
184 Returns true, if set was removed.
185 */
185 */
186 bool QBarSeries::remove(QBarSet *set)
186 bool QBarSeries::remove(QBarSet *set)
187 {
187 {
188 Q_D(QBarSeries);
188 Q_D(QBarSeries);
189 bool success = d->remove(set);
189 bool success = d->remove(set);
190 if (success) {
190 if (success) {
191 QList<QBarSet*> sets;
191 QList<QBarSet*> sets;
192 sets.append(set);
192 sets.append(set);
193 emit barsetsRemoved(sets);
193 emit barsetsRemoved(sets);
194 }
194 }
195 return success;
195 return success;
196 }
196 }
197
197
198 /*!
198 /*!
199 Adds a list of barsets to series. Takes ownership of \a sets.
199 Adds a list of barsets to series. Takes ownership of \a sets.
200 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
200 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
201 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
201 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
202 and function returns false.
202 and function returns false.
203 */
203 */
204 bool QBarSeries::append(QList<QBarSet* > sets)
204 bool QBarSeries::append(QList<QBarSet* > sets)
205 {
205 {
206 Q_D(QBarSeries);
206 Q_D(QBarSeries);
207 bool success = d->append(sets);
207 bool success = d->append(sets);
208 if (success) {
208 if (success) {
209 emit barsetsAdded(sets);
209 emit barsetsAdded(sets);
210 }
210 }
211 return success;
211 return success;
212 }
212 }
213
213
214 /*!
214 /*!
215 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
215 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
216 */
216 */
217 bool QBarSeries::remove(QList<QBarSet* > sets)
217 bool QBarSeries::remove(QList<QBarSet* > sets)
218 {
218 {
219 Q_D(QBarSeries);
219 Q_D(QBarSeries);
220 bool success = d->remove(sets);
220 bool success = d->remove(sets);
221 if (success) {
221 if (success) {
222 emit barsetsRemoved(sets);
222 emit barsetsRemoved(sets);
223 }
223 }
224 return success;
224 return success;
225 }
225 }
226
226
227 /*!
227 /*!
228 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
228 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
229 Returns true, if inserting succeeded.
229 Returns true, if inserting succeeded.
230
230
231 */
231 */
232 bool QBarSeries::insert(int index, QBarSet *set)
232 bool QBarSeries::insert(int index, QBarSet *set)
233 {
233 {
234 Q_D(QBarSeries);
234 Q_D(QBarSeries);
235 bool success = d->insert(index, set);
235 bool success = d->insert(index, set);
236 if (success) {
236 if (success) {
237 QList<QBarSet*> sets;
237 QList<QBarSet*> sets;
238 sets.append(set);
238 sets.append(set);
239 emit barsetsAdded(sets);
239 emit barsetsAdded(sets);
240 }
240 }
241 return success;
241 return success;
242 }
242 }
243
243
244 /*!
244 /*!
245 Removes all of the bar sets from the series
245 Removes all of the bar sets from the series
246 */
246 */
247 void QBarSeries::clear()
247 void QBarSeries::clear()
248 {
248 {
249 Q_D(QBarSeries);
249 Q_D(QBarSeries);
250 bool success = d->remove(barSets());
250 QList<QBarSet *> sets = barSets();
251 bool success = d->remove(sets);
251 if (success) {
252 if (success) {
252 emit barsetsRemoved(sets);
253 emit barsetsRemoved(sets);
253 }
254 }
254 return success;
255 }
255 }
256
256
257 /*!
257 /*!
258 Returns number of sets in series.
258 Returns number of sets in series.
259 */
259 */
260 int QBarSeries::barsetCount() const
260 int QBarSeries::barsetCount() const
261 {
261 {
262 Q_D(const QBarSeries);
262 Q_D(const QBarSeries);
263 return d->m_barSets.count();
263 return d->m_barSets.count();
264 }
264 }
265
265
266 /*!
266 /*!
267 Returns a list of sets in series. Keeps ownership of sets.
267 Returns a list of sets in series. Keeps ownership of sets.
268 */
268 */
269 QList<QBarSet*> QBarSeries::barSets() const
269 QList<QBarSet*> QBarSeries::barSets() const
270 {
270 {
271 Q_D(const QBarSeries);
271 Q_D(const QBarSeries);
272 return d->m_barSets;
272 return d->m_barSets;
273 }
273 }
274
274
275 /*!
275 /*!
276 Sets the visibility of labels in series to \a visible
276 Sets the visibility of labels in series to \a visible
277 */
277 */
278 void QBarSeries::setLabelsVisible(bool visible)
278 void QBarSeries::setLabelsVisible(bool visible)
279 {
279 {
280 Q_D(QBarSeries);
280 Q_D(QBarSeries);
281 if (d->m_labelsVisible != visible) {
281 if (d->m_labelsVisible != visible) {
282 d->setLabelsVisible(visible);
282 d->setLabelsVisible(visible);
283 emit labelsVisibleChanged();
283 emit labelsVisibleChanged();
284 }
284 }
285 }
285 }
286
286
287 /*!
287 /*!
288 Returns the visibility of labels
288 Returns the visibility of labels
289 */
289 */
290 bool QBarSeries::isLabelsVisible() const
290 bool QBarSeries::isLabelsVisible() const
291 {
291 {
292 Q_D(const QBarSeries);
292 Q_D(const QBarSeries);
293 return d->m_labelsVisible;
293 return d->m_labelsVisible;
294 }
294 }
295
295
296 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
297
297
298 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
298 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
299 QAbstractSeriesPrivate(q),
299 QAbstractSeriesPrivate(q),
300 m_barMargin(0.5), // Default value is 50% of category width
300 m_barMargin(0.5), // Default value is 50% of category width
301 m_labelsVisible(false),
301 m_labelsVisible(false),
302 m_visible(true)
302 m_visible(true)
303 {
303 {
304 }
304 }
305
305
306 void QBarSeriesPrivate::setCategories(QStringList categories)
306 void QBarSeriesPrivate::setCategories(QStringList categories)
307 {
307 {
308 m_categories = categories;
308 m_categories = categories;
309 }
309 }
310
310
311 void QBarSeriesPrivate::insertCategory(int index, const QString category)
311 void QBarSeriesPrivate::insertCategory(int index, const QString category)
312 {
312 {
313 m_categories.insert(index, category);
313 m_categories.insert(index, category);
314 emit categoriesUpdated();
314 emit categoriesUpdated();
315 }
315 }
316
316
317 void QBarSeriesPrivate::removeCategory(int index)
317 void QBarSeriesPrivate::removeCategory(int index)
318 {
318 {
319 m_categories.removeAt(index);
319 m_categories.removeAt(index);
320 emit categoriesUpdated();
320 emit categoriesUpdated();
321 }
321 }
322
322
323 int QBarSeriesPrivate::categoryCount() const
323 int QBarSeriesPrivate::categoryCount() const
324 {
324 {
325 if (m_categories.count() > 0) {
325 if (m_categories.count() > 0) {
326 return m_categories.count();
326 return m_categories.count();
327 }
327 }
328
328
329 // No categories defined. return count of longest set.
329 // No categories defined. return count of longest set.
330 int count = 0;
330 int count = 0;
331 for (int i=0; i<m_barSets.count(); i++) {
331 for (int i=0; i<m_barSets.count(); i++) {
332 if (m_barSets.at(i)->count() > count) {
332 if (m_barSets.at(i)->count() > count) {
333 count = m_barSets.at(i)->count();
333 count = m_barSets.at(i)->count();
334 }
334 }
335 }
335 }
336
336
337 return count;
337 return count;
338 }
338 }
339
339
340 QStringList QBarSeriesPrivate::categories() const
340 QStringList QBarSeriesPrivate::categories() const
341 {
341 {
342 if (m_categories.count() > 0) {
342 if (m_categories.count() > 0) {
343 return m_categories;
343 return m_categories;
344 }
344 }
345
345
346 // No categories defined. retun list of indices.
346 // No categories defined. retun list of indices.
347 QStringList categories;
347 QStringList categories;
348
348
349 int count = categoryCount();
349 int count = categoryCount();
350 for (int i = 0; i < count; i++) {
350 for (int i = 0; i < count; i++) {
351 categories.append(QString::number(i));
351 categories.append(QString::number(i));
352 }
352 }
353 return categories;
353 return categories;
354 }
354 }
355
355
356 void QBarSeriesPrivate::setBarMargin(qreal margin)
356 void QBarSeriesPrivate::setBarMargin(qreal margin)
357 {
357 {
358 if (margin > 1.0) {
358 if (margin > 1.0) {
359 margin = 1.0;
359 margin = 1.0;
360 } else if (margin < 0.0) {
360 } else if (margin < 0.0) {
361 margin = 0.0;
361 margin = 0.0;
362 }
362 }
363
363
364 m_barMargin = margin;
364 m_barMargin = margin;
365 emit updatedBars();
365 emit updatedBars();
366 }
366 }
367
367
368 qreal QBarSeriesPrivate::barMargin() const
368 qreal QBarSeriesPrivate::barMargin() const
369 {
369 {
370 return m_barMargin;
370 return m_barMargin;
371 }
371 }
372
372
373 QBarSet* QBarSeriesPrivate::barsetAt(int index)
373 QBarSet* QBarSeriesPrivate::barsetAt(int index)
374 {
374 {
375 return m_barSets.at(index);
375 return m_barSets.at(index);
376 }
376 }
377
377
378 void QBarSeriesPrivate::setVisible(bool visible)
378 void QBarSeriesPrivate::setVisible(bool visible)
379 {
379 {
380 m_visible = visible;
380 m_visible = visible;
381 emit updatedBars();
381 emit updatedBars();
382 }
382 }
383
383
384 void QBarSeriesPrivate::setLabelsVisible(bool visible)
384 void QBarSeriesPrivate::setLabelsVisible(bool visible)
385 {
385 {
386 m_labelsVisible = visible;
386 m_labelsVisible = visible;
387 emit labelsVisibleChanged(visible);
387 emit labelsVisibleChanged(visible);
388 }
388 }
389
389
390 QString QBarSeriesPrivate::categoryName(int category)
390 QString QBarSeriesPrivate::categoryName(int category)
391 {
391 {
392 if ((category >= 0) && (category < m_categories.count())) {
392 if ((category >= 0) && (category < m_categories.count())) {
393 return m_categories.at(category);
393 return m_categories.at(category);
394 }
394 }
395
395
396 return QString::number(category);
396 return QString::number(category);
397 }
397 }
398
398
399 qreal QBarSeriesPrivate::min()
399 qreal QBarSeriesPrivate::min()
400 {
400 {
401 if (m_barSets.count() <= 0) {
401 if (m_barSets.count() <= 0) {
402 return 0;
402 return 0;
403 }
403 }
404 qreal min = INT_MAX;
404 qreal min = INT_MAX;
405
405
406 for (int i = 0; i < m_barSets.count(); i++) {
406 for (int i = 0; i < m_barSets.count(); i++) {
407 int categoryCount = m_barSets.at(i)->count();
407 int categoryCount = m_barSets.at(i)->count();
408 for (int j = 0; j < categoryCount; j++) {
408 for (int j = 0; j < categoryCount; j++) {
409 qreal temp = m_barSets.at(i)->at(j).y();
409 qreal temp = m_barSets.at(i)->at(j).y();
410 if (temp < min)
410 if (temp < min)
411 min = temp;
411 min = temp;
412 }
412 }
413 }
413 }
414 return min;
414 return min;
415 }
415 }
416
416
417 qreal QBarSeriesPrivate::max()
417 qreal QBarSeriesPrivate::max()
418 {
418 {
419 if (m_barSets.count() <= 0) {
419 if (m_barSets.count() <= 0) {
420 return 0;
420 return 0;
421 }
421 }
422 qreal max = INT_MIN;
422 qreal max = INT_MIN;
423
423
424 for (int i = 0; i < m_barSets.count(); i++) {
424 for (int i = 0; i < m_barSets.count(); i++) {
425 int categoryCount = m_barSets.at(i)->count();
425 int categoryCount = m_barSets.at(i)->count();
426 for (int j = 0; j < categoryCount; j++) {
426 for (int j = 0; j < categoryCount; j++) {
427 qreal temp = m_barSets.at(i)->at(j).y();
427 qreal temp = m_barSets.at(i)->at(j).y();
428 if (temp > max)
428 if (temp > max)
429 max = temp;
429 max = temp;
430 }
430 }
431 }
431 }
432
432
433 return max;
433 return max;
434 }
434 }
435
435
436 qreal QBarSeriesPrivate::valueAt(int set, int category)
436 qreal QBarSeriesPrivate::valueAt(int set, int category)
437 {
437 {
438 if ((set < 0) || (set >= m_barSets.count())) {
438 if ((set < 0) || (set >= m_barSets.count())) {
439 // No set, no value.
439 // No set, no value.
440 return 0;
440 return 0;
441 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
441 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
442 // No category, no value.
442 // No category, no value.
443 return 0;
443 return 0;
444 }
444 }
445
445
446 return m_barSets.at(set)->at(category).y();
446 return m_barSets.at(set)->at(category).y();
447 }
447 }
448
448
449 qreal QBarSeriesPrivate::percentageAt(int set, int category)
449 qreal QBarSeriesPrivate::percentageAt(int set, int category)
450 {
450 {
451 if ((set < 0) || (set >= m_barSets.count())) {
451 if ((set < 0) || (set >= m_barSets.count())) {
452 // No set, no value.
452 // No set, no value.
453 return 0;
453 return 0;
454 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
454 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
455 // No category, no value.
455 // No category, no value.
456 return 0;
456 return 0;
457 }
457 }
458
458
459 qreal value = m_barSets.at(set)->at(category).y();
459 qreal value = m_barSets.at(set)->at(category).y();
460 qreal sum = categorySum(category);
460 qreal sum = categorySum(category);
461 if ( qFuzzyIsNull(sum) ) {
461 if ( qFuzzyIsNull(sum) ) {
462 return 0;
462 return 0;
463 }
463 }
464
464
465 return value / sum;
465 return value / sum;
466 }
466 }
467
467
468 qreal QBarSeriesPrivate::categorySum(int category)
468 qreal QBarSeriesPrivate::categorySum(int category)
469 {
469 {
470 qreal sum(0);
470 qreal sum(0);
471 int count = m_barSets.count(); // Count sets
471 int count = m_barSets.count(); // Count sets
472 for (int set = 0; set < count; set++) {
472 for (int set = 0; set < count; set++) {
473 if (category < m_barSets.at(set)->count())
473 if (category < m_barSets.at(set)->count())
474 sum += m_barSets.at(set)->at(category).y();
474 sum += m_barSets.at(set)->at(category).y();
475 }
475 }
476 return sum;
476 return sum;
477 }
477 }
478
478
479 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
479 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
480 {
480 {
481 qreal sum(0);
481 qreal sum(0);
482 int count = m_barSets.count(); // Count sets
482 int count = m_barSets.count(); // Count sets
483 for (int set = 0; set < count; set++) {
483 for (int set = 0; set < count; set++) {
484 if (category < m_barSets.at(set)->count())
484 if (category < m_barSets.at(set)->count())
485 sum += qAbs(m_barSets.at(set)->at(category).y());
485 sum += qAbs(m_barSets.at(set)->at(category).y());
486 }
486 }
487 return sum;
487 return sum;
488 }
488 }
489
489
490 qreal QBarSeriesPrivate::maxCategorySum()
490 qreal QBarSeriesPrivate::maxCategorySum()
491 {
491 {
492 qreal max = INT_MIN;
492 qreal max = INT_MIN;
493 int count = categoryCount();
493 int count = categoryCount();
494 for (int i = 0; i < count; i++) {
494 for (int i = 0; i < count; i++) {
495 qreal sum = categorySum(i);
495 qreal sum = categorySum(i);
496 if (sum > max)
496 if (sum > max)
497 max = sum;
497 max = sum;
498 }
498 }
499 return max;
499 return max;
500 }
500 }
501
501
502 void QBarSeriesPrivate::scaleDomain(Domain& domain)
502 void QBarSeriesPrivate::scaleDomain(Domain& domain)
503 {
503 {
504 qreal minX(domain.minX());
504 qreal minX(domain.minX());
505 qreal minY(domain.minY());
505 qreal minY(domain.minY());
506 qreal maxX(domain.maxX());
506 qreal maxX(domain.maxX());
507 qreal maxY(domain.maxY());
507 qreal maxY(domain.maxY());
508 int tickXCount(domain.tickXCount());
508 int tickXCount(domain.tickXCount());
509 int tickYCount(domain.tickYCount());
509 int tickYCount(domain.tickYCount());
510
510
511 qreal x = categoryCount();
511 qreal x = categoryCount();
512 qreal y = max();
512 qreal y = max();
513 minX = qMin(minX, x) - 0.5;
513 minX = qMin(minX, x) - 0.5;
514 minY = qMin(minY, y);
514 minY = qMin(minY, y);
515 maxX = qMax(maxX, x) - 0.5;
515 maxX = qMax(maxX, x) - 0.5;
516 maxY = qMax(maxY, y);
516 maxY = qMax(maxY, y);
517 tickXCount = x+1;
517 tickXCount = x+1;
518
518
519 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
519 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
520 }
520 }
521
521
522 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
522 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
523 {
523 {
524 Q_Q(QBarSeries);
524 Q_Q(QBarSeries);
525
525
526 BarChartItem* bar = new BarChartItem(q,presenter);
526 BarChartItem* bar = new BarChartItem(q,presenter);
527 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
527 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
528 presenter->animator()->addAnimation(bar);
528 presenter->animator()->addAnimation(bar);
529 }
529 }
530 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
530 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
531 return bar;
531 return bar;
532
532
533 }
533 }
534
534
535 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
535 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
536 {
536 {
537 Q_Q(QBarSeries);
537 Q_Q(QBarSeries);
538 QList<LegendMarker*> markers;
538 QList<LegendMarker*> markers;
539 foreach(QBarSet* set, q->barSets()) {
539 foreach(QBarSet* set, q->barSets()) {
540 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
540 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
541 markers << marker;
541 markers << marker;
542 }
542 }
543
543
544 return markers;
544 return markers;
545 }
545 }
546
546
547 bool QBarSeriesPrivate::append(QBarSet *set)
547 bool QBarSeriesPrivate::append(QBarSet *set)
548 {
548 {
549 Q_Q(QBarSeries);
549 Q_Q(QBarSeries);
550 if ((m_barSets.contains(set)) || (set == 0)) {
550 if ((m_barSets.contains(set)) || (set == 0)) {
551 // Fail if set is already in list or set is null.
551 // Fail if set is already in list or set is null.
552 return false;
552 return false;
553 }
553 }
554 m_barSets.append(set);
554 m_barSets.append(set);
555 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
555 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
556 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
556 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
557 if (m_dataset) {
557 if (m_dataset) {
558 m_dataset->updateSeries(q); // this notifies legend
558 m_dataset->updateSeries(q); // this notifies legend
559 }
559 }
560 emit restructuredBars(); // this notifies barchartitem
560 emit restructuredBars(); // this notifies barchartitem
561 return true;
561 return true;
562 }
562 }
563
563
564 bool QBarSeriesPrivate::remove(QBarSet *set)
564 bool QBarSeriesPrivate::remove(QBarSet *set)
565 {
565 {
566 Q_Q(QBarSeries);
566 Q_Q(QBarSeries);
567 if (!m_barSets.contains(set)) {
567 if (!m_barSets.contains(set)) {
568 // Fail if set is not in list
568 // Fail if set is not in list
569 return false;
569 return false;
570 }
570 }
571 m_barSets.removeOne(set);
571 m_barSets.removeOne(set);
572 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
572 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
573 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
573 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
574 if (m_dataset) {
574 if (m_dataset) {
575 m_dataset->updateSeries(q); // this notifies legend
575 m_dataset->updateSeries(q); // this notifies legend
576 }
576 }
577 emit restructuredBars(); // this notifies barchartitem
577 emit restructuredBars(); // this notifies barchartitem
578 return true;
578 return true;
579 }
579 }
580
580
581 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
581 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
582 {
582 {
583 Q_Q(QBarSeries);
583 Q_Q(QBarSeries);
584 foreach (QBarSet* set, sets) {
584 foreach (QBarSet* set, sets) {
585 if ((set == 0) || (m_barSets.contains(set))) {
585 if ((set == 0) || (m_barSets.contains(set))) {
586 // Fail if any of the sets is null or is already appended.
586 // Fail if any of the sets is null or is already appended.
587 return false;
587 return false;
588 }
588 }
589 if (sets.count(set) != 1) {
589 if (sets.count(set) != 1) {
590 // Also fail if same set is more than once in given list.
590 // Also fail if same set is more than once in given list.
591 return false;
591 return false;
592 }
592 }
593 }
593 }
594
594
595 foreach (QBarSet* set, sets) {
595 foreach (QBarSet* set, sets) {
596 m_barSets.append(set);
596 m_barSets.append(set);
597 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
597 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
598 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
598 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
599 }
599 }
600 if (m_dataset) {
600 if (m_dataset) {
601 m_dataset->updateSeries(q); // this notifies legend
601 m_dataset->updateSeries(q); // this notifies legend
602 }
602 }
603 emit restructuredBars(); // this notifies barchartitem
603 emit restructuredBars(); // this notifies barchartitem
604 return true;
604 return true;
605 }
605 }
606
606
607 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
607 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
608 {
608 {
609 Q_Q(QBarSeries);
609 Q_Q(QBarSeries);
610 foreach (QBarSet* set, sets) {
610 foreach (QBarSet* set, sets) {
611 if ((set == 0) || (!m_barSets.contains(set))) {
611 if ((set == 0) || (!m_barSets.contains(set))) {
612 // Fail if any of the sets is null or is not in series
612 // Fail if any of the sets is null or is not in series
613 return false;
613 return false;
614 }
614 }
615 if (sets.count(set) != 1) {
615 if (sets.count(set) != 1) {
616 // Also fail if same set is more than once in given list.
616 // Also fail if same set is more than once in given list.
617 return false;
617 return false;
618 }
618 }
619 }
619 }
620
620
621 foreach (QBarSet* set, sets) {
621 foreach (QBarSet* set, sets) {
622 m_barSets.removeOne(set);
622 m_barSets.removeOne(set);
623 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
623 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
624 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
624 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
625 }
625 }
626
626
627 if (m_dataset) {
627 if (m_dataset) {
628 m_dataset->updateSeries(q); // this notifies legend
628 m_dataset->updateSeries(q); // this notifies legend
629 }
629 }
630 emit restructuredBars(); // this notifies barchartitem
630 emit restructuredBars(); // this notifies barchartitem
631 return true;
631 return true;
632 }
632 }
633
633
634 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
634 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
635 {
635 {
636 Q_Q(QBarSeries);
636 Q_Q(QBarSeries);
637 if ((m_barSets.contains(set)) || (set == 0)) {
637 if ((m_barSets.contains(set)) || (set == 0)) {
638 // Fail if set is already in list or set is null.
638 // Fail if set is already in list or set is null.
639 return false;
639 return false;
640 }
640 }
641 m_barSets.insert(index, set);
641 m_barSets.insert(index, set);
642 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
642 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
643 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
643 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
644 if (m_dataset) {
644 if (m_dataset) {
645 m_dataset->updateSeries(q); // this notifies legend
645 m_dataset->updateSeries(q); // this notifies legend
646 }
646 }
647 emit restructuredBars(); // this notifies barchartitem
647 emit restructuredBars(); // this notifies barchartitem
648 return true;
648 return true;
649 }
649 }
650
650
651 #include "moc_qbarseries.cpp"
651 #include "moc_qbarseries.cpp"
652 #include "moc_qbarseries_p.cpp"
652 #include "moc_qbarseries_p.cpp"
653
653
654 QTCOMMERCIALCHART_END_NAMESPACE
654 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,10 +1,10
1 !include( ../tests.pri ) {
1 !include( ../tests.pri ) {
2 error( "Couldn't find the tests.pri file!" )
2 error( "Couldn't find the tests.pri file!" )
3 }
3 }
4
4
5 TEMPLATE = subdirs
5 TEMPLATE = subdirs
6 SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper
6 SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper qbarmodelmapper
7
7
8 test_private:{
8 test_private:{
9 SUBDIRS += chartdataset domain
9 SUBDIRS += chartdataset domain
10 }
10 }
General Comments 0
You need to be logged in to leave comments. Login now