##// END OF EJS Templates
Don't reserve space for chart title if it is empty....
Miikka Heikkinen -
r2794:05fa3caa7879
parent child
Show More
@@ -1,201 +1,201
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/abstractchartlayout_p.h>
19 #include <private/abstractchartlayout_p.h>
20 #include <private/chartpresenter_p.h>
20 #include <private/chartpresenter_p.h>
21 #include <private/qlegend_p.h>
21 #include <private/qlegend_p.h>
22 #include <private/chartaxiselement_p.h>
22 #include <private/chartaxiselement_p.h>
23 #include <private/charttitle_p.h>
23 #include <private/charttitle_p.h>
24 #include <private/chartbackground_p.h>
24 #include <private/chartbackground_p.h>
25 #include <QtCore/QDebug>
25 #include <QtCore/QDebug>
26
26
27 QT_CHARTS_BEGIN_NAMESPACE
27 QT_CHARTS_BEGIN_NAMESPACE
28
28
29 static const qreal golden_ratio = 0.4;
29 static const qreal golden_ratio = 0.4;
30
30
31 AbstractChartLayout::AbstractChartLayout(ChartPresenter *presenter)
31 AbstractChartLayout::AbstractChartLayout(ChartPresenter *presenter)
32 : m_presenter(presenter),
32 : m_presenter(presenter),
33 m_margins(20, 20, 20, 20),
33 m_margins(20, 20, 20, 20),
34 m_minChartRect(0, 0, 200, 200)
34 m_minChartRect(0, 0, 200, 200)
35 {
35 {
36 }
36 }
37
37
38 AbstractChartLayout::~AbstractChartLayout()
38 AbstractChartLayout::~AbstractChartLayout()
39 {
39 {
40 }
40 }
41
41
42 void AbstractChartLayout::setGeometry(const QRectF &rect)
42 void AbstractChartLayout::setGeometry(const QRectF &rect)
43 {
43 {
44 if (!rect.isValid())
44 if (!rect.isValid())
45 return;
45 return;
46
46
47 if (m_presenter->chart()->isVisible()) {
47 if (m_presenter->chart()->isVisible()) {
48 QList<ChartAxisElement *> axes = m_presenter->axisItems();
48 QList<ChartAxisElement *> axes = m_presenter->axisItems();
49 ChartTitle *title = m_presenter->titleElement();
49 ChartTitle *title = m_presenter->titleElement();
50 QLegend *legend = m_presenter->legend();
50 QLegend *legend = m_presenter->legend();
51 ChartBackground *background = m_presenter->backgroundElement();
51 ChartBackground *background = m_presenter->backgroundElement();
52
52
53 QRectF contentGeometry = calculateBackgroundGeometry(rect, background);
53 QRectF contentGeometry = calculateBackgroundGeometry(rect, background);
54
54
55 contentGeometry = calculateContentGeometry(contentGeometry);
55 contentGeometry = calculateContentGeometry(contentGeometry);
56
56
57 if (title && title->isVisible())
57 if (title && title->isVisible() && !title->text().isEmpty())
58 contentGeometry = calculateTitleGeometry(contentGeometry, title);
58 contentGeometry = calculateTitleGeometry(contentGeometry, title);
59
59
60 if (legend->isAttachedToChart() && legend->isVisible())
60 if (legend->isAttachedToChart() && legend->isVisible())
61 contentGeometry = calculateLegendGeometry(contentGeometry, legend);
61 contentGeometry = calculateLegendGeometry(contentGeometry, legend);
62
62
63 contentGeometry = calculateAxisGeometry(contentGeometry, axes);
63 contentGeometry = calculateAxisGeometry(contentGeometry, axes);
64
64
65 m_presenter->setGeometry(contentGeometry);
65 m_presenter->setGeometry(contentGeometry);
66 if (m_presenter->chart()->chartType() == QChart::ChartTypeCartesian)
66 if (m_presenter->chart()->chartType() == QChart::ChartTypeCartesian)
67 static_cast<QGraphicsRectItem *>(m_presenter->plotAreaElement())->setRect(contentGeometry);
67 static_cast<QGraphicsRectItem *>(m_presenter->plotAreaElement())->setRect(contentGeometry);
68 else
68 else
69 static_cast<QGraphicsEllipseItem *>(m_presenter->plotAreaElement())->setRect(contentGeometry);
69 static_cast<QGraphicsEllipseItem *>(m_presenter->plotAreaElement())->setRect(contentGeometry);
70 }
70 }
71
71
72 QGraphicsLayout::setGeometry(rect);
72 QGraphicsLayout::setGeometry(rect);
73 }
73 }
74
74
75 QRectF AbstractChartLayout::calculateContentGeometry(const QRectF &geometry) const
75 QRectF AbstractChartLayout::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 AbstractChartLayout::calculateContentMinimum(const QRectF &minimum) const
80 QRectF AbstractChartLayout::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 AbstractChartLayout::calculateBackgroundGeometry(const QRectF &geometry, ChartBackground *background) const
86 QRectF AbstractChartLayout::calculateBackgroundGeometry(const QRectF &geometry, ChartBackground *background) const
87 {
87 {
88 qreal left;
88 qreal left;
89 qreal top;
89 qreal top;
90 qreal right;
90 qreal right;
91 qreal bottom;
91 qreal bottom;
92 getContentsMargins(&left, &top, &right, &bottom);
92 getContentsMargins(&left, &top, &right, &bottom);
93 QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom);
93 QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom);
94 if (background)
94 if (background)
95 background->setRect(backgroundGeometry);
95 background->setRect(backgroundGeometry);
96 return backgroundGeometry;
96 return backgroundGeometry;
97 }
97 }
98
98
99 QRectF AbstractChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const
99 QRectF AbstractChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const
100 {
100 {
101 qreal left;
101 qreal left;
102 qreal top;
102 qreal top;
103 qreal right;
103 qreal right;
104 qreal bottom;
104 qreal bottom;
105 getContentsMargins(&left, &top, &right, &bottom);
105 getContentsMargins(&left, &top, &right, &bottom);
106 return minimum.adjusted(0, 0, left + right, top + bottom);
106 return minimum.adjusted(0, 0, left + right, top + bottom);
107 }
107 }
108
108
109 QRectF AbstractChartLayout::calculateLegendGeometry(const QRectF &geometry, QLegend *legend) const
109 QRectF AbstractChartLayout::calculateLegendGeometry(const QRectF &geometry, QLegend *legend) const
110 {
110 {
111 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1));
111 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1));
112 QRectF legendRect;
112 QRectF legendRect;
113 QRectF result;
113 QRectF result;
114
114
115 switch (legend->alignment()) {
115 switch (legend->alignment()) {
116 case Qt::AlignTop: {
116 case Qt::AlignTop: {
117 legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height()));
117 legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height()));
118 result = geometry.adjusted(0, legendRect.height(), 0, 0);
118 result = geometry.adjusted(0, legendRect.height(), 0, 0);
119 break;
119 break;
120 }
120 }
121 case Qt::AlignBottom: {
121 case Qt::AlignBottom: {
122 legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height()));
122 legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height()));
123 result = geometry.adjusted(0, 0, 0, -legendRect.height());
123 result = geometry.adjusted(0, 0, 0, -legendRect.height());
124 break;
124 break;
125 }
125 }
126 case Qt::AlignLeft: {
126 case Qt::AlignLeft: {
127 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
127 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
128 legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height()));
128 legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height()));
129 result = geometry.adjusted(width, 0, 0, 0);
129 result = geometry.adjusted(width, 0, 0, 0);
130 break;
130 break;
131 }
131 }
132 case Qt::AlignRight: {
132 case Qt::AlignRight: {
133 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
133 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
134 legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height()));
134 legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height()));
135 result = geometry.adjusted(0, 0, -width, 0);
135 result = geometry.adjusted(0, 0, -width, 0);
136 break;
136 break;
137 }
137 }
138 default: {
138 default: {
139 legendRect = QRectF(0, 0, 0, 0);
139 legendRect = QRectF(0, 0, 0, 0);
140 result = geometry;
140 result = geometry;
141 break;
141 break;
142 }
142 }
143 }
143 }
144
144
145 legend->setGeometry(legendRect);
145 legend->setGeometry(legendRect);
146
146
147 return result;
147 return result;
148 }
148 }
149
149
150 QRectF AbstractChartLayout::calculateLegendMinimum(const QRectF &geometry, QLegend *legend) const
150 QRectF AbstractChartLayout::calculateLegendMinimum(const QRectF &geometry, QLegend *legend) const
151 {
151 {
152 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1));
152 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1));
153 return geometry.adjusted(0, 0, minSize.width(), minSize.height());
153 return geometry.adjusted(0, 0, minSize.width(), minSize.height());
154 }
154 }
155
155
156 QRectF AbstractChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) const
156 QRectF AbstractChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) const
157 {
157 {
158 title->setGeometry(geometry);
158 title->setGeometry(geometry);
159 QPointF center = geometry.center() - title->boundingRect().center();
159 QPointF center = geometry.center() - title->boundingRect().center();
160 title->setPos(center.x(), title->pos().y());
160 title->setPos(center.x(), title->pos().y());
161 return geometry.adjusted(0, title->boundingRect().height()+1, 0, 0);
161 return geometry.adjusted(0, title->boundingRect().height()+1, 0, 0);
162 }
162 }
163
163
164 QRectF AbstractChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const
164 QRectF AbstractChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const
165 {
165 {
166 QSizeF min = title->sizeHint(Qt::MinimumSize);
166 QSizeF min = title->sizeHint(Qt::MinimumSize);
167 return minimum.adjusted(0, 0, min.width(), min.height());
167 return minimum.adjusted(0, 0, min.width(), min.height());
168 }
168 }
169
169
170 QSizeF AbstractChartLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
170 QSizeF AbstractChartLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
171 {
171 {
172 Q_UNUSED(constraint);
172 Q_UNUSED(constraint);
173 if (which == Qt::MinimumSize) {
173 if (which == Qt::MinimumSize) {
174 QList<ChartAxisElement *> axes = m_presenter->axisItems();
174 QList<ChartAxisElement *> axes = m_presenter->axisItems();
175 ChartTitle *title = m_presenter->titleElement();
175 ChartTitle *title = m_presenter->titleElement();
176 QLegend *legend = m_presenter->legend();
176 QLegend *legend = m_presenter->legend();
177 QRectF minimumRect(0, 0, 0, 0);
177 QRectF minimumRect(0, 0, 0, 0);
178 minimumRect = calculateBackgroundMinimum(minimumRect);
178 minimumRect = calculateBackgroundMinimum(minimumRect);
179 minimumRect = calculateContentMinimum(minimumRect);
179 minimumRect = calculateContentMinimum(minimumRect);
180 minimumRect = calculateTitleMinimum(minimumRect, title);
180 minimumRect = calculateTitleMinimum(minimumRect, title);
181 minimumRect = calculateLegendMinimum(minimumRect, legend);
181 minimumRect = calculateLegendMinimum(minimumRect, legend);
182 minimumRect = calculateAxisMinimum(minimumRect, axes);
182 minimumRect = calculateAxisMinimum(minimumRect, axes);
183 return minimumRect.united(m_minChartRect).size().toSize();
183 return minimumRect.united(m_minChartRect).size().toSize();
184 }
184 }
185 return QSize(-1, -1);
185 return QSize(-1, -1);
186 }
186 }
187
187
188 void AbstractChartLayout::setMargins(const QMargins &margins)
188 void AbstractChartLayout::setMargins(const QMargins &margins)
189 {
189 {
190 if (m_margins != margins) {
190 if (m_margins != margins) {
191 m_margins = margins;
191 m_margins = margins;
192 updateGeometry();
192 updateGeometry();
193 }
193 }
194 }
194 }
195
195
196 QMargins AbstractChartLayout::margins() const
196 QMargins AbstractChartLayout::margins() const
197 {
197 {
198 return m_margins;
198 return m_margins;
199 }
199 }
200
200
201 QT_CHARTS_END_NAMESPACE
201 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now