##// END OF EJS Templates
label angle initialization fix
sauimone -
r2406:d3d25f8317f5
parent child
Show More
@@ -1,526 +1,527
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_labels->setZValue(ChartPresenter::AxisZValue);
54 54 m_shades->setZValue(ChartPresenter::ShadesZValue);
55 55 m_grid->setZValue(ChartPresenter::GridZValue);
56 56 m_title->setZValue(ChartPresenter::GridZValue);
57 57 handleVisibleChanged(m_axis->isVisible());
58 58 connectSlots();
59 59
60 60 setFlag(QGraphicsItem::ItemHasNoContents,true);
61 61 }
62 62
63 63 void ChartAxis::connectSlots()
64 64 {
65 65 QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool)));
66 66 QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool)));
67 67 QObject::connect(m_axis,SIGNAL(gridVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool)));
68 68 QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool)));
69 69 QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool)));
70 70 QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int)));
71 71 QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&)));
72 72 QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&)));
73 73 QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&)));
74 74 QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&)));
75 75 QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&)));
76 76 QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&)));
77 77 QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&)));
78 78 QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&)));
79 79 QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&)));
80 80 QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&)));
81 81 QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&)));
82 82 QObject::connect(m_axis,SIGNAL(titleVisibleChanged(bool)),this,SLOT(handleTitleVisibleChanged(bool)));
83 83 QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal)));
84 84 }
85 85
86 86 ChartAxis::~ChartAxis()
87 87 {
88 88 }
89 89
90 90 void ChartAxis::setAnimation(AxisAnimation *animation)
91 91 {
92 92 m_animation = animation;
93 93 }
94 94
95 95 void ChartAxis::setLayout(QVector<qreal> &layout)
96 96 {
97 97 m_layoutVector = layout;
98 98 }
99 99
100 100 void ChartAxis::createItems(int count)
101 101 {
102 102 if (m_arrow->childItems().size() == 0){
103 103 QGraphicsLineItem* arrow = new ArrowItem(this, this);
104 104 arrow->setPen(m_axis->linePen());
105 105 m_arrow->addToGroup(arrow);
106 106 }
107 107
108 108 if (m_intervalAxis && m_grid->childItems().size() == 0) {
109 109 for (int i = 0 ; i < 2 ; i ++){
110 110 QGraphicsLineItem* item = new QGraphicsLineItem(this);
111 111 item->setPen(m_axis->gridLinePen());
112 112 m_grid->addToGroup(item);
113 113 }
114 114 }
115 115
116 116 for (int i = 0; i < count; ++i) {
117 117 QGraphicsLineItem* arrow = new QGraphicsLineItem(this);
118 118 arrow->setPen(m_axis->linePen());
119 119 QGraphicsLineItem* grid = new QGraphicsLineItem(this);
120 120 grid->setPen(m_axis->gridLinePen());
121 121 QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this);
122 122 label->setFont(m_axis->labelsFont());
123 123 label->setPen(m_axis->labelsPen());
124 124 label->setBrush(m_axis->labelsBrush());
125 label->setRotation(m_labelsAngle);
125 126 m_arrow->addToGroup(arrow);
126 127 m_grid->addToGroup(grid);
127 128 m_labels->addToGroup(label);
128 129
129 130 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){
130 131 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
131 132 shades->setPen(m_axis->shadesPen());
132 133 shades->setBrush(m_axis->shadesBrush());
133 134 m_shades->addToGroup(shades);
134 135 }
135 136 }
136 137
137 138 }
138 139
139 140 void ChartAxis::deleteItems(int count)
140 141 {
141 142 QList<QGraphicsItem *> lines = m_grid->childItems();
142 143 QList<QGraphicsItem *> labels = m_labels->childItems();
143 144 QList<QGraphicsItem *> shades = m_shades->childItems();
144 145 QList<QGraphicsItem *> axis = m_arrow->childItems();
145 146
146 147 for (int i = 0; i < count; ++i) {
147 148 if (lines.size() % 2 && lines.size() > 1)
148 149 delete(shades.takeLast());
149 150 delete(lines.takeLast());
150 151 delete(labels.takeLast());
151 152 delete(axis.takeLast());
152 153 }
153 154 }
154 155
155 156 void ChartAxis::updateLayout(QVector<qreal> &layout)
156 157 {
157 158 int diff = m_layoutVector.size() - layout.size();
158 159
159 160 if (diff > 0)
160 161 deleteItems(diff);
161 162 else if (diff < 0)
162 163 createItems(-diff);
163 164
164 165 if (m_animation) {
165 166 switch (presenter()->state()) {
166 167 case ChartPresenter::ZoomInState:
167 168 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
168 169 m_animation->setAnimationPoint(presenter()->statePoint());
169 170 break;
170 171 case ChartPresenter::ZoomOutState:
171 172 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
172 173 m_animation->setAnimationPoint(presenter()->statePoint());
173 174 break;
174 175 case ChartPresenter::ScrollUpState:
175 176 case ChartPresenter::ScrollLeftState:
176 177 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
177 178 break;
178 179 case ChartPresenter::ScrollDownState:
179 180 case ChartPresenter::ScrollRightState:
180 181 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
181 182 break;
182 183 case ChartPresenter::ShowState:
183 184 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
184 185 break;
185 186 }
186 187 m_animation->setValues(m_layoutVector, layout);
187 188 presenter()->startAnimation(m_animation);
188 189 } else {
189 190 setLayout(layout);
190 191 updateGeometry();
191 192 }
192 193 }
193 194
194 195 void ChartAxis::setLabelPadding(int padding)
195 196 {
196 197 m_labelPadding = padding;
197 198 }
198 199
199 200 bool ChartAxis::isEmpty()
200 201 {
201 202 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max());
202 203 }
203 204
204 205 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
205 206 {
206 207 m_gridRect = grid;
207 208 m_axisRect = axis;
208 209
209 210 if (isEmpty())
210 211 return;
211 212
212 213 QVector<qreal> layout = calculateLayout();
213 214 updateLayout(layout);
214 215 }
215 216
216 217 qreal ChartAxis::min() const
217 218 {
218 219 return m_axis->d_ptr->min();
219 220 }
220 221
221 222 qreal ChartAxis::max() const
222 223 {
223 224 return m_axis->d_ptr->max();
224 225 }
225 226
226 227 QFont ChartAxis::font() const
227 228 {
228 229 return m_axis->labelsFont();
229 230 }
230 231
231 232 QFont ChartAxis::titleFont() const
232 233 {
233 234 return m_axis->titleFont();
234 235 }
235 236
236 237 QString ChartAxis::titleText() const
237 238 {
238 239 return m_axis->titleText();
239 240 }
240 241
241 242 void ChartAxis::axisSelected()
242 243 {
243 244 emit clicked();
244 245 }
245 246
246 247 Qt::Orientation ChartAxis::orientation() const
247 248 {
248 249 return m_axis->orientation();
249 250 }
250 251
251 252 Qt::Alignment ChartAxis::alignment() const
252 253 {
253 254 return m_axis->alignment();
254 255 }
255 256
256 257 void ChartAxis::setLabels(const QStringList &labels)
257 258 {
258 259 m_labelsList = labels;
259 260 }
260 261
261 262 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
262 263 {
263 264 Q_UNUSED(which);
264 265 Q_UNUSED(constraint);
265 266 return QSizeF();
266 267 }
267 268
268 269 //handlers
269 270
270 271 void ChartAxis::handleArrowVisibleChanged(bool visible)
271 272 {
272 273 m_arrow->setVisible(visible);
273 274 }
274 275
275 276 void ChartAxis::handleGridVisibleChanged(bool visible)
276 277 {
277 278 m_grid->setVisible(visible);
278 279 }
279 280
280 281 void ChartAxis::handleLabelsVisibleChanged(bool visible)
281 282 {
282 283 m_labels->setVisible(visible);
283 284 }
284 285
285 286 void ChartAxis::handleShadesVisibleChanged(bool visible)
286 287 {
287 288 m_shades->setVisible(visible);
288 289 }
289 290
290 291 void ChartAxis::handleTitleVisibleChanged(bool visible)
291 292 {
292 293 m_title->setVisible(visible);
293 294 presenter()->layout()->invalidate();
294 295 }
295 296
296 297 void ChartAxis::handleLabelsAngleChanged(int angle)
297 298 {
298 299 foreach (QGraphicsItem *item, m_labels->childItems())
299 300 item->setRotation(angle);
300 301
301 302 m_labelsAngle = angle;
302 303 }
303 304
304 305 void ChartAxis::handleLabelsPenChanged(const QPen &pen)
305 306 {
306 307 foreach (QGraphicsItem *item , m_labels->childItems())
307 308 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
308 309 }
309 310
310 311 void ChartAxis::handleLabelsBrushChanged(const QBrush &brush)
311 312 {
312 313 foreach (QGraphicsItem *item , m_labels->childItems())
313 314 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
314 315 }
315 316
316 317 void ChartAxis::handleLabelsFontChanged(const QFont &font)
317 318 {
318 319 foreach (QGraphicsItem *item , m_labels->childItems())
319 320 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
320 321 QGraphicsLayoutItem::updateGeometry();
321 322 presenter()->layout()->invalidate();
322 323 }
323 324
324 325 void ChartAxis::handleShadesBrushChanged(const QBrush &brush)
325 326 {
326 327 foreach (QGraphicsItem *item , m_shades->childItems())
327 328 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
328 329 }
329 330
330 331 void ChartAxis::handleShadesPenChanged(const QPen &pen)
331 332 {
332 333 foreach (QGraphicsItem *item , m_shades->childItems())
333 334 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
334 335 }
335 336
336 337 void ChartAxis::handleArrowPenChanged(const QPen &pen)
337 338 {
338 339 foreach (QGraphicsItem *item , m_arrow->childItems())
339 340 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
340 341 }
341 342
342 343 void ChartAxis::handleGridPenChanged(const QPen &pen)
343 344 {
344 345 foreach (QGraphicsItem *item , m_grid->childItems())
345 346 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
346 347 }
347 348
348 349 void ChartAxis::handleTitleTextChanged(const QString &title)
349 350 {
350 351 Q_UNUSED(title)
351 352 QGraphicsLayoutItem::updateGeometry();
352 353 presenter()->layout()->invalidate();
353 354 }
354 355
355 356
356 357 void ChartAxis::handleTitlePenChanged(const QPen &pen)
357 358 {
358 359 m_title->setPen(pen);
359 360 }
360 361
361 362 void ChartAxis::handleTitleBrushChanged(const QBrush &brush)
362 363 {
363 364 m_title->setBrush(brush);
364 365 }
365 366
366 367 void ChartAxis::handleTitleFontChanged(const QFont &font)
367 368 {
368 369 if(m_title->font() != font){
369 370 m_title->setFont(font);
370 371 QGraphicsLayoutItem::updateGeometry();
371 372 presenter()->layout()->invalidate();
372 373 }
373 374 }
374 375
375 376 void ChartAxis::handleVisibleChanged(bool visible)
376 377 {
377 378 setVisible(visible);
378 379 if(!visible) {
379 380 m_grid->setVisible(visible);
380 381 m_arrow->setVisible(visible);
381 382 m_shades->setVisible(visible);
382 383 m_labels->setVisible(visible);
383 384 m_title->setVisible(visible);
384 385 }else {
385 386 m_grid->setVisible(m_axis->isGridLineVisible());
386 387 m_arrow->setVisible(m_axis->isLineVisible());
387 388 m_shades->setVisible(m_axis->shadesVisible());
388 389 m_labels->setVisible(m_axis->labelsVisible());
389 390 m_title->setVisible(m_axis->isTitleVisible());
390 391 }
391 392
392 393 if(presenter()) presenter()->layout()->invalidate();
393 394 }
394 395
395 396 void ChartAxis::handleRangeChanged(qreal min, qreal max)
396 397 {
397 398 Q_UNUSED(min);
398 399 Q_UNUSED(max);
399 400
400 401 if (!isEmpty()) {
401 402
402 403 QVector<qreal> layout = calculateLayout();
403 404 updateLayout(layout);
404 405 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
405 406 QSizeF after = sizeHint(Qt::PreferredSize);
406 407
407 408 if (before != after) {
408 409 QGraphicsLayoutItem::updateGeometry();
409 410 //we don't want to call invalidate on layout, since it will change minimum size of component,
410 411 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
411 412 //instead recalculate layout and use plotArea for extra space.
412 413 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
413 414 }
414 415 }
415 416
416 417 }
417 418
418 419 //helpers
419 420
420 421 QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format)
421 422 {
422 423 QStringList labels;
423 424
424 425 if(max <= min || ticks < 1){
425 426 return labels;
426 427 }
427 428
428 429 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
429 430 n++;
430 431
431 432 if (format.isNull()) {
432 433 for (int i = 0; i < ticks; i++) {
433 434 qreal value = min + (i * (max - min) / (ticks - 1));
434 435 labels << QString::number(value, 'f', n);
435 436 }
436 437 } else {
437 438 QByteArray array = format.toLatin1();
438 439 for (int i = 0; i < ticks; i++) {
439 440 qreal value = min + (i * (max - min) / (ticks - 1));
440 441 if (format.contains("d")
441 442 || format.contains("i")
442 443 || format.contains("c"))
443 444 labels << QString().sprintf(array, (qint64)value);
444 445 else if (format.contains("u")
445 446 || format.contains("o")
446 447 || format.contains("x", Qt::CaseInsensitive))
447 448 labels << QString().sprintf(array, (quint64)value);
448 449 else if (format.contains("f", Qt::CaseInsensitive)
449 450 || format.contains("e", Qt::CaseInsensitive)
450 451 || format.contains("g", Qt::CaseInsensitive))
451 452 labels << QString().sprintf(array, value);
452 453 else
453 454 labels << QString();
454 455 }
455 456 }
456 457
457 458 return labels;
458 459 }
459 460
460 461 QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format)
461 462 {
462 463 // Q_ASSERT(m_max > m_min);
463 464 // Q_ASSERT(ticks > 1);
464 465
465 466 QStringList labels;
466 467
467 468 int n = 0;
468 469 if (ticks > 1)
469 470 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
470 471 n++;
471 472
472 473 int firstTick;
473 474 if (base > 1)
474 475 firstTick = ceil(log10(min) / log10(base));
475 476 else
476 477 firstTick = ceil(log10(max) / log10(base));
477 478
478 479 if (format.isNull()) {
479 480 for (int i = firstTick; i < ticks + firstTick; i++) {
480 481 qreal value = qPow(base, i);
481 482 labels << QString::number(value, 'f', n);
482 483 }
483 484 } else {
484 485 QByteArray array = format.toLatin1();
485 486 for (int i = firstTick; i < ticks + firstTick; i++) {
486 487 qreal value = qPow(base, i);
487 488 if (format.contains("d")
488 489 || format.contains("i")
489 490 || format.contains("c"))
490 491 labels << QString().sprintf(array, (qint64)value);
491 492 else if (format.contains("u")
492 493 || format.contains("o")
493 494 || format.contains("x", Qt::CaseInsensitive))
494 495 labels << QString().sprintf(array, (quint64)value);
495 496 else if (format.contains("f", Qt::CaseInsensitive)
496 497 || format.contains("e", Qt::CaseInsensitive)
497 498 || format.contains("g", Qt::CaseInsensitive))
498 499 labels << QString().sprintf(array, value);
499 500 else
500 501 labels << QString();
501 502 }
502 503 }
503 504
504 505 return labels;
505 506 }
506 507
507 508 QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format)
508 509 {
509 510 QStringList labels;
510 511
511 512 if(max <= min || ticks < 1) {
512 513 return labels;
513 514 }
514 515
515 516 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
516 517 n++;
517 518 for (int i = 0; i < ticks; i++) {
518 519 qreal value = min + (i * (max - min) / (ticks - 1));
519 520 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
520 521 }
521 522 return labels;
522 523 }
523 524
524 525 #include "moc_chartaxis_p.cpp"
525 526
526 527 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now