• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

KeyBindingEditor.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2008 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "KeyBindingEditor.h"
00022 
00023 // Qt
00024 #include <QtGui/QHeaderView>
00025 #include <QtGui/QKeyEvent>
00026 
00027 #include <KDebug>
00028 
00029 // Konsole
00030 #include "ui_KeyBindingEditor.h"
00031 #include "KeyboardTranslator.h"
00032 
00033 using namespace Konsole;
00034 
00035 KeyBindingEditor::KeyBindingEditor(QWidget* parent)
00036     : QWidget(parent)
00037     , _translator(new KeyboardTranslator( QString() ))
00038 {
00039     _ui = new Ui::KeyBindingEditor();
00040     _ui->setupUi(this);
00041 
00042     // description edit
00043     connect( _ui->descriptionEdit , SIGNAL(textChanged(const QString&)) , this , SLOT(setDescription(const QString&)) );
00044 
00045     // key bindings table
00046     _ui->keyBindingTable->setColumnCount(2);
00047 
00048     QStringList labels;
00049     labels << i18n("Key Combination") << i18n("Output");
00050 
00051     _ui->keyBindingTable->setHorizontalHeaderLabels(labels);
00052     _ui->keyBindingTable->horizontalHeader()->setStretchLastSection(true);
00053     _ui->keyBindingTable->verticalHeader()->hide();
00054 
00055     // add and remove buttons
00056     _ui->addEntryButton->setIcon( KIcon("list-add") );
00057     _ui->removeEntryButton->setIcon( KIcon("list-remove") );
00058 
00059     connect( _ui->removeEntryButton , SIGNAL(clicked()) , this , SLOT(removeSelectedEntry()) );
00060     connect( _ui->addEntryButton , SIGNAL(clicked()) , this , SLOT(addNewEntry()) );
00061     
00062     // test area
00063     _ui->testAreaInputEdit->installEventFilter(this);
00064 }
00065 
00066 KeyBindingEditor::~KeyBindingEditor()
00067 {
00068     delete _ui;
00069 }
00070 
00071 void KeyBindingEditor::removeSelectedEntry()
00072 {
00073     QListIterator<QTableWidgetItem*> iter( _ui->keyBindingTable->selectedItems() );
00074 
00075     while ( iter.hasNext() )
00076     {     
00077         // get the first item in the row which has the entry
00078         QTableWidgetItem* item = _ui->keyBindingTable->item(iter.next()->row(),0);
00079 
00080         KeyboardTranslator::Entry existing = item->data(Qt::UserRole).
00081                                                     value<KeyboardTranslator::Entry>();
00082         
00083         _translator->removeEntry(existing);
00084     
00085         _ui->keyBindingTable->removeRow( item->row() );
00086     }
00087 }
00088 
00089 void KeyBindingEditor::addNewEntry()
00090 {
00091    _ui->keyBindingTable->insertRow( _ui->keyBindingTable->rowCount() );
00092 
00093    int newRowCount = _ui->keyBindingTable->rowCount();
00094 
00095    // block signals here to avoid triggering bindingTableItemChanged() slot call
00096    _ui->keyBindingTable->blockSignals(true);
00097 
00098    _ui->keyBindingTable->setItem(newRowCount-1,0,new QTableWidgetItem() );
00099    _ui->keyBindingTable->setItem(newRowCount-1,1,new QTableWidgetItem() );
00100 
00101    _ui->keyBindingTable->blockSignals(false);
00102 
00103    // make sure user can see new row
00104    _ui->keyBindingTable->scrollToItem(_ui->keyBindingTable->item(newRowCount-1,0));
00105 }
00106 
00107 bool KeyBindingEditor::eventFilter( QObject* watched , QEvent* event )
00108 {
00109     if ( watched == _ui->testAreaInputEdit )
00110     {
00111        if ( event->type() == QEvent::KeyPress )
00112        {
00113             QKeyEvent* keyEvent = (QKeyEvent*)event;
00114 
00115             // The state here is currently set to the state that a newly started 
00116             // terminal in Konsole will be in ( which is also the same as the 
00117             // state just after a reset ), this has 'Ansi' turned on and all other
00118             // states off.
00119             //
00120             // TODO: It may be useful to be able to specify the state in the 'test input' 
00121             // area, but preferably not in a way which clutters the UI with lots of 
00122             // checkboxes.
00123             //
00124             const KeyboardTranslator::States states = KeyboardTranslator::AnsiState;
00125 
00126             KeyboardTranslator::Entry entry = _translator->findEntry( keyEvent->key() , 
00127                                                                       keyEvent->modifiers(), 
00128                                                                       states );
00129 
00130             if ( !entry.isNull() )
00131             {
00132                 _ui->testAreaInputEdit->setText(entry.conditionToString());
00133                 _ui->testAreaOutputEdit->setText(entry.resultToString(true,keyEvent->modifiers()));
00134             }
00135             else
00136             {
00137                 _ui->testAreaInputEdit->setText(keyEvent->text());
00138                 _ui->testAreaOutputEdit->setText(keyEvent->text());
00139             }
00140 
00141             keyEvent->accept();
00142             return true;
00143        } 
00144     }
00145     return false;
00146 }
00147 
00148 void KeyBindingEditor::setDescription(const QString& newDescription)
00149 {
00150      _ui->descriptionEdit->setText(newDescription);
00151     
00152      if ( _translator )
00153          _translator->setDescription(newDescription);
00154 }
00155 QString KeyBindingEditor::description() const
00156 {
00157     return _ui->descriptionEdit->text();
00158 }
00159 
00160 void KeyBindingEditor::setup(const KeyboardTranslator* translator)
00161 {
00162     if ( _translator )
00163         delete _translator;
00164 
00165     _translator = new KeyboardTranslator(*translator);
00166 
00167     // setup description edit
00168     _ui->descriptionEdit->setText(translator->description());
00169 
00170     // setup key binding table
00171     setupKeyBindingTable(translator);
00172 }
00173 
00174 KeyboardTranslator* KeyBindingEditor::translator() const
00175 {
00176     return _translator;
00177 }
00178 
00179 void KeyBindingEditor::bindingTableItemChanged(QTableWidgetItem* item)
00180 {
00181    QTableWidgetItem* key = _ui->keyBindingTable->item( item->row() , 0 );
00182    KeyboardTranslator::Entry existing = key->data(Qt::UserRole).value<KeyboardTranslator::Entry>();
00183 
00184    QString condition = key->text();
00185    QString result = _ui->keyBindingTable->item( item->row() , 1 )->text();
00186 
00187    KeyboardTranslator::Entry entry = KeyboardTranslatorReader::createEntry(condition,result);
00188    _translator->replaceEntry(existing,entry);
00189 
00190     // block signals to prevent this slot from being called repeatedly
00191    _ui->keyBindingTable->blockSignals(true);
00192 
00193    key->setData(Qt::UserRole,QVariant::fromValue(entry));
00194 
00195    _ui->keyBindingTable->blockSignals(false);
00196 }
00197 
00198 void KeyBindingEditor::setupKeyBindingTable(const KeyboardTranslator* translator)
00199 {
00200     disconnect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this , 
00201             SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
00202 
00203     QList<KeyboardTranslator::Entry> entries = translator->entries();
00204     _ui->keyBindingTable->setRowCount(entries.count());
00205 
00206     for ( int row = 0 ; row < entries.count() ; row++ )
00207     {
00208         const KeyboardTranslator::Entry& entry = entries.at(row);
00209 
00210         QTableWidgetItem* keyItem = new QTableWidgetItem(entry.conditionToString());
00211         keyItem->setData( Qt::UserRole , QVariant::fromValue(entry) );
00212 
00213         QTableWidgetItem* textItem = new QTableWidgetItem(QString(entry.resultToString()));
00214 
00215         _ui->keyBindingTable->setItem(row,0,keyItem);
00216         _ui->keyBindingTable->setItem(row,1,textItem);
00217     }
00218     _ui->keyBindingTable->sortItems(0);
00219 
00220     connect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this , 
00221             SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
00222 }
00223 
00224 #include "KeyBindingEditor.moc"
00225 

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal