KTextAddons

textautogeneratedelegateutils.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "textautogeneratedelegateutils.h"
8
9#include "textautogeneratelistviewtextselection.h"
10
11#include <QClipboard>
12#include <QTextDocument>
13#include <QTextFormat>
14#include <QTextStream>
15#include <qabstracttextdocumentlayout.h>
16
17using namespace Qt::Literals::StringLiterals;
18using namespace TextAutogenerateText;
19bool TextAutogenerateDelegateUtils::generateToolTip(const QTextDocument *doc, const QPoint &pos, QString &formattedTooltip)
20{
21 const auto format = doc->documentLayout()->formatAt(pos);
22 const auto tooltip = format.property(QTextFormat::TextToolTip).toString();
23 const auto href = format.property(QTextFormat::AnchorHref).toString();
24 if (href.isEmpty() && tooltip.isEmpty()) {
25 return false;
26 }
27 generateToolTip(tooltip, href, formattedTooltip);
28 return true;
29}
30
31void TextAutogenerateDelegateUtils::generateToolTip(const QString &toolTip, const QString &href, QString &formattedTooltip)
32{
33 QTextStream stream(&formattedTooltip);
34 auto addLine = [&](const QString &line) {
35 if (!line.isEmpty()) {
36 stream << "<p>"_L1 << line << "</p>"_L1;
37 }
38 };
39
40 stream << "<qt>"_L1;
41 addLine(toolTip);
42 addLine(href);
43 stream << "</qt>"_L1;
44}
45
46QList<QAbstractTextDocumentLayout::Selection> TextAutogenerateDelegateUtils::selection(TextAutogenerateListViewTextSelection *selection,
47 QTextDocument *doc,
48 const QModelIndex &index,
49 const QStyleOptionViewItem &option)
50{
52 const QTextCursor selectionTextCursor = selection->selectionForIndex(index, doc);
53 if (!selectionTextCursor.isNull()) {
54 QTextCharFormat selectionFormat;
55 selectionFormat.setBackground(option.palette.brush(QPalette::Highlight));
56 selectionFormat.setForeground(option.palette.brush(QPalette::HighlightedText));
57 selections.append({selectionTextCursor, selectionFormat});
58 }
59 return selections;
60}
61
62void TextAutogenerateDelegateUtils::setClipboardSelection(TextAutogenerateListViewTextSelection *selection)
63{
65 if (selection->hasSelection() && clipboard->supportsSelection()) {
66 const QString text = selection->selectedText(TextAutogenerateListViewTextSelection::Format::Text);
67 clipboard->setText(text, QClipboard::Selection);
68 }
69}
QTextFormat formatAt(const QPointF &pos) const const
void setText(const QString &text, Mode mode)
bool supportsSelection() const const
QClipboard * clipboard()
void append(QList< T > &&value)
bool isNull() const const
QAbstractTextDocumentLayout * documentLayout() const const
QVariant property(int propertyId) const const
void setBackground(const QBrush &brush)
void setForeground(const QBrush &brush)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:00:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.