@@ -1,476 +1,486 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "chartaxis_p.h" |
|
21 | #include "chartaxis_p.h" | |
22 | #include "qabstractaxis.h" |
|
22 | #include "qabstractaxis.h" | |
23 | #include "qabstractaxis_p.h" |
|
23 | #include "qabstractaxis_p.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include "chartlayout_p.h" |
|
25 | #include "chartlayout_p.h" | |
26 | #include "domain_p.h" |
|
26 | #include "domain_p.h" | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 | #include <QDateTime> |
|
28 | #include <QDateTime> | |
29 | #include <QValueAxis> |
|
29 | #include <QValueAxis> | |
30 | #include <QGraphicsLayout> |
|
30 | #include <QGraphicsLayout> | |
31 | #include <QFontMetrics> |
|
31 | #include <QFontMetrics> | |
32 |
|
32 | |||
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
34 |
|
34 | |||
35 | ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) |
|
35 | ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) | |
36 | : ChartElement(presenter), |
|
36 | : ChartElement(presenter), | |
37 | m_chartAxis(axis), |
|
37 | m_chartAxis(axis), | |
38 | m_labelsAngle(0), |
|
38 | m_labelsAngle(0), | |
39 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), |
|
39 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), | |
40 | m_shades(new QGraphicsItemGroup(presenter->rootItem())), |
|
40 | m_shades(new QGraphicsItemGroup(presenter->rootItem())), | |
41 | m_labels(new QGraphicsItemGroup(presenter->rootItem())), |
|
41 | m_labels(new QGraphicsItemGroup(presenter->rootItem())), | |
42 | m_arrow(new QGraphicsItemGroup(presenter->rootItem())), |
|
42 | m_arrow(new QGraphicsItemGroup(presenter->rootItem())), | |
43 | m_title(new QGraphicsSimpleTextItem(presenter->rootItem())), |
|
43 | m_title(new QGraphicsSimpleTextItem(presenter->rootItem())), | |
44 | m_min(0), |
|
44 | m_min(0), | |
45 | m_max(0), |
|
45 | m_max(0), | |
46 | m_animation(0), |
|
46 | m_animation(0), | |
47 | m_labelPadding(5), |
|
47 | m_labelPadding(5), | |
48 | m_intervalAxis(intervalAxis) |
|
48 | m_intervalAxis(intervalAxis) | |
49 | { |
|
49 | { | |
50 | //initial initialization |
|
50 | //initial initialization | |
51 | m_arrow->setZValue(ChartPresenter::AxisZValue); |
|
51 | m_arrow->setZValue(ChartPresenter::AxisZValue); | |
52 | m_arrow->setHandlesChildEvents(false); |
|
52 | m_arrow->setHandlesChildEvents(false); | |
53 | m_labels->setZValue(ChartPresenter::AxisZValue); |
|
53 | m_labels->setZValue(ChartPresenter::AxisZValue); | |
54 | m_shades->setZValue(ChartPresenter::ShadesZValue); |
|
54 | m_shades->setZValue(ChartPresenter::ShadesZValue); | |
55 | m_grid->setZValue(ChartPresenter::GridZValue); |
|
55 | m_grid->setZValue(ChartPresenter::GridZValue); | |
56 | m_title->setZValue(ChartPresenter::GridZValue); |
|
56 | m_title->setZValue(ChartPresenter::GridZValue); | |
57 |
|
57 | |||
58 | QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated())); |
|
58 | QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated())); | |
59 |
|
59 | |||
60 | QGraphicsSimpleTextItem item; |
|
60 | QGraphicsSimpleTextItem item; | |
61 | m_font = item.font(); |
|
61 | m_font = item.font(); | |
62 |
|
62 | |||
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | ChartAxis::~ChartAxis() |
|
65 | ChartAxis::~ChartAxis() | |
66 | { |
|
66 | { | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | void ChartAxis::setAnimation(AxisAnimation *animation) |
|
69 | void ChartAxis::setAnimation(AxisAnimation *animation) | |
70 | { |
|
70 | { | |
71 | m_animation = animation; |
|
71 | m_animation = animation; | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | void ChartAxis::setLayout(QVector<qreal> &layout) |
|
74 | void ChartAxis::setLayout(QVector<qreal> &layout) | |
75 | { |
|
75 | { | |
76 | m_layoutVector = layout; |
|
76 | m_layoutVector = layout; | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | void ChartAxis::createItems(int count) |
|
79 | void ChartAxis::createItems(int count) | |
80 | { |
|
80 | { | |
81 | if (m_arrow->childItems().size() == 0) |
|
81 | if (m_arrow->childItems().size() == 0) | |
82 | m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem())); |
|
82 | m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem())); | |
83 |
|
83 | |||
84 | if (m_intervalAxis && m_grid->childItems().size() == 0) { |
|
84 | if (m_intervalAxis && m_grid->childItems().size() == 0) { | |
85 | for (int i = 0 ; i < 2 ; i ++) |
|
85 | for (int i = 0 ; i < 2 ; i ++) | |
86 | m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); |
|
86 | m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | for (int i = 0; i < count; ++i) { |
|
89 | for (int i = 0; i < count; ++i) { | |
90 | m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); |
|
90 | m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); | |
91 | QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem()); |
|
91 | QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem()); | |
92 | label->setFont(m_font); |
|
92 | label->setFont(m_font); | |
93 | m_labels->addToGroup(label); |
|
93 | m_labels->addToGroup(label); | |
94 | m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); |
|
94 | m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); | |
95 | if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2) |
|
95 | if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2) | |
96 | m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); |
|
96 | m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); | |
97 | } |
|
97 | } | |
98 | } |
|
98 | } | |
99 |
|
99 | |||
100 | void ChartAxis::deleteItems(int count) |
|
100 | void ChartAxis::deleteItems(int count) | |
101 | { |
|
101 | { | |
102 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
102 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
103 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
103 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
104 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
104 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
105 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
105 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | |
106 |
|
106 | |||
107 | for (int i = 0; i < count; ++i) { |
|
107 | for (int i = 0; i < count; ++i) { | |
108 | if (lines.size() % 2 && lines.size() > 1) |
|
108 | if (lines.size() % 2 && lines.size() > 1) | |
109 | delete(shades.takeLast()); |
|
109 | delete(shades.takeLast()); | |
110 | delete(lines.takeLast()); |
|
110 | delete(lines.takeLast()); | |
111 | delete(labels.takeLast()); |
|
111 | delete(labels.takeLast()); | |
112 | delete(axis.takeLast()); |
|
112 | delete(axis.takeLast()); | |
113 | } |
|
113 | } | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | void ChartAxis::updateLayout(QVector<qreal> &layout) |
|
116 | void ChartAxis::updateLayout(QVector<qreal> &layout) | |
117 | { |
|
117 | { | |
118 | int diff = m_layoutVector.size() - layout.size(); |
|
118 | int diff = m_layoutVector.size() - layout.size(); | |
119 |
|
119 | |||
120 | if (diff > 0) |
|
120 | if (diff > 0) | |
121 | deleteItems(diff); |
|
121 | deleteItems(diff); | |
122 | else if (diff < 0) |
|
122 | else if (diff < 0) | |
123 | createItems(-diff); |
|
123 | createItems(-diff); | |
124 |
|
124 | |||
125 | if (diff < 0) handleAxisUpdated(); |
|
125 | if (diff < 0) handleAxisUpdated(); | |
126 |
|
126 | |||
127 | if (m_animation) { |
|
127 | if (m_animation) { | |
128 | switch (presenter()->state()) { |
|
128 | switch (presenter()->state()) { | |
129 | case ChartPresenter::ZoomInState: |
|
129 | case ChartPresenter::ZoomInState: | |
130 | m_animation->setAnimationType(AxisAnimation::ZoomInAnimation); |
|
130 | m_animation->setAnimationType(AxisAnimation::ZoomInAnimation); | |
131 | m_animation->setAnimationPoint(presenter()->statePoint()); |
|
131 | m_animation->setAnimationPoint(presenter()->statePoint()); | |
132 | break; |
|
132 | break; | |
133 | case ChartPresenter::ZoomOutState: |
|
133 | case ChartPresenter::ZoomOutState: | |
134 | m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation); |
|
134 | m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation); | |
135 | m_animation->setAnimationPoint(presenter()->statePoint()); |
|
135 | m_animation->setAnimationPoint(presenter()->statePoint()); | |
136 | break; |
|
136 | break; | |
137 | case ChartPresenter::ScrollUpState: |
|
137 | case ChartPresenter::ScrollUpState: | |
138 | case ChartPresenter::ScrollLeftState: |
|
138 | case ChartPresenter::ScrollLeftState: | |
139 | m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation); |
|
139 | m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation); | |
140 | break; |
|
140 | break; | |
141 | case ChartPresenter::ScrollDownState: |
|
141 | case ChartPresenter::ScrollDownState: | |
142 | case ChartPresenter::ScrollRightState: |
|
142 | case ChartPresenter::ScrollRightState: | |
143 | m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation); |
|
143 | m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation); | |
144 | break; |
|
144 | break; | |
145 | case ChartPresenter::ShowState: |
|
145 | case ChartPresenter::ShowState: | |
146 | m_animation->setAnimationType(AxisAnimation::DefaultAnimation); |
|
146 | m_animation->setAnimationType(AxisAnimation::DefaultAnimation); | |
147 | break; |
|
147 | break; | |
148 | } |
|
148 | } | |
149 | m_animation->setValues(m_layoutVector, layout); |
|
149 | m_animation->setValues(m_layoutVector, layout); | |
150 | presenter()->startAnimation(m_animation); |
|
150 | presenter()->startAnimation(m_animation); | |
151 | } else { |
|
151 | } else { | |
152 | setLayout(layout); |
|
152 | setLayout(layout); | |
153 | updateGeometry(); |
|
153 | updateGeometry(); | |
154 | } |
|
154 | } | |
155 | } |
|
155 | } | |
156 |
|
156 | |||
157 | void ChartAxis::setArrowOpacity(qreal opacity) |
|
157 | void ChartAxis::setArrowOpacity(qreal opacity) | |
158 | { |
|
158 | { | |
159 | m_arrow->setOpacity(opacity); |
|
159 | m_arrow->setOpacity(opacity); | |
160 | } |
|
160 | } | |
161 |
|
161 | |||
162 | qreal ChartAxis::arrowOpacity() const |
|
162 | qreal ChartAxis::arrowOpacity() const | |
163 | { |
|
163 | { | |
164 | return m_arrow->opacity(); |
|
164 | return m_arrow->opacity(); | |
165 | } |
|
165 | } | |
166 |
|
166 | |||
167 | void ChartAxis::setArrowVisibility(bool visible) |
|
167 | void ChartAxis::setArrowVisibility(bool visible) | |
168 | { |
|
168 | { | |
169 | m_arrow->setOpacity(visible); |
|
169 | m_arrow->setOpacity(visible); | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | void ChartAxis::setGridOpacity(qreal opacity) |
|
172 | void ChartAxis::setGridOpacity(qreal opacity) | |
173 | { |
|
173 | { | |
174 | m_grid->setOpacity(opacity); |
|
174 | m_grid->setOpacity(opacity); | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | qreal ChartAxis::gridOpacity() const |
|
177 | qreal ChartAxis::gridOpacity() const | |
178 | { |
|
178 | { | |
179 | return m_grid->opacity(); |
|
179 | return m_grid->opacity(); | |
180 | } |
|
180 | } | |
181 |
|
181 | |||
182 | void ChartAxis::setGridVisibility(bool visible) |
|
182 | void ChartAxis::setGridVisibility(bool visible) | |
183 | { |
|
183 | { | |
184 | m_grid->setOpacity(visible); |
|
184 | m_grid->setOpacity(visible); | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | void ChartAxis::setLabelsOpacity(qreal opacity) |
|
187 | void ChartAxis::setLabelsOpacity(qreal opacity) | |
188 | { |
|
188 | { | |
189 | m_labels->setOpacity(opacity); |
|
189 | m_labels->setOpacity(opacity); | |
190 | } |
|
190 | } | |
191 |
|
191 | |||
192 | qreal ChartAxis::labelsOpacity() const |
|
192 | qreal ChartAxis::labelsOpacity() const | |
193 | { |
|
193 | { | |
194 | return m_labels->opacity(); |
|
194 | return m_labels->opacity(); | |
195 | } |
|
195 | } | |
196 |
|
196 | |||
197 | void ChartAxis::setLabelsVisibility(bool visible) |
|
197 | void ChartAxis::setLabelsVisibility(bool visible) | |
198 | { |
|
198 | { | |
199 | m_labels->setOpacity(visible); |
|
199 | m_labels->setOpacity(visible); | |
200 | } |
|
200 | } | |
201 |
|
201 | |||
202 | void ChartAxis::setShadesOpacity(qreal opacity) |
|
202 | void ChartAxis::setShadesOpacity(qreal opacity) | |
203 | { |
|
203 | { | |
204 | m_shades->setOpacity(opacity); |
|
204 | m_shades->setOpacity(opacity); | |
205 | } |
|
205 | } | |
206 |
|
206 | |||
207 | qreal ChartAxis::shadesOpacity() const |
|
207 | qreal ChartAxis::shadesOpacity() const | |
208 | { |
|
208 | { | |
209 | return m_shades->opacity(); |
|
209 | return m_shades->opacity(); | |
210 | } |
|
210 | } | |
211 |
|
211 | |||
212 | void ChartAxis::setShadesVisibility(bool visible) |
|
212 | void ChartAxis::setShadesVisibility(bool visible) | |
213 | { |
|
213 | { | |
214 | m_shades->setVisible(visible); |
|
214 | m_shades->setVisible(visible); | |
215 | } |
|
215 | } | |
216 |
|
216 | |||
217 | void ChartAxis::setLabelsAngle(int angle) |
|
217 | void ChartAxis::setLabelsAngle(int angle) | |
218 | { |
|
218 | { | |
219 | foreach (QGraphicsItem *item, m_labels->childItems()) |
|
219 | foreach (QGraphicsItem *item, m_labels->childItems()) | |
220 | item->setRotation(angle); |
|
220 | item->setRotation(angle); | |
221 |
|
221 | |||
222 | m_labelsAngle = angle; |
|
222 | m_labelsAngle = angle; | |
223 | } |
|
223 | } | |
224 |
|
224 | |||
225 | void ChartAxis::setLabelsPen(const QPen &pen) |
|
225 | void ChartAxis::setLabelsPen(const QPen &pen) | |
226 | { |
|
226 | { | |
227 | foreach (QGraphicsItem *item , m_labels->childItems()) |
|
227 | foreach (QGraphicsItem *item , m_labels->childItems()) | |
228 | static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen); |
|
228 | static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen); | |
229 | } |
|
229 | } | |
230 |
|
230 | |||
231 | void ChartAxis::setLabelsBrush(const QBrush &brush) |
|
231 | void ChartAxis::setLabelsBrush(const QBrush &brush) | |
232 | { |
|
232 | { | |
233 | foreach (QGraphicsItem *item , m_labels->childItems()) |
|
233 | foreach (QGraphicsItem *item , m_labels->childItems()) | |
234 | static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush); |
|
234 | static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush); | |
235 | } |
|
235 | } | |
236 |
|
236 | |||
237 | void ChartAxis::setLabelsFont(const QFont &font) |
|
237 | void ChartAxis::setLabelsFont(const QFont &font) | |
238 | { |
|
238 | { | |
239 | if (m_font != font) { |
|
239 | if (m_font != font) { | |
240 | m_font = font; |
|
240 | m_font = font; | |
241 | foreach (QGraphicsItem *item , m_labels->childItems()) |
|
241 | foreach (QGraphicsItem *item , m_labels->childItems()) | |
242 | static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font); |
|
242 | static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font); | |
243 | QGraphicsLayoutItem::updateGeometry(); |
|
243 | QGraphicsLayoutItem::updateGeometry(); | |
244 | presenter()->layout()->invalidate(); |
|
244 | presenter()->layout()->invalidate(); | |
245 | } |
|
245 | } | |
246 | } |
|
246 | } | |
247 |
|
247 | |||
248 | void ChartAxis::setShadesBrush(const QBrush &brush) |
|
248 | void ChartAxis::setShadesBrush(const QBrush &brush) | |
249 | { |
|
249 | { | |
250 | foreach (QGraphicsItem *item , m_shades->childItems()) |
|
250 | foreach (QGraphicsItem *item , m_shades->childItems()) | |
251 | static_cast<QGraphicsRectItem *>(item)->setBrush(brush); |
|
251 | static_cast<QGraphicsRectItem *>(item)->setBrush(brush); | |
252 | } |
|
252 | } | |
253 |
|
253 | |||
254 | void ChartAxis::setShadesPen(const QPen &pen) |
|
254 | void ChartAxis::setShadesPen(const QPen &pen) | |
255 | { |
|
255 | { | |
256 | foreach (QGraphicsItem *item , m_shades->childItems()) |
|
256 | foreach (QGraphicsItem *item , m_shades->childItems()) | |
257 | static_cast<QGraphicsRectItem *>(item)->setPen(pen); |
|
257 | static_cast<QGraphicsRectItem *>(item)->setPen(pen); | |
258 | } |
|
258 | } | |
259 |
|
259 | |||
260 | void ChartAxis::setArrowPen(const QPen &pen) |
|
260 | void ChartAxis::setArrowPen(const QPen &pen) | |
261 | { |
|
261 | { | |
262 | foreach (QGraphicsItem *item , m_arrow->childItems()) |
|
262 | foreach (QGraphicsItem *item , m_arrow->childItems()) | |
263 | static_cast<QGraphicsLineItem *>(item)->setPen(pen); |
|
263 | static_cast<QGraphicsLineItem *>(item)->setPen(pen); | |
264 | } |
|
264 | } | |
265 |
|
265 | |||
266 | void ChartAxis::setGridPen(const QPen &pen) |
|
266 | void ChartAxis::setGridPen(const QPen &pen) | |
267 | { |
|
267 | { | |
268 | foreach (QGraphicsItem *item , m_grid->childItems()) |
|
268 | foreach (QGraphicsItem *item , m_grid->childItems()) | |
269 | static_cast<QGraphicsLineItem *>(item)->setPen(pen); |
|
269 | static_cast<QGraphicsLineItem *>(item)->setPen(pen); | |
270 | } |
|
270 | } | |
271 |
|
271 | |||
272 | void ChartAxis::setLabelPadding(int padding) |
|
272 | void ChartAxis::setLabelPadding(int padding) | |
273 | { |
|
273 | { | |
274 | m_labelPadding = padding; |
|
274 | m_labelPadding = padding; | |
275 | } |
|
275 | } | |
276 |
|
276 | |||
277 | bool ChartAxis::isEmpty() |
|
277 | bool ChartAxis::isEmpty() | |
278 | { |
|
278 | { | |
279 | return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(m_min, m_max); |
|
279 | return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(m_min, m_max); | |
280 | } |
|
280 | } | |
281 |
|
281 | |||
282 | void ChartAxis::handleDomainUpdated() |
|
282 | void ChartAxis::handleDomainUpdated() | |
283 | { |
|
283 | { | |
284 | Domain *domain = qobject_cast<Domain *>(sender()); |
|
284 | Domain *domain = qobject_cast<Domain *>(sender()); | |
285 | qreal min(0); |
|
285 | qreal min(0); | |
286 | qreal max(0); |
|
286 | qreal max(0); | |
287 |
|
287 | |||
288 | if (m_chartAxis->orientation() == Qt::Horizontal) { |
|
288 | if (m_chartAxis->orientation() == Qt::Horizontal) { | |
289 | min = domain->minX(); |
|
289 | min = domain->minX(); | |
290 | max = domain->maxX(); |
|
290 | max = domain->maxX(); | |
291 | } else if (m_chartAxis->orientation() == Qt::Vertical) { |
|
291 | } else if (m_chartAxis->orientation() == Qt::Vertical) { | |
292 | min = domain->minY(); |
|
292 | min = domain->minY(); | |
293 | max = domain->maxY(); |
|
293 | max = domain->maxY(); | |
294 | } |
|
294 | } | |
295 |
|
295 | |||
296 | if (!qFuzzyCompare(m_min, min) || !qFuzzyCompare(m_max, max)) { |
|
296 | if (!qFuzzyCompare(m_min, min) || !qFuzzyCompare(m_max, max)) { | |
297 | m_min = min; |
|
297 | m_min = min; | |
298 | m_max = max; |
|
298 | m_max = max; | |
299 |
|
299 | |||
300 | if (!isEmpty()) { |
|
300 | if (!isEmpty()) { | |
301 |
|
301 | |||
302 | QVector<qreal> layout = calculateLayout(); |
|
302 | QVector<qreal> layout = calculateLayout(); | |
303 | updateLayout(layout); |
|
303 | updateLayout(layout); | |
304 | QSizeF before = effectiveSizeHint(Qt::PreferredSize); |
|
304 | QSizeF before = effectiveSizeHint(Qt::PreferredSize); | |
305 | QSizeF after = sizeHint(Qt::PreferredSize); |
|
305 | QSizeF after = sizeHint(Qt::PreferredSize); | |
306 |
|
306 | |||
307 | if (before != after) { |
|
307 | if (before != after) { | |
308 | QGraphicsLayoutItem::updateGeometry(); |
|
308 | QGraphicsLayoutItem::updateGeometry(); | |
309 | //we don't want to call invalidate on layout, since it will change minimum size of component, |
|
309 | //we don't want to call invalidate on layout, since it will change minimum size of component, | |
310 | //which we would like to avoid since it causes nasty flips when scrolling or zooming, |
|
310 | //which we would like to avoid since it causes nasty flips when scrolling or zooming, | |
311 | //instead recalculate layout and use plotArea for extra space. |
|
311 | //instead recalculate layout and use plotArea for extra space. | |
312 | presenter()->layout()->setGeometry(presenter()->layout()->geometry()); |
|
312 | presenter()->layout()->setGeometry(presenter()->layout()->geometry()); | |
313 | } |
|
313 | } | |
314 | } |
|
314 | } | |
315 | } |
|
315 | } | |
316 | } |
|
316 | } | |
317 |
|
317 | |||
318 | void ChartAxis::handleAxisUpdated() |
|
318 | void ChartAxis::handleAxisUpdated() | |
319 | { |
|
319 | { | |
320 | if (isEmpty()) |
|
320 | if (isEmpty()) | |
321 | return; |
|
321 | return; | |
322 |
|
322 | |||
323 | bool visible = m_chartAxis->isVisible(); |
|
323 | bool visible = m_chartAxis->isVisible(); | |
324 |
|
324 | |||
325 | //TODO: split this into separate signal/slots ? |
|
325 | //TODO: split this into separate signal/slots ? | |
326 | setArrowVisibility(visible && m_chartAxis->isLineVisible()); |
|
326 | setArrowVisibility(visible && m_chartAxis->isLineVisible()); | |
327 | setGridVisibility(visible && m_chartAxis->isGridLineVisible()); |
|
327 | setGridVisibility(visible && m_chartAxis->isGridLineVisible()); | |
328 | setLabelsVisibility(visible && m_chartAxis->labelsVisible()); |
|
328 | setLabelsVisibility(visible && m_chartAxis->labelsVisible()); | |
329 | setShadesVisibility(visible && m_chartAxis->shadesVisible()); |
|
329 | setShadesVisibility(visible && m_chartAxis->shadesVisible()); | |
330 | setLabelsAngle(m_chartAxis->labelsAngle()); |
|
330 | setLabelsAngle(m_chartAxis->labelsAngle()); | |
331 | setArrowPen(m_chartAxis->linePen()); |
|
331 | setArrowPen(m_chartAxis->linePen()); | |
332 | setLabelsPen(m_chartAxis->labelsPen()); |
|
332 | setLabelsPen(m_chartAxis->labelsPen()); | |
333 | setLabelsBrush(m_chartAxis->labelsBrush()); |
|
333 | setLabelsBrush(m_chartAxis->labelsBrush()); | |
334 | setLabelsFont(m_chartAxis->labelsFont()); |
|
334 | setLabelsFont(m_chartAxis->labelsFont()); | |
335 | setGridPen(m_chartAxis->gridLinePen()); |
|
335 | setGridPen(m_chartAxis->gridLinePen()); | |
336 | setShadesPen(m_chartAxis->shadesPen()); |
|
336 | setShadesPen(m_chartAxis->shadesPen()); | |
337 | setShadesBrush(m_chartAxis->shadesBrush()); |
|
337 | setShadesBrush(m_chartAxis->shadesBrush()); | |
338 | setTitleText(m_chartAxis->title()); |
|
338 | setTitleText(m_chartAxis->title()); | |
339 | setTitleFont(m_chartAxis->titleFont()); |
|
339 | setTitleFont(m_chartAxis->titleFont()); | |
340 | setTitlePen(m_chartAxis->titlePen()); |
|
340 | setTitlePen(m_chartAxis->titlePen()); | |
341 | setTitleBrush(m_chartAxis->titleBrush()); |
|
341 | setTitleBrush(m_chartAxis->titleBrush()); | |
342 | } |
|
342 | } | |
343 |
|
343 | |||
344 | void ChartAxis::setTitleText(const QString &title) |
|
344 | void ChartAxis::setTitleText(const QString &title) | |
345 | { |
|
345 | { | |
346 | if (m_titleText != title) { |
|
346 | if (m_titleText != title) { | |
347 | m_titleText = title; |
|
347 | m_titleText = title; | |
348 | QGraphicsLayoutItem::updateGeometry(); |
|
348 | QGraphicsLayoutItem::updateGeometry(); | |
349 | presenter()->layout()->invalidate(); |
|
349 | presenter()->layout()->invalidate(); | |
350 | } |
|
350 | } | |
351 | } |
|
351 | } | |
352 |
|
352 | |||
353 | void ChartAxis::setTitlePen(const QPen &pen) |
|
353 | void ChartAxis::setTitlePen(const QPen &pen) | |
354 | { |
|
354 | { | |
355 | m_title->setPen(pen); |
|
355 | m_title->setPen(pen); | |
356 | } |
|
356 | } | |
357 |
|
357 | |||
358 | void ChartAxis::setTitleBrush(const QBrush &brush) |
|
358 | void ChartAxis::setTitleBrush(const QBrush &brush) | |
359 | { |
|
359 | { | |
360 | m_title->setBrush(brush); |
|
360 | m_title->setBrush(brush); | |
361 | } |
|
361 | } | |
362 |
|
362 | |||
363 | void ChartAxis::setTitleFont(const QFont &font) |
|
363 | void ChartAxis::setTitleFont(const QFont &font) | |
364 | { |
|
364 | { | |
365 | if(m_title->font() != font){ |
|
365 | if(m_title->font() != font){ | |
366 | m_title->setFont(font); |
|
366 | m_title->setFont(font); | |
367 | QGraphicsLayoutItem::updateGeometry(); |
|
367 | QGraphicsLayoutItem::updateGeometry(); | |
368 | presenter()->layout()->invalidate(); |
|
368 | presenter()->layout()->invalidate(); | |
369 | } |
|
369 | } | |
370 | } |
|
370 | } | |
371 |
|
371 | |||
372 | QFont ChartAxis::titleFont() const |
|
372 | QFont ChartAxis::titleFont() const | |
373 | { |
|
373 | { | |
374 | return m_title->font(); |
|
374 | return m_title->font(); | |
375 | } |
|
375 | } | |
376 |
|
376 | |||
377 | void ChartAxis::hide() |
|
377 | void ChartAxis::hide() | |
378 | { |
|
378 | { | |
379 | setArrowVisibility(false); |
|
379 | setArrowVisibility(false); | |
380 | setGridVisibility(false); |
|
380 | setGridVisibility(false); | |
381 | setLabelsVisibility(false); |
|
381 | setLabelsVisibility(false); | |
382 | setShadesVisibility(false); |
|
382 | setShadesVisibility(false); | |
383 | } |
|
383 | } | |
384 |
|
384 | |||
385 | void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) |
|
385 | void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) | |
386 | { |
|
386 | { | |
387 | m_gridRect = grid; |
|
387 | m_gridRect = grid; | |
388 | m_axisRect = axis; |
|
388 | m_axisRect = axis; | |
389 |
|
389 | |||
390 | if (isEmpty()) |
|
390 | if (isEmpty()) | |
391 | return; |
|
391 | return; | |
392 |
|
392 | |||
393 | QVector<qreal> layout = calculateLayout(); |
|
393 | QVector<qreal> layout = calculateLayout(); | |
394 | updateLayout(layout); |
|
394 | updateLayout(layout); | |
395 |
|
395 | |||
396 | } |
|
396 | } | |
397 |
|
397 | |||
398 | void ChartAxis::axisSelected() |
|
398 | void ChartAxis::axisSelected() | |
399 | { |
|
399 | { | |
400 | //TODO: axis clicked; |
|
400 | //TODO: axis clicked; | |
401 | } |
|
401 | } | |
402 |
|
402 | |||
403 | Qt::Orientation ChartAxis::orientation() const |
|
403 | Qt::Orientation ChartAxis::orientation() const | |
404 | { |
|
404 | { | |
405 | return m_chartAxis->orientation(); |
|
405 | return m_chartAxis->orientation(); | |
406 | } |
|
406 | } | |
407 |
|
407 | |||
408 | Qt::Alignment ChartAxis::alignment() const |
|
408 | Qt::Alignment ChartAxis::alignment() const | |
409 | { |
|
409 | { | |
410 | return m_chartAxis->alignment(); |
|
410 | return m_chartAxis->alignment(); | |
411 | } |
|
411 | } | |
412 |
|
412 | |||
413 | bool ChartAxis::isVisible() |
|
413 | bool ChartAxis::isVisible() | |
414 | { |
|
414 | { | |
415 | return m_chartAxis->isVisible(); |
|
415 | return m_chartAxis->isVisible(); | |
416 | } |
|
416 | } | |
417 |
|
417 | |||
418 | void ChartAxis::setLabels(const QStringList &labels) |
|
418 | void ChartAxis::setLabels(const QStringList &labels) | |
419 | { |
|
419 | { | |
420 | m_labelsList = labels; |
|
420 | m_labelsList = labels; | |
421 | } |
|
421 | } | |
422 |
|
422 | |||
423 | QStringList ChartAxis::createValueLabels(int ticks) const |
|
423 | QStringList ChartAxis::createValueLabels(int ticks) const | |
424 | { |
|
424 | { | |
425 | Q_ASSERT(m_max > m_min); |
|
425 | Q_ASSERT(m_max > m_min); | |
426 | Q_ASSERT(ticks > 1); |
|
426 | Q_ASSERT(ticks > 1); | |
427 |
|
427 | |||
428 | QStringList labels; |
|
428 | QStringList labels; | |
429 |
|
429 | |||
430 | int n = qMax(int(-qFloor(log10((m_max - m_min) / (ticks - 1)))), 0); |
|
430 | int n = qMax(int(-qFloor(log10((m_max - m_min) / (ticks - 1)))), 0); | |
431 | n++; |
|
431 | n++; | |
432 |
|
432 | |||
433 | QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis); |
|
433 | QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis); | |
434 |
|
434 | |||
435 | QString format = axis->labelFormat(); |
|
435 | QString format = axis->labelFormat(); | |
436 |
|
436 | |||
437 | if (format.isNull()) { |
|
437 | if (format.isNull()) { | |
438 | for (int i = 0; i < ticks; i++) { |
|
438 | for (int i = 0; i < ticks; i++) { | |
439 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); |
|
439 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); | |
440 | labels << QString::number(value, 'f', n); |
|
440 | labels << QString::number(value, 'f', n); | |
441 | } |
|
441 | } | |
442 | } else { |
|
442 | } else { | |
443 | QByteArray array = format.toLatin1(); |
|
443 | QByteArray array = format.toLatin1(); | |
444 | for (int i = 0; i < ticks; i++) { |
|
444 | for (int i = 0; i < ticks; i++) { | |
445 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); |
|
445 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); | |
446 | labels << QString().sprintf(array, value); |
|
446 | if (format.contains("d") | |
|
447 | || format.contains("i") | |||
|
448 | || format.contains("c")) | |||
|
449 | ||||
|
450 | labels << QString().sprintf(array, (qint64)value); | |||
|
451 | else if (format.contains("u") | |||
|
452 | || format.contains("o") | |||
|
453 | || format.contains("x", Qt::CaseInsensitive)) | |||
|
454 | labels << QString().sprintf(array, (quint64)value); | |||
|
455 | else | |||
|
456 | labels << QString().sprintf(array, value); | |||
447 | } |
|
457 | } | |
448 | } |
|
458 | } | |
449 |
|
459 | |||
450 | return labels; |
|
460 | return labels; | |
451 | } |
|
461 | } | |
452 |
|
462 | |||
453 | QStringList ChartAxis::createDateTimeLabels(const QString &format, int ticks) const |
|
463 | QStringList ChartAxis::createDateTimeLabels(const QString &format, int ticks) const | |
454 | { |
|
464 | { | |
455 | Q_ASSERT(m_max > m_min); |
|
465 | Q_ASSERT(m_max > m_min); | |
456 | Q_ASSERT(ticks > 1); |
|
466 | Q_ASSERT(ticks > 1); | |
457 | QStringList labels; |
|
467 | QStringList labels; | |
458 | int n = qMax(int(-floor(log10((m_max - m_min) / (ticks - 1)))), 0); |
|
468 | int n = qMax(int(-floor(log10((m_max - m_min) / (ticks - 1)))), 0); | |
459 | n++; |
|
469 | n++; | |
460 | for (int i = 0; i < ticks; i++) { |
|
470 | for (int i = 0; i < ticks; i++) { | |
461 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); |
|
471 | qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); | |
462 | labels << QDateTime::fromMSecsSinceEpoch(value).toString(format); |
|
472 | labels << QDateTime::fromMSecsSinceEpoch(value).toString(format); | |
463 | } |
|
473 | } | |
464 | return labels; |
|
474 | return labels; | |
465 | } |
|
475 | } | |
466 |
|
476 | |||
467 | QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
477 | QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
468 | { |
|
478 | { | |
469 | Q_UNUSED(which); |
|
479 | Q_UNUSED(which); | |
470 | Q_UNUSED(constraint); |
|
480 | Q_UNUSED(constraint); | |
471 | return QSizeF(); |
|
481 | return QSizeF(); | |
472 | } |
|
482 | } | |
473 |
|
483 | |||
474 | #include "moc_chartaxis_p.cpp" |
|
484 | #include "moc_chartaxis_p.cpp" | |
475 |
|
485 | |||
476 | QTCOMMERCIALCHART_END_NAMESPACE |
|
486 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,420 +1,422 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "qvalueaxis.h" |
|
21 | #include "qvalueaxis.h" | |
22 | #include "qvalueaxis_p.h" |
|
22 | #include "qvalueaxis_p.h" | |
23 | #include "chartvalueaxisx_p.h" |
|
23 | #include "chartvalueaxisx_p.h" | |
24 | #include "chartvalueaxisy_p.h" |
|
24 | #include "chartvalueaxisy_p.h" | |
25 | #include "domain_p.h" |
|
25 | #include "domain_p.h" | |
26 | #include "chartdataset_p.h" |
|
26 | #include "chartdataset_p.h" | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
31 | /*! |
|
31 | /*! | |
32 | \class QValueAxis |
|
32 | \class QValueAxis | |
33 | \brief The QValueAxis class is used for manipulating chart's axis. |
|
33 | \brief The QValueAxis class is used for manipulating chart's axis. | |
34 | \mainclass |
|
34 | \mainclass | |
35 |
|
35 | |||
36 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
36 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. | |
37 | Values of axis are drawn to position of ticks. |
|
37 | Values of axis are drawn to position of ticks. | |
38 |
|
38 | |||
39 | Example code on how to use QValueAxis. |
|
39 | Example code on how to use QValueAxis. | |
40 | \code |
|
40 | \code | |
41 | QChartView *chartView = new QChartView; |
|
41 | QChartView *chartView = new QChartView; | |
42 | QLineSeries *series = new QLineSeries; |
|
42 | QLineSeries *series = new QLineSeries; | |
43 | // ... |
|
43 | // ... | |
44 | chartView->chart()->addSeries(series); |
|
44 | chartView->chart()->addSeries(series); | |
45 |
|
45 | |||
46 | QValueAxis *axisX = new QValueAxis; |
|
46 | QValueAxis *axisX = new QValueAxis; | |
47 | axisX->setRange(10, 20.5); |
|
47 | axisX->setRange(10, 20.5); | |
48 | axisX->setTickCount(10); |
|
48 | axisX->setTickCount(10); | |
49 | axisX->setLabelFormat("%.2f"); |
|
49 | axisX->setLabelFormat("%.2f"); | |
50 | chartView->chart()->setAxisX(axisX, series); |
|
50 | chartView->chart()->setAxisX(axisX, series); | |
51 | \endcode |
|
51 | \endcode | |
52 | */ |
|
52 | */ | |
53 |
|
53 | |||
54 | /*! |
|
54 | /*! | |
55 | \qmlclass ValueAxis QValueAxis |
|
55 | \qmlclass ValueAxis QValueAxis | |
56 | \inherits AbstractAxis |
|
56 | \inherits AbstractAxis | |
57 | \brief The ValueAxis element is used for manipulating chart's axes |
|
57 | \brief The ValueAxis element is used for manipulating chart's axes | |
58 |
|
58 | |||
59 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
59 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. | |
60 | Values of axis are drawn to position of ticks |
|
60 | Values of axis are drawn to position of ticks | |
61 |
|
61 | |||
62 | To access Axes you can use ChartView API. For example: |
|
62 | To access Axes you can use ChartView API. For example: | |
63 | \code |
|
63 | \code | |
64 | ChartView { |
|
64 | ChartView { | |
65 | ValueAxis { |
|
65 | ValueAxis { | |
66 | id: xAxis |
|
66 | id: xAxis | |
67 | min: 0 |
|
67 | min: 0 | |
68 | max: 10 |
|
68 | max: 10 | |
69 | } |
|
69 | } | |
70 | // Add a few series... |
|
70 | // Add a few series... | |
71 | } |
|
71 | } | |
72 | \endcode |
|
72 | \endcode | |
73 | */ |
|
73 | */ | |
74 |
|
74 | |||
75 | /*! |
|
75 | /*! | |
76 | \property QValueAxis::min |
|
76 | \property QValueAxis::min | |
77 | Defines the minimum value on the axis. |
|
77 | Defines the minimum value on the axis. | |
78 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
78 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. | |
79 | */ |
|
79 | */ | |
80 | /*! |
|
80 | /*! | |
81 | \qmlproperty real ValueAxis::min |
|
81 | \qmlproperty real ValueAxis::min | |
82 | Defines the minimum value on the axis. |
|
82 | Defines the minimum value on the axis. | |
83 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
83 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. | |
84 | */ |
|
84 | */ | |
85 |
|
85 | |||
86 | /*! |
|
86 | /*! | |
87 | \property QValueAxis::max |
|
87 | \property QValueAxis::max | |
88 | Defines the maximum value on the axis. |
|
88 | Defines the maximum value on the axis. | |
89 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
89 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. | |
90 | */ |
|
90 | */ | |
91 | /*! |
|
91 | /*! | |
92 | \qmlproperty real ValueAxis::max |
|
92 | \qmlproperty real ValueAxis::max | |
93 | Defines the maximum value on the axis. |
|
93 | Defines the maximum value on the axis. | |
94 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
94 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. | |
95 | */ |
|
95 | */ | |
96 |
|
96 | |||
97 | /*! |
|
97 | /*! | |
98 | \property QValueAxis::labelFormat |
|
98 | \property QValueAxis::labelFormat | |
99 | Defines the label format for the axis. |
|
99 | Defines the label format for the axis. | |
100 | See QString::sprintf() for the details. |
|
100 | Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c | |
|
101 | See QString::sprintf() for additional details. | |||
101 | */ |
|
102 | */ | |
102 | /*! |
|
103 | /*! | |
103 | \qmlproperty real ValueAxis::labelFormat |
|
104 | \qmlproperty real ValueAxis::labelFormat | |
104 | Defines the label format for the axis. |
|
105 | Defines the label format for the axis. | |
105 | See QString::sprintf() for the details. |
|
106 | Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c | |
|
107 | See QString::sprintf() for additional details. | |||
106 | */ |
|
108 | */ | |
107 |
|
109 | |||
108 | /*! |
|
110 | /*! | |
109 | \fn void QValueAxis::minChanged(qreal min) |
|
111 | \fn void QValueAxis::minChanged(qreal min) | |
110 | Axis emits signal when \a min of axis has changed. |
|
112 | Axis emits signal when \a min of axis has changed. | |
111 | */ |
|
113 | */ | |
112 | /*! |
|
114 | /*! | |
113 | \qmlsignal ValueAxis::onMinChanged(real min) |
|
115 | \qmlsignal ValueAxis::onMinChanged(real min) | |
114 | Axis emits signal when \a min of axis has changed. |
|
116 | Axis emits signal when \a min of axis has changed. | |
115 | */ |
|
117 | */ | |
116 |
|
118 | |||
117 | /*! |
|
119 | /*! | |
118 | \fn void QValueAxis::maxChanged(qreal max) |
|
120 | \fn void QValueAxis::maxChanged(qreal max) | |
119 | Axis emits signal when \a max of axis has changed. |
|
121 | Axis emits signal when \a max of axis has changed. | |
120 | */ |
|
122 | */ | |
121 | /*! |
|
123 | /*! | |
122 | \qmlsignal ValueAxis::onMaxChanged(real max) |
|
124 | \qmlsignal ValueAxis::onMaxChanged(real max) | |
123 | Axis emits signal when \a max of axis has changed. |
|
125 | Axis emits signal when \a max of axis has changed. | |
124 | */ |
|
126 | */ | |
125 |
|
127 | |||
126 | /*! |
|
128 | /*! | |
127 | \fn void QValueAxis::rangeChanged(qreal min, qreal max) |
|
129 | \fn void QValueAxis::rangeChanged(qreal min, qreal max) | |
128 | Axis emits signal when \a min or \a max of axis has changed. |
|
130 | Axis emits signal when \a min or \a max of axis has changed. | |
129 | */ |
|
131 | */ | |
130 |
|
132 | |||
131 | /*! |
|
133 | /*! | |
132 | \property QValueAxis::tickCount |
|
134 | \property QValueAxis::tickCount | |
133 | The number of tick marks for the axis. |
|
135 | The number of tick marks for the axis. | |
134 | */ |
|
136 | */ | |
135 |
|
137 | |||
136 | /*! |
|
138 | /*! | |
137 | \qmlproperty int ValueAxis::tickCount |
|
139 | \qmlproperty int ValueAxis::tickCount | |
138 | The number of tick marks for the axis. |
|
140 | The number of tick marks for the axis. | |
139 | */ |
|
141 | */ | |
140 |
|
142 | |||
141 | /*! |
|
143 | /*! | |
142 | \property QValueAxis::niceNumbersEnabled |
|
144 | \property QValueAxis::niceNumbersEnabled | |
143 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
145 | Whether the nice numbers algorithm is enabled or not for the axis. | |
144 | */ |
|
146 | */ | |
145 |
|
147 | |||
146 | /*! |
|
148 | /*! | |
147 | \qmlproperty bool ValueAxis::niceNumbersEnabled |
|
149 | \qmlproperty bool ValueAxis::niceNumbersEnabled | |
148 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
150 | Whether the nice numbers algorithm is enabled or not for the axis. | |
149 | */ |
|
151 | */ | |
150 |
|
152 | |||
151 | /*! |
|
153 | /*! | |
152 | Constructs an axis object which is a child of \a parent. |
|
154 | Constructs an axis object which is a child of \a parent. | |
153 | */ |
|
155 | */ | |
154 | QValueAxis::QValueAxis(QObject *parent) : |
|
156 | QValueAxis::QValueAxis(QObject *parent) : | |
155 | QAbstractAxis(*new QValueAxisPrivate(this), parent) |
|
157 | QAbstractAxis(*new QValueAxisPrivate(this), parent) | |
156 | { |
|
158 | { | |
157 |
|
159 | |||
158 | } |
|
160 | } | |
159 |
|
161 | |||
160 | /*! |
|
162 | /*! | |
161 | \internal |
|
163 | \internal | |
162 | */ |
|
164 | */ | |
163 | QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) |
|
165 | QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) | |
164 | : QAbstractAxis(d, parent) |
|
166 | : QAbstractAxis(d, parent) | |
165 | { |
|
167 | { | |
166 |
|
168 | |||
167 | } |
|
169 | } | |
168 |
|
170 | |||
169 | /*! |
|
171 | /*! | |
170 | Destroys the object |
|
172 | Destroys the object | |
171 | */ |
|
173 | */ | |
172 | QValueAxis::~QValueAxis() |
|
174 | QValueAxis::~QValueAxis() | |
173 | { |
|
175 | { | |
174 | Q_D(QValueAxis); |
|
176 | Q_D(QValueAxis); | |
175 | if (d->m_dataset) |
|
177 | if (d->m_dataset) | |
176 | d->m_dataset->removeAxis(this); |
|
178 | d->m_dataset->removeAxis(this); | |
177 | } |
|
179 | } | |
178 |
|
180 | |||
179 | void QValueAxis::setMin(qreal min) |
|
181 | void QValueAxis::setMin(qreal min) | |
180 | { |
|
182 | { | |
181 | Q_D(QValueAxis); |
|
183 | Q_D(QValueAxis); | |
182 | setRange(min, qMax(d->m_max, min)); |
|
184 | setRange(min, qMax(d->m_max, min)); | |
183 | } |
|
185 | } | |
184 |
|
186 | |||
185 | qreal QValueAxis::min() const |
|
187 | qreal QValueAxis::min() const | |
186 | { |
|
188 | { | |
187 | Q_D(const QValueAxis); |
|
189 | Q_D(const QValueAxis); | |
188 | return d->m_min; |
|
190 | return d->m_min; | |
189 | } |
|
191 | } | |
190 |
|
192 | |||
191 | void QValueAxis::setMax(qreal max) |
|
193 | void QValueAxis::setMax(qreal max) | |
192 | { |
|
194 | { | |
193 | Q_D(QValueAxis); |
|
195 | Q_D(QValueAxis); | |
194 | setRange(qMin(d->m_min, max), max); |
|
196 | setRange(qMin(d->m_min, max), max); | |
195 | } |
|
197 | } | |
196 |
|
198 | |||
197 | qreal QValueAxis::max() const |
|
199 | qreal QValueAxis::max() const | |
198 | { |
|
200 | { | |
199 | Q_D(const QValueAxis); |
|
201 | Q_D(const QValueAxis); | |
200 | return d->m_max; |
|
202 | return d->m_max; | |
201 | } |
|
203 | } | |
202 |
|
204 | |||
203 | /*! |
|
205 | /*! | |
204 | Sets range from \a min to \a max on the axis. |
|
206 | Sets range from \a min to \a max on the axis. | |
205 | If min is greater than max then this function returns without making any changes. |
|
207 | If min is greater than max then this function returns without making any changes. | |
206 | */ |
|
208 | */ | |
207 | void QValueAxis::setRange(qreal min, qreal max) |
|
209 | void QValueAxis::setRange(qreal min, qreal max) | |
208 | { |
|
210 | { | |
209 | Q_D(QValueAxis); |
|
211 | Q_D(QValueAxis); | |
210 | bool changed = false; |
|
212 | bool changed = false; | |
211 |
|
213 | |||
212 | if (min > max) |
|
214 | if (min > max) | |
213 | return; |
|
215 | return; | |
214 |
|
216 | |||
215 | if (d->m_niceNumbers) { |
|
217 | if (d->m_niceNumbers) { | |
216 | int ticks = d->m_tickCount; |
|
218 | int ticks = d->m_tickCount; | |
217 | d->looseNiceNumbers(min, max, ticks); |
|
219 | d->looseNiceNumbers(min, max, ticks); | |
218 | if (ticks != d->m_tickCount) |
|
220 | if (ticks != d->m_tickCount) | |
219 | setTickCount(ticks); |
|
221 | setTickCount(ticks); | |
220 | } |
|
222 | } | |
221 |
|
223 | |||
222 | if (!qFuzzyCompare(d->m_min, min)) { |
|
224 | if (!qFuzzyCompare(d->m_min, min)) { | |
223 | d->m_min = min; |
|
225 | d->m_min = min; | |
224 | changed = true; |
|
226 | changed = true; | |
225 | emit minChanged(min); |
|
227 | emit minChanged(min); | |
226 | } |
|
228 | } | |
227 |
|
229 | |||
228 | if (!qFuzzyCompare(d->m_max, max)) { |
|
230 | if (!qFuzzyCompare(d->m_max, max)) { | |
229 | d->m_max = max; |
|
231 | d->m_max = max; | |
230 | changed = true; |
|
232 | changed = true; | |
231 | emit maxChanged(max); |
|
233 | emit maxChanged(max); | |
232 | } |
|
234 | } | |
233 |
|
235 | |||
234 | if (changed) { |
|
236 | if (changed) { | |
235 | emit rangeChanged(min, max); |
|
237 | emit rangeChanged(min, max); | |
236 | d->emitUpdated(); |
|
238 | d->emitUpdated(); | |
237 | } |
|
239 | } | |
238 | } |
|
240 | } | |
239 |
|
241 | |||
240 | /*! |
|
242 | /*! | |
241 | Sets \a count for ticks on the axis. |
|
243 | Sets \a count for ticks on the axis. | |
242 | */ |
|
244 | */ | |
243 | void QValueAxis::setTickCount(int count) |
|
245 | void QValueAxis::setTickCount(int count) | |
244 | { |
|
246 | { | |
245 | Q_D(QValueAxis); |
|
247 | Q_D(QValueAxis); | |
246 | if (d->m_tickCount != count && count >= 2) { |
|
248 | if (d->m_tickCount != count && count >= 2) { | |
247 | d->m_tickCount = count; |
|
249 | d->m_tickCount = count; | |
248 | d->emitUpdated(); |
|
250 | d->emitUpdated(); | |
249 | } |
|
251 | } | |
250 | } |
|
252 | } | |
251 |
|
253 | |||
252 | /*! |
|
254 | /*! | |
253 | \fn int QValueAxis::tickCount() const |
|
255 | \fn int QValueAxis::tickCount() const | |
254 | Return number of ticks on the axis |
|
256 | Return number of ticks on the axis | |
255 | */ |
|
257 | */ | |
256 | int QValueAxis::tickCount() const |
|
258 | int QValueAxis::tickCount() const | |
257 | { |
|
259 | { | |
258 | Q_D(const QValueAxis); |
|
260 | Q_D(const QValueAxis); | |
259 | return d->m_tickCount; |
|
261 | return d->m_tickCount; | |
260 | } |
|
262 | } | |
261 |
|
263 | |||
262 | void QValueAxis::setNiceNumbersEnabled(bool enable) |
|
264 | void QValueAxis::setNiceNumbersEnabled(bool enable) | |
263 | { |
|
265 | { | |
264 | Q_D(QValueAxis); |
|
266 | Q_D(QValueAxis); | |
265 | if (d->m_niceNumbers != enable) { |
|
267 | if (d->m_niceNumbers != enable) { | |
266 | d->m_niceNumbers = enable; |
|
268 | d->m_niceNumbers = enable; | |
267 | if (enable && !qFuzzyCompare(d->m_max, d->m_min)) |
|
269 | if (enable && !qFuzzyCompare(d->m_max, d->m_min)) | |
268 | setRange(d->m_min, d->m_max); |
|
270 | setRange(d->m_min, d->m_max); | |
269 | } |
|
271 | } | |
270 | } |
|
272 | } | |
271 |
|
273 | |||
272 | bool QValueAxis::niceNumbersEnabled() const |
|
274 | bool QValueAxis::niceNumbersEnabled() const | |
273 | { |
|
275 | { | |
274 | Q_D(const QValueAxis); |
|
276 | Q_D(const QValueAxis); | |
275 | return d->m_niceNumbers; |
|
277 | return d->m_niceNumbers; | |
276 | } |
|
278 | } | |
277 |
|
279 | |||
278 | void QValueAxis::setLabelFormat(const QString &format) |
|
280 | void QValueAxis::setLabelFormat(const QString &format) | |
279 | { |
|
281 | { | |
280 | Q_D(QValueAxis); |
|
282 | Q_D(QValueAxis); | |
281 | d->m_format = format; |
|
283 | d->m_format = format; | |
282 | } |
|
284 | } | |
283 |
|
285 | |||
284 | QString QValueAxis::labelFormat() const |
|
286 | QString QValueAxis::labelFormat() const | |
285 | { |
|
287 | { | |
286 | Q_D(const QValueAxis); |
|
288 | Q_D(const QValueAxis); | |
287 | return d->m_format; |
|
289 | return d->m_format; | |
288 | } |
|
290 | } | |
289 |
|
291 | |||
290 | /*! |
|
292 | /*! | |
291 | Returns the type of the axis |
|
293 | Returns the type of the axis | |
292 | */ |
|
294 | */ | |
293 | QAbstractAxis::AxisType QValueAxis::type() const |
|
295 | QAbstractAxis::AxisType QValueAxis::type() const | |
294 | { |
|
296 | { | |
295 | return AxisTypeValue; |
|
297 | return AxisTypeValue; | |
296 | } |
|
298 | } | |
297 |
|
299 | |||
298 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
300 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
299 |
|
301 | |||
300 | QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q) |
|
302 | QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q) | |
301 | : QAbstractAxisPrivate(q), |
|
303 | : QAbstractAxisPrivate(q), | |
302 | m_min(0), |
|
304 | m_min(0), | |
303 | m_max(0), |
|
305 | m_max(0), | |
304 | m_tickCount(5), |
|
306 | m_tickCount(5), | |
305 | m_niceNumbers(false), |
|
307 | m_niceNumbers(false), | |
306 | m_format(QString::null) |
|
308 | m_format(QString::null) | |
307 | { |
|
309 | { | |
308 |
|
310 | |||
309 | } |
|
311 | } | |
310 |
|
312 | |||
311 | QValueAxisPrivate::~QValueAxisPrivate() |
|
313 | QValueAxisPrivate::~QValueAxisPrivate() | |
312 | { |
|
314 | { | |
313 |
|
315 | |||
314 | } |
|
316 | } | |
315 |
|
317 | |||
316 | void QValueAxisPrivate::handleDomainUpdated() |
|
318 | void QValueAxisPrivate::handleDomainUpdated() | |
317 | { |
|
319 | { | |
318 | Q_Q(QValueAxis); |
|
320 | Q_Q(QValueAxis); | |
319 | Domain *domain = qobject_cast<Domain *>(sender()); |
|
321 | Domain *domain = qobject_cast<Domain *>(sender()); | |
320 | Q_ASSERT(domain); |
|
322 | Q_ASSERT(domain); | |
321 |
|
323 | |||
322 | if (orientation() == Qt::Horizontal) |
|
324 | if (orientation() == Qt::Horizontal) | |
323 | q->setRange(domain->minX(), domain->maxX()); |
|
325 | q->setRange(domain->minX(), domain->maxX()); | |
324 | else if (orientation() == Qt::Vertical) |
|
326 | else if (orientation() == Qt::Vertical) | |
325 | q->setRange(domain->minY(), domain->maxY()); |
|
327 | q->setRange(domain->minY(), domain->maxY()); | |
326 | } |
|
328 | } | |
327 |
|
329 | |||
328 |
|
330 | |||
329 | void QValueAxisPrivate::setMin(const QVariant &min) |
|
331 | void QValueAxisPrivate::setMin(const QVariant &min) | |
330 | { |
|
332 | { | |
331 | Q_Q(QValueAxis); |
|
333 | Q_Q(QValueAxis); | |
332 | bool ok; |
|
334 | bool ok; | |
333 | qreal value = min.toReal(&ok); |
|
335 | qreal value = min.toReal(&ok); | |
334 | if (ok) |
|
336 | if (ok) | |
335 | q->setMin(value); |
|
337 | q->setMin(value); | |
336 | } |
|
338 | } | |
337 |
|
339 | |||
338 | void QValueAxisPrivate::setMax(const QVariant &max) |
|
340 | void QValueAxisPrivate::setMax(const QVariant &max) | |
339 | { |
|
341 | { | |
340 |
|
342 | |||
341 | Q_Q(QValueAxis); |
|
343 | Q_Q(QValueAxis); | |
342 | bool ok; |
|
344 | bool ok; | |
343 | qreal value = max.toReal(&ok); |
|
345 | qreal value = max.toReal(&ok); | |
344 | if (ok) |
|
346 | if (ok) | |
345 | q->setMax(value); |
|
347 | q->setMax(value); | |
346 | } |
|
348 | } | |
347 |
|
349 | |||
348 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
350 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) | |
349 | { |
|
351 | { | |
350 | Q_Q(QValueAxis); |
|
352 | Q_Q(QValueAxis); | |
351 | bool ok1; |
|
353 | bool ok1; | |
352 | bool ok2; |
|
354 | bool ok2; | |
353 | qreal value1 = min.toReal(&ok1); |
|
355 | qreal value1 = min.toReal(&ok1); | |
354 | qreal value2 = max.toReal(&ok2); |
|
356 | qreal value2 = max.toReal(&ok2); | |
355 | if (ok1 && ok2) |
|
357 | if (ok1 && ok2) | |
356 | q->setRange(value1, value2); |
|
358 | q->setRange(value1, value2); | |
357 | } |
|
359 | } | |
358 |
|
360 | |||
359 | ChartAxis *QValueAxisPrivate::createGraphics(ChartPresenter *presenter) |
|
361 | ChartAxis *QValueAxisPrivate::createGraphics(ChartPresenter *presenter) | |
360 | { |
|
362 | { | |
361 | Q_Q(QValueAxis); |
|
363 | Q_Q(QValueAxis); | |
362 | if (m_orientation == Qt::Vertical) |
|
364 | if (m_orientation == Qt::Vertical) | |
363 | return new ChartValueAxisY(q, presenter); |
|
365 | return new ChartValueAxisY(q, presenter); | |
364 | return new ChartValueAxisX(q, presenter); |
|
366 | return new ChartValueAxisX(q, presenter); | |
365 | } |
|
367 | } | |
366 |
|
368 | |||
367 | void QValueAxisPrivate::intializeDomain(Domain *domain) |
|
369 | void QValueAxisPrivate::intializeDomain(Domain *domain) | |
368 | { |
|
370 | { | |
369 | Q_Q(QValueAxis); |
|
371 | Q_Q(QValueAxis); | |
370 | if (qFuzzyCompare(m_max, m_min)) { |
|
372 | if (qFuzzyCompare(m_max, m_min)) { | |
371 | if (m_orientation == Qt::Vertical) |
|
373 | if (m_orientation == Qt::Vertical) | |
372 | q->setRange(domain->minY(), domain->maxY()); |
|
374 | q->setRange(domain->minY(), domain->maxY()); | |
373 | else |
|
375 | else | |
374 | q->setRange(domain->minX(), domain->maxX()); |
|
376 | q->setRange(domain->minX(), domain->maxX()); | |
375 | } else { |
|
377 | } else { | |
376 | if (m_orientation == Qt::Vertical) |
|
378 | if (m_orientation == Qt::Vertical) | |
377 | domain->setRangeY(m_min, m_max); |
|
379 | domain->setRangeY(m_min, m_max); | |
378 | else |
|
380 | else | |
379 | domain->setRangeX(m_min, m_max); |
|
381 | domain->setRangeX(m_min, m_max); | |
380 | } |
|
382 | } | |
381 | } |
|
383 | } | |
382 |
|
384 | |||
383 | //algorithm defined by Paul S.Heckbert GraphicalGems I |
|
385 | //algorithm defined by Paul S.Heckbert GraphicalGems I | |
384 |
|
386 | |||
385 | void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const |
|
387 | void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const | |
386 | { |
|
388 | { | |
387 | qreal range = niceNumber(max - min, true); //range with ceiling |
|
389 | qreal range = niceNumber(max - min, true); //range with ceiling | |
388 | qreal step = niceNumber(range / (ticksCount - 1), false); |
|
390 | qreal step = niceNumber(range / (ticksCount - 1), false); | |
389 | min = qFloor(min / step); |
|
391 | min = qFloor(min / step); | |
390 | max = qCeil(max / step); |
|
392 | max = qCeil(max / step); | |
391 | ticksCount = int(max - min) + 1; |
|
393 | ticksCount = int(max - min) + 1; | |
392 | min *= step; |
|
394 | min *= step; | |
393 | max *= step; |
|
395 | max *= step; | |
394 | } |
|
396 | } | |
395 |
|
397 | |||
396 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n |
|
398 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n | |
397 |
|
399 | |||
398 | qreal QValueAxisPrivate::niceNumber(qreal x, bool ceiling) const |
|
400 | qreal QValueAxisPrivate::niceNumber(qreal x, bool ceiling) const | |
399 | { |
|
401 | { | |
400 | qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x |
|
402 | qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x | |
401 | qreal q = x / z; //q<10 && q>=1; |
|
403 | qreal q = x / z; //q<10 && q>=1; | |
402 |
|
404 | |||
403 | if (ceiling) { |
|
405 | if (ceiling) { | |
404 | if (q <= 1.0) q = 1; |
|
406 | if (q <= 1.0) q = 1; | |
405 | else if (q <= 2.0) q = 2; |
|
407 | else if (q <= 2.0) q = 2; | |
406 | else if (q <= 5.0) q = 5; |
|
408 | else if (q <= 5.0) q = 5; | |
407 | else q = 10; |
|
409 | else q = 10; | |
408 | } else { |
|
410 | } else { | |
409 | if (q < 1.5) q = 1; |
|
411 | if (q < 1.5) q = 1; | |
410 | else if (q < 3.0) q = 2; |
|
412 | else if (q < 3.0) q = 2; | |
411 | else if (q < 7.0) q = 5; |
|
413 | else if (q < 7.0) q = 5; | |
412 | else q = 10; |
|
414 | else q = 10; | |
413 | } |
|
415 | } | |
414 | return q * z; |
|
416 | return q * z; | |
415 | } |
|
417 | } | |
416 |
|
418 | |||
417 | #include "moc_qvalueaxis.cpp" |
|
419 | #include "moc_qvalueaxis.cpp" | |
418 | #include "moc_qvalueaxis_p.cpp" |
|
420 | #include "moc_qvalueaxis_p.cpp" | |
419 |
|
421 | |||
420 | QTCOMMERCIALCHART_END_NAMESPACE |
|
422 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now