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