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