##// END OF EJS Templates
Refacotr axisitem to handle ticks changes
Michal Klocek -
r452:47a9fc437f2c
parent child
Show More
@@ -1,74 +1,77
1 1 #include "axisanimationitem_p.h"
2 2 #include <QTimer>
3 3
4 4 Q_DECLARE_METATYPE(QVector<qreal>)
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 const static int duration = 500;
9 9
10 10 AxisAnimationItem::AxisAnimationItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 11 AxisItem(axis,type,parent),
12 12 m_animation(new AxisAnimator(this,this))
13 13 {
14 14 }
15 15
16 16 AxisAnimationItem::~AxisAnimationItem()
17 17 {
18 18 }
19 19
20 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
20 void AxisAnimationItem::updateItem()
21 21 {
22 QVector<qreal> oldLayout = layout();
23 AxisItem::updateItem();
24 QVector<qreal> newLayout = layout();
25
22 26 if(newLayout.count()==0) return;
23 27 oldLayout.resize(newLayout.size());
24 28
25 29 if(m_animation->state()!=QAbstractAnimation::Stopped){
26 30 m_animation->stop();
27 31 }
28 32
29 33 m_animation->setDuration(duration);
30 34 m_animation->setEasingCurve(QEasingCurve::InOutBack);
31 35 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
32 36 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 37 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
35 38 }
36 39
37 40 void AxisAnimationItem::setLabelsAngle(int angle)
38 41 {
39 42 AxisItem::setLabelsAngle(angle);
40 43 }
41 44
42 45 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
43 46 m_axis(axis)
44 47 {
45 48 }
46 49
47 50 AxisAnimator::~AxisAnimator()
48 51 {
49 52 }
50 53
51 54 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
52 55 {
53 56 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
54 57 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
55 58 QVector<qreal> result;
56 59 Q_ASSERT(startVector.count() == endVecotr.count());
57 60
58 61 for(int i =0 ;i< startVector.count();i++){
59 62 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
60 63 result << value;
61 64 }
62 65 return qVariantFromValue(result);
63 66 }
64 67
65 68
66 69 void AxisAnimator::updateCurrentValue (const QVariant & value )
67 70 {
68 71 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
69 m_axis->applyLayout(vector);
72 m_axis->setLayout(vector);
70 73 }
71 74
72 75 #include "moc_axisanimationitem_p.cpp"
73 76
74 77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,45
1 1 #ifndef AXISANIMATIONITEM_H_
2 2 #define AXISANIMATIONITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include "axisitem_p.h"
6 6 #include <QGraphicsItem>
7 7 #include <QVariantAnimation>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class AxisAnimator;
12 12
13 13 class AxisAnimationItem : public AxisItem
14 14 {
15 15 Q_OBJECT
16 16
17 17 public:
18 18 AxisAnimationItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 19 ~AxisAnimationItem();
20 20
21 21 void setLabelsAngle(int angle);
22 22
23 23 protected:
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
24 virtual void updateItem();
25 25 private:
26 26 AxisAnimator *m_animation;
27 27 };
28 28
29 29 class AxisAnimator: public QVariantAnimation
30 30 {
31 31 public:
32 32 AxisAnimator(AxisItem *axis,QObject *parent = 0);
33 33 ~AxisAnimator();
34 34 protected:
35 35 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
36 36 virtual void updateCurrentValue (const QVariant & value );
37 37 private:
38 38 AxisItem* m_axis;
39 39 };
40 40
41 41 QTCOMMERCIALCHART_END_NAMESPACE
42 42
43 43
44 44
45 45 #endif /* AXISITEM_H_ */
@@ -1,377 +1,393
1 1 #include "axisitem_p.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QDebug>
6 6
7 7 static int label_padding = 5;
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
12 12 ChartItem(parent),
13 13 m_chartAxis(axis),
14 14 m_type(type),
15 15 m_labelsAngle(0),
16 16 m_grid(parent),
17 17 m_shades(parent),
18 18 m_labels(parent),
19 19 m_axis(parent)
20 20 {
21 21 //initial initialization
22 22 m_axis.setZValue(ChartPresenter::AxisZValue);
23 23 m_shades.setZValue(ChartPresenter::ShadesZValue);
24 24 m_grid.setZValue(ChartPresenter::GridZValue);
25 25 setFlags(QGraphicsItem::ItemHasNoContents);
26 26
27 27 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
28 28 }
29 29
30 30 AxisItem::~AxisItem()
31 31 {
32 32 }
33 33
34 34 QRectF AxisItem::boundingRect() const
35 35 {
36 36 return QRectF();
37 37 }
38 38
39 39 void AxisItem::createItems(int count)
40 40 {
41 41 if(m_axis.children().size()==0)
42 42 m_axis.addToGroup(new QGraphicsLineItem());
43 43 for (int i = 0; i < count; ++i) {
44 44 m_grid.addToGroup(new QGraphicsLineItem());
45 45 m_labels.addToGroup(new QGraphicsSimpleTextItem());
46 46 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
47 47 m_axis.addToGroup(new QGraphicsLineItem());
48 48 }
49 49 }
50 50
51 51 void AxisItem::clear(int count)
52 52 {
53 53 QList<QGraphicsItem *> lines = m_grid.childItems();
54 54 QList<QGraphicsItem *> labels = m_labels.childItems();
55 55 QList<QGraphicsItem *> shades = m_shades.childItems();
56 56 QList<QGraphicsItem *> axis = m_axis.childItems();
57 57
58 58 for (int i = 0; i < count; ++i) {
59 59 delete(lines.takeLast());
60 60 delete(labels.takeLast());
61 61 if(lines.size()%2) delete(shades.takeLast());
62 62 delete(axis.takeLast());
63 63 }
64 64 }
65 65
66 66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 67 {
68 68 Q_UNUSED(painter);
69 69 Q_UNUSED(option);
70 70 Q_UNUSED(widget);
71 71 }
72 72
73 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
73 void AxisItem::updateItem()
74 74 {
75 if(newLayout.count()==0) return;
76 applyLayout(newLayout);
77 oldLayout=newLayout;
75 QStringList labels = createLabels(m_ticks,m_min,m_max);
76
77 int diff = m_thicksList.size() - labels.size();
78
79 if(diff>0) {
80 clear(diff);
81 }
82 else if(diff<0) {
83 createItems(-diff);
84 }
85
86 if(diff!=0) handleAxisUpdated();
87
88 m_thicksList=labels;
89
90 QVector<qreal> layout = calculateLayout();
91 if(layout.count()==0) return;
92 setLayout(layout);
93
78 94 }
79 95
80 96 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
81 97 {
82 98
83 99
84 100 Q_ASSERT(max>=min);
85 101
86 102 QStringList labels;
87 103
88 104 //int ticks = axis->ticksCount()-1;
89 105
90 106 for(int i=0; i<= ticks; i++) {
91 107 qreal value = min + (i * (max - min)/ ticks);
92 108 QString label ;//= axis->axisTickLabel(value);
93 109 if(label.isEmpty()) {
94 110 labels << QString::number(value);
95 111 }
96 112 else {
97 113 labels << label;
98 114 }
99 115 }
100 116 return labels;
101 117 }
102 118
103 119 void AxisItem::setAxisOpacity(qreal opacity)
104 120 {
105 121 m_axis.setOpacity(opacity);
106 122 }
107 123
108 124 qreal AxisItem::axisOpacity() const
109 125 {
110 126 return m_axis.opacity();
111 127 }
112 128
113 129 void AxisItem::setGridOpacity(qreal opacity)
114 130 {
115 131 m_grid.setOpacity(opacity);
116 132 }
117 133
118 134 qreal AxisItem::gridOpacity() const
119 135 {
120 136 return m_grid.opacity();
121 137 }
122 138
123 139 void AxisItem::setLabelsOpacity(qreal opacity)
124 140 {
125 141 m_labels.setOpacity(opacity);
126 142 }
127 143
128 144 qreal AxisItem::labelsOpacity() const
129 145 {
130 146 return m_labels.opacity();
131 147 }
132 148
133 149 void AxisItem::setShadesOpacity(qreal opacity)
134 150 {
135 151 m_shades.setOpacity(opacity);
136 152 }
137 153
138 154 qreal AxisItem::shadesOpacity() const
139 155 {
140 156 return m_shades.opacity();
141 157 }
142 158
143 159 void AxisItem::setLabelsAngle(int angle)
144 160 {
145 161 foreach(QGraphicsItem* item , m_labels.childItems()) {
146 162 QPointF center = item->boundingRect().center();
147 163 item->setRotation(angle);
148 164 }
149 165
150 166 m_labelsAngle=angle;
151 167 }
152 168
153 169 void AxisItem::setLabelsPen(const QPen& pen)
154 170 {
155 171 foreach(QGraphicsItem* item , m_labels.childItems()) {
156 172 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
157 173 }
158 174 }
159 175
160 176 void AxisItem::setLabelsBrush(const QBrush& brush)
161 177 {
162 178 foreach(QGraphicsItem* item , m_labels.childItems()) {
163 179 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
164 180 }
165 181 }
166 182
167 183 void AxisItem::setLabelsFont(const QFont& font)
168 184 {
169 185 foreach(QGraphicsItem* item , m_labels.childItems()) {
170 186 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
171 187 }
172 188 }
173 189
174 190 void AxisItem::setShadesBrush(const QBrush& brush)
175 191 {
176 192 foreach(QGraphicsItem* item , m_shades.childItems()) {
177 193 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
178 194 }
179 195 }
180 196
181 197 void AxisItem::setShadesPen(const QPen& pen)
182 198 {
183 199 foreach(QGraphicsItem* item , m_shades.childItems()) {
184 200 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
185 201 }
186 202 }
187 203
188 204 void AxisItem::setAxisPen(const QPen& pen)
189 205 {
190 206 foreach(QGraphicsItem* item , m_axis.childItems()) {
191 207 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
192 208 }
193 209 }
194 210
195 211 void AxisItem::setGridPen(const QPen& pen)
196 212 {
197 213 foreach(QGraphicsItem* item , m_grid.childItems()) {
198 214 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
199 215 }
200 216 }
201 217
202 218 QVector<qreal> AxisItem::calculateLayout() const
203 219 {
204 220 QVector<qreal> points;
205 221 points.resize(m_thicksList.size());
206 222
207 223 switch (m_type)
208 224 {
209 225 case X_AXIS:
210 226 {
211 227 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
212 228 for (int i = 0; i < m_thicksList.size(); ++i) {
213 229 int x = i * deltaX + m_rect.left();
214 230 points[i] = x;
215 231 }
216 232 }
217 233 break;
218 234 case Y_AXIS:
219 235 {
220 236 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
221 237 for (int i = 0; i < m_thicksList.size(); ++i) {
222 238 int y = i * -deltaY + m_rect.bottom();
223 239 points[i] = y;
224 240 }
225 241 }
226 242 break;
227 243 }
228 244 return points;
229 245 }
230 246
231 void AxisItem::applyLayout(const QVector<qreal>& points)
247 void AxisItem::setLayout(const QVector<qreal>& layout)
232 248 {
233 Q_ASSERT(points.size() == m_thicksList.size());
234 249
235 250 QList<QGraphicsItem *> lines = m_grid.childItems();
236 251 QList<QGraphicsItem *> labels = m_labels.childItems();
237 252 QList<QGraphicsItem *> shades = m_shades.childItems();
238 253 QList<QGraphicsItem *> axis = m_axis.childItems();
239 254
240 255 Q_ASSERT(labels.size() == m_thicksList.size());
256 Q_ASSERT(layout.size() == m_thicksList.size());
241 257
242 258 switch (m_type)
243 259 {
244 260 case X_AXIS:
245 261 {
246 262 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
247 263 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
248 264
249 for (int i = 0; i < points.size(); ++i) {
265 for (int i = 0; i < layout.size(); ++i) {
250 266 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
251 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
267 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
252 268 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
253 269 labelItem->setText(m_thicksList.at(i));
254 270 QPointF center = labelItem->boundingRect().center();
255 271 labelItem->setTransformOriginPoint(center.x(), center.y());
256 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
272 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
257 273 if(i%2){
258 274 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
259 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
275 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
260 276 }
261 277 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
262 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
278 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
263 279 }
264 280 }
265 281 break;
266 282
267 283 case Y_AXIS:
268 284 {
269 285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
270 286 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
271 287
272 for (int i = 0; i < points.size(); ++i) {
288 for (int i = 0; i < layout.size(); ++i) {
273 289 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
274 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
290 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
275 291 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
276 292 labelItem->setText(m_thicksList.at(i));
277 293 QPointF center = labelItem->boundingRect().center();
278 294 labelItem->setTransformOriginPoint(center.x(), center.y());
279 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
295 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
280 296 if(i%2){
281 297 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
282 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
298 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
283 299 }
284 300 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
285 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
301 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
286 302 }
287 303 }
288 304 break;
289 305 default:
290 306 qDebug()<<"Unknown axis type";
291 307 break;
292 308 }
309
310 m_layoutVector=layout;
293 311 }
294 312
295 313 //handlers
296 314
297 315 void AxisItem::handleAxisUpdated()
298 316 {
299 if(m_layoutVector.count()==0) return;
317 if(isEmpty()) return;
300 318
301 319 if(m_chartAxis->isAxisVisible()) {
302 320 setAxisOpacity(100);
303 321 }
304 322 else {
305 323 setAxisOpacity(0);
306 324 }
307 325
308 326 if(m_chartAxis->isGridVisible()) {
309 327 setGridOpacity(100);
310 328 }
311 329 else {
312 330 setGridOpacity(0);
313 331 }
314 332
315 333 if(m_chartAxis->labelsVisible())
316 334 {
317 335 setLabelsOpacity(100);
318 336 }
319 337 else {
320 338 setLabelsOpacity(0);
321 339 }
322 340
323 341 if(m_chartAxis->shadesVisible()) {
324 342 setShadesOpacity(m_chartAxis->shadesOpacity());
325 343 }
326 344 else {
327 345 setShadesOpacity(0);
328 346 }
329 347
330 348 setLabelsAngle(m_chartAxis->labelsAngle());
331 349 setAxisPen(m_chartAxis->axisPen());
332 350 setLabelsPen(m_chartAxis->labelsPen());
333 351 setLabelsBrush(m_chartAxis->labelsBrush());
334 352 setLabelsFont(m_chartAxis->labelsFont());
335 353 setGridPen(m_chartAxis->gridPen());
336 354 setShadesPen(m_chartAxis->shadesPen());
337 355 setShadesBrush(m_chartAxis->shadesBrush());
356
338 357 }
339 358
340 359 void AxisItem::handleRangeChanged(qreal min, qreal max)
341 360 {
342 if(min == max) return;
343
344 QStringList labels = createLabels(4,min,max);
345
346 int diff = m_thicksList.size() - labels.size();
347 361
348 if(diff>0){
349 clear(diff);
350 }else if(diff<0){
351 createItems(-diff);
352 }
353 m_thicksList=labels;
362 m_min = min;
363 m_max = max;
354 364
355 if(m_rect.isEmpty()) return;
365 if(isEmpty()) return;
366 updateItem();
356 367
357 QVector<qreal> vector = calculateLayout();
368 }
358 369
359 updateItems(m_layoutVector,vector);
370 void AxisItem::handleTicksCountChanged(int ticks)
371 {
372 m_ticks=ticks;
360 373
361 if(diff!=0) handleAxisUpdated();
374 if(isEmpty()) return;
375 updateItem();
362 376 }
363 377
364 378 void AxisItem::handleGeometryChanged(const QRectF& rect)
365 379 {
366 380 m_rect = rect;
381 if(isEmpty()) return;
382 updateItem();
383 }
367 384
368 if(m_thicksList.size()==0) return;
369
370 QVector<qreal> vector = calculateLayout();
371 updateItems(m_layoutVector,vector);
385 bool AxisItem::isEmpty()
386 {
387 return m_rect.isEmpty() || m_min==m_max || m_ticks==0;
372 388 }
373 389
374 390 //TODO "nice numbers algorithm"
375 391 #include "moc_axisitem_p.cpp"
376 392
377 393 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,88
1 1 #ifndef AXISITEM_H_
2 2 #define AXISITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include "chartitem_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class AxisItem : public QObject, public ChartItem
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 enum AxisType{X_AXIS,Y_AXIS};
17 17
18 18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 19 ~AxisItem();
20 20
21 21 //from QGraphicsItem
22 22 QRectF boundingRect() const;
23 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 24
25 25 AxisType axisType() const {return m_type;};
26 26
27 27 void setAxisOpacity(qreal opacity);
28 28 qreal axisOpacity() const;
29 29
30 30 void setGridOpacity(qreal opacity);
31 31 qreal gridOpacity() const;
32 32
33 33 void setLabelsOpacity(qreal opacity);
34 34 qreal labelsOpacity() const;
35 35
36 36 void setShadesOpacity(qreal opacity);
37 37 qreal shadesOpacity() const;
38 38
39 39 void setLabelsAngle(int angle);
40 40 int labelsAngle()const { return m_labelsAngle; }
41 41
42 42 void setShadesBrush(const QBrush& brush);
43 43 void setShadesPen(const QPen& pen);
44 44
45 45 void setAxisPen(const QPen& pen);
46 46 void setGridPen(const QPen& pen);
47 47
48 48 void setLabelsPen(const QPen& pen);
49 49 void setLabelsBrush(const QBrush& brush);
50 50 void setLabelsFont(const QFont& font);
51 51
52 52 public slots:
53 53 void handleAxisUpdated();//qchartaxis update calls
54 54 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleTicksCountChanged(int ticks); //ticks changed
55 56 void handleGeometryChanged(const QRectF& size); //geometry update calls
56 57
57 58 public:
58 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
59 virtual void updateItem();
59 60 QVector<qreal> calculateLayout() const;
60 void applyLayout(const QVector<qreal>& points);
61 void setLayout(const QVector<qreal>& points);
62 QVector<qreal> layout() { return m_layoutVector;};
61 63
62 64 private:
65 inline bool isEmpty();
63 66 void clear(int count);
64 67 void createItems(int count);
65 68 QStringList createLabels(int ticks, qreal min, qreal max);
66 69 private:
67 70 QChartAxis* m_chartAxis;
68 71 AxisType m_type;
69 72 QRectF m_rect;
70 73 int m_labelsAngle;
71 74 QGraphicsItemGroup m_grid;
72 75 QGraphicsItemGroup m_shades;
73 76 QGraphicsItemGroup m_labels;
74 77 QGraphicsItemGroup m_axis;
75 78 QStringList m_thicksList;
76 79 QVector<qreal> m_layoutVector;
80 qreal m_min;
81 qreal m_max;
82 int m_ticks;
77 83
78 84 };
79 85
80 86 QTCOMMERCIALCHART_END_NAMESPACE
81 87
82 88 #endif /* AXISITEM_H_ */
@@ -1,307 +1,307
1 1 #include "chartdataset_p.h"
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlineseries.h"
5 5 #include "qareaseries.h"
6 6 #include "qbarseries.h"
7 7 #include "qstackedbarseries.h"
8 8 #include "qpercentbarseries.h"
9 9 #include "qpieseries.h"
10 10 #include "qscatterseries.h"
11 11 #include "qsplineseries.h"
12 12
13 13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 14
15 15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
16 16 m_axisX(new QChartAxis(this)),
17 17 m_axisY(new QChartAxis(this)),
18 18 m_domainIndex(0),
19 19 m_axisXInitialized(false)
20 20 {
21 21 }
22 22
23 23 ChartDataSet::~ChartDataSet()
24 24 {
25 25 }
26 26
27 27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
28 28 {
29 29 if(axisY==0) axisY = m_axisY;
30 30
31 31 QChartAxis* axis = m_seriesAxisMap.value(series);
32 32
33 33 if(axis) {
34 34 qWarning() << "Can not add series. Series already on the chart";
35 35 return;
36 36 }
37 37
38 38 if(!series->parent()){
39 39 series->setParent(this); // take ownership
40 40 };
41 41
42 42 if(!axisY->parent()){
43 43 axisY->setParent(this); // take ownership
44 44 }
45 45
46 46 Domain* domain = m_axisDomainMap.value(axisY);
47 47
48 48 if(!domain) {
49 49 domain = new Domain();
50 50
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
53 53 //initialize
54 54 m_axisDomainMap.insert(axisY,domain);
55 55 emit axisAdded(axisY,domain);
56 56 }
57 57
58 58 if(!m_axisXInitialized){
59 59 emit axisAdded(axisX(),domain);
60 60 m_axisXInitialized=true;
61 61 }
62 62
63 63 calculateDomain(series,domain);
64 64
65 65 m_seriesAxisMap.insert(series,axisY);
66 66 emit seriesAdded(series,domain);
67 67
68 68 }
69 69
70 70 void ChartDataSet::removeSeries(QSeries* series)
71 71 {
72 72
73 73 QChartAxis* axis = m_seriesAxisMap.value(series);
74 74
75 75 if(!axis){
76 76 qWarning()<<"Can not remove series. Series not found on the chart.";
77 77 return;
78 78 }
79 79 emit seriesRemoved(series);
80 80 m_seriesAxisMap.remove(series);
81 81
82 82 if(series->parent()==this){
83 83 delete series;
84 84 series=0;
85 85 }
86 86
87 87 QList<QChartAxis*> axes = m_seriesAxisMap.values();
88 88
89 89 int i = axes.indexOf(axis);
90 90
91 91 if(i==-1){
92 92 Domain* domain = m_axisDomainMap.take(axis);
93 93 emit axisRemoved(axis);
94 94 if(axis!=axisY()){
95 95 if(axis->parent()==this){
96 96 delete axis;
97 97 axis=0;
98 98 }
99 99 }
100 100 delete domain;
101 101 }
102 102
103 103 if(m_seriesAxisMap.values().size()==0)
104 104 {
105 105 m_axisXInitialized=false;
106 106 emit axisRemoved(axisX());
107 107 }
108 108 }
109 109
110 110 void ChartDataSet::removeAllSeries()
111 111 {
112 112
113 113 QList<QSeries*> series = m_seriesAxisMap.keys();
114 114
115 115 foreach(QSeries* s , series) {
116 116 removeSeries(s);
117 117 }
118 118
119 119 Q_ASSERT(m_seriesAxisMap.count()==0);
120 120 Q_ASSERT(m_axisDomainMap.count()==0);
121 121
122 122 }
123 123
124 124 //to be removed with PIMPL
125 125 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
126 126 {
127 127 switch(series->type())
128 128 {
129 129 case QSeries::SeriesTypeLine: {
130 130
131 131 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
132 132
133 133 for (int i = 0; i < lineSeries->count(); i++)
134 134 {
135 135 qreal x = lineSeries->x(i);
136 136 qreal y = lineSeries->y(i);
137 137 domain->setMinX(qMin(domain->minX(),x));
138 138 domain->setMinY(qMin(domain->minY(),y));
139 139 domain->setMaxX(qMax(domain->maxX(),x));
140 140 domain->setMaxY(qMax(domain->maxY(),y));
141 141 }
142 142 break;
143 143 }
144 144 case QSeries::SeriesTypeArea: {
145 145
146 146 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
147 147
148 148 QLineSeries* upperSeries = areaSeries->upperSeries();
149 149 QLineSeries* lowerSeries = areaSeries->lowerSeries();
150 150
151 151 for (int i = 0; i < upperSeries->count(); i++)
152 152 {
153 153 qreal x = upperSeries->x(i);
154 154 qreal y = upperSeries->y(i);
155 155 domain->setMinX(qMin(domain->minX(),x));
156 156 domain->setMinY(qMin(domain->minY(),y));
157 157 domain->setMaxX(qMax(domain->maxX(),x));
158 158 domain->setMaxY(qMax(domain->maxY(),y));
159 159 }
160 160 if(lowerSeries) {
161 161 for (int i = 0; i < lowerSeries->count(); i++)
162 162 {
163 163 qreal x = lowerSeries->x(i);
164 164 qreal y = lowerSeries->y(i);
165 165 domain->setMinX(qMin(domain->minX(),x));
166 166 domain->setMinY(qMin(domain->minY(),y));
167 167 domain->setMaxX(qMax(domain->maxX(),x));
168 168 domain->setMaxY(qMax(domain->maxY(),y));
169 169 }}
170 170 break;
171 171 }
172 172 case QSeries::SeriesTypeBar: {
173 173 qDebug() << "QChartSeries::SeriesTypeBar";
174 174 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
175 175 qreal x = barSeries->categoryCount();
176 176 qreal y = barSeries->max();
177 177 domain->setMinX(qMin(domain->minX(),x));
178 178 domain->setMinY(qMin(domain->minY(),y));
179 179 domain->setMaxX(qMax(domain->maxX(),x));
180 180 domain->setMaxY(qMax(domain->maxY(),y));
181 181 break;
182 182 }
183 183 case QSeries::SeriesTypeStackedBar: {
184 184 qDebug() << "QChartSeries::SeriesTypeStackedBar";
185 185
186 186 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
187 187 qreal x = stackedBarSeries->categoryCount();
188 188 qreal y = stackedBarSeries->maxCategorySum();
189 189 domain->setMinX(qMin(domain->minX(),x));
190 190 domain->setMinY(qMin(domain->minY(),y));
191 191 domain->setMaxX(qMax(domain->maxX(),x));
192 192 domain->setMaxY(qMax(domain->maxY(),y));
193 193 break;
194 194 }
195 195 case QSeries::SeriesTypePercentBar: {
196 196 qDebug() << "QChartSeries::SeriesTypePercentBar";
197 197
198 198 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
199 199 qreal x = percentBarSeries->categoryCount();
200 200 domain->setMinX(qMin(domain->minX(),x));
201 201 domain->setMinY(0);
202 202 domain->setMaxX(qMax(domain->maxX(),x));
203 203 domain->setMaxY(100);
204 204 break;
205 205 }
206 206
207 207 case QSeries::SeriesTypePie: {
208 208 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
209 209 // TODO: domain stuff
210 210 break;
211 211 }
212 212
213 213 case QSeries::SeriesTypeScatter: {
214 214 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
215 215 Q_ASSERT(scatterSeries);
216 216 qreal minX(domain->minX());
217 217 qreal minY(domain->minY());
218 218 qreal maxX(domain->maxX());
219 219 qreal maxY(domain->maxY());
220 220 foreach (QPointF point, scatterSeries->data()) {
221 221 minX = qMin(minX, point.x());
222 222 minY = qMin(minY, point.y());
223 223 maxX = qMax(maxX, point.x());
224 224 maxY = qMax(maxY, point.y());
225 225 }
226 226 domain->setMinX(minX);
227 227 domain->setMinY(minY);
228 228 domain->setMaxX(maxX);
229 229 domain->setMaxY(maxY);
230 230 break;
231 231 }
232 232
233 233 case QSeries::SeriesTypeSpline: {
234 234 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
235 235
236 236 for (int i = 0; i < splineSeries->count(); i++)
237 237 {
238 238 qreal x = splineSeries->x(i);
239 239 qreal y = splineSeries->y(i);
240 240 domain->setMinX(qMin(domain->minX(),x));
241 241 domain->setMinY(qMin(domain->minY(),y));
242 242 domain->setMaxX(qMax(domain->maxX(),x));
243 243 domain->setMaxY(qMax(domain->maxY(),y));
244 244 }
245 245 break;
246 246 }
247 247
248 248 default: {
249 249 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
250 250 return;
251 251 break;
252 252 }
253 253
254 254 }
255 255 }
256 256
257 257 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
258 258 {
259 259 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
260 260 while (i.hasNext()) {
261 261 i.next();
262 262 i.value()->zoomIn(rect,size);
263 263 }
264 264 }
265 265
266 266 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
267 267 {
268 268 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
269 269 while (i.hasNext()) {
270 270 i.next();
271 271 i.value()->zoomOut(rect,size);
272 272 }
273 273 }
274 274
275 275 QChartAxis* ChartDataSet::axisY(QSeries* series) const
276 276 {
277 277 if(series == 0) return m_axisY;
278 278 return m_seriesAxisMap.value(series);
279 279 }
280 280
281 281 Domain* ChartDataSet::domain(QSeries* series) const
282 282 {
283 283 QChartAxis* axis = m_seriesAxisMap.value(series);
284 284 if(axis){
285 285 return m_axisDomainMap.value(axis);
286 286 }else
287 287 return 0;
288 288 }
289 289
290 290 Domain* ChartDataSet::domain(QChartAxis* axis) const
291 291 {
292 292 if(axis==axisX()) {
293 293 return m_axisDomainMap.value(axisY());
294 294 }
295 295 else {
296 296 return m_axisDomainMap.value(axis);
297 297 }
298 298 }
299 299
300 300 QChartAxis* ChartDataSet::axis(QSeries* series) const
301 301 {
302 302 return m_seriesAxisMap.value(series);
303 303 }
304 304
305 305 #include "moc_chartdataset_p.cpp"
306 306
307 307 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,368 +1,370
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include "charttheme_p.h"
6 6 //series
7 7 #include "qbarseries.h"
8 8 #include "qstackedbarseries.h"
9 9 #include "qpercentbarseries.h"
10 10 #include "qlineseries.h"
11 11 #include "qareaseries.h"
12 12 #include "qpieseries.h"
13 13 #include "qscatterseries.h"
14 14 #include "qsplineseries.h"
15 15 //items
16 16 #include "axisitem_p.h"
17 17 #include "axisanimationitem_p.h"
18 18 #include "areachartitem_p.h"
19 19 #include "barpresenter_p.h"
20 20 #include "stackedbarpresenter_p.h"
21 21 #include "percentbarpresenter_p.h"
22 22 #include "linechartitem_p.h"
23 23 #include "linechartanimationitem_p.h"
24 24 #include "piepresenter_p.h"
25 25 #include "scatterpresenter_p.h"
26 26 #include "splinepresenter_p.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
31 31 m_chart(chart),
32 32 m_dataset(dataset),
33 33 m_chartTheme(0),
34 34 m_zoomIndex(0),
35 35 m_marginSize(0),
36 36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
37 37 m_options(QChart::NoAnimation)
38 38 {
39 39 createConnections();
40 40 setChartTheme(QChart::ChartThemeDefault);
41 41 }
42 42
43 43 ChartPresenter::~ChartPresenter()
44 44 {
45 45 }
46 46
47 47 void ChartPresenter::createConnections()
48 48 {
49 49 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
50 50 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
51 51 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
52 52 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
53 53 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
54 54 }
55 55
56 56
57 57 QRectF ChartPresenter::geometry() const
58 58 {
59 59 return m_rect;
60 60 }
61 61
62 62 void ChartPresenter::handleGeometryChanged()
63 63 {
64 64 QRectF rect(QPoint(0,0),m_chart->size());
65 65 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
66 66
67 67 //rewrite zoom stack
68 68 for(int i=0;i<m_zoomStack.count();i++){
69 69 QRectF r = m_zoomStack[i];
70 70 qreal w = rect.width()/m_rect.width();
71 71 qreal h = rect.height()/m_rect.height();
72 72 QPointF tl = r.topLeft();
73 73 tl.setX(tl.x()*w);
74 74 tl.setY(tl.y()*h);
75 75 QPointF br = r.bottomRight();
76 76 br.setX(br.x()*w);
77 77 br.setY(br.y()*h);
78 78 r.setTopLeft(tl);
79 79 r.setBottomRight(br);
80 80 m_zoomStack[i]=r;
81 81 }
82 82
83 83 m_rect = rect;
84 84 Q_ASSERT(m_rect.isValid());
85 85 emit geometryChanged(m_rect);
86 86 }
87 87
88 88 int ChartPresenter::margin() const
89 89 {
90 90 return m_marginSize;
91 91 }
92 92
93 93 void ChartPresenter::setMargin(int margin)
94 94 {
95 95 m_marginSize = margin;
96 96 }
97 97
98 98 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
99 99 {
100 100
101 101 AxisItem* item ;
102 102
103 103 if(!m_options.testFlag(QChart::GridAxisAnimations))
104 104 {
105 105 item = new AxisItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
106 106 }else{
107 107 item = new AxisAnimationItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
108 108 }
109 109 if(axis==m_dataset->axisX()){
110 110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
111 111 //initialize
112 112 item->handleRangeChanged(domain->minX(),domain->maxX());
113 item->handleTicksCountChanged(4);
113 114 }
114 115 else{
115 116 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 117 //initialize
117 118 item->handleRangeChanged(domain->minY(),domain->maxY());
119 item->handleTicksCountChanged(4);
118 120 }
119 121
120 122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 123 //initialize
122 124 item->handleGeometryChanged(m_rect);
123 125 m_chartTheme->decorate(axis,item);
124 126 m_axisItems.insert(axis,item);
125 127 }
126 128
127 129 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
128 130 {
129 131 AxisItem* item = m_axisItems.take(axis);
130 132 Q_ASSERT(item);
131 133 delete item;
132 134 }
133 135
134 136
135 137 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
136 138 {
137 139 switch(series->type())
138 140 {
139 141 case QSeries::SeriesTypeLine: {
140 142
141 143 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
142 144 LineChartItem* item;
143 145 if(m_options.testFlag(QChart::SeriesAnimations)){
144 146 item = new LineChartAnimationItem(lineSeries,m_chart);
145 147 }else{
146 148 item = new LineChartItem(lineSeries,m_chart);
147 149 }
148 150 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
149 151 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
150 152 //initialize
151 153 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
152 154 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
153 155 //decorate
154 156 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
155 157 m_chartItems.insert(series,item);
156 158 break;
157 159 }
158 160
159 161 case QSeries::SeriesTypeArea: {
160 162
161 163 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
162 164 AreaChartItem* item;
163 165 if(m_options.testFlag(QChart::SeriesAnimations)) {
164 166 item = new AreaChartItem(areaSeries,m_chart);
165 167 }
166 168 else {
167 169 item = new AreaChartItem(areaSeries,m_chart);
168 170 }
169 171 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
170 172 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
171 173 //initialize
172 174 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
173 175 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
174 176 //decorate
175 177 m_chartTheme->decorate(item,areaSeries,m_chartItems.count());
176 178 m_chartItems.insert(series,item);
177 179 break;
178 180 }
179 181
180 182 case QSeries::SeriesTypeBar: {
181 183 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
182 184 BarPresenter* item = new BarPresenter(barSeries,m_chart);
183 185 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
184 186 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
185 187 // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
186 188 m_chartItems.insert(series,item);
187 189 // m_axisXItem->setVisible(false);
188 190 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
189 191 break;
190 192 }
191 193
192 194 case QSeries::SeriesTypeStackedBar: {
193 195
194 196 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
195 197 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
196 198 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
197 199 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
198 200 // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
199 201 m_chartItems.insert(series,item);
200 202 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
201 203 break;
202 204 }
203 205
204 206 case QSeries::SeriesTypePercentBar: {
205 207
206 208 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
207 209 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
208 210 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
209 211 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
210 212 // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
211 213 m_chartItems.insert(series,item);
212 214 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
213 215 break;
214 216 }
215 217 case QSeries::SeriesTypeScatter: {
216 218 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
217 219 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
218 220 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
219 221 scatterSeries, SIGNAL(clicked(QPointF)));
220 222 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
221 223 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
222 224 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
223 225 scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
224 226 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
225 227 m_chartItems.insert(scatterSeries, scatterPresenter);
226 228 if (m_rect.isValid())
227 229 scatterPresenter->handleGeometryChanged(m_rect);
228 230 scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY());
229 231 break;
230 232 }
231 233 case QSeries::SeriesTypePie: {
232 234 QPieSeries *s = qobject_cast<QPieSeries *>(series);
233 235 PiePresenter* pie = new PiePresenter(m_chart, s);
234 236 m_chartTheme->decorate(pie, s, m_chartItems.count());
235 237 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
236 238
237 239 // Hide all from background when there is only piechart
238 240 // TODO: refactor this ugly code... should be one setting for this
239 241 if (m_chartItems.count() == 0) {
240 242 m_chart->axisX()->setAxisVisible(false);
241 243 m_chart->axisY()->setAxisVisible(false);
242 244 m_chart->axisX()->setGridVisible(false);
243 245 m_chart->axisY()->setGridVisible(false);
244 246 m_chart->axisX()->setLabelsVisible(false);
245 247 m_chart->axisY()->setLabelsVisible(false);
246 248 m_chart->axisX()->setShadesVisible(false);
247 249 m_chart->axisY()->setShadesVisible(false);
248 250 m_chart->setChartBackgroundBrush(Qt::transparent);
249 251 }
250 252
251 253 m_chartItems.insert(series, pie);
252 254 pie->handleGeometryChanged(m_rect);
253 255 break;
254 256 }
255 257
256 258 case QSeries::SeriesTypeSpline: {
257 259 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
258 260 SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart);
259 261 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&)));
260 262 m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count());
261 263 m_chartItems.insert(splineSeries, splinePresenter);
262 264 break;
263 265 }
264 266 default: {
265 267 qDebug()<< "Series type" << series->type() << "not implemented.";
266 268 break;
267 269 }
268 270 }
269 271
270 272 zoomReset();
271 273 }
272 274
273 275 void ChartPresenter::handleSeriesRemoved(QSeries* series)
274 276 {
275 277 ChartItem* item = m_chartItems.take(series);
276 278 delete item;
277 279 }
278 280
279 281 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
280 282 {
281 283 delete m_chartTheme;
282 284
283 285 m_chartTheme = ChartTheme::createTheme(theme);
284 286
285 287 m_chartTheme->decorate(m_chart);
286 288 QMapIterator<QSeries*,ChartItem*> i(m_chartItems);
287 289
288 290 int index=0;
289 291 while (i.hasNext()) {
290 292 i.next();
291 293 m_chartTheme->decorate(i.value(),i.key(),index);
292 294 index++;
293 295 }
294 296
295 297 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
296 298 while (j.hasNext()) {
297 299 j.next();
298 300 m_chartTheme->decorate(j.key(),j.value());
299 301 }
300 302 }
301 303
302 304 QChart::ChartTheme ChartPresenter::chartTheme()
303 305 {
304 306 return m_chartTheme->id();
305 307 }
306 308
307 309 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
308 310 {
309 311 if(m_options!=options) {
310 312
311 313 m_options=options;
312 314
313 315 //recreate elements
314 316 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
315 317 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
316 318
317 319 foreach(QChartAxis* axis, axisList) {
318 320 handleAxisRemoved(axis);
319 321 handleAxisAdded(axis,m_dataset->domain(axis));
320 322 }
321 323 foreach(QSeries* series, seriesList) {
322 324 handleSeriesRemoved(series);
323 325 handleSeriesAdded(series,m_dataset->domain(series));
324 326 }
325 327 }
326 328 }
327 329
328 330 void ChartPresenter::zoomIn()
329 331 {
330 332 QRectF rect = geometry();
331 333 rect.setWidth(rect.width()/2);
332 334 rect.setHeight(rect.height()/2);
333 335 rect.moveCenter(geometry().center());
334 336 zoomIn(rect);
335 337 }
336 338
337 339 void ChartPresenter::zoomIn(const QRectF& rect)
338 340 {
339 341 QRectF r = rect.normalized();
340 342 r.translate(-m_marginSize, -m_marginSize);
341 343 m_dataset->zoomInDomain(r,geometry().size());
342 344 m_zoomStack<<r;
343 345 m_zoomIndex++;
344 346 }
345 347
346 348 void ChartPresenter::zoomOut()
347 349 {
348 350 if(m_zoomIndex==0) return;
349 351 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
350 352 m_zoomIndex--;
351 353 m_zoomStack.resize(m_zoomIndex);
352 354 }
353 355
354 356 void ChartPresenter::zoomReset()
355 357 {
356 358 m_zoomIndex=0;
357 359 m_zoomStack.resize(m_zoomIndex);
358 360 }
359 361
360 362 QChart::AnimationOptions ChartPresenter::animationOptions() const
361 363 {
362 364 return m_options;
363 365 }
364 366
365 367
366 368 #include "moc_chartpresenter_p.cpp"
367 369
368 370 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now