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