@@ -1,261 +1,261 | |||
|
1 | 1 | #include "chartanimator_p.h" |
|
2 | 2 | #include "axisanimation_p.h" |
|
3 | 3 | #include "xyanimation_p.h" |
|
4 | 4 | #include "splineanimation_p.h" |
|
5 | 5 | #include "xychartitem_p.h" |
|
6 | 6 | #include "pieanimation_p.h" |
|
7 | 7 | #include "areachartitem_p.h" |
|
8 | 8 | #include "splinechartitem_p.h" |
|
9 | 9 | #include "scatterchartitem_p.h" |
|
10 | 10 | #include <QTimer> |
|
11 | 11 | |
|
12 | 12 | Q_DECLARE_METATYPE(QVector<QPointF>) |
|
13 | 13 | Q_DECLARE_METATYPE(QVector<qreal>) |
|
14 | 14 | |
|
15 | 15 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
16 | 16 | |
|
17 | 17 | const static int duration = 1000; |
|
18 | 18 | |
|
19 | 19 | ChartAnimator::ChartAnimator(QObject *parent):QObject(parent) |
|
20 | 20 | { |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | ChartAnimator::~ChartAnimator() |
|
24 | 24 | { |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | void ChartAnimator::addAnimation(AxisItem* item) |
|
28 | 28 | { |
|
29 | 29 | ChartAnimation* animation = m_animations.value(item); |
|
30 | 30 | |
|
31 | 31 | if(!animation) { |
|
32 | 32 | animation = new AxisAnimation(item); |
|
33 | 33 | m_animations.insert(item,animation); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | item->setAnimator(this); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | void ChartAnimator::addAnimation(SplineChartItem* item) |
|
40 | 40 | { |
|
41 | 41 | ChartAnimation* animation = m_animations.value(item); |
|
42 | 42 | |
|
43 | 43 | if(!animation) { |
|
44 | 44 | animation = new SplineAnimation(item); |
|
45 | 45 | m_animations.insert(item,animation); |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | item->setAnimator(this); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void ChartAnimator::addAnimation(ScatterChartItem* item) |
|
52 | 52 | { |
|
53 | 53 | ChartAnimation* animation = m_animations.value(item); |
|
54 | 54 | |
|
55 | 55 | if(!animation) { |
|
56 | 56 | animation = new XYAnimation(item); |
|
57 | 57 | m_animations.insert(item,animation); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | item->setAnimator(this); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | void ChartAnimator::addAnimation(LineChartItem* item) |
|
64 | 64 | { |
|
65 | 65 | ChartAnimation* animation = m_animations.value(item); |
|
66 | 66 | |
|
67 | 67 | if(!animation) { |
|
68 | 68 | animation = new XYAnimation(item); |
|
69 | 69 | m_animations.insert(item,animation); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | item->setAnimator(this); |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | void ChartAnimator::addAnimation(PieChartItem* item) |
|
76 | 76 | { |
|
77 | 77 | ChartAnimation* animation = m_animations.value(item); |
|
78 | 78 | |
|
79 | 79 | if(!animation) { |
|
80 | 80 | animation = new PieAnimation(item); |
|
81 | 81 | m_animations.insert(item,animation); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | item->setAnimator(this); |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | void ChartAnimator::removeAnimation(ChartItem* item) |
|
88 | 88 | { |
|
89 | 89 | item->setAnimator(0); |
|
90 | 90 | m_animations.remove(item); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 |
void ChartAnimator:: |
|
|
93 | void ChartAnimator::updateLayout(AxisItem* item , QVector<qreal>& newLayout) | |
|
94 | 94 | { |
|
95 | 95 | AxisAnimation* animation = static_cast<AxisAnimation*>(m_animations.value(item)); |
|
96 | 96 | |
|
97 | 97 | Q_ASSERT(animation); |
|
98 | 98 | |
|
99 | 99 | QVector<qreal> oldLayout = item->layout(); |
|
100 | 100 | |
|
101 | 101 | if(newLayout.count()==0) return; |
|
102 | 102 | |
|
103 | 103 | switch(m_state) |
|
104 | 104 | { |
|
105 | 105 | case ZoomOutState: { |
|
106 | 106 | QRectF rect = item->geometry(); |
|
107 | 107 | oldLayout.resize(newLayout.count()); |
|
108 | 108 | |
|
109 | 109 | for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--) |
|
110 | 110 | { |
|
111 | 111 | oldLayout[i]= item->axisType()==AxisItem::X_AXIS?rect.left():rect.bottom(); |
|
112 | 112 | oldLayout[j]= item->axisType()==AxisItem::X_AXIS?rect.right():rect.top(); |
|
113 | 113 | } |
|
114 | 114 | } |
|
115 | 115 | break; |
|
116 | 116 | case ZoomInState: { |
|
117 | 117 | int index = qMin(oldLayout.count()*(item->axisType()==AxisItem::X_AXIS?m_point.x():(1 -m_point.y())),newLayout.count()-1.0); |
|
118 | 118 | oldLayout.resize(newLayout.count()); |
|
119 | 119 | |
|
120 | 120 | for(int i=0;i<oldLayout.count();i++) |
|
121 | 121 | { |
|
122 | 122 | oldLayout[i]= oldLayout[index]; |
|
123 | 123 | } |
|
124 | 124 | } |
|
125 | 125 | break; |
|
126 | 126 | case ScrollDownState: |
|
127 | 127 | case ScrollRightState: { |
|
128 | 128 | oldLayout.resize(newLayout.count()); |
|
129 | 129 | |
|
130 | 130 | for(int i=0, j=i+1;i<oldLayout.count()-1;i++,j++) |
|
131 | 131 | { |
|
132 | 132 | oldLayout[i]= oldLayout[j]; |
|
133 | 133 | } |
|
134 | 134 | } |
|
135 | 135 | break; |
|
136 | 136 | case ScrollUpState: |
|
137 | 137 | case ScrollLeftState: { |
|
138 | 138 | oldLayout.resize(newLayout.count()); |
|
139 | 139 | |
|
140 | 140 | for(int i=oldLayout.count()-1, j=i-1;i>0;i--,j--) |
|
141 | 141 | { |
|
142 | 142 | oldLayout[i]= oldLayout[j]; |
|
143 | 143 | } |
|
144 | 144 | } |
|
145 | 145 | break; |
|
146 | 146 | default: { |
|
147 | 147 | oldLayout.resize(newLayout.count()); |
|
148 | 148 | QRectF rect = item->geometry(); |
|
149 | 149 | for(int i=0, j=oldLayout.count()-1;i<oldLayout.count();i++,j--) |
|
150 | 150 | { |
|
151 | 151 | oldLayout[i]= item->axisType()==AxisItem::X_AXIS?rect.left():rect.top(); |
|
152 | 152 | } |
|
153 | 153 | } |
|
154 | 154 | break; |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
159 | 159 | animation->stop(); |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | animation->setDuration(duration); |
|
163 | 163 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
164 | 164 | QVariantAnimation::KeyValues value; |
|
165 | 165 | animation->setKeyValues(value); //workaround for wrong interpolation call |
|
166 | 166 | animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); |
|
167 | 167 | animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); |
|
168 | 168 | |
|
169 | 169 | QTimer::singleShot(0,animation,SLOT(start())); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void ChartAnimator::updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints ,QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newControlPoints,int index) |
|
173 | 173 | { |
|
174 | 174 | SplineAnimation* animation = static_cast<SplineAnimation*>(m_animations.value(item)); |
|
175 | 175 | |
|
176 | 176 | Q_ASSERT(animation); |
|
177 | 177 | |
|
178 | 178 | if(newPoints.count()<2 || newControlPoints.count()<2) return; |
|
179 | 179 | |
|
180 | 180 | bool empty = oldPoints.count()==0; |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
184 | 184 | animation->stop(); |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | animation->setDuration(duration); |
|
188 | 188 | if(!empty) |
|
189 | 189 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); |
|
190 | 190 | else |
|
191 | 191 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); |
|
192 | 192 | |
|
193 | 193 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
194 | 194 | animation->setValues(oldPoints,newPoints,oldControlPoints,newControlPoints,index); |
|
195 | 195 | |
|
196 | 196 | QTimer::singleShot(0,animation,SLOT(start())); |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, int index) |
|
201 | 201 | { |
|
202 | 202 | XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item)); |
|
203 | 203 | |
|
204 | 204 | Q_ASSERT(animation); |
|
205 | 205 | |
|
206 | 206 | if(newPoints.count()==0) return; |
|
207 | 207 | |
|
208 | 208 | bool empty = oldPoints.count()==0; |
|
209 | 209 | |
|
210 | 210 | |
|
211 | 211 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
212 | 212 | animation->stop(); |
|
213 | 213 | } |
|
214 | 214 | |
|
215 | 215 | animation->setDuration(duration); |
|
216 | 216 | if(!empty) |
|
217 | 217 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); |
|
218 | 218 | else |
|
219 | 219 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); |
|
220 | 220 | |
|
221 | 221 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
222 | 222 | animation->setValues(oldPoints,newPoints,index); |
|
223 | 223 | |
|
224 | 224 | QTimer::singleShot(0,animation,SLOT(start())); |
|
225 | 225 | } |
|
226 | 226 | |
|
227 | 227 | void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout, bool isEmpty) |
|
228 | 228 | { |
|
229 | 229 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
230 | 230 | Q_ASSERT(animation); |
|
231 | 231 | animation->addSlice(slice, layout, isEmpty); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice) |
|
235 | 235 | { |
|
236 | 236 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
237 | 237 | Q_ASSERT(animation); |
|
238 | 238 | animation->removeSlice(slice); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout) |
|
242 | 242 | { |
|
243 | 243 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
244 | 244 | Q_ASSERT(animation); |
|
245 | 245 | animation->updateValues(layout); |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout) |
|
249 | 249 | { |
|
250 | 250 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
251 | 251 | Q_ASSERT(animation); |
|
252 | 252 | animation->updateValue(slice, layout); |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | void ChartAnimator::setState(State state,const QPointF& point) |
|
256 | 256 | { |
|
257 | 257 | m_state=state; |
|
258 | 258 | m_point=point; |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,53 +1,53 | |||
|
1 | 1 | #ifndef CHARTANIMATOR_P_H_ |
|
2 | 2 | #define CHARTANIMATOR_P_H_ |
|
3 | 3 | #include "qchartglobal.h" |
|
4 | 4 | #include "chartanimation_p.h" |
|
5 | 5 | #include "piechartitem_p.h" |
|
6 | 6 | #include <QPointF> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class ChartItem; |
|
11 | 11 | class AxisItem; |
|
12 | 12 | class AreaChartItem; |
|
13 | 13 | class SplineChartItem; |
|
14 | 14 | class ScatterChartItem; |
|
15 | 15 | class LineChartItem; |
|
16 | 16 | class XYChartItem; |
|
17 | 17 | |
|
18 | 18 | class ChartAnimator : public QObject { |
|
19 | 19 | |
|
20 | 20 | public: |
|
21 | 21 | //TODO: this should be flags in case of two state at the time |
|
22 | 22 | enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState,ScrollRightState,ZoomInState,ZoomOutState}; |
|
23 | 23 | ChartAnimator(QObject *parent = 0); |
|
24 | 24 | virtual ~ChartAnimator(); |
|
25 | 25 | |
|
26 | 26 | void addAnimation(AxisItem* item); |
|
27 | 27 | void addAnimation(PieChartItem* item); |
|
28 | 28 | void addAnimation(ScatterChartItem* item); |
|
29 | 29 | void addAnimation(LineChartItem* item); |
|
30 | 30 | void addAnimation(SplineChartItem* item); |
|
31 | 31 | void removeAnimation(ChartItem* item); |
|
32 | 32 | |
|
33 | 33 | void animationStarted(); |
|
34 | 34 | void updateLayout(XYChartItem* item, QVector<QPointF>& oldLayout,QVector<QPointF>& newLayout,int index); |
|
35 | 35 | void updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newContorlPoints,int index); |
|
36 |
void |
|
|
36 | void updateLayout(AxisItem* item, QVector<qreal>& layout); | |
|
37 | 37 | |
|
38 | 38 | void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout, bool isEmpty); |
|
39 | 39 | void removeAnimation(PieChartItem* item, QPieSlice *slice); |
|
40 | 40 | void updateLayout(PieChartItem* item, const PieLayout &layout); |
|
41 | 41 | void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout); |
|
42 | 42 | |
|
43 | 43 | void setState(State state,const QPointF& point = QPointF()); |
|
44 | 44 | |
|
45 | 45 | private: |
|
46 | 46 | QMap<ChartItem*,ChartAnimation*> m_animations; |
|
47 | 47 | State m_state; |
|
48 | 48 | QPointF m_point; |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | QTCOMMERCIALCHART_END_NAMESPACE |
|
52 | 52 | |
|
53 | 53 | #endif |
@@ -1,406 +1,406 | |||
|
1 | 1 | #include "axisitem_p.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartanimator_p.h" |
|
5 | 5 | #include <QPainter> |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | |
|
8 | 8 | static int label_padding = 5; |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | AxisItem::AxisItem(QChartAxis* axis,ChartPresenter* presenter,AxisType type,QGraphicsItem* parent) : |
|
13 | 13 | ChartItem(parent), |
|
14 | 14 | m_presenter(presenter), |
|
15 | 15 | m_chartAxis(axis), |
|
16 | 16 | m_type(type), |
|
17 | 17 | m_labelsAngle(0), |
|
18 | 18 | m_grid(parent), |
|
19 | 19 | m_shades(parent), |
|
20 | 20 | m_labels(parent), |
|
21 | 21 | m_axis(parent), |
|
22 | 22 | m_min(0), |
|
23 | 23 | m_max(0), |
|
24 | 24 | m_ticksCount(0) |
|
25 | 25 | { |
|
26 | 26 | //initial initialization |
|
27 | 27 | m_axis.setZValue(ChartPresenter::AxisZValue); |
|
28 | 28 | m_shades.setZValue(ChartPresenter::ShadesZValue); |
|
29 | 29 | m_grid.setZValue(ChartPresenter::GridZValue); |
|
30 | 30 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
31 | 31 | |
|
32 | 32 | QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); |
|
33 | 33 | QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); |
|
34 | 34 | |
|
35 | 35 | handleAxisUpdated(); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | AxisItem::~AxisItem() |
|
39 | 39 | { |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QRectF AxisItem::boundingRect() const |
|
43 | 43 | { |
|
44 | 44 | return QRectF(); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void AxisItem::createItems(int count) |
|
48 | 48 | { |
|
49 | 49 | |
|
50 | 50 | if(m_axis.children().size()==0) |
|
51 | 51 | m_axis.addToGroup(new QGraphicsLineItem()); |
|
52 | 52 | for (int i = 0; i < count; ++i) { |
|
53 | 53 | m_grid.addToGroup(new QGraphicsLineItem()); |
|
54 | 54 | m_labels.addToGroup(new QGraphicsSimpleTextItem()); |
|
55 | 55 | m_axis.addToGroup(new QGraphicsLineItem()); |
|
56 | 56 | if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem()); |
|
57 | 57 | } |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | void AxisItem::deleteItems(int count) |
|
61 | 61 | { |
|
62 | 62 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
63 | 63 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
64 | 64 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
65 | 65 | QList<QGraphicsItem *> axis = m_axis.childItems(); |
|
66 | 66 | |
|
67 | 67 | for (int i = 0; i < count; ++i) { |
|
68 | 68 | if(lines.size()%2 && lines.size()>1) delete(shades.takeLast()); |
|
69 | 69 | delete(lines.takeLast()); |
|
70 | 70 | delete(labels.takeLast()); |
|
71 | 71 | delete(axis.takeLast()); |
|
72 | 72 | } |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | void AxisItem::updateLayout(QVector<qreal>& layout) |
|
76 | 76 | { |
|
77 | 77 | if(m_animator){ |
|
78 |
m_animator-> |
|
|
78 | m_animator->updateLayout(this,layout); | |
|
79 | 79 | } |
|
80 | 80 | else setLayout(layout); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const |
|
84 | 84 | { |
|
85 | 85 | Q_ASSERT(max>=min); |
|
86 | 86 | Q_ASSERT(ticks>0); |
|
87 | 87 | |
|
88 | 88 | QStringList labels; |
|
89 | 89 | |
|
90 | 90 | QChartAxisCategories* categories = m_chartAxis->categories(); |
|
91 | 91 | |
|
92 | 92 | for(int i=0; i< ticks; i++) { |
|
93 | 93 | qreal value = min + (i * (max - min)/ (ticks-1)); |
|
94 | 94 | if(categories->count()==0) { |
|
95 | 95 | labels << QString::number(value); |
|
96 | 96 | } |
|
97 | 97 | else { |
|
98 | 98 | |
|
99 | 99 | QString label = categories->label(value); |
|
100 | 100 | labels << label; |
|
101 | 101 | } |
|
102 | 102 | } |
|
103 | 103 | return labels; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | void AxisItem::setAxisOpacity(qreal opacity) |
|
107 | 107 | { |
|
108 | 108 | m_axis.setOpacity(opacity); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | qreal AxisItem::axisOpacity() const |
|
112 | 112 | { |
|
113 | 113 | return m_axis.opacity(); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | void AxisItem::setGridOpacity(qreal opacity) |
|
117 | 117 | { |
|
118 | 118 | m_grid.setOpacity(opacity); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | qreal AxisItem::gridOpacity() const |
|
122 | 122 | { |
|
123 | 123 | return m_grid.opacity(); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void AxisItem::setLabelsOpacity(qreal opacity) |
|
127 | 127 | { |
|
128 | 128 | m_labels.setOpacity(opacity); |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | qreal AxisItem::labelsOpacity() const |
|
132 | 132 | { |
|
133 | 133 | return m_labels.opacity(); |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void AxisItem::setShadesOpacity(qreal opacity) |
|
137 | 137 | { |
|
138 | 138 | m_shades.setOpacity(opacity); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | qreal AxisItem::shadesOpacity() const |
|
142 | 142 | { |
|
143 | 143 | return m_shades.opacity(); |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | void AxisItem::setLabelsAngle(int angle) |
|
147 | 147 | { |
|
148 | 148 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
149 | 149 | QPointF center = item->boundingRect().center(); |
|
150 | 150 | item->setRotation(angle); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | m_labelsAngle=angle; |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | void AxisItem::setLabelsPen(const QPen& pen) |
|
157 | 157 | { |
|
158 | 158 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
159 | 159 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
160 | 160 | } |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | void AxisItem::setLabelsBrush(const QBrush& brush) |
|
164 | 164 | { |
|
165 | 165 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
166 | 166 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
167 | 167 | } |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | void AxisItem::setLabelsFont(const QFont& font) |
|
171 | 171 | { |
|
172 | 172 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
173 | 173 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
174 | 174 | } |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | void AxisItem::setShadesBrush(const QBrush& brush) |
|
178 | 178 | { |
|
179 | 179 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
180 | 180 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
181 | 181 | } |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | void AxisItem::setShadesPen(const QPen& pen) |
|
185 | 185 | { |
|
186 | 186 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
187 | 187 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
188 | 188 | } |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | void AxisItem::setAxisPen(const QPen& pen) |
|
192 | 192 | { |
|
193 | 193 | foreach(QGraphicsItem* item , m_axis.childItems()) { |
|
194 | 194 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
195 | 195 | } |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | void AxisItem::setGridPen(const QPen& pen) |
|
199 | 199 | { |
|
200 | 200 | foreach(QGraphicsItem* item , m_grid.childItems()) { |
|
201 | 201 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
202 | 202 | } |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | QVector<qreal> AxisItem::calculateLayout() const |
|
206 | 206 | { |
|
207 | 207 | Q_ASSERT(m_ticksCount>=2); |
|
208 | 208 | |
|
209 | 209 | QVector<qreal> points; |
|
210 | 210 | points.resize(m_ticksCount); |
|
211 | 211 | |
|
212 | 212 | switch (m_type) |
|
213 | 213 | { |
|
214 | 214 | case X_AXIS: |
|
215 | 215 | { |
|
216 | 216 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); |
|
217 | 217 | for (int i = 0; i < m_ticksCount; ++i) { |
|
218 | 218 | int x = i * deltaX + m_rect.left(); |
|
219 | 219 | points[i] = x; |
|
220 | 220 | } |
|
221 | 221 | } |
|
222 | 222 | break; |
|
223 | 223 | case Y_AXIS: |
|
224 | 224 | { |
|
225 | 225 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); |
|
226 | 226 | for (int i = 0; i < m_ticksCount; ++i) { |
|
227 | 227 | int y = i * -deltaY + m_rect.bottom(); |
|
228 | 228 | points[i] = y; |
|
229 | 229 | } |
|
230 | 230 | } |
|
231 | 231 | break; |
|
232 | 232 | } |
|
233 | 233 | return points; |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | void AxisItem::setLayout(QVector<qreal>& layout) |
|
237 | 237 | { |
|
238 | 238 | int diff = m_layoutVector.size() - layout.size(); |
|
239 | 239 | |
|
240 | 240 | if(diff>0) { |
|
241 | 241 | deleteItems(diff); |
|
242 | 242 | } |
|
243 | 243 | else if(diff<0) { |
|
244 | 244 | createItems(-diff); |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | if(diff!=0) handleAxisUpdated(); |
|
248 | 248 | |
|
249 | 249 | QStringList ticksList = createLabels(layout.size(),m_min,m_max); |
|
250 | 250 | |
|
251 | 251 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
252 | 252 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
253 | 253 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
254 | 254 | QList<QGraphicsItem *> axis = m_axis.childItems(); |
|
255 | 255 | |
|
256 | 256 | Q_ASSERT(labels.size() == ticksList.size()); |
|
257 | 257 | Q_ASSERT(layout.size() == ticksList.size()); |
|
258 | 258 | |
|
259 | 259 | switch (m_type) |
|
260 | 260 | { |
|
261 | 261 | case X_AXIS: |
|
262 | 262 | { |
|
263 | 263 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
264 | 264 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
265 | 265 | |
|
266 | 266 | for (int i = 0; i < layout.size(); ++i) { |
|
267 | 267 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
268 | 268 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
269 | 269 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
270 | 270 | labelItem->setText(ticksList.at(i)); |
|
271 | 271 | QPointF center = labelItem->boundingRect().center(); |
|
272 | 272 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
273 | 273 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
274 | 274 | if((i+1)%2 && i>1) { |
|
275 | 275 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
276 | 276 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
|
277 | 277 | } |
|
278 | 278 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
279 | 279 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); |
|
280 | 280 | } |
|
281 | 281 | } |
|
282 | 282 | break; |
|
283 | 283 | |
|
284 | 284 | case Y_AXIS: |
|
285 | 285 | { |
|
286 | 286 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
287 | 287 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
288 | 288 | |
|
289 | 289 | for (int i = 0; i < layout.size(); ++i) { |
|
290 | 290 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
291 | 291 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); |
|
292 | 292 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
293 | 293 | labelItem->setText(ticksList.at(i)); |
|
294 | 294 | QPointF center = labelItem->boundingRect().center(); |
|
295 | 295 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
296 | 296 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); |
|
297 | 297 | if((i+1)%2 && i>1) { |
|
298 | 298 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
299 | 299 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
300 | 300 | } |
|
301 | 301 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
302 | 302 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); |
|
303 | 303 | } |
|
304 | 304 | } |
|
305 | 305 | break; |
|
306 | 306 | default: |
|
307 | 307 | qDebug()<<"Unknown axis type"; |
|
308 | 308 | break; |
|
309 | 309 | } |
|
310 | 310 | |
|
311 | 311 | m_layoutVector=layout; |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | bool AxisItem::isEmpty() |
|
315 | 315 | { |
|
316 | 316 | return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0; |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | //handlers |
|
320 | 320 | |
|
321 | 321 | void AxisItem::handleAxisCategoriesUpdated() |
|
322 | 322 | { |
|
323 | 323 | if(isEmpty()) return; |
|
324 | 324 | updateLayout(m_layoutVector); |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | void AxisItem::handleAxisUpdated() |
|
328 | 328 | { |
|
329 | 329 | |
|
330 | 330 | if(isEmpty()) return; |
|
331 | 331 | |
|
332 | 332 | if(m_chartAxis->isAxisVisible()) { |
|
333 | 333 | setAxisOpacity(100); |
|
334 | 334 | } |
|
335 | 335 | else { |
|
336 | 336 | setAxisOpacity(0); |
|
337 | 337 | } |
|
338 | 338 | |
|
339 | 339 | if(m_chartAxis->isGridLineVisible()) { |
|
340 | 340 | setGridOpacity(100); |
|
341 | 341 | } |
|
342 | 342 | else { |
|
343 | 343 | setGridOpacity(0); |
|
344 | 344 | } |
|
345 | 345 | |
|
346 | 346 | if(m_chartAxis->labelsVisible()) |
|
347 | 347 | { |
|
348 | 348 | setLabelsOpacity(100); |
|
349 | 349 | } |
|
350 | 350 | else { |
|
351 | 351 | setLabelsOpacity(0); |
|
352 | 352 | } |
|
353 | 353 | |
|
354 | 354 | if(m_chartAxis->shadesVisible()) { |
|
355 | 355 | setShadesOpacity(m_chartAxis->shadesOpacity()); |
|
356 | 356 | } |
|
357 | 357 | else { |
|
358 | 358 | setShadesOpacity(0); |
|
359 | 359 | } |
|
360 | 360 | |
|
361 | 361 | setLabelsAngle(m_chartAxis->labelsAngle()); |
|
362 | 362 | setAxisPen(m_chartAxis->axisPen()); |
|
363 | 363 | setLabelsPen(m_chartAxis->labelsPen()); |
|
364 | 364 | setLabelsBrush(m_chartAxis->labelsBrush()); |
|
365 | 365 | setLabelsFont(m_chartAxis->labelsFont()); |
|
366 | 366 | setGridPen(m_chartAxis->gridLinePen()); |
|
367 | 367 | setShadesPen(m_chartAxis->shadesPen()); |
|
368 | 368 | setShadesBrush(m_chartAxis->shadesBrush()); |
|
369 | 369 | |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | void AxisItem::handleRangeChanged(qreal min, qreal max,int tickCount) |
|
373 | 373 | { |
|
374 | 374 | if(min==max || tickCount<2) return; |
|
375 | 375 | |
|
376 | 376 | m_min = min; |
|
377 | 377 | m_max = max; |
|
378 | 378 | m_ticksCount= tickCount; |
|
379 | 379 | |
|
380 | 380 | if(isEmpty()) return; |
|
381 | 381 | QVector<qreal> layout = calculateLayout(); |
|
382 | 382 | updateLayout(layout); |
|
383 | 383 | |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | void AxisItem::handleGeometryChanged(const QRectF& rect) |
|
387 | 387 | { |
|
388 | 388 | m_rect = rect; |
|
389 | 389 | if(isEmpty()) return; |
|
390 | 390 | QVector<qreal> layout = calculateLayout(); |
|
391 | 391 | updateLayout(layout); |
|
392 | 392 | } |
|
393 | 393 | |
|
394 | 394 | //painter |
|
395 | 395 | |
|
396 | 396 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
397 | 397 | { |
|
398 | 398 | Q_UNUSED(painter) |
|
399 | 399 | Q_UNUSED(option) |
|
400 | 400 | Q_UNUSED(widget) |
|
401 | 401 | } |
|
402 | 402 | |
|
403 | 403 | //TODO "nice numbers algorithm" |
|
404 | 404 | #include "moc_axisitem_p.cpp" |
|
405 | 405 | |
|
406 | 406 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now