##// END OF EJS Templates
Revert "Bugfix: wrong deallocation in axis due to class hierarchy changes."...
Michal Klocek -
r685:c7c4c30aaeb9
parent child
Show More
@@ -1,400 +1,400
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 #include <cmath>
8 8
9 9 static int label_padding = 5;
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 Axis::Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type) :
14 14 Chart(presenter),
15 15 m_chartAxis(axis),
16 16 m_type(type),
17 17 m_labelsAngle(0),
18 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
19 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
20 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
21 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
18 m_grid(presenter->rootItem()),
19 m_shades(presenter->rootItem()),
20 m_labels(presenter->rootItem()),
21 m_axis(presenter->rootItem()),
22 22 m_min(0),
23 23 m_max(0),
24 24 m_ticksCount(0)
25 25 {
26 26 //initial initialization
27 m_axis->setZValue(ChartPresenter::AxisZValue);
28 m_axis->setHandlesChildEvents(false);
27 m_axis.setZValue(ChartPresenter::AxisZValue);
28 m_axis.setHandlesChildEvents(false);
29 29
30 m_shades->setZValue(ChartPresenter::ShadesZValue);
31 m_grid->setZValue(ChartPresenter::GridZValue);
30 m_shades.setZValue(ChartPresenter::ShadesZValue);
31 m_grid.setZValue(ChartPresenter::GridZValue);
32 32
33 33 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
34 34 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
35 35
36 36 handleAxisUpdated();
37 37 }
38 38
39 39 Axis::~Axis()
40 40 {
41 41 }
42 42
43 43 void Axis::createItems(int count)
44 44 {
45 45
46 if(m_axis->children().size()==0)
47 m_axis->addToGroup(new AxisItem(this));
46 if(m_axis.children().size()==0)
47 m_axis.addToGroup(new AxisItem(this));
48 48 for (int i = 0; i < count; ++i) {
49 m_grid->addToGroup(new QGraphicsLineItem());
50 m_labels->addToGroup(new QGraphicsSimpleTextItem());
51 m_axis->addToGroup(new QGraphicsLineItem());
52 if((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
49 m_grid.addToGroup(new QGraphicsLineItem());
50 m_labels.addToGroup(new QGraphicsSimpleTextItem());
51 m_axis.addToGroup(new QGraphicsLineItem());
52 if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem());
53 53 }
54 54 }
55 55
56 56 void Axis::deleteItems(int count)
57 57 {
58 QList<QGraphicsItem *> lines = m_grid->childItems();
59 QList<QGraphicsItem *> labels = m_labels->childItems();
60 QList<QGraphicsItem *> shades = m_shades->childItems();
61 QList<QGraphicsItem *> axis = m_axis->childItems();
58 QList<QGraphicsItem *> lines = m_grid.childItems();
59 QList<QGraphicsItem *> labels = m_labels.childItems();
60 QList<QGraphicsItem *> shades = m_shades.childItems();
61 QList<QGraphicsItem *> axis = m_axis.childItems();
62 62
63 63 for (int i = 0; i < count; ++i) {
64 64 if(lines.size()%2 && lines.size()>1) delete(shades.takeLast());
65 65 delete(lines.takeLast());
66 66 delete(labels.takeLast());
67 67 delete(axis.takeLast());
68 68 }
69 69 }
70 70
71 71 void Axis::updateLayout(QVector<qreal>& layout)
72 72 {
73 73 if(animator()){
74 74 animator()->updateLayout(this,layout);
75 75 }
76 76 else setLayout(layout);
77 77 }
78 78
79 79 QStringList Axis::createLabels(int ticks, qreal min, qreal max) const
80 80 {
81 81 Q_ASSERT(max>=min);
82 82 Q_ASSERT(ticks>1);
83 83
84 84 QStringList labels;
85 85
86 86 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
87 87
88 88 QChartAxisCategories* categories = m_chartAxis->categories();
89 89
90 90 for(int i=0; i< ticks; i++) {
91 91 qreal value = min + (i * (max - min)/ (ticks-1));
92 92 if(categories->count()==0) {
93 93 labels << QString::number(value,'f',n);
94 94 }
95 95 else {
96 96
97 97 QString label = categories->label(value);
98 98 labels << label;
99 99 }
100 100 }
101 101 return labels;
102 102 }
103 103
104 104 void Axis::setAxisOpacity(qreal opacity)
105 105 {
106 m_axis->setOpacity(opacity);
106 m_axis.setOpacity(opacity);
107 107 }
108 108
109 109 qreal Axis::axisOpacity() const
110 110 {
111 return m_axis->opacity();
111 return m_axis.opacity();
112 112 }
113 113
114 114 void Axis::setGridOpacity(qreal opacity)
115 115 {
116 m_grid->setOpacity(opacity);
116 m_grid.setOpacity(opacity);
117 117 }
118 118
119 119 qreal Axis::gridOpacity() const
120 120 {
121 return m_grid->opacity();
121 return m_grid.opacity();
122 122 }
123 123
124 124 void Axis::setLabelsOpacity(qreal opacity)
125 125 {
126 m_labels->setOpacity(opacity);
126 m_labels.setOpacity(opacity);
127 127 }
128 128
129 129 qreal Axis::labelsOpacity() const
130 130 {
131 return m_labels->opacity();
131 return m_labels.opacity();
132 132 }
133 133
134 134 void Axis::setShadesOpacity(qreal opacity)
135 135 {
136 m_shades->setOpacity(opacity);
136 m_shades.setOpacity(opacity);
137 137 }
138 138
139 139 qreal Axis::shadesOpacity() const
140 140 {
141 return m_shades->opacity();
141 return m_shades.opacity();
142 142 }
143 143
144 144 void Axis::setLabelsAngle(int angle)
145 145 {
146 foreach(QGraphicsItem* item , m_labels->childItems()) {
146 foreach(QGraphicsItem* item , m_labels.childItems()) {
147 147 QPointF center = item->boundingRect().center();
148 148 item->setRotation(angle);
149 149 }
150 150
151 151 m_labelsAngle=angle;
152 152 }
153 153
154 154 void Axis::setLabelsPen(const QPen& pen)
155 155 {
156 foreach(QGraphicsItem* item , m_labels->childItems()) {
156 foreach(QGraphicsItem* item , m_labels.childItems()) {
157 157 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
158 158 }
159 159 }
160 160
161 161 void Axis::setLabelsBrush(const QBrush& brush)
162 162 {
163 foreach(QGraphicsItem* item , m_labels->childItems()) {
163 foreach(QGraphicsItem* item , m_labels.childItems()) {
164 164 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
165 165 }
166 166 }
167 167
168 168 void Axis::setLabelsFont(const QFont& font)
169 169 {
170 foreach(QGraphicsItem* item , m_labels->childItems()) {
170 foreach(QGraphicsItem* item , m_labels.childItems()) {
171 171 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
172 172 }
173 173 }
174 174
175 175 void Axis::setShadesBrush(const QBrush& brush)
176 176 {
177 foreach(QGraphicsItem* item , m_shades->childItems()) {
177 foreach(QGraphicsItem* item , m_shades.childItems()) {
178 178 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
179 179 }
180 180 }
181 181
182 182 void Axis::setShadesPen(const QPen& pen)
183 183 {
184 foreach(QGraphicsItem* item , m_shades->childItems()) {
184 foreach(QGraphicsItem* item , m_shades.childItems()) {
185 185 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
186 186 }
187 187 }
188 188
189 189 void Axis::setAxisPen(const QPen& pen)
190 190 {
191 foreach(QGraphicsItem* item , m_axis->childItems()) {
191 foreach(QGraphicsItem* item , m_axis.childItems()) {
192 192 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
193 193 }
194 194 }
195 195
196 196 void Axis::setGridPen(const QPen& pen)
197 197 {
198 foreach(QGraphicsItem* item , m_grid->childItems()) {
198 foreach(QGraphicsItem* item , m_grid.childItems()) {
199 199 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
200 200 }
201 201 }
202 202
203 203 QVector<qreal> Axis::calculateLayout() const
204 204 {
205 205 Q_ASSERT(m_ticksCount>=2);
206 206
207 207 QVector<qreal> points;
208 208 points.resize(m_ticksCount);
209 209
210 210 switch (m_type)
211 211 {
212 212 case X_AXIS:
213 213 {
214 214 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
215 215 for (int i = 0; i < m_ticksCount; ++i) {
216 216 int x = i * deltaX + m_rect.left();
217 217 points[i] = x;
218 218 }
219 219 }
220 220 break;
221 221 case Y_AXIS:
222 222 {
223 223 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
224 224 for (int i = 0; i < m_ticksCount; ++i) {
225 225 int y = i * -deltaY + m_rect.bottom();
226 226 points[i] = y;
227 227 }
228 228 }
229 229 break;
230 230 }
231 231 return points;
232 232 }
233 233
234 234 void Axis::setLayout(QVector<qreal>& layout)
235 235 {
236 236 int diff = m_layoutVector.size() - layout.size();
237 237
238 238 if(diff>0) {
239 239 deleteItems(diff);
240 240 }
241 241 else if(diff<0) {
242 242 createItems(-diff);
243 243 }
244 244
245 245 if(diff!=0) handleAxisUpdated();
246 246
247 247 QStringList ticksList = createLabels(layout.size(),m_min,m_max);
248 248
249 QList<QGraphicsItem *> lines = m_grid->childItems();
250 QList<QGraphicsItem *> labels = m_labels->childItems();
251 QList<QGraphicsItem *> shades = m_shades->childItems();
252 QList<QGraphicsItem *> axis = m_axis->childItems();
249 QList<QGraphicsItem *> lines = m_grid.childItems();
250 QList<QGraphicsItem *> labels = m_labels.childItems();
251 QList<QGraphicsItem *> shades = m_shades.childItems();
252 QList<QGraphicsItem *> axis = m_axis.childItems();
253 253
254 254 Q_ASSERT(labels.size() == ticksList.size());
255 255 Q_ASSERT(layout.size() == ticksList.size());
256 256
257 257 switch (m_type)
258 258 {
259 259 case X_AXIS:
260 260 {
261 261 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
262 262 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
263 263
264 264 for (int i = 0; i < layout.size(); ++i) {
265 265 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
266 266 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
267 267 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
268 268 labelItem->setText(ticksList.at(i));
269 269 QPointF center = labelItem->boundingRect().center();
270 270 labelItem->setTransformOriginPoint(center.x(), center.y());
271 271 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
272 272 if((i+1)%2 && i>1) {
273 273 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
274 274 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
275 275 }
276 276 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
277 277 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
278 278 }
279 279 }
280 280 break;
281 281
282 282 case Y_AXIS:
283 283 {
284 284 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
285 285 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
286 286
287 287 for (int i = 0; i < layout.size(); ++i) {
288 288 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
289 289 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
290 290 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
291 291 labelItem->setText(ticksList.at(i));
292 292 QPointF center = labelItem->boundingRect().center();
293 293 labelItem->setTransformOriginPoint(center.x(), center.y());
294 294 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
295 295 if((i+1)%2 && i>1) {
296 296 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
297 297 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
298 298 }
299 299 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
300 300 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
301 301 }
302 302 }
303 303 break;
304 304 default:
305 305 qDebug()<<"Unknown axis type";
306 306 break;
307 307 }
308 308
309 309 m_layoutVector=layout;
310 310 }
311 311
312 312 bool Axis::isEmpty()
313 313 {
314 314 return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0;
315 315 }
316 316
317 317 //handlers
318 318
319 319 void Axis::handleAxisCategoriesUpdated()
320 320 {
321 321 if(isEmpty()) return;
322 322 updateLayout(m_layoutVector);
323 323 }
324 324
325 325 void Axis::handleAxisUpdated()
326 326 {
327 327
328 328 if(isEmpty()) return;
329 329
330 330 if(m_chartAxis->isAxisVisible()) {
331 331 setAxisOpacity(100);
332 332 }
333 333 else {
334 334 setAxisOpacity(0);
335 335 }
336 336
337 337 if(m_chartAxis->isGridLineVisible()) {
338 338 setGridOpacity(100);
339 339 }
340 340 else {
341 341 setGridOpacity(0);
342 342 }
343 343
344 344 if(m_chartAxis->labelsVisible())
345 345 {
346 346 setLabelsOpacity(100);
347 347 }
348 348 else {
349 349 setLabelsOpacity(0);
350 350 }
351 351
352 352 if(m_chartAxis->shadesVisible()) {
353 353 setShadesOpacity(m_chartAxis->shadesOpacity());
354 354 }
355 355 else {
356 356 setShadesOpacity(0);
357 357 }
358 358
359 359 setLabelsAngle(m_chartAxis->labelsAngle());
360 360 setAxisPen(m_chartAxis->axisPen());
361 361 setLabelsPen(m_chartAxis->labelsPen());
362 362 setLabelsBrush(m_chartAxis->labelsBrush());
363 363 setLabelsFont(m_chartAxis->labelsFont());
364 364 setGridPen(m_chartAxis->gridLinePen());
365 365 setShadesPen(m_chartAxis->shadesPen());
366 366 setShadesBrush(m_chartAxis->shadesBrush());
367 367
368 368 }
369 369
370 370 void Axis::handleRangeChanged(qreal min, qreal max,int tickCount)
371 371 {
372 372 if(min==max || tickCount<2) return;
373 373
374 374 m_min = min;
375 375 m_max = max;
376 376 m_ticksCount= tickCount;
377 377
378 378 if(isEmpty()) return;
379 379 QVector<qreal> layout = calculateLayout();
380 380 updateLayout(layout);
381 381
382 382 }
383 383
384 384 void Axis::handleGeometryChanged(const QRectF& rect)
385 385 {
386 386 m_rect = rect;
387 387 if(isEmpty()) return;
388 388 QVector<qreal> layout = calculateLayout();
389 389 updateLayout(layout);
390 390 }
391 391
392 392 void Axis::axisSelected()
393 393 {
394 394 qDebug()<<"TODO axis clicked";
395 395 }
396 396
397 397 //TODO "nice numbers algorithm"
398 398 #include "moc_axisitem_p.cpp"
399 399
400 400 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,124 +1,124
1 1 #ifndef AXISITEM_H_
2 2 #define AXISITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include "chart_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11 class ChartPresenter;
12 12
13 13 class Axis : public Chart
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 enum AxisType{X_AXIS,Y_AXIS};
18 18
19 19 Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type = X_AXIS);
20 20 ~Axis();
21 21
22 22 AxisType axisType() const {return m_type;};
23 23
24 24 void setAxisOpacity(qreal opacity);
25 25 qreal axisOpacity() const;
26 26
27 27 void setGridOpacity(qreal opacity);
28 28 qreal gridOpacity() const;
29 29
30 30 void setLabelsOpacity(qreal opacity);
31 31 qreal labelsOpacity() const;
32 32
33 33 void setShadesOpacity(qreal opacity);
34 34 qreal shadesOpacity() const;
35 35
36 36 void setLabelsAngle(int angle);
37 37 int labelsAngle()const { return m_labelsAngle; }
38 38
39 39 void setShadesBrush(const QBrush& brush);
40 40 void setShadesPen(const QPen& pen);
41 41
42 42 void setAxisPen(const QPen& pen);
43 43 void setGridPen(const QPen& pen);
44 44
45 45 void setLabelsPen(const QPen& pen);
46 46 void setLabelsBrush(const QBrush& brush);
47 47 void setLabelsFont(const QFont& font);
48 48
49 49 inline QRectF geometry() const { return m_rect; }
50 50 inline QVector<qreal> layout() { return m_layoutVector;};
51 51
52 52 public slots:
53 53 void handleAxisUpdated();
54 54 void handleAxisCategoriesUpdated();
55 55 void handleRangeChanged(qreal min , qreal max,int tickCount);
56 56 void handleGeometryChanged(const QRectF& size);
57 57
58 58
59 59 private:
60 60 inline bool isEmpty();
61 61 void createItems(int count);
62 62 void deleteItems(int count);
63 63
64 64 QVector<qreal> calculateLayout() const;
65 65 void updateLayout(QVector<qreal>& layout);
66 66 void setLayout(QVector<qreal>& layout);
67 67
68 68 QStringList createLabels(int ticks, qreal min, qreal max) const;
69 69 void axisSelected();
70 70
71 71 private:
72 72 QChartAxis* m_chartAxis;
73 73 AxisType m_type;
74 74 QRectF m_rect;
75 75 int m_labelsAngle;
76 QGraphicsItemGroup *m_grid;
77 QGraphicsItemGroup *m_shades;
78 QGraphicsItemGroup *m_labels;
79 QGraphicsItemGroup *m_axis;
76 QGraphicsItemGroup m_grid;
77 QGraphicsItemGroup m_shades;
78 QGraphicsItemGroup m_labels;
79 QGraphicsItemGroup m_axis;
80 80 QVector<qreal> m_layoutVector;
81 81 qreal m_min;
82 82 qreal m_max;
83 83 int m_ticksCount;
84 84 qreal m_zoomFactor;
85 85
86 86 friend class AxisAnimation;
87 87 friend class AxisItem;
88 88
89 89 };
90 90
91 91 class AxisItem: public QGraphicsLineItem
92 92 {
93 93 public:
94 94
95 95 AxisItem(Axis* axis,QGraphicsItem* parent=0):QGraphicsLineItem(parent),m_axis(axis){};
96 96
97 97 protected:
98 98 void mousePressEvent(QGraphicsSceneMouseEvent *event)
99 99 {
100 100 Q_UNUSED(event)
101 101 m_axis->axisSelected();
102 102 }
103 103
104 104 QRectF boundingRect() const
105 105 {
106 106 return shape().boundingRect();
107 107 }
108 108
109 109 QPainterPath shape() const
110 110 {
111 111 QPainterPath path = QGraphicsLineItem::shape();
112 112 QRectF rect = path.boundingRect();
113 113 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?8:0,m_axis->axisType()!=Axis::Y_AXIS?8:0));
114 114 return path;
115 115 }
116 116
117 117 private:
118 118 Axis* m_axis;
119 119
120 120 };
121 121
122 122 QTCOMMERCIALCHART_END_NAMESPACE
123 123
124 124 #endif /* AXISITEM_H_ */
General Comments 0
You need to be logged in to leave comments. Login now