##// END OF EJS Templates
fix: forgot to restage previous commit
Marek Rosa -
r2269:b003e569935a
parent child
Show More
@@ -1,486 +1,487
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 "domain_p.h"
27 27 #include <qmath.h>
28 28 #include <QDateTime>
29 29 #include <QValueAxis>
30 30 #include <QGraphicsLayout>
31 31 #include <QFontMetrics>
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis)
36 36 : ChartElement(presenter),
37 37 m_chartAxis(axis),
38 38 m_labelsAngle(0),
39 39 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
40 40 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
41 41 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
42 42 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
43 43 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
44 44 m_min(0),
45 45 m_max(0),
46 46 m_animation(0),
47 47 m_labelPadding(5),
48 48 m_intervalAxis(intervalAxis)
49 49 {
50 50 //initial initialization
51 51 m_arrow->setZValue(ChartPresenter::AxisZValue);
52 52 m_arrow->setHandlesChildEvents(false);
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
58 58 QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated()));
59 59
60 60 QGraphicsSimpleTextItem item;
61 61 m_font = item.font();
62 62
63 63 }
64 64
65 65 ChartAxis::~ChartAxis()
66 66 {
67 67 }
68 68
69 69 void ChartAxis::setAnimation(AxisAnimation *animation)
70 70 {
71 71 m_animation = animation;
72 72 }
73 73
74 74 void ChartAxis::setLayout(QVector<qreal> &layout)
75 75 {
76 76 m_layoutVector = layout;
77 77 }
78 78
79 79 void ChartAxis::createItems(int count)
80 80 {
81 81 if (m_arrow->childItems().size() == 0)
82 82 m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem()));
83 83
84 84 if (m_intervalAxis && m_grid->childItems().size() == 0) {
85 85 for (int i = 0 ; i < 2 ; i ++)
86 86 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
87 87 }
88 88
89 89 for (int i = 0; i < count; ++i) {
90 90 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
91 91 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem());
92 92 label->setFont(m_font);
93 93 m_labels->addToGroup(label);
94 94 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
95 95 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2)
96 96 m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
97 97 }
98 98 }
99 99
100 100 void ChartAxis::deleteItems(int count)
101 101 {
102 102 QList<QGraphicsItem *> lines = m_grid->childItems();
103 103 QList<QGraphicsItem *> labels = m_labels->childItems();
104 104 QList<QGraphicsItem *> shades = m_shades->childItems();
105 105 QList<QGraphicsItem *> axis = m_arrow->childItems();
106 106
107 107 for (int i = 0; i < count; ++i) {
108 108 if (lines.size() % 2 && lines.size() > 1)
109 109 delete(shades.takeLast());
110 110 delete(lines.takeLast());
111 111 delete(labels.takeLast());
112 112 delete(axis.takeLast());
113 113 }
114 114 }
115 115
116 116 void ChartAxis::updateLayout(QVector<qreal> &layout)
117 117 {
118 118 int diff = m_layoutVector.size() - layout.size();
119 119
120 120 if (diff > 0)
121 121 deleteItems(diff);
122 122 else if (diff < 0)
123 123 createItems(-diff);
124 124
125 125 if (diff < 0) handleAxisUpdated();
126 126
127 127 if (m_animation) {
128 128 switch (presenter()->state()) {
129 129 case ChartPresenter::ZoomInState:
130 130 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
131 131 m_animation->setAnimationPoint(presenter()->statePoint());
132 132 break;
133 133 case ChartPresenter::ZoomOutState:
134 134 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
135 135 m_animation->setAnimationPoint(presenter()->statePoint());
136 136 break;
137 137 case ChartPresenter::ScrollUpState:
138 138 case ChartPresenter::ScrollLeftState:
139 139 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
140 140 break;
141 141 case ChartPresenter::ScrollDownState:
142 142 case ChartPresenter::ScrollRightState:
143 143 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
144 144 break;
145 145 case ChartPresenter::ShowState:
146 146 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
147 147 break;
148 148 }
149 149 m_animation->setValues(m_layoutVector, layout);
150 150 presenter()->startAnimation(m_animation);
151 151 } else {
152 152 setLayout(layout);
153 153 updateGeometry();
154 154 }
155 155 }
156 156
157 157 void ChartAxis::setArrowOpacity(qreal opacity)
158 158 {
159 159 m_arrow->setOpacity(opacity);
160 160 }
161 161
162 162 qreal ChartAxis::arrowOpacity() const
163 163 {
164 164 return m_arrow->opacity();
165 165 }
166 166
167 167 void ChartAxis::setArrowVisibility(bool visible)
168 168 {
169 169 m_arrow->setOpacity(visible);
170 170 }
171 171
172 172 void ChartAxis::setGridOpacity(qreal opacity)
173 173 {
174 174 m_grid->setOpacity(opacity);
175 175 }
176 176
177 177 qreal ChartAxis::gridOpacity() const
178 178 {
179 179 return m_grid->opacity();
180 180 }
181 181
182 182 void ChartAxis::setGridVisibility(bool visible)
183 183 {
184 184 m_grid->setOpacity(visible);
185 185 }
186 186
187 187 void ChartAxis::setLabelsOpacity(qreal opacity)
188 188 {
189 189 m_labels->setOpacity(opacity);
190 190 }
191 191
192 192 qreal ChartAxis::labelsOpacity() const
193 193 {
194 194 return m_labels->opacity();
195 195 }
196 196
197 197 void ChartAxis::setLabelsVisibility(bool visible)
198 198 {
199 199 m_labels->setOpacity(visible);
200 200 }
201 201
202 202 void ChartAxis::setShadesOpacity(qreal opacity)
203 203 {
204 204 m_shades->setOpacity(opacity);
205 205 }
206 206
207 207 qreal ChartAxis::shadesOpacity() const
208 208 {
209 209 return m_shades->opacity();
210 210 }
211 211
212 212 void ChartAxis::setShadesVisibility(bool visible)
213 213 {
214 214 m_shades->setVisible(visible);
215 215 }
216 216
217 217 void ChartAxis::setLabelsAngle(int angle)
218 218 {
219 219 foreach (QGraphicsItem *item, m_labels->childItems())
220 220 item->setRotation(angle);
221 221
222 222 m_labelsAngle = angle;
223 223 }
224 224
225 225 void ChartAxis::setLabelsPen(const QPen &pen)
226 226 {
227 227 foreach (QGraphicsItem *item , m_labels->childItems())
228 228 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
229 229 }
230 230
231 231 void ChartAxis::setLabelsBrush(const QBrush &brush)
232 232 {
233 233 foreach (QGraphicsItem *item , m_labels->childItems())
234 234 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
235 235 }
236 236
237 237 void ChartAxis::setLabelsFont(const QFont &font)
238 238 {
239 239 if (m_font != font) {
240 240 m_font = font;
241 241 foreach (QGraphicsItem *item , m_labels->childItems())
242 242 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
243 243 QGraphicsLayoutItem::updateGeometry();
244 244 presenter()->layout()->invalidate();
245 245 }
246 246 }
247 247
248 248 void ChartAxis::setShadesBrush(const QBrush &brush)
249 249 {
250 250 foreach (QGraphicsItem *item , m_shades->childItems())
251 251 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
252 252 }
253 253
254 254 void ChartAxis::setShadesPen(const QPen &pen)
255 255 {
256 256 foreach (QGraphicsItem *item , m_shades->childItems())
257 257 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
258 258 }
259 259
260 260 void ChartAxis::setArrowPen(const QPen &pen)
261 261 {
262 262 foreach (QGraphicsItem *item , m_arrow->childItems())
263 263 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
264 264 }
265 265
266 266 void ChartAxis::setGridPen(const QPen &pen)
267 267 {
268 268 foreach (QGraphicsItem *item , m_grid->childItems())
269 269 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
270 270 }
271 271
272 272 void ChartAxis::setLabelPadding(int padding)
273 273 {
274 274 m_labelPadding = padding;
275 275 }
276 276
277 277 bool ChartAxis::isEmpty()
278 278 {
279 279 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(m_min, m_max);
280 280 }
281 281
282 282 void ChartAxis::handleDomainUpdated()
283 283 {
284 284 Domain *domain = qobject_cast<Domain *>(sender());
285 285 qreal min(0);
286 286 qreal max(0);
287 287
288 288 if (m_chartAxis->orientation() == Qt::Horizontal) {
289 289 min = domain->minX();
290 290 max = domain->maxX();
291 291 } else if (m_chartAxis->orientation() == Qt::Vertical) {
292 292 min = domain->minY();
293 293 max = domain->maxY();
294 294 }
295 295
296 296 if (!qFuzzyCompare(m_min, min) || !qFuzzyCompare(m_max, max)) {
297 297 m_min = min;
298 298 m_max = max;
299 299
300 300 if (!isEmpty()) {
301 301
302 302 QVector<qreal> layout = calculateLayout();
303 303 updateLayout(layout);
304 304 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
305 305 QSizeF after = sizeHint(Qt::PreferredSize);
306 306
307 307 if (before != after) {
308 308 QGraphicsLayoutItem::updateGeometry();
309 309 //we don't want to call invalidate on layout, since it will change minimum size of component,
310 310 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
311 311 //instead recalculate layout and use plotArea for extra space.
312 312 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
313 313 }
314 314 }
315 315 }
316 316 }
317 317
318 318 void ChartAxis::handleAxisUpdated()
319 319 {
320 320 if (isEmpty())
321 321 return;
322 322
323 323 bool visible = m_chartAxis->isVisible();
324 324
325 325 //TODO: split this into separate signal/slots ?
326 326 setArrowVisibility(visible && m_chartAxis->isLineVisible());
327 327 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
328 328 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
329 329 setShadesVisibility(visible && m_chartAxis->shadesVisible());
330 330 setLabelsAngle(m_chartAxis->labelsAngle());
331 331 setArrowPen(m_chartAxis->linePen());
332 332 setLabelsPen(m_chartAxis->labelsPen());
333 333 setLabelsBrush(m_chartAxis->labelsBrush());
334 334 setLabelsFont(m_chartAxis->labelsFont());
335 335 setGridPen(m_chartAxis->gridLinePen());
336 336 setShadesPen(m_chartAxis->shadesPen());
337 337 setShadesBrush(m_chartAxis->shadesBrush());
338 338 setTitleText(m_chartAxis->title());
339 339 setTitleFont(m_chartAxis->titleFont());
340 340 setTitlePen(m_chartAxis->titlePen());
341 341 setTitleBrush(m_chartAxis->titleBrush());
342 342 }
343 343
344 344 void ChartAxis::setTitleText(const QString &title)
345 345 {
346 346 if (m_titleText != title) {
347 347 m_titleText = title;
348 348 QGraphicsLayoutItem::updateGeometry();
349 349 presenter()->layout()->invalidate();
350 350 }
351 351 }
352 352
353 353 void ChartAxis::setTitlePen(const QPen &pen)
354 354 {
355 355 m_title->setPen(pen);
356 356 }
357 357
358 358 void ChartAxis::setTitleBrush(const QBrush &brush)
359 359 {
360 360 m_title->setBrush(brush);
361 361 }
362 362
363 363 void ChartAxis::setTitleFont(const QFont &font)
364 364 {
365 365 if(m_title->font() != font){
366 366 m_title->setFont(font);
367 367 QGraphicsLayoutItem::updateGeometry();
368 368 presenter()->layout()->invalidate();
369 369 }
370 370 }
371 371
372 372 QFont ChartAxis::titleFont() const
373 373 {
374 374 return m_title->font();
375 375 }
376 376
377 377 void ChartAxis::hide()
378 378 {
379 379 setArrowVisibility(false);
380 380 setGridVisibility(false);
381 381 setLabelsVisibility(false);
382 382 setShadesVisibility(false);
383 383 }
384 384
385 385 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
386 386 {
387 387 m_gridRect = grid;
388 388 m_axisRect = axis;
389 389
390 390 if (isEmpty())
391 391 return;
392 392
393 393 QVector<qreal> layout = calculateLayout();
394 394 updateLayout(layout);
395 395
396 396 }
397 397
398 398 void ChartAxis::axisSelected()
399 399 {
400 400 //TODO: axis clicked;
401 401 }
402 402
403 403 Qt::Orientation ChartAxis::orientation() const
404 404 {
405 405 return m_chartAxis->orientation();
406 406 }
407 407
408 408 Qt::Alignment ChartAxis::alignment() const
409 409 {
410 410 return m_chartAxis->alignment();
411 411 }
412 412
413 413 bool ChartAxis::isVisible()
414 414 {
415 415 return m_chartAxis->isVisible();
416 416 }
417 417
418 418 void ChartAxis::setLabels(const QStringList &labels)
419 419 {
420 420 m_labelsList = labels;
421 421 }
422 422
423 423 QStringList ChartAxis::createValueLabels(int ticks) const
424 424 {
425 425 Q_ASSERT(m_max > m_min);
426 426 Q_ASSERT(ticks > 1);
427 427
428 428 QStringList labels;
429 429
430 430 int n = qMax(int(-qFloor(log10((m_max - m_min) / (ticks - 1)))), 0);
431 431 n++;
432 432
433 433 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
434 434
435 435 QString format = axis->labelFormat();
436 436
437 437 if (format.isNull()) {
438 438 for (int i = 0; i < ticks; i++) {
439 439 qreal value = m_min + (i * (m_max - m_min) / (ticks - 1));
440 440 labels << QString::number(value, 'f', n);
441 441 }
442 442 } else {
443 443 QByteArray array = format.toLatin1();
444 444 for (int i = 0; i < ticks; i++) {
445 445 qreal value = m_min + (i * (m_max - m_min) / (ticks - 1));
446 446 if (format.contains("d")
447 447 || format.contains("i")
448 448 || format.contains("c"))
449
450 449 labels << QString().sprintf(array, (qint64)value);
451 450 else if (format.contains("u")
452 451 || format.contains("o")
453 452 || format.contains("x", Qt::CaseInsensitive))
454 453 labels << QString().sprintf(array, (quint64)value);
455 else
454 else if (format.contains("f", Qt::CaseInsensitive)
455 || format.contains("e", Qt::CaseInsensitive)
456 || format.contains("g", Qt::CaseInsensitive))
456 457 labels << QString().sprintf(array, value);
457 458 }
458 459 }
459 460
460 461 return labels;
461 462 }
462 463
463 464 QStringList ChartAxis::createDateTimeLabels(const QString &format, int ticks) const
464 465 {
465 466 Q_ASSERT(m_max > m_min);
466 467 Q_ASSERT(ticks > 1);
467 468 QStringList labels;
468 469 int n = qMax(int(-floor(log10((m_max - m_min) / (ticks - 1)))), 0);
469 470 n++;
470 471 for (int i = 0; i < ticks; i++) {
471 472 qreal value = m_min + (i * (m_max - m_min) / (ticks - 1));
472 473 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
473 474 }
474 475 return labels;
475 476 }
476 477
477 478 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
478 479 {
479 480 Q_UNUSED(which);
480 481 Q_UNUSED(constraint);
481 482 return QSizeF();
482 483 }
483 484
484 485 #include "moc_chartaxis_p.cpp"
485 486
486 487 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now