File indexing completed on 2024-03-24 15:43:24

0001 //
0002 // C++ Implementation: cbutton
0003 //
0004 // Description: One button (no GUI)
0005 //
0006 /*
0007 Copyright 2008-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "cbutton.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cbuttonlist.h"
0027 #include "cscripteval.h"
0028 #include "kmuddy.h"
0029 
0030 #include <ktoolbar.h>
0031 
0032 #include <QAction>
0033 #include <QIcon>
0034 
0035 struct cButton::Private {
0036   QString command, command2;
0037   QAction *action;
0038 };
0039 
0040 cButton::cButton (cList *list) : cListObject (list)
0041 {
0042   d = new Private;
0043   d->action = new QAction (nullptr);
0044   connect (d->action, &QAction::triggered, this, &cButton::execute);
0045 }
0046 
0047 cButton::~cButton()
0048 {
0049   delete d->action;
0050   delete d;
0051 }
0052 
0053 void cButton::attribChanged (const QString &name)
0054 {
0055   if (name == "command") {
0056     d->command = strVal ("command");
0057   }
0058   else if (name == "command-released") {
0059     d->command2 = strVal ("command-released");
0060   }
0061   else if (name == "caption") {
0062     d->action->setText (strVal ("caption"));
0063     updateVisibleName ();
0064   }
0065   else if (name == "icon") {
0066     d->action->setIcon (QIcon::fromTheme (strVal ("icon")));
0067   }
0068   else if (name == "pushdown") {
0069     d->action->setCheckable (boolVal ("pushdown"));
0070   }
0071 }
0072 
0073 void cButton::updateVisibleName()
0074 {
0075   if (d->action->text().isEmpty())
0076     cListObject::updateVisibleName();
0077   else
0078     setVisibleName (d->action->text());
0079 }
0080 
0081 cList::TraverseAction cButton::traverse (int traversalType)
0082 {
0083   if (traversalType == BUTTON_UPDATE) {
0084     addButton ();
0085     return cList::Continue;
0086   }
0087   return cList::Stop;
0088 }
0089 
0090 void cButton::execute (bool checked)
0091 {
0092   cActionManager *am = cActionManager::self();
0093   int sess = list()->session();
0094 
0095   bool second = false;
0096   QString cmd = d->command;
0097   if (d->action->isCheckable() && (!checked)) {
0098     cmd = d->command2;
0099     second = true;
0100   }
0101   if (!cmd.isEmpty())
0102     am->invokeEvent ("command", sess, cmd);
0103 
0104   // execute the script, if any
0105   QString script = second ? strVal ("script-release") : strVal ("script");
0106   if (!script.isEmpty()) {
0107     cScriptEval *eval = dynamic_cast<cScriptEval *>(am->object ("scripteval", sess));
0108     if (eval) eval->eval (script);
0109   }
0110 }
0111 
0112 void cButton::addButton ()
0113 {
0114   KToolBar *bar = KMuddy::self()->buttonBar ();
0115   bar->addAction (d->action);
0116 }
0117 
0118 void cButton::objectMoved ()
0119 {
0120   ((cButtonList *) list())->updateButtons();
0121 }
0122 
0123 void cButton::objectEnabled ()
0124 {
0125   ((cButtonList *) list())->updateButtons();
0126 }
0127 
0128 void cButton::objectDisabled ()
0129 {
0130   ((cButtonList *) list())->updateButtons();
0131 }
0132 
0133 #include "moc_cbutton.cpp"