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 20;
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
50int TextAutogenerateDelegateUtils::iconSpacing()
51{
52 return 10;
53}
54
55bool TextAutogenerateDelegateUtils::generateToolTip(const QTextDocument *doc, const QPoint &pos, QString &formattedTooltip)
56{
57 const auto format = doc->documentLayout()->formatAt(pos);
58 const auto tooltip = format.property(QTextFormat::TextToolTip).toString();
59 const auto href = format.property(QTextFormat::AnchorHref).toString();
60 if (href.isEmpty() && tooltip.isEmpty()) {
61 return false;
62 }
63 generateToolTip(tooltip, href, formattedTooltip);
64 return true;
65}
66
67void TextAutogenerateDelegateUtils::generateToolTip(const QString &toolTip, const QString &href, QString &formattedTooltip)
68{
69 QTextStream stream(&formattedTooltip);
70 auto addLine = [&](const QString &line) {
71 if (!line.isEmpty()) {
72 stream << "<p>"_L1 << line << "</p>"_L1;
73 }
74 };
75
76 stream << "<qt>"_L1;
77 addLine(toolTip);
78 addLine(href);
79 stream << "</qt>"_L1;
80}
81
82QList<QAbstractTextDocumentLayout::Selection> TextAutogenerateDelegateUtils::selection(TextAutogenerateListViewTextSelection *selection,
83 QTextDocument *doc,
84 const QModelIndex &index,
85 const QStyleOptionViewItem &option)
86{
88 const QTextCursor selectionTextCursor = selection->selectionForIndex(index, doc);
89 if (!selectionTextCursor.isNull()) {
90 QTextCharFormat selectionFormat;
91 selectionFormat.setBackground(option.palette.brush(QPalette::Highlight));
92 selectionFormat.setForeground(option.palette.brush(QPalette::HighlightedText));
93 selections.append({selectionTextCursor, selectionFormat});
94 }
95 return selections;
96}
97
98void TextAutogenerateDelegateUtils::setClipboardSelection(TextAutogenerateListViewTextSelection *selection)
99{
101 if (selection->hasSelection() && clipboard->supportsSelection()) {
102 const QString text = selection->selectedText(TextAutogenerateListViewTextSelection::Format::Text);
103 clipboard->setText(text, QClipboard::Selection);
104 }
105}
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 May 2 2025 12:06:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.