diff options
Diffstat (limited to 'tests/manual/qglyphruns/view.cpp')
-rw-r--r-- | tests/manual/qglyphruns/view.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/manual/qglyphruns/view.cpp b/tests/manual/qglyphruns/view.cpp new file mode 100644 index 0000000000..0e7d6dd478 --- /dev/null +++ b/tests/manual/qglyphruns/view.cpp @@ -0,0 +1,70 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#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); + } + } +} |