##// END OF EJS Templates
Clean up axis animation code
Michal Klocek -
r393:61d1f8f10e51
parent child
Show More
@@ -1,71 +1,74
1 #include "axisanimationitem_p.h"
1 #include "axisanimationitem_p.h"
2 #include <QPropertyAnimation>
2 #include <QTimer>
3
3
4 Q_DECLARE_METATYPE(QVector<qreal>)
4 Q_DECLARE_METATYPE(QVector<qreal>)
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 const static int duration = 500;
8 const static int duration = 500;
9
9
10 AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) :
10 AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem(type,parent)
11 AxisItem(type,parent),
12 m_animation(new AxisAnimator(this,this))
12 {
13 {
13 }
14 }
14
15
15 AxisAnimationItem::~AxisAnimationItem()
16 AxisAnimationItem::~AxisAnimationItem()
16 {
17 {
17 }
18 }
18
19
19 void AxisAnimationItem::updateItems(QVector<qreal>& vector1)
20 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
20 {
21 {
21 QVector<qreal> vector0 = vector1;
22 if(newLayout.count()==0) return;
22 calculateLayout(vector1);
23 oldLayout.resize(newLayout.size());
23 if(vector1.count()==0) return;
24
24 vector0.resize(vector1.size());
25 if(m_animation->state()!=QAbstractAnimation::Stopped){
25
26 m_animation->stop();
26 AxisAnimator *animation = new AxisAnimator(this,this);
27 }
27 animation->setDuration(duration);
28
28 animation->setEasingCurve(QEasingCurve::InOutBack);
29 m_animation->setDuration(duration);
29 animation->setKeyValueAt(0.0, qVariantFromValue(vector0));
30 m_animation->setEasingCurve(QEasingCurve::InOutBack);
30 animation->setKeyValueAt(1.0, qVariantFromValue(vector1));
31 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
31 animation->start(QAbstractAnimation::DeleteWhenStopped);
32 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
32 }
35 }
33
36
34 void AxisAnimationItem::setLabelsAngle(int angle)
37 void AxisAnimationItem::setLabelsAngle(int angle)
35 {
38 {
36 AxisItem::setLabelsAngle(angle);
39 AxisItem::setLabelsAngle(angle);
37 }
40 }
38
41
39 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
42 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
40 m_axis(axis)
43 m_axis(axis)
41 {
44 {
42 }
45 }
43
46
44 AxisAnimator::~AxisAnimator()
47 AxisAnimator::~AxisAnimator()
45 {
48 {
46 }
49 }
47
50
48 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
51 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
49 {
52 {
50 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
53 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
51 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
54 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
52 QVector<qreal> result;
55 QVector<qreal> result;
53 Q_ASSERT(startVector.count() == endVecotr.count());
56 Q_ASSERT(startVector.count() == endVecotr.count());
54
57
55 for(int i =0 ;i< startVector.count();i++){
58 for(int i =0 ;i< startVector.count();i++){
56 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
59 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
57 result << value;
60 result << value;
58 }
61 }
59 return qVariantFromValue(result);
62 return qVariantFromValue(result);
60 }
63 }
61
64
62
65
63 void AxisAnimator::updateCurrentValue (const QVariant & value )
66 void AxisAnimator::updateCurrentValue (const QVariant & value )
64 {
67 {
65 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
68 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
66 m_axis->applyLayout(vector);
69 m_axis->applyLayout(vector);
67 }
70 }
68
71
69 #include "moc_axisanimationitem_p.cpp"
72 #include "moc_axisanimationitem_p.cpp"
70
73
71 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,44 +1,45
1 #ifndef AXISANIMATIONITEM_H_
1 #ifndef AXISANIMATIONITEM_H_
2 #define AXISANIMATIONITEM_H_
2 #define AXISANIMATIONITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "axisitem_p.h"
5 #include "axisitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7 #include <QVariantAnimation>
7 #include <QVariantAnimation>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QChartAxis;
11 class AxisAnimator;
12
12
13 class AxisAnimationItem : public AxisItem
13 class AxisAnimationItem : public AxisItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 AxisAnimationItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisAnimationItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisAnimationItem();
19 ~AxisAnimationItem();
20
20
21 void setLabelsAngle(int angle);
21 void setLabelsAngle(int angle);
22
22
23 protected:
23 protected:
24 void updateItems(QVector<qreal>& vector);
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
25
25 private:
26 AxisAnimator *m_animation;
26 };
27 };
27
28
28 class AxisAnimator: public QVariantAnimation
29 class AxisAnimator: public QVariantAnimation
29 {
30 {
30 public:
31 public:
31 AxisAnimator(AxisItem *axis,QObject *parent = 0);
32 AxisAnimator(AxisItem *axis,QObject *parent = 0);
32 ~AxisAnimator();
33 ~AxisAnimator();
33 protected:
34 protected:
34 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
35 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
35 virtual void updateCurrentValue (const QVariant & value );
36 virtual void updateCurrentValue (const QVariant & value );
36 private:
37 private:
37 AxisItem* m_axis;
38 AxisItem* m_axis;
38 };
39 };
39
40
40 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
41
42
42
43
43
44
44 #endif /* AXISITEM_H_ */
45 #endif /* AXISITEM_H_ */
@@ -1,332 +1,340
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_type(type),
13 m_type(type),
14 m_labelsAngle(0),
14 m_labelsAngle(0),
15 m_grid(parent),
15 m_grid(parent),
16 m_shades(parent),
16 m_shades(parent),
17 m_labels(parent),
17 m_labels(parent),
18 m_axis(parent)
18 m_axis(parent)
19 {
19 {
20 //initial initialization
20 //initial initialization
21 m_axis.setZValue(ChartPresenter::AxisZValue);
21 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
24 setFlags(QGraphicsItem::ItemHasNoContents);
24 setFlags(QGraphicsItem::ItemHasNoContents);
25 }
25 }
26
26
27 AxisItem::~AxisItem()
27 AxisItem::~AxisItem()
28 {
28 {
29 }
29 }
30
30
31 QRectF AxisItem::boundingRect() const
31 QRectF AxisItem::boundingRect() const
32 {
32 {
33 return QRectF();
33 return QRectF();
34 }
34 }
35
35
36 void AxisItem::createItems(int count)
36 void AxisItem::createItems(int count)
37 {
37 {
38 if(m_axis.children().size()==0)
38 if(m_axis.children().size()==0)
39 m_axis.addToGroup(new QGraphicsLineItem());
39 m_axis.addToGroup(new QGraphicsLineItem());
40 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
41 m_grid.addToGroup(new QGraphicsLineItem());
41 m_grid.addToGroup(new QGraphicsLineItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
45 }
45 }
46 }
46 }
47
47
48 void AxisItem::clear(int count)
48 void AxisItem::clear(int count)
49 {
49 {
50 QList<QGraphicsItem *> lines = m_grid.childItems();
50 QList<QGraphicsItem *> lines = m_grid.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
54
54
55 for (int i = 0; i < count; ++i) {
55 for (int i = 0; i < count; ++i) {
56 delete(lines.takeLast());
56 delete(lines.takeLast());
57 delete(labels.takeLast());
57 delete(labels.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
59 delete(axis.takeLast());
59 delete(axis.takeLast());
60 }
60 }
61 }
61 }
62
62
63 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
64 {
64 {
65 Q_UNUSED(painter);
65 Q_UNUSED(painter);
66 Q_UNUSED(option);
66 Q_UNUSED(option);
67 Q_UNUSED(widget);
67 Q_UNUSED(widget);
68 }
68 }
69
69
70 void AxisItem::updateItems(QVector<qreal>& vector)
70 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
71 {
71 {
72 calculateLayout(vector);
72 if(newLayout.count()==0) return;
73 if(vector.count()==0) return;
73 applyLayout(newLayout);
74 applyLayout(vector);
74 oldLayout=newLayout;
75 }
75 }
76
76
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
78 {
78 {
79 if(m_layoutVector.count()==0) return;
79 if(m_layoutVector.count()==0) return;
80
80
81 if(axis->isAxisVisible()) {
81 if(axis->isAxisVisible()) {
82 setAxisOpacity(100);
82 setAxisOpacity(100);
83 }
83 }
84 else {
84 else {
85 setAxisOpacity(0);
85 setAxisOpacity(0);
86 }
86 }
87
87
88 if(axis->isGridVisible()) {
88 if(axis->isGridVisible()) {
89 setGridOpacity(100);
89 setGridOpacity(100);
90 }
90 }
91 else {
91 else {
92 setGridOpacity(0);
92 setGridOpacity(0);
93 }
93 }
94
94
95 if(axis->labelsVisible())
95 if(axis->labelsVisible())
96 {
96 {
97 setLabelsOpacity(100);
97 setLabelsOpacity(100);
98 }
98 }
99 else {
99 else {
100 setLabelsOpacity(0);
100 setLabelsOpacity(0);
101 }
101 }
102
102
103 if(axis->shadesVisible()) {
103 if(axis->shadesVisible()) {
104 setShadesOpacity(axis->shadesOpacity());
104 setShadesOpacity(axis->shadesOpacity());
105 }
105 }
106 else {
106 else {
107 setShadesOpacity(0);
107 setShadesOpacity(0);
108 }
108 }
109
109
110 setLabelsAngle(axis->labelsAngle());
110 setLabelsAngle(axis->labelsAngle());
111 setAxisPen(axis->axisPen());
111 setAxisPen(axis->axisPen());
112 setLabelsPen(axis->labelsPen());
112 setLabelsPen(axis->labelsPen());
113 setLabelsBrush(axis->labelsBrush());
113 setLabelsBrush(axis->labelsBrush());
114 setLabelsFont(axis->labelsFont());
114 setLabelsFont(axis->labelsFont());
115 setGridPen(axis->gridPen());
115 setGridPen(axis->gridPen());
116 setShadesPen(axis->shadesPen());
116 setShadesPen(axis->shadesPen());
117 setShadesBrush(axis->shadesBrush());
117 setShadesBrush(axis->shadesBrush());
118 }
118 }
119
119
120 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
120 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
121 {
121 {
122 int diff = m_thicksList.size() - labels.size();
122 int diff = m_thicksList.size() - labels.size();
123
123
124 if(diff>0){
124 if(diff>0){
125 clear(diff);
125 clear(diff);
126 }else if(diff<0){
126 }else if(diff<0){
127 createItems(-diff);
127 createItems(-diff);
128 }
128 }
129 m_thicksList=labels;
129 m_thicksList=labels;
130 m_layoutVector.resize(m_thicksList.size());
130 QVector<qreal> vector = calculateLayout();
131 updateItems(m_layoutVector);
131 updateItems(m_layoutVector,vector);
132 if(diff!=0) handleAxisUpdate(axis);
132 if(diff!=0) handleAxisUpdate(axis);
133 }
133 }
134
134
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
136 {
136 {
137 m_rect = rect;
137 m_rect = rect;
138 updateItems(m_layoutVector);
138
139 if(m_thicksList.size()==0) return;
140
141 QVector<qreal> vector = calculateLayout();
142 updateItems(m_layoutVector,vector);
139 }
143 }
140
144
141 void AxisItem::setAxisOpacity(qreal opacity)
145 void AxisItem::setAxisOpacity(qreal opacity)
142 {
146 {
143 m_axis.setOpacity(opacity);
147 m_axis.setOpacity(opacity);
144 }
148 }
145
149
146 qreal AxisItem::axisOpacity() const
150 qreal AxisItem::axisOpacity() const
147 {
151 {
148 return m_axis.opacity();
152 return m_axis.opacity();
149 }
153 }
150
154
151 void AxisItem::setGridOpacity(qreal opacity)
155 void AxisItem::setGridOpacity(qreal opacity)
152 {
156 {
153 m_grid.setOpacity(opacity);
157 m_grid.setOpacity(opacity);
154 }
158 }
155
159
156 qreal AxisItem::gridOpacity() const
160 qreal AxisItem::gridOpacity() const
157 {
161 {
158 return m_grid.opacity();
162 return m_grid.opacity();
159 }
163 }
160
164
161 void AxisItem::setLabelsOpacity(qreal opacity)
165 void AxisItem::setLabelsOpacity(qreal opacity)
162 {
166 {
163 m_labels.setOpacity(opacity);
167 m_labels.setOpacity(opacity);
164 }
168 }
165
169
166 qreal AxisItem::labelsOpacity() const
170 qreal AxisItem::labelsOpacity() const
167 {
171 {
168 return m_labels.opacity();
172 return m_labels.opacity();
169 }
173 }
170
174
171 void AxisItem::setShadesOpacity(qreal opacity)
175 void AxisItem::setShadesOpacity(qreal opacity)
172 {
176 {
173 m_shades.setOpacity(opacity);
177 m_shades.setOpacity(opacity);
174 }
178 }
175
179
176 qreal AxisItem::shadesOpacity() const
180 qreal AxisItem::shadesOpacity() const
177 {
181 {
178 return m_shades.opacity();
182 return m_shades.opacity();
179 }
183 }
180
184
181 void AxisItem::setLabelsAngle(int angle)
185 void AxisItem::setLabelsAngle(int angle)
182 {
186 {
183 foreach(QGraphicsItem* item , m_labels.childItems()) {
187 foreach(QGraphicsItem* item , m_labels.childItems()) {
184 QPointF center = item->boundingRect().center();
188 QPointF center = item->boundingRect().center();
185 item->setRotation(angle);
189 item->setRotation(angle);
186 }
190 }
187
191
188 m_labelsAngle=angle;
192 m_labelsAngle=angle;
189 }
193 }
190
194
191 void AxisItem::setLabelsPen(const QPen& pen)
195 void AxisItem::setLabelsPen(const QPen& pen)
192 {
196 {
193 foreach(QGraphicsItem* item , m_labels.childItems()) {
197 foreach(QGraphicsItem* item , m_labels.childItems()) {
194 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
198 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
195 }
199 }
196 }
200 }
197
201
198 void AxisItem::setLabelsBrush(const QBrush& brush)
202 void AxisItem::setLabelsBrush(const QBrush& brush)
199 {
203 {
200 foreach(QGraphicsItem* item , m_labels.childItems()) {
204 foreach(QGraphicsItem* item , m_labels.childItems()) {
201 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
205 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
202 }
206 }
203 }
207 }
204
208
205 void AxisItem::setLabelsFont(const QFont& font)
209 void AxisItem::setLabelsFont(const QFont& font)
206 {
210 {
207 foreach(QGraphicsItem* item , m_labels.childItems()) {
211 foreach(QGraphicsItem* item , m_labels.childItems()) {
208 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
212 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
209 }
213 }
210 }
214 }
211
215
212 void AxisItem::setShadesBrush(const QBrush& brush)
216 void AxisItem::setShadesBrush(const QBrush& brush)
213 {
217 {
214 foreach(QGraphicsItem* item , m_shades.childItems()) {
218 foreach(QGraphicsItem* item , m_shades.childItems()) {
215 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
219 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
216 }
220 }
217 }
221 }
218
222
219 void AxisItem::setShadesPen(const QPen& pen)
223 void AxisItem::setShadesPen(const QPen& pen)
220 {
224 {
221 foreach(QGraphicsItem* item , m_shades.childItems()) {
225 foreach(QGraphicsItem* item , m_shades.childItems()) {
222 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
226 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
223 }
227 }
224 }
228 }
225
229
226 void AxisItem::setAxisPen(const QPen& pen)
230 void AxisItem::setAxisPen(const QPen& pen)
227 {
231 {
228 foreach(QGraphicsItem* item , m_axis.childItems()) {
232 foreach(QGraphicsItem* item , m_axis.childItems()) {
229 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
233 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
230 }
234 }
231 }
235 }
232
236
233 void AxisItem::setGridPen(const QPen& pen)
237 void AxisItem::setGridPen(const QPen& pen)
234 {
238 {
235 foreach(QGraphicsItem* item , m_grid.childItems()) {
239 foreach(QGraphicsItem* item , m_grid.childItems()) {
236 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
240 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
237 }
241 }
238 }
242 }
239
243
240 void AxisItem::calculateLayout(QVector<qreal>& points)
244 QVector<qreal> AxisItem::calculateLayout() const
241 {
245 {
246 QVector<qreal> points;
247 points.resize(m_thicksList.size());
248
242 switch (m_type)
249 switch (m_type)
243 {
250 {
244 case X_AXIS:
251 case X_AXIS:
245 {
252 {
246 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
253 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
247 for (int i = 0; i < m_thicksList.size(); ++i) {
254 for (int i = 0; i < m_thicksList.size(); ++i) {
248 int x = i * deltaX + m_rect.left();
255 int x = i * deltaX + m_rect.left();
249 points[i]=x;
256 points[i] = x;
250 }
257 }
251 }
258 }
252 break;
259 break;
253 case Y_AXIS:
260 case Y_AXIS:
254 {
261 {
255 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
262 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
256 for (int i = 0; i < m_thicksList.size(); ++i) {
263 for (int i = 0; i < m_thicksList.size(); ++i) {
257 int y = i * -deltaY + m_rect.bottom();
264 int y = i * -deltaY + m_rect.bottom();
258 points[i]=y;
265 points[i] = y;
259 }
266 }
260 }
267 }
261 break;
268 break;
262 }
269 }
270 return points;
263 }
271 }
264
272
265 void AxisItem::applyLayout(const QVector<qreal>& points)
273 void AxisItem::applyLayout(const QVector<qreal>& points)
266 {
274 {
267 Q_ASSERT(points.size() == m_thicksList.size());
275 Q_ASSERT(points.size() == m_thicksList.size());
268
276
269 QList<QGraphicsItem *> lines = m_grid.childItems();
277 QList<QGraphicsItem *> lines = m_grid.childItems();
270 QList<QGraphicsItem *> labels = m_labels.childItems();
278 QList<QGraphicsItem *> labels = m_labels.childItems();
271 QList<QGraphicsItem *> shades = m_shades.childItems();
279 QList<QGraphicsItem *> shades = m_shades.childItems();
272 QList<QGraphicsItem *> axis = m_axis.childItems();
280 QList<QGraphicsItem *> axis = m_axis.childItems();
273
281
274 Q_ASSERT(labels.size() == m_thicksList.size());
282 Q_ASSERT(labels.size() == m_thicksList.size());
275
283
276 switch (m_type)
284 switch (m_type)
277 {
285 {
278 case X_AXIS:
286 case X_AXIS:
279 {
287 {
280 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
288 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
281 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
289 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
282
290
283 for (int i = 0; i < points.size(); ++i) {
291 for (int i = 0; i < points.size(); ++i) {
284 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
292 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
285 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
293 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
286 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
294 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
287 labelItem->setText(m_thicksList.at(i));
295 labelItem->setText(m_thicksList.at(i));
288 QPointF center = labelItem->boundingRect().center();
296 QPointF center = labelItem->boundingRect().center();
289 labelItem->setTransformOriginPoint(center.x(), center.y());
297 labelItem->setTransformOriginPoint(center.x(), center.y());
290 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
298 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
291 if(i%2){
299 if(i%2){
292 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
300 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
293 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
301 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
294 }
302 }
295 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
303 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
296 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
304 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
297 }
305 }
298 }
306 }
299 break;
307 break;
300
308
301 case Y_AXIS:
309 case Y_AXIS:
302 {
310 {
303 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
311 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
304 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
312 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
305
313
306 for (int i = 0; i < points.size(); ++i) {
314 for (int i = 0; i < points.size(); ++i) {
307 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
315 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
308 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
316 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
309 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
317 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
310 labelItem->setText(m_thicksList.at(i));
318 labelItem->setText(m_thicksList.at(i));
311 QPointF center = labelItem->boundingRect().center();
319 QPointF center = labelItem->boundingRect().center();
312 labelItem->setTransformOriginPoint(center.x(), center.y());
320 labelItem->setTransformOriginPoint(center.x(), center.y());
313 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
321 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
314 if(i%2){
322 if(i%2){
315 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
323 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
316 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
324 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
317 }
325 }
318 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
326 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
319 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
327 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
320 }
328 }
321 }
329 }
322 break;
330 break;
323 default:
331 default:
324 qDebug()<<"Unknown axis type";
332 qDebug()<<"Unknown axis type";
325 break;
333 break;
326 }
334 }
327 }
335 }
328
336
329 //TODO "nice numbers algorithm"
337 //TODO "nice numbers algorithm"
330 #include "moc_axisitem_p.cpp"
338 #include "moc_axisitem_p.cpp"
331
339
332 QTCOMMERCIALCHART_END_NAMESPACE
340 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,79
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class AxisItem : public QObject, public ChartItem
12 class AxisItem : public QObject, public ChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 enum AxisType{X_AXIS,Y_AXIS};
16 enum AxisType{X_AXIS,Y_AXIS};
17
17
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisItem();
19 ~AxisItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 AxisType axisType() const {return m_type;};
25 AxisType axisType() const {return m_type;};
26
26
27 void setAxisOpacity(qreal opacity);
27 void setAxisOpacity(qreal opacity);
28 qreal axisOpacity() const;
28 qreal axisOpacity() const;
29
29
30 void setGridOpacity(qreal opacity);
30 void setGridOpacity(qreal opacity);
31 qreal gridOpacity() const;
31 qreal gridOpacity() const;
32
32
33 void setLabelsOpacity(qreal opacity);
33 void setLabelsOpacity(qreal opacity);
34 qreal labelsOpacity() const;
34 qreal labelsOpacity() const;
35
35
36 void setShadesOpacity(qreal opacity);
36 void setShadesOpacity(qreal opacity);
37 qreal shadesOpacity() const;
37 qreal shadesOpacity() const;
38
38
39 void setLabelsAngle(int angle);
39 void setLabelsAngle(int angle);
40 int labelsAngle()const { return m_labelsAngle; }
40 int labelsAngle()const { return m_labelsAngle; }
41
41
42 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
44
44
45 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
47
47
48 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
49 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
50 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
51
51
52 public slots:
52 public slots:
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 public:
56 public:
57 virtual void updateItems(QVector<qreal>& points);
57 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
58 virtual void calculateLayout(QVector<qreal>& points);
58 QVector<qreal> calculateLayout() const;
59 virtual void applyLayout(const QVector<qreal>& points);
59 void applyLayout(const QVector<qreal>& points);
60
60 private:
61 private:
61 void clear(int count);
62 void clear(int count);
62 void createItems(int count);
63 void createItems(int count);
63 private:
64 private:
64 AxisType m_type;
65 AxisType m_type;
65 QRectF m_rect;
66 QRectF m_rect;
66 int m_labelsAngle;
67 int m_labelsAngle;
67 QGraphicsItemGroup m_grid;
68 QGraphicsItemGroup m_grid;
68 QGraphicsItemGroup m_shades;
69 QGraphicsItemGroup m_shades;
69 QGraphicsItemGroup m_labels;
70 QGraphicsItemGroup m_labels;
70 QGraphicsItemGroup m_axis;
71 QGraphicsItemGroup m_axis;
71 QStringList m_thicksList;
72 QStringList m_thicksList;
72 QVector<qreal> m_layoutVector;
73 QVector<qreal> m_layoutVector;
73
74
74 };
75 };
75
76
76 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
77
78
78 #endif /* AXISITEM_H_ */
79 #endif /* AXISITEM_H_ */
General Comments 0
You need to be logged in to leave comments. Login now