##// END OF EJS Templates
category axis magic
sauimone -
r1605:61a8c8f8b102
parent child
Show More
@@ -1,307 +1,296
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qcategoriesaxis.h"
22 22 #include "qcategoriesaxis_p.h"
23 23 #include "chartcategoriesaxisx_p.h"
24 24 #include "chartcategoriesaxisy_p.h"
25 25 #include <qmath.h>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 QCategoriesAxis::QCategoriesAxis(QObject *parent):
30 30 QAbstractAxis(*new QCategoriesAxisPrivate(this),parent)
31 31 {
32 32 }
33 33
34 34 QCategoriesAxis::~QCategoriesAxis()
35 35 {
36 36 }
37 37
38 38 QCategoriesAxis::QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
39 39 {
40 40
41 41 }
42 42
43 43 /*!
44 44 Appends \a categories to axis
45 45 */
46 46 void QCategoriesAxis::append(const QStringList &categories)
47 47 {
48 48 Q_D(QCategoriesAxis);
49 if (d->m_categories.isEmpty()) {
50 d->m_minCategory = categories.first();
51 d->m_maxCategory = categories.last();
52 }
53
49 54 d->m_categories.append(categories);
50 55 emit categoriesChanged();
51 56 }
52 57
53 58 /*!
54 59 Appends \a category to axis
55 60 */
56 61 void QCategoriesAxis::append(const QString &category)
57 62 {
58 63 Q_D(QCategoriesAxis);
64 if (d->m_categories.isEmpty()) {
65 d->m_minCategory = category;
66 d->m_maxCategory = category;
67 }
68
59 69 d->m_categories.append(category);
60 70 emit categoriesChanged();
61 71 }
62 72
63 73 /*!
64 74 Removes \a category from axis
65 75 */
66 76 void QCategoriesAxis::remove(const QString &category)
67 77 {
68 78 Q_D(QCategoriesAxis);
69 79 if (d->m_categories.contains(category)) {
70 80 d->m_categories.removeAt(d->m_categories.indexOf(category));
81 d->m_minCategory = d->m_categories.first();
82 d->m_maxCategory = d->m_categories.last();
71 83 emit categoriesChanged();
72 84 }
73 85 }
74 86
75 87 /*!
76 88 Inserts \a category to axis at \a index
77 89 */
78 90 void QCategoriesAxis::insert(int index, const QString &category)
79 91 {
80 92 Q_D(QCategoriesAxis);
93 if (d->m_categories.isEmpty()) {
94 d->m_minCategory = category;
95 d->m_maxCategory = category;
96 }
81 97 d->m_categories.insert(index,category);
82 98 emit categoriesChanged();
83 99 }
84 100
85 101 /*!
86 102 Removes all categories.
87 103 */
88 104 void QCategoriesAxis::clear()
89 105 {
90 106 Q_D(QCategoriesAxis);
91 107 d->m_categories.clear();
108 d->m_minCategory.clear();
109 d->m_maxCategory.clear();
92 110 emit categoriesChanged();
93 111 }
94 112
95 113 void QCategoriesAxis::setCategories(const QStringList &categories)
96 114 {
97 115 Q_D(QCategoriesAxis);
98 116 d->m_categories = categories;
117 d->m_minCategory = categories.first();
118 d->m_maxCategory = categories.last();
99 119 emit categoriesChanged();
100 120 }
101 121
102 122 QStringList QCategoriesAxis::categories()
103 123 {
104 124 Q_D(QCategoriesAxis);
105 125 return d->m_categories;
106 126 }
107 127
108 128 /*!
109 129 Returns number of categories.
110 130 */
111 131 int QCategoriesAxis::count() const
112 132 {
113 133 Q_D(const QCategoriesAxis);
114 134 return d->m_categories.count();
115 135 }
116 136
117 137 /*!
118 138 Returns category at \a index. Index must be valid.
119 139 */
120 140 QString QCategoriesAxis::at(int index) const
121 141 {
122 142 Q_D(const QCategoriesAxis);
123 143 return d->m_categories.at(index);
124 144 }
125 145
126 146 /*!
127 Sets minimum category to \a minCategory.
147 Sets minimum category to \a min.
128 148 */
129 void QCategoriesAxis::setMinCategory(const QString& minCategory)
149 void QCategoriesAxis::setMin(const QString& min)
130 150 {
131 151 Q_D(QCategoriesAxis);
132 d->setMinCategory(minCategory);
152 setRange(min,d->m_maxCategory);
133 153 }
134 154
135 155 /*!
136 156 Returns minimum category.
137 157 */
138 QString QCategoriesAxis::minCategory() const
158 QString QCategoriesAxis::min() const
139 159 {
140 160 Q_D(const QCategoriesAxis);
141 161 return d->m_minCategory;
142 162 }
143 163
144 164 /*!
145 Sets maximum category to \a maxCategory.
165 Sets maximum category to \a max.
146 166 */
147 void QCategoriesAxis::setMaxCategory(const QString& maxCategory)
167 void QCategoriesAxis::setMax(const QString& max)
148 168 {
149 169 Q_D(QCategoriesAxis);
150 d->setMaxCategory(maxCategory);
170 setRange(d->m_minCategory,max);
151 171 }
152 172
153 173 /*!
154 174 Returns maximum category
155 175 */
156 QString QCategoriesAxis::maxCategory() const
176 QString QCategoriesAxis::max() const
157 177 {
158 178 Q_D(const QCategoriesAxis);
159 179 return d->m_maxCategory;
160 180 }
161 181
162 182 /*!
163 183 Sets range from \a minCategory to \a maxCategory
164 184 */
165 void QCategoriesAxis::setCategoryRange(const QString& minCategory, const QString& maxCategory)
185 void QCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
166 186 {
167 187 Q_D(QCategoriesAxis);
168 d->setRangeCategory(minCategory,maxCategory);
188
189 int minIndex = d->m_categories.indexOf(minCategory);
190 if (minIndex == -1) {
191 return;
192 }
193 int maxIndex = d->m_categories.indexOf(maxCategory);
194 if (maxIndex == -1) {
195 return;
196 }
197
198 if (maxIndex <= minIndex) {
199 // max must be greater than min
200 return;
201 }
202
203 d->m_minCategory = minCategory;
204 d->m_maxCategory = maxCategory;
205
206 bool changed = false;
207 if (!qFuzzyIsNull(d->m_min - (minIndex))) {
208 d->m_min = minIndex;
209 changed = true;
210 }
211
212 if (!qFuzzyIsNull(d->m_max - (maxIndex))) {
213 d->m_max = maxIndex;
214 changed = true;
215 }
216
217 if ((changed)) {
218 if(!signalsBlocked()){
219 emit d->changed(d->m_min -0.5, d->m_max +0.5, qCeil(d->m_max + 0.5) -qCeil(d->m_min - 0.5) +1, false);
220 }
221 emit categoriesChanged();
222 }
169 223 }
170 224
171 225 /*!
172 226 Returns the type of axis.
173 227 */
174 228 QAbstractAxis::AxisType QCategoriesAxis::type() const
175 229 {
176 230 return AxisTypeCategories;
177 231 }
178 232
179 233 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180 234
181 235 QCategoriesAxisPrivate::QCategoriesAxisPrivate(QCategoriesAxis* q):
182 236 QAbstractAxisPrivate(q)
183 237 {
184 238
185 239 }
186 240
187 241 QCategoriesAxisPrivate::~QCategoriesAxisPrivate()
188 242 {
189 243
190 244 }
191 245
192 void QCategoriesAxisPrivate::setMinCategory(const QString& minCategory)
193 {
194 // Convert the category to value
195 int minIndex = m_categories.indexOf(minCategory);
196 if (minIndex == -1) {
197 return;
198 }
199
200 int maxIndex = qFloor(m_max);
201 if (minIndex > maxIndex) {
202 maxIndex = m_categories.count()-1;
203 }
204 setRange(minIndex - 0.5, maxIndex + 0.5);
205 }
206
207 void QCategoriesAxisPrivate::setMaxCategory(const QString& maxCategory)
208 {
209 // Convert the category to value
210 int maxIndex = m_categories.indexOf(maxCategory);
211 if (maxIndex == -1) {
212 return;
213 }
214
215 int minIndex = qCeil(m_min);
216 if (maxIndex < minIndex) {
217 minIndex = 0;
218 }
219 setRange(minIndex - 0.5, maxIndex + 0.5);
220 }
221
222 void QCategoriesAxisPrivate::setRangeCategory(const QString& minCategory, const QString& maxCategory)
223 {
224 // TODO:
225 int minIndex = m_categories.indexOf(minCategory);
226 if (minIndex == -1) {
227 return;
228 }
229 int maxIndex = m_categories.indexOf(maxCategory);
230 if (maxIndex == -1) {
231 return;
232 }
233 setRange(minIndex -0.5, maxIndex + 0.5);
234 }
235
236 246 void QCategoriesAxisPrivate::setMin(const QVariant &min)
237 247 {
238 setRange(min,m_max);
248 setRange(min,m_maxCategory);
239 249 }
240 250
241 251 void QCategoriesAxisPrivate::setMax(const QVariant &max)
242 252 {
243 setRange(m_min,max);
253 setRange(m_minCategory,max);
244 254 }
245 255
246 256 void QCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max, bool force)
247 257 {
248 Q_UNUSED(min);
249 Q_UNUSED(max);
250 Q_UNUSED(force);
251 // TODO: refactor
252 /*
253 if (max <= min) {
254 // max must be greater than min
255 return;
256 }
258 Q_UNUSED(force); // TODO: use this?
257 259 Q_Q(QCategoriesAxis);
258 bool changed = false;
259 if (!qFuzzyIsNull(m_min - min)) {
260 m_min = min;
261 changed = true;
262 }
263
264 if (!qFuzzyIsNull(m_max - max)) {
265 m_max = max;
266 changed = true;
267 }
268
269 if ((changed) ||(force)) {
270 emit this->changed(m_min, m_max, qCeil(m_max) -qCeil(m_min) +1, false);
271 emit q->categoriesChanged();
272 }
273 */
260 QString value1 = min.toString();
261 QString value2 = max.toString();
262 q->setRange(value1,value2);
274 263 }
275 264
276 265 int QCategoriesAxisPrivate::ticksCount() const
277 266 {
278 267 return m_categories.count()+1;
279 268 }
280 269
281 270 void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
282 271 {
283 272 m_min = min;
284 273 m_max = max;
285 274 m_ticksCount = count;
286 275 }
287 276
288 277 ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
289 278 {
290 279 Q_Q( QCategoriesAxis);
291 280 if(m_orientation == Qt::Vertical){
292 281 return new ChartCategoriesAxisY(q,presenter);
293 282 }else{
294 283 return new ChartCategoriesAxisX(q,presenter);
295 284 }
296 285 }
297 286
298 287 void QCategoriesAxisPrivate::updateRange()
299 288 {
300 289 setRange(m_min,m_max,true);
301 290 }
302 291
303 292
304 293 #include "moc_qcategoriesaxis.cpp"
305 294 #include "moc_qcategoriesaxis_p.cpp"
306 295
307 296 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,71
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCATEGORIESAXIS_H
22 22 #define QCATEGORIESAXIS_H
23 23
24 24 #include "qabstractaxis.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QCategoriesAxisPrivate;
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QCategoriesAxis : public QAbstractAxis
31 31 {
32 32 Q_OBJECT
33 33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
34 34
35 35 public:
36 36 explicit QCategoriesAxis(QObject *parent = 0);
37 37 ~QCategoriesAxis();
38 38
39 39 protected:
40 40 QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent = 0);
41 41
42 42 public:
43 43 AxisType type() const;
44 44 void append(const QStringList &categories);
45 45 void append(const QString &category);
46 46 void remove(const QString &category);
47 47 void insert(int index, const QString &category);
48 48 void clear();
49 49 void setCategories(const QStringList &categories);
50 50 QStringList categories();
51 51 int count() const;
52
53 52 QString at(int index) const;
54 53
55 //range handling convenience functions
56 void setMinCategory(const QString& minCategory);
57 QString minCategory() const;
58 void setMaxCategory(const QString& maxCategory);
59 QString maxCategory() const;
60 void setCategoryRange(const QString& minCategory, const QString& maxCategory);
61
62 void setMin(const QVariant &min);
63 void setMax(const QVariant &max);
64 void setRange(const QVariant &min, const QVariant &max);
54 //range handling
55 void setMin(const QString& minCategory);
56 QString min() const;
57 void setMax(const QString& maxCategory);
58 QString max() const;
59 void setRange(const QString& minCategory, const QString& maxCategory);
65 60
66 61 Q_SIGNALS:
67 62 void categoriesChanged();
68 63
69 64 private:
70 65 Q_DECLARE_PRIVATE(QCategoriesAxis)
71 66 Q_DISABLE_COPY(QCategoriesAxis)
72 67 };
73 68
74 69 QTCOMMERCIALCHART_END_NAMESPACE
75 70
76 71 #endif // QCATEGORIESAXIS_H
@@ -1,78 +1,74
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QCATEGORIESAXIS_P_H
31 31 #define QCATEGORIESAXIS_P_H
32 32
33 33 #include "qcategoriesaxis.h"
34 34 #include "qabstractaxis_p.h"
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 38 class QCategoriesAxisPrivate : public QAbstractAxisPrivate
39 39 {
40 40 Q_OBJECT
41 41
42 42 public:
43 43 QCategoriesAxisPrivate(QCategoriesAxis *q);
44 44 ~QCategoriesAxisPrivate();
45 45
46 46 public:
47 47 ChartAxis* createGraphics(ChartPresenter* presenter);
48 48 void updateRange();
49 49
50 50 private:
51 void setMinCategory(const QString& minCategory);
52 void setMaxCategory(const QString& maxCategory);
53 void setRangeCategory(const QString& minCategory, const QString& maxCategory);
54
55 51 //range handling
56 52 void setMin(const QVariant &min);
57 53 void setMax(const QVariant &max);
58 54 void setRange(const QVariant &min, const QVariant &max, bool force = false);
59 55 int ticksCount() const;
60 56
61 57 Q_SIGNALS:
62 58 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
63 59
64 60 public Q_SLOTS:
65 61 void handleAxisRangeChanged(qreal min, qreal max,int count);
66 62
67 63 private:
68 64 QStringList m_categories;
69 65 QString m_minCategory;
70 66 QString m_maxCategory;
71 67
72 68 private:
73 69 Q_DECLARE_PUBLIC(QCategoriesAxis)
74 70 };
75 71
76 72 QTCOMMERCIALCHART_END_NAMESPACE
77 73
78 74 #endif // QCATEGORIESAXIS_P_H
General Comments 0
You need to be logged in to leave comments. Login now