PolkitQt-1

polkitqt1-gui-actionbutton.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
4 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
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
13namespace PolkitQt1
14{
15
16namespace Gui
17{
18
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
29ActionButton::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
38ActionButton::~ActionButton()
39{
40 delete d_ptr;
41}
42
43void 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
100void 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
124void 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
142void 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"
Class used to hold and update a QAbstractButton.
QAbstractButton * button() const
Returns the current button.
void setButton(QAbstractButton *button)
Sets the button associated to the underlying action.
ActionButton(QAbstractButton *button, const QString &actionId=QString(), QObject *parent=nullptr)
Constructs a new ActionButton.
bool activate()
Connect clicked() signals to this slot.
Class used to manage actions.
bool activate()
Use this slot if you want to activate the action.
void dataChanged()
Emitted when the PolicyKit result (PolKitResult) for the given action or the internal data changes (i...
bool isCheckable() const const
void setChecked(bool)
void setIcon(const QIcon &icon)
void setText(const QString &text)
bool isChecked() const const
void toggle()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
void setEnabled(bool)
void setToolTip(const QString &)
virtual void setVisible(bool visible)
void setWhatsThis(const QString &)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:57:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.