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