Messagelib

templatestexteditor.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "templatestexteditor.h"
8#include "templatesutil_p.h"
9
10#include <TextCustomEditor/PlainTextSyntaxSpellCheckingHighlighter>
11#include <TextCustomEditor/TextEditorCompleter>
12
13#include <KSyntaxHighlighting/Definition>
14#include <KSyntaxHighlighting/Theme>
15
16#include <QAbstractItemView>
17#include <QCompleter>
18#include <QFontDatabase>
19#include <QKeyEvent>
20using namespace TemplateParser;
21
22TemplatesTextEditor::TemplatesTextEditor(QWidget *parent)
23 : TextCustomEditor::PlainTextEditor(parent)
24{
25 setFocus();
27 setFont(f);
28 QStringList excludeKeyWord;
29 const QStringList lst = TemplateParser::Util::keywords();
30 excludeKeyWord.reserve(lst.count() * 2);
31 for (QString str : lst) {
32 excludeKeyWord << str.remove(QLatin1Char('%'));
33 excludeKeyWord << str.replace(QLatin1StringView("\\("), QLatin1StringView("("));
34 }
35 addIgnoreWords(excludeKeyWord);
36 setWordWrapMode(QTextOption::NoWrap);
37 initCompleter();
38 createHighlighter();
39}
40
41TemplatesTextEditor::~TemplatesTextEditor() = default;
42
43void TemplatesTextEditor::updateHighLighter()
44{
45 auto hlighter = dynamic_cast<TextCustomEditor::PlainTextSyntaxSpellCheckingHighlighter *>(highlighter());
46 if (hlighter) {
47 hlighter->toggleSpellHighlighting(checkSpellingEnabled());
48 }
49}
50
51void TemplatesTextEditor::clearDecorator()
52{
53 // Nothing
54}
55
56void TemplatesTextEditor::createHighlighter()
57{
58 auto highlighter = new TextCustomEditor::PlainTextSyntaxSpellCheckingHighlighter(this);
59 highlighter->toggleSpellHighlighting(checkSpellingEnabled());
60 highlighter->setCurrentLanguage(spellCheckingLanguage());
61 highlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("KMail Template")));
62 highlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
63 : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
64 setHighlighter(highlighter);
65}
66
67void TemplatesTextEditor::initCompleter()
68{
69 QStringList listWord;
70 QStringList excludeKeyWord;
71 const QStringList lst = TemplateParser::Util::keywords();
72 excludeKeyWord.reserve(lst.count());
73 for (QString str : lst) {
74 excludeKeyWord << str.replace(QLatin1StringView("\\("), QLatin1StringView("("));
75 }
76 listWord << excludeKeyWord;
77 listWord << Util::keywordsWithArgs();
78
79 mTextEditorCompleter = new TextCustomEditor::TextEditorCompleter(this, this);
80 mTextEditorCompleter->setCompleterStringList(listWord);
81 mTextEditorCompleter->setExcludeOfCharacters(QStringLiteral("~!@#$^&*()+{}|\"<>,./;'[]\\-= "));
82}
83
84void TemplatesTextEditor::keyPressEvent(QKeyEvent *e)
85{
86 if (mTextEditorCompleter->completer()->popup()->isVisible()) {
87 switch (e->key()) {
88 case Qt::Key_Enter:
89 case Qt::Key_Return:
90 case Qt::Key_Escape:
91 case Qt::Key_Tab:
92 case Qt::Key_Backtab:
93 e->ignore();
94 return; // let the completer do default behavior
95 default:
96 break;
97 }
98 }
99 TextCustomEditor::PlainTextEditor::keyPressEvent(e);
100 mTextEditorCompleter->completeText();
101}
102
103#include "moc_templatestexteditor.cpp"
Q_INVOKABLE KSyntaxHighlighting::Theme defaultTheme(DefaultTheme t=LightTheme) const
Q_INVOKABLE KSyntaxHighlighting::Definition definitionForName(const QString &defName) const
void ignore()
QFont systemFont(SystemFont type)
int key() const const
qsizetype count() const const
void remove(qsizetype i, qsizetype n)
void replace(qsizetype i, parameter_type value)
void reserve(qsizetype size)
Key_Enter
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.