##// END OF EJS Templates
Removed all the unfinished model functionality from XYSeries and BarSeries
Marek Rosa -
r990:2f31b8379e5f
parent child
Show More
@@ -1,89 +1,89
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 "declarativebarseries.h"
21 #include "declarativebarseries.h"
22 #include "declarativechart.h"
22 #include "declarativechart.h"
23 #include "qchart.h"
23 #include "qchart.h"
24 #include "qbarseries.h"
24 #include "qbarseries.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
29 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
30 QDeclarativeItem(parent)
30 QDeclarativeItem(parent)
31 {
31 {
32 setFlag(QGraphicsItem::ItemHasNoContents, false);
32 setFlag(QGraphicsItem::ItemHasNoContents, false);
33 }
33 }
34
34
35 void DeclarativeBarSeries::componentComplete()
35 void DeclarativeBarSeries::componentComplete()
36 {
36 {
37 if (!m_series) {
37 if (!m_series) {
38 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
38 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
39
39
40 if (declarativeChart) {
40 if (declarativeChart) {
41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
42 Q_ASSERT(chart);
42 Q_ASSERT(chart);
43
43
44 // QStringList categories;
44 // QStringList categories;
45 // categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
45 // categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
46 // m_series = new QBarSeries(categories);
46 // m_series = new QBarSeries(categories);
47 // m_series = new QBarSeries(m_categories);
47 // m_series = new QBarSeries(m_categories);
48 m_series = new QBarSeries(m_categories);
48 m_series = new QBarSeries(m_categories);
49
49
50 // TODO: use data from model
50 // TODO: use data from model
51 QBarSet *set0 = new QBarSet("Bub");
51 QBarSet *set0 = new QBarSet("Bub");
52 QBarSet *set1 = new QBarSet("Bob");
52 QBarSet *set1 = new QBarSet("Bob");
53 QBarSet *set2 = new QBarSet("Guybrush");
53 QBarSet *set2 = new QBarSet("Guybrush");
54
54
55 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
55 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
56 *set1 << 5 << 1 << 2 << 4 << 1 << 7;
56 *set1 << 5 << 1 << 2 << 4 << 1 << 7;
57 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
57 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
58
58
59 m_series->appendBarSet(set0);
59 m_series->appendBarSet(set0);
60 m_series->appendBarSet(set1);
60 m_series->appendBarSet(set1);
61 m_series->appendBarSet(set2);
61 m_series->appendBarSet(set2);
62
62
63 chart->addSeries(m_series);
63 chart->addSeries(m_series);
64 }
64 }
65 }
65 }
66 }
66 }
67
67
68 void DeclarativeBarSeries::setBarCategories(QStringList categories)
68 void DeclarativeBarSeries::setBarCategories(QStringList /*categories*/)
69 {
69 {
70 m_categories = categories;
70 // m_categories = categories;
71 if (m_series) {
71 // if (m_series) {
72 // Replace categories of the QBarSeries with the new categories
72 // // Replace categories of the QBarSeries with the new categories
73 for (int i(0); i < m_categories.count(); i++) {
73 // for (int i(0); i < m_categories.count(); i++) {
74 if (m_series->categories().at(i) != m_categories.at(i))
74 // if (m_series->categories().at(i) != m_categories.at(i))
75 m_series->insertCategory(m_series->categoryCount(), m_categories.at(i));
75 // m_series->insertCategory(m_series->categoryCount(), m_categories.at(i));
76 }
76 // }
77 while (m_series->categoryCount() > m_categories.count())
77 // while (m_series->categoryCount() > m_categories.count())
78 m_series->removeCategory(m_series->categoryCount() - 1);
78 // m_series->removeCategory(m_series->categoryCount() - 1);
79 }
79 // }
80 }
80 }
81
81
82 QStringList DeclarativeBarSeries::barCategories()
82 QStringList DeclarativeBarSeries::barCategories()
83 {
83 {
84 return m_categories;
84 return m_categories;
85 }
85 }
86
86
87 #include "moc_declarativebarseries.cpp"
87 #include "moc_declarativebarseries.cpp"
88
88
89 QTCOMMERCIALCHART_END_NAMESPACE
89 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,527 +1,454
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 "barchartmodel_p.h"
25 #include "barchartmodel_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "chartdataset_p.h"
28 #include "chartdataset_p.h"
29 #include "charttheme_p.h"
29 #include "charttheme_p.h"
30 #include "chartanimator_p.h"
30 #include "chartanimator_p.h"
31
31
32 #include <QAbstractItemModel>
32 #include <QAbstractItemModel>
33 #include <QModelIndex>
33 #include <QModelIndex>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 /*!
37 /*!
38 \class QBarSeries
38 \class QBarSeries
39 \brief part of QtCommercial chart API.
39 \brief part of QtCommercial chart API.
40
40
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 by QStringList.
43 by QStringList.
44
44
45 \mainclass
45 \mainclass
46
46
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
51 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
52
52
53 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
53 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
54 contained by the series.
54 contained by the series.
55 */
55 */
56
56
57 /*!
57 /*!
58 \fn void QBarSeries::selected()
58 \fn void QBarSeries::selected()
59
59
60 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
60 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
61 implemented by the user of QBarSeries API.
61 implemented by the user of QBarSeries API.
62 */
62 */
63
63
64 /*!
64 /*!
65 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
65 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
66
66
67 The signal is emitted if mouse is hovered on top of series.
67 The signal is emitted if mouse is hovered on top of series.
68 Parameter \a barset is the pointer of barset, where hover happened.
68 Parameter \a barset is the pointer of barset, where hover happened.
69 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
69 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
70 */
70 */
71
71
72 /*!
72 /*!
73 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
73 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
74 QBarSeries is QObject which is a child of a \a parent.
74 QBarSeries is QObject which is a child of a \a parent.
75 */
75 */
76 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
76 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
77 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
77 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
78 {
78 {
79 }
79 }
80
80
81 /*!
81 /*!
82 Destructs barseries and owned barsets.
82 Destructs barseries and owned barsets.
83 */
83 */
84 QBarSeries::~QBarSeries()
84 QBarSeries::~QBarSeries()
85 {
85 {
86 // NOTE: d_ptr destroyed by QObject
86 // NOTE: d_ptr destroyed by QObject
87 }
87 }
88
88
89 /*!
89 /*!
90 \internal
90 \internal
91 */
91 */
92 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
92 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
93 QAbstractSeries(d,parent)
93 QAbstractSeries(d,parent)
94 {
94 {
95 }
95 }
96
96
97 /*!
97 /*!
98 Returns the type of series. Derived classes override this.
98 Returns the type of series. Derived classes override this.
99 */
99 */
100 QAbstractSeries::QSeriesType QBarSeries::type() const
100 QAbstractSeries::QSeriesType QBarSeries::type() const
101 {
101 {
102 return QAbstractSeries::SeriesTypeBar;
102 return QAbstractSeries::SeriesTypeBar;
103 }
103 }
104
104
105 /*!
105 /*!
106 Adds a set of bars to series. Takes ownership of \a set.
106 Adds a set of bars to series. Takes ownership of \a set.
107 Connects the clicked(QString, Qt::MouseButtons) signal
107 Connects the clicked(QString, Qt::MouseButtons) signal
108 of \a set to this series
108 of \a set to this series
109 */
109 */
110 void QBarSeries::appendBarSet(QBarSet *set)
110 void QBarSeries::appendBarSet(QBarSet *set)
111 {
111 {
112 Q_D(QBarSeries);
112 Q_D(QBarSeries);
113 d->m_internalModel->appendBarSet(set);
113 d->m_internalModel->appendBarSet(set);
114 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
114 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
115 emit d->restructuredBars();
115 emit d->restructuredBars();
116 }
116 }
117
117
118 /*!
118 /*!
119 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
119 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
120 Disconnects the clicked(QString, Qt::MouseButtons) signal
120 Disconnects the clicked(QString, Qt::MouseButtons) signal
121 of \a set from this series
121 of \a set from this series
122 */
122 */
123 void QBarSeries::removeBarSet(QBarSet *set)
123 void QBarSeries::removeBarSet(QBarSet *set)
124 {
124 {
125 Q_D(QBarSeries);
125 Q_D(QBarSeries);
126 d->m_internalModel->removeBarSet(set);
126 d->m_internalModel->removeBarSet(set);
127 emit d->restructuredBars();
127 emit d->restructuredBars();
128 }
128 }
129
129
130 /*!
130 /*!
131 Adds a list of barsets to series. Takes ownership of \a sets.
131 Adds a list of barsets to series. Takes ownership of \a sets.
132 Connects the clicked(QString, Qt::MouseButtons) signals
132 Connects the clicked(QString, Qt::MouseButtons) signals
133 of \a sets to this series
133 of \a sets to this series
134 */
134 */
135 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
135 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
136 {
136 {
137 Q_D(QBarSeries);
137 Q_D(QBarSeries);
138 foreach (QBarSet* barset, sets) {
138 foreach (QBarSet* barset, sets) {
139 d->m_internalModel->appendBarSet(barset);
139 d->m_internalModel->appendBarSet(barset);
140 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
140 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
141 }
141 }
142 emit d->restructuredBars();
142 emit d->restructuredBars();
143
143
144 }
144 }
145
145
146 /*!
146 /*!
147 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
147 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
148 Disconnects the clicked(QString, Qt::MouseButtons) signal
148 Disconnects the clicked(QString, Qt::MouseButtons) signal
149 of \a sets from this series
149 of \a sets from this series
150 */
150 */
151 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
151 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
152 {
152 {
153 Q_D(QBarSeries);
153 Q_D(QBarSeries);
154
154
155 foreach (QBarSet* barset, sets) {
155 foreach (QBarSet* barset, sets) {
156 d->m_internalModel->removeBarSet(barset);
156 d->m_internalModel->removeBarSet(barset);
157 }
157 }
158 emit d->restructuredBars();
158 emit d->restructuredBars();
159 }
159 }
160
160
161 /*!
161 /*!
162 Inserts new \a set on the \a i position.
162 Inserts new \a set on the \a i position.
163 The barset that is currently at this postion is moved to postion i + 1
163 The barset that is currently at this postion is moved to postion i + 1
164 */
164 */
165 void QBarSeries::insertBarSet(int i, QBarSet *set)
165 void QBarSeries::insertBarSet(int i, QBarSet *set)
166 {
166 {
167 Q_D(QBarSeries);
167 Q_D(QBarSeries);
168 d->m_internalModel->insertBarSet(i, set);
168 d->m_internalModel->insertBarSet(i, set);
169 emit d->barsetChanged();
169 emit d->barsetChanged();
170 }
170 }
171
171
172 /*!
172 /*!
173 Inserts new \a category on the \a i position.
174 The category that is currently at this postion is moved to postion i + 1
175 \sa removeCategory()
176 */
177 void QBarSeries::insertCategory(int i, QString category)
178 {
179 Q_D(QBarSeries);
180 d->m_internalModel->insertCategory(i, category);
181 }
182
183 /*!
184 Removes the category specified by \a i
185 \sa insertCategory()
186 */
187 void QBarSeries::removeCategory(int i)
188 {
189 Q_D(QBarSeries);
190 d->m_internalModel->removeCategory(i);
191 }
192
193 /*!
194 Returns number of sets in series.
173 Returns number of sets in series.
195 */
174 */
196 int QBarSeries::barsetCount() const
175 int QBarSeries::barsetCount() const
197 {
176 {
198 Q_D(const QBarSeries);
177 Q_D(const QBarSeries);
199 return d->m_internalModel->barsetCount();
178 return d->m_internalModel->barsetCount();
200 }
179 }
201
180
202 /*!
181 /*!
203 Returns number of categories in series
182 Returns number of categories in series
204 */
183 */
205 int QBarSeries::categoryCount() const
184 int QBarSeries::categoryCount() const
206 {
185 {
207 Q_D(const QBarSeries);
186 Q_D(const QBarSeries);
208 return d->m_internalModel->categoryCount();
187 return d->m_internalModel->categoryCount();
209 }
188 }
210
189
211 /*!
190 /*!
212 Returns a list of sets in series. Keeps ownership of sets.
191 Returns a list of sets in series. Keeps ownership of sets.
213 */
192 */
214 QList<QBarSet*> QBarSeries::barSets() const
193 QList<QBarSet*> QBarSeries::barSets() const
215 {
194 {
216 Q_D(const QBarSeries);
195 Q_D(const QBarSeries);
217 return d->m_internalModel->barSets();
196 return d->m_internalModel->barSets();
218 }
197 }
219
198
220 /*!
199 /*!
221 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
200 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
222 Sets the \a model to be used as a data source
201 Sets the \a model to be used as a data source
223 */
202 */
224 bool QBarSeries::setModel(QAbstractItemModel *model)
203 bool QBarSeries::setModel(QAbstractItemModel *model)
225 {
204 {
226 Q_D(QBarSeries);
205 Q_D(QBarSeries);
227 return d->setModel(model);
206 return d->setModel(model);
228 }
207 }
229
208
230 /*!
209 /*!
231 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
210 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
232 Sets column/row specified by \a categories to be used as a list of bar series categories.
211 Sets column/row specified by \a categories to be used as a list of bar series categories.
233 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
212 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
234 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
213 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
235 All the columns/rows inbetween those two values are also used as data for bar sets.
214 All the columns/rows inbetween those two values are also used as data for bar sets.
236 The \a orientation parameter specifies whether the data is in columns or in rows.
215 The \a orientation parameter specifies whether the data is in columns or in rows.
237 */
216 */
238 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
217 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
239 {
218 {
240 Q_D(QBarSeries);
219 Q_D(QBarSeries);
241 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
220 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
242 }
221 }
243
222
244 /*!
223 /*!
245 Returns the bar categories of the series.
224 Returns the bar categories of the series.
246 */
225 */
247 QBarCategories QBarSeries::categories() const
226 QBarCategories QBarSeries::categories() const
248 {
227 {
249 Q_D(const QBarSeries);
228 Q_D(const QBarSeries);
250
229
251 QBarCategories categories;
230 QBarCategories categories;
252 int count = d->m_internalModel->categoryCount();
231 int count = d->m_internalModel->categoryCount();
253 for (int i=1; i <= count; i++) {
232 for (int i=1; i <= count; i++) {
254 categories.insert(i, d->m_internalModel->categoryName(i - 1));
233 categories.insert(i, d->m_internalModel->categoryName(i - 1));
255 }
234 }
256 return categories;
235 return categories;
257
236
258 }
237 }
259
238
260 /*!
239 /*!
261 Sets the visibility of labels in series to \a visible
240 Sets the visibility of labels in series to \a visible
262 */
241 */
263 void QBarSeries::setLabelsVisible(bool visible)
242 void QBarSeries::setLabelsVisible(bool visible)
264 {
243 {
265 foreach (QBarSet* s, barSets()) {
244 foreach (QBarSet* s, barSets()) {
266 s->setLabelsVisible(visible);
245 s->setLabelsVisible(visible);
267 }
246 }
268 }
247 }
269
248
270 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
249 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
271
250
272 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
251 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
273 QAbstractSeriesPrivate(q),
252 QAbstractSeriesPrivate(q),
274 m_internalModel(new BarChartModel(categories,this)),
253 m_internalModel(new BarChartModel(categories,this)),
275 m_model(0),
254 m_model(0),
276 m_mapCategories(-1),
255 m_mapCategories(-1),
277 m_mapBarBottom(-1),
256 m_mapBarBottom(-1),
278 m_mapBarTop(-1),
257 m_mapBarTop(-1),
279 m_mapFirst(0),
280 m_mapCount(0),
281 m_mapOrientation(Qt::Vertical)
258 m_mapOrientation(Qt::Vertical)
282 {
259 {
283 }
260 }
284
261
285 QBarSet* QBarSeriesPrivate::barsetAt(int index)
262 QBarSet* QBarSeriesPrivate::barsetAt(int index)
286 {
263 {
287 return m_internalModel->barsetAt(index);
264 return m_internalModel->barsetAt(index);
288 }
265 }
289
266
290 QString QBarSeriesPrivate::categoryName(int category)
267 QString QBarSeriesPrivate::categoryName(int category)
291 {
268 {
292 return m_internalModel->categoryName(category);
269 return m_internalModel->categoryName(category);
293 }
270 }
294
271
295 qreal QBarSeriesPrivate::min()
272 qreal QBarSeriesPrivate::min()
296 {
273 {
297 return m_internalModel->min();
274 return m_internalModel->min();
298 }
275 }
299
276
300 qreal QBarSeriesPrivate::max()
277 qreal QBarSeriesPrivate::max()
301 {
278 {
302 return m_internalModel->max();
279 return m_internalModel->max();
303 }
280 }
304
281
305 qreal QBarSeriesPrivate::valueAt(int set, int category)
282 qreal QBarSeriesPrivate::valueAt(int set, int category)
306 {
283 {
307 return m_internalModel->valueAt(set, category);
284 return m_internalModel->valueAt(set, category);
308 }
285 }
309
286
310 qreal QBarSeriesPrivate::percentageAt(int set, int category)
287 qreal QBarSeriesPrivate::percentageAt(int set, int category)
311 {
288 {
312 return m_internalModel->percentageAt(set, category);
289 return m_internalModel->percentageAt(set, category);
313 }
290 }
314
291
315 qreal QBarSeriesPrivate::categorySum(int category)
292 qreal QBarSeriesPrivate::categorySum(int category)
316 {
293 {
317 return m_internalModel->categorySum(category);
294 return m_internalModel->categorySum(category);
318 }
295 }
319
296
320 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
297 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
321 {
298 {
322 return m_internalModel->absoluteCategorySum(category);
299 return m_internalModel->absoluteCategorySum(category);
323 }
300 }
324
301
325 qreal QBarSeriesPrivate::maxCategorySum()
302 qreal QBarSeriesPrivate::maxCategorySum()
326 {
303 {
327 return m_internalModel->maxCategorySum();
304 return m_internalModel->maxCategorySum();
328 }
305 }
329
306
330 BarChartModel& QBarSeriesPrivate::modelInternal()
307 BarChartModel& QBarSeriesPrivate::modelInternal()
331 {
308 {
332 return *m_internalModel;
309 return *m_internalModel;
333 }
310 }
334
311
335 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
312 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
336 {
313 {
337 // disconnect signals from old model
314 // disconnect signals from old model
338 if(m_model)
315 if(m_model)
339 {
316 {
340 disconnect(m_model, 0, this, 0);
317 disconnect(m_model, 0, this, 0);
341 m_mapCategories = -1;
318 m_mapCategories = -1;
342 m_mapBarBottom = -1;
319 m_mapBarBottom = -1;
343 m_mapBarTop = -1;
320 m_mapBarTop = -1;
344 m_mapFirst = 0;
345 m_mapCount = 0;
346 m_mapOrientation = Qt::Vertical;
321 m_mapOrientation = Qt::Vertical;
347 }
322 }
348
323
349 // set new model
324 // set new model
350 if(model)
325 if(model)
351 {
326 {
352 m_model = model;
327 m_model = model;
353 return true;
328 return true;
354 }
329 }
355 else
330 else
356 {
331 {
357 m_model = 0;
332 m_model = 0;
358 return false;
333 return false;
359 }
334 }
360 }
335 }
361
336
362 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
337 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
363 {
338 {
364 Q_Q(QBarSeries);
339 Q_Q(QBarSeries);
365
340
366 if (m_model == 0)
341 if (m_model == 0)
367 return;
342 return;
368
343
369 m_mapCategories = categories;
344 m_mapCategories = categories;
370 m_mapBarBottom = bottomBoundry;
345 m_mapBarBottom = bottomBoundry;
371 m_mapBarTop = topBoundry;
346 m_mapBarTop = topBoundry;
372 // m_mapFirst = 1;
373 m_mapOrientation = orientation;
347 m_mapOrientation = orientation;
374
348
375 // connect the signals
349 // connect the signals
376 if (m_mapOrientation == Qt::Vertical) {
350 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
377 m_mapCount = m_model->rowCount() - m_mapFirst;
351 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
378 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
379 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
380 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)),
381 this, SLOT(modelDataAdded(QModelIndex,int,int)));
382 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
383 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
384 } else {
385 m_mapCount = m_model->columnCount() - m_mapFirst;
386 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
387 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
388 connect(m_model,SIGNAL(columnsInserted(QModelIndex,int,int)),
389 this, SLOT(modelDataAdded(QModelIndex,int,int)));
390 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
391 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
392 }
393
352
394 // create the initial bars
353 // create the initial bars
395 delete m_internalModel;
354 delete m_internalModel;
396 if (m_mapOrientation == Qt::Vertical) {
355 if (m_mapOrientation == Qt::Vertical) {
397 QStringList categories;
356 QStringList categories;
398 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
357 for (int k = 0; k < m_model->rowCount(); k++)
399 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
358 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
400 m_internalModel = new BarChartModel(categories, this);
359 m_internalModel = new BarChartModel(categories, this);
401
360
402 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
361 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
403 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
362 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
404 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
363 for(int m = 0; m < m_model->rowCount(); m++)
405 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
364 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
406 q->appendBarSet(barSet);
365 q->appendBarSet(barSet);
407 }
366 }
408 } else {
367 } else {
409 QStringList categories;
368 QStringList categories;
410 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
369 for (int k = 0; k < m_model->columnCount(); k++)
411 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
370 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
412 m_internalModel = new BarChartModel(categories, this);
371 m_internalModel = new BarChartModel(categories, this);
413
372
414 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
373 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
415 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
374 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
416 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
375 for(int m = 0; m < m_model->columnCount(); m++)
417 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
376 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
418 q->appendBarSet(barSet);
377 q->appendBarSet(barSet);
419 }
378 }
420 }
379 }
421 }
380 }
422
381
423 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
382 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
424 {
383 {
425 Q_UNUSED(bottomRight)
384 Q_UNUSED(bottomRight)
426
385
427 if (m_mapOrientation == Qt::Vertical)
386 if (m_mapOrientation == Qt::Vertical)
428 {
387 {
429 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
388 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
430 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
389 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
431 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
390 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
432 }
391 }
433 else
392 else
434 {
393 {
435 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
394 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
436 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
395 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
437 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
396 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
438 }
439 }
440
441 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
442 {
443 Q_Q(QBarSeries);
444
445 if (m_mapOrientation == Qt::Vertical) {
446 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
447 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
448 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
449 }
450 } else {
451 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
452 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
453 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
454 }
455 }
456 emit restructuredBars();
457 }
458
459 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
460 {
461 Q_Q(QBarSeries);
462 Q_UNUSED(parent)
463 Q_UNUSED(end)
464
465 q->removeCategory(start - m_mapFirst);
466 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
467 {
468 barsetAt(i)->removeValue(start - m_mapFirst);
469 }
397 }
470 emit restructuredBars();
471 }
398 }
472
399
473 void QBarSeriesPrivate::barsetChanged()
400 void QBarSeriesPrivate::barsetChanged()
474 {
401 {
475 emit updatedBars();
402 emit updatedBars();
476 }
403 }
477
404
478 void QBarSeriesPrivate::scaleDomain(Domain& domain)
405 void QBarSeriesPrivate::scaleDomain(Domain& domain)
479 {
406 {
480 qreal minX(domain.minX());
407 qreal minX(domain.minX());
481 qreal minY(domain.minY());
408 qreal minY(domain.minY());
482 qreal maxX(domain.maxX());
409 qreal maxX(domain.maxX());
483 qreal maxY(domain.maxY());
410 qreal maxY(domain.maxY());
484 int tickXCount(domain.tickXCount());
411 int tickXCount(domain.tickXCount());
485 int tickYCount(domain.tickYCount());
412 int tickYCount(domain.tickYCount());
486
413
487 qreal x = m_internalModel->categoryCount();
414 qreal x = m_internalModel->categoryCount();
488 qreal y = max();
415 qreal y = max();
489 minX = qMin(minX, x);
416 minX = qMin(minX, x);
490 minY = qMin(minY, y);
417 minY = qMin(minY, y);
491 maxX = qMax(maxX, x);
418 maxX = qMax(maxX, x);
492 maxY = qMax(maxY, y);
419 maxY = qMax(maxY, y);
493 tickXCount = x+1;
420 tickXCount = x+1;
494
421
495 domain.setRangeX(minX,maxX,tickXCount);
422 domain.setRangeX(minX,maxX,tickXCount);
496 domain.setRangeY(minY,maxY,tickYCount);
423 domain.setRangeY(minY,maxY,tickYCount);
497 }
424 }
498
425
499 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
426 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
500 {
427 {
501 Q_Q(QBarSeries);
428 Q_Q(QBarSeries);
502
429
503 BarChartItem* bar = new BarChartItem(q,presenter);
430 BarChartItem* bar = new BarChartItem(q,presenter);
504 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
431 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
505 presenter->animator()->addAnimation(bar);
432 presenter->animator()->addAnimation(bar);
506 }
433 }
507 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
434 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
508 return bar;
435 return bar;
509
436
510 }
437 }
511
438
512 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
439 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
513 {
440 {
514 Q_Q(QBarSeries);
441 Q_Q(QBarSeries);
515 QList<LegendMarker*> markers;
442 QList<LegendMarker*> markers;
516 foreach(QBarSet* set, q->barSets()) {
443 foreach(QBarSet* set, q->barSets()) {
517 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
444 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
518 markers << marker;
445 markers << marker;
519 }
446 }
520
447
521 return markers;
448 return markers;
522 }
449 }
523
450
524 #include "moc_qbarseries.cpp"
451 #include "moc_qbarseries.cpp"
525 #include "moc_qbarseries_p.cpp"
452 #include "moc_qbarseries_p.cpp"
526
453
527 QTCOMMERCIALCHART_END_NAMESPACE
454 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,80
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 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 typedef QStringList QBarCategories;
31 typedef QStringList QBarCategories;
32
32
33 class QBarSet;
33 class QBarSet;
34 class BarChartModel;
34 class BarChartModel;
35 class BarCategory;
35 class BarCategory;
36 class QBarSeriesPrivate;
36 class QBarSeriesPrivate;
37
37
38 // Container for series
38 // Container for series
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
43 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
44 virtual ~QBarSeries();
44 virtual ~QBarSeries();
45
45
46 QAbstractSeries::QSeriesType type() const;
46 QAbstractSeries::QSeriesType type() const;
47
47
48 void appendBarSet(QBarSet *set); // Takes ownership of set
48 void appendBarSet(QBarSet *set); // Takes ownership of set
49 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
49 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 void appendBarSets(QList<QBarSet* > sets);
50 void appendBarSets(QList<QBarSet* > sets);
51 void removeBarSets(QList<QBarSet* > sets);
51 void removeBarSets(QList<QBarSet* > sets);
52 void insertBarSet(int i, QBarSet *set);
52 void insertBarSet(int i, QBarSet *set);
53 void insertCategory(int i, QString category);
54 void removeCategory(int i);
55 int barsetCount() const;
53 int barsetCount() const;
56 int categoryCount() const;
54 int categoryCount() const;
57 QList<QBarSet*> barSets() const;
55 QList<QBarSet*> barSets() const;
58 QBarCategories categories() const;
56 QBarCategories categories() const;
59
57
60 void setLabelsVisible(bool visible = true);
58 void setLabelsVisible(bool visible = true);
61
59
62 bool setModel(QAbstractItemModel *model);
60 bool setModel(QAbstractItemModel *model);
63 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
61 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
64
62
65 protected:
63 protected:
66 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
64 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
67
65
68 Q_SIGNALS:
66 Q_SIGNALS:
69 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
67 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
70 void selected();
68 void selected();
71 void hovered(QBarSet* barset, bool status);
69 void hovered(QBarSet* barset, bool status);
72
70
73 protected:
71 protected:
74 Q_DECLARE_PRIVATE(QBarSeries)
72 Q_DECLARE_PRIVATE(QBarSeries)
75 friend class BarChartItem;
73 friend class BarChartItem;
76 friend class PercentBarChartItem;
74 friend class PercentBarChartItem;
77 friend class StackedBarChartItem;
75 friend class StackedBarChartItem;
78 };
76 };
79
77
80 QTCOMMERCIALCHART_END_NAMESPACE
78 QTCOMMERCIALCHART_END_NAMESPACE
81
79
82 #endif // BARSERIES_H
80 #endif // BARSERIES_H
@@ -1,66 +1,62
1 #ifndef QBARSERIES_P_H
1 #ifndef QBARSERIES_P_H
2 #define QBARSERIES_P_H
2 #define QBARSERIES_P_H
3
3
4 #include "qbarseries.h"
4 #include "qbarseries.h"
5 #include "qabstractseries_p.h"
5 #include "qabstractseries_p.h"
6 #include <QStringList>
6 #include <QStringList>
7 #include <QAbstractSeries>
7 #include <QAbstractSeries>
8
8
9 class QModelIndex;
9 class QModelIndex;
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 // Container for series
13 // Container for series
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
19
19
20 void scaleDomain(Domain& domain);
20 void scaleDomain(Domain& domain);
21 Chart* createGraphics(ChartPresenter* presenter);
21 Chart* createGraphics(ChartPresenter* presenter);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
23
23
24 bool setModel(QAbstractItemModel *model);
24 bool setModel(QAbstractItemModel *model);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26
26
27 QBarSet* barsetAt(int index);
27 QBarSet* barsetAt(int index);
28 QString categoryName(int category);
28 QString categoryName(int category);
29 qreal min();
29 qreal min();
30 qreal max();
30 qreal max();
31 qreal valueAt(int set, int category);
31 qreal valueAt(int set, int category);
32 qreal percentageAt(int set, int category);
32 qreal percentageAt(int set, int category);
33 qreal categorySum(int category);
33 qreal categorySum(int category);
34 qreal absoluteCategorySum(int category);
34 qreal absoluteCategorySum(int category);
35 qreal maxCategorySum();
35 qreal maxCategorySum();
36 BarChartModel& modelInternal();
36 BarChartModel& modelInternal();
37
37
38 Q_SIGNALS:
38 Q_SIGNALS:
39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40 void selected();
40 void selected();
41 void updatedBars();
41 void updatedBars();
42 void restructuredBars();
42 void restructuredBars();
43
43
44 private Q_SLOTS:
44 private Q_SLOTS:
45 // slots for updating bars when data in model changes
45 // slots for updating bars when data in model changes
46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
47 void modelDataAdded(QModelIndex parent, int start, int end);
48 void modelDataRemoved(QModelIndex parent, int start, int end);
49 void barsetChanged();
47 void barsetChanged();
50
48
51 protected:
49 protected:
52 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
50 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
53 QAbstractItemModel* m_model;
51 QAbstractItemModel* m_model;
54 int m_mapCategories;
52 int m_mapCategories;
55 int m_mapBarBottom;
53 int m_mapBarBottom;
56 int m_mapBarTop;
54 int m_mapBarTop;
57 int m_mapFirst;
58 int m_mapCount;
59 Qt::Orientation m_mapOrientation;
55 Qt::Orientation m_mapOrientation;
60 private:
56 private:
61 Q_DECLARE_PUBLIC(QBarSeries)
57 Q_DECLARE_PUBLIC(QBarSeries)
62 };
58 };
63
59
64 QTCOMMERCIALCHART_END_NAMESPACE
60 QTCOMMERCIALCHART_END_NAMESPACE
65
61
66 #endif // QBARSERIESPRIVATE_P_H
62 #endif // QBARSERIESPRIVATE_P_H
@@ -1,236 +1,205
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 "qsplineseries.h"
21 #include "qsplineseries.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "splinechartitem_p.h"
23 #include "splinechartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 /*!
28 /*!
29 \class QSplineSeries
29 \class QSplineSeries
30 \brief Series type used to store data needed to draw a spline.
30 \brief Series type used to store data needed to draw a spline.
31
31
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34 */
34 */
35
35
36 /*!
36 /*!
37 \fn QSeriesType QSplineSeries::type() const
37 \fn QSeriesType QSplineSeries::type() const
38 Returns the type of the series
38 Returns the type of the series
39 */
39 */
40
40
41 /*!
41 /*!
42 \fn QSeriesType QSplineSeries::controlPoint(int index) const
42 \fn QSeriesType QSplineSeries::controlPoint(int index) const
43 Returns the control point specified by \a index
43 Returns the control point specified by \a index
44 */
44 */
45
45
46 QTCOMMERCIALCHART_BEGIN_NAMESPACE
46 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47
47
48 /*!
48 /*!
49 Constructs empty series object which is a child of \a parent.
49 Constructs empty series object which is a child of \a parent.
50 When series object is added to QChartView or QChart instance then the ownerships is transferred.
50 When series object is added to QChartView or QChart instance then the ownerships is transferred.
51 */
51 */
52
52
53 QSplineSeries::QSplineSeries(QObject *parent) :
53 QSplineSeries::QSplineSeries(QObject *parent) :
54 QLineSeries(*new QSplineSeriesPrivate(this),parent)
54 QLineSeries(*new QSplineSeriesPrivate(this),parent)
55 {
55 {
56 }
56 }
57
57
58 QAbstractSeries::QSeriesType QSplineSeries::type() const
58 QAbstractSeries::QSeriesType QSplineSeries::type() const
59 {
59 {
60 return QAbstractSeries::SeriesTypeSpline;
60 return QAbstractSeries::SeriesTypeSpline;
61 }
61 }
62
62
63 QPointF QSplineSeries::controlPoint(int index) const
63 QPointF QSplineSeries::controlPoint(int index) const
64 {
64 {
65 Q_D(const QSplineSeries);
65 Q_D(const QSplineSeries);
66 return d->m_controlPoints[index];
66 return d->m_controlPoints[index];
67 }
67 }
68
68
69 void QSplineSeries::setModelMappingRange(int first, int count)
70 {
71 Q_D(QSplineSeries);
72 QLineSeries::setModelMappingRange(first, count);
73 d->calculateControlPoints();
74 }
75
76 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77
70
78 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
71 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
79 {
72 {
80 QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
73 QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
81 QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
74 QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
82 QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
75 QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
83 };
76 };
84
77
85 /*!
78 /*!
86 \internal
79 \internal
87 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
80 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
88 */
81 */
89 void QSplineSeriesPrivate::calculateControlPoints()
82 void QSplineSeriesPrivate::calculateControlPoints()
90 {
83 {
91
84
92 Q_Q(QSplineSeries);
85 Q_Q(QSplineSeries);
93 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
86 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
94 // CPOL License
87 // CPOL License
95
88
96 int n = q->count() - 1;
89 int n = q->count() - 1;
97 if (n == 1)
90 if (n == 1)
98 { // Special case: Bezier curve should be a straight line.
91 { // Special case: Bezier curve should be a straight line.
99 // firstControlPoints = new Point[1];
92 // firstControlPoints = new Point[1];
100 // 3P1 = 2P0 + P3
93 // 3P1 = 2P0 + P3
101 m_controlPoints.append(QPointF((2 * q->x(0) + q->x(1)) / 3, (2 * q->y(0) + q->y(1)) / 3));
94 m_controlPoints.append(QPointF((2 * q->x(0) + q->x(1)) / 3, (2 * q->y(0) + q->y(1)) / 3));
102
95
103 // P2 = 2P1 P0
96 // P2 = 2P1 P0
104 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - q->x(0), 2 * m_controlPoints[0].y() - q->y(0)));
97 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - q->x(0), 2 * m_controlPoints[0].y() - q->y(0)));
105 return;
98 return;
106 }
99 }
107
100
108 // Calculate first Bezier control points
101 // Calculate first Bezier control points
109 // Right hand side vector
102 // Right hand side vector
110 // Set of equations for P0 to Pn points.
103 // Set of equations for P0 to Pn points.
111 //
104 //
112 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
105 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
113 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
106 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
114 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
107 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
115 // | . . . . . . . . . . . . | | ... | | ... |
108 // | . . . . . . . . . . . . | | ... | | ... |
116 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
109 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
117 // | . . . . . . . . . . . . | | ... | | ... |
110 // | . . . . . . . . . . . . | | ... | | ... |
118 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
111 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
119 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
112 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
120 //
113 //
121 QList<qreal> rhs;
114 QList<qreal> rhs;
122 rhs.append(q->x(0) + 2 * q->x(1));
115 rhs.append(q->x(0) + 2 * q->x(1));
123
116
124 // Set right hand side X values
117 // Set right hand side X values
125 for (int i = 1; i < n - 1; ++i)
118 for (int i = 1; i < n - 1; ++i)
126 rhs.append(4 * q->x(i) + 2 * q->x(i + 1));
119 rhs.append(4 * q->x(i) + 2 * q->x(i + 1));
127
120
128 rhs.append((8 * q->x(n - 1) + q->x(n)) / 2.0);
121 rhs.append((8 * q->x(n - 1) + q->x(n)) / 2.0);
129 // Get first control points X-values
122 // Get first control points X-values
130 QList<qreal> xControl = getFirstControlPoints(rhs);
123 QList<qreal> xControl = getFirstControlPoints(rhs);
131 rhs[0] = q->y(0) + 2 * q->y(1);
124 rhs[0] = q->y(0) + 2 * q->y(1);
132
125
133 // Set right hand side Y values
126 // Set right hand side Y values
134 for (int i = 1; i < n - 1; ++i)
127 for (int i = 1; i < n - 1; ++i)
135 rhs[i] = 4 * q->y(i) + 2 * q->y(i + 1);
128 rhs[i] = 4 * q->y(i) + 2 * q->y(i + 1);
136
129
137 rhs[n - 1] = (8 * q->y(n - 1) + q->y(n)) / 2.0;
130 rhs[n - 1] = (8 * q->y(n - 1) + q->y(n)) / 2.0;
138 // Get first control points Y-values
131 // Get first control points Y-values
139 QList<qreal> yControl = getFirstControlPoints(rhs);
132 QList<qreal> yControl = getFirstControlPoints(rhs);
140
133
141 // Fill output arrays.
134 // Fill output arrays.
142 for (int i = 0; i < n; ++i) {
135 for (int i = 0; i < n; ++i) {
143 // First control point
136 // First control point
144 m_controlPoints.append(QPointF(xControl[i], yControl[i]));
137 m_controlPoints.append(QPointF(xControl[i], yControl[i]));
145 // Second control point
138 // Second control point
146 if (i < n - 1)
139 if (i < n - 1)
147 m_controlPoints.append(QPointF(2 * q->x(i + 1) - xControl[i + 1], 2 * q->y(i + 1) - yControl[i + 1]));
140 m_controlPoints.append(QPointF(2 * q->x(i + 1) - xControl[i + 1], 2 * q->y(i + 1) - yControl[i + 1]));
148 else
141 else
149 m_controlPoints.append(QPointF((q->x(n) + xControl[n - 1]) / 2, (q->y(n) + yControl[n - 1]) / 2));
142 m_controlPoints.append(QPointF((q->x(n) + xControl[n - 1]) / 2, (q->y(n) + yControl[n - 1]) / 2));
150 }
143 }
151 }
144 }
152
145
153 /*!
146 /*!
154 \internal
147 \internal
155 */
148 */
156 QList<qreal> QSplineSeriesPrivate::getFirstControlPoints(QList<qreal> rhs)
149 QList<qreal> QSplineSeriesPrivate::getFirstControlPoints(QList<qreal> rhs)
157 {
150 {
158 QList<qreal> x; // Solution vector.
151 QList<qreal> x; // Solution vector.
159 QList<qreal> tmp; // Temp workspace.
152 QList<qreal> tmp; // Temp workspace.
160
153
161 qreal b = 2.0;
154 qreal b = 2.0;
162 x.append(rhs[0] / b);
155 x.append(rhs[0] / b);
163 tmp.append(0);
156 tmp.append(0);
164 for (int i = 1; i < rhs.size(); i++) {
157 for (int i = 1; i < rhs.size(); i++) {
165 // Decomposition and forward substitution.
158 // Decomposition and forward substitution.
166 tmp.append(1 / b);
159 tmp.append(1 / b);
167 b = (i < rhs.size() - 1 ? 4.0 : 3.5) - tmp[i];
160 b = (i < rhs.size() - 1 ? 4.0 : 3.5) - tmp[i];
168 x.append((rhs[i] - x[i - 1]) / b);
161 x.append((rhs[i] - x[i - 1]) / b);
169 }
162 }
170 for (int i = 1; i < rhs.size(); i++)
163 for (int i = 1; i < rhs.size(); i++)
171 x[rhs.size() - i - 1] -= tmp[rhs.size() - i] * x[rhs.size() - i]; // Backsubstitution.
164 x[rhs.size() - i - 1] -= tmp[rhs.size() - i] * x[rhs.size() - i]; // Backsubstitution.
172
165
173 return x;
166 return x;
174 }
167 }
175
168
176 /*!
169 /*!
177 \internal
170 \internal
178 Updates the control points, besed on currently avaiable knots.
171 Updates the control points, besed on currently avaiable knots.
179 */
172 */
180 void QSplineSeriesPrivate::updateControlPoints()
173 void QSplineSeriesPrivate::updateControlPoints()
181 {
174 {
182 Q_Q(QSplineSeries);
175 Q_Q(QSplineSeries);
183 if (q->count() > 1) {
176 if (q->count() > 1) {
184 m_controlPoints.clear();
177 m_controlPoints.clear();
185 calculateControlPoints();
178 calculateControlPoints();
186 }
179 }
187 }
180 }
188
181
189 /*//!
190 \fn bool QSplineSeries::setModel(QAbstractItemModel *model)
191 Sets the \a model to be used as a data source
192 \sa setModelMapping(), setModelMappingRange()
193 */
194 //bool QSplineSeries::setModel(QAbstractItemModel* model)
195 //{
196 // QXYSeries::setModel(model);
197 //// calculateControlPoints();
198 // return true;
199 //}
200
201 /*//!
202 \fn bool QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
203 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
204 as a data source for y coordinate. The \a orientation parameter specifies whether the data
205 is in columns or in rows.
206 */
207 //void QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
208 //{
209 // QLineSeries::setModelMapping(modelX, modelY, orientation);
210 //// calculateControlPoints();
211 //}
212
213 /*!
182 /*!
214 \fn bool QSplineSeries::setModelMappingRange(int first, int count)
183 \fn bool QSplineSeries::setModelMappingRange(int first, int count)
215 Allows limiting the model mapping.
184 Allows limiting the model mapping.
216 Parameter \a first specifies which element of the model should be used as a first one of the series.
185 Parameter \a first specifies which element of the model should be used as a first one of the series.
217 Parameter \a count specifies how many elements should be mapped. If count is not specified (defaults to -1)
186 Parameter \a count specifies how many elements should be mapped. If count is not specified (defaults to -1)
218 then all the items following \a first item in a model are used.
187 then all the items following \a first item in a model are used.
219 \sa setModel(), setModelMapping()
188 \sa setModel(), setModelMapping()
220 */
189 */
221
190
222 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
191 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
223 {
192 {
224 Q_Q(QSplineSeries);
193 Q_Q(QSplineSeries);
225 SplineChartItem* spline = new SplineChartItem(q,presenter);
194 SplineChartItem* spline = new SplineChartItem(q,presenter);
226 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
195 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
227 presenter->animator()->addAnimation(spline);
196 presenter->animator()->addAnimation(spline);
228 }
197 }
229 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
198 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
230 return spline;
199 return spline;
231 }
200 }
232
201
233 #include "moc_qsplineseries.cpp"
202 #include "moc_qsplineseries.cpp"
234 #include "moc_qsplineseries_p.cpp"
203 #include "moc_qsplineseries_p.cpp"
235
204
236 QTCOMMERCIALCHART_END_NAMESPACE
205 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,52
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 #ifndef QSPLINESERIES_H
21 #ifndef QSPLINESERIES_H
22 #define QSPLINESERIES_H
22 #define QSPLINESERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qlineseries.h>
25 #include <qlineseries.h>
26 #include <QList>
26 #include <QList>
27 #include <QPointF>
27 #include <QPointF>
28 #include <QtGlobal>
28 #include <QtGlobal>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QSplineSeriesPrivate;
32 class QSplineSeriesPrivate;
33
33
34 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
34 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 public:
37 public:
38
38
39 explicit QSplineSeries(QObject *parent = 0);
39 explicit QSplineSeries(QObject *parent = 0);
40 QAbstractSeries::QSeriesType type() const;
40 QAbstractSeries::QSeriesType type() const;
41
41
42 QPointF controlPoint(int index) const;
42 QPointF controlPoint(int index) const;
43 void setModelMappingRange(int first, int count);
44
43
45 private:
44 private:
46 Q_DECLARE_PRIVATE(QSplineSeries);
45 Q_DECLARE_PRIVATE(QSplineSeries);
47 Q_DISABLE_COPY(QSplineSeries);
46 Q_DISABLE_COPY(QSplineSeries);
48 friend class SplineChartItem;
47 friend class SplineChartItem;
49 };
48 };
50
49
51 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
52
51
53 #endif // QSPLINESERIES_H
52 #endif // QSPLINESERIES_H
@@ -1,670 +1,465
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 "qxyseries.h"
21 #include "qxyseries.h"
22 #include "qxyseries_p.h"
22 #include "qxyseries_p.h"
23 #include "domain_p.h"
23 #include "domain_p.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /*!
29 /*!
30 \class QXYSeries
30 \class QXYSeries
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 */
32 */
33
33
34 /*!
34 /*!
35 \fn QPen QXYSeries::pen() const
35 \fn QPen QXYSeries::pen() const
36 \brief Returns pen used to draw points for series.
36 \brief Returns pen used to draw points for series.
37 \sa setPen()
37 \sa setPen()
38 */
38 */
39
39
40 /*!
40 /*!
41 \fn QBrush QXYSeries::brush() const
41 \fn QBrush QXYSeries::brush() const
42 \brief Returns brush used to draw points for series.
42 \brief Returns brush used to draw points for series.
43 \sa setBrush()
43 \sa setBrush()
44 */
44 */
45
45
46 /*!
46 /*!
47 \fn void QXYSeries::clicked(const QPointF& point)
47 \fn void QXYSeries::clicked(const QPointF& point)
48 \brief Signal is emitted when user clicks the \a point on chart.
48 \brief Signal is emitted when user clicks the \a point on chart.
49 */
49 */
50
50
51 /*!
51 /*!
52 \fn void QXYSeries::selected()
52 \fn void QXYSeries::selected()
53
53
54 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
54 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
55 implemented by the user of QXYSeries API.
55 implemented by the user of QXYSeries API.
56 */
56 */
57
57
58 /*!
58 /*!
59 \fn void QXYSeriesPrivate::pointReplaced(int index)
59 \fn void QXYSeriesPrivate::pointReplaced(int index)
60 \brief \internal \a index
60 \brief \internal \a index
61 */
61 */
62
62
63 /*!
63 /*!
64 \fn void QXYSeriesPrivate::pointAdded(int index)
64 \fn void QXYSeriesPrivate::pointAdded(int index)
65 \brief \internal \a index
65 \brief \internal \a index
66 */
66 */
67
67
68 /*!
68 /*!
69 \fn void QXYSeriesPrivate::pointRemoved(int index)
69 \fn void QXYSeriesPrivate::pointRemoved(int index)
70 \brief \internal \a index
70 \brief \internal \a index
71 */
71 */
72
72
73 /*!
73 /*!
74 \fn void QXYSeriesPrivate::updated()
74 \fn void QXYSeriesPrivate::updated()
75 \brief \internal
75 \brief \internal
76 */
76 */
77
77
78 /*!
78 /*!
79 \fn int QXYSeries::mapFirst() const
80 Returns the index of the model's item that is used as a first one for the series.
81 \sa mapCount()
82 */
83
84 /*!
85 \fn int QXYSeries::mapCount() const
86 Returns the number of the items that are taken from the model.
87 If -1 it means all the items of the model following the first one are used.
88 \sa mapFirst()
89 */
90
91 /*!
92 \internal
79 \internal
93
80
94 Constructs empty series object which is a child of \a parent.
81 Constructs empty series object which is a child of \a parent.
95 When series object is added to QChartView or QChart instance ownerships is transferred.
82 When series object is added to QChartView or QChart instance ownerships is transferred.
96 */
83 */
97 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
84 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
98 {
85 {
99
86
100 }
87 }
101 /*!
88 /*!
102 Destroys the object. Series added to QChartView or QChart instances are owned by those,
89 Destroys the object. Series added to QChartView or QChart instances are owned by those,
103 and are deleted when mentioned object are destroyed.
90 and are deleted when mentioned object are destroyed.
104 */
91 */
105 QXYSeries::~QXYSeries()
92 QXYSeries::~QXYSeries()
106 {
93 {
107 }
94 }
108
95
109 /*!
96 /*!
110 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
97 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
111 */
98 */
112 void QXYSeries::append(qreal x,qreal y)
99 void QXYSeries::append(qreal x,qreal y)
113 {
100 {
114 Q_D(QXYSeries);
101 Q_D(QXYSeries);
115 Q_ASSERT(d->m_x.size() == d->m_y.size());
102 Q_ASSERT(d->m_x.size() == d->m_y.size());
116 d->m_x<<x;
103 d->m_x<<x;
117 d->m_y<<y;
104 d->m_y<<y;
118 emit d->pointAdded(d->m_x.size()-1);
105 emit d->pointAdded(d->m_x.size()-1);
119 }
106 }
120
107
121 /*!
108 /*!
122 This is an overloaded function.
109 This is an overloaded function.
123 Adds data \a point to the series. Points are connected with lines on the chart.
110 Adds data \a point to the series. Points are connected with lines on the chart.
124 */
111 */
125 void QXYSeries::append(const QPointF &point)
112 void QXYSeries::append(const QPointF &point)
126 {
113 {
127 append(point.x(),point.y());
114 append(point.x(),point.y());
128 }
115 }
129
116
130 /*!
117 /*!
131 This is an overloaded function.
118 This is an overloaded function.
132 Adds list of data \a points to the series. Points are connected with lines on the chart.
119 Adds list of data \a points to the series. Points are connected with lines on the chart.
133 */
120 */
134 void QXYSeries::append(const QList<QPointF> points)
121 void QXYSeries::append(const QList<QPointF> points)
135 {
122 {
136 foreach(const QPointF& point , points) {
123 foreach(const QPointF& point , points) {
137 append(point.x(),point.y());
124 append(point.x(),point.y());
138 }
125 }
139 }
126 }
140
127
141 /*!
128 /*!
142 Modifies \a y value for given \a x a value.
129 Modifies \a y value for given \a x a value.
143 */
130 */
144 void QXYSeries::replace(qreal x,qreal y)
131 void QXYSeries::replace(qreal x,qreal y)
145 {
132 {
146 Q_D(QXYSeries);
133 Q_D(QXYSeries);
147 int index = d->m_x.indexOf(x);
134 int index = d->m_x.indexOf(x);
148 d->m_x[index] = x;
135 d->m_x[index] = x;
149 d->m_y[index] = y;
136 d->m_y[index] = y;
150 emit d->pointReplaced(index);
137 emit d->pointReplaced(index);
151 }
138 }
152
139
153 /*!
140 /*!
154 This is an overloaded function.
141 This is an overloaded function.
155 Replaces current y value of for given \a point x value with \a point y value.
142 Replaces current y value of for given \a point x value with \a point y value.
156 */
143 */
157 void QXYSeries::replace(const QPointF &point)
144 void QXYSeries::replace(const QPointF &point)
158 {
145 {
159 replace(point.x(),point.y());
146 replace(point.x(),point.y());
160 }
147 }
161
148
162 /*!
149 /*!
163 Removes first \a x value and related y value.
150 Removes first \a x value and related y value.
164 */
151 */
165 void QXYSeries::remove(qreal x)
152 void QXYSeries::remove(qreal x)
166 {
153 {
167 Q_D(QXYSeries);
154 Q_D(QXYSeries);
168 int index = d->m_x.indexOf(x);
155 int index = d->m_x.indexOf(x);
169
156
170 if (index == -1) return;
157 if (index == -1) return;
171
158
172 d->m_x.remove(index);
159 d->m_x.remove(index);
173 d->m_y.remove(index);
160 d->m_y.remove(index);
174
161
175 emit d->pointRemoved(index);
162 emit d->pointRemoved(index);
176 }
163 }
177
164
178 /*!
165 /*!
179 Removes current \a x and \a y value.
166 Removes current \a x and \a y value.
180 */
167 */
181 void QXYSeries::remove(qreal x,qreal y)
168 void QXYSeries::remove(qreal x,qreal y)
182 {
169 {
183 Q_D(QXYSeries);
170 Q_D(QXYSeries);
184 int index =-1;
171 int index =-1;
185 do {
172 do {
186 index = d->m_x.indexOf(x,index+1);
173 index = d->m_x.indexOf(x,index+1);
187 } while (index !=-1 && d->m_y.at(index)!=y);
174 } while (index !=-1 && d->m_y.at(index)!=y);
188
175
189 if (index==-1) return;
176 if (index==-1) return;
190
177
191 d->m_x.remove(index);
178 d->m_x.remove(index);
192 d->m_y.remove(index);
179 d->m_y.remove(index);
193 emit d->pointRemoved(index);
180 emit d->pointRemoved(index);
194 }
181 }
195
182
196 /*!
183 /*!
197 Removes current \a point x value. Note \a point y value is ignored.
184 Removes current \a point x value. Note \a point y value is ignored.
198 */
185 */
199 void QXYSeries::remove(const QPointF &point)
186 void QXYSeries::remove(const QPointF &point)
200 {
187 {
201 remove(point.x(),point.y());
188 remove(point.x(),point.y());
202 }
189 }
203
190
204 /*!
191 /*!
205 Removes all data points from the series.
192 Removes all data points from the series.
206 */
193 */
207 void QXYSeries::removeAll()
194 void QXYSeries::removeAll()
208 {
195 {
209 Q_D(QXYSeries);
196 Q_D(QXYSeries);
210 d->m_x.clear();
197 d->m_x.clear();
211 d->m_y.clear();
198 d->m_y.clear();
212 }
199 }
213
200
214 /*!
201 /*!
215 \internal \a pos
202 \internal \a pos
216 */
203 */
217 qreal QXYSeries::x(int pos) const
204 qreal QXYSeries::x(int pos) const
218 {
205 {
219 Q_D(const QXYSeries);
206 Q_D(const QXYSeries);
220 if (d->m_model) {
207 if (d->m_model) {
221 if (d->m_mapOrientation == Qt::Vertical)
208 if (d->m_mapOrientation == Qt::Vertical)
222 // consecutive data is read from model's column
209 // consecutive data is read from model's column
223 return d->m_model->data(d->m_model->index(pos + d->m_mapFirst, d->m_mapX), Qt::DisplayRole).toDouble();
210 return d->m_model->data(d->m_model->index(pos, d->m_mapX), Qt::DisplayRole).toDouble();
224 else
211 else
225 // consecutive data is read from model's row
212 // consecutive data is read from model's row
226 return d->m_model->data(d->m_model->index(d->m_mapX, pos + d->m_mapFirst), Qt::DisplayRole).toDouble();
213 return d->m_model->data(d->m_model->index(d->m_mapX, pos), Qt::DisplayRole).toDouble();
227 } else {
214 } else {
228 // model is not specified, return the data from series' internal data store
215 // model is not specified, return the data from series' internal data store
229 return d->m_x.at(pos);
216 return d->m_x.at(pos);
230 }
217 }
231 }
218 }
232
219
233 /*!
220 /*!
234 \internal \a pos
221 \internal \a pos
235 */
222 */
236 qreal QXYSeries::y(int pos) const
223 qreal QXYSeries::y(int pos) const
237 {
224 {
238 Q_D(const QXYSeries);
225 Q_D(const QXYSeries);
239 if (d->m_model) {
226 if (d->m_model) {
240 if (d->m_mapOrientation == Qt::Vertical)
227 if (d->m_mapOrientation == Qt::Vertical)
241 // consecutive data is read from model's column
228 // consecutive data is read from model's column
242 return d->m_model->data(d->m_model->index(pos + d->m_mapFirst, d->m_mapY), Qt::DisplayRole).toDouble();
229 return d->m_model->data(d->m_model->index(pos, d->m_mapY), Qt::DisplayRole).toDouble();
243 else
230 else
244 // consecutive data is read from model's row
231 // consecutive data is read from model's row
245 return d->m_model->data(d->m_model->index(d->m_mapY, pos + d->m_mapFirst), Qt::DisplayRole).toDouble();
232 return d->m_model->data(d->m_model->index(d->m_mapY, pos), Qt::DisplayRole).toDouble();
246 } else {
233 } else {
247 // model is not specified, return the data from series' internal data store
234 // model is not specified, return the data from series' internal data store
248 return d->m_y.at(pos);
235 return d->m_y.at(pos);
249 }
236 }
250 }
237 }
251
238
252 /*!
239 /*!
253 Returns number of data points within series.
240 Returns number of data points within series.
254 */
241 */
255 int QXYSeries::count() const
242 int QXYSeries::count() const
256 {
243 {
257 Q_D(const QXYSeries);
244 Q_D(const QXYSeries);
258
245
259 Q_ASSERT(d->m_x.size() == d->m_y.size());
246 Q_ASSERT(d->m_x.size() == d->m_y.size());
260
247
261 if (d->m_model) {
248 if (d->m_model) {
262 if (d->m_mapOrientation == Qt::Vertical) {
249 if (d->m_mapOrientation == Qt::Vertical) {
263 // data is in a column. Return the number of mapped items if the model's column have enough items
250 // data is in a column. Return the number of mapped items
264 // or the number of items that can be mapped
251 return d->m_model->rowCount();
265 if (d->m_mapLimited)
266 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
267 else
268 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
269 } else {
252 } else {
270 // data is in a row. Return the number of mapped items if the model's row have enough items
253 // data is in a row. Return the number of mapped items
271 // or the number of items that can be mapped
254 return d->m_model->columnCount();
272 if (d->m_mapLimited)
273 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
274 else
275 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
276 }
255 }
277 }
256 }
278
257
279 // model is not specified, return the number of points in the series internal data store
258 // model is not specified, return the number of points in the series internal data store
280 return d->m_x.size();
259 return d->m_x.size();
281 }
260 }
282
261
283 /*!
262 /*!
284 Returns the data points of the series.
263 Returns the data points of the series.
285 */
264 */
286 QList<QPointF> QXYSeries::data()
265 QList<QPointF> QXYSeries::data()
287 {
266 {
288 Q_D(QXYSeries);
267 Q_D(QXYSeries);
289 QList<QPointF> data;
268 QList<QPointF> data;
290 for (int i(0); i < d->m_x.count() && i < d->m_y.count(); i++)
269 for (int i(0); i < d->m_x.count() && i < d->m_y.count(); i++)
291 data.append(QPointF(d->m_x.at(i), d->m_y.at(i)));
270 data.append(QPointF(d->m_x.at(i), d->m_y.at(i)));
292 return data;
271 return data;
293 }
272 }
294
273
295
274
296 /*!
275 /*!
297 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
276 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
298 pen from chart theme is used.
277 pen from chart theme is used.
299 \sa QChart::setTheme()
278 \sa QChart::setTheme()
300 */
279 */
301 void QXYSeries::setPen(const QPen &pen)
280 void QXYSeries::setPen(const QPen &pen)
302 {
281 {
303 Q_D(QXYSeries);
282 Q_D(QXYSeries);
304 if (d->m_pen!=pen) {
283 if (d->m_pen!=pen) {
305 d->m_pen = pen;
284 d->m_pen = pen;
306 emit d->updated();
285 emit d->updated();
307 }
286 }
308 }
287 }
309
288
310 QPen QXYSeries::pen() const
289 QPen QXYSeries::pen() const
311 {
290 {
312 Q_D(const QXYSeries);
291 Q_D(const QXYSeries);
313 return d->m_pen;
292 return d->m_pen;
314 }
293 }
315
294
316 /*!
295 /*!
317 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
296 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
318 from chart theme setting is used.
297 from chart theme setting is used.
319 \sa QChart::setTheme()
298 \sa QChart::setTheme()
320 */
299 */
321 void QXYSeries::setBrush(const QBrush &brush)
300 void QXYSeries::setBrush(const QBrush &brush)
322 {
301 {
323 Q_D(QXYSeries);
302 Q_D(QXYSeries);
324 if (d->m_brush!=brush) {
303 if (d->m_brush!=brush) {
325 d->m_brush = brush;
304 d->m_brush = brush;
326 emit d->updated();
305 emit d->updated();
327 }
306 }
328 }
307 }
329
308
330 QBrush QXYSeries::brush() const
309 QBrush QXYSeries::brush() const
331 {
310 {
332 Q_D(const QXYSeries);
311 Q_D(const QXYSeries);
333 return d->m_brush;
312 return d->m_brush;
334 }
313 }
335
314
336
315
337 /*!
316 /*!
338 Sets if data points are \a visible and should be drawn on line.
317 Sets if data points are \a visible and should be drawn on line.
339 */
318 */
340 void QXYSeries::setPointsVisible(bool visible)
319 void QXYSeries::setPointsVisible(bool visible)
341 {
320 {
342 Q_D(QXYSeries);
321 Q_D(QXYSeries);
343 if (d->m_pointsVisible != visible){
322 if (d->m_pointsVisible != visible){
344 d->m_pointsVisible = visible;
323 d->m_pointsVisible = visible;
345 emit d->updated();
324 emit d->updated();
346 }
325 }
347 }
326 }
348
327
349 /*!
328 /*!
350 Returns true if drawing the data points of the series is enabled.
329 Returns true if drawing the data points of the series is enabled.
351 */
330 */
352 bool QXYSeries::pointsVisible() const
331 bool QXYSeries::pointsVisible() const
353 {
332 {
354 Q_D(const QXYSeries);
333 Q_D(const QXYSeries);
355 return d->m_pointsVisible;
334 return d->m_pointsVisible;
356 }
335 }
357
336
358
337
359 /*!
338 /*!
360 Stream operator for adding a data \a point to the series.
339 Stream operator for adding a data \a point to the series.
361 \sa append()
340 \sa append()
362 */
341 */
363 QXYSeries& QXYSeries::operator<< (const QPointF &point)
342 QXYSeries& QXYSeries::operator<< (const QPointF &point)
364 {
343 {
365 append(point);
344 append(point);
366 return *this;
345 return *this;
367 }
346 }
368
347
369
348
370 /*!
349 /*!
371 Stream operator for adding a list of \a points to the series.
350 Stream operator for adding a list of \a points to the series.
372 \sa append()
351 \sa append()
373 */
352 */
374
353
375 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
354 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
376 {
355 {
377 append(points);
356 append(points);
378 return *this;
357 return *this;
379 }
358 }
380
359
381 /*!
360 /*!
382 \internal
361 \internal
383 */
362 */
384 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
363 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
385 {
364 {
386 Q_UNUSED(bottomRight)
365 Q_UNUSED(bottomRight)
387 Q_D(QXYSeries);
366 Q_D(QXYSeries);
388 if (d->m_mapOrientation == Qt::Vertical) {
367 if (d->m_mapOrientation == Qt::Vertical) {
389 if (topLeft.row() >= d->m_mapFirst && (!d->m_mapLimited || topLeft.row() < d->m_mapFirst + d->m_mapCount))
368 if (topLeft.column() == d->m_mapX || topLeft.column() == d->m_mapY)
390 emit d->pointReplaced(topLeft.row() - d->m_mapFirst);
369 emit d->pointReplaced(topLeft.row());
391 } else {
392 if (topLeft.column() >= d->m_mapFirst && (!d->m_mapLimited || topLeft.column() < d->m_mapFirst + d->m_mapCount))
393 emit d->pointReplaced(topLeft.column() - d->m_mapFirst);
394 }
395 }
396
397 /*!
398 \internal
399 */
400 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
401 {
402 Q_UNUSED(parent)
403 // Q_UNUSED(end)
404 Q_D(QXYSeries);
405 if (d->m_mapLimited) {
406 if (start >= d->m_mapFirst + d->m_mapCount) {
407 // the added data is below mapped area
408 // therefore it has no relevance
409 return;
410 } else {
411 // the added data is in the mapped area or before it and update is needed
412
413 // check how many mapped items there is currently (before new items are added)
414 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
415 // internal storage before new ones can be added
416
417 int itemsToRemove = qMin(count() - qMax(start - d->m_mapFirst, 0), end - start + 1);
418 if (d->m_mapCount == count()) {
419 for (int i = 0; i < itemsToRemove; i++)
420 emit d->pointRemoved(qMin(end, count()) - i);
421 }
422 }
423 } else {
424 // map is not limited (it includes all the items starting from m_mapFirst till the end of model)
425 // nothing to do
426 // emit pointAdded(qMax(start - m_mapFirst, 0));
427 }
428 }
429
430 /*!
431 \internal
432 */
433 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
434 {
435 Q_UNUSED(parent)
436 // Q_UNUSED(end)
437 Q_D(QXYSeries);
438 if (d->m_mapLimited) {
439 if (start >= d->m_mapFirst + d->m_mapCount) {
440 // the added data is below mapped area
441 // therefore it has no relevance
442 return;
443 } else {
444 // the added data is in the mapped area or before it
445 // update needed
446 if (count() > 0) {
447 int toBeAdded = qMin(d->m_mapCount - (start - d->m_mapFirst), end - start + 1);
448 for (int i = 0; i < toBeAdded; i++)
449 if (start + i >= d->m_mapFirst)
450 emit d->pointAdded(start + i);
451 }
452 }
453 } else {
454 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
455 for (int i = 0; i < end - start + 1; i++)
456 emit d->pointAdded(start + i);
457 }
458 }
459
460 /*!
461 \internal
462 */
463 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end)
464 {
465 Q_UNUSED(parent)
466 // Q_UNUSED(end)
467 Q_D(QXYSeries);
468 if (d->m_mapLimited) {
469 if (start >= d->m_mapFirst + d->m_mapCount) {
470 // the removed data is below mapped area
471 // therefore it has no relevance
472 return;
473 } else {
474 // the removed data is in the mapped area or before it
475 // update needed
476
477 // check how many items need to be removed from the xychartitem storage
478 // the number equals the number of items that are removed and that lay before
479 // or in the mapped area. Items that lay beyond the map do not count
480 // the max is the current number of items in storage (count())
481 int itemsToRemove = qMin(count(), qMin(end, d->m_mapFirst + d->m_mapCount - 1) - start + 1);
482 for (int i = 0; i < itemsToRemove; i++)
483 emit d->pointRemoved(start);
484 }
485 } else {
486 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
487 for (int i = 0; i < end - start + 1; i++)
488 emit d->pointRemoved(start);
489 }
490 }
491
492 /*!
493 \internal
494 */
495 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
496 {
497
498 Q_UNUSED(parent)
499 Q_UNUSED(end)
500 Q_D(QXYSeries);
501 // how many items there were before data was removed
502 // int oldCount = count() - 1;
503
504 if (d->m_mapLimited) {
505 if (start >= d->m_mapFirst + d->m_mapCount) {
506 // the removed data is below mapped area
507 // therefore it has no relevance
508 return;
509 } else {
510 // if the current items count in the whole model is bigger than the index of the last item
511 // that was removed than it means there are some extra items available
512
513 int removedItemsCount = qMin(count(), qMin(end, d->m_mapFirst + d->m_mapCount - 1) - start + 1);
514 int extraItemsAvailable = 0;
515 if (d->m_mapOrientation == Qt::Vertical) {
516 extraItemsAvailable = qMax(d->m_model->rowCount() + (end - start + 1) - qMax(end + 1, d->m_mapFirst + d->m_mapCount), 0);
517 } else {
518 extraItemsAvailable = qMax(d->m_model->columnCount() + (end - start + 1) - qMax(end + 1, d->m_mapFirst + d->m_mapCount), 0);
519 }
520
521 // if there are excess items available (below the mapped area) use them to repopulate mapped area
522 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
523 for (int k = 0; k < toBeAdded; k++)
524 emit d->pointAdded(d->m_mapFirst + d->m_mapCount - removedItemsCount + k);
525 }
526 } else {
370 } else {
527 // data was removed from XYSeries interal storage. Nothing more to do
371 if (topLeft.row() == d->m_mapX || topLeft.row() == d->m_mapY)
372 emit d->pointReplaced(topLeft.column());
528 }
373 }
529 }
374 }
530
375
531 /*!
376 /*!
532 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
377 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
533 Sets the \a model to be used as a data source
378 Sets the \a model to be used as a data source
534 \sa setModelMapping(), setModelMappingRange()
379 \sa setModelMapping(), setModelMappingRange()
535 */
380 */
536 bool QXYSeries::setModel(QAbstractItemModel *model)
381 bool QXYSeries::setModel(QAbstractItemModel *model)
537 {
382 {
538 Q_D(QXYSeries);
383 Q_D(QXYSeries);
539 // disconnect signals from old model
384 // disconnect signals from old model
540 if (d->m_model) {
385 if (d->m_model) {
541 QObject::disconnect(d->m_model, 0, this, 0);
386 QObject::disconnect(d->m_model, 0, this, 0);
542 d->m_mapX = -1;
387 d->m_mapX = -1;
543 d->m_mapY = -1;
388 d->m_mapY = -1;
544 d->m_mapFirst = 0;
545 d->m_mapCount = 0;
546 d->m_mapLimited = false;
547 d->m_mapOrientation = Qt::Vertical;
389 d->m_mapOrientation = Qt::Vertical;
548 }
390 }
549
391
550 // set new model
392 // set new model
551 if (model) {
393 if (model) {
552 d->m_model = model;
394 d->m_model = model;
553 return true;
395 return true;
554 } else {
396 } else {
555 d->m_model = 0;
397 d->m_model = 0;
556 return false;
398 return false;
557 }
399 }
558 }
400 }
559
401
560 /*!
402 /*!
561 \fn bool QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
403 \fn bool QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
562 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
404 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
563 as a data source for y coordinate. The \a orientation parameter specifies whether the data
405 as a data source for y coordinate. The \a orientation parameter specifies whether the data
564 is in columns or in rows.
406 is in columns or in rows.
565 \sa setModel(), setModelMappingRange()
407 \sa setModel(), setModelMappingRange()
566 */
408 */
567 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
409 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
568 {
410 {
569 Q_D(QXYSeries);
411 Q_D(QXYSeries);
570 if (d->m_model == 0)
412 if (d->m_model == 0)
571 return;
413 return;
572 d->m_mapX = modelX;
414 d->m_mapX = modelX;
573 d->m_mapY = modelY;
415 d->m_mapY = modelY;
574 d->m_mapFirst = 0;
575 d->m_mapOrientation = orientation;
416 d->m_mapOrientation = orientation;
576 if (d->m_mapOrientation == Qt::Vertical) {
417 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
577 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
578 connect(d->m_model,SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
579 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
580 connect(d->m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
581 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
582 } else {
583 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
584 connect(d->m_model,SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
585 connect(d->m_model,SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
586 connect(d->m_model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
587 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
588 }
589 }
590
591 /*!
592 \fn bool QXYSeries::setModelMappingRange(int first, int count)
593 Allows limiting the model mapping.
594 Parameter \a first specifies which element of the model should be used as a first one of the series.
595 Parameter \a count specifies how many elements should be mapped. If count is not specified (defaults to -1)
596 then all the items following \a first item in a model are used.
597 \sa setModel(), setModelMapping()
598 */
599 void QXYSeries::setModelMappingRange(int first, int count)
600 {
601 Q_D(QXYSeries);
602 d->m_mapFirst = first;
603 if (count == 0) {
604 d->m_mapLimited = false;
605 } else {
606 d->m_mapCount = count;
607 d->m_mapLimited = true;
608 }
609 }
610
611 int QXYSeries::mapFirst() const
612 {
613 Q_D(const QXYSeries);
614 return d->m_mapFirst;
615 }
616
617 int QXYSeries::mapCount() const
618 {
619 Q_D(const QXYSeries);
620 return d->m_mapCount;
621 }
418 }
622
419
623 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
420 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
624
421
422
625 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
423 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
626 m_mapX(-1),
424 m_mapX(-1),
627 m_mapY(-1),
425 m_mapY(-1),
628 m_mapFirst(0),
629 m_mapCount(0),
630 m_mapLimited(false),
631 m_mapOrientation( Qt::Vertical),
426 m_mapOrientation( Qt::Vertical),
632 m_pointsVisible(false)
427 m_pointsVisible(false)
633 {
428 {
634 }
429 }
635
430
636 void QXYSeriesPrivate::scaleDomain(Domain& domain)
431 void QXYSeriesPrivate::scaleDomain(Domain& domain)
637 {
432 {
638 qreal minX(domain.minX());
433 qreal minX(domain.minX());
639 qreal minY(domain.minY());
434 qreal minY(domain.minY());
640 qreal maxX(domain.maxX());
435 qreal maxX(domain.maxX());
641 qreal maxY(domain.maxY());
436 qreal maxY(domain.maxY());
642 int tickXCount(domain.tickXCount());
437 int tickXCount(domain.tickXCount());
643 int tickYCount(domain.tickYCount());
438 int tickYCount(domain.tickYCount());
644
439
645 Q_Q(QXYSeries);
440 Q_Q(QXYSeries);
646 for (int i = 0; i < q->count(); i++)
441 for (int i = 0; i < q->count(); i++)
647 {
442 {
648 qreal x = q->x(i);
443 qreal x = q->x(i);
649 qreal y = q->y(i);
444 qreal y = q->y(i);
650 minX = qMin(minX, x);
445 minX = qMin(minX, x);
651 minY = qMin(minY, y);
446 minY = qMin(minY, y);
652 maxX = qMax(maxX, x);
447 maxX = qMax(maxX, x);
653 maxY = qMax(maxY, y);
448 maxY = qMax(maxY, y);
654 }
449 }
655
450
656 domain.setRangeX(minX,maxX,tickXCount);
451 domain.setRangeX(minX,maxX,tickXCount);
657 domain.setRangeY(minY,maxY,tickYCount);
452 domain.setRangeY(minY,maxY,tickYCount);
658 }
453 }
659
454
660 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
455 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
661 {
456 {
662 Q_Q(QXYSeries);
457 Q_Q(QXYSeries);
663 QList<LegendMarker*> list;
458 QList<LegendMarker*> list;
664 return list << new XYLegendMarker(q,legend);
459 return list << new XYLegendMarker(q,legend);
665 }
460 }
666
461
667 #include "moc_qxyseries.cpp"
462 #include "moc_qxyseries.cpp"
668 #include "moc_qxyseries_p.cpp"
463 #include "moc_qxyseries_p.cpp"
669
464
670 QTCOMMERCIALCHART_END_NAMESPACE
465 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,97 +1,89
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 #ifndef QXYSERIES_H
21 #ifndef QXYSERIES_H
22 #define QXYSERIES_H
22 #define QXYSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 class QModelIndex;
29 class QModelIndex;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 class QXYSeriesPrivate;
33 class QXYSeriesPrivate;
34
34
35 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
35 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 {
36 {
37 Q_OBJECT
37 Q_OBJECT
38 protected:
38 protected:
39 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
39 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
40 ~QXYSeries();
40 ~QXYSeries();
41
41
42 public:
42 public:
43 void append(qreal x, qreal y);
43 void append(qreal x, qreal y);
44 void append(const QPointF &point);
44 void append(const QPointF &point);
45 void append(const QList<QPointF> points);
45 void append(const QList<QPointF> points);
46 void replace(qreal x,qreal y);
46 void replace(qreal x,qreal y);
47 void replace(const QPointF &point);
47 void replace(const QPointF &point);
48 void remove(qreal x);
48 void remove(qreal x);
49 void remove(qreal x, qreal y);
49 void remove(qreal x, qreal y);
50 void remove(const QPointF &point);
50 void remove(const QPointF &point);
51 void removeAll();
51 void removeAll();
52
52
53 int count() const;
53 int count() const;
54 qreal x(int pos) const;
54 qreal x(int pos) const;
55 qreal y(int pos) const;
55 qreal y(int pos) const;
56 QList<QPointF> data();
56 QList<QPointF> data();
57
57
58 QXYSeries& operator << (const QPointF &point);
58 QXYSeries& operator << (const QPointF &point);
59 QXYSeries& operator << (const QList<QPointF> points);
59 QXYSeries& operator << (const QList<QPointF> points);
60
60
61 void setPen(const QPen &pen);
61 void setPen(const QPen &pen);
62 QPen pen() const;
62 QPen pen() const;
63
63
64 void setBrush(const QBrush &brush);
64 void setBrush(const QBrush &brush);
65 QBrush brush() const;
65 QBrush brush() const;
66
66
67 void setPointsVisible(bool visible = true);
67 void setPointsVisible(bool visible = true);
68 bool pointsVisible() const;
68 bool pointsVisible() const;
69
69
70 bool setModel(QAbstractItemModel *model);
70 bool setModel(QAbstractItemModel *model);
71
72 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
71 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
73 virtual void setModelMappingRange(int first, int count = 0);
74 int mapFirst() const;
75 int mapCount() const;
76
72
77 private Q_SLOTS:
73 private Q_SLOTS:
78 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
74 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
79 void modelDataAboutToBeAdded(QModelIndex parent, int start, int end);
80 void modelDataAdded(QModelIndex parent, int start, int end);
81 void modelDataAboutToBeRemoved(QModelIndex parent, int start, int end);
82 void modelDataRemoved(QModelIndex parent, int start, int end);
83
75
84 Q_SIGNALS:
76 Q_SIGNALS:
85 void clicked(const QPointF &point);
77 void clicked(const QPointF &point);
86 void selected();
78 void selected();
87
79
88 private:
80 private:
89 Q_DECLARE_PRIVATE(QXYSeries);
81 Q_DECLARE_PRIVATE(QXYSeries);
90 Q_DISABLE_COPY(QXYSeries);
82 Q_DISABLE_COPY(QXYSeries);
91 friend class XYLegendMarker;
83 friend class XYLegendMarker;
92 friend class XYChartItem;
84 friend class XYChartItem;
93 };
85 };
94
86
95 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
96
88
97 #endif
89 #endif
@@ -1,79 +1,75
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QXYSERIES_P_H
30 #ifndef QXYSERIES_P_H
31 #define QXYSERIES_P_H
31 #define QXYSERIES_P_H
32
32
33 #include "qabstractseries_p.h"
33 #include "qabstractseries_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QXYSeries;
37 class QXYSeries;
38
38
39 class QXYSeriesPrivate: public QAbstractSeriesPrivate
39 class QXYSeriesPrivate: public QAbstractSeriesPrivate
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42
42
43 public:
43 public:
44 QXYSeriesPrivate(QXYSeries* q);
44 QXYSeriesPrivate(QXYSeries* q);
45
45
46 void scaleDomain(Domain& domain);
46 void scaleDomain(Domain& domain);
47 QList<LegendMarker*> createLegendMarker(QLegend* legend);
47 QList<LegendMarker*> createLegendMarker(QLegend* legend);
48
48
49 Q_SIGNALS:
49 Q_SIGNALS:
50 void updated();
50 void updated();
51 void pointReplaced(int index);
51 void pointReplaced(int index);
52 void pointRemoved(int index);
52 void pointRemoved(int index);
53 void pointAdded(int index);
53 void pointAdded(int index);
54
54
55 protected:
55 protected:
56 QVector<qreal> m_x;
56 QVector<qreal> m_x;
57 QVector<qreal> m_y;
57 QVector<qreal> m_y;
58
58
59 QPen m_pen;
59 QPen m_pen;
60 QBrush m_brush;
60 QBrush m_brush;
61
61
62 int m_mapX;
62 int m_mapX;
63 int m_mapY;
63 int m_mapY;
64 int m_mapFirst;
65 int m_mapCount;
66 bool m_mapLimited;
67 Qt::Orientation m_mapOrientation;
64 Qt::Orientation m_mapOrientation;
68 int tempItemsRemoved;
69 bool m_pointsVisible;
65 bool m_pointsVisible;
70
66
71 private:
67 private:
72 Q_DECLARE_PUBLIC(QXYSeries);
68 Q_DECLARE_PUBLIC(QXYSeries);
73 friend class QScatterSeries;
69 friend class QScatterSeries;
74
70
75 };
71 };
76
72
77 QTCOMMERCIALCHART_END_NAMESPACE
73 QTCOMMERCIALCHART_END_NAMESPACE
78
74
79 #endif
75 #endif
@@ -1,202 +1,178
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 "xychartitem_p.h"
21 #include "xychartitem_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsSceneMouseEvent>
28
28
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 //TODO: optimize : remove points which are not visible
32 //TODO: optimize : remove points which are not visible
33
33
34 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
34 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
35 m_minX(0),
35 m_minX(0),
36 m_maxX(0),
36 m_maxX(0),
37 m_minY(0),
37 m_minY(0),
38 m_maxY(0),
38 m_maxY(0),
39 m_series(series)
39 m_series(series)
40 {
40 {
41 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
41 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
42 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
42 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
43 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
43 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
44 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
44 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
45 }
45 }
46
46
47 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
47 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
48 {
48 {
49 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
49 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
50 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
50 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
51 qreal x = (point.x() - m_minX)* deltaX;
51 qreal x = (point.x() - m_minX)* deltaX;
52 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
52 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
53 return QPointF(x,y);
53 return QPointF(x,y);
54 }
54 }
55
55
56
56
57 QPointF XYChartItem::calculateGeometryPoint(int index) const
57 QPointF XYChartItem::calculateGeometryPoint(int index) const
58 {
58 {
59 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
59 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
60 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
60 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
61 qreal x = (m_series->x(index) - m_minX)* deltaX;
61 qreal x = (m_series->x(index) - m_minX)* deltaX;
62 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
62 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
63 return QPointF(x,y);
63 return QPointF(x,y);
64 }
64 }
65
65
66 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
66 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
67 {
67 {
68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
70
70
71 QVector<QPointF> points;
71 QVector<QPointF> points;
72 points.reserve(m_series->count());
72 points.reserve(m_series->count());
73 for (int i = 0; i < m_series->count(); ++i) {
73 for (int i = 0; i < m_series->count(); ++i) {
74 qreal x = (m_series->x(i) - m_minX)* deltaX;
74 qreal x = (m_series->x(i) - m_minX)* deltaX;
75 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
75 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
76 points << QPointF(x,y);
76 points << QPointF(x,y);
77 }
77 }
78 return points;
78 return points;
79 }
79 }
80
80
81 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
81 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
82 {
82 {
83 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
83 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
84 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
84 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
85 qreal x = point.x()/deltaX +m_minX;
85 qreal x = point.x()/deltaX +m_minX;
86 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
86 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
87 return QPointF(x,y);
87 return QPointF(x,y);
88 }
88 }
89
89
90 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
90 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
91 {
91 {
92 if (animator()) {
92 if (animator()) {
93 animator()->updateLayout(this,oldPoints,newPoints,index);
93 animator()->updateLayout(this,oldPoints,newPoints,index);
94 } else {
94 } else {
95 setLayout(newPoints);
95 setLayout(newPoints);
96 }
96 }
97 }
97 }
98
98
99 void XYChartItem::setLayout(QVector<QPointF> &points)
99 void XYChartItem::setLayout(QVector<QPointF> &points)
100 {
100 {
101 m_points = points;
101 m_points = points;
102 update();
102 update();
103 }
103 }
104
104
105 //handlers
105 //handlers
106
106
107 void XYChartItem::handlePointAdded(int index)
107 void XYChartItem::handlePointAdded(int index)
108 {
108 {
109 QVector<QPointF> points = m_points;
109 QVector<QPointF> points = m_points;
110 QPointF point;
110 QPointF point;
111 if (m_series->model()) {
111 Q_ASSERT(index<m_series->count());
112 point = calculateGeometryPoint(index - m_series->mapFirst());
112 Q_ASSERT(index>=0);
113 if (index < m_series->mapFirst()) {
113 point = calculateGeometryPoint(index);
114 points.insert(0, point);
114 points.insert(index, point);
115 updateLayout(m_points, points, 0);
115 updateLayout(m_points, points, index);
116 }
117 else {
118 points.insert(index - m_series->mapFirst(), point);
119 updateLayout(m_points, points, index - m_series->mapFirst());
120 }
121 }
122 else {
123 // this checks do not work correctly if model is set
124 Q_ASSERT(index<m_series->count());
125 Q_ASSERT(index>=0);
126 point = calculateGeometryPoint(index);
127 points.insert(index, point);
128 updateLayout(m_points, points, index);
129 }
130 update();
116 update();
131 }
117 }
132 void XYChartItem::handlePointRemoved(int index)
118 void XYChartItem::handlePointRemoved(int index)
133 {
119 {
134 QVector<QPointF> points = m_points;
120 QVector<QPointF> points = m_points;
135 if (m_series->model()) {
121 Q_ASSERT(index<m_series->count() + 1);
136 if (index < m_series->mapFirst())
122 Q_ASSERT(index>=0);
137 points.remove(0);
123 points.remove(index);
138 else
124 updateLayout(m_points, points, index);
139 points.remove(index - m_series->mapFirst());
140 updateLayout(m_points, points, index - m_series->mapFirst());
141 }
142 else {
143 // this checks do not work correctly if model is set
144 Q_ASSERT(index<m_series->count() + 1);
145 Q_ASSERT(index>=0);
146 points.remove(index);
147 updateLayout(m_points, points, index);
148 }
149 update();
125 update();
150 }
126 }
151
127
152 void XYChartItem::handlePointReplaced(int index)
128 void XYChartItem::handlePointReplaced(int index)
153 {
129 {
154 Q_ASSERT(index<m_series->count());
130 Q_ASSERT(index<m_series->count());
155 Q_ASSERT(index>=0);
131 Q_ASSERT(index>=0);
156 QPointF point = calculateGeometryPoint(index);
132 QPointF point = calculateGeometryPoint(index);
157 QVector<QPointF> points = m_points;
133 QVector<QPointF> points = m_points;
158 points.replace(index,point);
134 points.replace(index,point);
159 updateLayout(m_points,points,index);
135 updateLayout(m_points,points,index);
160 update();
136 update();
161 }
137 }
162
138
163 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
139 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
164 {
140 {
165 m_minX=minX;
141 m_minX=minX;
166 m_maxX=maxX;
142 m_maxX=maxX;
167 m_minY=minY;
143 m_minY=minY;
168 m_maxY=maxY;
144 m_maxY=maxY;
169
145
170 if (isEmpty()) return;
146 if (isEmpty()) return;
171 QVector<QPointF> points = calculateGeometryPoints();
147 QVector<QPointF> points = calculateGeometryPoints();
172 updateLayout(m_points,points);
148 updateLayout(m_points,points);
173 update();
149 update();
174 }
150 }
175
151
176 void XYChartItem::handleGeometryChanged(const QRectF &rect)
152 void XYChartItem::handleGeometryChanged(const QRectF &rect)
177 {
153 {
178 Q_ASSERT(rect.isValid());
154 Q_ASSERT(rect.isValid());
179 m_size=rect.size();
155 m_size=rect.size();
180 m_clipRect=rect.translated(-rect.topLeft());
156 m_clipRect=rect.translated(-rect.topLeft());
181 setPos(rect.topLeft());
157 setPos(rect.topLeft());
182
158
183 if (isEmpty()) return;
159 if (isEmpty()) return;
184 QVector<QPointF> points = calculateGeometryPoints();
160 QVector<QPointF> points = calculateGeometryPoints();
185 updateLayout(m_points,points);
161 updateLayout(m_points,points);
186 update();
162 update();
187 }
163 }
188
164
189
165
190 bool XYChartItem::isEmpty()
166 bool XYChartItem::isEmpty()
191 {
167 {
192 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
168 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
193 }
169 }
194
170
195 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
171 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
196 {
172 {
197 emit clicked(calculateDomainPoint(event->pos()));
173 emit clicked(calculateDomainPoint(event->pos()));
198 }
174 }
199
175
200 #include "moc_xychartitem_p.cpp"
176 #include "moc_xychartitem_p.cpp"
201
177
202 QTCOMMERCIALCHART_END_NAMESPACE
178 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,336 +1,336
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 "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QStyledItemDelegate>
24 #include <QStyledItemDelegate>
25 #include <QLineSeries>
25 #include <QLineSeries>
26 #include <QSplineSeries>
26 #include <QSplineSeries>
27 #include <QScatterSeries>
27 #include <QScatterSeries>
28 #include "customtablemodel.h"
28 #include "customtablemodel.h"
29 #include <QPieSeries>
29 #include <QPieSeries>
30 #include <QPieSlice>
30 #include <QPieSlice>
31 #include <QAreaSeries>
31 #include <QAreaSeries>
32 #include <QBarSeries>
32 #include <QBarSeries>
33 #include <QBarSet>
33 #include <QBarSet>
34 #include <QPushButton>
34 #include <QPushButton>
35 #include <QRadioButton>
35 #include <QRadioButton>
36 #include <QLabel>
36 #include <QLabel>
37 #include <QSpinBox>
37 #include <QSpinBox>
38 #include <QTime>
38 #include <QTime>
39
39
40 TableWidget::TableWidget(QWidget *parent)
40 TableWidget::TableWidget(QWidget *parent)
41 : QWidget(parent)
41 : QWidget(parent)
42 {
42 {
43 setGeometry(100, 100, 1000, 600);
43 setGeometry(100, 100, 1000, 600);
44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 // create simple model for storing data
45 // create simple model for storing data
46 // user's table data model
46 // user's table data model
47 m_model = new CustomTableModel;
47 m_model = new CustomTableModel;
48 m_tableView = new QTableView;
48 m_tableView = new QTableView;
49 m_tableView->setModel(m_model);
49 m_tableView->setModel(m_model);
50 m_tableView->setMinimumHeight(300);
50 m_tableView->setMinimumHeight(300);
51 // tableView->setMinimumSize(340, 480);
51 // tableView->setMinimumSize(340, 480);
52 // tableView->setItemDelegate(new QStyledItemDelegate);
52 // tableView->setItemDelegate(new QStyledItemDelegate);
53 m_chart = new QChart;
53 m_chart = new QChart;
54 m_chartView = new QChartView(m_chart);
54 m_chartView = new QChartView(m_chart);
55 m_chartView->setRenderHint(QPainter::Antialiasing);
55 m_chartView->setRenderHint(QPainter::Antialiasing);
56 m_chartView->setMinimumSize(640, 480);
56 m_chartView->setMinimumSize(640, 480);
57
57
58 // add, remove data buttons
58 // add, remove data buttons
59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
61
61
62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
64
64
65 QPushButton* removeRowButton = new QPushButton("Remove row");
65 QPushButton* removeRowButton = new QPushButton("Remove row");
66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
67
67
68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
69
69
70 // spin box for setting number of affected items (add, remove)
70 // spin box for setting number of affected items (add, remove)
71 m_linesCountSpinBox = new QSpinBox;
71 m_linesCountSpinBox = new QSpinBox;
72 m_linesCountSpinBox->setRange(1, 10);
72 m_linesCountSpinBox->setRange(1, 10);
73 m_linesCountSpinBox->setValue(1);
73 m_linesCountSpinBox->setValue(1);
74
74
75 // buttons layout
75 // buttons layout
76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
77 buttonsLayout->addWidget(spinBoxLabel);
77 buttonsLayout->addWidget(spinBoxLabel);
78 buttonsLayout->addWidget(m_linesCountSpinBox);
78 buttonsLayout->addWidget(m_linesCountSpinBox);
79 buttonsLayout->addWidget(addRowAboveButton);
79 buttonsLayout->addWidget(addRowAboveButton);
80 buttonsLayout->addWidget(addRowBelowButton);
80 buttonsLayout->addWidget(addRowBelowButton);
81 buttonsLayout->addWidget(removeRowButton);
81 buttonsLayout->addWidget(removeRowButton);
82 buttonsLayout->addStretch();
82 buttonsLayout->addStretch();
83
83
84 // chart type radio buttons
84 // chart type radio buttons
85 m_lineRadioButton = new QRadioButton("Line");
85 m_lineRadioButton = new QRadioButton("Line");
86 m_splineRadioButton = new QRadioButton("Spline");
86 m_splineRadioButton = new QRadioButton("Spline");
87 m_scatterRadioButton = new QRadioButton("Scatter");
87 m_scatterRadioButton = new QRadioButton("Scatter");
88 m_pieRadioButton = new QRadioButton("Pie");
88 m_pieRadioButton = new QRadioButton("Pie");
89 m_areaRadioButton = new QRadioButton("Area");
89 m_areaRadioButton = new QRadioButton("Area");
90 m_barRadioButton = new QRadioButton("Bar");
90 m_barRadioButton = new QRadioButton("Bar");
91
91
92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
98 m_lineRadioButton->setChecked(true);
98 m_lineRadioButton->setChecked(true);
99
99
100 // radio buttons layout
100 // radio buttons layout
101 QVBoxLayout* radioLayout = new QVBoxLayout;
101 QVBoxLayout* radioLayout = new QVBoxLayout;
102 radioLayout->addWidget(m_lineRadioButton);
102 radioLayout->addWidget(m_lineRadioButton);
103 radioLayout->addWidget(m_splineRadioButton);
103 radioLayout->addWidget(m_splineRadioButton);
104 radioLayout->addWidget(m_scatterRadioButton);
104 radioLayout->addWidget(m_scatterRadioButton);
105 radioLayout->addWidget(m_pieRadioButton);
105 radioLayout->addWidget(m_pieRadioButton);
106 radioLayout->addWidget(m_areaRadioButton);
106 radioLayout->addWidget(m_areaRadioButton);
107 radioLayout->addWidget(m_barRadioButton);
107 radioLayout->addWidget(m_barRadioButton);
108 radioLayout->addStretch();
108 radioLayout->addStretch();
109
109
110 // create main layout
110 // create main layout
111 QGridLayout* mainLayout = new QGridLayout;
111 QGridLayout* mainLayout = new QGridLayout;
112 mainLayout->addLayout(buttonsLayout, 1, 1);
112 mainLayout->addLayout(buttonsLayout, 1, 1);
113 mainLayout->addLayout(radioLayout, 2, 1);
113 mainLayout->addLayout(radioLayout, 2, 1);
114 mainLayout->addWidget(m_tableView, 1, 0);
114 mainLayout->addWidget(m_tableView, 1, 0);
115 mainLayout->addWidget(m_chartView, 2, 0);
115 mainLayout->addWidget(m_chartView, 2, 0);
116 setLayout(mainLayout);
116 setLayout(mainLayout);
117 m_lineRadioButton->setFocus();
117 m_lineRadioButton->setFocus();
118 }
118 }
119
119
120 void TableWidget::addRowAbove()
120 void TableWidget::addRowAbove()
121 {
121 {
122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
123
123
124 }
124 }
125
125
126 void TableWidget::addRowBelow()
126 void TableWidget::addRowBelow()
127 {
127 {
128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
129
129
130 }
130 }
131
131
132 void TableWidget::removeRow()
132 void TableWidget::removeRow()
133 {
133 {
134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
135 }
135 }
136
136
137 void TableWidget::updateChartType(bool toggle)
137 void TableWidget::updateChartType(bool toggle)
138 {
138 {
139 // this if is needed, so that the function is only called once.
139 // this if is needed, so that the function is only called once.
140 // For the radioButton that was enabled.
140 // For the radioButton that was enabled.
141 if (toggle) {
141 if (toggle) {
142 m_chart->removeAllSeries();
142 m_chart->removeAllSeries();
143
143
144 // renable axes of the chart (pie hides them)
144 // renable axes of the chart (pie hides them)
145 // x axis
145 // x axis
146 QChartAxis *axis = m_chart->axisX();
146 QChartAxis *axis = m_chart->axisX();
147 axis->setAxisVisible(true);
147 axis->setAxisVisible(true);
148 axis->setGridLineVisible(true);
148 axis->setGridLineVisible(true);
149 axis->setLabelsVisible(true);
149 axis->setLabelsVisible(true);
150
150
151 // y axis
151 // y axis
152 axis = m_chart->axisY();
152 axis = m_chart->axisY();
153 axis->setAxisVisible(true);
153 axis->setAxisVisible(true);
154 axis->setGridLineVisible(true);
154 axis->setGridLineVisible(true);
155 axis->setLabelsVisible(true);
155 axis->setLabelsVisible(true);
156
156
157 m_model->clearMapping();
157 m_model->clearMapping();
158
158
159 QString seriesColorHex = "#000000";
159 QString seriesColorHex = "#000000";
160 QPen pen;
160 QPen pen;
161 pen.setWidth(2);
161 pen.setWidth(2);
162
162
163 if (m_lineRadioButton->isChecked())
163 if (m_lineRadioButton->isChecked())
164 {
164 {
165 // series 1
165 // series 1
166 m_series = new QLineSeries;
166 m_series = new QLineSeries;
167 m_series->setModel(m_model);
167 m_series->setModel(m_model);
168 m_series->setModelMapping(0,1, Qt::Vertical);
168 m_series->setModelMapping(0,1, Qt::Vertical);
169 m_series->setModelMappingRange(1, 4);
169 // m_series->setModelMappingRange(1, 4);
170 m_chart->addSeries(m_series);
170 m_chart->addSeries(m_series);
171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
173
173
174 // series 2
174 // series 2
175 m_series = new QLineSeries;
175 m_series = new QLineSeries;
176 m_series->setModel(m_model);
176 m_series->setModel(m_model);
177 m_series->setModelMapping(2,3, Qt::Vertical);
177 m_series->setModelMapping(2,3, Qt::Vertical);
178 m_chart->addSeries(m_series);
178 m_chart->addSeries(m_series);
179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
181
181
182 // series 3
182 // series 3
183 m_series = new QLineSeries;
183 m_series = new QLineSeries;
184 m_series->setModel(m_model);
184 m_series->setModel(m_model);
185 m_series->setModelMapping(4,5, Qt::Vertical);
185 m_series->setModelMapping(4,5, Qt::Vertical);
186 m_series->setModelMappingRange(2, 0);
186 // m_series->setModelMappingRange(2, 0);
187 m_chart->addSeries(m_series);
187 m_chart->addSeries(m_series);
188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
190 }
190 }
191 else if (m_splineRadioButton->isChecked())
191 else if (m_splineRadioButton->isChecked())
192 {
192 {
193 // series 1
193 // series 1
194 m_series = new QSplineSeries;
194 m_series = new QSplineSeries;
195 m_series->setModel(m_model);
195 m_series->setModel(m_model);
196 m_series->setModelMapping(0,1, Qt::Vertical);
196 m_series->setModelMapping(0,1, Qt::Vertical);
197 m_series->setModelMappingRange(1, 4);
197 // m_series->setModelMappingRange(1, 4);
198 // series->setModelMapping(0,1, Qt::Horizontal);
198 // series->setModelMapping(0,1, Qt::Horizontal);
199 m_chart->addSeries(m_series);
199 m_chart->addSeries(m_series);
200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
202
202
203 // series 2
203 // series 2
204 m_series = new QSplineSeries;
204 m_series = new QSplineSeries;
205 m_series->setModel(m_model);
205 m_series->setModel(m_model);
206 m_series->setModelMapping(2,3, Qt::Vertical);
206 m_series->setModelMapping(2,3, Qt::Vertical);
207 m_series->setModelMappingRange(0, 0);
207 // m_series->setModelMappingRange(0, 0);
208 // series->setModelMapping(2,3, Qt::Horizontal);
208 // series->setModelMapping(2,3, Qt::Horizontal);
209 m_chart->addSeries(m_series);
209 m_chart->addSeries(m_series);
210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
212
212
213 // series 3
213 // series 3
214 m_series = new QSplineSeries;
214 m_series = new QSplineSeries;
215 m_series->setModel(m_model);
215 m_series->setModel(m_model);
216 m_series->setModelMapping(4,5, Qt::Vertical);
216 m_series->setModelMapping(4,5, Qt::Vertical);
217 m_series->setModelMappingRange(2, 0);
217 // m_series->setModelMappingRange(2, 0);
218 // series->setModelMapping(4,5, Qt::Horizontal);
218 // series->setModelMapping(4,5, Qt::Horizontal);
219 m_chart->addSeries(m_series);
219 m_chart->addSeries(m_series);
220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
222 }
222 }
223 else if (m_scatterRadioButton->isChecked())
223 else if (m_scatterRadioButton->isChecked())
224 {
224 {
225 // series 1
225 // series 1
226 m_series = new QScatterSeries;
226 m_series = new QScatterSeries;
227 m_series->setModel(m_model);
227 m_series->setModel(m_model);
228 m_series->setModelMapping(0,1, Qt::Vertical);
228 m_series->setModelMapping(0,1, Qt::Vertical);
229 m_series->setModelMappingRange(2, 0);
229 // m_series->setModelMappingRange(2, 0);
230 // series->setModelMapping(0,1, Qt::Horizontal);
230 // series->setModelMapping(0,1, Qt::Horizontal);
231 m_chart->addSeries(m_series);
231 m_chart->addSeries(m_series);
232
232
233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
235
235
236 // series 2
236 // series 2
237 m_series = new QScatterSeries;
237 m_series = new QScatterSeries;
238 m_series->setModel(m_model);
238 m_series->setModel(m_model);
239 m_series->setModelMapping(2,3, Qt::Vertical);
239 m_series->setModelMapping(2,3, Qt::Vertical);
240 m_series->setModelMappingRange(1, 6);
240 // m_series->setModelMappingRange(1, 6);
241 // series->setModelMapping(2,3, Qt::Horizontal);
241 // series->setModelMapping(2,3, Qt::Horizontal);
242 m_chart->addSeries(m_series);
242 m_chart->addSeries(m_series);
243
243
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
246
246
247 // series 3
247 // series 3
248 m_series = new QScatterSeries;
248 m_series = new QScatterSeries;
249 m_series->setModel(m_model);
249 m_series->setModel(m_model);
250 m_series->setModelMapping(4,5, Qt::Vertical);
250 m_series->setModelMapping(4,5, Qt::Vertical);
251 // series->setModelMapping(4,5, Qt::Horizontal);
251 // series->setModelMapping(4,5, Qt::Horizontal);
252 m_chart->addSeries(m_series);
252 m_chart->addSeries(m_series);
253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
255 }
255 }
256 else if (m_pieRadioButton->isChecked())
256 else if (m_pieRadioButton->isChecked())
257 {
257 {
258 // pie 1
258 // pie 1
259 QPieSeries* pieSeries = new QPieSeries;
259 QPieSeries* pieSeries = new QPieSeries;
260 pieSeries->setModel(m_model);
260 pieSeries->setModel(m_model);
261 pieSeries->setModelMapping(0,0, Qt::Vertical);
261 pieSeries->setModelMapping(0,0, Qt::Vertical);
262 pieSeries->setLabelsVisible(true);
262 pieSeries->setLabelsVisible(true);
263 pieSeries->setPieSize(0.4);
263 pieSeries->setPieSize(0.4);
264 pieSeries->setHorizontalPosition(0.2);
264 pieSeries->setHorizontalPosition(0.2);
265 pieSeries->setVerticalPosition(0.35);
265 pieSeries->setVerticalPosition(0.35);
266
266
267 m_chart->addSeries(pieSeries);
267 m_chart->addSeries(pieSeries);
268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
270
270
271 // pie 2
271 // pie 2
272 pieSeries = new QPieSeries;
272 pieSeries = new QPieSeries;
273 pieSeries->setModel(m_model);
273 pieSeries->setModel(m_model);
274 pieSeries->setModelMapping(1,1, Qt::Vertical);
274 pieSeries->setModelMapping(1,1, Qt::Vertical);
275 pieSeries->setLabelsVisible(true);
275 pieSeries->setLabelsVisible(true);
276 pieSeries->setPieSize(0.4);
276 pieSeries->setPieSize(0.4);
277 pieSeries->setHorizontalPosition(0.8);
277 pieSeries->setHorizontalPosition(0.8);
278 pieSeries->setVerticalPosition(0.35);
278 pieSeries->setVerticalPosition(0.35);
279 m_chart->addSeries(pieSeries);
279 m_chart->addSeries(pieSeries);
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
282
282
283 // pie 3
283 // pie 3
284 pieSeries = new QPieSeries;
284 pieSeries = new QPieSeries;
285 pieSeries->setModel(m_model);
285 pieSeries->setModel(m_model);
286 pieSeries->setModelMapping(2,2, Qt::Vertical);
286 pieSeries->setModelMapping(2,2, Qt::Vertical);
287 pieSeries->setLabelsVisible(true);
287 pieSeries->setLabelsVisible(true);
288 pieSeries->setPieSize(0.4);
288 pieSeries->setPieSize(0.4);
289 pieSeries->setHorizontalPosition(0.5);
289 pieSeries->setHorizontalPosition(0.5);
290 pieSeries->setVerticalPosition(0.65);
290 pieSeries->setVerticalPosition(0.65);
291 m_chart->addSeries(pieSeries);
291 m_chart->addSeries(pieSeries);
292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
294 }
294 }
295 else if (m_areaRadioButton->isChecked())
295 else if (m_areaRadioButton->isChecked())
296 {
296 {
297 QLineSeries* upperLineSeries = new QLineSeries;
297 QLineSeries* upperLineSeries = new QLineSeries;
298 upperLineSeries->setModel(m_model);
298 upperLineSeries->setModel(m_model);
299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
300 upperLineSeries->setModelMappingRange(1, 5);
300 // upperLineSeries->setModelMappingRange(1, 5);
301 QLineSeries* lowerLineSeries = new QLineSeries;
301 QLineSeries* lowerLineSeries = new QLineSeries;
302 lowerLineSeries->setModel(m_model);
302 lowerLineSeries->setModel(m_model);
303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
305 m_chart->addSeries(areaSeries);
305 m_chart->addSeries(areaSeries);
306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
309 }
309 }
310 else if (m_barRadioButton->isChecked())
310 else if (m_barRadioButton->isChecked())
311 {
311 {
312 QBarSeries* barSeries = new QBarSeries(QStringList());
312 QBarSeries* barSeries = new QBarSeries(QStringList());
313 barSeries->setModel(m_model);
313 barSeries->setModel(m_model);
314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
315 m_chart->addSeries(barSeries);
315 m_chart->addSeries(barSeries);
316 QList<QBarSet*> barsets = barSeries->barSets();
316 QList<QBarSet*> barsets = barSeries->barSets();
317 for (int i = 0; i < barsets.count(); i++) {
317 for (int i = 0; i < barsets.count(); i++) {
318 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
318 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
319 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
319 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
320 }
320 }
321 }
321 }
322
322
323
323
324 m_chart->axisX()->setRange(0, 500);
324 m_chart->axisX()->setRange(0, 500);
325 m_chart->axisY()->setRange(0, 120);
325 m_chart->axisY()->setRange(0, 120);
326
326
327 // repaint table view colors
327 // repaint table view colors
328 m_tableView->repaint();
328 m_tableView->repaint();
329 m_tableView->setFocus();
329 m_tableView->setFocus();
330 }
330 }
331 }
331 }
332
332
333 TableWidget::~TableWidget()
333 TableWidget::~TableWidget()
334 {
334 {
335
335
336 }
336 }
General Comments 0
You need to be logged in to leave comments. Login now