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