1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <private/charttitle_p.h> |
31 | #include <private/chartpresenter_p.h> |
32 | #include <QtGui/QFont> |
33 | #include <QtGui/QFontMetrics> |
34 | #include <QtCore/QDebug> |
35 | #include <QtGui/QTextDocument> |
36 | |
37 | QT_CHARTS_BEGIN_NAMESPACE |
38 | |
39 | ChartTitle::ChartTitle(QGraphicsItem *parent) |
40 | : QGraphicsTextItem(parent) |
41 | { |
42 | document()->setDocumentMargin(ChartPresenter::textMargin()); |
43 | } |
44 | |
45 | ChartTitle::~ChartTitle() |
46 | { |
47 | |
48 | } |
49 | |
50 | void ChartTitle::setText(const QString &text) |
51 | { |
52 | m_text = text; |
53 | } |
54 | |
55 | QString ChartTitle::text() const |
56 | { |
57 | return m_text; |
58 | } |
59 | |
60 | void ChartTitle::setGeometry(const QRectF &rect) |
61 | { |
62 | QRectF truncatedRect; |
63 | if (m_text.isEmpty()) { |
64 | QGraphicsTextItem::setHtml(m_text); |
65 | QGraphicsTextItem::setTextWidth(0.0); |
66 | } else { |
67 | QGraphicsTextItem::setHtml(ChartPresenter::truncatedText(font: font(), text: m_text, angle: qreal(0.0), |
68 | maxWidth: rect.width(), maxHeight: rect.height(), |
69 | boundingRect&: truncatedRect)); |
70 | QGraphicsTextItem::setTextWidth(truncatedRect.width()); |
71 | } |
72 | setPos(rect.topLeft()); |
73 | } |
74 | |
75 | |
76 | QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
77 | { |
78 | Q_UNUSED(constraint); |
79 | QSizeF sh; |
80 | |
81 | switch (which) { |
82 | case Qt::MinimumSize: { |
83 | QRectF titleRect = ChartPresenter::textBoundingRect(font: font(), QStringLiteral("..." )); |
84 | sh = QSizeF(titleRect.width(), titleRect.height()); |
85 | break; |
86 | } |
87 | case Qt::PreferredSize: |
88 | case Qt::MaximumSize: { |
89 | QRectF titleRect = ChartPresenter::textBoundingRect(font: font(), text: m_text); |
90 | sh = QSizeF(titleRect.width(), titleRect.height()); |
91 | break; |
92 | } |
93 | case Qt::MinimumDescent: { |
94 | QFontMetrics fn(font()); |
95 | sh = QSizeF(0, fn.descent()); |
96 | break; |
97 | } |
98 | default: |
99 | break; |
100 | } |
101 | |
102 | return sh; |
103 | } |
104 | |
105 | QT_CHARTS_END_NAMESPACE |
106 | |