add more spacing
[personal-kdebase.git] / apps / konsole / src / KeyBindingEditor.cpp
blob490825b7ba7b58e643404d368e82a97ee7189eea
1 /*
2 Copyright 2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "KeyBindingEditor.h"
23 // Qt
24 #include <QtGui/QHeaderView>
25 #include <QtGui/QKeyEvent>
27 #include <KDebug>
29 // Konsole
30 #include "ui_KeyBindingEditor.h"
31 #include "KeyboardTranslator.h"
33 using namespace Konsole;
35 KeyBindingEditor::KeyBindingEditor(QWidget* parent)
36 : QWidget(parent)
37 , _translator(new KeyboardTranslator( QString() ))
39 _ui = new Ui::KeyBindingEditor();
40 _ui->setupUi(this);
42 // description edit
43 connect( _ui->descriptionEdit , SIGNAL(textChanged(const QString&)) , this , SLOT(setDescription(const QString&)) );
45 // key bindings table
46 _ui->keyBindingTable->setColumnCount(2);
48 QStringList labels;
49 labels << i18n("Key Combination") << i18n("Output");
51 _ui->keyBindingTable->setHorizontalHeaderLabels(labels);
52 _ui->keyBindingTable->horizontalHeader()->setStretchLastSection(true);
53 _ui->keyBindingTable->verticalHeader()->hide();
55 // add and remove buttons
56 _ui->addEntryButton->setIcon( KIcon("list-add") );
57 _ui->removeEntryButton->setIcon( KIcon("list-remove") );
59 connect( _ui->removeEntryButton , SIGNAL(clicked()) , this , SLOT(removeSelectedEntry()) );
60 connect( _ui->addEntryButton , SIGNAL(clicked()) , this , SLOT(addNewEntry()) );
62 // test area
63 _ui->testAreaInputEdit->installEventFilter(this);
66 KeyBindingEditor::~KeyBindingEditor()
68 delete _ui;
71 void KeyBindingEditor::removeSelectedEntry()
73 QListIterator<QTableWidgetItem*> iter( _ui->keyBindingTable->selectedItems() );
75 while ( iter.hasNext() )
77 // get the first item in the row which has the entry
78 QTableWidgetItem* item = _ui->keyBindingTable->item(iter.next()->row(),0);
80 KeyboardTranslator::Entry existing = item->data(Qt::UserRole).
81 value<KeyboardTranslator::Entry>();
83 _translator->removeEntry(existing);
85 _ui->keyBindingTable->removeRow( item->row() );
89 void KeyBindingEditor::addNewEntry()
91 _ui->keyBindingTable->insertRow( _ui->keyBindingTable->rowCount() );
93 int newRowCount = _ui->keyBindingTable->rowCount();
95 // block signals here to avoid triggering bindingTableItemChanged() slot call
96 _ui->keyBindingTable->blockSignals(true);
98 _ui->keyBindingTable->setItem(newRowCount-1,0,new QTableWidgetItem() );
99 _ui->keyBindingTable->setItem(newRowCount-1,1,new QTableWidgetItem() );
101 _ui->keyBindingTable->blockSignals(false);
103 // make sure user can see new row
104 _ui->keyBindingTable->scrollToItem(_ui->keyBindingTable->item(newRowCount-1,0));
107 bool KeyBindingEditor::eventFilter( QObject* watched , QEvent* event )
109 if ( watched == _ui->testAreaInputEdit )
111 if ( event->type() == QEvent::KeyPress )
113 QKeyEvent* keyEvent = (QKeyEvent*)event;
115 // The state here is currently set to the state that a newly started
116 // terminal in Konsole will be in ( which is also the same as the
117 // state just after a reset ), this has 'Ansi' turned on and all other
118 // states off.
120 // TODO: It may be useful to be able to specify the state in the 'test input'
121 // area, but preferably not in a way which clutters the UI with lots of
122 // checkboxes.
124 const KeyboardTranslator::States states = KeyboardTranslator::AnsiState;
126 KeyboardTranslator::Entry entry = _translator->findEntry( keyEvent->key() ,
127 keyEvent->modifiers(),
128 states );
130 if ( !entry.isNull() )
132 _ui->testAreaInputEdit->setText(entry.conditionToString());
133 _ui->testAreaOutputEdit->setText(entry.resultToString(true,keyEvent->modifiers()));
135 else
137 _ui->testAreaInputEdit->setText(keyEvent->text());
138 _ui->testAreaOutputEdit->setText(keyEvent->text());
141 keyEvent->accept();
142 return true;
145 return false;
148 void KeyBindingEditor::setDescription(const QString& newDescription)
150 _ui->descriptionEdit->setText(newDescription);
152 if ( _translator )
153 _translator->setDescription(newDescription);
155 QString KeyBindingEditor::description() const
157 return _ui->descriptionEdit->text();
160 void KeyBindingEditor::setup(const KeyboardTranslator* translator)
162 if ( _translator )
163 delete _translator;
165 _translator = new KeyboardTranslator(*translator);
167 // setup description edit
168 _ui->descriptionEdit->setText(translator->description());
170 // setup key binding table
171 setupKeyBindingTable(translator);
174 KeyboardTranslator* KeyBindingEditor::translator() const
176 return _translator;
179 void KeyBindingEditor::bindingTableItemChanged(QTableWidgetItem* item)
181 QTableWidgetItem* key = _ui->keyBindingTable->item( item->row() , 0 );
182 KeyboardTranslator::Entry existing = key->data(Qt::UserRole).value<KeyboardTranslator::Entry>();
184 QString condition = key->text();
185 QString result = _ui->keyBindingTable->item( item->row() , 1 )->text();
187 KeyboardTranslator::Entry entry = KeyboardTranslatorReader::createEntry(condition,result);
188 _translator->replaceEntry(existing,entry);
190 // block signals to prevent this slot from being called repeatedly
191 _ui->keyBindingTable->blockSignals(true);
193 key->setData(Qt::UserRole,QVariant::fromValue(entry));
195 _ui->keyBindingTable->blockSignals(false);
198 void KeyBindingEditor::setupKeyBindingTable(const KeyboardTranslator* translator)
200 disconnect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this ,
201 SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
203 QList<KeyboardTranslator::Entry> entries = translator->entries();
204 _ui->keyBindingTable->setRowCount(entries.count());
206 for ( int row = 0 ; row < entries.count() ; row++ )
208 const KeyboardTranslator::Entry& entry = entries.at(row);
210 QTableWidgetItem* keyItem = new QTableWidgetItem(entry.conditionToString());
211 keyItem->setData( Qt::UserRole , QVariant::fromValue(entry) );
213 QTableWidgetItem* textItem = new QTableWidgetItem(QString(entry.resultToString()));
215 _ui->keyBindingTable->setItem(row,0,keyItem);
216 _ui->keyBindingTable->setItem(row,1,textItem);
218 _ui->keyBindingTable->sortItems(0);
220 connect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this ,
221 SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
224 #include "KeyBindingEditor.moc"