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;
19
20int TextAutogenerateDelegateUtils::spacingText()
21{
22 return 10;
23}
24
25int TextAutogenerateDelegateUtils::roundRectValue()
26{
27 return 20;
28}
29
30int TextAutogenerateDelegateUtils::marginText()
31{
32 return 10;
33}
34
35int TextAutogenerateDelegateUtils::rightIndent()
36{
37 return 30;
38}
39
40int TextAutogenerateDelegateUtils::leftLLMIndent()
41{
42 return rightIndent();
43}
44
45int TextAutogenerateDelegateUtils::leftUserIndent()
46{
47 return 80;
48}
49
50bool TextAutogenerateDelegateUtils::generateToolTip(const QTextDocument *doc, const QPoint &pos, QString &formattedTooltip)
51{
52 const auto format = doc->documentLayout()->formatAt(pos);
53 const auto tooltip = format.property(QTextFormat::TextToolTip).toString();
54 const auto href = format.property(QTextFormat::AnchorHref).toString();
55 if (href.isEmpty() && tooltip.isEmpty()) {
56 return false;
57 }
58 generateToolTip(tooltip, href, formattedTooltip);
59 return true;
60}
61
62void TextAutogenerateDelegateUtils::generateToolTip(const QString &toolTip, const QString &href, QString &formattedTooltip)
63{
64 QTextStream stream(&formattedTooltip);
65 auto addLine = [&](const QString &line) {
66 if (!line.isEmpty()) {
67 stream << "<p>"_L1 << line << "</p>"_L1;
68 }
69 };
70
71 stream << "<qt>"_L1;
72 addLine(toolTip);
73 addLine(href);
74 stream << "</qt>"_L1;
75}
76
77QList<QAbstractTextDocumentLayout::Selection> TextAutogenerateDelegateUtils::selection(TextAutogenerateListViewTextSelection *selection,
78 QTextDocument *doc,
79 const QModelIndex &index,
80 const QStyleOptionViewItem &option)
81{
83 const QTextCursor selectionTextCursor = selection->selectionForIndex(index, doc);
84 if (!selectionTextCursor.isNull()) {
85 QTextCharFormat selectionFormat;
86 selectionFormat.setBackground(option.palette.brush(QPalette::Highlight));
87 selectionFormat.setForeground(option.palette.brush(QPalette::HighlightedText));
88 selections.append({selectionTextCursor, selectionFormat});
89 }
90 return selections;
91}
92
93void TextAutogenerateDelegateUtils::setClipboardSelection(TextAutogenerateListViewTextSelection *selection)
94{
96 if (selection->hasSelection() && clipboard->supportsSelection()) {
97 const QString text = selection->selectedText(TextAutogenerateListViewTextSelection::Format::Text);
98 clipboard->setText(text, QClipboard::Selection);
99 }
100}
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 25 2025 12:06:13 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.