1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2010 Aurélien Gâteau <agateau@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7#include "kdualaction.h"
8#include "kdualaction_p.h"
9
10#include <QIcon>
11
12//---------------------------------------------------------------------
13// KDualActionPrivate
14//---------------------------------------------------------------------
15void KDualActionPrivate::init(KDualAction *q_ptr)
16{
17 q = q_ptr;
18 autoToggle = true;
19 isActive = false;
20
21 QObject::connect(sender: q, signal: &KDualAction::triggered, context: q, slot: [this]() {
22 slotTriggered();
23 });
24}
25
26void KDualActionPrivate::updateFromCurrentState()
27{
28 KGuiItem &currentItem = item(active: isActive);
29 QAction *qq = static_cast<QAction *>(q);
30 qq->setIcon(currentItem.icon());
31 qq->setText(currentItem.text());
32 qq->setToolTip(currentItem.toolTip());
33}
34
35void KDualActionPrivate::slotTriggered()
36{
37 if (!autoToggle) {
38 return;
39 }
40 q->setActive(!isActive);
41 Q_EMIT q->activeChangedByUser(isActive);
42}
43
44//---------------------------------------------------------------------
45// KDualAction
46//---------------------------------------------------------------------
47KDualAction::KDualAction(const QString &inactiveText, const QString &activeText, QObject *parent)
48 : QAction(parent)
49 , d(new KDualActionPrivate)
50{
51 d->init(q_ptr: this);
52 d->item(active: false).setText(inactiveText);
53 d->item(active: true).setText(activeText);
54 d->updateFromCurrentState();
55}
56
57KDualAction::KDualAction(QObject *parent)
58 : QAction(parent)
59 , d(new KDualActionPrivate)
60{
61 d->init(q_ptr: this);
62}
63
64KDualAction::~KDualAction() = default;
65
66void KDualAction::setActiveGuiItem(const KGuiItem &item)
67{
68 d->setGuiItem(active: true, item: item);
69}
70KGuiItem KDualAction::activeGuiItem() const
71{
72 return d->item(active: true);
73}
74void KDualAction::setInactiveGuiItem(const KGuiItem &item)
75{
76 d->setGuiItem(active: false, item: item);
77}
78KGuiItem KDualAction::inactiveGuiItem() const
79{
80 return d->item(active: false);
81}
82
83void KDualAction::setActiveIcon(const QIcon &icon)
84{
85 d->setIcon(active: true, icon);
86}
87QIcon KDualAction::activeIcon() const
88{
89 return d->item(active: true).icon();
90}
91void KDualAction::setInactiveIcon(const QIcon &icon)
92{
93 d->setIcon(active: false, icon);
94}
95QIcon KDualAction::inactiveIcon() const
96{
97 return d->item(active: false).icon();
98}
99
100void KDualAction::setActiveText(const QString &text)
101{
102 d->setText(active: true, text);
103}
104QString KDualAction::activeText() const
105{
106 return d->item(active: true).text();
107}
108void KDualAction::setInactiveText(const QString &text)
109{
110 d->setText(active: false, text);
111}
112QString KDualAction::inactiveText() const
113{
114 return d->item(active: false).text();
115}
116
117void KDualAction::setActiveToolTip(const QString &toolTip)
118{
119 d->setToolTip(active: true, toolTip);
120}
121QString KDualAction::activeToolTip() const
122{
123 return d->item(active: true).toolTip();
124}
125void KDualAction::setInactiveToolTip(const QString &toolTip)
126{
127 d->setToolTip(active: false, toolTip);
128}
129QString KDualAction::inactiveToolTip() const
130{
131 return d->item(active: false).toolTip();
132}
133
134void KDualAction::setIconForStates(const QIcon &icon)
135{
136 setInactiveIcon(icon);
137 setActiveIcon(icon);
138}
139
140void KDualAction::setAutoToggle(bool value)
141{
142 d->autoToggle = value;
143}
144
145bool KDualAction::autoToggle() const
146{
147 return d->autoToggle;
148}
149
150void KDualAction::setActive(bool active)
151{
152 if (active == d->isActive) {
153 return;
154 }
155 d->isActive = active;
156 d->updateFromCurrentState();
157 Q_EMIT activeChanged(active);
158}
159
160bool KDualAction::isActive() const
161{
162 return d->isActive;
163}
164
165#include "moc_kdualaction.cpp"
166

source code of kwidgetsaddons/src/kdualaction.cpp