KPimTextEdit

inserthtmleditor.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "inserthtmleditor.h"
9#include "kpimtextedit_debug.h"
10#include <TextCustomEditor/TextEditorCompleter>
11
12#include <KSyntaxHighlighting/Definition>
13#include <KSyntaxHighlighting/SyntaxHighlighter>
14#include <KSyntaxHighlighting/Theme>
15
16#include <QAbstractItemView>
17#include <QCompleter>
18#include <QStringList>
19
20using namespace KPIMTextEdit;
21
22InsertHtmlEditor::InsertHtmlEditor(QWidget *parent)
23 : TextCustomEditor::PlainTextEditor(parent)
24 , mTextEditorCompleter(new TextCustomEditor::TextEditorCompleter(this, this))
25{
26 const KSyntaxHighlighting::Definition def = mRepo.definitionForName(QStringLiteral("HTML"));
27 if (!def.isValid()) {
28 qCWarning(KPIMTEXTEDIT_LOG) << "Invalid definition name";
29 }
30
31 auto hl = new KSyntaxHighlighting::SyntaxHighlighter(document());
32 hl->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
34 hl->setDefinition(def);
35 setFocus();
36 const QStringList completerList = {QStringLiteral("<b></b>"), QStringLiteral("<i></i>"), QStringLiteral("<u></u>")};
37 // Add more
38 mTextEditorCompleter->setCompleterStringList(completerList);
39 mTextEditorCompleter->setExcludeOfCharacters(QStringLiteral("~!@#$%^&*()+{}|,./;'[]\\-= "));
40}
41
42InsertHtmlEditor::~InsertHtmlEditor() = default;
43
44void InsertHtmlEditor::keyPressEvent(QKeyEvent *e)
45{
46 if (mTextEditorCompleter->completer()->popup()->isVisible()) {
47 switch (e->key()) {
48 case Qt::Key_Enter:
49 case Qt::Key_Return:
50 case Qt::Key_Escape:
51 case Qt::Key_Tab:
52 case Qt::Key_Backtab:
53 e->ignore();
54 return; // let the completer do default behavior
55 default:
56 break;
57 }
58 }
60 mTextEditorCompleter->completeText();
61}
62
63#include "moc_inserthtmleditor.cpp"
void setTheme(const Theme &theme) override
virtual void keyPressEvent(QKeyEvent *e) override
void ignore()
int key() const const
Key_Enter
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.