File indexing completed on 2024-05-05 05:21:40

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