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