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

0001 /***************************************************************************
0002                           cshortcut.cpp  -  macro key/shortcut
0003                              -------------------
0004     begin                : St máj 28 2003
0005     copyright            : (C) 2003-2008 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "cshortcut.h"
0019 
0020 #include "cactionmanager.h"
0021 #include "cinputline.h"
0022 #include "cshortcutlist.h"
0023 #include "cscripteval.h"
0024 
0025 #include <qkeysequence.h>
0026 #include <KLocalizedString>
0027 
0028 struct cShortcut::Private {
0029   QString command;
0030   unsigned int key, modifiers;
0031   bool send, overwrite;
0032 };
0033 
0034 cShortcut::cShortcut (cList *list) : cListObject (list)
0035 {
0036   d = new Private;
0037   d->send = true;
0038   d->overwrite = false;
0039   d->key = 0;
0040   d->modifiers = 0;
0041 }
0042 
0043 cShortcut::~cShortcut ()
0044 {
0045   delete d;
0046 }
0047 
0048 void cShortcut::attribChanged (const QString &name)
0049 {
0050   if (name == "command") {
0051     d->command = strVal ("command");
0052     updateVisibleName ();
0053     return;
0054   }
0055   if (name == "key") {
0056     d->key = intVal ("key");
0057     updateVisibleName ();
0058     return;
0059   }
0060   if (name == "modifiers") {
0061     d->modifiers = intVal ("modifiers");
0062     updateVisibleName ();
0063     return;
0064   }
0065   if (name == "send") {
0066     d->send = boolVal ("send");
0067     return;
0068   }
0069   if (name == "overwrite") {
0070     d->overwrite = boolVal ("overwrite");
0071     return;
0072   }
0073   if (name == "script") updateVisibleName ();
0074 }
0075 
0076 void cShortcut::updateVisibleName()
0077 {
0078   QString cmd = d->command;
0079   if (cmd.isEmpty() && (!strVal ("script").isEmpty())) cmd = "(script)";
0080   if (cmd.isEmpty())
0081     cListObject::updateVisibleName();
0082   setVisibleName (keyToString (d->key, d->modifiers) + " -> " + cmd);
0083 }
0084 
0085 cList::TraverseAction cShortcut::traverse (int traversalType)
0086 {
0087   cActionManager *am = cActionManager::self();
0088   int sess = list()->session();
0089 
0090   if (traversalType == SHORTCUT_MATCH) {
0091     cShortcutList *sl = (cShortcutList *) list ();
0092     if ((sl->currentKey == d->key) && (sl->currentModifiers == d->modifiers)) {
0093       // match
0094       if (!d->command.isEmpty()) {
0095         if (d->send)
0096           am->invokeEvent ("command", sess, d->command);
0097         else {
0098           cInputLine *inputline = dynamic_cast<cInputLine *>(am->object ("inputline", sess));
0099           d->overwrite ? inputline->setText (d->command) : inputline->insert (d->command);
0100         }
0101       }
0102       // execute the script, if any
0103       QString script = strVal ("script");
0104       if (!script.isEmpty()) {
0105         cScriptEval *eval = dynamic_cast<cScriptEval *>(am->object ("scripteval", sess));
0106         if (eval) eval->eval (script);
0107       }
0108 
0109       sl->matched = true;
0110     }
0111     return cList::Continue;
0112   }
0113   return cList::Stop;
0114 }
0115 
0116 
0117 QString cShortcut::keyToString (int _key, int _state)
0118 {
0119   //all existing functions are strange... Or at least I failed to get
0120   //anything useful out of KKey and KShortcut. QKeySequence also seems
0121   //to accept input in some other format, and it doesn't recognize numpad
0122   //keys properly. Therefore, I made my own function that works well :)
0123 
0124   QString base = QKeySequence (_key).toString();
0125   if (base.length() == 0)
0126     base = i18n ("(unknown)");
0127   if (_key == 0)
0128     base = i18n ("(none)");
0129   if (_state & Qt::KeypadModifier)
0130     base = "KP_" + base;
0131   if (_state & Qt::ShiftModifier)
0132     base = i18n ("Shift") + " + " + base;
0133   if (_state & Qt::ControlModifier)
0134     base = i18n ("Ctrl") + " + " + base;
0135   if (_state & Qt::AltModifier)
0136     base = i18n ("Alt") + " + " + base;
0137   if (_state & Qt::MetaModifier)
0138     base = i18n ("Meta") + " + " + base;
0139   return base;  
0140 }
0141