blob: 95c05c608dd3bf6d10d3988e537d7b3c79add57f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "view.h"
#include <QTextLayout>
#include <QPainter>
View::View(QWidget *parent)
: QWidget{parent}
{
setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum));
}
View::~View()
{
delete m_layout;
}
void View::updateLayout(const QString &sourceString,
float width,
QTextOption::WrapMode mode,
const QFont &font)
{
if (m_layout == nullptr)
m_layout = new QTextLayout;
m_layout->setText(sourceString);
QTextOption option;
option.setWrapMode(mode);
m_layout->setTextOption(option);
m_layout->setFont(font);
m_layout->beginLayout();
float y = 0.0f;
forever {
QTextLine line = m_layout->createLine();
if (!line.isValid())
break;
line.setLineWidth(width);
line.setPosition(QPointF(0, y));
y += line.height();
}
m_layout->endLayout();
update();
updateGeometry();
}
QSize View::sizeHint() const
{
if (m_layout != nullptr)
return m_layout->boundingRect().size().toSize();
else
return QSize(100, 100);
}
void View::paintEvent(QPaintEvent *)
{
QPainter p(this);
if (m_layout != nullptr)
m_layout->draw(&p, QPointF(0, 0));
if (!m_bounds.isEmpty()) {
p.setPen(Qt::NoPen);
p.setBrush(Qt::yellow);
p.setOpacity(0.25);
for (const QRect &r : m_bounds) {
p.drawRect(r);
}
}
}
|