1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/charttitle_p.h>
5#include <private/chartpresenter_p.h>
6#include <QtGui/QFont>
7#include <QtGui/QFontMetrics>
8#include <QtCore/QDebug>
9#include <QtGui/QTextDocument>
10
11QT_BEGIN_NAMESPACE
12
13ChartTitle::ChartTitle(QGraphicsItem *parent)
14 : QGraphicsTextItem(parent)
15{
16 document()->setDocumentMargin(ChartPresenter::textMargin());
17 setAcceptedMouseButtons({});
18}
19
20ChartTitle::~ChartTitle()
21{
22
23}
24
25void ChartTitle::setText(const QString &text)
26{
27 m_text = text;
28}
29
30QString ChartTitle::text() const
31{
32 return m_text;
33}
34
35void ChartTitle::setGeometry(const QRectF &rect)
36{
37 QRectF truncatedRect;
38 if (m_text.isEmpty()) {
39 QGraphicsTextItem::setHtml(m_text);
40 QGraphicsTextItem::setTextWidth(0.0);
41 } else {
42 QGraphicsTextItem::setHtml(ChartPresenter::truncatedText(font: font(), text: m_text, angle: qreal(0.0),
43 maxWidth: rect.width(), maxHeight: rect.height(),
44 boundingRect&: truncatedRect));
45 QGraphicsTextItem::setTextWidth(truncatedRect.width());
46 }
47 setPos(rect.topLeft());
48}
49
50
51QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
52{
53 Q_UNUSED(constraint);
54 QSizeF sh;
55
56 switch (which) {
57 case Qt::MinimumSize: {
58 QRectF titleRect = ChartPresenter::textBoundingRect(font: font(), QStringLiteral("..."));
59 sh = QSizeF(titleRect.width(), titleRect.height());
60 break;
61 }
62 case Qt::PreferredSize:
63 case Qt::MaximumSize: {
64 QRectF titleRect = ChartPresenter::textBoundingRect(font: font(), text: m_text);
65 sh = QSizeF(titleRect.width(), titleRect.height());
66 break;
67 }
68 case Qt::MinimumDescent: {
69 QFontMetrics fn(font());
70 sh = QSizeF(0, fn.descent());
71 break;
72 }
73 default:
74 break;
75 }
76
77 return sh;
78}
79
80QT_END_NAMESPACE
81

source code of qtcharts/src/charts/charttitle.cpp