##// END OF EJS Templates
renamed setRange to setCategoryRange in category axis.
sauimone -
r1576:89c5fcadff3e
parent child
Show More
@@ -1,131 +1,132
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 "chartcategoriesaxisx_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "chartanimator_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QDebug>
27 27 #include <QFontMetrics>
28 28 #include <QCategoriesAxis>
29 29 #include <qmath.h>
30 30
31 31 static int label_padding = 5;
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 ChartCategoriesAxisX::ChartCategoriesAxisX(QCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
36 36 m_categoriesAxis(axis)
37 37 {
38 38 }
39 39
40 40 ChartCategoriesAxisX::~ChartCategoriesAxisX()
41 41 {
42 42 }
43 43
44 44 bool ChartCategoriesAxisX::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
45 45 {
46 46 Q_ASSERT(max>min);
47 47 Q_UNUSED(ticks);
48 48 Q_UNUSED(max);
49 49
50 50 QStringList categories = m_categoriesAxis->categories();
51 51
52 52 int x = qCeil(min);
53 53 int count = 0;
54 54
55 55 // Try to find category for x coordinate
56 56 while (count < ticks) {
57 57 if ((x < categories.count()) && (x >= 0)) {
58 58 labels << categories.at(x);
59 59 } else {
60 60 // No label for x coordinate
61 61 labels << "";
62 62 }
63 63 x++;
64 64 count++;
65 65 }
66 66
67 67 return true;
68 68 }
69 69
70 70 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
71 71 {
72 72 Q_ASSERT(m_ticksCount>=2);
73 73
74 74 QVector<qreal> points;
75 75 points.resize(m_ticksCount);
76 76
77 // TODO: shift logic
77 78 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
78 79 for (int i = 0; i < m_ticksCount; ++i) {
79 80 int x = i * deltaX + m_rect.left();
80 81 points[i] = x;
81 82 }
82 83 return points;
83 84 }
84 85
85 86 void ChartCategoriesAxisX::updateGeometry()
86 87 {
87 88 const QVector<qreal>& layout = ChartAxis::layout();
88 89
89 90 m_minWidth = 0;
90 91 m_minHeight = 0;
91 92
92 93 if(layout.isEmpty()) return;
93 94
94 95 QStringList ticksList;
95 96
96 97 createLabels(ticksList,m_min,m_max,layout.size());
97 98
98 99 QList<QGraphicsItem *> lines = m_grid->childItems();
99 100 QList<QGraphicsItem *> labels = m_labels->childItems();
100 101 QList<QGraphicsItem *> shades = m_shades->childItems();
101 102 QList<QGraphicsItem *> axis = m_axis->childItems();
102 103
103 104 Q_ASSERT(labels.size() == ticksList.size());
104 105 Q_ASSERT(layout.size() == ticksList.size());
105 106
106 107 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
107 108 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
108 109
109 110 for (int i = 1; i < layout.size(); ++i) {
110 111 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
111 112 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
112 113 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i-1));
113 114
114 115 labelItem->setText(ticksList.at(i-1));
115 116 const QRectF& rect = labelItem->boundingRect();
116 117 QPointF center = rect.center();
117 118 labelItem->setTransformOriginPoint(center.x(), center.y());
118 119 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
119 120 m_minWidth+=rect.width();
120 121 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
121 122
122 123 if ((i+1)%2 && i>1) {
123 124 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
124 125 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
125 126 }
126 127 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
127 128 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
128 129 }
129 130 }
130 131
131 132 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,301 +1,301
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 void QCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
165 void QCategoriesAxis::setCategoryRange(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 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 214
215 215 int minIndex = qCeil(m_min);
216 216 if (maxIndex < minIndex) {
217 217 minIndex = 0;
218 218 }
219 219 setRange(minIndex - 0.5, maxIndex + 0.5);
220 220 }
221 221
222 222 void QCategoriesAxisPrivate::setRangeCategory(const QString& minCategory, const QString& maxCategory)
223 223 {
224 224 // TODO:
225 225 int minIndex = m_categories.indexOf(minCategory);
226 226 if (minIndex == -1) {
227 227 return;
228 228 }
229 229 int maxIndex = m_categories.indexOf(maxCategory);
230 230 if (maxIndex == -1) {
231 231 return;
232 232 }
233 233 setRange(minIndex -0.5, maxIndex + 0.5);
234 234 }
235 235
236 236 void QCategoriesAxisPrivate::setMin(const qreal min)
237 237 {
238 238 setRange(min,m_max);
239 239 }
240 240
241 241 void QCategoriesAxisPrivate::setMax(const qreal max)
242 242 {
243 243 setRange(m_min,max);
244 244 }
245 245
246 246 void QCategoriesAxisPrivate::setRange(const qreal min, const qreal max, bool force)
247 247 {
248 248 if (max <= min) {
249 249 // max must be greater than min
250 250 return;
251 251 }
252 252 Q_Q(QCategoriesAxis);
253 253 bool changed = false;
254 254 if (!qFuzzyIsNull(m_min - min)) {
255 255 m_min = min;
256 256 changed = true;
257 257 }
258 258
259 259 if (!qFuzzyIsNull(m_max - max)) {
260 260 m_max = max;
261 261 changed = true;
262 262 }
263 263
264 264 if ((changed) ||(force)) {
265 265 emit this->changed(m_min, m_max, qCeil(m_max) -qCeil(m_min) +1, false);
266 266 emit q->categoriesChanged();
267 267 }
268 268 }
269 269
270 270 int QCategoriesAxisPrivate::ticksCount() const
271 271 {
272 272 return m_categories.count()+1;
273 273 }
274 274
275 275 void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
276 276 {
277 277 m_min = min;
278 278 m_max = max;
279 279 m_ticksCount = count;
280 280 }
281 281
282 282 ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
283 283 {
284 284 Q_Q( QCategoriesAxis);
285 285 if(m_orientation == Qt::Vertical){
286 286 return new ChartCategoriesAxisY(q,presenter);
287 287 }else{
288 288 return new ChartCategoriesAxisX(q,presenter);
289 289 }
290 290 }
291 291
292 292 void QCategoriesAxisPrivate::updateRange()
293 293 {
294 294 setRange(m_min,m_max,true);
295 295 }
296 296
297 297
298 298 #include "moc_qcategoriesaxis.cpp"
299 299 #include "moc_qcategoriesaxis_p.cpp"
300 300
301 301 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,72
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 52
53 53 QString at(int index) const;
54 54
55 55 //range handling convenience functions
56 56 void setMinCategory(const QString& minCategory);
57 57 QString minCategory() const;
58 58 void setMaxCategory(const QString& maxCategory);
59 59 QString maxCategory() const;
60 void setRange(const QString& minCategory, const QString& maxCategory);
60 void setCategoryRange(const QString& minCategory, const QString& maxCategory);
61 61
62 62 Q_SIGNALS:
63 63 void categoriesChanged();
64 64
65 65 private:
66 66 Q_DECLARE_PRIVATE(QCategoriesAxis)
67 67 Q_DISABLE_COPY(QCategoriesAxis)
68 68 };
69 69
70 70 QTCOMMERCIALCHART_END_NAMESPACE
71 71
72 72 #endif // QCATEGORIESAXIS_H
General Comments 0
You need to be logged in to leave comments. Login now