##// END OF EJS Templates
category range handling fix
sauimone -
r1573:be2bb9da91cd
parent child
Show More
@@ -1,293 +1,295
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 "qcategoriesaxis.h"
21 #include "qcategoriesaxis.h"
22 #include "qcategoriesaxis_p.h"
22 #include "qcategoriesaxis_p.h"
23 #include "chartcategoriesaxisx_p.h"
23 #include "chartcategoriesaxisx_p.h"
24 #include "chartcategoriesaxisy_p.h"
24 #include "chartcategoriesaxisy_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 QCategoriesAxis::QCategoriesAxis(QObject *parent):
29 QCategoriesAxis::QCategoriesAxis(QObject *parent):
30 QAbstractAxis(*new QCategoriesAxisPrivate(this),parent)
30 QAbstractAxis(*new QCategoriesAxisPrivate(this),parent)
31 {
31 {
32 }
32 }
33
33
34 QCategoriesAxis::~QCategoriesAxis()
34 QCategoriesAxis::~QCategoriesAxis()
35 {
35 {
36 }
36 }
37
37
38 QCategoriesAxis::QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
38 QCategoriesAxis::QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
39 {
39 {
40
40
41 }
41 }
42
42
43 /*!
43 /*!
44 Appends \a categories to axis
44 Appends \a categories to axis
45 */
45 */
46 void QCategoriesAxis::append(const QStringList &categories)
46 void QCategoriesAxis::append(const QStringList &categories)
47 {
47 {
48 Q_D(QCategoriesAxis);
48 Q_D(QCategoriesAxis);
49 d->m_categories.append(categories);
49 d->m_categories.append(categories);
50 emit categoriesChanged();
50 emit categoriesChanged();
51 }
51 }
52
52
53 /*!
53 /*!
54 Appends \a category to axis
54 Appends \a category to axis
55 */
55 */
56 void QCategoriesAxis::append(const QString &category)
56 void QCategoriesAxis::append(const QString &category)
57 {
57 {
58 Q_D(QCategoriesAxis);
58 Q_D(QCategoriesAxis);
59 d->m_categories.append(category);
59 d->m_categories.append(category);
60 emit categoriesChanged();
60 emit categoriesChanged();
61 }
61 }
62
62
63 /*!
63 /*!
64 Removes \a category from axis
64 Removes \a category from axis
65 */
65 */
66 void QCategoriesAxis::remove(const QString &category)
66 void QCategoriesAxis::remove(const QString &category)
67 {
67 {
68 Q_D(QCategoriesAxis);
68 Q_D(QCategoriesAxis);
69 if (d->m_categories.contains(category)) {
69 if (d->m_categories.contains(category)) {
70 d->m_categories.removeAt(d->m_categories.indexOf(category));
70 d->m_categories.removeAt(d->m_categories.indexOf(category));
71 emit categoriesChanged();
71 emit categoriesChanged();
72 }
72 }
73 }
73 }
74
74
75 /*!
75 /*!
76 Inserts \a category to axis at \a index
76 Inserts \a category to axis at \a index
77 */
77 */
78 void QCategoriesAxis::insert(int index, const QString &category)
78 void QCategoriesAxis::insert(int index, const QString &category)
79 {
79 {
80 Q_D(QCategoriesAxis);
80 Q_D(QCategoriesAxis);
81 d->m_categories.insert(index,category);
81 d->m_categories.insert(index,category);
82 emit categoriesChanged();
82 emit categoriesChanged();
83 }
83 }
84
84
85 /*!
85 /*!
86 Removes all categories.
86 Removes all categories.
87 */
87 */
88 void QCategoriesAxis::clear()
88 void QCategoriesAxis::clear()
89 {
89 {
90 Q_D(QCategoriesAxis);
90 Q_D(QCategoriesAxis);
91 d->m_categories.clear();
91 d->m_categories.clear();
92 emit categoriesChanged();
92 emit categoriesChanged();
93 }
93 }
94
94
95 void QCategoriesAxis::setCategories(const QStringList &categories)
95 void QCategoriesAxis::setCategories(const QStringList &categories)
96 {
96 {
97 Q_D(QCategoriesAxis);
97 Q_D(QCategoriesAxis);
98 d->m_categories = categories;
98 d->m_categories = categories;
99 emit categoriesChanged();
99 emit categoriesChanged();
100 }
100 }
101
101
102 QStringList QCategoriesAxis::categories()
102 QStringList QCategoriesAxis::categories()
103 {
103 {
104 Q_D(QCategoriesAxis);
104 Q_D(QCategoriesAxis);
105 return d->m_categories;
105 return d->m_categories;
106 }
106 }
107
107
108 /*!
108 /*!
109 Returns number of categories.
109 Returns number of categories.
110 */
110 */
111 int QCategoriesAxis::count() const
111 int QCategoriesAxis::count() const
112 {
112 {
113 Q_D(const QCategoriesAxis);
113 Q_D(const QCategoriesAxis);
114 return d->m_categories.count();
114 return d->m_categories.count();
115 }
115 }
116
116
117 /*!
117 /*!
118 Returns category at \a index. Index must be valid.
118 Returns category at \a index. Index must be valid.
119 */
119 */
120 QString QCategoriesAxis::at(int index) const
120 QString QCategoriesAxis::at(int index) const
121 {
121 {
122 Q_D(const QCategoriesAxis);
122 Q_D(const QCategoriesAxis);
123 return d->m_categories.at(index);
123 return d->m_categories.at(index);
124 }
124 }
125
125
126 /*!
126 /*!
127 Sets minimum category to \a minCategory.
127 Sets minimum category to \a minCategory.
128 */
128 */
129 void QCategoriesAxis::setMinCategory(const QString& minCategory)
129 void QCategoriesAxis::setMinCategory(const QString& minCategory)
130 {
130 {
131 Q_D(QCategoriesAxis);
131 Q_D(QCategoriesAxis);
132 d->setMinCategory(minCategory);
132 d->setMinCategory(minCategory);
133 }
133 }
134
134
135 /*!
135 /*!
136 Returns minimum category.
136 Returns minimum category.
137 */
137 */
138 QString QCategoriesAxis::minCategory() const
138 QString QCategoriesAxis::minCategory() const
139 {
139 {
140 Q_D(const QCategoriesAxis);
140 Q_D(const QCategoriesAxis);
141 return d->m_minCategory;
141 return d->m_minCategory;
142 }
142 }
143
143
144 /*!
144 /*!
145 Sets maximum category to \a maxCategory.
145 Sets maximum category to \a maxCategory.
146 */
146 */
147 void QCategoriesAxis::setMaxCategory(const QString& maxCategory)
147 void QCategoriesAxis::setMaxCategory(const QString& maxCategory)
148 {
148 {
149 Q_D(QCategoriesAxis);
149 Q_D(QCategoriesAxis);
150 d->setMaxCategory(maxCategory);
150 d->setMaxCategory(maxCategory);
151 }
151 }
152
152
153 /*!
153 /*!
154 Returns maximum category
154 Returns maximum category
155 */
155 */
156 QString QCategoriesAxis::maxCategory() const
156 QString QCategoriesAxis::maxCategory() const
157 {
157 {
158 Q_D(const QCategoriesAxis);
158 Q_D(const QCategoriesAxis);
159 return d->m_maxCategory;
159 return d->m_maxCategory;
160 }
160 }
161
161
162 /*!
162 /*!
163 Sets range from \a minCategory to \a maxCategory
163 Sets range from \a minCategory to \a maxCategory
164 */
164 */
165 void QCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
165 void QCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
166 {
166 {
167 Q_D(QCategoriesAxis);
167 Q_D(QCategoriesAxis);
168 d->setRangeCategory(minCategory,maxCategory);
168 d->setRangeCategory(minCategory,maxCategory);
169 }
169 }
170
170
171 /*!
171 /*!
172 Returns the type of axis.
172 Returns the type of axis.
173 */
173 */
174 QAbstractAxis::AxisType QCategoriesAxis::type() const
174 QAbstractAxis::AxisType QCategoriesAxis::type() const
175 {
175 {
176 return AxisTypeCategories;
176 return AxisTypeCategories;
177 }
177 }
178
178
179 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180
180
181 QCategoriesAxisPrivate::QCategoriesAxisPrivate(QCategoriesAxis* q):
181 QCategoriesAxisPrivate::QCategoriesAxisPrivate(QCategoriesAxis* q):
182 QAbstractAxisPrivate(q)
182 QAbstractAxisPrivate(q)
183 {
183 {
184
184
185 }
185 }
186
186
187 QCategoriesAxisPrivate::~QCategoriesAxisPrivate()
187 QCategoriesAxisPrivate::~QCategoriesAxisPrivate()
188 {
188 {
189
189
190 }
190 }
191
191
192 void QCategoriesAxisPrivate::setMinCategory(const QString& minCategory)
192 void QCategoriesAxisPrivate::setMinCategory(const QString& minCategory)
193 {
193 {
194 // Convert the category to value
194 // Convert the category to value
195 int minIndex = m_categories.indexOf(minCategory);
195 int minIndex = m_categories.indexOf(minCategory);
196 if (minIndex == -1) {
196 if (minIndex == -1) {
197 return;
197 return;
198 }
198 }
199
199
200 int maxIndex = m_max;
200 int maxIndex = qFloor(m_max);
201 if (minIndex > maxIndex) {
201 if (minIndex > maxIndex) {
202 maxIndex = m_categories.count()-1;
202 maxIndex = m_categories.count()-1;
203 }
203 }
204 setRange(minIndex - 0.5, maxIndex + 0.5);
204 setRange(minIndex - 0.5, maxIndex + 0.5);
205 }
205 }
206
206
207 void QCategoriesAxisPrivate::setMaxCategory(const QString& maxCategory)
207 void QCategoriesAxisPrivate::setMaxCategory(const QString& maxCategory)
208 {
208 {
209 // Convert the category to value
209 // Convert the category to value
210 int maxIndex = m_categories.indexOf(maxCategory);
210 int maxIndex = m_categories.indexOf(maxCategory);
211 if (maxIndex == -1) {
211 if (maxIndex == -1) {
212 return;
212 return;
213 }
213 }
214 if (maxIndex < m_min) {
214
215 m_min = 0;
215 int minIndex = qCeil(m_min);
216 if (maxIndex < minIndex) {
217 minIndex = 0;
216 }
218 }
217 setRange(m_min - 0.5, maxIndex + 0.5);
219 setRange(minIndex - 0.5, maxIndex + 0.5);
218 }
220 }
219
221
220 void QCategoriesAxisPrivate::setRangeCategory(const QString& minCategory, const QString& maxCategory)
222 void QCategoriesAxisPrivate::setRangeCategory(const QString& minCategory, const QString& maxCategory)
221 {
223 {
222 // TODO:
224 // TODO:
223 int minIndex = m_categories.indexOf(minCategory);
225 int minIndex = m_categories.indexOf(minCategory);
224 if (minIndex == -1) {
226 if (minIndex == -1) {
225 return;
227 return;
226 }
228 }
227 int maxIndex = m_categories.indexOf(maxCategory);
229 int maxIndex = m_categories.indexOf(maxCategory);
228 if (maxIndex == -1) {
230 if (maxIndex == -1) {
229 return;
231 return;
230 }
232 }
231 setRange(minIndex -0.5, maxIndex + 0.5);
233 setRange(minIndex -0.5, maxIndex + 0.5);
232 }
234 }
233
235
234 void QCategoriesAxisPrivate::setMin(const qreal min)
236 void QCategoriesAxisPrivate::setMin(const qreal min)
235 {
237 {
236 setRange(min,m_max);
238 setRange(min,m_max);
237 }
239 }
238
240
239 void QCategoriesAxisPrivate::setMax(const qreal max)
241 void QCategoriesAxisPrivate::setMax(const qreal max)
240 {
242 {
241 setRange(m_min,max);
243 setRange(m_min,max);
242 }
244 }
243
245
244 void QCategoriesAxisPrivate::setRange(const qreal min, const qreal max)
246 void QCategoriesAxisPrivate::setRange(const qreal min, const qreal max)
245 {
247 {
246 if (max <= min) {
248 if (max <= min) {
247 // max must be greater than min
249 // max must be greater than min
248 return;
250 return;
249 }
251 }
250 Q_Q(QCategoriesAxis);
252 Q_Q(QCategoriesAxis);
251 bool changed = false;
253 bool changed = false;
252 if (!qFuzzyIsNull(m_min - min)) {
254 if (!qFuzzyIsNull(m_min - min)) {
253 m_min = min;
255 m_min = min;
254 changed = true;
256 changed = true;
255 }
257 }
256
258
257 if (!qFuzzyIsNull(m_max - max)) {
259 if (!qFuzzyIsNull(m_max - max)) {
258 m_max = max;
260 m_max = max;
259 changed = true;
261 changed = true;
260 }
262 }
261
263
262 if (changed) {
264 if (changed) {
263 emit this->changed(m_min, m_max, qCeil(m_max) -qCeil(m_min) +1, false);
265 emit this->changed(m_min, m_max, qCeil(m_max) -qCeil(m_min) +1, false);
264 emit q->categoriesChanged();
266 emit q->categoriesChanged();
265 }
267 }
266 }
268 }
267
269
268 int QCategoriesAxisPrivate::ticksCount() const
270 int QCategoriesAxisPrivate::ticksCount() const
269 {
271 {
270 return m_categories.count()+1;
272 return m_categories.count()+1;
271 }
273 }
272
274
273 void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
275 void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
274 {
276 {
275 m_min = min;
277 m_min = min;
276 m_max = max;
278 m_max = max;
277 m_ticksCount = count;
279 m_ticksCount = count;
278 }
280 }
279
281
280 ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
282 ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
281 {
283 {
282 Q_Q( QCategoriesAxis);
284 Q_Q( QCategoriesAxis);
283 if(m_orientation == Qt::Vertical){
285 if(m_orientation == Qt::Vertical){
284 return new ChartCategoriesAxisY(q,presenter);
286 return new ChartCategoriesAxisY(q,presenter);
285 }else{
287 }else{
286 return new ChartCategoriesAxisX(q,presenter);
288 return new ChartCategoriesAxisX(q,presenter);
287 }
289 }
288 }
290 }
289
291
290 #include "moc_qcategoriesaxis.cpp"
292 #include "moc_qcategoriesaxis.cpp"
291 #include "moc_qcategoriesaxis_p.cpp"
293 #include "moc_qcategoriesaxis_p.cpp"
292
294
293 QTCOMMERCIALCHART_END_NAMESPACE
295 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now