KIdentityManagement

signaturerichtexteditor.cpp
1/*
2 SPDX-FileCopyrightText: 2002-2004 Marc Mutz <mutz@kde.org>
3 SPDX-FileCopyrightText: 2007 Tom Albers <tomalbers@kde.nl>
4 SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "signaturerichtexteditor_p.h"
10
11#include <KPIMTextEdit/RichTextComposer>
12#include <KPIMTextEdit/RichTextComposerControler>
13#include <KPIMTextEdit/RichTextComposerImages>
14
15using namespace KIdentityManagementWidgets;
16
17static bool isCursorAtEndOfLine(const QTextCursor &cursor)
18{
19 QTextCursor testCursor = cursor;
21 return !testCursor.hasSelection();
22}
23
24static void insertSignatureHelper(const QString &signature,
27 bool isHtml,
28 bool addNewlines)
29{
30 if (!signature.isEmpty()) {
31 // Save the modified state of the document, as inserting a signature
32 // shouldn't change this. Restore it at the end of this function.
33 bool isModified = textEdit->document()->isModified();
34
35 // Move to the desired position, where the signature should be inserted
36 QTextCursor cursor = textEdit->textCursor();
37 QTextCursor oldCursor = cursor;
38 cursor.beginEditBlock();
39
42 } else if (placement == KIdentityManagementCore::Signature::Start) {
44 } else if (placement == KIdentityManagementCore::Signature::AtCursor) {
46 }
47 textEdit->setTextCursor(cursor);
48
49 QString lineSep;
50 if (addNewlines) {
51 if (isHtml) {
52 lineSep = QStringLiteral("<br>");
53 } else {
54 lineSep = QLatin1Char('\n');
55 }
56 }
57
58 // Insert the signature and newlines depending on where it was inserted.
59 int newCursorPos = -1;
60 QString headSep;
61 QString tailSep;
62
64 // There is one special case when re-setting the old cursor: The cursor
65 // was at the end. In this case, QTextEdit has no way to know
66 // if the signature was added before or after the cursor, and just
67 // decides that it was added before (and the cursor moves to the end,
68 // but it should not when appending a signature). See bug 167961
69 if (oldCursor.position() == textEdit->toPlainText().length()) {
70 newCursorPos = oldCursor.position();
71 }
72 headSep = lineSep;
73 } else if (placement == KIdentityManagementCore::Signature::Start) {
74 // When prepending signatures, add a couple of new lines before
75 // the signature, and move the cursor to the beginning of the QTextEdit.
76 // People tends to insert new text there.
77 newCursorPos = 0;
78 headSep = lineSep + lineSep;
79 if (!isCursorAtEndOfLine(cursor)) {
80 tailSep = lineSep;
81 }
82 } else if (placement == KIdentityManagementCore::Signature::AtCursor) {
83 if (!isCursorAtEndOfLine(cursor)) {
84 tailSep = lineSep;
85 }
86 }
87
88 const QString full_signature = headSep + signature + tailSep;
89 if (isHtml) {
90 textEdit->insertHtml(full_signature);
91 } else {
92 textEdit->insertPlainText(full_signature);
93 }
94
95 cursor.endEditBlock();
96 if (newCursorPos != -1) {
97 oldCursor.setPosition(newCursorPos);
98 }
99
100 textEdit->setTextCursor(oldCursor);
101 textEdit->ensureCursorVisible();
102
103 textEdit->document()->setModified(isModified);
104
105 if (isHtml) {
106 textEdit->activateRichText();
107 }
108 }
109}
110
111void SignatureRichTextEditor::insertIntoTextEdit(const KIdentityManagementCore::Signature &sig,
115 bool forceDisplay)
116{
117 if (!forceDisplay) {
118 if (!sig.isEnabledSignature()) {
119 return;
120 }
121 }
122 QString signature;
124 signature = sig.withSeparator();
125 } else {
126 signature = sig.rawText();
127 }
128 insertSignatureHelper(signature,
129 textEdit,
130 placement,
131 (sig.isInlinedHtml() && sig.type() == KIdentityManagementCore::Signature::Inlined),
133
134 // We added the text of the signature above, now it is time to add the images as well.
135 if (sig.isInlinedHtml()) {
136 const auto embeddedImgs = sig.embeddedImages();
137 for (const KIdentityManagementCore::Signature::EmbeddedImagePtr &image : embeddedImgs) {
138 textEdit->composerControler()->composerImages()->loadImage(image->image, image->name, image->name);
139 }
140 }
141}
Abstraction of a signature (aka "footer").
Definition signature.h:61
Placement
Describes the placement of the signature text when it is to be inserted into a text edit.
Definition signature.h:80
@ Start
The signature is placed at the start of the textedit.
Definition signature.h:81
@ AtCursor
The signature is placed at the current cursor position.
Definition signature.h:83
@ End
The signature is placed at the end of the textedit.
Definition signature.h:82
@ AddSeparator
The separator '– ' will be added in front.
Definition signature.h:196
QString rawText(bool *ok=nullptr, QString *errorMessage=nullptr) const
QString withSeparator(bool *ok=nullptr, QString *errorMessage=nullptr) const
bool isEmpty() const const
void beginEditBlock()
void endEditBlock()
bool hasSelection() const const
bool movePosition(MoveOperation operation, MoveMode mode, int n)
int position() const const
void setPosition(int pos, MoveMode m)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:14:39 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.