1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/
#include "verticalaxis_p.h"
#include "qabstractaxis.h"
#include <QFontMetrics>
#include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis)
: ChartAxis(axis, item, intervalAxis)
{
}
VerticalAxis::~VerticalAxis()
{
}
void VerticalAxis::updateGeometry()
{
const QVector<qreal> &layout = ChartAxis::layout();
if (layout.isEmpty())
return;
QStringList labelList = labels();
QList<QGraphicsItem *> lines = lineItems();
QList<QGraphicsItem *> labels = labelItems();
QList<QGraphicsItem *> shades = shadeItems();
QList<QGraphicsItem *> axis = arrowItems();
QGraphicsSimpleTextItem* title = titleItem();
Q_ASSERT(labels.size() == labelList.size());
Q_ASSERT(layout.size() == labelList.size());
const QRectF &axisRect = axisGeometry();
const QRectF &gridRect = gridGeometry();
qreal height = axisRect.bottom();
//arrow
QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
//arrow position
if (alignment()==Qt::AlignLeft)
arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
else if(alignment()==Qt::AlignRight)
arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
QFontMetrics fn(font());
//title
int titlePad = 0;
QRectF titleBoundingRect;
if (!titleText().isEmpty() && titleItem()->isVisible()) {
QFontMetrics fn(title->font());
int size(0);
size = gridRect.height();
QString titleText = this->titleText();
if (fn.boundingRect(titleText).width() > size) {
QString string = titleText + "...";
while (fn.boundingRect(string).width() > size && string.length() > 3)
string.remove(string.length() - 4, 1);
title->setText(string);
}
else {
title->setText(titleText);
}
titlePad = titlePadding();
titleBoundingRect = title->boundingRect();
QPointF center = gridRect.center() - titleBoundingRect.center();
if (alignment() == Qt::AlignLeft) {
title->setPos(axisRect.left() - titleBoundingRect.width() / 2 + titleBoundingRect.height() / 2 + titlePad, center.y());
}
else if (alignment() == Qt::AlignRight) {
title->setPos(axisRect.right() - titleBoundingRect.width() / 2 - titleBoundingRect.height() / 2 - titlePad, center.y());
}
title->setTransformOriginPoint(titleBoundingRect.center());
title->setRotation(270);
}
for (int i = 0; i < layout.size(); ++i) {
//items
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
//grid line
gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
//label text wrapping
QString text = labelList.at(i);
QRectF boundingRect = labelBoundingRect(fn, text);
qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
if (boundingRect.width() > size) {
QString label = text + "...";
while (boundingRect.width() > size && label.length() > 3) {
label.remove(label.length() - 4, 1);
boundingRect = labelBoundingRect(fn, label);
}
labelItem->setText(label);
} else {
labelItem->setText(text);
}
//label transformation origin point
const QRectF &rect = labelItem->boundingRect();
QPointF center = rect.center();
labelItem->setTransformOriginPoint(center.x(), center.y());
int widthDiff = rect.width() - boundingRect.width();
//ticks and label position
if (alignment() == Qt::AlignLeft) {
labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2) - labelPadding(), layout[i] - center.y());
tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
} else if (alignment() == Qt::AlignRight) {
labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2), layout[i] - center.y());
tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
}
//label in between
bool forceHide = false;
if (intervalAxis() && (i + 1) != layout.size()) {
qreal lowerBound = qMin(layout[i], gridRect.bottom());
qreal upperBound = qMax(layout[i + 1], gridRect.top());
const qreal delta = lowerBound - upperBound;
// Hide label in case visible part of the category at the grid edge is too narrow
if (delta < boundingRect.height()
&& (lowerBound == gridRect.bottom() || upperBound == gridRect.top())) {
forceHide = true;
} else {
labelItem->setPos(labelItem->pos().x() , lowerBound - (delta / 2.0) - center.y());
}
}
//label overlap detection - compensate one pixel for rounding errors
if (labelItem->pos().y() + boundingRect.height() > height || forceHide ||
(labelItem->pos().y() + (boundingRect.height() / 2.0) - 1.0) > axisRect.bottom() ||
labelItem->pos().y() + (boundingRect.height() / 2.0) < (axisRect.top() - 1.0)) {
labelItem->setVisible(false);
}
else {
labelItem->setVisible(true);
height=labelItem->pos().y();
}
//shades
if ((i + 1) % 2 && i > 1) {
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
qreal lowerBound = qMin(layout[i - 1], gridRect.bottom());
qreal upperBound = qMax(layout[i], gridRect.top());
rectItem->setRect(gridRect.left(), upperBound, gridRect.width(), lowerBound - upperBound);
if (rectItem->rect().height() <= 0.0)
rectItem->setVisible(false);
else
rectItem->setVisible(true);
}
// check if the grid line and the axis tick should be shown
qreal y = gridItem->line().p1().y();
if ((y < gridRect.top() || y > gridRect.bottom()))
{
gridItem->setVisible(false);
tickItem->setVisible(false);
}else{
gridItem->setVisible(true);
tickItem->setVisible(true);
}
}
//begin/end grid line in case labels between
if (intervalAxis()) {
QGraphicsLineItem *gridLine;
gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
gridLine->setVisible(true);
gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
gridLine->setVisible(true);
}
}
QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(constraint);
QFontMetrics fn(titleFont());
QSizeF sh(0,0);
if (titleText().isEmpty() || !titleItem()->isVisible())
return sh;
switch (which) {
case Qt::MinimumSize:
sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width());
break;
case Qt::MaximumSize:
case Qt::PreferredSize:
sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect(axis()->titleText()).width());
break;
default:
break;
}
return sh;
}
QTCOMMERCIALCHART_END_NAMESPACE
|