File indexing completed on 2024-05-12 05:13:30

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "grantleeplaintexteditor.h"
0007 #include "grantleethemeeditor_debug.h"
0008 
0009 #include <KSyntaxHighlighting/Definition>
0010 #include <KSyntaxHighlighting/SyntaxHighlighter>
0011 #include <KSyntaxHighlighting/Theme>
0012 
0013 #include <QAbstractItemView>
0014 #include <QCompleter>
0015 #include <QKeyEvent>
0016 #include <TextCustomEditor/TextEditorCompleter>
0017 
0018 using namespace GrantleeThemeEditor;
0019 
0020 GrantleePlainTextEditor::GrantleePlainTextEditor(QWidget *parent)
0021     : TextCustomEditor::PlainTextEditor(parent)
0022 {
0023     const KSyntaxHighlighting::Definition def = mRepo.definitionForName(QStringLiteral("HTML"));
0024     if (!def.isValid()) {
0025         qCWarning(GRANTLEETHEMEEDITOR_LOG) << "Invalid definition name";
0026     }
0027 
0028     auto hl = new KSyntaxHighlighting::SyntaxHighlighter(document());
0029     hl->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
0030                                                                      : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
0031     hl->setDefinition(def);
0032     setSpellCheckingSupport(false);
0033     initCompleter();
0034 }
0035 
0036 GrantleePlainTextEditor::~GrantleePlainTextEditor() = default;
0037 
0038 void GrantleePlainTextEditor::initCompleter()
0039 {
0040     mTextEditorCompleter = new TextCustomEditor::TextEditorCompleter(this, this);
0041 }
0042 
0043 void GrantleePlainTextEditor::createCompleterList(const QStringList &extraCompletion)
0044 {
0045     mTextEditorCompleter->setCompleterStringList(extraCompletion);
0046 }
0047 
0048 void GrantleePlainTextEditor::keyPressEvent(QKeyEvent *e)
0049 {
0050     if (mTextEditorCompleter->completer()->popup()->isVisible()) {
0051         switch (e->key()) {
0052         case Qt::Key_Enter:
0053         case Qt::Key_Return:
0054         case Qt::Key_Escape:
0055         case Qt::Key_Tab:
0056         case Qt::Key_Backtab:
0057             e->ignore();
0058             return; // let the completer do default behavior
0059         default:
0060             break;
0061         }
0062     }
0063     TextCustomEditor::PlainTextEditor::keyPressEvent(e);
0064     mTextEditorCompleter->completeText();
0065 }
0066 
0067 #include "moc_grantleeplaintexteditor.cpp"