File: selectspecialchardialog.cpp

package info (click to toggle)
ktextaddons 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,360 kB
  • sloc: cpp: 54,647; ansic: 6,591; xml: 2,630; sh: 11; makefile: 7
file content (134 lines) | stat: -rw-r--r-- 3,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
  SPDX-FileCopyrightText: 2012-2025 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "selectspecialchardialog.h"
using namespace Qt::Literals::StringLiterals;

#include <KCharSelect>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include <KWindowConfig>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWindow>
namespace
{
const char mySelectSpecialCharDialogConfigGroupName[] = "SelectSpecialCharDialog";
}

namespace TextAddonsWidgets
{
class SelectSpecialCharDialogPrivate
{
public:
    explicit SelectSpecialCharDialogPrivate(SelectSpecialCharDialog *qq)
        : q(qq)
        , mCharSelect(new KCharSelect(q, nullptr, KCharSelect::CharacterTable | KCharSelect::BlockCombos))
        , mButtonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q))
    {
        q->setWindowTitle(i18nc("@title:window", "Select Special Characters"));

        auto lay = new QVBoxLayout(q);

        q->connect(mCharSelect, &KCharSelect::charSelected, q, &SelectSpecialCharDialog::charSelected);
        lay->addWidget(mCharSelect);

        QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
        okButton->setText(i18nc("@action:button", "Insert"));
        okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
        lay->addWidget(mButtonBox);
        q->connect(mButtonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
        q->connect(mButtonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);

        q->connect(okButton, &QPushButton::clicked, q, [this]() {
            _k_slotInsertChar();
        });
    }

    void addSelectButton()
    {
        mSelectButton = new QPushButton(i18nc("@action:button", "Select"), q);
        mButtonBox->addButton(mSelectButton, QDialogButtonBox::ActionRole);
        q->connect(mSelectButton, &QPushButton::clicked, q, [this]() {
            _k_slotInsertChar();
        });
    }

    void _k_slotInsertChar();
    void readConfig();
    void writeConfig();

    SelectSpecialCharDialog *const q;
    KCharSelect *const mCharSelect;
    QDialogButtonBox *const mButtonBox;
    QPushButton *mSelectButton = nullptr;
};

void SelectSpecialCharDialogPrivate::readConfig()
{
    q->create(); // ensure a window is created
    q->windowHandle()->resize(QSize(300, 200));
    const KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectSpecialCharDialogConfigGroupName));
    KWindowConfig::restoreWindowSize(q->windowHandle(), group);
    q->resize(q->windowHandle()->size()); // workaround for QTBUG-40584
}

void SelectSpecialCharDialogPrivate::writeConfig()
{
    KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectSpecialCharDialogConfigGroupName));
    KWindowConfig::saveWindowSize(q->windowHandle(), group);
}

void SelectSpecialCharDialogPrivate::_k_slotInsertChar()
{
    Q_EMIT q->charSelected(mCharSelect->currentChar());
}

SelectSpecialCharDialog::SelectSpecialCharDialog(QWidget *parent)
    : QDialog(parent)
    , d(new SelectSpecialCharDialogPrivate(this))
{
    d->readConfig();
}

SelectSpecialCharDialog::~SelectSpecialCharDialog()
{
    d->writeConfig();
}

void SelectSpecialCharDialog::showSelectButton(bool show)
{
    if (show) {
        d->addSelectButton();
    } else {
        d->mButtonBox->removeButton(d->mSelectButton);
    }
}

void SelectSpecialCharDialog::setCurrentChar(QChar c)
{
    d->mCharSelect->setCurrentChar(c);
}

QChar SelectSpecialCharDialog::currentChar() const
{
    return d->mCharSelect->currentChar();
}

void SelectSpecialCharDialog::autoInsertChar()
{
    connect(d->mCharSelect, &KCharSelect::charSelected, this, &SelectSpecialCharDialog::accept);
}

void SelectSpecialCharDialog::setOkButtonText(const QString &text)
{
    d->mButtonBox->button(QDialogButtonBox::Ok)->setText(text);
}
}

#include "moc_selectspecialchardialog.cpp"