##// END OF EJS Templates
QLogValueAxis added. Log domain missing
Marek Rosa -
r2274:2d2a1c8b0f36
parent child
Show More
@@ -0,0 +1,122
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 "chartlogvalueaxisx_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27 #include <qmath.h>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
32 : HorizontalAxis(axis, item),
33 m_axis(axis)
34 {
35 }
36
37 ChartLogValueAxisX::~ChartLogValueAxisX()
38 {
39 }
40
41 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
42 {
43 QVector<qreal> points;
44
45 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
46 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
47 int tickCount = qAbs(qRound(logMax - logMin));
48
49 points.resize(tickCount);
50 const QRectF &gridRect = gridGeometry();
51 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
52 for (int i = 0; i < tickCount; ++i)
53 if (logMax > logMin)
54 points[i] = ((int)logMin + i) * deltaX - logMin * deltaX + gridRect.left();
55 else
56 points[i] = ((int)logMax + i) * deltaX - logMax * deltaX + gridRect.left();
57
58 return points;
59 }
60
61 void ChartLogValueAxisX::updateGeometry()
62 {
63 const QVector<qreal>& layout = ChartAxis::layout();
64 if (layout.isEmpty())
65 return;
66 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 HorizontalAxis::updateGeometry();
68 }
69
70 //void ChartLogValueAxisX::handleAxisUpdated()
71 //{
72 // ChartAxis::handleAxisUpdated();
73 //}
74
75 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 {
77 Q_UNUSED(constraint)
78
79 QFontMetrics fn(font());
80 QSizeF sh;
81
82 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
83 QStringList ticksList;
84 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
85 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
86 int tickCount = qAbs(qRound(logMax - logMin));
87
88 if (m_axis->max() > m_axis->min() && tickCount > 1)
89 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
90 else
91 ticksList.append(QString(" "));
92 qreal width = 0;
93 qreal height = 0;
94
95
96 switch (which) {
97 case Qt::MinimumSize:{
98 int count = qMax(ticksList.last().count(),ticksList.first().count());
99 width = fn.averageCharWidth() * count;
100 height = fn.height() + labelPadding();
101 width = qMax(width,base.width());
102 height += base.height();
103 sh = QSizeF(width,height);
104 break;
105 }
106 case Qt::PreferredSize: {
107 int count = qMax(ticksList.last().count(),ticksList.first().count());
108 width=fn.averageCharWidth() * count;
109 height=fn.height()+labelPadding();
110 width=qMax(width,base.width());
111 height+=base.height();
112 sh = QSizeF(width,height);
113 break;
114 }
115 default:
116 break;
117 }
118
119 return sh;
120 }
121
122 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,59
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 CHARTLOGVALUEAXISX_H
31 #define CHARTLOGVALUEAXISX_H
32
33 #include "horizontalaxis_p.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class QLogValueAxis;
38 class ChartPresenter;
39
40 class ChartLogValueAxisX : public HorizontalAxis
41 {
42 public:
43 ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item);
44 ~ChartLogValueAxisX();
45
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
47
48 protected:
49 void handleAxisUpdated();
50 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
52
53 private:
54 QLogValueAxis *m_axis;
55 };
56
57 QTCOMMERCIALCHART_END_NAMESPACE
58
59 #endif /* CHARTLOGVALUEAXISX_H */
@@ -0,0 +1,119
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 "chartlogvalueaxisy_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27 #include <qmath.h>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 : VerticalAxis(axis, item),
33 m_axis(axis)
34 {
35 }
36
37 ChartLogValueAxisY::~ChartLogValueAxisY()
38 {
39 }
40
41 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
42 {
43 QVector<qreal> points;
44 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
45 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
46 int tickCount = qAbs(qRound(logMax - logMin));
47
48 points.resize(tickCount);
49 const QRectF &gridRect = gridGeometry();
50 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
51 for (int i = 0; i < tickCount; ++i)
52 if (logMax > logMin)
53 points[i] = ((int)logMin + i) * -deltaY - logMin * -deltaY + gridRect.bottom();
54 else
55 points[i] = ((int)logMax + i) * -deltaY - logMax * -deltaY + gridRect.bottom();
56
57 return points;
58 }
59
60
61 void ChartLogValueAxisY::updateGeometry()
62 {
63 const QVector<qreal> &layout = ChartAxis::layout();
64 if (layout.isEmpty())
65 return;
66 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 VerticalAxis::updateGeometry();
68 }
69
70 //void ChartLogValueAxisY::handleAxisUpdated()
71 //{
72 // ChartAxis::handleAxisUpdated();
73 //}
74
75 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 {
77 Q_UNUSED(constraint)
78
79 QFontMetrics fn(font());
80 QSizeF sh;
81
82 QSizeF base = VerticalAxis::sizeHint(which, constraint);
83 QStringList ticksList;
84 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
85 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
86 int tickCount = qAbs(qRound(logMax - logMin));
87 if (m_axis->max() > m_axis->min() && tickCount > 1)
88 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
89 else
90 ticksList.append(QString(" "));
91 qreal width = 0;
92 qreal height = 0;
93
94 switch (which) {
95 case Qt::MinimumSize: {
96 width = fn.boundingRect("...").width() + labelPadding();
97 width += base.width();
98 height = fn.height();
99 height = qMax(height,base.height());
100 sh = QSizeF(width,height);
101 break;
102 }
103 case Qt::PreferredSize: {
104 int count = qMax(ticksList.first().count() , ticksList.last().count());
105 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
106 width += base.width();
107 height = fn.height() * ticksList.count();
108 height = qMax(height,base.height());
109 sh = QSizeF(width,height);
110 break;
111 }
112 default:
113 break;
114 }
115
116 return sh;
117 }
118
119 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,59
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 CHARTLOGVALUEAXISY_H
31 #define CHARTLOGVALUEAXISY_H
32
33 #include "verticalaxis_p.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class QLogValueAxis;
38 class ChartPresenter;
39
40 class ChartLogValueAxisY : public VerticalAxis
41 {
42 public:
43 ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item);
44 ~ChartLogValueAxisY();
45
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
47
48 protected:
49 void handleAxisUpdated();
50 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
52
53 private:
54 QLogValueAxis *m_axis;
55 };
56
57 QTCOMMERCIALCHART_END_NAMESPACE
58
59 #endif /* CHARTLOGVALUEAXISY_H */
@@ -0,0 +1,315
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 "qlogvalueaxis.h"
22 #include "qlogvalueaxis_p.h"
23 #include "chartlogvalueaxisx_p.h"
24 #include "chartlogvalueaxisy_p.h"
25 #include "domain_p.h"
26 #include <float.h>
27 #include <cmath>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 /*!
31 \class QLogValueAxis
32 \brief The QLogValueAxis class is used for manipulating chart's axis.
33 \mainclass
34 */
35
36 /*!
37 \qmlclass DateTimeAxis QLogValueAxis
38 \brief The DateTimeAxis element is used for manipulating chart's axes
39 \inherits AbstractAxis
40 */
41
42 /*!
43 \property QLogValueAxis::min
44 Defines the minimum value on the axis.
45 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
46 */
47 /*!
48 \qmlproperty real ValuesAxis::min
49 Defines the minimum value on the axis.
50 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
51 */
52
53 /*!
54 \property QLogValueAxis::max
55 Defines the maximum value on the axis.
56 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
57 */
58 /*!
59 \qmlproperty real ValuesAxis::max
60 Defines the maximum value on the axis.
61 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
62 */
63
64 /*!
65 \fn void QLogValueAxis::minChanged(qreal min)
66 Axis emits signal when \a min of axis has changed.
67 */
68 /*!
69 \qmlsignal ValuesAxis::onMinChanged(qreal min)
70 Axis emits signal when \a min of axis has changed.
71 */
72
73 /*!
74 \fn void QLogValueAxis::maxChanged(qreal max)
75 Axis emits signal when \a max of axis has changed.
76 */
77 /*!
78 \qmlsignal ValuesAxis::onMaxChanged(qreal max)
79 Axis emits signal when \a max of axis has changed.
80 */
81
82 /*!
83 \fn void QLogValueAxis::rangeChanged(qreal min, qreal max)
84 Axis emits signal when \a min or \a max of axis has changed.
85 */
86
87 /*!
88 Constructs an axis object which is a child of \a parent.
89 */
90 QLogValueAxis::QLogValueAxis(QObject *parent) :
91 QAbstractAxis(*new QLogValueAxisPrivate(this), parent)
92 {
93
94 }
95
96 /*!
97 \internal
98 */
99 QLogValueAxis::QLogValueAxis(QLogValueAxisPrivate &d, QObject *parent) : QAbstractAxis(d, parent)
100 {
101
102 }
103
104 /*!
105 Destroys the object
106 */
107 QLogValueAxis::~QLogValueAxis()
108 {
109
110 }
111
112 void QLogValueAxis::setMin(qreal min)
113 {
114 Q_D(QLogValueAxis);
115 setRange(min, qMax(d->m_max, min));
116 }
117
118 qreal QLogValueAxis::min() const
119 {
120 Q_D(const QLogValueAxis);
121 return d->m_min;
122 }
123
124 void QLogValueAxis::setMax(qreal max)
125 {
126 Q_D(QLogValueAxis);
127 setRange(qMin(d->m_min, max), max);
128 }
129
130 qreal QLogValueAxis::max() const
131 {
132 Q_D(const QLogValueAxis);
133 return d->m_max;
134 }
135
136 /*!
137 Sets range from \a min to \a max on the axis.
138 If min is greater than max then this function returns without making any changes.
139 */
140 void QLogValueAxis::setRange(qreal min, qreal max)
141 {
142 Q_D(QLogValueAxis);
143 bool changed = false;
144
145 if (min > max)
146 return;
147
148 if (min > 0) {
149 if (!qFuzzyCompare(d->m_min, min)) {
150 d->m_min = min;
151 changed = true;
152 emit minChanged(min);
153 }
154
155 if (!qFuzzyCompare(d->m_max, max)) {
156 d->m_max = max;
157 changed = true;
158 emit maxChanged(max);
159 }
160
161 if (changed) {
162 emit rangeChanged(min, max);
163 emit d->rangeChanged(min,max);
164 }
165 }
166 }
167
168 void QLogValueAxis::setLabelFormat(const QString &format)
169 {
170 Q_D(QLogValueAxis);
171 d->m_format = format;
172 }
173
174 QString QLogValueAxis::labelFormat() const
175 {
176 Q_D(const QLogValueAxis);
177 return d->m_format;
178 }
179
180 void QLogValueAxis::setBase(qreal base)
181 {
182 // check if base is correct
183 if (base <= 0 || qFuzzyCompare(base, 1))
184 return;
185
186 Q_D(QLogValueAxis);
187 d->m_base = base;
188 }
189
190 qreal QLogValueAxis::base() const
191 {
192 Q_D(const QLogValueAxis);
193 return d->m_base;
194 }
195
196 /*!
197 Returns the type of the axis
198 */
199 QAbstractAxis::AxisType QLogValueAxis::type() const
200 {
201 return AxisTypeLogValue;
202 }
203
204 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205
206 QLogValueAxisPrivate::QLogValueAxisPrivate(QLogValueAxis *q)
207 : QAbstractAxisPrivate(q),
208 m_min(1),
209 m_max(10),
210 m_base(10),
211 m_format(QString::null)
212 {
213 }
214
215 QLogValueAxisPrivate::~QLogValueAxisPrivate()
216 {
217
218 }
219
220 void QLogValueAxisPrivate::setMin(const QVariant &min)
221 {
222 Q_Q(QLogValueAxis);
223 bool ok;
224 qreal value = min.toReal(&ok);
225 if (ok)
226 q->setMin(value);
227 }
228
229 void QLogValueAxisPrivate::setMax(const QVariant &max)
230 {
231
232 Q_Q(QLogValueAxis);
233 bool ok;
234 qreal value = max.toReal(&ok);
235 if (ok)
236 q->setMax(value);
237 }
238
239 void QLogValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
240 {
241 Q_Q(QLogValueAxis);
242 bool ok1;
243 bool ok2;
244 qreal value1 = min.toReal(&ok1);
245 qreal value2 = max.toReal(&ok2);
246 if (ok1 && ok2)
247 q->setRange(value1, value2);
248 }
249
250 void QLogValueAxisPrivate::setRange(qreal min, qreal max)
251 {
252 Q_Q(QLogValueAxis);
253 bool changed = false;
254
255 if (min > max)
256 return;
257
258 if (min > 0) {
259 if (!qFuzzyCompare(m_min, min)) {
260 m_min = min;
261 changed = true;
262 emit q->minChanged(min);
263 }
264
265 if (!qFuzzyCompare(m_max, max)) {
266 m_max = max;
267 changed = true;
268 emit q->maxChanged(max);
269 }
270
271 if (changed) {
272 emit q->rangeChanged(min, max);
273 emit rangeChanged(min,max);
274 }
275 }
276 }
277
278 void QLogValueAxisPrivate::initializeGraphics(QGraphicsItem* parent)
279 {
280 Q_Q(QLogValueAxis);
281 ChartAxis* axis(0);
282 if (orientation() == Qt::Vertical)
283 axis = new ChartLogValueAxisY(q,parent);
284 if (orientation() == Qt::Horizontal)
285 axis = new ChartLogValueAxisX(q,parent);
286
287 m_item.reset(axis);
288 QAbstractAxisPrivate::initializeGraphics(parent);
289 }
290
291
292 void QLogValueAxisPrivate::initializeDomain(Domain *domain)
293 {
294 if (orientation() == Qt::Vertical) {
295 if(!qFuzzyCompare(m_max, m_min)) {
296 domain->setRangeY(m_min, m_max);
297 }
298 else {
299 setRange(domain->minY() + 1, domain->maxY());
300 }
301 }
302 if (orientation() == Qt::Horizontal) {
303 if(!qFuzzyCompare(m_max, m_min)) {
304 domain->setRangeX(m_min, m_max);
305 }
306 else {
307 setRange(domain->minX() + 1, domain->maxX());
308 }
309 }
310 }
311
312 #include "moc_qlogvalueaxis.cpp"
313 #include "moc_qlogvalueaxis_p.cpp"
314
315 QTCOMMERCIALCHART_END_NAMESPACE
@@ -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 #ifndef QLOGVALUEAXIS_H
22 #define QLOGVALUEAXIS_H
23
24 #include "qabstractaxis.h"
25
26 class QDateTime;
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 class QLogValueAxisPrivate;
31
32 class QTCOMMERCIALCHART_EXPORT QLogValueAxis : public QAbstractAxis
33 {
34 Q_OBJECT
35 Q_PROPERTY(qreal min READ min WRITE setMin NOTIFY minChanged)
36 Q_PROPERTY(qreal max READ max WRITE setMax NOTIFY maxChanged)
37 Q_PROPERTY(QString labelFormat READ labelFormat WRITE setLabelFormat)
38
39 public:
40 explicit QLogValueAxis(QObject *parent = 0);
41 ~QLogValueAxis();
42
43 protected:
44 QLogValueAxis(QLogValueAxisPrivate &d, QObject *parent = 0);
45
46 public:
47 AxisType type() const;
48
49 //range handling
50 void setMin(qreal min);
51 qreal min() const;
52 void setMax(qreal max);
53 qreal max() const;
54 void setRange(qreal min, qreal max);
55
56 void setLabelFormat(const QString &format);
57 QString labelFormat() const;
58
59 void setBase(qreal base);
60 qreal base() const;
61
62 Q_SIGNALS:
63 void minChanged(qreal min);
64 void maxChanged(qreal max);
65 void rangeChanged(qreal min, qreal max);
66
67 private:
68 Q_DECLARE_PRIVATE(QLogValueAxis)
69 Q_DISABLE_COPY(QLogValueAxis)
70 };
71
72 QTCOMMERCIALCHART_END_NAMESPACE
73
74 #endif // QLOGVALUEAXIS_H
@@ -0,0 +1,69
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 QLOGVALUEAXIS_P_H
31 #define QLOGVALUEAXIS_P_H
32
33 #include "qlogvalueaxis.h"
34 #include "qabstractaxis_p.h"
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
38 class QLogValueAxisPrivate : public QAbstractAxisPrivate
39 {
40 Q_OBJECT
41 public:
42 QLogValueAxisPrivate(QLogValueAxis *q);
43 ~QLogValueAxisPrivate();
44
45 public:
46 void initializeGraphics(QGraphicsItem* parent);
47 void initializeDomain(Domain *domain);
48
49 qreal min() { return m_min; }
50 qreal max() { return m_max; }
51 void setRange(qreal min,qreal max);
52
53 protected:
54 void setMin(const QVariant &min);
55 void setMax(const QVariant &max);
56 void setRange(const QVariant &min, const QVariant &max);
57 int tickCount() const;
58
59 protected:
60 qreal m_min;
61 qreal m_max;
62 qreal m_base;
63 QString m_format;
64 Q_DECLARE_PUBLIC(QLogValueAxis)
65 };
66
67 QTCOMMERCIALCHART_END_NAMESPACE
68
69 #endif // QLOGVALUEAXIS_P_H
@@ -1,68 +1,78
1 1 #Subdirectiores are defined here, because qt creator doesn't handle nested include(foo.pri) chains very well.
2 2
3 3 INCLUDEPATH += $$PWD \
4 4 $$PWD/valueaxis \
5 5 $$PWD/barcategoryaxis \
6 $$PWD/categoryaxis
6 $$PWD/categoryaxis \
7 $$PWD/logvalueaxis
7 8
8 9 DEPENDPATH += $$PWD \
9 10 $$PWD/valueaxis \
10 11 $$PWD/barcategoryaxis \
11 $$PWD/categoryaxis
12 $$PWD/categoryaxis \
13 $$PWD/logvalueaxis
12 14
13 15 SOURCES += \
14 16 $$PWD/chartaxis.cpp \
15 17 $$PWD/qabstractaxis.cpp \
16 18 $$PWD/verticalaxis.cpp \
17 19 $$PWD/horizontalaxis.cpp \
18 20 $$PWD/valueaxis/chartvalueaxisx.cpp \
19 21 $$PWD/valueaxis/chartvalueaxisy.cpp \
20 22 $$PWD/valueaxis/qvalueaxis.cpp \
21 23 $$PWD/barcategoryaxis/chartbarcategoryaxisx.cpp \
22 24 $$PWD/barcategoryaxis/chartbarcategoryaxisy.cpp \
23 25 $$PWD/barcategoryaxis/qbarcategoryaxis.cpp \
24 26 $$PWD/categoryaxis/chartcategoryaxisx.cpp \
25 27 $$PWD/categoryaxis/chartcategoryaxisy.cpp \
26 $$PWD/categoryaxis/qcategoryaxis.cpp
28 $$PWD/categoryaxis/qcategoryaxis.cpp \
29 $$PWD/logvalueaxis/chartlogvalueaxisx.cpp \
30 $$PWD/logvalueaxis/chartlogvalueaxisy.cpp \
31 $$PWD/logvalueaxis/qlogvalueaxis.cpp
27 32
28 33 PRIVATE_HEADERS += \
29 34 $$PWD/chartaxis_p.h \
30 35 $$PWD/qabstractaxis_p.h \
31 36 $$PWD/verticalaxis_p.h \
32 37 $$PWD/horizontalaxis_p.h \
33 38 $$PWD/valueaxis/chartvalueaxisx_p.h \
34 39 $$PWD/valueaxis/chartvalueaxisy_p.h \
35 40 $$PWD/valueaxis/qvalueaxis_p.h \
36 41 $$PWD/barcategoryaxis/chartbarcategoryaxisx_p.h \
37 42 $$PWD/barcategoryaxis/chartbarcategoryaxisy_p.h \
38 43 $$PWD/barcategoryaxis/qbarcategoryaxis_p.h \
39 44 $$PWD/categoryaxis/chartcategoryaxisx_p.h \
40 45 $$PWD/categoryaxis/chartcategoryaxisy_p.h \
41 $$PWD/categoryaxis/qcategoryaxis_p.h
46 $$PWD/categoryaxis/qcategoryaxis_p.h \
47 $$PWD/logvalueaxis/chartlogvalueaxisx_p.h \
48 $$PWD/logvalueaxis/chartlogvalueaxisy_p.h \
49 $$PWD/logvalueaxis/qlogvalueaxis_p.h
42 50
43 51 PUBLIC_HEADERS += \
44 52 $$PWD/qabstractaxis.h \
45 53 $$PWD/valueaxis/qvalueaxis.h \
46 54 $$PWD/barcategoryaxis/qbarcategoryaxis.h \
47 $$PWD/categoryaxis/qcategoryaxis.h
55 $$PWD/categoryaxis/qcategoryaxis.h \
56 $$PWD/logvalueaxis/qlogvalueaxis.h \
48 57
49 58 !linux-arm*: {
50 59 INCLUDEPATH += \
51 60 $$PWD/datetimeaxis
52 61
53 62 DEPENDPATH += \
54 63 $$PWD/datetimeaxis
55 64
56 65 SOURCES += \
57 66 $$PWD/datetimeaxis/chartdatetimeaxisx.cpp \
58 67 $$PWD/datetimeaxis/chartdatetimeaxisy.cpp \
59 68 $$PWD/datetimeaxis/qdatetimeaxis.cpp
60 69
61 70 PRIVATE_HEADERS += \
62 71 $$PWD/datetimeaxis/chartdatetimeaxisx_p.h \
63 72 $$PWD/datetimeaxis/chartdatetimeaxisy_p.h \
64 73 $$PWD/datetimeaxis/qdatetimeaxis_p.h
65 74
66 75 PUBLIC_HEADERS += \
67 76 $$PWD/datetimeaxis/qdatetimeaxis.h
68 77 }
78
@@ -1,462 +1,501
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 "chartaxis_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "qabstractaxis_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "chartlayout_p.h"
26 26 #include "domain_p.h"
27 27 #include <qmath.h>
28 28 #include <QDateTime>
29 29 #include <QValueAxis>
30 #include <QLogValueAxis>
30 31 #include <QGraphicsLayout>
31 32 #include <QFontMetrics>
32 33
33 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 35
35 36 ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
36 37 : ChartElement(item),
37 38 m_axis(axis),
38 39 m_labelsAngle(0),
39 40 m_grid(new QGraphicsItemGroup(item)),
40 41 m_arrow(new QGraphicsItemGroup(item)),
41 42 m_shades(new QGraphicsItemGroup(item)),
42 43 m_labels(new QGraphicsItemGroup(item)),
43 44 m_title(new QGraphicsSimpleTextItem(item)),
44 45 m_animation(0),
45 46 m_labelPadding(5),
46 47 m_intervalAxis(intervalAxis)
47 48 {
48 49 Q_ASSERT(item);
49 50 //initial initialization
50 51 m_arrow->setHandlesChildEvents(false);
51 52 m_arrow->setZValue(ChartPresenter::AxisZValue);
52 53 m_arrow->setVisible(m_axis->isLineVisible());
53 54 m_labels->setZValue(ChartPresenter::AxisZValue);
54 55 m_labels->setVisible(m_axis->labelsVisible());
55 56 m_shades->setZValue(ChartPresenter::ShadesZValue);
56 57 m_shades->setVisible(m_axis->shadesVisible());
57 58 m_grid->setZValue(ChartPresenter::GridZValue);
58 59 m_grid->setVisible(m_axis->isGridLineVisible());
59 60 m_title->setZValue(ChartPresenter::GridZValue);
60 61 connectSlots();
61 62
62 63 setFlag(QGraphicsItem::ItemHasNoContents,true);
63 64 }
64 65
65 66 void ChartAxis::connectSlots()
66 67 {
67 68 QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool)));
68 69 QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool)));
69 70 QObject::connect(m_axis,SIGNAL(gridLineVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool)));
70 71 QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool)));
71 72 QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool)));
72 73 QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int)));
73 74 QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&)));
74 75 QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&)));
75 76 QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&)));
76 77 QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&)));
77 78 QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&)));
78 79 QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&)));
79 80 QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&)));
80 81 QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&)));
81 82 QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&)));
82 83 QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&)));
83 84 QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&)));
84 85 QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal)));
85 86 }
86 87
87 88 ChartAxis::~ChartAxis()
88 89 {
89 90 }
90 91
91 92 void ChartAxis::setAnimation(AxisAnimation *animation)
92 93 {
93 94 m_animation = animation;
94 95 }
95 96
96 97 void ChartAxis::setLayout(QVector<qreal> &layout)
97 98 {
98 99 m_layoutVector = layout;
99 100 }
100 101
101 102 void ChartAxis::createItems(int count)
102 103 {
103 104 if (m_arrow->children().size() == 0){
104 105 QGraphicsLineItem* arrow = new ArrowItem(this, this);
105 106 arrow->setPen(m_axis->linePen());
106 107 m_arrow->addToGroup(arrow);
107 108 }
108 109
109 110 if (m_intervalAxis && m_grid->children().size() == 0) {
110 111 for (int i = 0 ; i < 2 ; i ++){
111 112 QGraphicsLineItem* item = new QGraphicsLineItem(this);
112 113 item->setPen(m_axis->gridLinePen());
113 114 m_grid->addToGroup(item);
114 115 }
115 116 }
116 117
117 118 for (int i = 0; i < count; ++i) {
118 119 QGraphicsLineItem* arrow = new QGraphicsLineItem(this);
119 120 arrow->setPen(m_axis->linePen());
120 121 QGraphicsLineItem* grid = new QGraphicsLineItem(this);
121 122 grid->setPen(m_axis->gridLinePen());
122 123 QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this);
123 124 label->setFont(m_axis->labelsFont());
124 125 label->setPen(m_axis->labelsPen());
125 126 label->setBrush(m_axis->labelsBrush());
126 127 m_arrow->addToGroup(arrow);
127 128 m_grid->addToGroup(grid);
128 129 m_labels->addToGroup(label);
129 130
130 131 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){
131 132 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
132 133 shades->setPen(m_axis->shadesPen());
133 134 shades->setBrush(m_axis->shadesBrush());
134 135 m_shades->addToGroup(shades);
135 136 }
136 137 }
137 138
138 139 }
139 140
140 141 void ChartAxis::deleteItems(int count)
141 142 {
142 143 QList<QGraphicsItem *> lines = m_grid->childItems();
143 144 QList<QGraphicsItem *> labels = m_labels->childItems();
144 145 QList<QGraphicsItem *> shades = m_shades->childItems();
145 146 QList<QGraphicsItem *> axis = m_arrow->childItems();
146 147
147 148 for (int i = 0; i < count; ++i) {
148 149 if (lines.size() % 2 && lines.size() > 1)
149 150 delete(shades.takeLast());
150 151 delete(lines.takeLast());
151 152 delete(labels.takeLast());
152 153 delete(axis.takeLast());
153 154 }
154 155 }
155 156
156 157 void ChartAxis::updateLayout(QVector<qreal> &layout)
157 158 {
158 159 int diff = m_layoutVector.size() - layout.size();
159 160
160 161 if (diff > 0)
161 162 deleteItems(diff);
162 163 else if (diff < 0)
163 164 createItems(-diff);
164 165
165 166 if (m_animation) {
166 167 switch (presenter()->state()) {
167 168 case ChartPresenter::ZoomInState:
168 169 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
169 170 m_animation->setAnimationPoint(presenter()->statePoint());
170 171 break;
171 172 case ChartPresenter::ZoomOutState:
172 173 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
173 174 m_animation->setAnimationPoint(presenter()->statePoint());
174 175 break;
175 176 case ChartPresenter::ScrollUpState:
176 177 case ChartPresenter::ScrollLeftState:
177 178 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
178 179 break;
179 180 case ChartPresenter::ScrollDownState:
180 181 case ChartPresenter::ScrollRightState:
181 182 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
182 183 break;
183 184 case ChartPresenter::ShowState:
184 185 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
185 186 break;
186 187 }
187 188 m_animation->setValues(m_layoutVector, layout);
188 189 presenter()->startAnimation(m_animation);
189 190 } else {
190 191 setLayout(layout);
191 192 updateGeometry();
192 193 }
193 194 }
194 195
195 196 void ChartAxis::setLabelPadding(int padding)
196 197 {
197 198 m_labelPadding = padding;
198 199 }
199 200
200 201 bool ChartAxis::isEmpty()
201 202 {
202 203 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max());
203 204 }
204 205
205 206 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
206 207 {
207 208 m_gridRect = grid;
208 209 m_axisRect = axis;
209 210
210 211 if (isEmpty())
211 212 return;
212 213
213 214 QVector<qreal> layout = calculateLayout();
214 215 updateLayout(layout);
215 216 }
216 217
217 218 qreal ChartAxis::min() const
218 219 {
219 220 return m_axis->d_ptr->min();
220 221 }
221 222
222 223 qreal ChartAxis::max() const
223 224 {
224 225 return m_axis->d_ptr->max();
225 226 }
226 227
227 228 QFont ChartAxis::font() const
228 229 {
229 230 return m_axis->labelsFont();
230 231 }
231 232
232 233 QFont ChartAxis::titleFont() const
233 234 {
234 235 return m_axis->titleFont();
235 236 }
236 237
237 238 QString ChartAxis::titleText() const
238 239 {
239 240 return m_axis->titleText();
240 241 }
241 242
242 243 void ChartAxis::axisSelected()
243 244 {
244 245 emit clicked();
245 246 }
246 247
247 248 Qt::Orientation ChartAxis::orientation() const
248 249 {
249 250 return m_axis->orientation();
250 251 }
251 252
252 253 Qt::Alignment ChartAxis::alignment() const
253 254 {
254 255 return m_axis->alignment();
255 256 }
256 257
257 258 void ChartAxis::setLabels(const QStringList &labels)
258 259 {
259 260 m_labelsList = labels;
260 261 }
261 262
262 263 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
263 264 {
264 265 Q_UNUSED(which);
265 266 Q_UNUSED(constraint);
266 267 return QSizeF();
267 268 }
268 269
269 270 //handlers
270 271
271 272 void ChartAxis::handleArrowVisibleChanged(bool visible)
272 273 {
273 274 m_arrow->setVisible(visible);
274 275 }
275 276
276 277 void ChartAxis::handleGridVisibleChanged(bool visible)
277 278 {
278 279 m_grid->setVisible(visible);
279 280 }
280 281
281 282 void ChartAxis::handleLabelsVisibleChanged(bool visible)
282 283 {
283 284 m_labels->setVisible(visible);
284 285 }
285 286
286 287 void ChartAxis::handleShadesVisibleChanged(bool visible)
287 288 {
288 289 m_shades->setVisible(visible);
289 290 }
290 291
291 292 void ChartAxis::handleLabelsAngleChanged(int angle)
292 293 {
293 294 foreach (QGraphicsItem *item, m_labels->childItems())
294 295 item->setRotation(angle);
295 296
296 297 m_labelsAngle = angle;
297 298 }
298 299
299 300 void ChartAxis::handleLabelsPenChanged(const QPen &pen)
300 301 {
301 302 foreach (QGraphicsItem *item , m_labels->childItems())
302 303 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
303 304 }
304 305
305 306 void ChartAxis::handleLabelsBrushChanged(const QBrush &brush)
306 307 {
307 308 foreach (QGraphicsItem *item , m_labels->childItems())
308 309 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
309 310 }
310 311
311 312 void ChartAxis::handleLabelsFontChanged(const QFont &font)
312 313 {
313 314 foreach (QGraphicsItem *item , m_labels->childItems())
314 315 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
315 316 QGraphicsLayoutItem::updateGeometry();
316 317 presenter()->layout()->invalidate();
317 318 }
318 319
319 320 void ChartAxis::handleShadesBrushChanged(const QBrush &brush)
320 321 {
321 322 foreach (QGraphicsItem *item , m_shades->childItems())
322 323 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
323 324 }
324 325
325 326 void ChartAxis::handleShadesPenChanged(const QPen &pen)
326 327 {
327 328 foreach (QGraphicsItem *item , m_shades->childItems())
328 329 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
329 330 }
330 331
331 332 void ChartAxis::handleArrowPenChanged(const QPen &pen)
332 333 {
333 334 foreach (QGraphicsItem *item , m_arrow->childItems())
334 335 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
335 336 }
336 337
337 338 void ChartAxis::handleGridPenChanged(const QPen &pen)
338 339 {
339 340 foreach (QGraphicsItem *item , m_grid->childItems())
340 341 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
341 342 }
342 343
343 344 void ChartAxis::handleTitleTextChanged(const QString &title)
344 345 {
345 346 Q_UNUSED(title)
346 347 QGraphicsLayoutItem::updateGeometry();
347 348 presenter()->layout()->invalidate();
348 349 }
349 350
350 351
351 352 void ChartAxis::handleTitlePenChanged(const QPen &pen)
352 353 {
353 354 m_title->setPen(pen);
354 355 }
355 356
356 357 void ChartAxis::handleTitleBrushChanged(const QBrush &brush)
357 358 {
358 359 m_title->setBrush(brush);
359 360 }
360 361
361 362 void ChartAxis::handleTitleFontChanged(const QFont &font)
362 363 {
363 364 if(m_title->font() != font){
364 365 m_title->setFont(font);
365 366 QGraphicsLayoutItem::updateGeometry();
366 367 presenter()->layout()->invalidate();
367 368 }
368 369 }
369 370
370 371 void ChartAxis::handleVisibleChanged(bool visible)
371 372 {
372 373 setVisible(visible);
373 374 }
374 375
375 376 void ChartAxis::handleRangeChanged(qreal min, qreal max)
376 377 {
377 378 Q_UNUSED(min);
378 379 Q_UNUSED(max);
379 380
380 381 if (!isEmpty()) {
381 382
382 383 QVector<qreal> layout = calculateLayout();
383 384 updateLayout(layout);
384 385 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
385 386 QSizeF after = sizeHint(Qt::PreferredSize);
386 387
387 388 if (before != after) {
388 389 QGraphicsLayoutItem::updateGeometry();
389 390 //we don't want to call invalidate on layout, since it will change minimum size of component,
390 391 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
391 392 //instead recalculate layout and use plotArea for extra space.
392 393 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
393 394 }
394 395 }
395 396
396 397 }
397 398
398 399 //helpers
399 400
400 401 QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format)
401 402 {
402 403 //TODO: Q_ASSERT(m_max > m_min);
403 404 //TODO: Q_ASSERT(ticks > 1);
404 405
405 406 QStringList labels;
406 407
407 408 if(max <= min || ticks < 1){
408 409 return labels;
409 410 }
410 411
411 412 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
412 413 n++;
413 414
414 415 if (format.isNull()) {
415 416 for (int i = 0; i < ticks; i++) {
416 417 qreal value = min + (i * (max - min) / (ticks - 1));
417 418 labels << QString::number(value, 'f', n);
418 419 }
419 420 } else {
420 421 QByteArray array = format.toLatin1();
421 422 for (int i = 0; i < ticks; i++) {
422 423 qreal value = min + (i * (max - min) / (ticks - 1));
423 424 if (format.contains("d")
424 425 || format.contains("i")
425 426 || format.contains("c"))
426 427 labels << QString().sprintf(array, (qint64)value);
427 428 else if (format.contains("u")
428 429 || format.contains("o")
429 430 || format.contains("x", Qt::CaseInsensitive))
430 431 labels << QString().sprintf(array, (quint64)value);
431 432 else if (format.contains("f", Qt::CaseInsensitive)
432 433 || format.contains("e", Qt::CaseInsensitive)
433 434 || format.contains("g", Qt::CaseInsensitive))
434 435 labels << QString().sprintf(array, value);
435 436 }
436 437 }
437 438
438 439 return labels;
439 440 }
440 441
442 QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format)
443 {
444 // Q_ASSERT(m_max > m_min);
445 // Q_ASSERT(ticks > 1);
446
447 QStringList labels;
448
449 int n = 0;
450 if (ticks > 1)
451 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
452 n++;
453
454 // QLogValueAxis *axis = qobject_cast<QLogValueAxis *>(m_chartAxis);
455
456 // QString format = axis->labelFormat();
457
458 int firstTick;
459 if (base > 1)
460 firstTick = (int)(log10(min) / log10(base));
461 else
462 firstTick = (int)(log10(max) / log10(base));
463
464 if (format.isNull()) {
465 for (int i = firstTick; i < ticks + firstTick; i++) {
466 qreal value = qPow(base, i);
467 labels << QString::number(value, 'f', n);
468 }
469 } else {
470 QByteArray array = format.toAscii();
471 for (int i = firstTick; i < ticks + firstTick; i++) {
472 qreal value = qPow(base, i);
473 labels << QString().sprintf(array, value);
474 }
475 }
476
477 return labels;
478 }
479
441 480 QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format)
442 481 {
443 482 //TODO: Q_ASSERT(m_max > m_min);
444 483 //TODO: Q_ASSERT(ticks > 1);
445 484 QStringList labels;
446 485
447 486 if(max <= min || ticks < 1) {
448 487 return labels;
449 488 }
450 489
451 490 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
452 491 n++;
453 492 for (int i = 0; i < ticks; i++) {
454 493 qreal value = min + (i * (max - min) / (ticks - 1));
455 494 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
456 495 }
457 496 return labels;
458 497 }
459 498
460 499 #include "moc_chartaxis_p.cpp"
461 500
462 501 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,194 +1,195
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 CHARTAXIS_H
31 31 #define CHARTAXIS_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include "chartelement_p.h"
35 35 #include "axisanimation_p.h"
36 36 #include <QGraphicsItem>
37 37 #include <QGraphicsLayoutItem>
38 38 #include <QFont>
39 39
40 40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 41
42 42 class QAbstractAxis;
43 43 class ChartPresenter;
44 44
45 45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
46 46 {
47 47 Q_OBJECT
48 48 Q_INTERFACES(QGraphicsLayoutItem)
49 49 public:
50 50
51 51 ChartAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false);
52 52 ~ChartAxis();
53 53
54 54 QAbstractAxis* axis() const { return m_axis; }
55 55
56 56 void setLabelPadding(int padding);
57 57 int labelPadding() const { return m_labelPadding;};
58 58
59 59 QFont titleFont() const;
60 60 QString titleText() const;
61 61
62 62 void setLayout(QVector<qreal> &layout);
63 63 QVector<qreal> layout() const { return m_layoutVector; }
64 64
65 65 void setAnimation(AxisAnimation *animation);
66 66 ChartAnimation *animation() const { return m_animation; };
67 67
68 68 Qt::Orientation orientation() const;
69 69 Qt::Alignment alignment() const;
70 70
71 71 void setGeometry(const QRectF &axis, const QRectF &grid);
72 72 QRectF axisGeometry() const { return m_axisRect; }
73 73 QRectF gridGeometry() const { return m_gridRect; }
74 74
75 75 void setLabels(const QStringList &labels);
76 76 QStringList labels() const { return m_labelsList; }
77 77
78 78 //this flag indicates that axis is used to show intervals it means labels are in between ticks
79 79 bool intervalAxis() const { return m_intervalAxis; }
80 80
81 81 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
82 82
83 83
84 84 QRectF boundingRect() const{
85 85 return QRectF();
86 86 }
87 87
88 88 void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
89 89 {
90 90
91 91 }
92 92
93 93 //helpers
94 94 static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format);
95 static QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format);
95 96 static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format);
96 97
97 98 protected:
98 99 void setGeometry(const QRectF &size) { Q_UNUSED(size);};
99 100 virtual void updateGeometry() = 0;
100 101 virtual QVector<qreal> calculateLayout() const = 0;
101 102
102 103 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
103 104 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
104 105 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
105 106 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
106 107 QGraphicsSimpleTextItem* titleItem() const { return m_title.data();}
107 108
108 109 QFont font() const;
109 110 qreal min() const;
110 111 qreal max() const;
111 112
112 113 //handlers
113 114 public Q_SLOTS:
114 115 void handleVisibleChanged(bool visible);
115 116 void handleArrowVisibleChanged(bool visible);
116 117 void handleGridVisibleChanged(bool visible);
117 118 void handleLabelsVisibleChanged(bool visible);
118 119 void handleShadesVisibleChanged(bool visible);
119 120 void handleLabelsAngleChanged(int angle);
120 121 void handleShadesBrushChanged(const QBrush &brush);
121 122 void handleShadesPenChanged(const QPen &pen);
122 123 void handleArrowPenChanged(const QPen &pen);
123 124 void handleGridPenChanged(const QPen &pen);
124 125 void handleLabelsPenChanged(const QPen &pen);
125 126 void handleLabelsBrushChanged(const QBrush &brush);
126 127 void handleLabelsFontChanged(const QFont &font);
127 128 void handleTitlePenChanged(const QPen &pen);
128 129 void handleTitleBrushChanged(const QBrush &brush);
129 130 void handleTitleFontChanged(const QFont &font);
130 131 void handleTitleTextChanged(const QString &title);
131 132 void handleRangeChanged(qreal min , qreal max);
132 133
133 134 Q_SIGNALS:
134 135 void clicked();
135 136
136 137 private:
137 138 inline bool isEmpty();
138 139 void createItems(int count);
139 140 void deleteItems(int count);
140 141 void updateLayout(QVector<qreal> &layout);
141 142 void axisSelected();
142 143 void connectSlots();
143 144
144 145 private:
145 146 QAbstractAxis *m_axis;
146 147 int m_labelsAngle;
147 148 QRectF m_axisRect;
148 149 QRectF m_gridRect;
149 150 QScopedPointer<QGraphicsItemGroup> m_grid;
150 151 QScopedPointer<QGraphicsItemGroup> m_arrow;
151 152 QScopedPointer<QGraphicsItemGroup> m_shades;
152 153 QScopedPointer<QGraphicsItemGroup> m_labels;
153 154 QScopedPointer<QGraphicsSimpleTextItem> m_title;
154 155 QVector<qreal> m_layoutVector;
155 156 AxisAnimation *m_animation;
156 157 int m_labelPadding;
157 158 QStringList m_labelsList;
158 159 bool m_intervalAxis;
159 160
160 161 friend class AxisAnimation;
161 162 friend class ArrowItem;
162 163
163 164 };
164 165
165 166 class ArrowItem: public QGraphicsLineItem
166 167 {
167 168
168 169 public:
169 170 explicit ArrowItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
170 171
171 172 protected:
172 173 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
173 174 Q_UNUSED(event)
174 175 m_axis->axisSelected();
175 176 }
176 177
177 178 QRectF boundingRect() const {
178 179 return shape().boundingRect();
179 180 }
180 181
181 182 QPainterPath shape() const {
182 183 QPainterPath path = QGraphicsLineItem::shape();
183 184 QRectF rect = path.boundingRect();
184 185 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
185 186 return path;
186 187 }
187 188
188 189 private:
189 190 ChartAxis *m_axis;
190 191 };
191 192
192 193 QTCOMMERCIALCHART_END_NAMESPACE
193 194
194 195 #endif /* CHARTAXI_H */
@@ -1,191 +1,192
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 QABSTRACTAXIS_H
22 22 #define QABSTRACTAXIS_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QFont>
27 27 #include <QVariant>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QAbstractAxisPrivate;
32 32
33 33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
34 34 {
35 35 Q_OBJECT
36 36 //visibility
37 37 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
38 38 //arrow
39 39 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
40 40 Q_PROPERTY(QPen linePen READ linePen WRITE setLinePen NOTIFY linePenChanged)
41 41 //TODO: make wrapping of color for qml
42 42 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
43 43 //labels
44 44 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
45 45 Q_PROPERTY(QPen lablesPen READ labelsPen WRITE setLabelsPen NOTIFY labelsPenChanged)
46 46 Q_PROPERTY(QBrush lablesBrush READ labelsBrush WRITE setLabelsBrush NOTIFY labelsBrushChanged)
47 47 //TODO: fix labels angles to work with layout
48 48 Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
49 49 Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont NOTIFY labelsFontChanged)
50 50 //TODO: make wrapping of color for qml
51 51 Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged)
52 52 //grid
53 53 Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridLineVisibleChanged)
54 54 Q_PROPERTY(QPen girdLinePen READ gridLinePen WRITE setGridLinePen NOTIFY gridLinePenChanged)
55 55 //shades
56 56 Q_PROPERTY(bool shadesVisible READ shadesVisible WRITE setShadesVisible NOTIFY shadesVisibleChanged)
57 57 //TODO: make wrapping of color for qml
58 58 Q_PROPERTY(QColor shadesColor READ shadesColor WRITE setShadesColor NOTIFY shadesColorChanged)
59 59 //TODO: make wrapping of border for qml
60 60 Q_PROPERTY(QColor shadesBorderColor READ shadesBorderColor WRITE setShadesBorderColor NOTIFY shadesBorderColorChanged)
61 61 Q_PROPERTY(QPen shadesPen READ shadesPen WRITE setShadesPen NOTIFY shadesPenChanged)
62 62 Q_PROPERTY(QBrush shadesBrush READ shadesBrush WRITE setShadesBrush NOTIFY shadesBrushChanged)
63 63 //title
64 64 Q_PROPERTY(QString titleText READ titleText WRITE setTitleText NOTIFY titleTextChanged)
65 65 Q_PROPERTY(QPen titlePen READ titlePen WRITE setTitlePen NOTIFY titlePenChanged)
66 66 Q_PROPERTY(QBrush titleBrush READ titleBrush WRITE setTitleBrush NOTIFY titleBrushChanged)
67 67 Q_PROPERTY(bool titleVisible READ titleVisible WRITE setTitleVisible)
68 68 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
69 69
70 70 public:
71 71
72 72 enum AxisType {
73 73 AxisTypeNoAxis = 0x0,
74 74 AxisTypeValue = 0x1,
75 75 AxisTypeBarCategory = 0x2,
76 76 AxisTypeCategory = 0x3,
77 AxisTypeDateTime = 0x4
77 AxisTypeDateTime = 0x4,
78 AxisTypeLogValue = 0x5
78 79 };
79 80
80 81 Q_DECLARE_FLAGS(AxisTypes, AxisType)
81 82
82 83 protected:
83 84 explicit QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent = 0);
84 85
85 86 public:
86 87 ~QAbstractAxis();
87 88
88 89 virtual AxisType type() const = 0;
89 90
90 91 //visibility handling
91 92 bool isVisible() const;
92 93 void setVisible(bool visible = true);
93 94 void show();
94 95 void hide();
95 96
96 97 //arrow handling
97 98 bool isLineVisible() const;
98 99 void setLineVisible(bool visible = true);
99 100 void setLinePen(const QPen &pen);
100 101 QPen linePen() const;
101 102 void setLinePenColor(QColor color);
102 103 QColor linePenColor() const;
103 104
104 105 //grid handling
105 106 bool isGridLineVisible() const;
106 107 void setGridLineVisible(bool visible = true);
107 108 void setGridLinePen(const QPen &pen);
108 109 QPen gridLinePen() const;
109 110
110 111 //labels handling
111 112 bool labelsVisible() const;
112 113 void setLabelsVisible(bool visible = true);
113 114 void setLabelsPen(const QPen &pen);
114 115 QPen labelsPen() const;
115 116 void setLabelsBrush(const QBrush &brush);
116 117 QBrush labelsBrush() const;
117 118 void setLabelsFont(const QFont &font);
118 119 QFont labelsFont() const;
119 120 void setLabelsAngle(int angle);
120 121 int labelsAngle() const;
121 122 void setLabelsColor(QColor color);
122 123 QColor labelsColor() const;
123 124
124 125 //title handling
125 126 bool titleVisible() const;
126 127 void setTitleVisible(bool visible = true);
127 128 void setTitlePen(const QPen &pen);
128 129 QPen titlePen() const;
129 130 void setTitleBrush(const QBrush &brush);
130 131 QBrush titleBrush() const;
131 132 void setTitleFont(const QFont &font);
132 133 QFont titleFont() const;
133 134 void setTitleText(const QString &title);
134 135 QString titleText() const;
135 136
136 137 //shades handling
137 138 bool shadesVisible() const;
138 139 void setShadesVisible(bool visible = true);
139 140 void setShadesPen(const QPen &pen);
140 141 QPen shadesPen() const;
141 142 void setShadesBrush(const QBrush &brush);
142 143 QBrush shadesBrush() const;
143 144 void setShadesColor(QColor color);
144 145 QColor shadesColor() const;
145 146 void setShadesBorderColor(QColor color);
146 147 QColor shadesBorderColor() const;
147 148
148 149 Qt::Orientation orientation(); //TODO: missing const <- BC
149 150 Qt::Alignment alignment() const;
150 151
151 152 //range handling
152 153 void setMin(const QVariant &min);
153 154 void setMax(const QVariant &max);
154 155 void setRange(const QVariant &min, const QVariant &max);
155 156
156 157 Q_SIGNALS:
157 158 void visibleChanged(bool visible);
158 159 void linePenChanged(const QPen& pen);
159 160 void lineVisibleChanged(bool visible);
160 161 void labelsVisibleChanged(bool visible);
161 162 void labelsPenChanged(const QPen& pen);
162 163 void labelsBrushChanged(const QBrush& brush);
163 164 void labelsFontChanged(const QFont& pen);
164 165 void labelsAngleChanged(int angle);
165 166 void gridLinePenChanged(const QPen& pen);
166 167 void gridLineVisibleChanged(bool visible);
167 168 void colorChanged(QColor color);
168 169 void labelsColorChanged(QColor color);
169 170 void titleTextChanged(const QString& title);
170 171 void titlePenChanged(const QPen& pen);
171 172 void titleBrushChanged(const QBrush brush);
172 173 void titleVisibleChanged(bool visible);
173 174 void titleFontChanged(const QFont& font);
174 175 void shadesVisibleChanged(bool visible);
175 176 void shadesColorChanged(QColor color);
176 177 void shadesBorderColorChanged(QColor color);
177 178 void shadesPenChanged(const QPen& pen);
178 179 void shadesBrushChanged(const QBrush brush);
179 180
180 181 protected:
181 182 QScopedPointer<QAbstractAxisPrivate> d_ptr;
182 183 Q_DISABLE_COPY(QAbstractAxis)
183 184 friend class ChartDataSet;
184 185 friend class ChartAxis;
185 186 friend class ChartPresenter;
186 187 friend class ChartThemeManager;
187 188 };
188 189
189 190 QTCOMMERCIALCHART_END_NAMESPACE
190 191
191 192 #endif // QABSTRACTAXIS_H
General Comments 0
You need to be logged in to leave comments. Login now