File indexing completed on 2024-04-14 04:38:32

0001 /*
0002     This file is part of the Polkit-qt project
0003     SPDX-FileCopyrightText: 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
0004     SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "polkitqt1-gui-actionbutton.h"
0010 
0011 #include "polkitqt1-gui-actionbutton_p.h"
0012 
0013 namespace PolkitQt1
0014 {
0015 
0016 namespace Gui
0017 {
0018 
0019 ActionButton::ActionButton(QAbstractButton *button, const QString &actionId, QObject *parent)
0020         : Action(actionId, parent)
0021         , d_ptr(new ActionButtonPrivate(QList<QAbstractButton *>() << button))
0022 {
0023     d_ptr->q_ptr = this;
0024 
0025     setButton(button);
0026     connect(this, SIGNAL(dataChanged()), SLOT(updateButton()));
0027 }
0028 
0029 ActionButton::ActionButton(ActionButtonPrivate &dd, const QString &actionId, QObject *parent)
0030         : Action(actionId, parent)
0031         , d_ptr(&dd)
0032 {
0033     d_ptr->q_ptr = this;
0034 
0035     connect(this, SIGNAL(dataChanged()), SLOT(updateButton()));
0036 }
0037 
0038 ActionButton::~ActionButton()
0039 {
0040     delete d_ptr;
0041 }
0042 
0043 void ActionButtonPrivate::updateButton()
0044 {
0045     Q_Q(ActionButton);
0046 
0047     Q_FOREACH(QAbstractButton *ent, buttons) {
0048         ent->setVisible(q->isVisible());
0049         ent->setEnabled(q->isEnabled());
0050         ent->setText(q->text());
0051         if (!q->toolTip().isNull()) {
0052             ent->setToolTip(q->toolTip());
0053         }
0054         if (!q->whatsThis().isNull()) {
0055             ent->setWhatsThis(q->whatsThis());
0056         }
0057         ent->setIcon(q->icon());
0058         // if the item cannot do the action anymore
0059         // lets revert to the initial state
0060         if (ent->isCheckable()) {
0061             ent->setChecked(q->isChecked());
0062         }
0063     }
0064 }
0065 
0066 bool ActionButton::activate()
0067 {
0068     Q_D(ActionButton);
0069 
0070     bool tg = false;
0071     Q_FOREACH(QAbstractButton *ent, d->buttons) {
0072         if (ent->isCheckable()) {
0073             // we set the current Action state
0074             ent->setChecked(isChecked());
0075             // toggle the action cause we are not directly connected there..
0076             tg = true;
0077         }
0078     }
0079 
0080     if (tg) {
0081         toggle();
0082     }
0083 
0084     return Action::activate();
0085 }
0086 
0087 void ActionButton::setButton(QAbstractButton *button)
0088 {
0089     Q_D(ActionButton);
0090 
0091     // First, let's clear the list
0092     Q_FOREACH(QAbstractButton *ent, d->buttons) {
0093         d->removeButton(ent);
0094     }
0095 
0096     // And then add it
0097     d->addButton(button);
0098 }
0099 
0100 void ActionButtonPrivate::addButton(QAbstractButton *button)
0101 {
0102     Q_Q(ActionButton);
0103 
0104     buttons.append(button);
0105     QObject::connect(button, SIGNAL(clicked(bool)), q, SLOT(streamClicked(bool)));
0106     QObject::connect(q, SIGNAL(toggled(bool)), button, SLOT(toggle()));
0107     if (q->isCheckable()) {
0108         // the button should follow our first buttons
0109         button->setCheckable(true);
0110     } else if (button->isCheckable()) {
0111         // if we are not checkable BUT the button
0112         // is (eg a QCheckBox) we should set all buttons to
0113         // checkable.
0114         Q_FOREACH(QAbstractButton *ent, buttons) {
0115             ent->setCheckable(true);
0116         }
0117         // set the checkable state of Action to store the initial state
0118         q->setCheckable(true);
0119     }
0120     // call this after m_activateOnCheck receives the value
0121     updateButton();
0122 }
0123 
0124 void ActionButtonPrivate::removeButton(QAbstractButton *button)
0125 {
0126     Q_Q(ActionButton);
0127 
0128     if (buttons.contains(button)) {
0129         QObject::disconnect(button, SIGNAL(clicked(bool)), q, SLOT(streamClicked(bool)));
0130         QObject::disconnect(q, SIGNAL(toggled(bool)), button, SLOT(toggle()));
0131         buttons.removeOne(button);
0132     }
0133 }
0134 
0135 QAbstractButton *ActionButton::button() const
0136 {
0137     Q_D(const ActionButton);
0138 
0139     return d->buttons.first();
0140 }
0141 
0142 void ActionButtonPrivate::streamClicked(bool c)
0143 {
0144     Q_Q(ActionButton);
0145 
0146     Q_EMIT q->clicked(qobject_cast<QAbstractButton *>(q->sender()), c);
0147 }
0148 
0149 }
0150 
0151 }
0152 
0153 #include "moc_polkitqt1-gui-actionbutton.cpp"