KWidgetsAddons

kdualaction.cpp
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
22 slotTriggered();
23 });
24}
25
26void KDualActionPrivate::updateFromCurrentState()
27{
28 KGuiItem &currentItem = item(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(this);
52 d->item(false).setText(inactiveText);
53 d->item(true).setText(activeText);
54 d->updateFromCurrentState();
55}
56
58 : QAction(parent)
59 , d(new KDualActionPrivate)
60{
61 d->init(this);
62}
63
64KDualAction::~KDualAction() = default;
65
67{
68 d->setGuiItem(true, item);
69}
71{
72 return d->item(true);
73}
75{
76 d->setGuiItem(false, item);
77}
79{
80 return d->item(false);
81}
82
84{
85 d->setIcon(true, icon);
86}
88{
89 return d->item(true).icon();
90}
92{
93 d->setIcon(false, icon);
94}
96{
97 return d->item(false).icon();
98}
99
101{
102 d->setText(true, text);
103}
105{
106 return d->item(true).text();
107}
109{
110 d->setText(false, text);
111}
113{
114 return d->item(false).text();
115}
116
118{
119 d->setToolTip(true, toolTip);
120}
122{
123 return d->item(true).toolTip();
124}
126{
127 d->setToolTip(false, toolTip);
128}
130{
131 return d->item(false).toolTip();
132}
133
139
141{
142 d->autoToggle = value;
143}
144
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
161{
162 return d->isActive;
163}
164
165#include "moc_kdualaction.cpp"
An action which can alternate between two texts/icons when triggered.
Definition kdualaction.h:51
KDualAction(QObject *parent)
Constructs a KDualAction with the specified parent.
void setActiveGuiItem(const KGuiItem &)
Sets the KGuiItem for the active state.
void setActiveIcon(const QIcon &)
Sets the icon for the active state.
QIcon activeIcon() const
Gets the icon for the active state.
void setActiveText(const QString &)
Sets the text for the active state.
void setInactiveText(const QString &)
Sets the text for the inactive state.
void setIconForStates(const QIcon &icon)
Convenience method to set the icon for both active and inactive states.
QIcon inactiveIcon() const
Gets the icon for the inactive state.
KGuiItem inactiveGuiItem() const
Gets the KGuiItem for the inactive state.
void setInactiveIcon(const QIcon &)
Sets the icon for the inactive state.
QString inactiveText() const
Gets the text for the inactive state.
bool isActive() const
Returns the action state.
QString activeText() const
Gets the text for the active state.
void setInactiveGuiItem(const KGuiItem &)
Sets the KGuiItem for the inactive state.
void activeChanged(bool)
Emitted when the state changes.
void setActiveToolTip(const QString &)
Sets the tooltip for the active state.
void setActive(bool state)
Sets the action state.
void setInactiveToolTip(const QString &)
Sets the tooltip for the inactive state.
QString inactiveToolTip() const
Gets the tooltip for the inactive state.
KGuiItem activeGuiItem() const
Gets the KGuiItem for the active state.
bool autoToggle() const
Returns whether the current action will automatically be changed when the user triggers this action.
void setAutoToggle(bool)
Defines whether the current action should automatically be changed when the user triggers this action...
QString activeToolTip() const
Gets the tooltip for the active state.
An abstract class for setting the text, icon, tooltip and WhatsThis data on a GUI item (e....
Definition kguiitem.h:34
QIcon icon() const
Returns the icon used by this GUI item.
Definition kguiitem.cpp:101
QString toolTip() const
Returns the tooltip used for this GUI item.
Definition kguiitem.cpp:118
QString text() const
Returns the text used by this GUI item.
Definition kguiitem.cpp:68
void setIcon(const QIcon &icon)
void setText(const QString &text)
void setToolTip(const QString &tip)
void triggered(bool checked)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.