1 /*
2 SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6 */
7
8 #include "inserthtmldialog.h"
9 #include "inserthtmleditor.h"
10 #include <KLocalizedString>
11
12 #include "texteditor/plaintexteditor/plaintexteditorwidget.h"
13
14 #include <KConfigGroup>
15 #include <KSharedConfig>
16 #include <QDialogButtonBox>
17 #include <QLabel>
18 #include <QPushButton>
19 #include <QVBoxLayout>
20
21 namespace KPIMTextEdit
22 {
23 class InsertHtmlDialogPrivate
24 {
25 public:
InsertHtmlDialogPrivate(InsertHtmlDialog * qq)26 InsertHtmlDialogPrivate(InsertHtmlDialog *qq)
27 : q(qq)
28 {
29 q->setWindowTitle(i18nc("@title:window", "Insert HTML"));
30 auto lay = new QVBoxLayout(q);
31 QLabel *label = new QLabel(i18n("Insert HTML tags and texts:"));
32 lay->addWidget(label);
33 editor = new InsertHtmlEditor;
34 editor->setSpellCheckingSupport(false);
35 auto editorWidget = new KPIMTextEdit::PlainTextEditorWidget(editor);
36 lay->addWidget(editorWidget);
37 label = new QLabel(i18n("Example: <i> Hello word </i>"));
38 QFont font = label->font();
39 font.setBold(true);
40 label->setFont(font);
41 label->setTextFormat(Qt::PlainText);
42 lay->addWidget(label);
43 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
44 okButton = buttonBox->button(QDialogButtonBox::Ok);
45 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
46 okButton->setText(i18nc("@action:button", "Insert"));
47
48 q->connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
49 q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
50
51 lay->addWidget(buttonBox);
52 q->connect(editor, &InsertHtmlEditor::textChanged, q, [this]() {
53 _k_slotTextChanged();
54 });
55 okButton->setEnabled(false);
56 }
57
58 void _k_slotTextChanged();
59 QPushButton *okButton = nullptr;
60 InsertHtmlEditor *editor = nullptr;
61 InsertHtmlDialog *const q;
62 };
63
_k_slotTextChanged()64 void InsertHtmlDialogPrivate::_k_slotTextChanged()
65 {
66 okButton->setEnabled(!editor->document()->isEmpty());
67 }
68
InsertHtmlDialog(QWidget * parent)69 InsertHtmlDialog::InsertHtmlDialog(QWidget *parent)
70 : QDialog(parent)
71 , d(new InsertHtmlDialogPrivate(this))
72 {
73 readConfig();
74 }
75
~InsertHtmlDialog()76 InsertHtmlDialog::~InsertHtmlDialog()
77 {
78 writeConfig();
79 }
80
setSelectedText(const QString & str)81 void InsertHtmlDialog::setSelectedText(const QString &str)
82 {
83 d->editor->setPlainText(str);
84 }
85
html() const86 QString InsertHtmlDialog::html() const
87 {
88 return d->editor->toPlainText();
89 }
90
readConfig()91 void InsertHtmlDialog::readConfig()
92 {
93 KConfigGroup group(KSharedConfig::openStateConfig(), "InsertHtmlDialog");
94 const QSize sizeDialog = group.readEntry("Size", QSize(640, 480));
95 if (sizeDialog.isValid()) {
96 resize(sizeDialog);
97 }
98 }
99
writeConfig()100 void InsertHtmlDialog::writeConfig()
101 {
102 KConfigGroup group(KSharedConfig::openStateConfig(), "InsertHtmlDialog");
103 group.writeEntry("Size", size());
104 }
105 }
106
107 #include "moc_inserthtmldialog.cpp"
108