File indexing completed on 2025-02-16 04:57:43
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "templatestexteditor.h" 0008 #include "templatesutil_p.h" 0009 0010 #include <TextCustomEditor/PlainTextSyntaxSpellCheckingHighlighter> 0011 #include <TextCustomEditor/TextEditorCompleter> 0012 0013 #include <KSyntaxHighlighting/Definition> 0014 #include <KSyntaxHighlighting/Theme> 0015 0016 #include <QAbstractItemView> 0017 #include <QCompleter> 0018 #include <QFontDatabase> 0019 #include <QKeyEvent> 0020 using namespace TemplateParser; 0021 0022 TemplatesTextEditor::TemplatesTextEditor(QWidget *parent) 0023 : TextCustomEditor::PlainTextEditor(parent) 0024 { 0025 setFocus(); 0026 const QFont f = QFontDatabase::systemFont(QFontDatabase::FixedFont); 0027 setFont(f); 0028 QStringList excludeKeyWord; 0029 const QStringList lst = TemplateParser::Util::keywords(); 0030 excludeKeyWord.reserve(lst.count() * 2); 0031 for (QString str : lst) { 0032 excludeKeyWord << str.remove(QLatin1Char('%')); 0033 excludeKeyWord << str.replace(QLatin1StringView("\\("), QLatin1StringView("(")); 0034 } 0035 addIgnoreWords(excludeKeyWord); 0036 setWordWrapMode(QTextOption::NoWrap); 0037 initCompleter(); 0038 createHighlighter(); 0039 } 0040 0041 TemplatesTextEditor::~TemplatesTextEditor() = default; 0042 0043 void TemplatesTextEditor::updateHighLighter() 0044 { 0045 auto hlighter = dynamic_cast<TextCustomEditor::PlainTextSyntaxSpellCheckingHighlighter *>(highlighter()); 0046 if (hlighter) { 0047 hlighter->toggleSpellHighlighting(checkSpellingEnabled()); 0048 } 0049 } 0050 0051 void TemplatesTextEditor::clearDecorator() 0052 { 0053 // Nothing 0054 } 0055 0056 void TemplatesTextEditor::createHighlighter() 0057 { 0058 auto highlighter = new TextCustomEditor::PlainTextSyntaxSpellCheckingHighlighter(this); 0059 highlighter->toggleSpellHighlighting(checkSpellingEnabled()); 0060 highlighter->setCurrentLanguage(spellCheckingLanguage()); 0061 highlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("KMail Template"))); 0062 highlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) 0063 : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme)); 0064 setHighlighter(highlighter); 0065 } 0066 0067 void TemplatesTextEditor::initCompleter() 0068 { 0069 QStringList listWord; 0070 QStringList excludeKeyWord; 0071 const QStringList lst = TemplateParser::Util::keywords(); 0072 excludeKeyWord.reserve(lst.count()); 0073 for (QString str : lst) { 0074 excludeKeyWord << str.replace(QLatin1StringView("\\("), QLatin1StringView("(")); 0075 } 0076 listWord << excludeKeyWord; 0077 listWord << Util::keywordsWithArgs(); 0078 0079 mTextEditorCompleter = new TextCustomEditor::TextEditorCompleter(this, this); 0080 mTextEditorCompleter->setCompleterStringList(listWord); 0081 mTextEditorCompleter->setExcludeOfCharacters(QStringLiteral("~!@#$^&*()+{}|\"<>,./;'[]\\-= ")); 0082 } 0083 0084 void TemplatesTextEditor::keyPressEvent(QKeyEvent *e) 0085 { 0086 if (mTextEditorCompleter->completer()->popup()->isVisible()) { 0087 switch (e->key()) { 0088 case Qt::Key_Enter: 0089 case Qt::Key_Return: 0090 case Qt::Key_Escape: 0091 case Qt::Key_Tab: 0092 case Qt::Key_Backtab: 0093 e->ignore(); 0094 return; // let the completer do default behavior 0095 default: 0096 break; 0097 } 0098 } 0099 TextCustomEditor::PlainTextEditor::keyPressEvent(e); 0100 mTextEditorCompleter->completeText(); 0101 } 0102 0103 #include "moc_templatestexteditor.cpp"