diff --git a/src/KeyBindingEditor.cpp b/src/KeyBindingEditor.cpp index d163cda5b..b483c7ea4 100644 --- a/src/KeyBindingEditor.cpp +++ b/src/KeyBindingEditor.cpp @@ -44,12 +44,18 @@ KeyBindingEditor::KeyBindingEditor(QWidget* parent) _ui->keyBindingTable->setColumnCount(2); QStringList labels; - labels << i18n("Key Combination") << i18n("Binding"); + labels << i18n("Key Combination") << i18n("Output"); _ui->keyBindingTable->setHorizontalHeaderLabels(labels); _ui->keyBindingTable->horizontalHeader()->setStretchLastSection(true); _ui->keyBindingTable->verticalHeader()->hide(); + // add and remove buttons + _ui->addEntryButton->setIcon( KIcon("list-add") ); + _ui->removeEntryButton->setIcon( KIcon("list-remove") ); + + connect( _ui->removeEntryButton , SIGNAL(clicked()) , this , SLOT(removeSelectedEntry()) ); + connect( _ui->addEntryButton , SIGNAL(clicked()) , this , SLOT(addNewEntry()) ); // test area _ui->testAreaInputEdit->installEventFilter(this); @@ -60,6 +66,42 @@ KeyBindingEditor::~KeyBindingEditor() delete _ui; } +void KeyBindingEditor::removeSelectedEntry() +{ + QListIterator iter( _ui->keyBindingTable->selectedItems() ); + + while ( iter.hasNext() ) + { + // get the first item in the row which has the entry + QTableWidgetItem* item = _ui->keyBindingTable->item(iter.next()->row(),0); + + KeyboardTranslator::Entry existing = item->data(Qt::UserRole). + value(); + + _translator->removeEntry(existing); + + _ui->keyBindingTable->removeRow( item->row() ); + } +} + +void KeyBindingEditor::addNewEntry() +{ + _ui->keyBindingTable->insertRow( _ui->keyBindingTable->rowCount() ); + + int newRowCount = _ui->keyBindingTable->rowCount(); + + // block signals here to avoid triggering bindingTableItemChanged() slot call + _ui->keyBindingTable->blockSignals(true); + + _ui->keyBindingTable->setItem(newRowCount-1,0,new QTableWidgetItem() ); + _ui->keyBindingTable->setItem(newRowCount-1,1,new QTableWidgetItem() ); + + _ui->keyBindingTable->blockSignals(false); + + // make sure user can see new row + _ui->keyBindingTable->scrollToItem(_ui->keyBindingTable->item(newRowCount-1,0)); +} + bool KeyBindingEditor::eventFilter( QObject* watched , QEvent* event ) { if ( watched == _ui->testAreaInputEdit ) @@ -68,13 +110,31 @@ bool KeyBindingEditor::eventFilter( QObject* watched , QEvent* event ) { QKeyEvent* keyEvent = (QKeyEvent*)event; + // The state here is currently set to the state that a newly started + // terminal in Konsole will be in ( which is also the same as the + // state just after a reset ), this has 'Ansi' turned on and all other + // states off. + // + // TODO: It may be useful to be able to specify the state in the 'test input' + // area, but preferably not in a way which clutters the UI with lots of + // checkboxes. + // + const KeyboardTranslator::State state = KeyboardTranslator::AnsiState; + int modifiers = keyEvent->modifiers(); KeyboardTranslator::Entry entry = _translator->findEntry( keyEvent->key() , - (Qt::KeyboardModifier)modifiers ); - - _ui->testAreaInputEdit->setText(entry.conditionToString()); - _ui->testAreaOutputEdit->setText(entry.resultToString()); + (Qt::KeyboardModifier)modifiers, state ); + if ( !entry.isNull() ) + { + _ui->testAreaInputEdit->setText(entry.conditionToString()); + _ui->testAreaOutputEdit->setText(entry.resultToString()); + } + else + { + _ui->testAreaInputEdit->setText(keyEvent->text()); + _ui->testAreaOutputEdit->setText(keyEvent->text()); + } //qDebug() << "Entry: " << entry.resultToString(); keyEvent->accept(); @@ -130,12 +190,12 @@ void KeyBindingEditor::bindingTableItemChanged(QTableWidgetItem* item) qDebug() << "Created entry: " << entry.conditionToString() << " , " << entry.resultToString(); - _translator->replaceEntry(existing,entry); + _translator->replaceEntry(existing,entry); // block signals to prevent this slot from being called repeatedly _ui->keyBindingTable->blockSignals(true); - key->setData(Qt::UserRole,QVariant::fromValue(existing)); + key->setData(Qt::UserRole,QVariant::fromValue(entry)); _ui->keyBindingTable->blockSignals(false); } diff --git a/src/KeyBindingEditor.h b/src/KeyBindingEditor.h index e936b7cc8..2b7e00871 100644 --- a/src/KeyBindingEditor.h +++ b/src/KeyBindingEditor.h @@ -85,6 +85,8 @@ public slots: private slots: void bindingTableItemChanged(QTableWidgetItem* item); + void removeSelectedEntry(); + void addNewEntry(); private: void setupKeyBindingTable(const KeyboardTranslator* translator); diff --git a/src/KeyBindingEditor.ui b/src/KeyBindingEditor.ui index 3cd5aca9a..e8c7328f2 100644 --- a/src/KeyBindingEditor.ui +++ b/src/KeyBindingEditor.ui @@ -5,8 +5,8 @@ 0 0 - 302 - 395 + 374 + 530 @@ -49,6 +49,37 @@ + + + + + + Add + + + + + + + Remove + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/src/KeyboardTranslator.cpp b/src/KeyboardTranslator.cpp index 336f331fb..9aedc0afd 100644 --- a/src/KeyboardTranslator.cpp +++ b/src/KeyboardTranslator.cpp @@ -649,7 +649,7 @@ QKeySequence KeyboardTranslator::Entry::keySequence() const } #endif -bool KeyboardTranslator::Entry::operator==(const Entry& rhs) +bool KeyboardTranslator::Entry::operator==(const Entry& rhs) const { return _keyCode == rhs._keyCode && _modifiers == rhs._modifiers && @@ -827,7 +827,7 @@ void KeyboardTranslator::Entry::insertState( QString& item , int state ) const else if ( state == KeyboardTranslator::NewLineState ) item += "NewLine"; else if ( state == KeyboardTranslator::AnsiState ) - item += "ansi"; + item += "Ansi"; else if ( state == KeyboardTranslator::CursorKeysState ) item += "AppCuKeys"; else if ( state == KeyboardTranslator::AnyModifierState ) @@ -914,7 +914,8 @@ void KeyboardTranslator::addEntry(const Entry& entry) } void KeyboardTranslator::replaceEntry(const Entry& existing , const Entry& replacement) { - _entries.remove(existing.keyCode(),existing); + if ( !existing.isNull() ) + _entries.remove(existing.keyCode(),existing); _entries.insert(replacement.keyCode(),replacement); } void KeyboardTranslator::removeEntry(const Entry& entry) diff --git a/src/KeyboardTranslator.h b/src/KeyboardTranslator.h index 99f114e29..8316ea373 100644 --- a/src/KeyboardTranslator.h +++ b/src/KeyboardTranslator.h @@ -119,6 +119,12 @@ public: */ Entry(); + /** + * Returns true if this entry is null. + * This is true for newly constructed entries which have no properties set. + */ + bool isNull() const; + /** Returns the commands associated with this entry */ Command command() const; /** Sets the command associated with this entry. */ @@ -202,7 +208,7 @@ public: */ bool matches( int keyCode , Qt::KeyboardModifier modifiers , State flags ) const; - bool operator==(const Entry& rhs); + bool operator==(const Entry& rhs) const; private: void insertModifier( QString& item , int modifier ) const; @@ -256,7 +262,8 @@ public: void addEntry(const Entry& entry); /** - * Replaces an entry in the translator. + * Replaces an entry in the translator. If the @p existing entry is null, + * then this is equivalent to calling addEntry(@p replacement) */ void replaceEntry(const Entry& existing , const Entry& replacement); @@ -476,6 +483,11 @@ inline void KeyboardTranslator::Entry::setModifierMask( Qt::KeyboardModifier ma } inline Qt::KeyboardModifier KeyboardTranslator::Entry::modifierMask() const { return _modifierMask; } +inline bool KeyboardTranslator::Entry::isNull() const +{ + return ( *this == Entry() ); +} + inline void KeyboardTranslator::Entry::setCommand( Command command ) { _command = command;