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