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