@@ -0,0 +1,70 | |||
|
1 | #include "axisanimationitem_p.h" | |
|
2 | #include <QPropertyAnimation> | |
|
3 | ||
|
4 | Q_DECLARE_METATYPE(QVector<qreal>) | |
|
5 | ||
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
7 | ||
|
8 | const static int duration = 500; | |
|
9 | ||
|
10 | AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) : | |
|
11 | AxisItem(type,parent) | |
|
12 | { | |
|
13 | } | |
|
14 | ||
|
15 | AxisAnimationItem::~AxisAnimationItem() | |
|
16 | { | |
|
17 | } | |
|
18 | ||
|
19 | void AxisAnimationItem::updateItems(QVector<qreal>& vector1) | |
|
20 | { | |
|
21 | QVector<qreal> vector0 = vector1; | |
|
22 | calculateLayout(vector1); | |
|
23 | if(vector1.count()==0) return; | |
|
24 | vector0.resize(vector1.size()); | |
|
25 | ||
|
26 | AxisAnimator *animation = new AxisAnimator(this); | |
|
27 | animation->setDuration(duration); | |
|
28 | animation->setEasingCurve(QEasingCurve::InOutBack); | |
|
29 | animation->setKeyValueAt(0.0, qVariantFromValue(vector0)); | |
|
30 | animation->setKeyValueAt(1.0, qVariantFromValue(vector1)); | |
|
31 | animation->start(QAbstractAnimation::DeleteWhenStopped); | |
|
32 | } | |
|
33 | ||
|
34 | void AxisAnimationItem::setLabelsAngle(int angle) | |
|
35 | { | |
|
36 | AxisItem::setLabelsAngle(angle); | |
|
37 | } | |
|
38 | ||
|
39 | AxisAnimator::AxisAnimator(AxisItem *axis): m_axis(axis) | |
|
40 | { | |
|
41 | } | |
|
42 | ||
|
43 | AxisAnimator::~AxisAnimator() | |
|
44 | { | |
|
45 | } | |
|
46 | ||
|
47 | QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const | |
|
48 | { | |
|
49 | QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start); | |
|
50 | QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end); | |
|
51 | QVector<qreal> result; | |
|
52 | Q_ASSERT(startVector.count() == endVecotr.count()); | |
|
53 | ||
|
54 | for(int i =0 ;i< startVector.count();i++){ | |
|
55 | qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0)); | |
|
56 | result << value; | |
|
57 | } | |
|
58 | return qVariantFromValue(result); | |
|
59 | } | |
|
60 | ||
|
61 | ||
|
62 | void AxisAnimator::updateCurrentValue (const QVariant & value ) | |
|
63 | { | |
|
64 | QVector<qreal> vector = qVariantValue<QVector<qreal> >(value); | |
|
65 | m_axis->applyLayout(vector); | |
|
66 | } | |
|
67 | ||
|
68 | #include "moc_axisanimationitem_p.cpp" | |
|
69 | ||
|
70 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,44 | |||
|
1 | #ifndef AXISANIMATIONITEM_H_ | |
|
2 | #define AXISANIMATIONITEM_H_ | |
|
3 | ||
|
4 | #include "domain_p.h" | |
|
5 | #include "axisitem_p.h" | |
|
6 | #include <QGraphicsItem> | |
|
7 | #include <QVariantAnimation> | |
|
8 | ||
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
10 | ||
|
11 | class QChartAxis; | |
|
12 | ||
|
13 | class AxisAnimationItem : public AxisItem | |
|
14 | { | |
|
15 | Q_OBJECT | |
|
16 | ||
|
17 | public: | |
|
18 | AxisAnimationItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); | |
|
19 | ~AxisAnimationItem(); | |
|
20 | ||
|
21 | void setLabelsAngle(int angle); | |
|
22 | ||
|
23 | protected: | |
|
24 | void updateItems(QVector<qreal>& vector); | |
|
25 | ||
|
26 | }; | |
|
27 | ||
|
28 | class AxisAnimator: public QVariantAnimation | |
|
29 | { | |
|
30 | public: | |
|
31 | AxisAnimator(AxisItem *axis); | |
|
32 | virtual ~AxisAnimator(); | |
|
33 | protected: | |
|
34 | virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const; | |
|
35 | virtual void updateCurrentValue (const QVariant & value ); | |
|
36 | private: | |
|
37 | AxisItem* m_axis; | |
|
38 | }; | |
|
39 | ||
|
40 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
41 | ||
|
42 | ||
|
43 | ||
|
44 | #endif /* AXISITEM_H_ */ |
@@ -30,36 +30,33 AxisItem::~AxisItem() | |||
|
30 | 30 | |
|
31 | 31 | QRectF AxisItem::boundingRect() const |
|
32 | 32 | { |
|
33 |
return |
|
|
33 | return QRectF(); | |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | void AxisItem::createItems(int count) |
|
37 | 37 | { |
|
38 | m_axis.addToGroup(new QGraphicsLineItem(this)); | |
|
38 | if(m_axis.children().size()==0) | |
|
39 | m_axis.addToGroup(new QGraphicsLineItem()); | |
|
39 | 40 | for (int i = 0; i < count; ++i) { |
|
40 |
m_grid.addToGroup(new QGraphicsLineItem( |
|
|
41 |
m_labels.addToGroup(new QGraphicsSimpleTextItem( |
|
|
42 |
if(i%2) m_shades.addToGroup(new QGraphicsRectItem( |
|
|
43 |
m_axis.addToGroup(new QGraphicsLineItem( |
|
|
41 | m_grid.addToGroup(new QGraphicsLineItem()); | |
|
42 | m_labels.addToGroup(new QGraphicsSimpleTextItem()); | |
|
43 | if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem()); | |
|
44 | m_axis.addToGroup(new QGraphicsLineItem()); | |
|
44 | 45 | } |
|
45 | 46 | } |
|
46 | 47 | |
|
47 | void AxisItem::clear() | |
|
48 | void AxisItem::clear(int count) | |
|
48 | 49 | { |
|
49 |
|
|
|
50 | delete item; | |
|
51 | } | |
|
52 | ||
|
53 | foreach(QGraphicsItem* item , m_grid.childItems()) { | |
|
54 | delete item; | |
|
55 | } | |
|
56 | ||
|
57 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
|
58 | delete item; | |
|
59 | } | |
|
50 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
|
51 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
|
52 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
|
53 | QList<QGraphicsItem *> axis = m_axis.childItems(); | |
|
60 | 54 | |
|
61 | foreach(QGraphicsItem* item , m_axis.childItems()) { | |
|
62 |
|
|
|
55 | for (int i = 0; i < count; ++i) { | |
|
56 | delete(lines.takeLast()); | |
|
57 | delete(labels.takeLast()); | |
|
58 | if(lines.size()%2) delete(shades.takeLast()); | |
|
59 | delete(axis.takeLast()); | |
|
63 | 60 | } |
|
64 | 61 | |
|
65 | 62 | m_thicksList.clear(); |
@@ -68,75 +65,16 void AxisItem::clear() | |||
|
68 | 65 | |
|
69 | 66 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
70 | 67 | { |
|
71 | ||
|
68 | Q_UNUSED(painter); | |
|
69 | Q_UNUSED(option); | |
|
70 | Q_UNUSED(widget); | |
|
72 | 71 | } |
|
73 | 72 | |
|
74 |
void AxisItem::updateItem( |
|
|
73 | void AxisItem::updateItems(QVector<qreal>& vector) | |
|
75 | 74 | { |
|
76 | if(count ==0) return; | |
|
77 | ||
|
78 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
|
79 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
|
80 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
|
81 | QList<QGraphicsItem *> axis = m_axis.childItems(); | |
|
82 | ||
|
83 | switch (m_type) | |
|
84 | { | |
|
85 | case X_AXIS: | |
|
86 | { | |
|
87 | const qreal deltaX = m_rect.width() / (count-1); | |
|
88 | ||
|
89 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
|
90 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
|
91 | ||
|
92 | for (int i = 0; i < count; ++i) { | |
|
93 | int x = i * deltaX + m_rect.left(); | |
|
94 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
|
95 | lineItem->setLine(x, m_rect.top(), x, m_rect.bottom()); | |
|
96 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
|
97 | labelItem->setText(m_thicksList.at(i)); | |
|
98 | QPointF center = labelItem->boundingRect().center(); | |
|
99 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
100 | labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); | |
|
101 | if(i%2){ | |
|
102 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
|
103 | rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); | |
|
104 | } | |
|
105 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
|
106 | lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5); | |
|
107 | } | |
|
108 | } | |
|
109 | break; | |
|
110 | ||
|
111 | case Y_AXIS: | |
|
112 | { | |
|
113 | const qreal deltaY = m_rect.height()/ (count-1); | |
|
114 | ||
|
115 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
|
116 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
|
117 | ||
|
118 | for (int i = 0; i < count; ++i) { | |
|
119 | int y = i * -deltaY + m_rect.bottom(); | |
|
120 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
|
121 | lineItem->setLine(m_rect.left() , y, m_rect.right(), y); | |
|
122 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
|
123 | labelItem->setText(m_thicksList.at(i)); | |
|
124 | QPointF center = labelItem->boundingRect().center(); | |
|
125 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
126 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y()); | |
|
127 | if(i%2){ | |
|
128 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
|
129 | rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); | |
|
130 | } | |
|
131 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
|
132 | lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y); | |
|
133 | } | |
|
134 | } | |
|
135 | break; | |
|
136 | default: | |
|
137 | qDebug()<<"Unknown axis type"; | |
|
138 | break; | |
|
139 | } | |
|
75 | calculateLayout(vector); | |
|
76 | if(vector.count()==0) return; | |
|
77 | applyLayout(vector); | |
|
140 | 78 | } |
|
141 | 79 | |
|
142 | 80 | void AxisItem::handleAxisUpdate(QChartAxis* axis) |
@@ -182,32 +120,23 void AxisItem::handleAxisUpdate(QChartAxis* axis) | |||
|
182 | 120 | |
|
183 | 121 | void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
184 | 122 | { |
|
185 |
m_thicksList |
|
|
186 | QList<QGraphicsItem*> items = m_labels.childItems(); | |
|
187 | //if(items.size()!=m_thicksList.size()){ | |
|
188 | clear(); | |
|
189 | m_thicksList=labels; | |
|
190 |
createItems( |
|
|
191 | updateItem(m_thicksList.size()); | |
|
192 | items = m_labels.childItems(); | |
|
193 | handleAxisUpdate(axis); | |
|
194 | // } | |
|
195 | ||
|
196 | Q_ASSERT(items.size()==m_thicksList.size()); | |
|
197 | ||
|
198 | int i=0; | |
|
199 | foreach(QGraphicsItem* item, items){ | |
|
200 | static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i)); | |
|
201 | i++; | |
|
123 | int diff = m_thicksList.size() - labels.size(); | |
|
124 | ||
|
125 | if(diff>0){ | |
|
126 | clear(diff); | |
|
127 | }else if(diff<0){ | |
|
128 | createItems(-diff); | |
|
202 | 129 | } |
|
203 | update(); | |
|
130 | m_thicksList=labels; | |
|
131 | m_layoutVector.resize(m_thicksList.size()); | |
|
132 | updateItems(m_layoutVector); | |
|
133 | handleAxisUpdate(axis); | |
|
204 | 134 | } |
|
205 | 135 | |
|
206 | 136 | void AxisItem::handleGeometryChanged(const QRectF& rect) |
|
207 | 137 | { |
|
208 | 138 | m_rect = rect; |
|
209 |
updateItem(m_ |
|
|
210 | update(); | |
|
139 | updateItems(m_layoutVector); | |
|
211 | 140 | } |
|
212 | 141 | |
|
213 | 142 | void AxisItem::setAxisOpacity(qreal opacity) |
@@ -309,6 +238,94 void AxisItem::setGridPen(const QPen& pen) | |||
|
309 | 238 | } |
|
310 | 239 | } |
|
311 | 240 | |
|
241 | void AxisItem::calculateLayout(QVector<qreal>& points) | |
|
242 | { | |
|
243 | switch (m_type) | |
|
244 | { | |
|
245 | case X_AXIS: | |
|
246 | { | |
|
247 | const qreal deltaX = m_rect.width()/(m_thicksList.size()-1); | |
|
248 | for (int i = 0; i < m_thicksList.size(); ++i) { | |
|
249 | int x = i * deltaX + m_rect.left(); | |
|
250 | points[i]=x; | |
|
251 | } | |
|
252 | } | |
|
253 | break; | |
|
254 | case Y_AXIS: | |
|
255 | { | |
|
256 | const qreal deltaY = m_rect.height()/(m_thicksList.size()-1); | |
|
257 | for (int i = 0; i < m_thicksList.size(); ++i) { | |
|
258 | int y = i * -deltaY + m_rect.bottom(); | |
|
259 | points[i]=y; | |
|
260 | } | |
|
261 | } | |
|
262 | break; | |
|
263 | } | |
|
264 | } | |
|
265 | ||
|
266 | void AxisItem::applyLayout(const QVector<qreal>& points) | |
|
267 | { | |
|
268 | Q_ASSERT(points.size() == m_thicksList.size()); | |
|
269 | ||
|
270 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
|
271 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
|
272 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
|
273 | QList<QGraphicsItem *> axis = m_axis.childItems(); | |
|
274 | ||
|
275 | Q_ASSERT(labels.size() == m_thicksList.size()); | |
|
276 | ||
|
277 | switch (m_type) | |
|
278 | { | |
|
279 | case X_AXIS: | |
|
280 | { | |
|
281 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
|
282 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
|
283 | ||
|
284 | for (int i = 0; i < points.size(); ++i) { | |
|
285 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
|
286 | lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom()); | |
|
287 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
|
288 | labelItem->setText(m_thicksList.at(i)); | |
|
289 | QPointF center = labelItem->boundingRect().center(); | |
|
290 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
291 | labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding); | |
|
292 | if(i%2){ | |
|
293 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
|
294 | rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height()); | |
|
295 | } | |
|
296 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
|
297 | lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5); | |
|
298 | } | |
|
299 | } | |
|
300 | break; | |
|
301 | ||
|
302 | case Y_AXIS: | |
|
303 | { | |
|
304 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
|
305 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
|
306 | ||
|
307 | for (int i = 0; i < points.size(); ++i) { | |
|
308 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
|
309 | lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]); | |
|
310 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
|
311 | labelItem->setText(m_thicksList.at(i)); | |
|
312 | QPointF center = labelItem->boundingRect().center(); | |
|
313 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
314 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y()); | |
|
315 | if(i%2){ | |
|
316 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
|
317 | rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]); | |
|
318 | } | |
|
319 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
|
320 | lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]); | |
|
321 | } | |
|
322 | } | |
|
323 | break; | |
|
324 | default: | |
|
325 | qDebug()<<"Unknown axis type"; | |
|
326 | break; | |
|
327 | } | |
|
328 | } | |
|
312 | 329 | |
|
313 | 330 | //TODO "nice numbers algorithm" |
|
314 | 331 | #include "moc_axisitem_p.cpp" |
@@ -53,10 +53,12 public slots: | |||
|
53 | 53 | void handleAxisUpdate(QChartAxis* axis); //look and feel |
|
54 | 54 | void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset |
|
55 | 55 | void handleGeometryChanged(const QRectF& size); // geometry from presenter |
|
56 | protected: | |
|
57 | void updateItem(int count); | |
|
56 | public: | |
|
57 | virtual void updateItems(QVector<qreal>& points); | |
|
58 | virtual void calculateLayout(QVector<qreal>& points); | |
|
59 | virtual void applyLayout(const QVector<qreal>& points); | |
|
58 | 60 | private: |
|
59 | void clear(); | |
|
61 | void clear(int count); | |
|
60 | 62 | void createItems(int count); |
|
61 | 63 | private: |
|
62 | 64 | AxisType m_type; |
@@ -67,6 +69,7 private: | |||
|
67 | 69 | QGraphicsItemGroup m_labels; |
|
68 | 70 | QGraphicsItemGroup m_axis; |
|
69 | 71 | QStringList m_thicksList; |
|
72 | QVector<qreal> m_layoutVector; | |
|
70 | 73 | |
|
71 | 74 | }; |
|
72 | 75 |
@@ -8,6 +8,7 CONFIG += debug_and_release | |||
|
8 | 8 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
9 | 9 | SOURCES += \ |
|
10 | 10 | axisitem.cpp \ |
|
11 | axisanimationitem.cpp \ | |
|
11 | 12 | chartdataset.cpp \ |
|
12 | 13 | chartpresenter.cpp \ |
|
13 | 14 | charttheme.cpp \ |
@@ -18,6 +19,7 SOURCES += \ | |||
|
18 | 19 | qchartview.cpp |
|
19 | 20 | PRIVATE_HEADERS += \ |
|
20 | 21 | axisitem_p.h \ |
|
22 | axisanimationitem_p.h \ | |
|
21 | 23 | chartdataset_p.h \ |
|
22 | 24 | chartitem_p.h \ |
|
23 | 25 | chartpresenter_p.h \ |
General Comments 0
You need to be logged in to leave comments.
Login now