KTextAddons

selectspecialchardialog.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "selectspecialchardialog.h"
8#include <KCharSelect>
9#include <KConfigGroup>
10#include <KLocalizedString>
11#include <KSharedConfig>
12#include <KWindowConfig>
13#include <QDialogButtonBox>
14#include <QPushButton>
15#include <QVBoxLayout>
16#include <QWindow>
17namespace
18{
19static const char mySelectSpecialCharDialogConfigGroupName[] = "SelectSpecialCharDialog";
20}
21
22namespace TextAddonsWidgets
23{
24class SelectSpecialCharDialogPrivate
25{
26public:
27 explicit SelectSpecialCharDialogPrivate(SelectSpecialCharDialog *qq)
28 : q(qq)
29 , mCharSelect(new KCharSelect(q, nullptr, KCharSelect::CharacterTable | KCharSelect::BlockCombos))
31 {
32 q->setWindowTitle(i18nc("@title:window", "Select Special Characters"));
33
34 auto lay = new QVBoxLayout(q);
35
36 q->connect(mCharSelect, &KCharSelect::charSelected, q, &SelectSpecialCharDialog::charSelected);
37 lay->addWidget(mCharSelect);
38
39 QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
40 okButton->setText(i18nc("@action:button", "Insert"));
42 lay->addWidget(mButtonBox);
45
46 q->connect(okButton, &QPushButton::clicked, q, [this]() {
47 _k_slotInsertChar();
48 });
49 }
50
51 void addSelectButton()
52 {
53 mSelectButton = new QPushButton(i18nc("@action:button", "Select"), q);
54 mButtonBox->addButton(mSelectButton, QDialogButtonBox::ActionRole);
55 q->connect(mSelectButton, &QPushButton::clicked, q, [this]() {
56 _k_slotInsertChar();
57 });
58 }
59
60 void _k_slotInsertChar();
61 void readConfig();
62 void writeConfig();
63
64 SelectSpecialCharDialog *const q;
65 KCharSelect *const mCharSelect;
66 QDialogButtonBox *const mButtonBox;
67 QPushButton *mSelectButton = nullptr;
68};
69
70void SelectSpecialCharDialogPrivate::readConfig()
71{
72 q->create(); // ensure a window is created
73 q->windowHandle()->resize(QSize(300, 200));
74 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectSpecialCharDialogConfigGroupName));
76 q->resize(q->windowHandle()->size()); // workaround for QTBUG-40584
77}
78
79void SelectSpecialCharDialogPrivate::writeConfig()
80{
81 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectSpecialCharDialogConfigGroupName));
83}
84
85void SelectSpecialCharDialogPrivate::_k_slotInsertChar()
86{
87 Q_EMIT q->charSelected(mCharSelect->currentChar());
88}
89
90SelectSpecialCharDialog::SelectSpecialCharDialog(QWidget *parent)
91 : QDialog(parent)
92 , d(new SelectSpecialCharDialogPrivate(this))
93{
94 d->readConfig();
95}
96
97SelectSpecialCharDialog::~SelectSpecialCharDialog()
98{
99 d->writeConfig();
100}
101
102void SelectSpecialCharDialog::showSelectButton(bool show)
103{
104 if (show) {
105 d->addSelectButton();
106 } else {
107 d->mButtonBox->removeButton(d->mSelectButton);
108 }
109}
110
111void SelectSpecialCharDialog::setCurrentChar(QChar c)
112{
113 d->mCharSelect->setCurrentChar(c);
114}
115
116QChar SelectSpecialCharDialog::currentChar() const
117{
118 return d->mCharSelect->currentChar();
119}
120
121void SelectSpecialCharDialog::autoInsertChar()
122{
123 connect(d->mCharSelect, &KCharSelect::charSelected, this, &SelectSpecialCharDialog::accept);
124}
125
126void SelectSpecialCharDialog::setOkButtonText(const QString &text)
127{
128 d->mButtonBox->button(QDialogButtonBox::Ok)->setText(text);
129}
130}
131
132#include "moc_selectspecialchardialog.cpp"
QChar currentChar() const
void charSelected(const QChar &c)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
void setText(const QString &text)
virtual void accept()
virtual void reject()
QPushButton * addButton(StandardButton button)
QPushButton * button(StandardButton which) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(const QSize &)
QWindow * windowHandle() const const
void setWindowTitle(const QString &)
void resize(const QSize &newSize)
virtual QSize size() const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:51:28 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.