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