@@ -1,223 +1,225 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2014 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 Enterprise Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Enterprise licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Enterprise 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_p.h" |
|
23 | 23 | #include "chartpresenter_p.h" |
|
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 | : CartesianChartAxis(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 = ChartAxisElement::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 = gridItems(); |
|
48 | 48 | QList<QGraphicsItem *> labels = labelItems(); |
|
49 | 49 | QList<QGraphicsItem *> shades = shadeItems(); |
|
50 | 50 | QList<QGraphicsItem *> arrow = arrowItems(); |
|
51 | 51 | QGraphicsTextItem *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 *>(arrow.at(0)); |
|
61 | 61 | |
|
62 | 62 | if (axis()->alignment() == Qt::AlignTop) |
|
63 | 63 | arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom()); |
|
64 | 64 | else if (axis()->alignment() == Qt::AlignBottom) |
|
65 | 65 | arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top()); |
|
66 | 66 | |
|
67 | 67 | qreal width = 0; |
|
68 | const QLatin1String ellipsis("..."); | |
|
68 | 69 | |
|
69 | 70 | //title |
|
70 | 71 | QRectF titleBoundingRect; |
|
71 | 72 | QString titleText = axis()->titleText(); |
|
72 | 73 | qreal availableSpace = axisRect.height() - labelPadding(); |
|
73 | 74 | if (!titleText.isEmpty() && titleItem()->isVisible()) { |
|
74 | 75 | availableSpace -= titlePadding() * 2.0; |
|
75 | 76 | qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").height(); |
|
76 | 77 | qreal titleSpace = availableSpace - minimumLabelHeight; |
|
77 | 78 | title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), |
|
78 | 79 | gridRect.width(), titleSpace, |
|
79 | 80 | titleBoundingRect)); |
|
80 | 81 | title->setTextWidth(titleBoundingRect.width()); |
|
81 | 82 | |
|
82 | 83 | titleBoundingRect = title->boundingRect(); |
|
83 | 84 | |
|
84 | 85 | QPointF center = gridRect.center() - titleBoundingRect.center(); |
|
85 | 86 | if (axis()->alignment() == Qt::AlignTop) |
|
86 | 87 | title->setPos(center.x(), axisRect.top() + titlePadding()); |
|
87 | 88 | else if (axis()->alignment() == Qt::AlignBottom) |
|
88 | 89 | title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePadding()); |
|
89 | 90 | |
|
90 | 91 | availableSpace -= titleBoundingRect.height(); |
|
91 | 92 | } |
|
92 | 93 | |
|
93 | 94 | for (int i = 0; i < layout.size(); ++i) { |
|
94 | 95 | //items |
|
95 | 96 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
96 | 97 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1)); |
|
97 | 98 | QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i)); |
|
98 | 99 | |
|
99 | 100 | //grid line |
|
100 | 101 | gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); |
|
101 | 102 | |
|
102 | 103 | //label text wrapping |
|
103 | 104 | QString text = labelList.at(i); |
|
104 | 105 | QRectF boundingRect; |
|
105 | 106 | // don't truncate empty labels |
|
106 | 107 | if (text.isEmpty()) { |
|
107 | 108 | labelItem->setHtml(text); |
|
108 | 109 | } else { |
|
109 | 110 | qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding()); |
|
110 | 111 | QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text, |
|
111 | 112 | axis()->labelsAngle(), |
|
112 | 113 | labelWidth, |
|
113 | 114 | availableSpace, boundingRect); |
|
114 | 115 | labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(), |
|
115 | 116 | truncatedText).width()); |
|
116 | 117 | labelItem->setHtml(truncatedText); |
|
117 | 118 | } |
|
118 | 119 | |
|
119 | 120 | //label transformation origin point |
|
120 | 121 | const QRectF& rect = labelItem->boundingRect(); |
|
121 | 122 | QPointF center = rect.center(); |
|
122 | 123 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
123 | 124 | qreal heightDiff = rect.height() - boundingRect.height(); |
|
124 | 125 | qreal widthDiff = rect.width() - boundingRect.width(); |
|
125 | 126 | |
|
126 | 127 | //ticks and label position |
|
127 | 128 | if (axis()->alignment() == Qt::AlignTop) { |
|
128 | 129 | labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2.0) - labelPadding()); |
|
129 | 130 | tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding()); |
|
130 | 131 | } else if (axis()->alignment() == Qt::AlignBottom) { |
|
131 | 132 | labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) + labelPadding()); |
|
132 | 133 | tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding()); |
|
133 | 134 | } |
|
134 | 135 | |
|
135 | 136 | //label in between |
|
136 | 137 | bool forceHide = false; |
|
137 | 138 | if (intervalAxis() && (i + 1) != layout.size()) { |
|
138 | 139 | qreal leftBound = qMax(layout[i], gridRect.left()); |
|
139 | 140 | qreal rightBound = qMin(layout[i + 1], gridRect.right()); |
|
140 | 141 | const qreal delta = rightBound - leftBound; |
|
141 | 142 | // Hide label in case visible part of the category at the grid edge is too narrow |
|
142 | 143 | if (delta < boundingRect.width() |
|
143 | 144 | && (leftBound == gridRect.left() || rightBound == gridRect.right()) |
|
144 | 145 | && !intervalAxis()) { |
|
145 | 146 | forceHide = true; |
|
146 | 147 | } else { |
|
147 | 148 | labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y()); |
|
148 | 149 | } |
|
149 | 150 | } |
|
151 | ||
|
150 | 152 | //label overlap detection - compensate one pixel for rounding errors |
|
151 | if (labelItem->pos().x() < width || forceHide || | |
|
153 | if ((labelItem->pos().x() < width && labelItem->toPlainText() == ellipsis) || forceHide || | |
|
152 | 154 | (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) || |
|
153 | 155 | (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) { |
|
154 | 156 | labelItem->setVisible(false); |
|
155 | 157 | } else { |
|
156 | 158 | labelItem->setVisible(true); |
|
157 | 159 | width = boundingRect.width() + labelItem->pos().x(); |
|
158 | 160 | } |
|
159 | 161 | |
|
160 | 162 | //shades |
|
161 | 163 | if ((i + 1) % 2 && i > 1) { |
|
162 | 164 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); |
|
163 | 165 | qreal leftBound = qMax(layout[i - 1], gridRect.left()); |
|
164 | 166 | qreal rightBound = qMin(layout[i], gridRect.right()); |
|
165 | 167 | rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height()); |
|
166 | 168 | if (rectItem->rect().width() <= 0.0) |
|
167 | 169 | rectItem->setVisible(false); |
|
168 | 170 | else |
|
169 | 171 | rectItem->setVisible(true); |
|
170 | 172 | } |
|
171 | 173 | |
|
172 | 174 | // check if the grid line and the axis tick should be shown |
|
173 | 175 | qreal x = gridItem->line().p1().x(); |
|
174 | 176 | if (x < gridRect.left() || x > gridRect.right()) { |
|
175 | 177 | gridItem->setVisible(false); |
|
176 | 178 | tickItem->setVisible(false); |
|
177 | 179 | } else { |
|
178 | 180 | gridItem->setVisible(true); |
|
179 | 181 | tickItem->setVisible(true); |
|
180 | 182 | } |
|
181 | 183 | |
|
182 | 184 | } |
|
183 | 185 | |
|
184 | 186 | //begin/end grid line in case labels between |
|
185 | 187 | if (intervalAxis()) { |
|
186 | 188 | QGraphicsLineItem *gridLine; |
|
187 | 189 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); |
|
188 | 190 | gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom()); |
|
189 | 191 | gridLine->setVisible(true); |
|
190 | 192 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); |
|
191 | 193 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); |
|
192 | 194 | gridLine->setVisible(true); |
|
193 | 195 | } |
|
194 | 196 | } |
|
195 | 197 | |
|
196 | 198 | QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
197 | 199 | { |
|
198 | 200 | Q_UNUSED(constraint); |
|
199 | 201 | QSizeF sh(0,0); |
|
200 | 202 | |
|
201 | 203 | if (axis()->titleText().isEmpty() || !titleItem()->isVisible()) |
|
202 | 204 | return sh; |
|
203 | 205 | |
|
204 | 206 | switch (which) { |
|
205 | 207 | case Qt::MinimumSize: { |
|
206 | 208 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "..."); |
|
207 | 209 | sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0)); |
|
208 | 210 | break; |
|
209 | 211 | } |
|
210 | 212 | case Qt::MaximumSize: |
|
211 | 213 | case Qt::PreferredSize: { |
|
212 | 214 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText()); |
|
213 | 215 | sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0)); |
|
214 | 216 | break; |
|
215 | 217 | } |
|
216 | 218 | default: |
|
217 | 219 | break; |
|
218 | 220 | } |
|
219 | 221 | |
|
220 | 222 | return sh; |
|
221 | 223 | } |
|
222 | 224 | |
|
223 | 225 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now