KDEUI
kdualaction.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 * 00003 * Copyright (c) 2010 Aurélien Gâteau <agateau@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00018 * 02110-1301 USA 00019 */ 00020 #include "kdualaction.h" 00021 00022 #include <QIcon> 00023 00024 #include <kdebug.h> 00025 00026 #include "kdualaction_p.h" 00027 00028 //--------------------------------------------------------------------- 00029 // KDualActionPrivate 00030 //--------------------------------------------------------------------- 00031 void KDualActionPrivate::init(KDualAction *q_ptr) 00032 { 00033 q = q_ptr; 00034 autoToggle = true; 00035 isActive = false; 00036 00037 QObject::connect(q, SIGNAL(triggered()), q, SLOT(slotTriggered())); 00038 } 00039 00040 void KDualActionPrivate::updateFromCurrentState() 00041 { 00042 KGuiItem& currentItem = item(isActive); 00043 QAction* qq = static_cast<QAction*>(q); 00044 qq->setIcon(currentItem.icon()); 00045 qq->setText(currentItem.text()); 00046 qq->setToolTip(currentItem.toolTip()); 00047 } 00048 00049 void KDualActionPrivate::slotTriggered() 00050 { 00051 if (!autoToggle) { 00052 return; 00053 } 00054 q->setActive(!isActive); 00055 q->activeChangedByUser(isActive); 00056 } 00057 00058 //--------------------------------------------------------------------- 00059 // KDualAction 00060 //--------------------------------------------------------------------- 00061 KDualAction::KDualAction(const QString &inactiveText, const QString &activeText, QObject *parent) 00062 : KAction(parent) 00063 , d(new KDualActionPrivate) 00064 { 00065 d->init(this); 00066 d->item(false).setText(inactiveText); 00067 d->item(true).setText(activeText); 00068 d->updateFromCurrentState(); 00069 } 00070 00071 KDualAction::KDualAction(QObject *parent) 00072 : KAction(parent) 00073 , d(new KDualActionPrivate) 00074 { 00075 d->init(this); 00076 } 00077 00078 KDualAction::~KDualAction() 00079 { 00080 delete d; 00081 } 00082 00083 void KDualAction::setActiveGuiItem(const KGuiItem &item) { d->setGuiItem(true, item); } 00084 KGuiItem KDualAction::activeGuiItem() const { return d->item(true); } 00085 void KDualAction::setInactiveGuiItem(const KGuiItem &item) { d->setGuiItem(false, item); } 00086 KGuiItem KDualAction::inactiveGuiItem() const { return d->item(false); } 00087 00088 void KDualAction::setActiveIcon(const QIcon &icon) { d->setIcon(true, icon); } 00089 QIcon KDualAction::activeIcon() const { return d->item(true).icon(); } 00090 void KDualAction::setInactiveIcon(const QIcon &icon) { d->setIcon(false, icon); } 00091 QIcon KDualAction::inactiveIcon() const { return d->item(false).icon(); } 00092 00093 void KDualAction::setActiveText(const QString &text) { d->setText(true, text); } 00094 QString KDualAction::activeText() const { return d->item(true).text(); } 00095 void KDualAction::setInactiveText(const QString &text) { d->setText(false, text); } 00096 QString KDualAction::inactiveText() const { return d->item(false).text(); } 00097 00098 void KDualAction::setActiveToolTip(const QString &toolTip) { d->setToolTip(true, toolTip); } 00099 QString KDualAction::activeToolTip() const { return d->item(true).toolTip(); } 00100 void KDualAction::setInactiveToolTip(const QString &toolTip) { d->setToolTip(false, toolTip); } 00101 QString KDualAction::inactiveToolTip() const { return d->item(false).toolTip(); } 00102 00103 void KDualAction::setIconForStates(const QIcon &icon) 00104 { 00105 setInactiveIcon(icon); 00106 setActiveIcon(icon); 00107 } 00108 00109 void KDualAction::setAutoToggle(bool value) 00110 { 00111 d->autoToggle = value; 00112 } 00113 00114 bool KDualAction::autoToggle() const 00115 { 00116 return d->autoToggle; 00117 } 00118 00119 void KDualAction::setActive(bool active) 00120 { 00121 if (active == d->isActive) { 00122 return; 00123 } 00124 d->isActive = active; 00125 d->updateFromCurrentState(); 00126 activeChanged(active); 00127 } 00128 00129 bool KDualAction::isActive() const 00130 { 00131 return d->isActive; 00132 } 00133 00134 #include "kdualaction.moc"
KDE 4.6 API Reference