PolkitQt-1

polkitqt1-gui-actionbutton.cpp
1 /*
2  This file is part of the Polkit-qt project
3  SPDX-FileCopyrightText: 2009 Daniel Nicoletti <[email protected]>
4  SPDX-FileCopyrightText: 2009 Dario Freddi <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "polkitqt1-gui-actionbutton.h"
10 
11 #include "polkitqt1-gui-actionbutton_p.h"
12 
13 namespace PolkitQt1
14 {
15 
16 namespace Gui
17 {
18 
19 ActionButton::ActionButton(QAbstractButton *button, const QString &actionId, QObject *parent)
20  : Action(actionId, parent)
21  , d_ptr(new ActionButtonPrivate(QList<QAbstractButton *>() << button))
22 {
23  d_ptr->q_ptr = this;
24 
26  connect(this, SIGNAL(dataChanged()), SLOT(updateButton()));
27 }
28 
29 ActionButton::ActionButton(ActionButtonPrivate &dd, const QString &actionId, QObject *parent)
30  : Action(actionId, parent)
31  , d_ptr(&dd)
32 {
33  d_ptr->q_ptr = this;
34 
35  connect(this, SIGNAL(dataChanged()), SLOT(updateButton()));
36 }
37 
38 ActionButton::~ActionButton()
39 {
40  delete d_ptr;
41 }
42 
43 void ActionButtonPrivate::updateButton()
44 {
45  Q_Q(ActionButton);
46 
47  Q_FOREACH(QAbstractButton *ent, buttons) {
48  ent->setVisible(q->isVisible());
49  ent->setEnabled(q->isEnabled());
50  ent->setText(q->text());
51  if (!q->toolTip().isNull()) {
52  ent->setToolTip(q->toolTip());
53  }
54  if (!q->whatsThis().isNull()) {
55  ent->setWhatsThis(q->whatsThis());
56  }
57  ent->setIcon(q->icon());
58  // if the item cannot do the action anymore
59  // lets revert to the initial state
60  if (ent->isCheckable()) {
61  ent->setChecked(q->isChecked());
62  }
63  }
64 }
65 
67 {
69 
70  bool tg = false;
71  Q_FOREACH(QAbstractButton *ent, d->buttons) {
72  if (ent->isCheckable()) {
73  // we set the current Action state
74  ent->setChecked(isChecked());
75  // toggle the action cause we are not directly connected there..
76  tg = true;
77  }
78  }
79 
80  if (tg) {
81  toggle();
82  }
83 
84  return Action::activate();
85 }
86 
88 {
90 
91  // First, let's clear the list
92  Q_FOREACH(QAbstractButton *ent, d->buttons) {
93  d->removeButton(ent);
94  }
95 
96  // And then add it
97  d->addButton(button);
98 }
99 
100 void ActionButtonPrivate::addButton(QAbstractButton *button)
101 {
102  Q_Q(ActionButton);
103 
104  buttons.append(button);
105  QObject::connect(button, SIGNAL(clicked(bool)), q, SLOT(streamClicked(bool)));
106  QObject::connect(q, SIGNAL(toggled(bool)), button, SLOT(toggle()));
107  if (q->isCheckable()) {
108  // the button should follow our first buttons
109  button->setCheckable(true);
110  } else if (button->isCheckable()) {
111  // if we are not checkable BUT the button
112  // is (eg a QCheckBox) we should set all buttons to
113  // checkable.
114  Q_FOREACH(QAbstractButton *ent, buttons) {
115  ent->setCheckable(true);
116  }
117  // set the checkable state of Action to store the initial state
118  q->setCheckable(true);
119  }
120  // call this after m_activateOnCheck receives the value
121  updateButton();
122 }
123 
124 void ActionButtonPrivate::removeButton(QAbstractButton *button)
125 {
126  Q_Q(ActionButton);
127 
128  if (buttons.contains(button)) {
129  QObject::disconnect(button, SIGNAL(clicked(bool)), q, SLOT(streamClicked(bool)));
130  QObject::disconnect(q, SIGNAL(toggled(bool)), button, SLOT(toggle()));
131  buttons.removeOne(button);
132  }
133 }
134 
136 {
137  Q_D(const ActionButton);
138 
139  return d->buttons.first();
140 }
141 
142 void ActionButtonPrivate::streamClicked(bool c)
143 {
144  Q_Q(ActionButton);
145 
146  Q_EMIT q->clicked(qobject_cast<QAbstractButton *>(q->sender()), c);
147 }
148 
149 }
150 
151 }
152 
153 #include "moc_polkitqt1-gui-actionbutton.cpp"
void setWhatsThis(const QString &)
bool isCheckable() const const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QAbstractButton * button() const
Returns the current button.
Class used to hold and update a QAbstractButton.
void setChecked(bool)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual void setVisible(bool visible)
bool activate()
Use this slot if you want to activate the action.
void setEnabled(bool)
void setIcon(const QIcon &icon)
void setToolTip(const QString &)
ActionButton(QAbstractButton *button, const QString &actionId=QString(), QObject *parent=nullptr)
Constructs a new ActionButton.
void toggle()
Class used to manage actions.
Namespace wrapping Polkit-Qt classes.
bool isChecked() const const
void setButton(QAbstractButton *button)
Sets the button associated to the underlying action.
bool activate()
Connect clicked() signals to this slot.
void setText(const QString &text)
void dataChanged()
Emitted when the PolicyKit result (PolKitResult) for the given action or the internal data changes (i...
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:06:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.