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