##// END OF EJS Templates
Adds pimpl for qchartaxis and qchartaxiscategories
Michal Klocek -
r963:1ec42cbed063
parent child
Show More
@@ -0,0 +1,85
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef QCHARTAXIS_P_H_
31 #define QCHARTAXIS_P_H_
32
33 #include "qchartaxis.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class QChartAxisPrivate : public QObject
38 {
39 Q_OBJECT
40 public:
41 QChartAxisPrivate(QChartAxis *q);
42 ~QChartAxisPrivate();
43
44 Q_SIGNALS:
45 void updated();
46 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
47
48 public Q_SLOTS:
49 void handleAxisRangeChanged(qreal min, qreal max,int count);
50
51 private:
52 QChartAxis *q_ptr;
53
54 bool m_axisVisible;
55 QPen m_axisPen;
56 QBrush m_axisBrush;
57
58 bool m_gridLineVisible;
59 QPen m_gridLinePen;
60
61 bool m_labelsVisible;
62 QPen m_labelsPen;
63 QBrush m_labelsBrush;
64 QFont m_labelsFont;
65 int m_labelsAngle;
66
67 bool m_shadesVisible;
68 QPen m_shadesPen;
69 QBrush m_shadesBrush;
70 qreal m_shadesOpacity;
71
72 qreal m_min;
73 qreal m_max;
74
75 int m_ticksCount;
76 QChartAxisCategories m_category;
77
78 bool m_niceNumbers;
79
80 friend class QChartAxis;
81 };
82
83 QTCOMMERCIALCHART_END_NAMESPACE
84
85 #endif
@@ -0,0 +1,56
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef QCHARTAXISCATEGORIES_P_H_
31 #define QCHARTAXISCATEGORIES_P_H_
32
33 #include "qchartaxiscategories.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class QChartAxisCategoriesPrivate : public QObject
38 {
39 Q_OBJECT
40 public:
41 QChartAxisCategoriesPrivate(QChartAxisCategories *q);
42 ~QChartAxisCategoriesPrivate();
43
44 Q_SIGNALS:
45 void updated();
46
47 private:
48 QChartAxisCategories *q_ptr;
49 QMap<qreal,QString> m_map;
50 friend class QChartAxisCategories;
51
52 };
53
54 QTCOMMERCIALCHART_END_NAMESPACE
55
56 #endif
@@ -1,464 +1,465
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "axis_p.h"
22 22 #include "qchartaxis.h"
23 #include "qchartaxis_p.h"
24 #include "qchartaxiscategories_p.h"
23 25 #include "chartpresenter_p.h"
24 26 #include "chartanimator_p.h"
25 27 #include <QPainter>
26 28 #include <cmath>
27 29
28 30 static int label_padding = 5;
29 31
30 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 33
32 34 Axis::Axis(QChartAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter),
33 35 m_chartAxis(axis),
34 36 m_type(type),
35 37 m_labelsAngle(0),
36 38 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
37 39 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
38 40 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
39 41 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
40 42 m_min(0),
41 43 m_max(0),
42 44 m_ticksCount(0)
43 45 {
44 46 //initial initialization
45 47 m_axis->setZValue(ChartPresenter::AxisZValue);
46 48 m_axis->setHandlesChildEvents(false);
47 49
48 50 m_shades->setZValue(ChartPresenter::ShadesZValue);
49 51 m_grid->setZValue(ChartPresenter::GridZValue);
50 52
51 connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
52 connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
53 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
54 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
53 55
54 56 handleAxisUpdated();
55 57 }
56 58
57 59 Axis::~Axis()
58 60 {
59 61 }
60 62
61 63 void Axis::createItems(int count)
62 64 {
63 65
64 66 if (m_axis->children().size() == 0)
65 67 m_axis->addToGroup(new AxisItem(this));
66 68 for (int i = 0; i < count; ++i) {
67 69 m_grid->addToGroup(new QGraphicsLineItem());
68 70 m_labels->addToGroup(new QGraphicsSimpleTextItem());
69 71 m_axis->addToGroup(new QGraphicsLineItem());
70 72 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
71 73 }
72 74 }
73 75
74 76 void Axis::deleteItems(int count)
75 77 {
76 78 QList<QGraphicsItem *> lines = m_grid->childItems();
77 79 QList<QGraphicsItem *> labels = m_labels->childItems();
78 80 QList<QGraphicsItem *> shades = m_shades->childItems();
79 81 QList<QGraphicsItem *> axis = m_axis->childItems();
80 82
81 83 for (int i = 0; i < count; ++i) {
82 84 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
83 85 delete(lines.takeLast());
84 86 delete(labels.takeLast());
85 87 delete(axis.takeLast());
86 88 }
87 89 }
88 90
89 91 void Axis::updateLayout(QVector<qreal> &layout)
90 92 {
91 93 if (animator()) {
92 94 animator()->updateLayout(this,layout);
93 95 } else {
94 96 setLayout(layout);
95 97 }
96 98 }
97 99
98 100 bool Axis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
99 101 {
100 102 Q_ASSERT(max>=min);
101 103 Q_ASSERT(ticks>1);
102 104
103 105 QChartAxisCategories* categories = m_chartAxis->categories();
104 106
105 107 bool category = categories->count()>0;
106 108
107 109 if (!category) {
108 110 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
109 111 n++;
110 112 for (int i=0; i< ticks; i++) {
111 113 qreal value = min + (i * (max - min)/ (ticks-1));
112 114 labels << QString::number(value,'f',n);
113 115 }
114 116 } else {
115 117 QList<qreal> values = categories->values();
116 118 for (int i=0; i< ticks; i++) {
117 119 qreal value = (min + (i * (max - min)/ (ticks-1)));
118 120 int j=0;
119 121 for (; j<values.count(); j++) {
120 122 if (values.at(j) > value) break;
121 123 }
122 124 if (j!=0) value=values.at(j-1);
123 125
124 126 QString label = categories->label(value);
125 127 labels << label;
126 128 }
127 129 }
128 130
129 131 return category;
130 132 }
131 133
132 134 void Axis::setAxisOpacity(qreal opacity)
133 135 {
134 136 m_axis->setOpacity(opacity);
135 137 }
136 138
137 139 qreal Axis::axisOpacity() const
138 140 {
139 141 return m_axis->opacity();
140 142 }
141 143
142 144 void Axis::setGridOpacity(qreal opacity)
143 145 {
144 146 m_grid->setOpacity(opacity);
145 147 }
146 148
147 149 qreal Axis::gridOpacity() const
148 150 {
149 151 return m_grid->opacity();
150 152 }
151 153
152 154 void Axis::setLabelsOpacity(qreal opacity)
153 155 {
154 156 m_labels->setOpacity(opacity);
155 157 }
156 158
157 159 qreal Axis::labelsOpacity() const
158 160 {
159 161 return m_labels->opacity();
160 162 }
161 163
162 164 void Axis::setShadesOpacity(qreal opacity)
163 165 {
164 166 m_shades->setOpacity(opacity);
165 167 }
166 168
167 169 qreal Axis::shadesOpacity() const
168 170 {
169 171 return m_shades->opacity();
170 172 }
171 173
172 174 void Axis::setLabelsAngle(int angle)
173 175 {
174 176 foreach(QGraphicsItem* item , m_labels->childItems()) {
175 177 item->setRotation(angle);
176 178 }
177 179
178 180 m_labelsAngle=angle;
179 181 }
180 182
181 183 void Axis::setLabelsPen(const QPen &pen)
182 184 {
183 185 foreach(QGraphicsItem* item , m_labels->childItems()) {
184 186 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
185 187 }
186 188 }
187 189
188 190 void Axis::setLabelsBrush(const QBrush &brush)
189 191 {
190 192 foreach(QGraphicsItem* item , m_labels->childItems()) {
191 193 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
192 194 }
193 195 }
194 196
195 197 void Axis::setLabelsFont(const QFont &font)
196 198 {
197 199 foreach(QGraphicsItem* item , m_labels->childItems()) {
198 200 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
199 201 }
200 202 }
201 203
202 204 void Axis::setShadesBrush(const QBrush &brush)
203 205 {
204 206 foreach(QGraphicsItem* item , m_shades->childItems()) {
205 207 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
206 208 }
207 209 }
208 210
209 211 void Axis::setShadesPen(const QPen &pen)
210 212 {
211 213 foreach(QGraphicsItem* item , m_shades->childItems()) {
212 214 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
213 215 }
214 216 }
215 217
216 218 void Axis::setAxisPen(const QPen &pen)
217 219 {
218 220 foreach(QGraphicsItem* item , m_axis->childItems()) {
219 221 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
220 222 }
221 223 }
222 224
223 225 void Axis::setGridPen(const QPen &pen)
224 226 {
225 227 foreach(QGraphicsItem* item , m_grid->childItems()) {
226 228 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
227 229 }
228 230 }
229 231
230 232 QVector<qreal> Axis::calculateLayout() const
231 233 {
232 234 Q_ASSERT(m_ticksCount>=2);
233 235
234 236 QVector<qreal> points;
235 237 points.resize(m_ticksCount);
236 238
237 239 switch (m_type)
238 240 {
239 241 case X_AXIS:
240 242 {
241 243 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
242 244 for (int i = 0; i < m_ticksCount; ++i) {
243 245 int x = i * deltaX + m_rect.left();
244 246 points[i] = x;
245 247 }
246 248 }
247 249 break;
248 250 case Y_AXIS:
249 251 {
250 252 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
251 253 for (int i = 0; i < m_ticksCount; ++i) {
252 254 int y = i * -deltaY + m_rect.bottom();
253 255 points[i] = y;
254 256 }
255 257 }
256 258 break;
257 259 }
258 260 return points;
259 261 }
260 262
261 263 void Axis::setLayout(QVector<qreal> &layout)
262 264 {
263 265 int diff = m_layoutVector.size() - layout.size();
264 266
265 267 if (diff>0) {
266 268 deleteItems(diff);
267 269 } else if (diff<0) {
268 270 createItems(-diff);
269 271 }
270 272
271 273 if( diff!=0) handleAxisUpdated();
272 274
273 275 QStringList ticksList;
274 276
275 277 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
276 278
277 279 QList<QGraphicsItem *> lines = m_grid->childItems();
278 280 QList<QGraphicsItem *> labels = m_labels->childItems();
279 281 QList<QGraphicsItem *> shades = m_shades->childItems();
280 282 QList<QGraphicsItem *> axis = m_axis->childItems();
281 283
282 284 Q_ASSERT(labels.size() == ticksList.size());
283 285 Q_ASSERT(layout.size() == ticksList.size());
284 286
285 287 qreal minWidth = 0;
286 288 qreal minHeight = 0;
287 289
288 290 switch (m_type)
289 291 {
290 292 case X_AXIS:
291 293 {
292 294 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
293 295 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
294 296
295 297 for (int i = 0; i < layout.size(); ++i) {
296 298 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
297 299 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
298 300 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
299 301 if (!categories || i<1) {
300 302 labelItem->setText(ticksList.at(i));
301 303 const QRectF& rect = labelItem->boundingRect();
302 304 minWidth+=rect.width();
303 305 minHeight=qMax(rect.height(),minHeight);
304 306 QPointF center = rect.center();
305 307 labelItem->setTransformOriginPoint(center.x(), center.y());
306 308 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
307 309 } else {
308 310 labelItem->setText(ticksList.at(i));
309 311 const QRectF& rect = labelItem->boundingRect();
310 312 minWidth+=rect.width();
311 313 minHeight=qMax(rect.height()+label_padding,minHeight);
312 314 QPointF center = rect.center();
313 315 labelItem->setTransformOriginPoint(center.x(), center.y());
314 316 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
315 317 }
316 318
317 319 if ((i+1)%2 && i>1) {
318 320 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
319 321 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
320 322 }
321 323 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
322 324 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
323 325 }
324 326
325 327 }
326 328 break;
327 329
328 330 case Y_AXIS:
329 331 {
330 332 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
331 333 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
332 334
333 335 for (int i = 0; i < layout.size(); ++i) {
334 336 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
335 337 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
336 338 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
337 339
338 340 if (!categories || i<1) {
339 341 labelItem->setText(ticksList.at(i));
340 342 const QRectF& rect = labelItem->boundingRect();
341 343 minWidth=qMax(rect.width()+label_padding,minWidth);
342 344 minHeight+=rect.height();
343 345 QPointF center = rect.center();
344 346 labelItem->setTransformOriginPoint(center.x(), center.y());
345 347 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
346 348 } else {
347 349 labelItem->setText(ticksList.at(i));
348 350 const QRectF& rect = labelItem->boundingRect();
349 351 minWidth=qMax(rect.width(),minWidth);
350 352 minHeight+=rect.height();
351 353 QPointF center = rect.center();
352 354 labelItem->setTransformOriginPoint(center.x(), center.y());
353 355 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
354 356 }
355 357
356 358 if ((i+1)%2 && i>1) {
357 359 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
358 360 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
359 361 }
360 362 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
361 363 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
362 364 }
363 365 }
364 366 break;
365 367 default:
366 368 qDebug()<<"Unknown axis type";
367 369 break;
368 370 }
369 371
370 372 m_layoutVector=layout;
371 373
372 374 presenter()->setMinimumMarginWidth(this,minWidth);
373 375 presenter()->setMinimumMarginHeight(this,minHeight);
374 376
375 377 }
376 378
377 379 bool Axis::isEmpty()
378 380 {
379 381 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
380 382 }
381 383
382 384 //handlers
383 385
384 386 void Axis::handleAxisCategoriesUpdated()
385 387 {
386 388 if (isEmpty()) return;
387 389 updateLayout(m_layoutVector);
388 390 }
389 391
390 392 void Axis::handleAxisUpdated()
391 393 {
392 394
393 395 if (isEmpty()) return;
394 396
395 397 if (m_chartAxis->isAxisVisible()) {
396 398 setAxisOpacity(100);
397 399 } else {
398 400 setAxisOpacity(0);
399 401 }
400 402
401 403 if (m_chartAxis->isGridLineVisible()) {
402 404 setGridOpacity(100);
403 405 } else {
404 406 setGridOpacity(0);
405 407 }
406 408
407 409 if (m_chartAxis->labelsVisible()) {
408 410 setLabelsOpacity(100);
409 411 } else {
410 412 setLabelsOpacity(0);
411 413 }
412 414
413 415 if (m_chartAxis->shadesVisible()) {
414 416 setShadesOpacity(m_chartAxis->shadesOpacity());
415 417 } else {
416 418 setShadesOpacity(0);
417 419 }
418 420
419 421 setLabelsAngle(m_chartAxis->labelsAngle());
420 422 setAxisPen(m_chartAxis->axisPen());
421 423 setLabelsPen(m_chartAxis->labelsPen());
422 424 setLabelsBrush(m_chartAxis->labelsBrush());
423 425 setLabelsFont(m_chartAxis->labelsFont());
424 426 setGridPen(m_chartAxis->gridLinePen());
425 427 setShadesPen(m_chartAxis->shadesPen());
426 428 setShadesBrush(m_chartAxis->shadesBrush());
427 429
428 430 }
429 431
430 432 void Axis::handleRangeChanged(qreal min, qreal max,int tickCount)
431 433 {
432 434 if (qFuzzyIsNull(min - max) || tickCount < 2)
433 435 return;
434 436
435 437 m_min = min;
436 438 m_max = max;
437 439 m_ticksCount= tickCount;
438 440
439 441 if (isEmpty()) return;
440 442 QVector<qreal> layout = calculateLayout();
441 443 updateLayout(layout);
442 444
443 445 }
444 446
445 447 void Axis::handleGeometryChanged(const QRectF &rect)
446 448 {
447 449 if(m_rect != rect)
448 450 {
449 451 m_rect = rect;
450 452 if (isEmpty()) return;
451 453 QVector<qreal> layout = calculateLayout();
452 454 updateLayout(layout);
453 455 }
454 456 }
455 457
456 458 void Axis::axisSelected()
457 459 {
458 qDebug()<<"TODO axis clicked";
460 qDebug()<<"TODO: axis clicked";
459 461 }
460 462
461 //TODO "nice numbers algorithm"
462 463 #include "moc_axis_p.cpp"
463 464
464 465 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,14 +1,16
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/axis.cpp \
6 6 $$PWD/qchartaxis.cpp \
7 7 $$PWD/qchartaxiscategories.cpp
8 8
9 9 PRIVATE_HEADERS += \
10 $$PWD/axis_p.h
10 $$PWD/axis_p.h \
11 $$PWD/qchartaxis_p.h \
12 $$PWD/qchartaxiscategories_p.h
11 13
12 14 PUBLIC_HEADERS += \
13 15 $$PWD/qchartaxis.h \
14 16 $$PWD/qchartaxiscategories.h No newline at end of file
@@ -1,444 +1,545
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qchartaxis.h"
22 #include "qchartaxis_p.h"
22 23
23 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 25
25 26 /*!
26 27 \class QChartAxis
27 28 \brief The QChartAxis class is used for manipulating chart's axis
28 29 and for adding optional axes to the chart.
29 30 \mainclass
30 31
31 32 There is only one x Axis, however there can be multiple y axes.
32 33 Each chart series can be bound to exactly one Y axis and the share common X axis.
33 34 Axis can be setup to show axis line with ticks, gird lines and shades.
34 35
35 36 */
36 37
37 38 /*!
38 39 \fn bool QChartAxis::isAxisVisible() const
39 40 \brief Returns if axis is visible
40 41 \sa setAxisVisible()
41 42 */
42 43
43 44 /*!
44 45 \fn QPen QChartAxis::axisPen() const
45 46 \brief Returns pen used to draw axis and ticks.
46 47 \sa setAxisPen()
47 48 */
48 49
49 50
50 51 /*!
51 52 \fn bool QChartAxis::isGridLineVisible() const
52 53 \brief Returns if grid is visible
53 54 \sa setGridLineVisible()
54 55 */
55 56
56 57 /*!
57 58 \fn QPen QChartAxis::gridLinePen() const
58 59 \brief Returns pen used to draw grid.
59 60 \sa setGridLinePen()
60 61 */
61 62
62 63 /*!
63 64 \fn bool QChartAxis::labelsVisible() const
64 65 \brief Returns if grid is visible
65 66 \sa setLabelsVisible()
66 67 */
67 68
68 69 /*!
69 70 \fn QPen QChartAxis::labelsPen() const
70 71 \brief Returns the pen used to labels.
71 72 \sa setLabelsPen()
72 73 */
73 74
74 75 /*!
75 76 \fn QBrush QChartAxis::labelsBrush() const
76 77 \brief Returns brush used to draw labels.
77 78 \sa setLabelsBrush()
78 79 */
79 80
80 81 /*!
81 82 \fn QFont QChartAxis::labelsFont() const
82 83 \brief Returns font used to draw labels.
83 84 \sa setLabelsFont()
84 85 */
85 86
86 87 /*!
87 88 \fn QFont QChartAxis::labelsAngle() const
88 89 \brief Returns angle used to draw labels.
89 90 \sa setLabelsAngle()
90 91 */
91 92
92 93 /*!
93 94 \fn bool QChartAxis::shadesVisible() const
94 95 \brief Returns if shades are visible.
95 96 \sa setShadesVisible()
96 97 */
97 98
98 99 /*!
99 100 \fn qreal QChartAxis::shadesOpacity() const
100 101 \brief Returns opacity of shades.
101 102 */
102 103
103 104 /*!
104 105 \fn QPen QChartAxis::shadesPen() const
105 106 \brief Returns pen used to draw shades.
106 107 \sa setShadesPen()
107 108 */
108 109
109 110 /*!
110 111 \fn QBrush QChartAxis::shadesBrush() const
111 112 \brief Returns brush used to draw shades.
112 113 \sa setShadesBrush()
113 114 */
114 115
115 116 /*!
116 117 \fn qreal QChartAxis::min() const
117 118 \brief Returns minimum value on the axis.
118 119 \sa setMin()
119 120 */
120 121
121 122 /*!
122 123 \fn qreal QChartAxis::max() const
123 124 \brief Returns maximim value on the axis.
124 125 \sa setMax()
125 126 */
126 127
127 128 /*!
128 129 \fn void QChartAxis::minChanged(qreal min)
129 130 \brief Axis emits signal when \a min of axis has changed.
130 131 */
131 132
132 133 /*!
133 134 \fn void QChartAxis::maxChanged(qreal max)
134 135 \brief Axis emits signal when \a max of axis has changed.
135 136 */
136 137
137 138 /*!
138 139 \fn void QChartAxis::rangeChanged(qreal min, qreal max)
139 140 \brief Axis emits signal when \a min or \a max of axis has changed.
140 141 */
141 142
142 143 /*!
143 144 \fn int QChartAxis::ticksCount() const
144 145 \brief Return number of ticks on the axis
145 146 \sa setTicksCount()
146 147 */
147 148
148 149 /*!
149 150 \fn void QChartAxis::updated()
150 151 \brief \internal
151 152 */
152 153
153 154 /*!
154 155 \fn void QChartAxis::changed(qreal min, qreal max, int tickCount,bool niceNumbers)
155 156 \brief \internal
156 157 */
157 158
158 159 /*!
159 160 \fn bool QChartAxis::niceNumbers() const
160 161 \brief Returns whether nice numbers are enabled or not.
161 162 */
162 163
163 164 /*!
164 165 \fn QChartAxisCategories* QChartAxis::categories()
165 166 \brief Returns pointer to the list of categories which correspond to the values on the axis.
166 167 */
167 168
168 169 /*!
169 170 \fn void QChartAxis::ticksCountChanged(int count)
170 171 \brief Emits the new \a count of ticks on the axis
171 172 Signal is emitted when the number of the ticks on the axis has been changed to a different value.
172 173 Parementer count\a count is the new number of ticks on the axis.
173 174 */
174 175
175 176 /*!
176 177 Constructs new axis object which is a child of \a parent. Ownership is taken by
177 178 QChatView or QChart when axis added.
178 179 */
179 180
180 181 QChartAxis::QChartAxis(QObject *parent) : QObject(parent),
181 m_axisVisible(true),
182 m_gridLineVisible(true),
183 m_labelsVisible(true),
184 m_labelsAngle(0),
185 m_shadesVisible(false),
186 m_shadesOpacity(1.0),
187 m_min(0),
188 m_max(0),
189 m_ticksCount(5),
190 m_niceNumbers(false)
182 d_ptr(new QChartAxisPrivate(this))
191 183 {
192 184
193 185 }
194 186
195 187 /*!
196 188 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
197 189 */
198 190
199 191 QChartAxis::~QChartAxis()
200 192 {
201 193 }
202 194
203 195 /*!
204 196 Sets \a pen used to draw axis line and ticks.
205 197 */
206 198 void QChartAxis::setAxisPen(const QPen &pen)
207 199 {
208 if (pen != m_axisPen) {
209 m_axisPen = pen;
210 emit updated();
200 if (d_ptr->m_axisPen!=pen) {
201 d_ptr->m_axisPen = pen;
202 emit d_ptr->updated();
211 203 }
212 204 }
213 205
206 QPen QChartAxis::axisPen() const
207 {
208 return d_ptr->m_axisPen;
209 }
210
214 211 /*!
215 212 Sets if axis and ticks are \a visible.
216 213 */
217 214 void QChartAxis::setAxisVisible(bool visible)
218 215 {
219 if (m_axisVisible != visible) {
220 m_axisVisible = visible;
221 emit updated();
216 if (d_ptr->m_axisVisible != visible) {
217 d_ptr->m_axisVisible = visible;
218 emit d_ptr->updated();
222 219 }
223 220 }
224 221
222 bool QChartAxis::isAxisVisible() const
223 {
224 return d_ptr->m_axisVisible;
225 }
226
225 227 /*!
226 228 Sets if grid line is \a visible.
227 229 */
228 230 void QChartAxis::setGridLineVisible(bool visible)
229 231 {
230 if (m_gridLineVisible != visible) {
231 m_gridLineVisible = visible;
232 emit updated();
232 if (d_ptr->m_gridLineVisible != visible) {
233 d_ptr->m_gridLineVisible = visible;
234 emit d_ptr->updated();
233 235 }
234 236 }
235 237
238 bool QChartAxis::isGridLineVisible() const
239 {
240 return d_ptr->m_gridLineVisible;
241 }
236 242 /*!
237 243 Sets \a pen used to draw grid line.
238 244 */
239 245 void QChartAxis::setGridLinePen(const QPen &pen)
240 246 {
241 if (m_gridLinePen != pen) {
242 m_gridLinePen = pen;
243 emit updated();
247 if (d_ptr->m_gridLinePen != pen) {
248 d_ptr->m_gridLinePen = pen;
249 emit d_ptr->updated();
244 250 }
245 251 }
246 252
253 QPen QChartAxis::gridLinePen() const
254 {
255 return d_ptr->m_gridLinePen;
256 }
257
247 258 /*!
248 259 Sets if axis' labels are \a visible.
249 260 */
250 261 void QChartAxis::setLabelsVisible(bool visible)
251 262 {
252 if (m_labelsVisible != visible) {
253 m_labelsVisible = visible;
254 emit updated();
263 if (d_ptr->m_labelsVisible != visible) {
264 d_ptr->m_labelsVisible = visible;
265 emit d_ptr->updated();
255 266 }
256 267 }
257 268
269 bool QChartAxis::labelsVisible() const
270 {
271 return d_ptr->m_labelsVisible;
272 }
258 273 /*!
259 274 Sets \a pen used to draw labels.
260 275 */
261 276 void QChartAxis::setLabelsPen(const QPen &pen)
262 277 {
263 if (m_labelsPen != pen) {
264 m_labelsPen = pen;
265 emit updated();
278 if (d_ptr->m_labelsPen != pen) {
279 d_ptr->m_labelsPen = pen;
280 emit d_ptr->updated();
266 281 }
267 282 }
268 283
284 QPen QChartAxis::labelsPen() const
285 {
286 return d_ptr->m_labelsPen;
287 }
288
269 289 /*!
270 290 Sets \a brush used to draw labels.
271 291 */
272 292 void QChartAxis::setLabelsBrush(const QBrush &brush)
273 293 {
274 if (m_labelsBrush != brush) {
275 m_labelsBrush = brush;
276 emit updated();
294 if (d_ptr->m_labelsBrush != brush) {
295 d_ptr->m_labelsBrush = brush;
296 emit d_ptr->updated();
277 297 }
278 298 }
279 299
300 QBrush QChartAxis::labelsBrush() const
301 {
302 return d_ptr->m_labelsBrush;
303 }
304
280 305 /*!
281 306 Sets \a font used to draw labels.
282 307 */
283 308 void QChartAxis::setLabelsFont(const QFont &font)
284 309 {
285 if (m_labelsFont != font) {
286 m_labelsFont = font;
287 emit updated();
310 if (d_ptr->m_labelsFont != font) {
311 d_ptr->m_labelsFont = font;
312 emit d_ptr->updated();
288 313 }
289 314 }
290 315
316 QFont QChartAxis::labelsFont() const
317 {
318 return d_ptr->m_labelsFont;
319 }
320
291 321 /*!
292 322 Sets \a angle for all the labels on given axis.
293 323 */
294 324 void QChartAxis::setLabelsAngle(int angle)
295 325 {
296 if (m_labelsAngle != angle) {
297 m_labelsAngle = angle;
298 emit updated();
326 if (d_ptr->m_labelsAngle != angle) {
327 d_ptr->m_labelsAngle = angle;
328 emit d_ptr->updated();
299 329 }
300 330 }
301 331
332 int QChartAxis::labelsAngle() const
333 {
334 return d_ptr->m_labelsAngle;
335 }
336
302 337 /*!
303 338 Sets if shades are \a visible.
304 339 */
305 340 void QChartAxis::setShadesVisible(bool visible)
306 341 {
307 if (m_shadesVisible != visible) {
308 m_shadesVisible = visible;
309 emit updated();
342 if (d_ptr->m_shadesVisible != visible) {
343 d_ptr->m_shadesVisible = visible;
344 emit d_ptr->updated();
310 345 }
311 346 }
312 347
348 bool QChartAxis::shadesVisible() const
349 {
350 return d_ptr->m_shadesVisible;
351 }
352
313 353 /*!
314 354 Sets \a pen used to draw shades.
315 355 */
316 356 void QChartAxis::setShadesPen(const QPen &pen)
317 357 {
318 if (m_shadesPen != pen) {
319 m_shadesPen = pen;
320 emit updated();
358 if (d_ptr->m_shadesPen != pen) {
359 d_ptr->m_shadesPen = pen;
360 emit d_ptr->updated();
321 361 }
322 362 }
323 363
364 QPen QChartAxis::shadesPen() const
365 {
366 return d_ptr->m_shadesPen;
367 }
368
324 369 /*!
325 370 Sets \a brush used to draw shades.
326 371 */
327 372 void QChartAxis::setShadesBrush(const QBrush &brush)
328 373 {
329 if (m_shadesBrush != brush) {
330 m_shadesBrush = brush;
331 emit updated();
374 if (d_ptr->m_shadesBrush != brush) {
375 d_ptr->m_shadesBrush = brush;
376 emit d_ptr->updated();
332 377 }
333 378 }
334 379
380 QBrush QChartAxis::shadesBrush() const
381 {
382 return d_ptr->m_shadesBrush;
383 }
384
335 385 /*!
336 386 Sets \a opacity of the shades.
337 387 */
338 388 void QChartAxis::setShadesOpacity(qreal opacity)
339 389 {
340 if (m_shadesOpacity != opacity) {
341 m_shadesOpacity=opacity;
342 emit updated();
390 if (d_ptr->m_shadesOpacity != opacity) {
391 d_ptr->m_shadesOpacity=opacity;
392 emit d_ptr->updated();
343 393 }
344 394 }
345 395
396 qreal QChartAxis::shadesOpacity() const
397 {
398 return d_ptr->m_shadesOpacity;
399 }
400
346 401 /*!
347 402 Sets \a min value on the axis.
348 403 */
349 404 void QChartAxis::setMin(qreal min)
350 405 {
351 setRange(min,m_max);
406 setRange(min,d_ptr->m_max);
407 }
408
409 qreal QChartAxis::min() const
410 {
411 return d_ptr->m_min;
352 412 }
353 413
354 414 /*!
355 415 Sets \a max value on the axis.
356 416 */
357 417 void QChartAxis::setMax(qreal max)
358 418 {
359 setRange(m_min,max);
419 setRange(d_ptr->m_min,max);
420 }
421
422 qreal QChartAxis::max() const
423 {
424 return d_ptr->m_max;
360 425 }
361 426
362 427 /*!
363 428 Sets range from \a min to \a max on the axis.
364 429 */
365 430 void QChartAxis::setRange(qreal min, qreal max)
366 431 {
367 432 bool changed = false;
368 if (!qFuzzyIsNull(m_min - min)) {
369 m_min = min;
433 if (!qFuzzyIsNull(d_ptr->m_min - min)) {
434 d_ptr->m_min = min;
370 435 changed = true;
371 436 emit minChanged(min);
372 437 }
373 438
374 if (!qFuzzyIsNull(m_max - max)) {
375 m_max = max;
439 if (!qFuzzyIsNull(d_ptr->m_max - max)) {
440 d_ptr->m_max = max;
376 441 changed = true;
377 442 emit maxChanged(max);
378 443 }
379 444
380 445 if (changed) {
381 emit rangeChanged(m_min,m_max);
382 emit this->changed(m_min, m_max, m_ticksCount, m_niceNumbers);
446 emit rangeChanged(d_ptr->m_min,d_ptr->m_max);
447 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
383 448 }
384 449 }
385 450
386 451 /*!
387 452 Sets \a count for ticks on the axis.
388 453 */
389 454 void QChartAxis::setTicksCount(int count)
390 455 {
391 if (m_ticksCount != count) {
392 m_ticksCount = count;
456 if (d_ptr->m_ticksCount != count) {
457 d_ptr->m_ticksCount = count;
393 458 emit ticksCountChanged(count);
394 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
459 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
395 460 }
396 461 }
397 462
463 int QChartAxis::ticksCount() const
464 {
465 return d_ptr->m_ticksCount;
466 }
467
398 468 /*!
399 469 Sets axis, shades, labels and grid lines to be visible.
400 470 */
401 471 void QChartAxis::show()
402 472 {
403 m_axisVisible=true;
404 m_gridLineVisible=true;
405 m_labelsVisible=true;
406 m_shadesVisible=true;
407 emit updated();
473 d_ptr->m_axisVisible=true;
474 d_ptr->m_gridLineVisible=true;
475 d_ptr->m_labelsVisible=true;
476 d_ptr->m_shadesVisible=true;
477 emit d_ptr->updated();
408 478 }
409 479
410 480 /*!
411 481 Sets axis, shades, labels and grid lines to not be visible.
412 482 */
413 483 void QChartAxis::hide()
414 484 {
415 m_axisVisible = false;
416 m_gridLineVisible = false;
417 m_labelsVisible = false;
418 m_shadesVisible = false;
419 emit updated();
420 }
421
422 /*!
423 \internal
424 */
425 void QChartAxis::handleAxisRangeChanged(qreal min, qreal max,int count)
426 {
427 setRange(min,max);
428 setTicksCount(count);
485 d_ptr->m_axisVisible = false;
486 d_ptr->m_gridLineVisible = false;
487 d_ptr->m_labelsVisible = false;
488 d_ptr->m_shadesVisible = false;
489 emit d_ptr->updated();
429 490 }
430 491
431 492 /*!
432 493 Sets the nice numbers state to \a enable
433 494 */
434 495 void QChartAxis::setNiceNumbers(bool enable)
435 496 {
436 if (m_niceNumbers != enable){
437 m_niceNumbers = enable;
438 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
497 if (d_ptr->m_niceNumbers != enable){
498 d_ptr->m_niceNumbers = enable;
499 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
439 500 }
440 501 }
441 502
503 bool QChartAxis::niceNumbers() const
504 {
505 return d_ptr->m_niceNumbers;
506 }
507
508 QChartAxisCategories* QChartAxis::categories()
509 {
510 return &d_ptr->m_category;
511 }
512
513 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
514
515 QChartAxisPrivate::QChartAxisPrivate(QChartAxis* q):
516 q_ptr(q),
517 m_axisVisible(true),
518 m_gridLineVisible(true),
519 m_labelsVisible(true),
520 m_labelsAngle(0),
521 m_shadesVisible(false),
522 m_shadesOpacity(1.0),
523 m_min(0),
524 m_max(0),
525 m_ticksCount(5),
526 m_niceNumbers(false)
527 {
528
529 }
530
531 QChartAxisPrivate::~QChartAxisPrivate()
532 {
533
534 }
535
536 void QChartAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
537 {
538 q_ptr->setRange(min,max);
539 q_ptr->setTicksCount(count);
540 }
541
442 542 #include "moc_qchartaxis.cpp"
543 #include "moc_qchartaxis_p.cpp"
443 544
444 545 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,134 +1,108
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHARTAXIS_H_
22 22 #define QCHARTAXIS_H_
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qchartaxiscategories.h>
26 26 #include <QPen>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 class QChartAxisPrivate;
32
31 33 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
32 34 {
33 35 Q_OBJECT
34 36 public:
35 37
36 38 QChartAxis(QObject *parent =0);
37 39 ~QChartAxis();
38 40
39 41 //axis handling
40 bool isAxisVisible() const { return m_axisVisible; }
42 bool isAxisVisible() const;
41 43 void setAxisVisible(bool visible);
42 44 void setAxisPen(const QPen &pen);
43 QPen axisPen() const { return m_axisPen; }
45 QPen axisPen() const;
44 46
45 47 //grid handling
46 bool isGridLineVisible() const { return m_gridLineVisible; }
48 bool isGridLineVisible() const;
47 49 void setGridLineVisible(bool visible);
48 50 void setGridLinePen(const QPen &pen);
49 QPen gridLinePen() const { return m_gridLinePen; }
51 QPen gridLinePen() const;
50 52
51 53 //labels handling
52 bool labelsVisible() const { return m_labelsVisible; }
54 bool labelsVisible() const;
53 55 void setLabelsVisible(bool visible);
54 56 void setLabelsPen(const QPen &pen);
55 QPen labelsPen() const { return m_labelsPen;}
57 QPen labelsPen() const;
56 58 void setLabelsBrush(const QBrush &brush);
57 QBrush labelsBrush() const { return m_labelsBrush; }
59 QBrush labelsBrush() const;
58 60 void setLabelsFont(const QFont &font);
59 QFont labelsFont() const { return m_labelsFont; }
61 QFont labelsFont() const;
60 62 void setLabelsAngle(int angle);
61 int labelsAngle() const { return m_labelsAngle; }
63 int labelsAngle() const;
62 64
63 65 //shades handling
64 bool shadesVisible() const { return m_shadesVisible; }
66 bool shadesVisible() const;
65 67 void setShadesVisible(bool visible);
66 68 void setShadesPen(const QPen &pen);
67 QPen shadesPen() const { return m_shadesPen; }
69 QPen shadesPen() const;
68 70 void setShadesBrush(const QBrush &brush);
69 QBrush shadesBrush() const { return m_shadesBrush; }
71 QBrush shadesBrush() const;
70 72 void setShadesOpacity(qreal opacity);
71 qreal shadesOpacity() const { return m_shadesOpacity; }
73 qreal shadesOpacity() const;
72 74
73 75 //range handling
74 76 void setMin(qreal min);
75 qreal min() const { return m_min; }
77 qreal min() const;
76 78 void setMax(qreal max);
77 qreal max() const { return m_max; }
79 qreal max() const;
78 80 void setRange(qreal min, qreal max);
79 81
80 82 //ticks handling
81 83 void setTicksCount(int count);
82 int ticksCount() const { return m_ticksCount;}
84 int ticksCount() const;
83 85
84 86 void setNiceNumbers(bool enable);
85 bool niceNumbers() const { return m_niceNumbers;}
87 bool niceNumbers() const;
86 88
87 QChartAxisCategories* categories() { return &m_category; }
89 QChartAxisCategories* categories();
88 90
89 91 void show();
90 92 void hide();
91 93
92 94 Q_SIGNALS:
93 95 void minChanged(qreal min);
94 96 void maxChanged(qreal max);
95 97 void rangeChanged(qreal min, qreal max);
96 98 void ticksCountChanged(int count);
97 99
98 //interal signal
99 void updated();
100 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
101 //internal slot
102 public Q_SLOTS:
103 void handleAxisRangeChanged(qreal min, qreal max,int count);
104
105 100 private:
106 bool m_axisVisible;
107 QPen m_axisPen;
108 QBrush m_axisBrush;
109
110 bool m_gridLineVisible;
111 QPen m_gridLinePen;
112
113 bool m_labelsVisible;
114 QPen m_labelsPen;
115 QBrush m_labelsBrush;
116 QFont m_labelsFont;
117 int m_labelsAngle;
118
119 bool m_shadesVisible;
120 QPen m_shadesPen;
121 QBrush m_shadesBrush;
122 qreal m_shadesOpacity;
123
124 qreal m_min;
125 qreal m_max;
126
127 int m_ticksCount;
128 QChartAxisCategories m_category;
129
130 bool m_niceNumbers;
101 QScopedPointer<QChartAxisPrivate> d_ptr;
102 Q_DISABLE_COPY(QChartAxis);
103 friend class ChartDataSet;
104 friend class Axis;
131 105 };
132 106
133 107 QTCOMMERCIALCHART_END_NAMESPACE
134 108 #endif /* QCHARTAXIS_H_ */
@@ -1,81 +1,96
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qchartaxiscategories.h"
22 #include "qchartaxiscategories_p.h"
22 23
23 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 25
25 QChartAxisCategories::QChartAxisCategories()
26 QChartAxisCategories::QChartAxisCategories():
27 d_ptr(new QChartAxisCategoriesPrivate(this))
26 28 {
27 // TODO Auto-generated constructor stub
28 29
29 30 }
30 31
31 32 QChartAxisCategories::~QChartAxisCategories()
32 33 {
33 // TODO Auto-generated destructor stub
34
34 35 }
35 36
36 37 void QChartAxisCategories::insert(const QBarCategories &categories)
37 38 {
38 39 int i=1;
39 40 foreach (QString string , categories) {
40 m_map.insert(i,string);
41 d_ptr->m_map.insert(i,string);
41 42 i++;
42 43 }
43 emit updated();
44 emit d_ptr->updated();
44 45 }
45 46
46 47 void QChartAxisCategories::insert(qreal value,QString label)
47 48 {
48 m_map.insert(value,label);
49 emit updated();
49 d_ptr->m_map.insert(value,label);
50 emit d_ptr->updated();
50 51 }
51 52
52 53 void QChartAxisCategories::remove(qreal value)
53 54 {
54 m_map.remove(value);
55 emit updated();
55 d_ptr->m_map.remove(value);
56 emit d_ptr->updated();
56 57 }
57 58
58 59 void QChartAxisCategories::clear()
59 60 {
60 m_map.clear();
61 emit updated();
61 d_ptr->m_map.clear();
62 emit d_ptr->updated();
62 63 }
63 64
64 65 int QChartAxisCategories::count()
65 66 {
66 return m_map.count();
67 return d_ptr->m_map.count();
67 68 }
68 69
69 70 QList<qreal> QChartAxisCategories::values() const
70 71 {
71 return m_map.keys();
72 return d_ptr->m_map.keys();
72 73 }
73 74
74 75 QString QChartAxisCategories::label(qreal value) const
75 76 {
76 return m_map.value(value);
77 return d_ptr->m_map.value(value);
78 }
79
80 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81
82 QChartAxisCategoriesPrivate::QChartAxisCategoriesPrivate(QChartAxisCategories *q):
83 q_ptr(q)
84 {
85
86 }
87
88 QChartAxisCategoriesPrivate::~QChartAxisCategoriesPrivate()
89 {
90
77 91 }
78 92
79 93 #include "moc_qchartaxiscategories.cpp"
94 #include "moc_qchartaxiscategories_p.cpp"
80 95
81 96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,56
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHARTAXISCATEGORIES_H_
22 22 #define QCHARTAXISCATEGORIES_H_
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qbarseries.h>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 class QChartAxisCategoriesPrivate;
30
29 31 class QTCOMMERCIALCHART_EXPORT QChartAxisCategories : public QObject
30 32 {
31 33 Q_OBJECT
32 34 private:
33 35 QChartAxisCategories();
34 36 public:
35 37 ~QChartAxisCategories();
36 38
37 39 void insert(const QBarCategories &category);
38 40 void insert(qreal value,QString label);
39 41 void remove(qreal value);
40 42 QList<qreal> values() const;
41 43 QString label(qreal value) const;
42 44 void clear();
43 45 int count();
44 46
45 //internal signal
46 Q_SIGNALS:
47 void updated();
48
49 47 private:
50 QMap<qreal,QString> m_map;
51
52 friend class QChartAxis;
48 QScopedPointer<QChartAxisCategoriesPrivate> d_ptr;
49 friend class QChartAxisPrivate;
50 friend class Axis;
53 51 };
54 52
55 53
56 54 QTCOMMERCIALCHART_END_NAMESPACE
57 55
58 56 #endif /* QCHARTAXISCATEGORIES_H_ */
@@ -1,259 +1,255
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartdataset_p.h"
22 22 #include "qchartaxis.h"
23 //series
23 #include "qchartaxis_p.h"
24 24 #include "qseries_p.h"
25 #include "qlineseries.h"
26 #include "qareaseries.h"
27 25 #include "qbarseries.h"
28 26 #include "qstackedbarseries.h"
29 27 #include "qpercentbarseries.h"
30 28 #include "qpieseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
33 29
34 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 31
36 32 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
37 33 m_axisX(new QChartAxis(this)),
38 34 m_axisY(new QChartAxis(this)),
39 35 m_domainIndex(0),
40 36 m_axisXInitialized(false)
41 37 {
42 38 }
43 39
44 40 ChartDataSet::~ChartDataSet()
45 41 {
46 42 }
47 43
48 44 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
49 45 {
50 46 if(axisY==0) axisY = m_axisY;
51 47
52 48 QChartAxis* axis = m_seriesAxisMap.value(series);
53 49
54 50 if(axis) {
55 51 qWarning() << "Can not add series. Series already on the chart";
56 52 return;
57 53 }
58 54
59 55 if(!series->parent()){
60 56 series->setParent(this); // take ownership
61 57 };
62 58
63 59 if(!axisY->parent()){
64 60 axisY->setParent(this); // take ownership
65 61 }
66 62
67 63 Domain* domain = m_axisDomainMap.value(axisY);
68 64
69 65 if(!domain) {
70 66 domain = new Domain(axisY);
71 QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
72 QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
73 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal,int)));
74 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
67 QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
68 QObject::connect(axisX()->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
69 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
70 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX()->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
75 71 //initialize
76 72 m_axisDomainMap.insert(axisY,domain);
77 73 emit axisAdded(axisY,domain);
78 74 }
79 75
80 76 if(!m_axisXInitialized){
81 77 emit axisAdded(axisX(),domain);
82 78 m_axisXInitialized=true;
83 79 }
84 80
85 81 series->d_ptr->scaleDomain(*domain);
86 82
87 83 if(series->type() == QSeries::SeriesTypeBar || series->type() == QSeries::SeriesTypeStackedBar || series->type() == QSeries::SeriesTypePercentBar)
88 84 {
89 85 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
90 86 setupCategories(barSeries);
91 87 }
92 88
93 89 if (series->type()== QSeries::SeriesTypePie && m_seriesAxisMap.count()==0)
94 90 {
95 91 axisX()->hide();
96 92 this->axisY()->hide();
97 93 }
98 94
99 95 m_seriesAxisMap.insert(series,axisY);
100 96
101 97 QMapIterator<int, QSeries*> i(m_indexSeriesMap);
102 98
103 99 int key=0;
104 100 while (i.hasNext()) {
105 101 i.next();
106 102 if(i.key()!=key) {
107 103 break;
108 104 }
109 105 key++;
110 106 }
111 107
112 108 m_indexSeriesMap.insert(key,series);
113 109
114 110 emit seriesAdded(series,domain);
115 111
116 112 }
117 113
118 114 void ChartDataSet::removeSeries(QSeries* series)
119 115 {
120 116
121 117 QChartAxis* axis = m_seriesAxisMap.value(series);
122 118
123 119 if(!axis){
124 120 qWarning()<<"Can not remove series. Series not found on the chart.";
125 121 return;
126 122 }
127 123 emit seriesRemoved(series);
128 124 m_seriesAxisMap.remove(series);
129 125 int key = seriesIndex(series);
130 126 Q_ASSERT(key!=-1);
131 127
132 128 m_indexSeriesMap.remove(key);
133 129
134 130 if(series->parent()==this){
135 131 delete series;
136 132 series=0;
137 133 }
138 134
139 135 QList<QChartAxis*> axes = m_seriesAxisMap.values();
140 136
141 137 int i = axes.indexOf(axis);
142 138
143 139 if(i==-1){
144 140 Domain* domain = m_axisDomainMap.take(axis);
145 141 emit axisRemoved(axis);
146 142 if(axis!=axisY()){
147 143 if(axis->parent()==this){
148 144 delete axis;
149 145 axis=0;
150 146 }
151 147 }
152 148 delete domain;
153 149 }
154 150
155 151 if(m_seriesAxisMap.values().size()==0)
156 152 {
157 153 m_axisXInitialized=false;
158 154 emit axisRemoved(axisX());
159 155 }
160 156 }
161 157
162 158 void ChartDataSet::removeAllSeries()
163 159 {
164 160
165 161 QList<QSeries*> series = m_seriesAxisMap.keys();
166 162
167 163 foreach(QSeries* s , series) {
168 164 removeSeries(s);
169 165 }
170 166
171 167 Q_ASSERT(m_seriesAxisMap.count()==0);
172 168 Q_ASSERT(m_axisDomainMap.count()==0);
173 169
174 170 }
175 171
176 172 void ChartDataSet::setupCategories(QBarSeries* series)
177 173 {
178 174 QChartAxisCategories* categories = axisX()->categories();
179 175 categories->clear();
180 176 categories->insert(series->categories());
181 177 }
182 178
183 179 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
184 180 {
185 181 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
186 182 while (i.hasNext()) {
187 183 i.next();
188 184 i.value()->zoomIn(rect,size);
189 185 }
190 186 }
191 187
192 188 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
193 189 {
194 190 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
195 191 while (i.hasNext()) {
196 192 i.next();
197 193 i.value()->zoomOut(rect,size);
198 194 }
199 195 }
200 196
201 197 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
202 198 {
203 199 int count=0;
204 200 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
205 201 while (i.hasNext()) {
206 202 i.next();
207 203 if(i.key()->type()==type) count++;
208 204 }
209 205 return count;
210 206 }
211 207
212 208 int ChartDataSet::seriesIndex(QSeries *series)
213 209 {
214 210 QMapIterator<int, QSeries*> i(m_indexSeriesMap);
215 211 while (i.hasNext()) {
216 212 i.next();
217 213 if (i.value() == series)
218 214 return i.key();
219 215 }
220 216 return -1;
221 217 }
222 218
223 219 QChartAxis* ChartDataSet::axisY(QSeries* series) const
224 220 {
225 221 if(series == 0) return m_axisY;
226 222 return m_seriesAxisMap.value(series);
227 223 }
228 224
229 225 Domain* ChartDataSet::domain(QSeries* series) const
230 226 {
231 227 QChartAxis* axis = m_seriesAxisMap.value(series);
232 228 if(axis){
233 229 return m_axisDomainMap.value(axis);
234 230 }else
235 231 return 0;
236 232 }
237 233
238 234 Domain* ChartDataSet::domain(QChartAxis* axis) const
239 235 {
240 236 if(!axis || axis==axisX()) {
241 237 return m_axisDomainMap.value(axisY());
242 238 }
243 239 else {
244 240 return m_axisDomainMap.value(axis);
245 241 }
246 242 }
247 243
248 244 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
249 245 {
250 246 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
251 247 while (i.hasNext()) {
252 248 i.next();
253 249 i.value()->move(dx,dy,size);
254 250 }
255 251 }
256 252
257 253 #include "moc_chartdataset_p.cpp"
258 254
259 255 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now