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

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "inserthtmldialog.h"
0009 #include "inserthtmleditor.h"
0010 #include <KLocalizedString>
0011 
0012 #include <TextCustomEditor/PlainTextEditorWidget>
0013 
0014 #include <KConfigGroup>
0015 #include <KSharedConfig>
0016 #include <KWindowConfig>
0017 #include <QDialogButtonBox>
0018 #include <QLabel>
0019 #include <QPushButton>
0020 #include <QVBoxLayout>
0021 #include <QWindow>
0022 namespace
0023 {
0024 static const char myInsertHtmlDialogConfigGroupName[] = "InsertHtmlDialog";
0025 }
0026 namespace KPIMTextEdit
0027 {
0028 class InsertHtmlDialogPrivate
0029 {
0030 public:
0031     explicit InsertHtmlDialogPrivate(InsertHtmlDialog *qq)
0032         : q(qq)
0033     {
0034         q->setWindowTitle(i18nc("@title:window", "Insert HTML"));
0035         auto lay = new QVBoxLayout(q);
0036         auto label = new QLabel(i18n("Insert HTML tags and texts:"));
0037         lay->addWidget(label);
0038         editor = new InsertHtmlEditor;
0039         editor->setSpellCheckingSupport(false);
0040         auto editorWidget = new TextCustomEditor::PlainTextEditorWidget(editor);
0041         lay->addWidget(editorWidget);
0042         label = new QLabel(i18n("Example: <i> Hello word </i>"));
0043         QFont font = label->font();
0044         font.setBold(true);
0045         label->setFont(font);
0046         label->setTextFormat(Qt::PlainText);
0047         lay->addWidget(label);
0048         auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
0049         okButton = buttonBox->button(QDialogButtonBox::Ok);
0050         okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0051         okButton->setText(i18nc("@action:button", "Insert"));
0052 
0053         q->connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0054         q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0055 
0056         lay->addWidget(buttonBox);
0057         q->connect(editor, &InsertHtmlEditor::textChanged, q, [this]() {
0058             _k_slotTextChanged();
0059         });
0060         okButton->setEnabled(false);
0061     }
0062 
0063     void _k_slotTextChanged();
0064     QPushButton *okButton = nullptr;
0065     InsertHtmlEditor *editor = nullptr;
0066     InsertHtmlDialog *const q;
0067 };
0068 
0069 void InsertHtmlDialogPrivate::_k_slotTextChanged()
0070 {
0071     okButton->setEnabled(!editor->document()->isEmpty());
0072 }
0073 
0074 InsertHtmlDialog::InsertHtmlDialog(QWidget *parent)
0075     : QDialog(parent)
0076     , d(new InsertHtmlDialogPrivate(this))
0077 {
0078     readConfig();
0079 }
0080 
0081 InsertHtmlDialog::~InsertHtmlDialog()
0082 {
0083     writeConfig();
0084 }
0085 
0086 void InsertHtmlDialog::setSelectedText(const QString &str)
0087 {
0088     d->editor->setPlainText(str);
0089 }
0090 
0091 QString InsertHtmlDialog::html() const
0092 {
0093     return d->editor->toPlainText();
0094 }
0095 
0096 void InsertHtmlDialog::readConfig()
0097 {
0098     create(); // ensure a window is created
0099     windowHandle()->resize(QSize(640, 480));
0100     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myInsertHtmlDialogConfigGroupName));
0101     KWindowConfig::restoreWindowSize(windowHandle(), group);
0102     resize(windowHandle()->size()); // workaround for QTBUG-40584
0103 }
0104 
0105 void InsertHtmlDialog::writeConfig()
0106 {
0107     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myInsertHtmlDialogConfigGroupName));
0108     KWindowConfig::saveWindowSize(windowHandle(), group);
0109 }
0110 }
0111 
0112 #include "moc_inserthtmldialog.cpp"