Messagelib

templatestexteditor.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "templatestexteditor.h"
8 #include "templatesutil_p.h"
9 
10 #include <KPIMTextEdit/PlainTextSyntaxSpellCheckingHighlighter>
11 #include <KPIMTextEdit/TextEditorCompleter>
12 
13 #include <KSyntaxHighlighting/Definition>
14 #include <KSyntaxHighlighting/Theme>
15 
16 #include <QAbstractItemView>
17 #include <QCompleter>
18 #include <QFontDatabase>
19 #include <QKeyEvent>
20 using namespace TemplateParser;
21 
22 TemplatesTextEditor::TemplatesTextEditor(QWidget *parent)
23  : KPIMTextEdit::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(QLatin1String("\\("), QLatin1String("("));
34  }
35  addIgnoreWords(excludeKeyWord);
36  setWordWrapMode(QTextOption::NoWrap);
37  initCompleter();
38  createHighlighter();
39 }
40 
41 TemplatesTextEditor::~TemplatesTextEditor() = default;
42 
43 void TemplatesTextEditor::updateHighLighter()
44 {
45  auto hlighter = dynamic_cast<KPIMTextEdit::PlainTextSyntaxSpellCheckingHighlighter *>(highlighter());
46  if (hlighter) {
47  hlighter->toggleSpellHighlighting(checkSpellingEnabled());
48  }
49 }
50 
51 void TemplatesTextEditor::clearDecorator()
52 {
53  // Nothing
54 }
55 
56 void TemplatesTextEditor::createHighlighter()
57 {
58  auto highlighter = new KPIMTextEdit::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 
67 void 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(QLatin1String("\\("), QLatin1String("("));
75  }
76  listWord << excludeKeyWord;
77  listWord << Util::keywordsWithArgs();
78 
79  mTextEditorCompleter = new KPIMTextEdit::TextEditorCompleter(this, this);
80  mTextEditorCompleter->setCompleterStringList(listWord);
81  mTextEditorCompleter->setExcludeOfCharacters(QStringLiteral("[email protected]#$^&*()+{}|\"<>,./;'[]\\-= "));
82 }
83 
84 void 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  }
100  mTextEditorCompleter->completeText();
101 }
const QPalette & palette() const const
void reserve(int alloc)
int count(const T &value) const const
void ignore()
void setFocus()
virtual void keyPressEvent(QKeyEvent *e) override
int key() const const
void setFont(const QFont &)
QFont systemFont(QFontDatabase::SystemFont type)
Key_Enter
void replace(int i, const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:16:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.