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