KWidgetsAddons

kdualaction.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2010 Aurélien Gâteau <[email protected]>
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 //---------------------------------------------------------------------
15 void KDualActionPrivate::init(KDualAction *q_ptr)
16 {
17  q = q_ptr;
18  autoToggle = true;
19  isActive = false;
20 
21  QObject::connect(q, SIGNAL(triggered()), q, SLOT(slotTriggered()));
22 }
23 
24 void KDualActionPrivate::updateFromCurrentState()
25 {
26  KGuiItem &currentItem = item(isActive);
27  QAction *qq = static_cast<QAction *>(q);
28  qq->setIcon(currentItem.icon());
29  qq->setText(currentItem.text());
30  qq->setToolTip(currentItem.toolTip());
31 }
32 
33 void KDualActionPrivate::slotTriggered()
34 {
35  if (!autoToggle) {
36  return;
37  }
38  q->setActive(!isActive);
39  emit q->activeChangedByUser(isActive);
40 }
41 
42 //---------------------------------------------------------------------
43 // KDualAction
44 //---------------------------------------------------------------------
45 KDualAction::KDualAction(const QString &inactiveText, const QString &activeText, QObject *parent)
46  : QAction(parent)
47  , d(new KDualActionPrivate)
48 {
49  d->init(this);
50  d->item(false).setText(inactiveText);
51  d->item(true).setText(activeText);
52  d->updateFromCurrentState();
53 }
54 
56  : QAction(parent)
57  , d(new KDualActionPrivate)
58 {
59  d->init(this);
60 }
61 
62 KDualAction::~KDualAction()
63 {
64  delete d;
65 }
66 
68 {
69  d->setGuiItem(true, item);
70 }
72 {
73  return d->item(true);
74 }
76 {
77  d->setGuiItem(false, item);
78 }
80 {
81  return d->item(false);
82 }
83 
85 {
86  d->setIcon(true, icon);
87 }
89 {
90  return d->item(true).icon();
91 }
93 {
94  d->setIcon(false, icon);
95 }
97 {
98  return d->item(false).icon();
99 }
100 
102 {
103  d->setText(true, text);
104 }
106 {
107  return d->item(true).text();
108 }
110 {
111  d->setText(false, text);
112 }
114 {
115  return d->item(false).text();
116 }
117 
119 {
120  d->setToolTip(true, toolTip);
121 }
123 {
124  return d->item(true).toolTip();
125 }
127 {
128  d->setToolTip(false, toolTip);
129 }
131 {
132  return d->item(false).toolTip();
133 }
134 
136 {
137  setInactiveIcon(icon);
138  setActiveIcon(icon);
139 }
140 
142 {
143  d->autoToggle = value;
144 }
145 
147 {
148  return d->autoToggle;
149 }
150 
151 void KDualAction::setActive(bool active)
152 {
153  if (active == d->isActive) {
154  return;
155  }
156  d->isActive = active;
157  d->updateFromCurrentState();
158  emit activeChanged(active);
159 }
160 
162 {
163  return d->isActive;
164 }
165 
166 #include "moc_kdualaction.cpp"
void setText(const QString &text)
QString inactiveText() const
Gets the text for the inactive state.
bool isActive() const
Returns the action state.
void setActiveToolTip(const QString &)
Sets the tooltip for the active state.
void setInactiveText(const QString &)
Sets the text for the inactive state.
void setIcon(const QIcon &icon)
void activeChanged(bool)
Emitted when the state changes.
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.
Definition: kdualaction.cpp:96
KGuiItem activeGuiItem() const
Gets the KGuiItem for the active state.
Definition: kdualaction.cpp:71
void setActiveText(const QString &)
Sets the text for the active state.
void setToolTip(const QString &tip)
QString activeToolTip() const
Gets the tooltip for the active state.
bool autoToggle() const
Returns whether the current action will automatically be changed when the user triggers this action...
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:31
void setInactiveIcon(const QIcon &)
Sets the icon for the inactive state.
Definition: kdualaction.cpp:92
QString activeText() const
Gets the text for the active state.
void setInactiveGuiItem(const KGuiItem &)
Sets the KGuiItem for the inactive state.
Definition: kdualaction.cpp:75
KGuiItem inactiveGuiItem() const
Gets the KGuiItem for the inactive state.
Definition: kdualaction.cpp:79
void setAutoToggle(bool)
Defines whether the current action should automatically be changed when the user triggers this action...
void setActive(bool state)
Sets the action state.
QIcon activeIcon() const
Gets the icon for the active state.
Definition: kdualaction.cpp:88
void setActiveIcon(const QIcon &)
Sets the icon for the active state.
Definition: kdualaction.cpp:84
An action which can alternate between two texts/icons when triggered.
Definition: kdualaction.h:49
void setInactiveToolTip(const QString &)
Sets the tooltip for the inactive state.
QString inactiveToolTip() const
Gets the tooltip for the inactive state.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
KDualAction(QObject *parent)
Constructs a KDualAction with the specified parent.
Definition: kdualaction.cpp:55
void setActiveGuiItem(const KGuiItem &)
Sets the KGuiItem for the active state.
Definition: kdualaction.cpp:67
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 22:42:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.