##// END OF EJS Templates
Serious bugfix of wrong parenting of axis grid, shades, and arrow
Michal Klocek -
r1744:2f1116056985
parent child
Show More
@@ -1,370 +1,370
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 "domain_p.h"
25 #include "domain_p.h"
26 #include <qmath.h>
26 #include <qmath.h>
27 #include <QDateTime>
27 #include <QDateTime>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
31 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
32 m_chartAxis(axis),
32 m_chartAxis(axis),
33 m_labelsAngle(0),
33 m_labelsAngle(0),
34 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
34 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
35 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
35 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
36 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
36 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
37 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
37 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
38 m_min(0),
38 m_min(0),
39 m_max(0),
39 m_max(0),
40 m_animation(0),
40 m_animation(0),
41 m_minWidth(0),
41 m_minWidth(0),
42 m_minHeight(0)
42 m_minHeight(0)
43 {
43 {
44 //initial initialization
44 //initial initialization
45 m_arrow->setZValue(ChartPresenter::AxisZValue);
45 m_arrow->setZValue(ChartPresenter::AxisZValue);
46 m_arrow->setHandlesChildEvents(false);
46 m_arrow->setHandlesChildEvents(false);
47
47
48 m_shades->setZValue(ChartPresenter::ShadesZValue);
48 m_shades->setZValue(ChartPresenter::ShadesZValue);
49 m_grid->setZValue(ChartPresenter::GridZValue);
49 m_grid->setZValue(ChartPresenter::GridZValue);
50
50
51 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
51 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
52
52
53 QGraphicsSimpleTextItem item;
53 QGraphicsSimpleTextItem item;
54 m_font = item.font();
54 m_font = item.font();
55 }
55 }
56
56
57 ChartAxis::~ChartAxis()
57 ChartAxis::~ChartAxis()
58 {
58 {
59 }
59 }
60
60
61 void ChartAxis::setAnimation(AxisAnimation* animation)
61 void ChartAxis::setAnimation(AxisAnimation* animation)
62 {
62 {
63 m_animation=animation;
63 m_animation=animation;
64 }
64 }
65
65
66 void ChartAxis::setLayout(QVector<qreal> &layout)
66 void ChartAxis::setLayout(QVector<qreal> &layout)
67 {
67 {
68 m_layoutVector=layout;
68 m_layoutVector=layout;
69 }
69 }
70
70
71 void ChartAxis::createItems(int count)
71 void ChartAxis::createItems(int count)
72 {
72 {
73 if (m_arrow->children().size() == 0)
73 if (m_arrow->children().size() == 0)
74 m_arrow->addToGroup(new AxisItem(this));
74 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
75 for (int i = 0; i < count; ++i) {
75 for (int i = 0; i < count; ++i) {
76 m_grid->addToGroup(new QGraphicsLineItem());
76 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
77 m_labels->addToGroup(new QGraphicsSimpleTextItem());
77 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
78 m_arrow->addToGroup(new QGraphicsLineItem());
78 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
79 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
79 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
80 }
80 }
81 }
81 }
82
82
83 void ChartAxis::deleteItems(int count)
83 void ChartAxis::deleteItems(int count)
84 {
84 {
85 QList<QGraphicsItem *> lines = m_grid->childItems();
85 QList<QGraphicsItem *> lines = m_grid->childItems();
86 QList<QGraphicsItem *> labels = m_labels->childItems();
86 QList<QGraphicsItem *> labels = m_labels->childItems();
87 QList<QGraphicsItem *> shades = m_shades->childItems();
87 QList<QGraphicsItem *> shades = m_shades->childItems();
88 QList<QGraphicsItem *> axis = m_arrow->childItems();
88 QList<QGraphicsItem *> axis = m_arrow->childItems();
89
89
90 for (int i = 0; i < count; ++i) {
90 for (int i = 0; i < count; ++i) {
91 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
91 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
92 delete(lines.takeLast());
92 delete(lines.takeLast());
93 delete(labels.takeLast());
93 delete(labels.takeLast());
94 delete(axis.takeLast());
94 delete(axis.takeLast());
95 }
95 }
96 }
96 }
97
97
98 void ChartAxis::updateLayout(QVector<qreal> &layout)
98 void ChartAxis::updateLayout(QVector<qreal> &layout)
99 {
99 {
100 int diff = m_layoutVector.size() - layout.size();
100 int diff = m_layoutVector.size() - layout.size();
101
101
102 if (diff>0) {
102 if (diff>0) {
103 deleteItems(diff);
103 deleteItems(diff);
104 }
104 }
105 else if (diff<0) {
105 else if (diff<0) {
106 createItems(-diff);
106 createItems(-diff);
107 }
107 }
108
108
109 if(diff<0) handleAxisUpdated();
109 if(diff<0) handleAxisUpdated();
110
110
111 if (m_animation) {
111 if (m_animation) {
112 switch(presenter()->state()){
112 switch(presenter()->state()){
113 case ChartPresenter::ZoomInState:
113 case ChartPresenter::ZoomInState:
114 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
114 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
115 m_animation->setAnimationPoint(presenter()->statePoint());
115 m_animation->setAnimationPoint(presenter()->statePoint());
116 break;
116 break;
117 case ChartPresenter::ZoomOutState:
117 case ChartPresenter::ZoomOutState:
118 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
118 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
119 m_animation->setAnimationPoint(presenter()->statePoint());
119 m_animation->setAnimationPoint(presenter()->statePoint());
120 break;
120 break;
121 case ChartPresenter::ScrollUpState:
121 case ChartPresenter::ScrollUpState:
122 case ChartPresenter::ScrollLeftState:
122 case ChartPresenter::ScrollLeftState:
123 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
123 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
124 break;
124 break;
125 case ChartPresenter::ScrollDownState:
125 case ChartPresenter::ScrollDownState:
126 case ChartPresenter::ScrollRightState:
126 case ChartPresenter::ScrollRightState:
127 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
127 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
128 break;
128 break;
129 case ChartPresenter::ShowState:
129 case ChartPresenter::ShowState:
130 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
130 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
131 break;
131 break;
132 }
132 }
133 m_animation->setValues(m_layoutVector,layout);
133 m_animation->setValues(m_layoutVector,layout);
134 presenter()->startAnimation(m_animation);
134 presenter()->startAnimation(m_animation);
135 }
135 }
136 else {
136 else {
137 setLayout(layout);
137 setLayout(layout);
138 updateGeometry();
138 updateGeometry();
139 }
139 }
140 }
140 }
141
141
142 void ChartAxis::setArrowOpacity(qreal opacity)
142 void ChartAxis::setArrowOpacity(qreal opacity)
143 {
143 {
144 m_arrow->setOpacity(opacity);
144 m_arrow->setOpacity(opacity);
145 }
145 }
146
146
147 qreal ChartAxis::arrowOpacity() const
147 qreal ChartAxis::arrowOpacity() const
148 {
148 {
149 return m_arrow->opacity();
149 return m_arrow->opacity();
150 }
150 }
151
151
152 void ChartAxis::setArrowVisibility(bool visible)
152 void ChartAxis::setArrowVisibility(bool visible)
153 {
153 {
154 m_arrow->setOpacity(visible);
154 m_arrow->setOpacity(visible);
155 }
155 }
156
156
157 void ChartAxis::setGridOpacity(qreal opacity)
157 void ChartAxis::setGridOpacity(qreal opacity)
158 {
158 {
159 m_grid->setOpacity(opacity);
159 m_grid->setOpacity(opacity);
160 }
160 }
161
161
162 qreal ChartAxis::gridOpacity() const
162 qreal ChartAxis::gridOpacity() const
163 {
163 {
164 return m_grid->opacity();
164 return m_grid->opacity();
165 }
165 }
166
166
167 void ChartAxis::setGridVisibility(bool visible)
167 void ChartAxis::setGridVisibility(bool visible)
168 {
168 {
169 m_grid->setOpacity(visible);
169 m_grid->setOpacity(visible);
170 }
170 }
171
171
172 void ChartAxis::setLabelsOpacity(qreal opacity)
172 void ChartAxis::setLabelsOpacity(qreal opacity)
173 {
173 {
174 m_labels->setOpacity(opacity);
174 m_labels->setOpacity(opacity);
175 }
175 }
176
176
177 qreal ChartAxis::labelsOpacity() const
177 qreal ChartAxis::labelsOpacity() const
178 {
178 {
179 return m_labels->opacity();
179 return m_labels->opacity();
180 }
180 }
181
181
182 void ChartAxis::setLabelsVisibility(bool visible)
182 void ChartAxis::setLabelsVisibility(bool visible)
183 {
183 {
184 m_labels->setOpacity(visible);
184 m_labels->setOpacity(visible);
185 }
185 }
186
186
187 void ChartAxis::setShadesOpacity(qreal opacity)
187 void ChartAxis::setShadesOpacity(qreal opacity)
188 {
188 {
189 m_shades->setOpacity(opacity);
189 m_shades->setOpacity(opacity);
190 }
190 }
191
191
192 qreal ChartAxis::shadesOpacity() const
192 qreal ChartAxis::shadesOpacity() const
193 {
193 {
194 return m_shades->opacity();
194 return m_shades->opacity();
195 }
195 }
196
196
197 void ChartAxis::setShadesVisibility(bool visible)
197 void ChartAxis::setShadesVisibility(bool visible)
198 {
198 {
199 m_shades->setVisible(visible);
199 m_shades->setVisible(visible);
200 }
200 }
201
201
202 void ChartAxis::setLabelsAngle(int angle)
202 void ChartAxis::setLabelsAngle(int angle)
203 {
203 {
204 foreach(QGraphicsItem* item , m_labels->childItems()) {
204 foreach(QGraphicsItem* item , m_labels->childItems()) {
205 item->setRotation(angle);
205 item->setRotation(angle);
206 }
206 }
207
207
208 m_labelsAngle=angle;
208 m_labelsAngle=angle;
209 }
209 }
210
210
211 void ChartAxis::setLabelsPen(const QPen &pen)
211 void ChartAxis::setLabelsPen(const QPen &pen)
212 {
212 {
213 foreach(QGraphicsItem* item , m_labels->childItems()) {
213 foreach(QGraphicsItem* item , m_labels->childItems()) {
214 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
214 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
215 }
215 }
216 }
216 }
217
217
218 void ChartAxis::setLabelsBrush(const QBrush &brush)
218 void ChartAxis::setLabelsBrush(const QBrush &brush)
219 {
219 {
220 foreach(QGraphicsItem* item , m_labels->childItems()) {
220 foreach(QGraphicsItem* item , m_labels->childItems()) {
221 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
221 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
222 }
222 }
223 }
223 }
224
224
225 void ChartAxis::setLabelsFont(const QFont &font)
225 void ChartAxis::setLabelsFont(const QFont &font)
226 {
226 {
227 foreach(QGraphicsItem* item , m_labels->childItems()) {
227 foreach(QGraphicsItem* item , m_labels->childItems()) {
228 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
228 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
229 }
229 }
230 m_font = font;
230 m_font = font;
231 }
231 }
232
232
233 void ChartAxis::setShadesBrush(const QBrush &brush)
233 void ChartAxis::setShadesBrush(const QBrush &brush)
234 {
234 {
235 foreach(QGraphicsItem* item , m_shades->childItems()) {
235 foreach(QGraphicsItem* item , m_shades->childItems()) {
236 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
236 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
237 }
237 }
238 }
238 }
239
239
240 void ChartAxis::setShadesPen(const QPen &pen)
240 void ChartAxis::setShadesPen(const QPen &pen)
241 {
241 {
242 foreach(QGraphicsItem* item , m_shades->childItems()) {
242 foreach(QGraphicsItem* item , m_shades->childItems()) {
243 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
243 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
244 }
244 }
245 }
245 }
246
246
247 void ChartAxis::setArrowPen(const QPen &pen)
247 void ChartAxis::setArrowPen(const QPen &pen)
248 {
248 {
249 foreach(QGraphicsItem* item , m_arrow->childItems()) {
249 foreach(QGraphicsItem* item , m_arrow->childItems()) {
250 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
250 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
251 }
251 }
252 }
252 }
253
253
254 void ChartAxis::setGridPen(const QPen &pen)
254 void ChartAxis::setGridPen(const QPen &pen)
255 {
255 {
256 foreach(QGraphicsItem* item , m_grid->childItems()) {
256 foreach(QGraphicsItem* item , m_grid->childItems()) {
257 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
257 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
258 }
258 }
259 }
259 }
260
260
261 bool ChartAxis::isEmpty()
261 bool ChartAxis::isEmpty()
262 {
262 {
263 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max);
263 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max);
264 }
264 }
265
265
266 void ChartAxis::handleDomainUpdated()
266 void ChartAxis::handleDomainUpdated()
267 {
267 {
268 Domain* domain = qobject_cast<Domain*>(sender());
268 Domain* domain = qobject_cast<Domain*>(sender());
269 qreal min(0);
269 qreal min(0);
270 qreal max(0);
270 qreal max(0);
271
271
272 if(m_chartAxis->orientation()==Qt::Horizontal) {
272 if(m_chartAxis->orientation()==Qt::Horizontal) {
273 min = domain->minX();
273 min = domain->minX();
274 max = domain->maxX();
274 max = domain->maxX();
275 }
275 }
276 else if (m_chartAxis->orientation()==Qt::Vertical)
276 else if (m_chartAxis->orientation()==Qt::Vertical)
277 {
277 {
278 min = domain->minY();
278 min = domain->minY();
279 max = domain->maxY();
279 max = domain->maxY();
280 }
280 }
281
281
282 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
282 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
283 {
283 {
284 m_min = min;
284 m_min = min;
285 m_max = max;
285 m_max = max;
286
286
287 if (!isEmpty()) {
287 if (!isEmpty()) {
288 QVector<qreal> layout = calculateLayout();
288 QVector<qreal> layout = calculateLayout();
289 updateLayout(layout);
289 updateLayout(layout);
290 }
290 }
291 }
291 }
292 }
292 }
293
293
294 void ChartAxis::handleAxisUpdated()
294 void ChartAxis::handleAxisUpdated()
295 {
295 {
296 if(isEmpty()) return;
296 if(isEmpty()) return;
297
297
298
298
299 bool visible = m_chartAxis->isVisible();
299 bool visible = m_chartAxis->isVisible();
300
300
301 setArrowVisibility(visible && m_chartAxis->isArrowVisible());
301 setArrowVisibility(visible && m_chartAxis->isArrowVisible());
302 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
302 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
303 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
303 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
304 setShadesVisibility(visible && m_chartAxis->shadesVisible());
304 setShadesVisibility(visible && m_chartAxis->shadesVisible());
305 setLabelsAngle(m_chartAxis->labelsAngle());
305 setLabelsAngle(m_chartAxis->labelsAngle());
306 setArrowPen(m_chartAxis->axisPen());
306 setArrowPen(m_chartAxis->axisPen());
307 setLabelsPen(m_chartAxis->labelsPen());
307 setLabelsPen(m_chartAxis->labelsPen());
308 setLabelsBrush(m_chartAxis->labelsBrush());
308 setLabelsBrush(m_chartAxis->labelsBrush());
309 setLabelsFont(m_chartAxis->labelsFont());
309 setLabelsFont(m_chartAxis->labelsFont());
310 setGridPen(m_chartAxis->gridLinePen());
310 setGridPen(m_chartAxis->gridLinePen());
311 setShadesPen(m_chartAxis->shadesPen());
311 setShadesPen(m_chartAxis->shadesPen());
312 setShadesBrush(m_chartAxis->shadesBrush());
312 setShadesBrush(m_chartAxis->shadesBrush());
313
313
314 }
314 }
315
315
316 void ChartAxis::hide()
316 void ChartAxis::hide()
317 {
317 {
318 setArrowVisibility(false);
318 setArrowVisibility(false);
319 setGridVisibility(false);
319 setGridVisibility(false);
320 setLabelsVisibility(false);
320 setLabelsVisibility(false);
321 setShadesVisibility(false);
321 setShadesVisibility(false);
322 }
322 }
323
323
324 void ChartAxis::handleGeometryChanged(const QRectF &rect)
324 void ChartAxis::handleGeometryChanged(const QRectF &rect)
325 {
325 {
326 if(m_rect != rect)
326 if(m_rect != rect)
327 {
327 {
328 m_rect = rect;
328 m_rect = rect;
329 if (isEmpty()) return;
329 if (isEmpty()) return;
330 QVector<qreal> layout = calculateLayout();
330 QVector<qreal> layout = calculateLayout();
331 updateLayout(layout);
331 updateLayout(layout);
332 }
332 }
333 }
333 }
334
334
335
335
336 qreal ChartAxis::minimumWidth()
336 qreal ChartAxis::minimumWidth()
337 {
337 {
338 if(m_minWidth == 0) updateGeometry();
338 if(m_minWidth == 0) updateGeometry();
339 return m_minWidth;
339 return m_minWidth;
340 }
340 }
341
341
342 qreal ChartAxis::minimumHeight()
342 qreal ChartAxis::minimumHeight()
343 {
343 {
344 if(m_minHeight == 0) updateGeometry();
344 if(m_minHeight == 0) updateGeometry();
345 return m_minHeight;
345 return m_minHeight;
346 }
346 }
347
347
348
348
349 void ChartAxis::axisSelected()
349 void ChartAxis::axisSelected()
350 {
350 {
351 qDebug()<<"TODO: axis clicked";
351 qDebug()<<"TODO: axis clicked";
352 }
352 }
353
353
354
354
355 void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const
355 void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const
356 {
356 {
357 Q_ASSERT(max>min);
357 Q_ASSERT(max>min);
358 Q_ASSERT(ticks>1);
358 Q_ASSERT(ticks>1);
359
359
360 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
360 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
361 n++;
361 n++;
362 for (int i=0; i< ticks; i++) {
362 for (int i=0; i< ticks; i++) {
363 qreal value = min + (i * (max - min)/ (ticks-1));
363 qreal value = min + (i * (max - min)/ (ticks-1));
364 labels << QString::number(value,'f',n);
364 labels << QString::number(value,'f',n);
365 }
365 }
366 }
366 }
367
367
368 #include "moc_chartaxis_p.cpp"
368 #include "moc_chartaxis_p.cpp"
369
369
370 QTCOMMERCIALCHART_END_NAMESPACE
370 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now