File indexing completed on 2024-05-19 04:44:49

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "selectspecialchardialog.h"
0008 #include <KCharSelect>
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KWindowConfig>
0013 #include <QDialogButtonBox>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 #include <QWindow>
0017 namespace
0018 {
0019 static const char mySelectSpecialCharDialogConfigGroupName[] = "SelectSpecialCharDialog";
0020 }
0021 
0022 namespace TextAddonsWidgets
0023 {
0024 class SelectSpecialCharDialogPrivate
0025 {
0026 public:
0027     explicit SelectSpecialCharDialogPrivate(SelectSpecialCharDialog *qq)
0028         : q(qq)
0029         , mCharSelect(new KCharSelect(q, nullptr, KCharSelect::CharacterTable | KCharSelect::BlockCombos))
0030         , mButtonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q))
0031     {
0032         q->setWindowTitle(i18nc("@title:window", "Select Special Characters"));
0033 
0034         auto lay = new QVBoxLayout(q);
0035 
0036         q->connect(mCharSelect, &KCharSelect::charSelected, q, &SelectSpecialCharDialog::charSelected);
0037         lay->addWidget(mCharSelect);
0038 
0039         QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
0040         okButton->setText(i18n("Insert"));
0041         okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0042         lay->addWidget(mButtonBox);
0043         q->connect(mButtonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0044         q->connect(mButtonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0045 
0046         q->connect(okButton, &QPushButton::clicked, q, [this]() {
0047             _k_slotInsertChar();
0048         });
0049     }
0050 
0051     void addSelectButton()
0052     {
0053         mSelectButton = new QPushButton(i18n("Select"));
0054         mButtonBox->addButton(mSelectButton, QDialogButtonBox::ActionRole);
0055         q->connect(mSelectButton, &QPushButton::clicked, q, [this]() {
0056             _k_slotInsertChar();
0057         });
0058     }
0059 
0060     void _k_slotInsertChar();
0061     void readConfig();
0062     void writeConfig();
0063 
0064     SelectSpecialCharDialog *const q;
0065     KCharSelect *const mCharSelect;
0066     QDialogButtonBox *const mButtonBox;
0067     QPushButton *mSelectButton = nullptr;
0068 };
0069 
0070 void SelectSpecialCharDialogPrivate::readConfig()
0071 {
0072     q->create(); // ensure a window is created
0073     q->windowHandle()->resize(QSize(300, 200));
0074     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(mySelectSpecialCharDialogConfigGroupName));
0075     KWindowConfig::restoreWindowSize(q->windowHandle(), group);
0076     q->resize(q->windowHandle()->size()); // workaround for QTBUG-40584
0077 }
0078 
0079 void SelectSpecialCharDialogPrivate::writeConfig()
0080 {
0081     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(mySelectSpecialCharDialogConfigGroupName));
0082     KWindowConfig::saveWindowSize(q->windowHandle(), group);
0083 }
0084 
0085 void SelectSpecialCharDialogPrivate::_k_slotInsertChar()
0086 {
0087     Q_EMIT q->charSelected(mCharSelect->currentChar());
0088 }
0089 
0090 SelectSpecialCharDialog::SelectSpecialCharDialog(QWidget *parent)
0091     : QDialog(parent)
0092     , d(new SelectSpecialCharDialogPrivate(this))
0093 {
0094     d->readConfig();
0095 }
0096 
0097 SelectSpecialCharDialog::~SelectSpecialCharDialog()
0098 {
0099     d->writeConfig();
0100 }
0101 
0102 void SelectSpecialCharDialog::showSelectButton(bool show)
0103 {
0104     if (show) {
0105         d->addSelectButton();
0106     } else {
0107         d->mButtonBox->removeButton(d->mSelectButton);
0108     }
0109 }
0110 
0111 void SelectSpecialCharDialog::setCurrentChar(QChar c)
0112 {
0113     d->mCharSelect->setCurrentChar(c);
0114 }
0115 
0116 QChar SelectSpecialCharDialog::currentChar() const
0117 {
0118     return d->mCharSelect->currentChar();
0119 }
0120 
0121 void SelectSpecialCharDialog::autoInsertChar()
0122 {
0123     connect(d->mCharSelect, &KCharSelect::charSelected, this, &SelectSpecialCharDialog::accept);
0124 }
0125 
0126 void SelectSpecialCharDialog::setOkButtonText(const QString &text)
0127 {
0128     d->mButtonBox->button(QDialogButtonBox::Ok)->setText(text);
0129 }
0130 }
0131 
0132 #include "moc_selectspecialchardialog.cpp"