KConfigWidgets

kcodecaction.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Jason Keirstead <jason@keirstead.org>
3 SPDX-FileCopyrightText: 2006 Michel Hermier <michel.hermier@gmail.com>
4 SPDX-FileCopyrightText: 2007 Nick Shaforostoff <shafff@ukr.net>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kcodecaction.h"
10#include "kconfigwidgets_debug.h"
11
12#include <KCharsets>
13#include <KEncodingProber>
14#include <KLocalizedString>
15
16#include <QMenu>
17#include <QVariant>
18
19class KCodecActionPrivate
20{
21public:
22 KCodecActionPrivate(KCodecAction *parent)
23 : q(parent)
24 {
25 }
26
27 void init(bool);
28
29 void subActionTriggered(QAction *);
30
31 KCodecAction *const q;
32 QAction *defaultAction = nullptr;
33 QAction *currentSubAction = nullptr;
34};
35
36KCodecAction::KCodecAction(QObject *parent, bool showAutoOptions)
37 : KSelectAction(parent)
38 , d(new KCodecActionPrivate(this))
39{
40 d->init(showAutoOptions);
41}
42
43KCodecAction::KCodecAction(const QString &text, QObject *parent, bool showAutoOptions)
44 : KSelectAction(text, parent)
45 , d(new KCodecActionPrivate(this))
46{
47 d->init(showAutoOptions);
48}
49
50KCodecAction::KCodecAction(const QIcon &icon, const QString &text, QObject *parent, bool showAutoOptions)
51 : KSelectAction(icon, text, parent)
52 , d(new KCodecActionPrivate(this))
53{
54 d->init(showAutoOptions);
55}
56
57KCodecAction::~KCodecAction() = default;
58
59void KCodecActionPrivate::init(bool showAutoOptions)
60{
62 defaultAction = q->addAction(i18nc("Encodings menu", "Default"));
63
64 const auto lstEncodings = KCharsets::charsets()->encodingsByScript();
65 for (const QStringList &encodingsForScript : lstEncodings) {
66 KSelectAction *tmp = new KSelectAction(encodingsForScript.at(0), q);
67 if (showAutoOptions) {
68 KEncodingProber::ProberType scri = KEncodingProber::proberTypeForName(encodingsForScript.at(0));
69 if (scri != KEncodingProber::None) {
70 tmp->addAction(i18nc("Encodings menu", "Autodetect"))->setData(QVariant((uint)scri));
71 tmp->menu()->addSeparator();
72 }
73 }
74 for (int i = 1; i < encodingsForScript.size(); ++i) {
75 tmp->addAction(encodingsForScript.at(i));
76 }
77 q->connect(tmp, &KSelectAction::actionTriggered, q, [this](QAction *action) {
78 subActionTriggered(action);
79 });
80 tmp->setCheckable(true);
81 q->addAction(tmp);
82 }
83 q->setCurrentItem(0);
84}
85
86void KCodecAction::slotActionTriggered(QAction *action)
87{
88 // we don't want to emit any signals from top-level items
89 // except for the default one
90 if (action == d->defaultAction) {
92 }
93}
94
95void KCodecActionPrivate::subActionTriggered(QAction *action)
96{
97 if (currentSubAction == action) {
98 return;
99 }
100 currentSubAction = action;
101 Q_EMIT q->textTriggered(action->text());
102 Q_EMIT q->codecNameTriggered(action->text().toUtf8());
103}
104
105QString KCodecAction::currentCodecName() const
106{
107 return d->currentSubAction->text();
108}
109
110bool KCodecAction::setCurrentCodec(const QString &codecName)
111{
112 if (codecName.isEmpty()) {
113 return false;
114 }
115
116 for (int i = 0; i < actions().size(); ++i) {
117 if (actions().at(i)->menu()) {
118 for (int j = 0; j < actions().at(i)->menu()->actions().size(); ++j) {
119 if (!j && !actions().at(i)->menu()->actions().at(j)->data().isNull()) {
120 continue;
121 }
122 if (codecName == actions().at(i)->menu()->actions().at(j)->text()) {
123 d->currentSubAction = actions().at(i)->menu()->actions().at(j);
124 d->currentSubAction->trigger();
125 return true;
126 }
127 }
128 }
129 }
130 return false;
131}
132
133#include "moc_kcodecaction.cpp"
QList< QStringList > encodingsByScript() const
static KCharsets * charsets()
Action for selecting one of several text codecs.
void defaultItemTriggered()
Emitted when the 'Default' codec action is triggered.
void codecNameTriggered(const QByteArray &name)
Emitted when a codec was selected.
static ProberType proberTypeForName(const QString &lang)
QAction * addAction(const QIcon &icon, const QString &text)
QList< QAction * > actions() const
void setToolBarMode(ToolBarMode mode)
void textTriggered(const QString &text)
QAction * action(const QString &text, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
void actionTriggered(QAction *action)
bool setCurrentItem(int index)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setCheckable(bool)
QVariant data() const const
QMenu * menu() const const
void setData(const QVariant &data)
const_reference at(qsizetype i) const const
qsizetype size() const const
QAction * addSeparator()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:18:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.