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