File indexing completed on 2024-04-14 03:59:57

0001 //
0002 // C++ Implementation: cshortcuteditor
0003 //
0004 // Description: 
0005 //
0006 /*
0007 Copyright 2002-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 "cshortcuteditor.h"
0024 
0025 #include "dialogs/dlggrabkey.h"
0026 #include "cshortcut.h"
0027 #include "cscripteditor.h"
0028 
0029 #include <QCheckBox>
0030 #include <QGridLayout>
0031 #include <QLabel>
0032 #include <QLineEdit>
0033 #include <QPushButton>
0034 #include <QTabWidget>
0035 
0036 #include <KLocalizedString>
0037 
0038 struct cShortcutEditor::Private {
0039   QLineEdit *cmd;
0040   QCheckBox *chksendit, *chkoverwrite;
0041   QLabel *lblkey;
0042   int key, modifiers;
0043   cScriptEditor *script;
0044 };
0045 
0046 cShortcutEditor::cShortcutEditor (QWidget *parent)
0047   : cListEditor (parent)
0048 {
0049   d = new Private;
0050 }
0051 
0052 cShortcutEditor::~cShortcutEditor ()
0053 {
0054   // the GUI elements will be destroyed automatically
0055   delete d;
0056 }
0057 
0058 void cShortcutEditor::createGUI(QWidget *parent)
0059 {
0060   QVBoxLayout *mainLayout = new QVBoxLayout (parent);
0061   QTabWidget *tabs = new QTabWidget (parent);
0062   mainLayout->addWidget (tabs);
0063 
0064   QFrame *basicPage = new QFrame (tabs);
0065   QGridLayout *basicLayout = new QGridLayout (basicPage);
0066   
0067   //command
0068   QLabel *lbl1 = new QLabel (i18n ("&Command"), basicPage);
0069   d->cmd = new QLineEdit (basicPage);
0070   lbl1->setBuddy (d->cmd);
0071   d->cmd->setWhatsThis( i18n ("Command that will be executed when you press the "
0072       "defined key combination.\n"
0073       "Command can include aliases, script calls and similar stuff."));
0074 
0075   //key
0076   QLabel *lbl2 = new QLabel (i18n ("Shortcut"), basicPage);
0077   d->lblkey = new QLabel (basicPage);
0078   d->lblkey->setFrameStyle (QFrame::StyledPanel | QFrame::Sunken);
0079   d->lblkey->setLineWidth (2);
0080   QPushButton *btgrab = new QPushButton (i18n ("Grab shortcut..."), basicPage);
0081   connect (btgrab, &QPushButton::clicked, this, &cShortcutEditor::grabKey);
0082 
0083   d->chksendit = new QCheckBox (i18n ("&Send command"), basicPage);
0084   d->chksendit->setWhatsThis( i18n ("If checked, the command will be sent "
0085       "to the MUD. Otherwise, it will be put in the inputline."));
0086 
0087   d->chkoverwrite = new QCheckBox (i18n ("&Overwrite existing text"), basicPage);
0088   d->chkoverwrite->setWhatsThis( i18n ("If there already is some text in "
0089       "the input line, should it be overwritten?"));
0090   connect (d->chksendit, &QCheckBox::toggled, d->chkoverwrite, &QCheckBox::setDisabled);
0091    
0092   QWidget *commonEditor = createCommonAttribEditor (basicPage);
0093 
0094   //place'em there!
0095   basicLayout->setSpacing (5);
0096   basicLayout->addWidget (lbl1, 0, 0);
0097   basicLayout->addWidget (d->cmd, 0, 1, 1, 2);
0098   basicLayout->addWidget (lbl2, 1, 0);
0099   basicLayout->addWidget (d->lblkey, 1, 1);
0100   basicLayout->addWidget (btgrab, 1, 2);
0101   basicLayout->addWidget (d->chksendit, 2, 0, 1, 3);
0102   basicLayout->addWidget (d->chkoverwrite, 3, 0, 1, 3);
0103   basicLayout->addWidget (commonEditor, 4, 0, 1, 3);
0104 
0105   // initial state
0106   d->chksendit->setChecked (true);
0107 
0108   // the Script tab
0109   QFrame *scriptPage = new QFrame (tabs);
0110   QVBoxLayout *scriptlayout = new QVBoxLayout (scriptPage);
0111   d->script = new cScriptEditor (scriptPage);
0112   scriptlayout->addWidget (d->script);
0113 
0114   tabs->addTab (basicPage, i18n ("&General"));
0115   tabs->addTab (scriptPage, i18n ("&Script"));
0116 }
0117 
0118 void cShortcutEditor::fillGUI (const cListObjectData &data)
0119 {
0120   // Common attributes
0121   fillCommonAttribEditor (data);
0122 
0123   d->cmd->setText (data.strValue ("command"));
0124   d->key = data.intValue ("key");
0125   d->modifiers = data.intValue ("modifiers");
0126   d->lblkey->setText (cShortcut::keyToString (d->key, d->modifiers));
0127   d->chksendit->setChecked (data.boolValue ("send"));
0128   d->chkoverwrite->setChecked (data.boolValue ("overwrite"));
0129   // Script
0130   d->script->setText (data.strValue ("script"));
0131 }
0132 
0133 void cShortcutEditor::getDataFromGUI (cListObjectData *data)
0134 {
0135   // Comon attributes
0136   getDataFromCommonAttribEditor (data);
0137 
0138   data->strValues["command"] = d->cmd->text();
0139   data->intValues["key"] = d->key;
0140   data->intValues["modifiers"] = d->modifiers;
0141   data->boolValues["send"] = d->chksendit->isChecked();
0142   data->boolValues["overwrite"] = d->chkoverwrite->isChecked();
0143   // Script
0144   data->strValues["script"] = d->script->text();
0145 }
0146 
0147 void cShortcutEditor::grabKey ()
0148 {
0149   dlgGrabKey *gk = new dlgGrabKey (this);
0150   if (gk->exec ())
0151   {
0152     d->key = gk->key();
0153     d->modifiers = gk->state();
0154     d->modifiers = d->modifiers & (Qt::KeyboardModifierMask | Qt::KeypadModifier);
0155     d->lblkey->setText (cShortcut::keyToString (d->key, d->modifiers));
0156   }
0157   delete gk;
0158   d->cmd->setFocus ();
0159 }
0160 
0161 #include "moc_cshortcuteditor.cpp"