##// END OF EJS Templates
Bugfix: axis visibily issues...
Michal Klocek -
r2297:bf59b22cf3e9
parent child
Show More
@@ -1,508 +1,530
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 "chartaxis_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "qabstractaxis_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "chartlayout_p.h"
26 26 #include "abstractdomain_p.h"
27 27 #include <qmath.h>
28 28 #include <QDateTime>
29 29 #include <QValueAxis>
30 30 #include <QLogValueAxis>
31 31 #include <QGraphicsLayout>
32 32 #include <QFontMetrics>
33 33
34 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 35
36 36 ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
37 37 : ChartElement(item),
38 38 m_axis(axis),
39 39 m_labelsAngle(0),
40 40 m_grid(new QGraphicsItemGroup(item)),
41 41 m_arrow(new QGraphicsItemGroup(item)),
42 42 m_shades(new QGraphicsItemGroup(item)),
43 43 m_labels(new QGraphicsItemGroup(item)),
44 44 m_title(new QGraphicsSimpleTextItem(item)),
45 45 m_animation(0),
46 46 m_labelPadding(5),
47 47 m_intervalAxis(intervalAxis)
48 48 {
49 49 Q_ASSERT(item);
50 50 //initial initialization
51 51 m_arrow->setHandlesChildEvents(false);
52 52 m_arrow->setZValue(ChartPresenter::AxisZValue);
53 53 m_arrow->setVisible(m_axis->isLineVisible());
54 54 m_labels->setZValue(ChartPresenter::AxisZValue);
55 55 m_labels->setVisible(m_axis->labelsVisible());
56 56 m_shades->setZValue(ChartPresenter::ShadesZValue);
57 57 m_shades->setVisible(m_axis->shadesVisible());
58 58 m_grid->setZValue(ChartPresenter::GridZValue);
59 59 m_grid->setVisible(m_axis->isGridLineVisible());
60 60 m_title->setZValue(ChartPresenter::GridZValue);
61 61 connectSlots();
62 62
63 63 setFlag(QGraphicsItem::ItemHasNoContents,true);
64 64 }
65 65
66 66 void ChartAxis::connectSlots()
67 67 {
68 68 QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool)));
69 69 QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool)));
70 70 QObject::connect(m_axis,SIGNAL(gridVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool)));
71 71 QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool)));
72 72 QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool)));
73 73 QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int)));
74 74 QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&)));
75 75 QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&)));
76 76 QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&)));
77 77 QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&)));
78 78 QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&)));
79 79 QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&)));
80 80 QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&)));
81 81 QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&)));
82 82 QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&)));
83 83 QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&)));
84 84 QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&)));
85 QObject::connect(m_axis,SIGNAL(titleVisibleChanged(bool)),this,SLOT(handleTitleVisibleChanged(bool)));
85 86 QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal)));
86 87 }
87 88
88 89 ChartAxis::~ChartAxis()
89 90 {
90 91 }
91 92
92 93 void ChartAxis::setAnimation(AxisAnimation *animation)
93 94 {
94 95 m_animation = animation;
95 96 }
96 97
97 98 void ChartAxis::setLayout(QVector<qreal> &layout)
98 99 {
99 100 m_layoutVector = layout;
100 101 }
101 102
102 103 void ChartAxis::createItems(int count)
103 104 {
104 105 if (m_arrow->childItems().size() == 0){
105 106 QGraphicsLineItem* arrow = new ArrowItem(this, this);
106 107 arrow->setPen(m_axis->linePen());
107 108 m_arrow->addToGroup(arrow);
108 109 }
109 110
110 111 if (m_intervalAxis && m_grid->childItems().size() == 0) {
111 112 for (int i = 0 ; i < 2 ; i ++){
112 113 QGraphicsLineItem* item = new QGraphicsLineItem(this);
113 114 item->setPen(m_axis->gridLinePen());
114 115 m_grid->addToGroup(item);
115 116 }
116 117 }
117 118
118 119 for (int i = 0; i < count; ++i) {
119 120 QGraphicsLineItem* arrow = new QGraphicsLineItem(this);
120 121 arrow->setPen(m_axis->linePen());
121 122 QGraphicsLineItem* grid = new QGraphicsLineItem(this);
122 123 grid->setPen(m_axis->gridLinePen());
123 124 QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this);
124 125 label->setFont(m_axis->labelsFont());
125 126 label->setPen(m_axis->labelsPen());
126 127 label->setBrush(m_axis->labelsBrush());
127 128 m_arrow->addToGroup(arrow);
128 129 m_grid->addToGroup(grid);
129 130 m_labels->addToGroup(label);
130 131
131 132 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){
132 133 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
133 134 shades->setPen(m_axis->shadesPen());
134 135 shades->setBrush(m_axis->shadesBrush());
135 136 m_shades->addToGroup(shades);
136 137 }
137 138 }
138 139
139 140 }
140 141
141 142 void ChartAxis::deleteItems(int count)
142 143 {
143 144 QList<QGraphicsItem *> lines = m_grid->childItems();
144 145 QList<QGraphicsItem *> labels = m_labels->childItems();
145 146 QList<QGraphicsItem *> shades = m_shades->childItems();
146 147 QList<QGraphicsItem *> axis = m_arrow->childItems();
147 148
148 149 for (int i = 0; i < count; ++i) {
149 150 if (lines.size() % 2 && lines.size() > 1)
150 151 delete(shades.takeLast());
151 152 delete(lines.takeLast());
152 153 delete(labels.takeLast());
153 154 delete(axis.takeLast());
154 155 }
155 156 }
156 157
157 158 void ChartAxis::updateLayout(QVector<qreal> &layout)
158 159 {
159 160 int diff = m_layoutVector.size() - layout.size();
160 161
161 162 if (diff > 0)
162 163 deleteItems(diff);
163 164 else if (diff < 0)
164 165 createItems(-diff);
165 166
166 167 if (m_animation) {
167 168 switch (presenter()->state()) {
168 169 case ChartPresenter::ZoomInState:
169 170 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
170 171 m_animation->setAnimationPoint(presenter()->statePoint());
171 172 break;
172 173 case ChartPresenter::ZoomOutState:
173 174 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
174 175 m_animation->setAnimationPoint(presenter()->statePoint());
175 176 break;
176 177 case ChartPresenter::ScrollUpState:
177 178 case ChartPresenter::ScrollLeftState:
178 179 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
179 180 break;
180 181 case ChartPresenter::ScrollDownState:
181 182 case ChartPresenter::ScrollRightState:
182 183 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
183 184 break;
184 185 case ChartPresenter::ShowState:
185 186 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
186 187 break;
187 188 }
188 189 m_animation->setValues(m_layoutVector, layout);
189 190 presenter()->startAnimation(m_animation);
190 191 } else {
191 192 setLayout(layout);
192 193 updateGeometry();
193 194 }
194 195 }
195 196
196 197 void ChartAxis::setLabelPadding(int padding)
197 198 {
198 199 m_labelPadding = padding;
199 200 }
200 201
201 202 bool ChartAxis::isEmpty()
202 203 {
203 204 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max());
204 205 }
205 206
206 207 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
207 208 {
208 209 m_gridRect = grid;
209 210 m_axisRect = axis;
210 211
211 212 if (isEmpty())
212 213 return;
213 214
214 215 QVector<qreal> layout = calculateLayout();
215 216 updateLayout(layout);
216 217 }
217 218
218 219 qreal ChartAxis::min() const
219 220 {
220 221 return m_axis->d_ptr->min();
221 222 }
222 223
223 224 qreal ChartAxis::max() const
224 225 {
225 226 return m_axis->d_ptr->max();
226 227 }
227 228
228 229 QFont ChartAxis::font() const
229 230 {
230 231 return m_axis->labelsFont();
231 232 }
232 233
233 234 QFont ChartAxis::titleFont() const
234 235 {
235 236 return m_axis->titleFont();
236 237 }
237 238
238 239 QString ChartAxis::titleText() const
239 240 {
240 241 return m_axis->titleText();
241 242 }
242 243
243 244 void ChartAxis::axisSelected()
244 245 {
245 246 emit clicked();
246 247 }
247 248
248 249 Qt::Orientation ChartAxis::orientation() const
249 250 {
250 251 return m_axis->orientation();
251 252 }
252 253
253 254 Qt::Alignment ChartAxis::alignment() const
254 255 {
255 256 return m_axis->alignment();
256 257 }
257 258
258 259 void ChartAxis::setLabels(const QStringList &labels)
259 260 {
260 261 m_labelsList = labels;
261 262 }
262 263
263 264 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
264 265 {
265 266 Q_UNUSED(which);
266 267 Q_UNUSED(constraint);
267 268 return QSizeF();
268 269 }
269 270
270 271 //handlers
271 272
272 273 void ChartAxis::handleArrowVisibleChanged(bool visible)
273 274 {
274 275 m_arrow->setVisible(visible);
275 276 }
276 277
277 278 void ChartAxis::handleGridVisibleChanged(bool visible)
278 279 {
279 280 m_grid->setVisible(visible);
280 281 }
281 282
282 283 void ChartAxis::handleLabelsVisibleChanged(bool visible)
283 284 {
284 285 m_labels->setVisible(visible);
285 286 }
286 287
287 288 void ChartAxis::handleShadesVisibleChanged(bool visible)
288 289 {
289 290 m_shades->setVisible(visible);
290 291 }
291 292
293 void ChartAxis::handleTitleVisibleChanged(bool visible)
294 {
295 m_title->setVisible(visible);
296 presenter()->layout()->invalidate();
297 }
298
292 299 void ChartAxis::handleLabelsAngleChanged(int angle)
293 300 {
294 301 foreach (QGraphicsItem *item, m_labels->childItems())
295 302 item->setRotation(angle);
296 303
297 304 m_labelsAngle = angle;
298 305 }
299 306
300 307 void ChartAxis::handleLabelsPenChanged(const QPen &pen)
301 308 {
302 309 foreach (QGraphicsItem *item , m_labels->childItems())
303 310 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
304 311 }
305 312
306 313 void ChartAxis::handleLabelsBrushChanged(const QBrush &brush)
307 314 {
308 315 foreach (QGraphicsItem *item , m_labels->childItems())
309 316 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
310 317 }
311 318
312 319 void ChartAxis::handleLabelsFontChanged(const QFont &font)
313 320 {
314 321 foreach (QGraphicsItem *item , m_labels->childItems())
315 322 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
316 323 QGraphicsLayoutItem::updateGeometry();
317 324 presenter()->layout()->invalidate();
318 325 }
319 326
320 327 void ChartAxis::handleShadesBrushChanged(const QBrush &brush)
321 328 {
322 329 foreach (QGraphicsItem *item , m_shades->childItems())
323 330 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
324 331 }
325 332
326 333 void ChartAxis::handleShadesPenChanged(const QPen &pen)
327 334 {
328 335 foreach (QGraphicsItem *item , m_shades->childItems())
329 336 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
330 337 }
331 338
332 339 void ChartAxis::handleArrowPenChanged(const QPen &pen)
333 340 {
334 341 foreach (QGraphicsItem *item , m_arrow->childItems())
335 342 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
336 343 }
337 344
338 345 void ChartAxis::handleGridPenChanged(const QPen &pen)
339 346 {
340 347 foreach (QGraphicsItem *item , m_grid->childItems())
341 348 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
342 349 }
343 350
344 351 void ChartAxis::handleTitleTextChanged(const QString &title)
345 352 {
346 353 Q_UNUSED(title)
347 354 QGraphicsLayoutItem::updateGeometry();
348 355 presenter()->layout()->invalidate();
349 356 }
350 357
351 358
352 359 void ChartAxis::handleTitlePenChanged(const QPen &pen)
353 360 {
354 361 m_title->setPen(pen);
355 362 }
356 363
357 364 void ChartAxis::handleTitleBrushChanged(const QBrush &brush)
358 365 {
359 366 m_title->setBrush(brush);
360 367 }
361 368
362 369 void ChartAxis::handleTitleFontChanged(const QFont &font)
363 370 {
364 371 if(m_title->font() != font){
365 372 m_title->setFont(font);
366 373 QGraphicsLayoutItem::updateGeometry();
367 374 presenter()->layout()->invalidate();
368 375 }
369 376 }
370 377
371 378 void ChartAxis::handleVisibleChanged(bool visible)
372 379 {
373 380 setVisible(visible);
381 if(!visible) {
382 m_grid->setVisible(visible);
383 m_arrow->setVisible(visible);
384 m_shades->setVisible(visible);
385 m_labels->setVisible(visible);
386 m_title->setVisible(visible);
387 }else {
388 m_grid->setVisible(m_axis->isGridLineVisible());
389 m_arrow->setVisible(m_axis->isLineVisible());
390 m_shades->setVisible(m_axis->shadesVisible());
391 m_labels->setVisible(m_axis->labelsVisible());
392 m_title->setVisible(m_axis->isTitleVisible());
393 }
394
395 presenter()->layout()->invalidate();
374 396 }
375 397
376 398 void ChartAxis::handleRangeChanged(qreal min, qreal max)
377 399 {
378 400 Q_UNUSED(min);
379 401 Q_UNUSED(max);
380 402
381 403 if (!isEmpty()) {
382 404
383 405 QVector<qreal> layout = calculateLayout();
384 406 updateLayout(layout);
385 407 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
386 408 QSizeF after = sizeHint(Qt::PreferredSize);
387 409
388 410 if (before != after) {
389 411 QGraphicsLayoutItem::updateGeometry();
390 412 //we don't want to call invalidate on layout, since it will change minimum size of component,
391 413 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
392 414 //instead recalculate layout and use plotArea for extra space.
393 415 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
394 416 }
395 417 }
396 418
397 419 }
398 420
399 421 //helpers
400 422
401 423 QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format)
402 424 {
403 425 //TODO: Q_ASSERT(m_max > m_min);
404 426 //TODO: Q_ASSERT(ticks > 1);
405 427
406 428 QStringList labels;
407 429
408 430 if(max <= min || ticks < 1){
409 431 return labels;
410 432 }
411 433
412 434 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
413 435 n++;
414 436
415 437 if (format.isNull()) {
416 438 for (int i = 0; i < ticks; i++) {
417 439 qreal value = min + (i * (max - min) / (ticks - 1));
418 440 labels << QString::number(value, 'f', n);
419 441 }
420 442 } else {
421 443 QByteArray array = format.toLatin1();
422 444 for (int i = 0; i < ticks; i++) {
423 445 qreal value = min + (i * (max - min) / (ticks - 1));
424 446 if (format.contains("d")
425 447 || format.contains("i")
426 448 || format.contains("c"))
427 449 labels << QString().sprintf(array, (qint64)value);
428 450 else if (format.contains("u")
429 451 || format.contains("o")
430 452 || format.contains("x", Qt::CaseInsensitive))
431 453 labels << QString().sprintf(array, (quint64)value);
432 454 else if (format.contains("f", Qt::CaseInsensitive)
433 455 || format.contains("e", Qt::CaseInsensitive)
434 456 || format.contains("g", Qt::CaseInsensitive))
435 457 labels << QString().sprintf(array, value);
436 458 }
437 459 }
438 460
439 461 return labels;
440 462 }
441 463
442 464 QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format)
443 465 {
444 466 // Q_ASSERT(m_max > m_min);
445 467 // Q_ASSERT(ticks > 1);
446 468
447 469 QStringList labels;
448 470
449 471 int n = 0;
450 472 if (ticks > 1)
451 473 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
452 474 n++;
453 475
454 476 int firstTick;
455 477 if (base > 1)
456 478 firstTick = ceil(log10(min) / log10(base));
457 479 else
458 480 firstTick = ceil(log10(max) / log10(base));
459 481
460 482 if (format.isNull()) {
461 483 for (int i = firstTick; i < ticks + firstTick; i++) {
462 484 qreal value = qPow(base, i);
463 485 labels << QString::number(value, 'f', n);
464 486 }
465 487 } else {
466 488 QByteArray array = format.toLatin1();
467 489 for (int i = firstTick; i < ticks + firstTick; i++) {
468 490 qreal value = qPow(base, i);
469 491 if (format.contains("d")
470 492 || format.contains("i")
471 493 || format.contains("c"))
472 494 labels << QString().sprintf(array, (qint64)value);
473 495 else if (format.contains("u")
474 496 || format.contains("o")
475 497 || format.contains("x", Qt::CaseInsensitive))
476 498 labels << QString().sprintf(array, (quint64)value);
477 499 else if (format.contains("f", Qt::CaseInsensitive)
478 500 || format.contains("e", Qt::CaseInsensitive)
479 501 || format.contains("g", Qt::CaseInsensitive))
480 502 labels << QString().sprintf(array, value);
481 503 }
482 504 }
483 505
484 506 return labels;
485 507 }
486 508
487 509 QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format)
488 510 {
489 511 //TODO: Q_ASSERT(m_max > m_min);
490 512 //TODO: Q_ASSERT(ticks > 1);
491 513 QStringList labels;
492 514
493 515 if(max <= min || ticks < 1) {
494 516 return labels;
495 517 }
496 518
497 519 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
498 520 n++;
499 521 for (int i = 0; i < ticks; i++) {
500 522 qreal value = min + (i * (max - min) / (ticks - 1));
501 523 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
502 524 }
503 525 return labels;
504 526 }
505 527
506 528 #include "moc_chartaxis_p.cpp"
507 529
508 530 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,195 +1,196
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTAXIS_H
31 31 #define CHARTAXIS_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include "chartelement_p.h"
35 35 #include "axisanimation_p.h"
36 36 #include <QGraphicsItem>
37 37 #include <QGraphicsLayoutItem>
38 38 #include <QFont>
39 39
40 40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 41
42 42 class QAbstractAxis;
43 43 class ChartPresenter;
44 44
45 45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
46 46 {
47 47 Q_OBJECT
48 48 Q_INTERFACES(QGraphicsLayoutItem)
49 49 public:
50 50
51 51 ChartAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false);
52 52 ~ChartAxis();
53 53
54 54 QAbstractAxis* axis() const { return m_axis; }
55 55
56 56 void setLabelPadding(int padding);
57 57 int labelPadding() const { return m_labelPadding;};
58 58
59 59 QFont titleFont() const;
60 60 QString titleText() const;
61 61
62 62 void setLayout(QVector<qreal> &layout);
63 63 QVector<qreal> layout() const { return m_layoutVector; }
64 64
65 65 void setAnimation(AxisAnimation *animation);
66 66 ChartAnimation *animation() const { return m_animation; };
67 67
68 68 Qt::Orientation orientation() const;
69 69 Qt::Alignment alignment() const;
70 70
71 71 void setGeometry(const QRectF &axis, const QRectF &grid);
72 72 QRectF axisGeometry() const { return m_axisRect; }
73 73 QRectF gridGeometry() const { return m_gridRect; }
74 74
75 75 void setLabels(const QStringList &labels);
76 76 QStringList labels() const { return m_labelsList; }
77 77
78 78 //this flag indicates that axis is used to show intervals it means labels are in between ticks
79 79 bool intervalAxis() const { return m_intervalAxis; }
80 80
81 81 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
82 82
83 83
84 84 QRectF boundingRect() const{
85 85 return QRectF();
86 86 }
87 87
88 88 void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
89 89 {
90 90
91 91 }
92 92
93 93 //helpers
94 94 static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format);
95 95 static QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format);
96 96 static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format);
97 97
98 98 protected:
99 99 void setGeometry(const QRectF &size) { Q_UNUSED(size);};
100 100 virtual void updateGeometry() = 0;
101 101 virtual QVector<qreal> calculateLayout() const = 0;
102 102
103 103 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
104 104 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
105 105 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
106 106 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
107 107 QGraphicsSimpleTextItem* titleItem() const { return m_title.data();}
108 108
109 109 QFont font() const;
110 110 qreal min() const;
111 111 qreal max() const;
112 112
113 113 //handlers
114 114 public Q_SLOTS:
115 115 void handleVisibleChanged(bool visible);
116 116 void handleArrowVisibleChanged(bool visible);
117 117 void handleGridVisibleChanged(bool visible);
118 118 void handleLabelsVisibleChanged(bool visible);
119 119 void handleShadesVisibleChanged(bool visible);
120 120 void handleLabelsAngleChanged(int angle);
121 121 void handleShadesBrushChanged(const QBrush &brush);
122 122 void handleShadesPenChanged(const QPen &pen);
123 123 void handleArrowPenChanged(const QPen &pen);
124 124 void handleGridPenChanged(const QPen &pen);
125 125 void handleLabelsPenChanged(const QPen &pen);
126 126 void handleLabelsBrushChanged(const QBrush &brush);
127 127 void handleLabelsFontChanged(const QFont &font);
128 128 void handleTitlePenChanged(const QPen &pen);
129 129 void handleTitleBrushChanged(const QBrush &brush);
130 130 void handleTitleFontChanged(const QFont &font);
131 131 void handleTitleTextChanged(const QString &title);
132 void handleTitleVisibleChanged(bool visible);
132 133 void handleRangeChanged(qreal min , qreal max);
133 134
134 135 Q_SIGNALS:
135 136 void clicked();
136 137
137 138 private:
138 139 inline bool isEmpty();
139 140 void createItems(int count);
140 141 void deleteItems(int count);
141 142 void updateLayout(QVector<qreal> &layout);
142 143 void axisSelected();
143 144 void connectSlots();
144 145
145 146 private:
146 147 QAbstractAxis *m_axis;
147 148 int m_labelsAngle;
148 149 QRectF m_axisRect;
149 150 QRectF m_gridRect;
150 151 QScopedPointer<QGraphicsItemGroup> m_grid;
151 152 QScopedPointer<QGraphicsItemGroup> m_arrow;
152 153 QScopedPointer<QGraphicsItemGroup> m_shades;
153 154 QScopedPointer<QGraphicsItemGroup> m_labels;
154 155 QScopedPointer<QGraphicsSimpleTextItem> m_title;
155 156 QVector<qreal> m_layoutVector;
156 157 AxisAnimation *m_animation;
157 158 int m_labelPadding;
158 159 QStringList m_labelsList;
159 160 bool m_intervalAxis;
160 161
161 162 friend class AxisAnimation;
162 163 friend class ArrowItem;
163 164
164 165 };
165 166
166 167 class ArrowItem: public QGraphicsLineItem
167 168 {
168 169
169 170 public:
170 171 explicit ArrowItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
171 172
172 173 protected:
173 174 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
174 175 Q_UNUSED(event)
175 176 m_axis->axisSelected();
176 177 }
177 178
178 179 QRectF boundingRect() const {
179 180 return shape().boundingRect();
180 181 }
181 182
182 183 QPainterPath shape() const {
183 184 QPainterPath path = QGraphicsLineItem::shape();
184 185 QRectF rect = path.boundingRect();
185 186 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
186 187 return path;
187 188 }
188 189
189 190 private:
190 191 ChartAxis *m_axis;
191 192 };
192 193
193 194 QTCOMMERCIALCHART_END_NAMESPACE
194 195
195 196 #endif /* CHARTAXI_H */
@@ -1,210 +1,210
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 "horizontalaxis_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include <QFontMetrics>
24 24 #include <qmath.h>
25 25 #include <QDebug>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
30 30 : ChartAxis(axis, item, intervalAxis)
31 31 {
32 32 }
33 33
34 34 HorizontalAxis::~HorizontalAxis()
35 35 {
36 36 }
37 37
38 38 void HorizontalAxis::updateGeometry()
39 39 {
40 40 const QVector<qreal>& layout = ChartAxis::layout();
41 41
42 42 if (layout.isEmpty())
43 43 return;
44 44
45 45 QStringList labelList = labels();
46 46
47 47 QList<QGraphicsItem *> lines = lineItems();
48 48 QList<QGraphicsItem *> labels = labelItems();
49 49 QList<QGraphicsItem *> shades = shadeItems();
50 50 QList<QGraphicsItem *> axis = arrowItems();
51 51 QGraphicsSimpleTextItem* title = titleItem();
52 52
53 53 Q_ASSERT(labels.size() == labelList.size());
54 54 Q_ASSERT(layout.size() == labelList.size());
55 55
56 56 const QRectF &axisRect = axisGeometry();
57 57 const QRectF &gridRect = gridGeometry();
58 58
59 59 //arrow
60 60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(axis.at(0));
61 61
62 62 if (alignment() == Qt::AlignTop)
63 63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
64 64 else if (alignment() == Qt::AlignBottom)
65 65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
66 66
67 67 qreal width = 0;
68 68 QFontMetrics fn(font());
69 69
70 70 //title
71 71
72 72 if (!titleText().isNull()) {
73 73 QFontMetrics fn(title->font());
74 74
75 75 int size(0);
76 76
77 77 size = gridRect.width();
78 78 QString titleText = this->titleText();
79 79
80 80 if (fn.boundingRect(titleText).width() > size) {
81 81 QString string = titleText + "...";
82 82 while (fn.boundingRect(string).width() > size && string.length() > 3)
83 83 string.remove(string.length() - 4, 1);
84 84 title->setText(string);
85 85 } else {
86 86 title->setText(titleText);
87 87 }
88 88
89 89 QPointF center = gridRect.center() - title->boundingRect().center();
90 90 if (alignment() == Qt::AlignTop) {
91 91 title->setPos(center.x(), axisRect.top());
92 92 } else if (alignment() == Qt::AlignBottom) {
93 93 title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height());
94 94 }
95 95 }
96 96
97 97 for (int i = 0; i < layout.size(); ++i) {
98 98
99 99 //items
100 100 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
101 101 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
102 102 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
103 103
104 104 //grid line
105 105 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
106 106
107 107 //label text wrapping
108 108 if(intervalAxis()&& i+1!=layout.size()) {
109 109 //wrapping in case of interval axis
110 110 const qreal delta = layout[i+1] - layout[i];
111 111 QString text = labelList.at(i);
112 112 if (fn.boundingRect(text).width() + 1 > delta )
113 113 {
114 114 QString label = text + "...";
115 115 while (fn.boundingRect(label).width() >= delta && label.length() > 3)
116 116 label.remove(label.length() - 4, 1);
117 117 labelItem->setText(label);
118 118 }
119 119 else {
120 120 labelItem->setText(text);
121 121 }
122 122 }else{
123 123 labelItem->setText(labelList.at(i));
124 124 }
125 125
126 126 //label transformation origin point
127 127 const QRectF& rect = labelItem->boundingRect();
128 128 QPointF center = rect.center();
129 129 labelItem->setTransformOriginPoint(center.x(), center.y());
130 130
131 131 //ticks and label position
132 132 if (alignment() == Qt::AlignTop) {
133 133 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
134 134 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
135 135 } else if (alignment() == Qt::AlignBottom) {
136 136 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
137 137 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
138 138 }
139 139
140 140 //label in beetwen
141 141 if(intervalAxis()&& i+1!=layout.size()) {
142 142 const qreal delta = (layout[i+1] - layout[i])/2;
143 143 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
144 144 }
145 145
146 146 //label overlap detection
147 147 if(labelItem->pos().x() < width ||
148 148 labelItem->pos().x() < axisRect.left() ||
149 149 labelItem->pos().x() + rect.width() > axisRect.right()) {
150 150 labelItem->setVisible(false);
151 151 } else {
152 152 labelItem->setVisible(true);
153 153 width=rect.width()+labelItem->pos().x();
154 154 }
155 155
156 156 //shades
157 157 if ((i + 1) % 2 && i > 1) {
158 158 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
159 159 rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height());
160 160 }
161 161
162 162 // check if the grid line and the axis tick should be shown
163 163 qreal x = gridItem->line().p1().x();
164 164 if (x < gridRect.left() || x > gridRect.right()) {
165 165 gridItem->setVisible(false);
166 166 tickItem->setVisible(false);
167 167 }else{
168 168 gridItem->setVisible(true);
169 169 tickItem->setVisible(true);
170 170 }
171 171
172 172 }
173 173
174 174 //begin/end grid line in case labels between
175 175 if (intervalAxis()) {
176 176 QGraphicsLineItem *gridLine;
177 177 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
178 178 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
179 179 gridLine->setVisible(true);
180 180 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
181 181 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
182 182 gridLine->setVisible(true);
183 183 }
184 184 }
185 185
186 186 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
187 187 {
188 188 Q_UNUSED(constraint);
189 189 QFontMetrics fn(titleFont());
190 190 QSizeF sh;
191 191
192 if (titleText().isNull())
192 if (titleText().isNull() || !titleItem()->isVisible())
193 193 return sh;
194 194
195 195 switch (which) {
196 196 case Qt::MinimumSize:
197 197 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
198 198 break;
199 199 case Qt::MaximumSize:
200 200 case Qt::PreferredSize:
201 201 sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height());
202 202 break;
203 203 default:
204 204 break;
205 205 }
206 206
207 207 return sh;
208 208 }
209 209
210 210 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,869 +1,869
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 "qabstractaxis.h"
22 22 #include "qabstractaxis_p.h"
23 23 #include "chartdataset_p.h"
24 24 #include "charttheme_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \class QAbstractAxis
30 30 \brief The QAbstractAxis class is used for manipulating chart's axis.
31 31 \mainclass
32 32
33 33 There is only one x Axis visible at the time, however there can be multiple y axes.
34 34 Each chart series can be bound to exactly one Y axis and the shared common X axis.
35 35 Axis can be setup to show axis line with tick marks, grid lines and shades.
36 36 */
37 37
38 38 /*!
39 39 \qmlclass AbstractAxis QAbstractAxis
40 40 \brief The Axis element is used for manipulating chart's axes
41 41
42 42 There is only one x Axis visible at the time, however there can be multiple y axes on a ChartView.
43 43 Each chart series can be bound to exactly one Y axis and the shared common X axis.
44 44 Axis can be setup to show axis line with tick marks, grid lines and shades.
45 45
46 46 To access Axes you can use ChartView API. For example:
47 47 \code
48 48 ChartView {
49 49 axisX.min: 0
50 50 axisX.max: 3
51 51 axisX.ticksCount: 4
52 52 axisY.min: 0
53 53 axisY.max: 4
54 54 // Add a few series...
55 55 }
56 56 \endcode
57 57 */
58 58
59 59 /*!
60 60 \enum QAbstractAxis::AxisType
61 61
62 62 The type of the series object.
63 63
64 64 \value AxisTypeNoAxis
65 65 \value AxisTypeValue
66 66 \value AxisTypeBarCategory
67 67 \value AxisTypeCategory
68 68 \value AxisTypeDateTime
69 69 */
70 70
71 71 /*!
72 72 *\fn void QAbstractAxis::type() const
73 73 Returns the type of the axis
74 74 */
75 75
76 76 /*!
77 77 \property QAbstractAxis::lineVisible
78 78 The visibility of the axis line
79 79 */
80 80 /*!
81 81 \qmlproperty bool AbstractAxis::lineVisible
82 82 The visibility of the axis line
83 83 */
84 84
85 85 /*!
86 86 \property QAbstractAxis::labelsVisible
87 87 Defines if axis labels are visible.
88 88 */
89 89 /*!
90 90 \qmlproperty bool AbstractAxis::labelsVisible
91 91 Defines if axis labels are visible.
92 92 */
93 93
94 94 /*!
95 95 \property QAbstractAxis::visible
96 96 The visibility of the axis.
97 97 */
98 98 /*!
99 99 \qmlproperty bool AbstractAxis::visible
100 100 The visibility of the axis.
101 101 */
102 102
103 103 /*!
104 104 \property QAbstractAxis::gridVisible
105 105 The visibility of the grid lines.
106 106 */
107 107 /*!
108 108 \qmlproperty bool AbstractAxis::gridVisible
109 109 The visibility of the grid lines.
110 110 */
111 111
112 112 /*!
113 113 \property QAbstractAxis::color
114 114 The color of the axis and ticks.
115 115 */
116 116 /*!
117 117 \qmlproperty color AbstractAxis::color
118 118 The color of the axis and ticks.
119 119 */
120 120
121 121 /*!
122 122 \property QAbstractAxis::labelsFont
123 123 The font of the axis labels.
124 124 */
125 125
126 126 /*!
127 127 \qmlproperty Font AbstractAxis::labelsFont
128 128 The font of the axis labels.
129 129
130 130 See the \l {Font} {QML Font Element} for detailed documentation.
131 131 */
132 132
133 133 /*!
134 134 \property QAbstractAxis::labelsColor
135 135 The color of the axis labels.
136 136 */
137 137 /*!
138 138 \qmlproperty color AbstractAxis::labelsColor
139 139 The color of the axis labels.
140 140 */
141 141
142 142 /*!
143 143 \property QAbstractAxis::labelsAngle
144 144 The angle of the axis labels in degrees.
145 145 */
146 146 /*!
147 147 \qmlproperty int AbstractAxis::labelsAngle
148 148 The angle of the axis labels in degrees.
149 149 */
150 150
151 151 /*!
152 152 \property QAbstractAxis::shadesVisible
153 153 The visibility of the axis shades.
154 154 */
155 155 /*!
156 156 \qmlproperty bool AbstractAxis::shadesVisible
157 157 The visibility of the axis shades.
158 158 */
159 159
160 160 /*!
161 161 \property QAbstractAxis::shadesColor
162 162 The fill (brush) color of the axis shades.
163 163 */
164 164 /*!
165 165 \qmlproperty color AbstractAxis::shadesColor
166 166 The fill (brush) color of the axis shades.
167 167 */
168 168
169 169 /*!
170 170 \property QAbstractAxis::shadesBorderColor
171 171 The border (pen) color of the axis shades.
172 172 */
173 173 /*!
174 174 \qmlproperty color AbstractAxis::shadesBorderColor
175 175 The border (pen) color of the axis shades.
176 176 */
177 177
178 178 /*!
179 179 \property QAbstractAxis::titleVisible
180 180 The visibility of the axis title. By default the value is true.
181 181 */
182 182 /*!
183 183 \qmlproperty bool AbstractAxis::titleVisible
184 184 The visibility of the axis title. By default the value is true.
185 185 */
186 186
187 187 /*!
188 188 \property QAbstractAxis::titleFont
189 189 The font of the title of the axis.
190 190 */
191 191 /*!
192 192 \qmlproperty Font AbstractAxis::title
193 193 The font of the title of the axis.
194 194 */
195 195
196 196 /*!
197 197 \property QAbstractAxis::title
198 198 The title of the axis. Empty by default.
199 199 */
200 200 /*!
201 201 \qmlproperty string AbstractAxis::title
202 202 The title of the axis. Empty string by default.
203 203 */
204 204
205 205 /*!
206 206 \property QAbstractAxis::alignment
207 207 The alignment of the axis. Either Qt::AlignLeft or Qt::AlignBottom.
208 208 */
209 209 /*!
210 210 \qmlproperty alignment AbstractAxis::alignment
211 211 The alignment of the axis. Either Qt.AlignLeft or Qt.AlignBottom.
212 212 */
213 213
214 214 /*!
215 215 \fn void QAbstractAxis::visibleChanged(bool visible)
216 216 Visibility of the axis has changed to \a visible.
217 217 */
218 218 /*!
219 219 \qmlsignal AbstractAxis::onVisibleChanged(bool visible)
220 220 Visibility of the axis has changed to \a visible.
221 221 */
222 222
223 223 /*!
224 224 \fn void QAbstractAxis::lineVisibleChanged(bool visible)
225 225 Visibility of the axis line has changed to \a visible.
226 226 */
227 227 /*!
228 228 \qmlsignal AbstractAxis::onLineVisibleChanged(bool visible)
229 229 Visibility of the axis line has changed to \a visible.
230 230 */
231 231
232 232 /*!
233 233 \fn void QAbstractAxis::labelsVisibleChanged(bool visible)
234 234 Visibility of the labels of the axis has changed to \a visible.
235 235 */
236 236 /*!
237 237 \qmlsignal AbstractAxis::onLabelsVisibleChanged(bool visible)
238 238 Visibility of the labels of the axis has changed to \a visible.
239 239 */
240 240
241 241 /*!
242 242 \fn void QAbstractAxis::gridVisibleChanged(bool visible)
243 243 Visibility of the grid lines of the axis has changed to \a visible.
244 244 */
245 245 /*!
246 246 \qmlsignal AbstractAxis::onGridVisibleChanged(bool visible)
247 247 Visibility of the grid lines of the axis has changed to \a visible.
248 248 */
249 249
250 250 /*!
251 251 \fn void QAbstractAxis::colorChanged(QColor color)
252 252 Emitted if the \a color of the axis is changed.
253 253 */
254 254 /*!
255 255 \qmlsignal AbstractAxis::onColorChanged(QColor color)
256 256 Emitted if the \a color of the axis is changed.
257 257 */
258 258
259 259 /*!
260 260 \fn void QAbstractAxis::labelsColorChanged(QColor color)
261 261 Emitted if the \a color of the axis labels is changed.
262 262 */
263 263 /*!
264 264 \qmlsignal AbstractAxis::onLabelsColorChanged(QColor color)
265 265 Emitted if the \a color of the axis labels is changed.
266 266 */
267 267
268 268 /*!
269 269 \fn void QAbstractAxis::shadesVisibleChanged(bool)
270 270 Emitted if the visibility of the axis shades is changed to \a visible.
271 271 */
272 272 /*!
273 273 \qmlsignal AbstractAxis::onShadesVisibleChanged(bool visible)
274 274 Emitted if the visibility of the axis shades is changed to \a visible.
275 275 */
276 276
277 277 /*!
278 278 \fn void QAbstractAxis::shadesColorChanged(QColor color)
279 279 Emitted if the \a color of the axis shades is changed.
280 280 */
281 281 /*!
282 282 \qmlsignal AbstractAxis::onShadesColorChanged(QColor color)
283 283 Emitted if the \a color of the axis shades is changed.
284 284 */
285 285
286 286 /*!
287 287 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
288 288 Emitted if the border \a color of the axis shades is changed.
289 289 */
290 290 /*!
291 291 \qmlsignal AbstractAxis::onBorderColorChanged(QColor color)
292 292 Emitted if the border \a color of the axis shades is changed.
293 293 */
294 294
295 295 /*!
296 296 \internal
297 297 Constructs new axis object which is a child of \a parent. Ownership is taken by
298 298 QChart when axis added.
299 299 */
300 300
301 301 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent)
302 302 : QObject(parent),
303 303 d_ptr(&d)
304 304 {
305 305 }
306 306
307 307 /*!
308 308 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
309 309 */
310 310
311 311 QAbstractAxis::~QAbstractAxis()
312 312 {
313 313 if (d_ptr->m_chart)
314 314 qFatal("Still binded axis detected !");
315 315 }
316 316
317 317 /*!
318 318 Sets \a pen used to draw axis line and ticks.
319 319 */
320 320 void QAbstractAxis::setLinePen(const QPen &pen)
321 321 {
322 322 if (d_ptr->m_axisPen != pen) {
323 323 d_ptr->m_axisPen = pen;
324 324 emit linePenChanged(pen);
325 325 }
326 326 }
327 327
328 328 /*!
329 329 Returns pen used to draw axis and ticks.
330 330 */
331 331 QPen QAbstractAxis::linePen() const
332 332 {
333 333 return d_ptr->m_axisPen;
334 334 }
335 335
336 336 //TODO: remove me
337 337 void QAbstractAxis::setLinePenColor(QColor color)
338 338 {
339 339 QPen p = d_ptr->m_axisPen;
340 340 if (p.color() != color) {
341 341 p.setColor(color);
342 342 setLinePen(p);
343 343 emit colorChanged(color);
344 344 }
345 345 }
346 346
347 347 QColor QAbstractAxis::linePenColor() const
348 348 {
349 349 return d_ptr->m_axisPen.color();
350 350 }
351 351
352 352 /*!
353 353 Sets if axis and ticks are \a visible.
354 354 */
355 355 void QAbstractAxis::setLineVisible(bool visible)
356 356 {
357 357 if (d_ptr->m_arrowVisible != visible) {
358 358 d_ptr->m_arrowVisible = visible;
359 359 emit lineVisibleChanged(visible);
360 360 }
361 361 }
362 362
363 363 bool QAbstractAxis::isLineVisible() const
364 364 {
365 365 return d_ptr->m_arrowVisible;
366 366 }
367 367
368 368 void QAbstractAxis::setGridLineVisible(bool visible)
369 369 {
370 370 if (d_ptr->m_gridLineVisible != visible) {
371 371 d_ptr->m_gridLineVisible = visible;
372 372 emit gridVisibleChanged(visible);
373 373 }
374 374 }
375 375
376 376 bool QAbstractAxis::isGridLineVisible() const
377 377 {
378 378 return d_ptr->m_gridLineVisible;
379 379 }
380 380
381 381 /*!
382 382 Sets \a pen used to draw grid line.
383 383 */
384 384 void QAbstractAxis::setGridLinePen(const QPen &pen)
385 385 {
386 386 if (d_ptr->m_gridLinePen != pen) {
387 387 d_ptr->m_gridLinePen = pen;
388 388 emit gridLinePenChanged(pen);
389 389 }
390 390 }
391 391
392 392 /*!
393 393 Returns pen used to draw grid.
394 394 */
395 395 QPen QAbstractAxis::gridLinePen() const
396 396 {
397 397 return d_ptr->m_gridLinePen;
398 398 }
399 399
400 400 void QAbstractAxis::setLabelsVisible(bool visible)
401 401 {
402 402 if (d_ptr->m_labelsVisible != visible) {
403 403 d_ptr->m_labelsVisible = visible;
404 404 emit labelsVisibleChanged(visible);
405 405 }
406 406 }
407 407
408 408 bool QAbstractAxis::labelsVisible() const
409 409 {
410 410 return d_ptr->m_labelsVisible;
411 411 }
412 412
413 413 /*!
414 414 Sets \a pen used to draw labels.
415 415 */
416 416 void QAbstractAxis::setLabelsPen(const QPen &pen)
417 417 {
418 418 if (d_ptr->m_labelsPen != pen) {
419 419 d_ptr->m_labelsPen = pen;
420 420 emit labelsPenChanged(pen);
421 421 }
422 422 }
423 423
424 424 /*!
425 425 Returns the pen used to labels.
426 426 */
427 427 QPen QAbstractAxis::labelsPen() const
428 428 {
429 429 return d_ptr->m_labelsPen;
430 430 }
431 431
432 432 /*!
433 433 Sets \a brush used to draw labels.
434 434 */
435 435 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
436 436 {
437 437 if (d_ptr->m_labelsBrush != brush) {
438 438 d_ptr->m_labelsBrush = brush;
439 439 emit labelsBrushChanged(brush);
440 440 }
441 441 }
442 442
443 443 /*!
444 444 Returns brush used to draw labels.
445 445 */
446 446 QBrush QAbstractAxis::labelsBrush() const
447 447 {
448 448 return d_ptr->m_labelsBrush;
449 449 }
450 450
451 451 /*!
452 452 Sets \a font used to draw labels.
453 453 */
454 454 void QAbstractAxis::setLabelsFont(const QFont &font)
455 455 {
456 456 if (d_ptr->m_labelsFont != font) {
457 457 d_ptr->m_labelsFont = font;
458 458 emit labelsFontChanged(font);
459 459 }
460 460 }
461 461
462 462 /*!
463 463 Returns font used to draw labels.
464 464 */
465 465 QFont QAbstractAxis::labelsFont() const
466 466 {
467 467 return d_ptr->m_labelsFont;
468 468 }
469 469
470 470 void QAbstractAxis::setLabelsAngle(int angle)
471 471 {
472 472 if (d_ptr->m_labelsAngle != angle) {
473 473 d_ptr->m_labelsAngle = angle;
474 474 emit labelsAngleChanged(angle);
475 475 }
476 476 }
477 477
478 478 int QAbstractAxis::labelsAngle() const
479 479 {
480 480 return d_ptr->m_labelsAngle;
481 481 }
482 482 //TODO: remove me
483 483 void QAbstractAxis::setLabelsColor(QColor color)
484 484 {
485 485 QBrush b = d_ptr->m_labelsBrush;
486 486 if (b.color() != color) {
487 487 b.setColor(color);
488 488 setLabelsBrush(b);
489 489 emit labelsColorChanged(color);
490 490 }
491 491 }
492 492
493 493 QColor QAbstractAxis::labelsColor() const
494 494 {
495 495 return d_ptr->m_labelsBrush.color();
496 496 }
497 497
498 498 void QAbstractAxis::setTitleVisible(bool visible)
499 499 {
500 500 if (d_ptr->m_titleVisible != visible) {
501 501 d_ptr->m_titleVisible = visible;
502 emit labelsVisibleChanged(visible);
502 emit titleVisibleChanged(visible);
503 503 }
504 504 }
505 505
506 bool QAbstractAxis::titleVisible() const
506 bool QAbstractAxis::isTitleVisible() const
507 507 {
508 508 return d_ptr->m_titleVisible;
509 509 }
510 510
511 511 /*!
512 512 Sets \a pen used to draw title.
513 513 */
514 514 void QAbstractAxis::setTitlePen(const QPen &pen)
515 515 {
516 516 if (d_ptr->m_titlePen != pen) {
517 517 d_ptr->m_titlePen = pen;
518 518 emit titlePenChanged(pen);
519 519 }
520 520 }
521 521
522 522 /*!
523 523 Returns the pen used to title.
524 524 */
525 525 QPen QAbstractAxis::titlePen() const
526 526 {
527 527 return d_ptr->m_titlePen;
528 528 }
529 529
530 530 /*!
531 531 Sets \a brush used to draw title.
532 532 */
533 533 void QAbstractAxis::setTitleBrush(const QBrush &brush)
534 534 {
535 535 if (d_ptr->m_titleBrush != brush) {
536 536 d_ptr->m_titleBrush = brush;
537 537 emit titleBrushChanged(brush);
538 538 }
539 539 }
540 540
541 541 /*!
542 542 Returns brush used to draw title.
543 543 */
544 544 QBrush QAbstractAxis::titleBrush() const
545 545 {
546 546 return d_ptr->m_titleBrush;
547 547 }
548 548
549 549 /*!
550 550 Sets \a font used to draw title.
551 551 */
552 552 void QAbstractAxis::setTitleFont(const QFont &font)
553 553 {
554 554 if (d_ptr->m_titleFont != font) {
555 555 d_ptr->m_titleFont = font;
556 556 emit titleFontChanged(font);
557 557 }
558 558 }
559 559
560 560 /*!
561 561 Returns font used to draw title.
562 562 */
563 563 QFont QAbstractAxis::titleFont() const
564 564 {
565 565 return d_ptr->m_titleFont;
566 566 }
567 567
568 568 void QAbstractAxis::setTitleText(const QString &title)
569 569 {
570 570 if (d_ptr->m_title != title) {
571 571 d_ptr->m_title = title;
572 572 emit titleTextChanged(title);
573 573 }
574 574 }
575 575
576 576 QString QAbstractAxis::titleText() const
577 577 {
578 578 return d_ptr->m_title;
579 579 }
580 580
581 581
582 582 void QAbstractAxis::setShadesVisible(bool visible)
583 583 {
584 584 if (d_ptr->m_shadesVisible != visible) {
585 585 d_ptr->m_shadesVisible = visible;
586 586 emit shadesVisibleChanged(visible);
587 587 }
588 588 }
589 589
590 590 bool QAbstractAxis::shadesVisible() const
591 591 {
592 592 return d_ptr->m_shadesVisible;
593 593 }
594 594
595 595 /*!
596 596 Sets \a pen used to draw shades.
597 597 */
598 598 void QAbstractAxis::setShadesPen(const QPen &pen)
599 599 {
600 600 if (d_ptr->m_shadesPen != pen) {
601 601 d_ptr->m_shadesPen = pen;
602 602 emit shadesPenChanged(pen);
603 603 }
604 604 }
605 605
606 606 /*!
607 607 Returns pen used to draw shades.
608 608 */
609 609 QPen QAbstractAxis::shadesPen() const
610 610 {
611 611 return d_ptr->m_shadesPen;
612 612 }
613 613
614 614 /*!
615 615 Sets \a brush used to draw shades.
616 616 */
617 617 void QAbstractAxis::setShadesBrush(const QBrush &brush)
618 618 {
619 619 if (d_ptr->m_shadesBrush != brush) {
620 620 d_ptr->m_shadesBrush = brush;
621 621 emit shadesBrushChanged(brush);
622 622 }
623 623 }
624 624
625 625 /*!
626 626 Returns brush used to draw shades.
627 627 */
628 628 QBrush QAbstractAxis::shadesBrush() const
629 629 {
630 630 return d_ptr->m_shadesBrush;
631 631 }
632 632
633 633 void QAbstractAxis::setShadesColor(QColor color)
634 634 {
635 635 QBrush b = d_ptr->m_shadesBrush;
636 636 if (b.color() != color) {
637 637 b.setColor(color);
638 638 setShadesBrush(b);
639 639 emit shadesColorChanged(color);
640 640 }
641 641 }
642 642
643 643 QColor QAbstractAxis::shadesColor() const
644 644 {
645 645 return d_ptr->m_shadesBrush.color();
646 646 }
647 647
648 648 void QAbstractAxis::setShadesBorderColor(QColor color)
649 649 {
650 650 QPen p = d_ptr->m_shadesPen;
651 651 if (p.color() != color) {
652 652 p.setColor(color);
653 653 setShadesPen(p);
654 654 emit shadesColorChanged(color);
655 655 }
656 656 }
657 657
658 658 QColor QAbstractAxis::shadesBorderColor() const
659 659 {
660 660 return d_ptr->m_shadesPen.color();
661 661 }
662 662
663 663
664 664 bool QAbstractAxis::isVisible() const
665 665 {
666 666 return d_ptr->m_visible;
667 667 }
668 668
669 669 /*!
670 670 Sets axis, shades, labels and grid lines to be visible.
671 671 */
672 672 void QAbstractAxis::setVisible(bool visible)
673 673 {
674 674 if (d_ptr->m_visible != visible) {
675 675 d_ptr->m_visible = visible;
676 676 emit visibleChanged(visible);
677 677 }
678 678 }
679 679
680 680
681 681 /*!
682 682 Sets axis, shades, labels and grid lines to be visible.
683 683 */
684 684 void QAbstractAxis::show()
685 685 {
686 686 setVisible(true);
687 687 }
688 688
689 689 /*!
690 690 Sets axis, shades, labels and grid lines to not be visible.
691 691 */
692 692 void QAbstractAxis::hide()
693 693 {
694 694 setVisible(false);
695 695 }
696 696
697 697 /*!
698 698 Sets the minimum value shown on the axis.
699 699 Depending on the actual axis type the \a min parameter is converted to appropriate type.
700 700 If the conversion is impossible then the function call does nothing
701 701 */
702 702 void QAbstractAxis::setMin(const QVariant &min)
703 703 {
704 704 d_ptr->setMin(min);
705 705 }
706 706
707 707 /*!
708 708 Sets the maximum value shown on the axis.
709 709 Depending on the actual axis type the \a max parameter is converted to appropriate type.
710 710 If the conversion is impossible then the function call does nothing
711 711 */
712 712 void QAbstractAxis::setMax(const QVariant &max)
713 713 {
714 714 d_ptr->setMax(max);
715 715 }
716 716
717 717 /*!
718 718 Sets the range shown on the axis.
719 719 Depending on the actual axis type the \a min and \a max parameters are converted to appropriate types.
720 720 If the conversion is impossible then the function call does nothing.
721 721 */
722 722 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
723 723 {
724 724 d_ptr->setRange(min, max);
725 725 }
726 726
727 727
728 728 /*!
729 729 Returns the orientation in which the axis is being used (Vertical or Horizontal)
730 730 */
731 731 // NOTE: should have const but it breaks BC:
732 732 // http://techbase.kde.org/Policies/Binary_Compatibility_Examples#Change_the_CV-qualifiers_of_a_member_function
733 733 Qt::Orientation QAbstractAxis::orientation()
734 734 {
735 735 return d_ptr->orientation();
736 736 }
737 737
738 738 Qt::Alignment QAbstractAxis::alignment() const
739 739 {
740 740 return d_ptr->alignment();
741 741 }
742 742
743 743 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
744 744
745 745 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q)
746 746 : q_ptr(q),
747 747 m_chart(0),
748 748 m_orientation(Qt::Orientation(0)),
749 749 m_alignment(0),
750 750 m_visible(true),
751 751 m_arrowVisible(true),
752 752 m_gridLineVisible(true),
753 753 m_labelsVisible(true),
754 754 m_labelsAngle(0),
755 755 m_shadesVisible(false),
756 756 m_shadesBrush(Qt::SolidPattern),
757 757 m_shadesOpacity(1.0),
758 758 m_dirty(false)
759 759 {
760 760
761 761 }
762 762
763 763 QAbstractAxisPrivate::~QAbstractAxisPrivate()
764 764 {
765 765 }
766 766
767 767 void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment)
768 768 {
769 769 switch(alignment) {
770 770 case Qt::AlignTop:
771 771 case Qt::AlignBottom:
772 772 m_orientation = Qt::Horizontal;
773 773 break;
774 774 case Qt::AlignLeft:
775 775 case Qt::AlignRight:
776 776 m_orientation = Qt::Vertical;
777 777 break;
778 778 default:
779 779 qWarning()<<"No alignment specified !";
780 780 break;
781 781 };
782 782 m_alignment=alignment;
783 783 }
784 784
785 785 void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced)
786 786 {
787 787 QPen pen;
788 788 QBrush brush;
789 789 QFont font;
790 790
791 791 bool axisX = m_orientation == Qt::Horizontal;
792 792
793 793 if (m_arrowVisible) {
794 794
795 795 if (forced || brush == m_labelsBrush){
796 796 q_ptr->setLabelsBrush(theme->labelBrush());
797 797 }
798 798 //TODO: introduce axis brush
799 799 if (forced || brush == m_titleBrush){
800 800 q_ptr->setTitleBrush(theme->labelBrush());
801 801 }
802 802 if (forced || pen == m_labelsPen){
803 803 q_ptr->setLabelsPen(Qt::NoPen);// NoPen for performance reasons
804 804 }
805 805 if (forced || pen == m_titlePen){
806 806 q_ptr->setTitlePen(Qt::NoPen);// Noen for performance reasons
807 807 }
808 808 if (forced || m_shadesVisible) {
809 809
810 810 if (forced || brush == m_shadesBrush){
811 811 q_ptr->setShadesBrush(theme->backgroundShadesBrush());
812 812 }
813 813 if (forced || pen == m_shadesPen){
814 814 q_ptr->setShadesPen(theme->backgroundShadesPen());
815 815 }
816 816 if (forced && (theme->backgroundShades() == ChartTheme::BackgroundShadesBoth
817 817 || (theme->backgroundShades() == ChartTheme::BackgroundShadesVertical && axisX)
818 818 || (theme->backgroundShades() == ChartTheme::BackgroundShadesHorizontal && !axisX))) {
819 819 q_ptr->setShadesVisible(true);
820 820 }
821 821 }
822 822
823 823 if (forced || pen == m_axisPen) {
824 824 q_ptr->setLinePen(theme->axisLinePen());
825 825 }
826 826
827 827 if (forced || pen == m_gridLinePen) {
828 828 q_ptr->setGridLinePen(theme->girdLinePen());
829 829 }
830 830
831 831 if (forced || font == m_labelsFont){
832 832 q_ptr->setLabelsFont(theme->labelFont());
833 833 }
834 834 //TODO: discuss with Tero
835 835 if (forced || font == m_titleFont){
836 836 QFont font(m_labelsFont);
837 837 font.setBold(true);
838 838 q_ptr->setTitleFont(font);
839 839 }
840 840 }
841 841 }
842 842
843 843 void QAbstractAxisPrivate::handleRangeChanged(qreal min, qreal max)
844 844 {
845 845 setRange(min,max);
846 846 }
847 847
848 848 void QAbstractAxisPrivate::initializeGraphics(QGraphicsItem* parent)
849 849 {
850 850 Q_UNUSED(parent);
851 851 }
852 852
853 853 void QAbstractAxisPrivate::initializeAnimations(QChart::AnimationOptions options)
854 854 {
855 855 ChartAxis* axis = m_item.data();
856 856 Q_ASSERT(axis);
857 857 if(options.testFlag(QChart::GridAxisAnimations)) {
858 858 axis->setAnimation(new AxisAnimation(axis));
859 859 }else{
860 860 axis->setAnimation(0);
861 861 }
862 862 }
863 863
864 864
865 865
866 866 #include "moc_qabstractaxis.cpp"
867 867 #include "moc_qabstractaxis_p.cpp"
868 868
869 869 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,197 +1,197
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 QABSTRACTAXIS_H
22 22 #define QABSTRACTAXIS_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QFont>
27 27 #include <QVariant>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QAbstractAxisPrivate;
32 32
33 33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
34 34 {
35 35 Q_OBJECT
36 36 //visibility
37 37 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
38 38 //arrow
39 39 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
40 40 Q_PROPERTY(QPen linePen READ linePen WRITE setLinePen NOTIFY linePenChanged)
41 41 //TODO: make wrapping of color for qml
42 42 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
43 43 //labels
44 44 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
45 45 Q_PROPERTY(QPen lablesPen READ labelsPen WRITE setLabelsPen NOTIFY labelsPenChanged)
46 46 Q_PROPERTY(QBrush lablesBrush READ labelsBrush WRITE setLabelsBrush NOTIFY labelsBrushChanged)
47 47 //TODO: fix labels angles to work with layout
48 48 Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
49 49 Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont NOTIFY labelsFontChanged)
50 50 //TODO: make wrapping of color for qml
51 51 Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged)
52 52 //grid
53 53 Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridVisibleChanged)
54 54 Q_PROPERTY(QPen girdLinePen READ gridLinePen WRITE setGridLinePen NOTIFY gridLinePenChanged)
55 55 //shades
56 56 Q_PROPERTY(bool shadesVisible READ shadesVisible WRITE setShadesVisible NOTIFY shadesVisibleChanged)
57 57 //TODO: make wrapping of color for qml
58 58 Q_PROPERTY(QColor shadesColor READ shadesColor WRITE setShadesColor NOTIFY shadesColorChanged)
59 59 //TODO: make wrapping of border for qml
60 60 Q_PROPERTY(QColor shadesBorderColor READ shadesBorderColor WRITE setShadesBorderColor NOTIFY shadesBorderColorChanged)
61 61 Q_PROPERTY(QPen shadesPen READ shadesPen WRITE setShadesPen NOTIFY shadesPenChanged)
62 62 Q_PROPERTY(QBrush shadesBrush READ shadesBrush WRITE setShadesBrush NOTIFY shadesBrushChanged)
63 63 //title
64 64 Q_PROPERTY(QString titleText READ titleText WRITE setTitleText NOTIFY titleTextChanged)
65 65 Q_PROPERTY(QPen titlePen READ titlePen WRITE setTitlePen NOTIFY titlePenChanged)
66 66 Q_PROPERTY(QBrush titleBrush READ titleBrush WRITE setTitleBrush NOTIFY titleBrushChanged)
67 Q_PROPERTY(bool titleVisible READ titleVisible WRITE setTitleVisible)
68 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
67 Q_PROPERTY(bool titleVisible READ isTitleVisible WRITE setTitleVisible NOTIFY titleVisibleChanged)
68 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont NOTIFY titleFontChanged)
69 69 //orientation
70 70 Q_PROPERTY(Qt::Orientation orinetation READ orientation)
71 71 //aligment
72 72 Q_PROPERTY(Qt::Alignment alignment READ alignment)
73 73
74 74 public:
75 75
76 76 enum AxisType {
77 77 AxisTypeNoAxis = 0x0,
78 78 AxisTypeValue = 0x1,
79 79 AxisTypeBarCategory = 0x2,
80 80 AxisTypeCategory = 0x3,
81 81 AxisTypeDateTime = 0x4,
82 82 AxisTypeLogValue = 0x5
83 83 };
84 84
85 85 Q_DECLARE_FLAGS(AxisTypes, AxisType)
86 86
87 87 protected:
88 88 explicit QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent = 0);
89 89
90 90 public:
91 91 ~QAbstractAxis();
92 92
93 93 virtual AxisType type() const = 0;
94 94
95 95 //visibility handling
96 96 bool isVisible() const;
97 97 void setVisible(bool visible = true);
98 98 void show();
99 99 void hide();
100 100
101 101 //arrow handling
102 102 bool isLineVisible() const;
103 103 void setLineVisible(bool visible = true);
104 104 void setLinePen(const QPen &pen);
105 105 QPen linePen() const;
106 106 void setLinePenColor(QColor color);
107 107 QColor linePenColor() const;
108 108
109 109 //grid handling
110 110 bool isGridLineVisible() const;
111 111 void setGridLineVisible(bool visible = true);
112 112 void setGridLinePen(const QPen &pen);
113 113 QPen gridLinePen() const;
114 114
115 115 //labels handling
116 116 bool labelsVisible() const;
117 117 void setLabelsVisible(bool visible = true);
118 118 void setLabelsPen(const QPen &pen);
119 119 QPen labelsPen() const;
120 120 void setLabelsBrush(const QBrush &brush);
121 121 QBrush labelsBrush() const;
122 122 void setLabelsFont(const QFont &font);
123 123 QFont labelsFont() const;
124 124 void setLabelsAngle(int angle);
125 125 int labelsAngle() const;
126 126 void setLabelsColor(QColor color);
127 127 QColor labelsColor() const;
128 128
129 129 //title handling
130 bool titleVisible() const;
130 bool isTitleVisible() const;
131 131 void setTitleVisible(bool visible = true);
132 132 void setTitlePen(const QPen &pen);
133 133 QPen titlePen() const;
134 134 void setTitleBrush(const QBrush &brush);
135 135 QBrush titleBrush() const;
136 136 void setTitleFont(const QFont &font);
137 137 QFont titleFont() const;
138 138 void setTitleText(const QString &title);
139 139 QString titleText() const;
140 140
141 141 //shades handling
142 142 bool shadesVisible() const;
143 143 void setShadesVisible(bool visible = true);
144 144 void setShadesPen(const QPen &pen);
145 145 QPen shadesPen() const;
146 146 void setShadesBrush(const QBrush &brush);
147 147 QBrush shadesBrush() const;
148 148 void setShadesColor(QColor color);
149 149 QColor shadesColor() const;
150 150 void setShadesBorderColor(QColor color);
151 151 QColor shadesBorderColor() const;
152 152
153 153 Qt::Orientation orientation(); //TODO: missing const <- BC
154 154 Qt::Alignment alignment() const;
155 155
156 156 //range handling
157 157 void setMin(const QVariant &min);
158 158 void setMax(const QVariant &max);
159 159 void setRange(const QVariant &min, const QVariant &max);
160 160
161 161 Q_SIGNALS:
162 162 void visibleChanged(bool visible);
163 163 void linePenChanged(const QPen& pen);
164 164 void lineVisibleChanged(bool visible);
165 165 void labelsVisibleChanged(bool visible);
166 166 void labelsPenChanged(const QPen& pen);
167 167 void labelsBrushChanged(const QBrush& brush);
168 168 void labelsFontChanged(const QFont& pen);
169 169 void labelsAngleChanged(int angle);
170 170 void gridLinePenChanged(const QPen& pen);
171 171 void gridVisibleChanged(bool visible);
172 172 void colorChanged(QColor color);
173 173 void labelsColorChanged(QColor color);
174 174 void titleTextChanged(const QString& title);
175 175 void titlePenChanged(const QPen& pen);
176 176 void titleBrushChanged(const QBrush brush);
177 177 void titleVisibleChanged(bool visible);
178 178 void titleFontChanged(const QFont& font);
179 179 void shadesVisibleChanged(bool visible);
180 180 void shadesColorChanged(QColor color);
181 181 void shadesBorderColorChanged(QColor color);
182 182 void shadesPenChanged(const QPen& pen);
183 183 void shadesBrushChanged(const QBrush brush);
184 184
185 185 protected:
186 186 QScopedPointer<QAbstractAxisPrivate> d_ptr;
187 187 Q_DISABLE_COPY(QAbstractAxis)
188 188 friend class ChartDataSet;
189 189 friend class ChartAxis;
190 190 friend class ChartPresenter;
191 191 friend class ChartThemeManager;
192 192 friend class AbstractDomain;
193 193 };
194 194
195 195 QTCOMMERCIALCHART_END_NAMESPACE
196 196
197 197 #endif // QABSTRACTAXIS_H
@@ -1,212 +1,212
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 "verticalaxis_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include <QFontMetrics>
24 24 #include <QDebug>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis)
29 29 : ChartAxis(axis, item, intervalAxis)
30 30 {
31 31
32 32 }
33 33
34 34 VerticalAxis::~VerticalAxis()
35 35 {
36 36
37 37 }
38 38
39 39 void VerticalAxis::updateGeometry()
40 40 {
41 41 const QVector<qreal> &layout = ChartAxis::layout();
42 42
43 43 if (layout.isEmpty())
44 44 return;
45 45
46 46 QStringList labelList = labels();
47 47
48 48 QList<QGraphicsItem *> lines = lineItems();
49 49 QList<QGraphicsItem *> labels = labelItems();
50 50 QList<QGraphicsItem *> shades = shadeItems();
51 51 QList<QGraphicsItem *> axis = arrowItems();
52 52 QGraphicsSimpleTextItem* title = titleItem();
53 53
54 54 Q_ASSERT(labels.size() == labelList.size());
55 55 Q_ASSERT(layout.size() == labelList.size());
56 56
57 57 const QRectF &axisRect = axisGeometry();
58 58 const QRectF &gridRect = gridGeometry();
59 59
60 60 qreal height = axisRect.bottom();
61 61
62 62
63 63 //arrow
64 64 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
65 65
66 66 //arrow position
67 67 if (alignment()==Qt::AlignLeft)
68 68 arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
69 69 else if(alignment()==Qt::AlignRight)
70 70 arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
71 71
72 72 QFontMetrics fn(font());
73 73
74 74 //title
75 75
76 76 if (!titleText().isNull()) {
77 77 QFontMetrics fn(title->font());
78 78
79 79 int size(0);
80 80 size = gridRect.height();
81 81 QString titleText = this->titleText();
82 82
83 83 if (fn.boundingRect(titleText).width() > size) {
84 84 QString string = titleText + "...";
85 85 while (fn.boundingRect(string).width() > size && string.length() > 3)
86 86 string.remove(string.length() - 4, 1);
87 87 title->setText(string);
88 88 }
89 89 else {
90 90 title->setText(titleText);
91 91 }
92 92
93 93 QPointF center = gridRect.center() - title->boundingRect().center();
94 94 if (alignment() == Qt::AlignLeft) {
95 95 title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y());
96 96 }
97 97 else if (alignment() == Qt::AlignRight) {
98 98 title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y());
99 99 }
100 100 title->setTransformOriginPoint(title->boundingRect().center());
101 101 title->setRotation(270);
102 102 }
103 103
104 104 for (int i = 0; i < layout.size(); ++i) {
105 105
106 106 //items
107 107 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
108 108 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
109 109 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
110 110
111 111 //grid line
112 112 gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
113 113
114 114 //label text wrapping
115 115 QString text = labelList.at(i);
116 116 qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height();
117 117 if (fn.boundingRect(text).width() > size) {
118 118 QString label = text + "...";
119 119 while (fn.boundingRect(label).width() > size && label.length() > 3)
120 120 label.remove(label.length() - 4, 1);
121 121 labelItem->setText(label);
122 122 } else {
123 123 labelItem->setText(text);
124 124 }
125 125 //label transformation origin point
126 126 const QRectF &rect = labelItem->boundingRect();
127 127
128 128 QPointF center = rect.center();
129 129 labelItem->setTransformOriginPoint(center.x(), center.y());
130 130
131 131 //ticks and label position
132 132 if (alignment() == Qt::AlignLeft) {
133 133 labelItem->setPos(axisRect.right() - rect.width() - labelPadding() , layout[i] - center.y());
134 134 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
135 135 } else if (alignment() == Qt::AlignRight) {
136 136 labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y());
137 137 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
138 138 }
139 139
140 140 //label in beetwen
141 141 if(intervalAxis()&& i+1!=layout.size()) {
142 142 const qreal delta = (layout[i+1] - layout[i])/2;
143 143 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
144 144 }
145 145
146 146 //label overlap detection
147 147 if(labelItem->pos().y() + rect.height() > height ||
148 148 labelItem->pos().y() + rect.height()/2 > gridRect.bottom() ||
149 149 labelItem->pos().y() + rect.height()/2 < gridRect.top()) {
150 150 labelItem->setVisible(false);
151 151 }
152 152 else {
153 153 labelItem->setVisible(true);
154 154 height=labelItem->pos().y();
155 155 }
156 156
157 157 //shades
158 158 if ((i + 1) % 2 && i > 1) {
159 159 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
160 160 rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]);
161 161 }
162 162
163 163 // check if the grid line and the axis tick should be shown
164 164 qreal y = gridItem->line().p1().y();
165 165 if ((y < gridRect.top() || y > gridRect.bottom()))
166 166 {
167 167 gridItem->setVisible(false);
168 168 tickItem->setVisible(false);
169 169 }else{
170 170 gridItem->setVisible(true);
171 171 tickItem->setVisible(true);
172 172 }
173 173
174 174 }
175 175 //begin/end grid line in case labels between
176 176 if (intervalAxis()) {
177 177 QGraphicsLineItem *gridLine;
178 178 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
179 179 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
180 180 gridLine->setVisible(true);
181 181 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
182 182 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
183 183 gridLine->setVisible(true);
184 184 }
185 185 }
186 186
187 187 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
188 188 {
189 189
190 190 Q_UNUSED(constraint);
191 191 QFontMetrics fn(titleFont());
192 192 QSizeF sh;
193 193
194 if (titleText().isNull())
194 if (titleText().isNull() || !titleItem()->isVisible())
195 195 return sh;
196 196
197 197 switch (which) {
198 198 case Qt::MinimumSize:
199 199 sh = QSizeF(fn.height(), fn.boundingRect("...").width());
200 200 break;
201 201 case Qt::MaximumSize:
202 202 case Qt::PreferredSize:
203 203 sh = QSizeF(fn.height(), fn.boundingRect(axis()->titleText()).width());
204 204 break;
205 205 default:
206 206 break;
207 207 }
208 208
209 209 return sh;
210 210 }
211 211
212 212 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now