@@ -1,299 +1,299 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "chartlayout_p.h" |
|
22 | 22 | #include "chartpresenter_p.h" |
|
23 | 23 | #include "qlegend_p.h" |
|
24 | 24 | #include "chartaxis_p.h" |
|
25 | 25 | #include "charttitle_p.h" |
|
26 | 26 | #include "chartbackground_p.h" |
|
27 | 27 | #include "legendmarker_p.h" |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | static const qreal golden_ratio = 0.25; |
|
32 | 32 | |
|
33 | 33 | ChartLayout::ChartLayout(ChartPresenter* presenter): |
|
34 | 34 | m_presenter(presenter), |
|
35 | 35 | m_margins(20,20,20,20), |
|
36 | 36 | m_minChartRect(0,0,200,200) |
|
37 | 37 | { |
|
38 | 38 | |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | ChartLayout::~ChartLayout() |
|
42 | 42 | { |
|
43 | 43 | |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void ChartLayout::setGeometry(const QRectF& rect) |
|
47 | 47 | { |
|
48 | 48 | if (!rect.isValid()) |
|
49 | 49 | return; |
|
50 | 50 | |
|
51 | 51 | QList<ChartAxis*> axes = m_presenter->axisItems(); |
|
52 | 52 | ChartTitle* title = m_presenter->titleElement(); |
|
53 | 53 | QLegend* legend = m_presenter->legend(); |
|
54 | 54 | ChartBackground* background = m_presenter->backgroundElement(); |
|
55 | 55 | |
|
56 | 56 | QRectF contentGeometry = calculateBackgroundGeometry(rect,background); |
|
57 | 57 | |
|
58 | 58 | contentGeometry = calculateContentGeometry(contentGeometry); |
|
59 | 59 | |
|
60 | 60 | if (title && title->isVisible()) { |
|
61 | 61 | contentGeometry = calculateTitleGeometry(contentGeometry,title); |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | if (legend->isAttachedToChart() && legend->isVisible()) { |
|
65 | 65 | contentGeometry = calculateLegendGeometry(contentGeometry,legend); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | calculateChartGeometry(contentGeometry,axes); |
|
69 | 69 | |
|
70 | 70 | //TODO remove me |
|
71 | 71 | #ifdef SHOW_LAYOUT |
|
72 | 72 | LayoutDebuger* debuger = LayoutDebuger::instance(); |
|
73 | 73 | debuger->reset(); |
|
74 | 74 | debuger->setPen(QPen(Qt::red)); |
|
75 | 75 | debuger->add(backgroundGeometry,m_presenter->rootItem()); |
|
76 | 76 | debuger->add(titleGeometry,m_presenter->rootItem()); |
|
77 | 77 | debuger->add(legendGeometry ,m_presenter->rootItem()); |
|
78 | 78 | debuger->add(axisGeometry ,m_presenter->rootItem()); |
|
79 | 79 | debuger->add(geometry,m_presenter->rootItem()); |
|
80 | 80 | foreach(LegendMarker* marker,legend->d_ptr->markers()){ |
|
81 | 81 | debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem()); |
|
82 | 82 | } |
|
83 | 83 | #endif |
|
84 | 84 | |
|
85 | 85 | QGraphicsLayout::setGeometry(rect); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const |
|
89 | 89 | { |
|
90 | 90 | return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom()); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const |
|
94 | 94 | { |
|
95 | 95 | return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom()); |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const |
|
100 | 100 | { |
|
101 | 101 | qreal left, top, right, bottom; |
|
102 | 102 | getContentsMargins(&left, &top, &right, &bottom); |
|
103 | 103 | QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom); |
|
104 | 104 | if(background) background->setRect(backgroundGeometry); |
|
105 | 105 | return backgroundGeometry; |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const |
|
109 | 109 | { |
|
110 | 110 | qreal left, top, right, bottom; |
|
111 | 111 | getContentsMargins(&left, &top, &right, &bottom); |
|
112 | 112 | return minimum.adjusted(0,0,left + right,top+bottom); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const |
|
116 | 116 | { |
|
117 | 117 | |
|
118 | 118 | QSizeF vertical(0,0); |
|
119 | 119 | QSizeF horizontal(0,0); |
|
120 | 120 | |
|
121 | 121 | int topAxisCount = 0; |
|
122 | 122 | int bottomAxisCount = 0; |
|
123 | 123 | int leftAxisCount = 0; |
|
124 | 124 | int rightAxisCount = 0; |
|
125 | 125 | |
|
126 | 126 | // check axis size |
|
127 | 127 | foreach(ChartAxis* axis , axes) { |
|
128 | 128 | if(axis->orientation()==Qt::Vertical && axis->isVisible()) { |
|
129 | vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); | |
|
129 | // vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); | |
|
130 | 130 | if (axis->alternativePlacement()) |
|
131 | 131 | rightAxisCount++; |
|
132 | 132 | else |
|
133 | 133 | leftAxisCount++; |
|
134 | 134 | } |
|
135 | 135 | else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) { |
|
136 | horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); | |
|
136 | // horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); | |
|
137 | 137 | if (axis->alternativePlacement()) |
|
138 | 138 | topAxisCount++; |
|
139 | 139 | else |
|
140 | 140 | bottomAxisCount++; |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | qreal width = qMin(vertical.width(),geometry.width() * golden_ratio); |
|
146 | 146 | |
|
147 | 147 | QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height()); |
|
148 | 148 | |
|
149 | 149 | // axis area width |
|
150 | 150 | // TODO: replase with dynamic size code |
|
151 | 151 | int axisWidth = 35; |
|
152 | 152 | |
|
153 | 153 | // get the final size of the plot rect |
|
154 | 154 | rect.adjust(leftAxisCount * axisWidth, topAxisCount * axisWidth, -rightAxisCount * axisWidth, -bottomAxisCount * axisWidth); |
|
155 | 155 | m_presenter->setChartsGeometry(rect); |
|
156 | 156 | |
|
157 | 157 | leftAxisCount = 0; |
|
158 | 158 | rightAxisCount = 0; |
|
159 | 159 | bottomAxisCount = 0; |
|
160 | 160 | topAxisCount = 0; |
|
161 | 161 | |
|
162 | 162 | // adjust the axes internal and external rects |
|
163 | 163 | foreach(ChartAxis* axis , axes) { |
|
164 | 164 | if(axis->orientation()==Qt::Vertical) { |
|
165 | 165 | axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0)); |
|
166 | 166 | axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0)); |
|
167 | 167 | if (axis->alternativePlacement()) |
|
168 | 168 | rightAxisCount++; |
|
169 | 169 | else |
|
170 | 170 | leftAxisCount++; |
|
171 | 171 | } |
|
172 | 172 | else if(axis->orientation()==Qt::Horizontal) { |
|
173 | 173 | axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth)); |
|
174 | 174 | axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth)); |
|
175 | 175 | if (axis->alternativePlacement()) |
|
176 | 176 | topAxisCount++; |
|
177 | 177 | else |
|
178 | 178 | bottomAxisCount++; |
|
179 | 179 | } |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | return rect; |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const |
|
186 | 186 | { |
|
187 | 187 | QSizeF vertical(0,0); |
|
188 | 188 | QSizeF horizontal(0,0); |
|
189 | 189 | |
|
190 | 190 | // check axis size |
|
191 | 191 | foreach(ChartAxis* axis , axes) { |
|
192 | 192 | if(axis->orientation()==Qt::Vertical && axis->isVisible()){ |
|
193 | 193 | vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); |
|
194 | 194 | }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) { |
|
195 | 195 | horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); |
|
196 | 196 | } |
|
197 | 197 | } |
|
198 | 198 | return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height()); |
|
199 | 199 | } |
|
200 | 200 | |
|
201 | 201 | QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const |
|
202 | 202 | { |
|
203 | 203 | QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1)); |
|
204 | 204 | QRectF legendRect; |
|
205 | 205 | QRectF result; |
|
206 | 206 | |
|
207 | 207 | switch (legend->alignment()) { |
|
208 | 208 | case Qt::AlignTop: { |
|
209 | 209 | legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height())); |
|
210 | 210 | result = geometry.adjusted(0,legendRect.height(),0,0); |
|
211 | 211 | break; |
|
212 | 212 | } |
|
213 | 213 | case Qt::AlignBottom: { |
|
214 | 214 | legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height())); |
|
215 | 215 | result = geometry.adjusted(0,0,0,-legendRect.height()); |
|
216 | 216 | break; |
|
217 | 217 | } |
|
218 | 218 | case Qt::AlignLeft: { |
|
219 | 219 | qreal width = qMin(size.width(),geometry.width()*golden_ratio); |
|
220 | 220 | legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height())); |
|
221 | 221 | result = geometry.adjusted(width,0,0,0); |
|
222 | 222 | break; |
|
223 | 223 | } |
|
224 | 224 | case Qt::AlignRight: { |
|
225 | 225 | qreal width = qMin(size.width(),geometry.width()*golden_ratio); |
|
226 | 226 | legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height())); |
|
227 | 227 | result = geometry.adjusted(0,0,-width,0); |
|
228 | 228 | break; |
|
229 | 229 | } |
|
230 | 230 | default: { |
|
231 | 231 | legendRect = QRectF(0,0,0,0); |
|
232 | 232 | result = geometry; |
|
233 | 233 | break; |
|
234 | 234 | } |
|
235 | 235 | } |
|
236 | 236 | |
|
237 | 237 | legend->setGeometry(legendRect); |
|
238 | 238 | |
|
239 | 239 | return result; |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const |
|
243 | 243 | { |
|
244 | 244 | QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1)); |
|
245 | 245 | return geometry.adjusted(0,0,minSize.width(),minSize.height()); |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const |
|
249 | 249 | { |
|
250 | 250 | title->setGeometry(geometry); |
|
251 | 251 | QPointF center = geometry.center() - title->boundingRect().center(); |
|
252 | 252 | title->setPos(center.x(),title->pos().y()); |
|
253 | 253 | return geometry.adjusted(0,title->boundingRect().height(),0,0); |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | 256 | QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const |
|
257 | 257 | { |
|
258 | 258 | QSizeF min = title->sizeHint(Qt::MinimumSize); |
|
259 | 259 | return minimum.adjusted(0,0,min.width(),min.height()); |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const |
|
263 | 263 | { |
|
264 | 264 | Q_UNUSED(constraint); |
|
265 | 265 | if(which == Qt::MinimumSize){ |
|
266 | 266 | QList<ChartAxis*> axes = m_presenter->axisItems(); |
|
267 | 267 | ChartTitle* title = m_presenter->titleElement(); |
|
268 | 268 | QLegend* legend = m_presenter->legend(); |
|
269 | 269 | QRectF minimumRect(0,0,0,0); |
|
270 | 270 | minimumRect = calculateBackgroundMinimum(minimumRect); |
|
271 | 271 | minimumRect = calculateContentMinimum(minimumRect); |
|
272 | 272 | minimumRect = calculateTitleMinimum(minimumRect,title); |
|
273 | 273 | minimumRect = calculateLegendMinimum(minimumRect,legend); |
|
274 | 274 | minimumRect = calculateAxisMinimum(minimumRect,axes); |
|
275 | 275 | return minimumRect.united(m_minChartRect).size().toSize(); |
|
276 | 276 | }else |
|
277 | 277 | return QSize(-1,-1); |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | void ChartLayout::setMargins(const QMargins& margins) |
|
281 | 281 | { |
|
282 | 282 | |
|
283 | 283 | if(m_margins != margins){ |
|
284 | 284 | m_margins = margins; |
|
285 | 285 | updateGeometry(); |
|
286 | 286 | } |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | QMargins ChartLayout::margins() const |
|
290 | 290 | { |
|
291 | 291 | return m_margins; |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | void ChartLayout::adjustChartGeometry() |
|
295 | 295 | { |
|
296 | 296 | setGeometry(geometry()); |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now