@@ -1,211 +1,210 | |||||
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, ChartPresenter *presenter, bool intervalAxis) |
|
29 | HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) | |
30 | : ChartAxis(axis, presenter, intervalAxis) |
|
30 | : ChartAxis(axis, presenter, 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 | |||
|
71 | ||||
|
72 | if (!titleText().isNull()) { | |||
|
73 | QFontMetrics fn(title->font()); | |||
|
74 | ||||
|
75 | int size(0); | |||
|
76 | ||||
|
77 | size = gridRect.width(); | |||
|
78 | QString titleText = this->titleText(); | |||
|
79 | ||||
|
80 | if (fn.boundingRect(titleText).width() > size) { | |||
|
81 | QString string = titleText + "..."; | |||
|
82 | while (fn.boundingRect(string).width() > size && string.length() > 3) | |||
|
83 | string.remove(string.length() - 4, 1); | |||
|
84 | title->setText(string); | |||
|
85 | } else { | |||
|
86 | title->setText(titleText); | |||
|
87 | } | |||
|
88 | ||||
|
89 | QPointF center = gridRect.center() - title->boundingRect().center(); | |||
|
90 | if (alignment() == Qt::AlignTop) { | |||
|
91 | title->setPos(center.x(), axisRect.top()); | |||
|
92 | } else if (alignment() == Qt::AlignBottom) { | |||
|
93 | title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height()); | |||
|
94 | } | |||
|
95 | } | |||
|
96 | ||||
70 | for (int i = 0; i < layout.size(); ++i) { |
|
97 | for (int i = 0; i < layout.size(); ++i) { | |
71 |
|
98 | |||
72 | //items |
|
99 | //items | |
73 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
100 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
74 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1)); |
|
101 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1)); | |
75 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i)); |
|
102 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i)); | |
76 |
|
103 | |||
77 | //grid line |
|
104 | //grid line | |
78 | gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); |
|
105 | gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); | |
79 |
|
106 | |||
80 | //label text wrapping |
|
107 | //label text wrapping | |
81 | if(intervalAxis()&& i+1!=layout.size()) { |
|
108 | if(intervalAxis()&& i+1!=layout.size()) { | |
82 | //wrapping in case of interval axis |
|
109 | //wrapping in case of interval axis | |
83 | const qreal delta = layout[i+1] - layout[i]; |
|
110 | const qreal delta = layout[i+1] - layout[i]; | |
84 | QString text = labelList.at(i); |
|
111 | QString text = labelList.at(i); | |
85 | if (fn.boundingRect(text).width() + 1 > delta ) |
|
112 | if (fn.boundingRect(text).width() + 1 > delta ) | |
86 | { |
|
113 | { | |
87 | QString label = text + "..."; |
|
114 | QString label = text + "..."; | |
88 | while (fn.boundingRect(label).width() >= delta && label.length() > 3) |
|
115 | while (fn.boundingRect(label).width() >= delta && label.length() > 3) | |
89 | label.remove(label.length() - 4, 1); |
|
116 | label.remove(label.length() - 4, 1); | |
90 | labelItem->setText(label); |
|
117 | labelItem->setText(label); | |
91 | } |
|
118 | } | |
92 | else { |
|
119 | else { | |
93 | labelItem->setText(text); |
|
120 | labelItem->setText(text); | |
94 | } |
|
121 | } | |
95 | }else{ |
|
122 | }else{ | |
96 | labelItem->setText(labelList.at(i)); |
|
123 | labelItem->setText(labelList.at(i)); | |
97 | } |
|
124 | } | |
98 |
|
125 | |||
99 | //label transformation origin point |
|
126 | //label transformation origin point | |
100 | const QRectF& rect = labelItem->boundingRect(); |
|
127 | const QRectF& rect = labelItem->boundingRect(); | |
101 | QPointF center = rect.center(); |
|
128 | QPointF center = rect.center(); | |
102 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
129 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
103 |
|
130 | |||
104 | //ticks and label position |
|
131 | //ticks and label position | |
105 | if (alignment() == Qt::AlignTop) { |
|
132 | if (alignment() == Qt::AlignTop) { | |
106 | labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding()); |
|
133 | labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding()); | |
107 | tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding()); |
|
134 | tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding()); | |
108 | } else if (alignment() == Qt::AlignBottom) { |
|
135 | } else if (alignment() == Qt::AlignBottom) { | |
109 | labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding()); |
|
136 | labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding()); | |
110 | tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding()); |
|
137 | tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding()); | |
111 | } |
|
138 | } | |
112 |
|
139 | |||
113 | //label in beetwen |
|
140 | //label in beetwen | |
114 | if(intervalAxis()&& i+1!=layout.size()) { |
|
141 | if(intervalAxis()&& i+1!=layout.size()) { | |
115 | const qreal delta = (layout[i+1] - layout[i])/2; |
|
142 | const qreal delta = (layout[i+1] - layout[i])/2; | |
116 | labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y()); |
|
143 | labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y()); | |
117 | } |
|
144 | } | |
118 |
|
145 | |||
119 | //label overlap detection |
|
146 | //label overlap detection | |
120 | if(labelItem->pos().x() < width || |
|
147 | if(labelItem->pos().x() < width || | |
121 | labelItem->pos().x() < axisRect.left() || |
|
148 | labelItem->pos().x() < axisRect.left() || | |
122 | labelItem->pos().x() + rect.width() > axisRect.right()) { |
|
149 | labelItem->pos().x() + rect.width() > axisRect.right()) { | |
123 | labelItem->setVisible(false); |
|
150 | labelItem->setVisible(false); | |
124 | } else { |
|
151 | } else { | |
125 | labelItem->setVisible(true); |
|
152 | labelItem->setVisible(true); | |
126 | width=rect.width()+labelItem->pos().x(); |
|
153 | width=rect.width()+labelItem->pos().x(); | |
127 | } |
|
154 | } | |
128 |
|
155 | |||
129 | //shades |
|
156 | //shades | |
130 | if ((i + 1) % 2 && i > 1) { |
|
157 | if ((i + 1) % 2 && i > 1) { | |
131 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); |
|
158 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); | |
132 | rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height()); |
|
159 | rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height()); | |
133 | } |
|
160 | } | |
134 |
|
161 | |||
135 | // check if the grid line and the axis tick should be shown |
|
162 | // check if the grid line and the axis tick should be shown | |
136 | qreal x = gridItem->line().p1().x(); |
|
163 | qreal x = gridItem->line().p1().x(); | |
137 | if (x < gridRect.left() || x > gridRect.right()) { |
|
164 | if (x < gridRect.left() || x > gridRect.right()) { | |
138 | gridItem->setVisible(false); |
|
165 | gridItem->setVisible(false); | |
139 | tickItem->setVisible(false); |
|
166 | tickItem->setVisible(false); | |
140 | }else{ |
|
167 | }else{ | |
141 | gridItem->setVisible(true); |
|
168 | gridItem->setVisible(true); | |
142 | tickItem->setVisible(true); |
|
169 | tickItem->setVisible(true); | |
143 | } |
|
170 | } | |
144 |
|
171 | |||
145 | } |
|
172 | } | |
146 |
|
173 | |||
147 | //begin/end grid line in case labels between |
|
174 | //begin/end grid line in case labels between | |
148 | if (intervalAxis()) { |
|
175 | if (intervalAxis()) { | |
149 | QGraphicsLineItem *gridLine; |
|
176 | QGraphicsLineItem *gridLine; | |
150 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); |
|
177 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); | |
151 | gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom()); |
|
178 | gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom()); | |
152 | gridLine->setVisible(true); |
|
179 | gridLine->setVisible(true); | |
153 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); |
|
180 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); | |
154 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); |
|
181 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); | |
155 | gridLine->setVisible(true); |
|
182 | gridLine->setVisible(true); | |
156 | } |
|
183 | } | |
157 |
|
||||
158 | //title |
|
|||
159 |
|
||||
160 | if (!titleText().isNull()) { |
|
|||
161 | QFontMetrics fn(title->font()); |
|
|||
162 |
|
||||
163 | int size(0); |
|
|||
164 |
|
||||
165 | size = gridRect.width(); |
|
|||
166 | QString titleText = this->titleText(); |
|
|||
167 |
|
||||
168 | if (fn.boundingRect(titleText).width() > size) { |
|
|||
169 | QString string = titleText + "..."; |
|
|||
170 | while (fn.boundingRect(string).width() > size && string.length() > 3) |
|
|||
171 | string.remove(string.length() - 4, 1); |
|
|||
172 | title->setText(string); |
|
|||
173 | } else { |
|
|||
174 | title->setText(titleText); |
|
|||
175 | } |
|
|||
176 |
|
||||
177 | QPointF center = gridRect.center() - title->boundingRect().center(); |
|
|||
178 | if (alignment() == Qt::AlignTop) { |
|
|||
179 | title->setPos(center.x(), axisRect.top()); |
|
|||
180 | } else if (alignment() == Qt::AlignBottom) { |
|
|||
181 | title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height()); |
|
|||
182 | } |
|
|||
183 | } |
|
|||
184 |
|
||||
185 | } |
|
184 | } | |
186 |
|
185 | |||
187 | QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
186 | QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
188 | { |
|
187 | { | |
189 | Q_UNUSED(constraint); |
|
188 | Q_UNUSED(constraint); | |
190 | QFontMetrics fn(titleFont()); |
|
189 | QFontMetrics fn(titleFont()); | |
191 | QSizeF sh; |
|
190 | QSizeF sh; | |
192 |
|
191 | |||
193 | if (titleText().isNull()) |
|
192 | if (titleText().isNull()) | |
194 | return sh; |
|
193 | return sh; | |
195 |
|
194 | |||
196 | switch (which) { |
|
195 | switch (which) { | |
197 | case Qt::MinimumSize: |
|
196 | case Qt::MinimumSize: | |
198 | sh = QSizeF(fn.boundingRect("...").width(), fn.height()); |
|
197 | sh = QSizeF(fn.boundingRect("...").width(), fn.height()); | |
199 | break; |
|
198 | break; | |
200 | case Qt::MaximumSize: |
|
199 | case Qt::MaximumSize: | |
201 | case Qt::PreferredSize: |
|
200 | case Qt::PreferredSize: | |
202 | sh = QSizeF(fn.boundingRect(axis()->title()).width(), fn.height()); |
|
201 | sh = QSizeF(fn.boundingRect(axis()->title()).width(), fn.height()); | |
203 | break; |
|
202 | break; | |
204 | default: |
|
203 | default: | |
205 | break; |
|
204 | break; | |
206 | } |
|
205 | } | |
207 |
|
206 | |||
208 | return sh; |
|
207 | return sh; | |
209 | } |
|
208 | } | |
210 |
|
209 | |||
211 | QTCOMMERCIALCHART_END_NAMESPACE |
|
210 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,209 +1,212 | |||||
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, ChartPresenter *presenter, bool intervalAxis) |
|
28 | VerticalAxis::VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) | |
29 | : ChartAxis(axis, presenter, intervalAxis) |
|
29 | : ChartAxis(axis, presenter, 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 | |||
|
75 | ||||
|
76 | if (!titleText().isNull()) { | |||
|
77 | QFontMetrics fn(title->font()); | |||
|
78 | ||||
|
79 | int size(0); | |||
|
80 | size = gridRect.height(); | |||
|
81 | QString titleText = this->titleText(); | |||
|
82 | ||||
|
83 | if (fn.boundingRect(titleText).width() > size) { | |||
|
84 | QString string = titleText + "..."; | |||
|
85 | while (fn.boundingRect(string).width() > size && string.length() > 3) | |||
|
86 | string.remove(string.length() - 4, 1); | |||
|
87 | title->setText(string); | |||
|
88 | } | |||
|
89 | else { | |||
|
90 | title->setText(titleText); | |||
|
91 | } | |||
|
92 | ||||
|
93 | QPointF center = gridRect.center() - title->boundingRect().center(); | |||
|
94 | if (alignment() == Qt::AlignLeft) { | |||
|
95 | title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y()); | |||
|
96 | } | |||
|
97 | else if (alignment() == Qt::AlignRight) { | |||
|
98 | title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y()); | |||
|
99 | } | |||
|
100 | title->setTransformOriginPoint(title->boundingRect().center()); | |||
|
101 | title->setRotation(270); | |||
|
102 | } | |||
|
103 | ||||
74 | for (int i = 0; i < layout.size(); ++i) { |
|
104 | for (int i = 0; i < layout.size(); ++i) { | |
75 |
|
105 | |||
76 | //items |
|
106 | //items | |
77 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i)); |
|
107 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i)); | |
78 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1)); |
|
108 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1)); | |
79 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i)); |
|
109 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i)); | |
80 |
|
110 | |||
81 | //grid line |
|
111 | //grid line | |
82 | gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]); |
|
112 | gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]); | |
83 |
|
113 | |||
84 | //label text wrapping |
|
114 | //label text wrapping | |
85 | QString text = labelList.at(i); |
|
115 | QString text = labelList.at(i); | |
86 |
|
|
116 | qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height(); | |
|
117 | if (fn.boundingRect(text).width() > size) { | |||
87 | QString label = text + "..."; |
|
118 | QString label = text + "..."; | |
88 |
while (fn.boundingRect(label).width() > |
|
119 | while (fn.boundingRect(label).width() > size && label.length() > 3) | |
89 | label.remove(label.length() - 4, 1); |
|
120 | label.remove(label.length() - 4, 1); | |
90 | labelItem->setText(label); |
|
121 | labelItem->setText(label); | |
91 | } else { |
|
122 | } else { | |
92 | labelItem->setText(text); |
|
123 | labelItem->setText(text); | |
93 | } |
|
124 | } | |
94 | //label transformation origin point |
|
125 | //label transformation origin point | |
95 | const QRectF &rect = labelItem->boundingRect(); |
|
126 | const QRectF &rect = labelItem->boundingRect(); | |
96 |
|
127 | |||
97 | QPointF center = rect.center(); |
|
128 | QPointF center = rect.center(); | |
98 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
129 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
99 |
|
130 | |||
100 | //ticks and label position |
|
131 | //ticks and label position | |
101 | if (alignment() == Qt::AlignLeft) { |
|
132 | if (alignment() == Qt::AlignLeft) { | |
102 | labelItem->setPos(axisRect.right() - rect.width() - labelPadding() , layout[i] - center.y()); |
|
133 | labelItem->setPos(axisRect.right() - rect.width() - labelPadding() , layout[i] - center.y()); | |
103 | tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]); |
|
134 | tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]); | |
104 | } else if (alignment() == Qt::AlignRight) { |
|
135 | } else if (alignment() == Qt::AlignRight) { | |
105 | labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y()); |
|
136 | labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y()); | |
106 | tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]); |
|
137 | tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]); | |
107 | } |
|
138 | } | |
108 |
|
139 | |||
109 | //label in beetwen |
|
140 | //label in beetwen | |
110 | if(intervalAxis()&& i+1!=layout.size()) { |
|
141 | if(intervalAxis()&& i+1!=layout.size()) { | |
111 | const qreal delta = (layout[i+1] - layout[i])/2; |
|
142 | const qreal delta = (layout[i+1] - layout[i])/2; | |
112 | labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y()); |
|
143 | labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y()); | |
113 | } |
|
144 | } | |
114 |
|
145 | |||
115 | //label overlap detection |
|
146 | //label overlap detection | |
116 | if(labelItem->pos().y() + rect.height() > height || |
|
147 | if(labelItem->pos().y() + rect.height() > height || | |
117 | labelItem->pos().y() + rect.height()/2 > gridRect.bottom() || |
|
148 | labelItem->pos().y() + rect.height()/2 > gridRect.bottom() || | |
118 | labelItem->pos().y() + rect.height()/2 < gridRect.top()) { |
|
149 | labelItem->pos().y() + rect.height()/2 < gridRect.top()) { | |
119 | labelItem->setVisible(false); |
|
150 | labelItem->setVisible(false); | |
120 | } |
|
151 | } | |
121 | else { |
|
152 | else { | |
122 | labelItem->setVisible(true); |
|
153 | labelItem->setVisible(true); | |
123 | height=labelItem->pos().y(); |
|
154 | height=labelItem->pos().y(); | |
124 | } |
|
155 | } | |
125 |
|
156 | |||
126 | //shades |
|
157 | //shades | |
127 | if ((i + 1) % 2 && i > 1) { |
|
158 | if ((i + 1) % 2 && i > 1) { | |
128 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); |
|
159 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); | |
129 | rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]); |
|
160 | rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]); | |
130 | } |
|
161 | } | |
131 |
|
162 | |||
132 | // check if the grid line and the axis tick should be shown |
|
163 | // check if the grid line and the axis tick should be shown | |
133 | qreal y = gridItem->line().p1().y(); |
|
164 | qreal y = gridItem->line().p1().y(); | |
134 | if ((y < gridRect.top() || y > gridRect.bottom())) |
|
165 | if ((y < gridRect.top() || y > gridRect.bottom())) | |
135 | { |
|
166 | { | |
136 | gridItem->setVisible(false); |
|
167 | gridItem->setVisible(false); | |
137 | tickItem->setVisible(false); |
|
168 | tickItem->setVisible(false); | |
138 | }else{ |
|
169 | }else{ | |
139 | gridItem->setVisible(true); |
|
170 | gridItem->setVisible(true); | |
140 | tickItem->setVisible(true); |
|
171 | tickItem->setVisible(true); | |
141 | } |
|
172 | } | |
142 |
|
173 | |||
143 | } |
|
174 | } | |
144 | //begin/end grid line in case labels between |
|
175 | //begin/end grid line in case labels between | |
145 | if (intervalAxis()) { |
|
176 | if (intervalAxis()) { | |
146 | QGraphicsLineItem *gridLine; |
|
177 | QGraphicsLineItem *gridLine; | |
147 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); |
|
178 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); | |
148 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top()); |
|
179 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top()); | |
149 | gridLine->setVisible(true); |
|
180 | gridLine->setVisible(true); | |
150 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); |
|
181 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); | |
151 | gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom()); |
|
182 | gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom()); | |
152 | gridLine->setVisible(true); |
|
183 | gridLine->setVisible(true); | |
153 | } |
|
184 | } | |
154 |
|
||||
155 | //title |
|
|||
156 |
|
||||
157 | if (!titleText().isNull()) { |
|
|||
158 | QFontMetrics fn(title->font()); |
|
|||
159 |
|
||||
160 | int size(0); |
|
|||
161 | size = gridRect.height(); |
|
|||
162 | QString titleText = this->titleText(); |
|
|||
163 |
|
||||
164 | if (fn.boundingRect(titleText).width() > size) { |
|
|||
165 | QString string = titleText + "..."; |
|
|||
166 | while (fn.boundingRect(string).width() > size && string.length() > 3) |
|
|||
167 | string.remove(string.length() - 4, 1); |
|
|||
168 | title->setText(string); |
|
|||
169 | } else { |
|
|||
170 | title->setText(titleText); |
|
|||
171 | } |
|
|||
172 |
|
||||
173 | QPointF center = gridRect.center() - title->boundingRect().center(); |
|
|||
174 | if (alignment() == Qt::AlignLeft) { |
|
|||
175 | title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y()); |
|
|||
176 | }else if (alignment() == Qt::AlignRight) { |
|
|||
177 | title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y()); |
|
|||
178 | } |
|
|||
179 | title->setTransformOriginPoint(title->boundingRect().center()); |
|
|||
180 | title->setRotation(270); |
|
|||
181 | } |
|
|||
182 | } |
|
185 | } | |
183 |
|
186 | |||
184 | QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
187 | QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
185 | { |
|
188 | { | |
186 |
|
189 | |||
187 | Q_UNUSED(constraint); |
|
190 | Q_UNUSED(constraint); | |
188 | QFontMetrics fn(titleFont()); |
|
191 | QFontMetrics fn(titleFont()); | |
189 | QSizeF sh; |
|
192 | QSizeF sh; | |
190 |
|
193 | |||
191 | if (titleText().isNull()) |
|
194 | if (titleText().isNull()) | |
192 | return sh; |
|
195 | return sh; | |
193 |
|
196 | |||
194 | switch (which) { |
|
197 | switch (which) { | |
195 | case Qt::MinimumSize: |
|
198 | case Qt::MinimumSize: | |
196 | sh = QSizeF(fn.height(), fn.boundingRect("...").width()); |
|
199 | sh = QSizeF(fn.height(), fn.boundingRect("...").width()); | |
197 | break; |
|
200 | break; | |
198 | case Qt::MaximumSize: |
|
201 | case Qt::MaximumSize: | |
199 | case Qt::PreferredSize: |
|
202 | case Qt::PreferredSize: | |
200 | sh = QSizeF(fn.height(), fn.boundingRect(axis()->title()).width()); |
|
203 | sh = QSizeF(fn.height(), fn.boundingRect(axis()->title()).width()); | |
201 | break; |
|
204 | break; | |
202 | default: |
|
205 | default: | |
203 | break; |
|
206 | break; | |
204 | } |
|
207 | } | |
205 |
|
208 | |||
206 | return sh; |
|
209 | return sh; | |
207 | } |
|
210 | } | |
208 |
|
211 | |||
209 | QTCOMMERCIALCHART_END_NAMESPACE |
|
212 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,351 +1,351 | |||||
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 "chartlayout_p.h" |
|
21 | #include "chartlayout_p.h" | |
22 | #include "chartpresenter_p.h" |
|
22 | #include "chartpresenter_p.h" | |
23 | #include "qlegend_p.h" |
|
23 | #include "qlegend_p.h" | |
24 | #include "chartaxis_p.h" |
|
24 | #include "chartaxis_p.h" | |
25 | #include "charttitle_p.h" |
|
25 | #include "charttitle_p.h" | |
26 | #include "chartbackground_p.h" |
|
26 | #include "chartbackground_p.h" | |
27 | #include "legendmarker_p.h" |
|
27 | #include "legendmarker_p.h" | |
28 | #include <QDebug> |
|
28 | #include <QDebug> | |
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
31 |
|
31 | |||
32 |
static const qreal golden_ratio = 0. |
|
32 | static const qreal golden_ratio = 0.4; | |
33 |
|
33 | |||
34 | ChartLayout::ChartLayout(ChartPresenter *presenter) |
|
34 | ChartLayout::ChartLayout(ChartPresenter *presenter) | |
35 | : m_presenter(presenter), |
|
35 | : m_presenter(presenter), | |
36 | m_margins(20, 20, 20, 20), |
|
36 | m_margins(20, 20, 20, 20), | |
37 | m_minChartRect(0, 0, 200, 200) |
|
37 | m_minChartRect(0, 0, 200, 200) | |
38 | { |
|
38 | { | |
39 |
|
39 | |||
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | ChartLayout::~ChartLayout() |
|
42 | ChartLayout::~ChartLayout() | |
43 | { |
|
43 | { | |
44 |
|
44 | |||
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | void ChartLayout::setGeometry(const QRectF &rect) |
|
47 | void ChartLayout::setGeometry(const QRectF &rect) | |
48 | { |
|
48 | { | |
49 | if (!rect.isValid()) |
|
49 | if (!rect.isValid()) | |
50 | return; |
|
50 | return; | |
51 |
|
51 | |||
52 | QList<ChartAxis *> axes = m_presenter->axisItems(); |
|
52 | QList<ChartAxis *> axes = m_presenter->axisItems(); | |
53 | QList<ChartElement *> charts = m_presenter->chartItems(); |
|
53 | QList<ChartElement *> charts = m_presenter->chartItems(); | |
54 | ChartTitle *title = m_presenter->titleElement(); |
|
54 | ChartTitle *title = m_presenter->titleElement(); | |
55 | QLegend *legend = m_presenter->legend(); |
|
55 | QLegend *legend = m_presenter->legend(); | |
56 | ChartBackground *background = m_presenter->backgroundElement(); |
|
56 | ChartBackground *background = m_presenter->backgroundElement(); | |
57 |
|
57 | |||
58 | QRectF contentGeometry = calculateBackgroundGeometry(rect, background); |
|
58 | QRectF contentGeometry = calculateBackgroundGeometry(rect, background); | |
59 |
|
59 | |||
60 | contentGeometry = calculateContentGeometry(contentGeometry); |
|
60 | contentGeometry = calculateContentGeometry(contentGeometry); | |
61 |
|
61 | |||
62 | if (title && title->isVisible()) |
|
62 | if (title && title->isVisible()) | |
63 | contentGeometry = calculateTitleGeometry(contentGeometry, title); |
|
63 | contentGeometry = calculateTitleGeometry(contentGeometry, title); | |
64 |
|
64 | |||
65 | if (legend->isAttachedToChart() && legend->isVisible()) |
|
65 | if (legend->isAttachedToChart() && legend->isVisible()) | |
66 | contentGeometry = calculateLegendGeometry(contentGeometry, legend); |
|
66 | contentGeometry = calculateLegendGeometry(contentGeometry, legend); | |
67 |
|
67 | |||
68 | contentGeometry = calculateAxisGeometry(contentGeometry, axes); |
|
68 | contentGeometry = calculateAxisGeometry(contentGeometry, axes); | |
69 |
|
69 | |||
70 | m_chartsRect = calculateChartsGeometry(contentGeometry, charts); |
|
70 | m_chartsRect = calculateChartsGeometry(contentGeometry, charts); | |
71 |
|
71 | |||
72 | QGraphicsLayout::setGeometry(rect); |
|
72 | QGraphicsLayout::setGeometry(rect); | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | QRectF ChartLayout::calculateContentGeometry(const QRectF &geometry) const |
|
75 | QRectF ChartLayout::calculateContentGeometry(const QRectF &geometry) const | |
76 | { |
|
76 | { | |
77 | return geometry.adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()); |
|
77 | return geometry.adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()); | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | QRectF ChartLayout::calculateContentMinimum(const QRectF &minimum) const |
|
80 | QRectF ChartLayout::calculateContentMinimum(const QRectF &minimum) const | |
81 | { |
|
81 | { | |
82 | return minimum.adjusted(0, 0, m_margins.left() + m_margins.right(), m_margins.top() + m_margins.bottom()); |
|
82 | return minimum.adjusted(0, 0, m_margins.left() + m_margins.right(), m_margins.top() + m_margins.bottom()); | |
83 | } |
|
83 | } | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | QRectF ChartLayout::calculateBackgroundGeometry(const QRectF &geometry, ChartBackground *background) const |
|
86 | QRectF ChartLayout::calculateBackgroundGeometry(const QRectF &geometry, ChartBackground *background) const | |
87 | { |
|
87 | { | |
88 | qreal left, top, right, bottom; |
|
88 | qreal left, top, right, bottom; | |
89 | getContentsMargins(&left, &top, &right, &bottom); |
|
89 | getContentsMargins(&left, &top, &right, &bottom); | |
90 | QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom); |
|
90 | QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom); | |
91 | if (background) |
|
91 | if (background) | |
92 | background->setRect(backgroundGeometry); |
|
92 | background->setRect(backgroundGeometry); | |
93 | return backgroundGeometry; |
|
93 | return backgroundGeometry; | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | QRectF ChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const |
|
96 | QRectF ChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const | |
97 | { |
|
97 | { | |
98 | qreal left, top, right, bottom; |
|
98 | qreal left, top, right, bottom; | |
99 | getContentsMargins(&left, &top, &right, &bottom); |
|
99 | getContentsMargins(&left, &top, &right, &bottom); | |
100 | return minimum.adjusted(0, 0, left + right, top + bottom); |
|
100 | return minimum.adjusted(0, 0, left + right, top + bottom); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList<ChartAxis *>& axes) const |
|
104 | QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList<ChartAxis *>& axes) const | |
105 | { |
|
105 | { | |
106 | QSizeF left(0,0); |
|
106 | QSizeF left(0,0); | |
107 | QSizeF minLeft(0,0); |
|
107 | QSizeF minLeft(0,0); | |
108 | QSizeF right(0,0); |
|
108 | QSizeF right(0,0); | |
109 | QSizeF minRight(0,0); |
|
109 | QSizeF minRight(0,0); | |
110 | QSizeF bottom(0,0); |
|
110 | QSizeF bottom(0,0); | |
111 | QSizeF minBottom(0,0); |
|
111 | QSizeF minBottom(0,0); | |
112 | QSizeF top(0,0); |
|
112 | QSizeF top(0,0); | |
113 | QSizeF minTop(0,0); |
|
113 | QSizeF minTop(0,0); | |
114 | int leftCount = 0; |
|
114 | int leftCount = 0; | |
115 | int rightCount = 0; |
|
115 | int rightCount = 0; | |
116 | int topCount = 0; |
|
116 | int topCount = 0; | |
117 | int bottomCount = 0; |
|
117 | int bottomCount = 0; | |
118 |
|
118 | |||
119 | foreach (ChartAxis *axis , axes) { |
|
119 | foreach (ChartAxis *axis , axes) { | |
120 |
|
120 | |||
121 | if (!axis->isVisible()) |
|
121 | if (!axis->isVisible()) | |
122 | continue; |
|
122 | continue; | |
123 |
|
123 | |||
124 | QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize); |
|
124 | QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize); | |
125 | //this is used to get single thick font size |
|
125 | //this is used to get single thick font size | |
126 | QSizeF minSize = axis->effectiveSizeHint(Qt::MinimumSize); |
|
126 | QSizeF minSize = axis->effectiveSizeHint(Qt::MinimumSize); | |
127 |
|
127 | |||
128 | switch (axis->alignment()) { |
|
128 | switch (axis->alignment()) { | |
129 | case Qt::AlignLeft: |
|
129 | case Qt::AlignLeft: | |
130 | left.setWidth(left.width()+size.width()); |
|
130 | left.setWidth(left.width()+size.width()); | |
131 | left.setHeight(qMax(left.height(),size.height())); |
|
131 | left.setHeight(qMax(left.height(),size.height())); | |
132 | minLeft.setWidth(minLeft.width()+minSize.width()); |
|
132 | minLeft.setWidth(minLeft.width()+minSize.width()); | |
133 | minLeft.setHeight(qMax(minLeft.height(),minSize.height())); |
|
133 | minLeft.setHeight(qMax(minLeft.height(),minSize.height())); | |
134 | leftCount++; |
|
134 | leftCount++; | |
135 | break; |
|
135 | break; | |
136 | case Qt::AlignRight: |
|
136 | case Qt::AlignRight: | |
137 | right.setWidth(right.width()+size.width()); |
|
137 | right.setWidth(right.width()+size.width()); | |
138 | right.setHeight(qMax(right.height(),size.height())); |
|
138 | right.setHeight(qMax(right.height(),size.height())); | |
139 | minRight.setWidth(minRight.width()+minSize.width()); |
|
139 | minRight.setWidth(minRight.width()+minSize.width()); | |
140 | minRight.setHeight(qMax(minRight.height(),minSize.height())); |
|
140 | minRight.setHeight(qMax(minRight.height(),minSize.height())); | |
141 | rightCount++; |
|
141 | rightCount++; | |
142 | break; |
|
142 | break; | |
143 | case Qt::AlignTop: |
|
143 | case Qt::AlignTop: | |
144 | top.setWidth(qMax(top.width(),size.width())); |
|
144 | top.setWidth(qMax(top.width(),size.width())); | |
145 | top.setHeight(top.height()+size.height()); |
|
145 | top.setHeight(top.height()+size.height()); | |
146 | minTop.setWidth(qMax(minTop.width(),minSize.width())); |
|
146 | minTop.setWidth(qMax(minTop.width(),minSize.width())); | |
147 | minTop.setHeight(minTop.height()+minSize.height()); |
|
147 | minTop.setHeight(minTop.height()+minSize.height()); | |
148 | topCount++; |
|
148 | topCount++; | |
149 | break; |
|
149 | break; | |
150 | case Qt::AlignBottom: |
|
150 | case Qt::AlignBottom: | |
151 | bottom.setWidth(qMax(bottom.width(), size.width())); |
|
151 | bottom.setWidth(qMax(bottom.width(), size.width())); | |
152 | bottom.setHeight(bottom.height() + size.height()); |
|
152 | bottom.setHeight(bottom.height() + size.height()); | |
153 | minBottom.setWidth(qMax(minBottom.width(),minSize.width())); |
|
153 | minBottom.setWidth(qMax(minBottom.width(),minSize.width())); | |
154 | minBottom.setHeight(minBottom.height() + minSize.height()); |
|
154 | minBottom.setHeight(minBottom.height() + minSize.height()); | |
155 | bottomCount++; |
|
155 | bottomCount++; | |
156 | break; |
|
156 | break; | |
157 | } |
|
157 | } | |
158 | } |
|
158 | } | |
159 |
|
159 | |||
160 | int horizontal = leftCount + rightCount; |
|
160 | int horizontal = leftCount + rightCount; | |
161 | qreal hratio = 0 ; |
|
161 | qreal hratio = 0 ; | |
162 | if(horizontal>0) |
|
162 | if(horizontal>0) | |
163 | hratio = (golden_ratio*geometry.width())/horizontal; |
|
163 | hratio = (golden_ratio*geometry.width())/horizontal; | |
164 |
|
164 | |||
165 | if(leftCount>0) |
|
165 | if(leftCount>0) | |
166 | left.setWidth(qMin(left.width(),hratio*leftCount)); |
|
166 | left.setWidth(qMin(left.width(),hratio*leftCount)); | |
167 | if(rightCount>0) |
|
167 | if(rightCount>0) | |
168 | right.setWidth(qMin(right.width(),hratio*rightCount)); |
|
168 | right.setWidth(qMin(right.width(),hratio*rightCount)); | |
169 |
|
169 | |||
170 | qreal minHeight = qMax(minLeft.height(),minRight.height()); |
|
170 | qreal minHeight = qMax(minLeft.height(),minRight.height()); | |
171 | qreal minWidth = qMax(minTop.width(),minBottom.width()); |
|
171 | qreal minWidth = qMax(minTop.width(),minBottom.width()); | |
172 |
|
172 | |||
173 | QRectF chartRect = geometry.adjusted(qMax(left.width(),minWidth/2), qMax(top.height(), minHeight/2),-qMax(right.width(),minWidth/2),-qMax(bottom.height(),minHeight/2)); |
|
173 | QRectF chartRect = geometry.adjusted(qMax(left.width(),minWidth/2), qMax(top.height(), minHeight/2),-qMax(right.width(),minWidth/2),-qMax(bottom.height(),minHeight/2)); | |
174 |
|
174 | |||
175 | qreal leftOffset = 0; |
|
175 | qreal leftOffset = 0; | |
176 | qreal rightOffset = 0; |
|
176 | qreal rightOffset = 0; | |
177 | qreal topOffset = 0; |
|
177 | qreal topOffset = 0; | |
178 | qreal bottomOffset = 0; |
|
178 | qreal bottomOffset = 0; | |
179 |
|
179 | |||
180 | foreach(ChartAxis* axis , axes) { |
|
180 | foreach(ChartAxis* axis , axes) { | |
181 |
|
181 | |||
182 | if (!axis->isVisible()) |
|
182 | if (!axis->isVisible()) | |
183 | continue; |
|
183 | continue; | |
184 |
|
184 | |||
185 | QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize); |
|
185 | QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize); | |
186 |
|
186 | |||
187 | switch(axis->alignment()){ |
|
187 | switch(axis->alignment()){ | |
188 | case Qt::AlignLeft:{ |
|
188 | case Qt::AlignLeft:{ | |
189 | qreal width = qMin(size.width(),(left.width()/leftCount)); |
|
189 | qreal width = qMin(size.width(),(left.width()/leftCount)); | |
190 | leftOffset+=width; |
|
190 | leftOffset+=width; | |
191 | axis->setGeometry(QRect(chartRect.left()-leftOffset, geometry.top(),width, geometry.bottom()),chartRect); |
|
191 | axis->setGeometry(QRect(chartRect.left()-leftOffset, geometry.top(),width, geometry.bottom()),chartRect); | |
192 | break; |
|
192 | break; | |
193 | } |
|
193 | } | |
194 | case Qt::AlignRight:{ |
|
194 | case Qt::AlignRight:{ | |
195 | qreal width = qMin(size.width(),(right.width()/rightCount)); |
|
195 | qreal width = qMin(size.width(),(right.width()/rightCount)); | |
196 | axis->setGeometry(QRect(chartRect.right()+rightOffset,geometry.top(),width,geometry.bottom()),chartRect); |
|
196 | axis->setGeometry(QRect(chartRect.right()+rightOffset,geometry.top(),width,geometry.bottom()),chartRect); | |
197 | rightOffset+=width; |
|
197 | rightOffset+=width; | |
198 | break; |
|
198 | break; | |
199 | } |
|
199 | } | |
200 | case Qt::AlignTop: |
|
200 | case Qt::AlignTop: | |
201 | axis->setGeometry(QRect(geometry.left(), chartRect.top() - topOffset - size.height(), geometry.width(), size.height()), chartRect); |
|
201 | axis->setGeometry(QRect(geometry.left(), chartRect.top() - topOffset - size.height(), geometry.width(), size.height()), chartRect); | |
202 | topOffset += size.height(); |
|
202 | topOffset += size.height(); | |
203 | break; |
|
203 | break; | |
204 | case Qt::AlignBottom: |
|
204 | case Qt::AlignBottom: | |
205 | axis->setGeometry(QRect(geometry.left(), chartRect.bottom() + bottomOffset, geometry.width(), size.height()), chartRect); |
|
205 | axis->setGeometry(QRect(geometry.left(), chartRect.bottom() + bottomOffset, geometry.width(), size.height()), chartRect); | |
206 | bottomOffset += size.height(); |
|
206 | bottomOffset += size.height(); | |
207 | break; |
|
207 | break; | |
208 | } |
|
208 | } | |
209 | } |
|
209 | } | |
210 |
|
210 | |||
211 | return chartRect; |
|
211 | return chartRect; | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 | QRectF ChartLayout::calculateAxisMinimum(const QRectF &minimum, const QList<ChartAxis *>& axes) const |
|
214 | QRectF ChartLayout::calculateAxisMinimum(const QRectF &minimum, const QList<ChartAxis *>& axes) const | |
215 | { |
|
215 | { | |
216 | QSizeF left; |
|
216 | QSizeF left; | |
217 | QSizeF right; |
|
217 | QSizeF right; | |
218 | QSizeF bottom; |
|
218 | QSizeF bottom; | |
219 | QSizeF top; |
|
219 | QSizeF top; | |
220 |
|
220 | |||
221 | foreach (ChartAxis *axis , axes) { |
|
221 | foreach (ChartAxis *axis , axes) { | |
222 |
|
222 | |||
223 | QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize); |
|
223 | QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize); | |
224 |
|
224 | |||
225 | if (!axis->isVisible()) |
|
225 | if (!axis->isVisible()) | |
226 | continue; |
|
226 | continue; | |
227 |
|
227 | |||
228 | switch (axis->alignment()) { |
|
228 | switch (axis->alignment()) { | |
229 | case Qt::AlignLeft: |
|
229 | case Qt::AlignLeft: | |
230 | left.setWidth(left.width() + size.width()); |
|
230 | left.setWidth(left.width() + size.width()); | |
231 | left.setHeight(qMax(left.height() * 2, size.height())); |
|
231 | left.setHeight(qMax(left.height() * 2, size.height())); | |
232 | break; |
|
232 | break; | |
233 | case Qt::AlignRight: |
|
233 | case Qt::AlignRight: | |
234 | right.setWidth(right.width() + size.width()); |
|
234 | right.setWidth(right.width() + size.width()); | |
235 | right.setHeight(qMax(right.height() * 2, size.height())); |
|
235 | right.setHeight(qMax(right.height() * 2, size.height())); | |
236 | break; |
|
236 | break; | |
237 | case Qt::AlignTop: |
|
237 | case Qt::AlignTop: | |
238 | top.setWidth(qMax(top.width(), size.width())); |
|
238 | top.setWidth(qMax(top.width(), size.width())); | |
239 | top.setHeight(top.height() + size.height()); |
|
239 | top.setHeight(top.height() + size.height()); | |
240 | break; |
|
240 | break; | |
241 | case Qt::AlignBottom: |
|
241 | case Qt::AlignBottom: | |
242 | bottom.setWidth(qMax(bottom.width(), size.width())); |
|
242 | bottom.setWidth(qMax(bottom.width(), size.width())); | |
243 | bottom.setHeight(bottom.height() + size.height()); |
|
243 | bottom.setHeight(bottom.height() + size.height()); | |
244 | break; |
|
244 | break; | |
245 | } |
|
245 | } | |
246 | } |
|
246 | } | |
247 | return minimum.adjusted(0, 0, left.width() + right.width() + qMax(top.width(), bottom.width()), top.height() + bottom.height() + qMax(left.height(), right.height())); |
|
247 | return minimum.adjusted(0, 0, left.width() + right.width() + qMax(top.width(), bottom.width()), top.height() + bottom.height() + qMax(left.height(), right.height())); | |
248 | } |
|
248 | } | |
249 |
|
249 | |||
250 | QRectF ChartLayout::calculateLegendGeometry(const QRectF &geometry, QLegend *legend) const |
|
250 | QRectF ChartLayout::calculateLegendGeometry(const QRectF &geometry, QLegend *legend) const | |
251 | { |
|
251 | { | |
252 | QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1)); |
|
252 | QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1)); | |
253 | QRectF legendRect; |
|
253 | QRectF legendRect; | |
254 | QRectF result; |
|
254 | QRectF result; | |
255 |
|
255 | |||
256 | switch (legend->alignment()) { |
|
256 | switch (legend->alignment()) { | |
257 | case Qt::AlignTop: { |
|
257 | case Qt::AlignTop: { | |
258 | legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height())); |
|
258 | legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height())); | |
259 | result = geometry.adjusted(0, legendRect.height(), 0, 0); |
|
259 | result = geometry.adjusted(0, legendRect.height(), 0, 0); | |
260 | break; |
|
260 | break; | |
261 | } |
|
261 | } | |
262 | case Qt::AlignBottom: { |
|
262 | case Qt::AlignBottom: { | |
263 | legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height())); |
|
263 | legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height())); | |
264 | result = geometry.adjusted(0, 0, 0, -legendRect.height()); |
|
264 | result = geometry.adjusted(0, 0, 0, -legendRect.height()); | |
265 | break; |
|
265 | break; | |
266 | } |
|
266 | } | |
267 | case Qt::AlignLeft: { |
|
267 | case Qt::AlignLeft: { | |
268 | qreal width = qMin(size.width(), geometry.width() * golden_ratio); |
|
268 | qreal width = qMin(size.width(), geometry.width() * golden_ratio); | |
269 | legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height())); |
|
269 | legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height())); | |
270 | result = geometry.adjusted(width, 0, 0, 0); |
|
270 | result = geometry.adjusted(width, 0, 0, 0); | |
271 | break; |
|
271 | break; | |
272 | } |
|
272 | } | |
273 | case Qt::AlignRight: { |
|
273 | case Qt::AlignRight: { | |
274 | qreal width = qMin(size.width(), geometry.width() * golden_ratio); |
|
274 | qreal width = qMin(size.width(), geometry.width() * golden_ratio); | |
275 | legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height())); |
|
275 | legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height())); | |
276 | result = geometry.adjusted(0, 0, -width, 0); |
|
276 | result = geometry.adjusted(0, 0, -width, 0); | |
277 | break; |
|
277 | break; | |
278 | } |
|
278 | } | |
279 | default: { |
|
279 | default: { | |
280 | legendRect = QRectF(0, 0, 0, 0); |
|
280 | legendRect = QRectF(0, 0, 0, 0); | |
281 | result = geometry; |
|
281 | result = geometry; | |
282 | break; |
|
282 | break; | |
283 | } |
|
283 | } | |
284 | } |
|
284 | } | |
285 |
|
285 | |||
286 | legend->setGeometry(legendRect); |
|
286 | legend->setGeometry(legendRect); | |
287 |
|
287 | |||
288 | return result; |
|
288 | return result; | |
289 | } |
|
289 | } | |
290 |
|
290 | |||
291 |
|
291 | |||
292 | QRectF ChartLayout::calculateChartsGeometry(const QRectF &geometry, const QList<ChartElement *>& charts) const |
|
292 | QRectF ChartLayout::calculateChartsGeometry(const QRectF &geometry, const QList<ChartElement *>& charts) const | |
293 | { |
|
293 | { | |
294 | Q_ASSERT(geometry.isValid()); |
|
294 | Q_ASSERT(geometry.isValid()); | |
295 | foreach (ChartElement *chart, charts) |
|
295 | foreach (ChartElement *chart, charts) | |
296 | chart->handleGeometryChanged(geometry); |
|
296 | chart->handleGeometryChanged(geometry); | |
297 | return geometry; |
|
297 | return geometry; | |
298 | } |
|
298 | } | |
299 |
|
299 | |||
300 | QRectF ChartLayout::calculateLegendMinimum(const QRectF &geometry, QLegend *legend) const |
|
300 | QRectF ChartLayout::calculateLegendMinimum(const QRectF &geometry, QLegend *legend) const | |
301 | { |
|
301 | { | |
302 | QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)); |
|
302 | QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)); | |
303 | return geometry.adjusted(0, 0, minSize.width(), minSize.height()); |
|
303 | return geometry.adjusted(0, 0, minSize.width(), minSize.height()); | |
304 | } |
|
304 | } | |
305 |
|
305 | |||
306 | QRectF ChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) const |
|
306 | QRectF ChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) const | |
307 | { |
|
307 | { | |
308 | title->setGeometry(geometry); |
|
308 | title->setGeometry(geometry); | |
309 | QPointF center = geometry.center() - title->boundingRect().center(); |
|
309 | QPointF center = geometry.center() - title->boundingRect().center(); | |
310 | title->setPos(center.x(),title->pos().y()); |
|
310 | title->setPos(center.x(),title->pos().y()); | |
311 | return geometry.adjusted(0,title->boundingRect().height()+1,0,0); |
|
311 | return geometry.adjusted(0,title->boundingRect().height()+1,0,0); | |
312 | } |
|
312 | } | |
313 |
|
313 | |||
314 | QRectF ChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const |
|
314 | QRectF ChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const | |
315 | { |
|
315 | { | |
316 | QSizeF min = title->sizeHint(Qt::MinimumSize); |
|
316 | QSizeF min = title->sizeHint(Qt::MinimumSize); | |
317 | return minimum.adjusted(0, 0, min.width(), min.height()); |
|
317 | return minimum.adjusted(0, 0, min.width(), min.height()); | |
318 | } |
|
318 | } | |
319 |
|
319 | |||
320 | QSizeF ChartLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
320 | QSizeF ChartLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
321 | { |
|
321 | { | |
322 | Q_UNUSED(constraint); |
|
322 | Q_UNUSED(constraint); | |
323 | if (which == Qt::MinimumSize) { |
|
323 | if (which == Qt::MinimumSize) { | |
324 | QList<ChartAxis *> axes = m_presenter->axisItems(); |
|
324 | QList<ChartAxis *> axes = m_presenter->axisItems(); | |
325 | ChartTitle *title = m_presenter->titleElement(); |
|
325 | ChartTitle *title = m_presenter->titleElement(); | |
326 | QLegend *legend = m_presenter->legend(); |
|
326 | QLegend *legend = m_presenter->legend(); | |
327 | QRectF minimumRect(0, 0, 0, 0); |
|
327 | QRectF minimumRect(0, 0, 0, 0); | |
328 | minimumRect = calculateBackgroundMinimum(minimumRect); |
|
328 | minimumRect = calculateBackgroundMinimum(minimumRect); | |
329 | minimumRect = calculateContentMinimum(minimumRect); |
|
329 | minimumRect = calculateContentMinimum(minimumRect); | |
330 | minimumRect = calculateTitleMinimum(minimumRect, title); |
|
330 | minimumRect = calculateTitleMinimum(minimumRect, title); | |
331 | minimumRect = calculateLegendMinimum(minimumRect, legend); |
|
331 | minimumRect = calculateLegendMinimum(minimumRect, legend); | |
332 | minimumRect = calculateAxisMinimum(minimumRect, axes); |
|
332 | minimumRect = calculateAxisMinimum(minimumRect, axes); | |
333 | return minimumRect.united(m_minChartRect).size().toSize(); |
|
333 | return minimumRect.united(m_minChartRect).size().toSize(); | |
334 | } |
|
334 | } | |
335 | return QSize(-1, -1); |
|
335 | return QSize(-1, -1); | |
336 | } |
|
336 | } | |
337 |
|
337 | |||
338 | void ChartLayout::setMargins(const QMargins &margins) |
|
338 | void ChartLayout::setMargins(const QMargins &margins) | |
339 | { |
|
339 | { | |
340 | if (m_margins != margins) { |
|
340 | if (m_margins != margins) { | |
341 | m_margins = margins; |
|
341 | m_margins = margins; | |
342 | updateGeometry(); |
|
342 | updateGeometry(); | |
343 | } |
|
343 | } | |
344 | } |
|
344 | } | |
345 |
|
345 | |||
346 | QMargins ChartLayout::margins() const |
|
346 | QMargins ChartLayout::margins() const | |
347 | { |
|
347 | { | |
348 | return m_margins; |
|
348 | return m_margins; | |
349 | } |
|
349 | } | |
350 |
|
350 | |||
351 | QTCOMMERCIALCHART_END_NAMESPACE |
|
351 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now