~ubuntu-branches/ubuntu/saucy/konsole/saucy-proposed

« back to all changes in this revision

Viewing changes to src/KeyBindingEditor.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-06 14:29:24 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20120606142924-1rekqv6j25lw2k41
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "KeyBindingEditor.h"
22
22
 
23
23
// Qt
24
 
#include <QtGui/QHeaderView>
25
24
#include <QtGui/QKeyEvent>
26
25
 
27
 
// KDE
28
 
#include <KDebug>
29
 
 
30
26
// Konsole
31
27
#include "ui_KeyBindingEditor.h"
32
28
#include "KeyboardTranslator.h"
35
31
 
36
32
KeyBindingEditor::KeyBindingEditor(QWidget* parent)
37
33
    : QWidget(parent)
38
 
    , _translator(new KeyboardTranslator( QString() ))
 
34
    , _translator(new KeyboardTranslator(QString()))
39
35
{
40
36
    _ui = new Ui::KeyBindingEditor();
41
37
    _ui->setupUi(this);
42
38
 
43
39
    // description edit
44
 
    connect( _ui->descriptionEdit , SIGNAL(textChanged(QString)) , this , SLOT(setDescription(QString)) );
 
40
    connect(_ui->descriptionEdit , SIGNAL(textChanged(QString)) , this , SLOT(setDescription(QString)));
45
41
 
46
42
    // key bindings table
47
43
    _ui->keyBindingTable->setColumnCount(2);
55
51
    _ui->keyBindingTable->setSelectionBehavior(QAbstractItemView::SelectRows);
56
52
 
57
53
    // add and remove buttons
58
 
    _ui->addEntryButton->setIcon( KIcon("list-add") );
59
 
    _ui->removeEntryButton->setIcon( KIcon("list-remove") );
 
54
    _ui->addEntryButton->setIcon(KIcon("list-add"));
 
55
    _ui->removeEntryButton->setIcon(KIcon("list-remove"));
60
56
 
61
 
    connect( _ui->removeEntryButton , SIGNAL(clicked()) , this , SLOT(removeSelectedEntry()) );
62
 
    connect( _ui->addEntryButton , SIGNAL(clicked()) , this , SLOT(addNewEntry()) );
 
57
    connect(_ui->removeEntryButton , SIGNAL(clicked()) , this , SLOT(removeSelectedEntry()));
 
58
    connect(_ui->addEntryButton , SIGNAL(clicked()) , this , SLOT(addNewEntry()));
63
59
 
64
60
    // test area
65
61
    _ui->testAreaInputEdit->installEventFilter(this);
73
69
 
74
70
void KeyBindingEditor::removeSelectedEntry()
75
71
{
76
 
    QList<QTableWidgetItem*> selectedList =  _ui->keyBindingTable->selectedItems();
77
72
    QList<QTableWidgetItem*> uniqueList;
78
73
 
79
 
    //Filter unique items
80
 
    QListIterator<QTableWidgetItem*> iter( selectedList );
81
 
    while ( iter.hasNext() )
82
 
    {
83
 
        QTableWidgetItem* item = iter.next();
 
74
    foreach(QTableWidgetItem* item, _ui->keyBindingTable->selectedItems()) {
84
75
        if (item->column() == 1) //Select item at the first column
85
 
            item = _ui->keyBindingTable->item(item->row(),0);
 
76
            item = _ui->keyBindingTable->item(item->row(), 0);
86
77
 
87
 
        if ( !uniqueList.contains(item) )
 
78
        if (!uniqueList.contains(item))
88
79
            uniqueList.append(item);
89
80
    }
90
81
 
91
 
    iter = QListIterator<QTableWidgetItem*>( uniqueList );
92
 
    while ( iter.hasNext() )
93
 
    {     
 
82
    foreach(QTableWidgetItem* item, uniqueList) {
94
83
        // get the first item in the row which has the entry
95
 
        QTableWidgetItem* item = iter.next();
96
84
 
97
85
        KeyboardTranslator::Entry existing = item->data(Qt::UserRole).
98
 
                                                    value<KeyboardTranslator::Entry>();
 
86
                                             value<KeyboardTranslator::Entry>();
99
87
 
100
88
        _translator->removeEntry(existing);
101
89
 
102
 
        _ui->keyBindingTable->removeRow( item->row() );
 
90
        _ui->keyBindingTable->removeRow(item->row());
103
91
    }
104
92
}
105
93
 
106
94
void KeyBindingEditor::addNewEntry()
107
95
{
108
 
   _ui->keyBindingTable->insertRow( _ui->keyBindingTable->rowCount() );
109
 
 
110
 
   int newRowCount = _ui->keyBindingTable->rowCount();
111
 
 
112
 
   // block signals here to avoid triggering bindingTableItemChanged() slot call
113
 
   _ui->keyBindingTable->blockSignals(true);
114
 
 
115
 
   _ui->keyBindingTable->setItem(newRowCount-1,0,new QTableWidgetItem() );
116
 
   _ui->keyBindingTable->setItem(newRowCount-1,1,new QTableWidgetItem() );
117
 
 
118
 
   _ui->keyBindingTable->blockSignals(false);
119
 
 
120
 
   // make sure user can see new row
121
 
   _ui->keyBindingTable->scrollToItem(_ui->keyBindingTable->item(newRowCount-1,0));
 
96
    _ui->keyBindingTable->insertRow(_ui->keyBindingTable->rowCount());
 
97
 
 
98
    int newRowCount = _ui->keyBindingTable->rowCount();
 
99
 
 
100
    // block signals here to avoid triggering bindingTableItemChanged() slot call
 
101
    _ui->keyBindingTable->blockSignals(true);
 
102
 
 
103
    _ui->keyBindingTable->setItem(newRowCount - 1, 0, new QTableWidgetItem());
 
104
    _ui->keyBindingTable->setItem(newRowCount - 1, 1, new QTableWidgetItem());
 
105
 
 
106
    _ui->keyBindingTable->blockSignals(false);
 
107
 
 
108
    // make sure user can see new row
 
109
    _ui->keyBindingTable->scrollToItem(_ui->keyBindingTable->item(newRowCount - 1, 0));
122
110
}
123
111
 
124
 
bool KeyBindingEditor::eventFilter( QObject* watched , QEvent* event )
 
112
bool KeyBindingEditor::eventFilter(QObject* watched , QEvent* event)
125
113
{
126
 
    if ( watched == _ui->testAreaInputEdit )
127
 
    {
128
 
       if ( event->type() == QEvent::KeyPress )
129
 
       {
 
114
    if (watched == _ui->testAreaInputEdit) {
 
115
        if (event->type() == QEvent::KeyPress) {
130
116
            QKeyEvent* keyEvent = (QKeyEvent*)event;
131
117
 
132
 
            // The state here is currently set to the state that a newly started 
133
 
            // terminal in Konsole will be in ( which is also the same as the 
 
118
            // The state here is currently set to the state that a newly started
 
119
            // terminal in Konsole will be in ( which is also the same as the
134
120
            // state just after a reset ), this has 'Ansi' turned on and all other
135
121
            // states off.
136
122
            //
137
 
            // TODO: It may be useful to be able to specify the state in the 'test input' 
138
 
            // area, but preferably not in a way which clutters the UI with lots of 
 
123
            // TODO: It may be useful to be able to specify the state in the 'test input'
 
124
            // area, but preferably not in a way which clutters the UI with lots of
139
125
            // checkboxes.
140
126
            //
141
127
            const KeyboardTranslator::States states = KeyboardTranslator::AnsiState;
142
128
 
143
 
            KeyboardTranslator::Entry entry = _translator->findEntry( keyEvent->key() , 
144
 
                                                                      keyEvent->modifiers(), 
145
 
                                                                      states );
 
129
            KeyboardTranslator::Entry entry = _translator->findEntry(keyEvent->key() ,
 
130
                                              keyEvent->modifiers(),
 
131
                                              states);
146
132
 
147
 
            if ( !entry.isNull() )
148
 
            {
 
133
            if (!entry.isNull()) {
149
134
                _ui->testAreaInputEdit->setText(entry.conditionToString());
150
 
                _ui->testAreaOutputEdit->setText(entry.resultToString(true,keyEvent->modifiers()));
151
 
            }
152
 
            else
153
 
            {
 
135
                _ui->testAreaOutputEdit->setText(entry.resultToString(true, keyEvent->modifiers()));
 
136
            } else {
154
137
                _ui->testAreaInputEdit->setText(keyEvent->text());
155
138
                _ui->testAreaOutputEdit->setText(keyEvent->text());
156
139
            }
157
140
 
158
141
            keyEvent->accept();
159
142
            return true;
160
 
       } 
 
143
        }
161
144
    }
162
145
    return false;
163
146
}
164
147
 
165
148
void KeyBindingEditor::setDescription(const QString& newDescription)
166
149
{
167
 
     _ui->descriptionEdit->setText(newDescription);
 
150
    _ui->descriptionEdit->setText(newDescription);
168
151
 
169
 
     if ( _translator )
170
 
         _translator->setDescription(newDescription);
 
152
    if (_translator)
 
153
        _translator->setDescription(newDescription);
171
154
}
172
155
QString KeyBindingEditor::description() const
173
156
{
195
178
 
196
179
void KeyBindingEditor::bindingTableItemChanged(QTableWidgetItem* item)
197
180
{
198
 
   QTableWidgetItem* key = _ui->keyBindingTable->item( item->row() , 0 );
199
 
   KeyboardTranslator::Entry existing = key->data(Qt::UserRole).value<KeyboardTranslator::Entry>();
200
 
 
201
 
   QString condition = key->text();
202
 
   QString result = _ui->keyBindingTable->item( item->row() , 1 )->text();
203
 
 
204
 
   KeyboardTranslator::Entry entry = KeyboardTranslatorReader::createEntry(condition,result);
205
 
   _translator->replaceEntry(existing,entry);
 
181
    QTableWidgetItem* key = _ui->keyBindingTable->item(item->row() , 0);
 
182
    KeyboardTranslator::Entry existing = key->data(Qt::UserRole).value<KeyboardTranslator::Entry>();
 
183
 
 
184
    QString condition = key->text();
 
185
    QString result = _ui->keyBindingTable->item(item->row() , 1)->text();
 
186
 
 
187
    KeyboardTranslator::Entry entry = KeyboardTranslatorReader::createEntry(condition, result);
 
188
    _translator->replaceEntry(existing, entry);
206
189
 
207
190
    // block signals to prevent this slot from being called repeatedly
208
 
   _ui->keyBindingTable->blockSignals(true);
209
 
 
210
 
   key->setData(Qt::UserRole,QVariant::fromValue(entry));
211
 
 
212
 
   _ui->keyBindingTable->blockSignals(false);
 
191
    _ui->keyBindingTable->blockSignals(true);
 
192
 
 
193
    key->setData(Qt::UserRole, QVariant::fromValue(entry));
 
194
 
 
195
    _ui->keyBindingTable->blockSignals(false);
213
196
}
214
197
 
215
198
void KeyBindingEditor::setupKeyBindingTable(const KeyboardTranslator* translator)
216
199
{
217
 
    disconnect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this , 
218
 
            SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
 
200
    disconnect(_ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this ,
 
201
               SLOT(bindingTableItemChanged(QTableWidgetItem*)));
219
202
 
220
203
    QList<KeyboardTranslator::Entry> entries = translator->entries();
221
204
    _ui->keyBindingTable->setRowCount(entries.count());
222
205
 
223
 
    for ( int row = 0 ; row < entries.count() ; row++ )
224
 
    {
 
206
    for (int row = 0 ; row < entries.count() ; row++) {
225
207
        const KeyboardTranslator::Entry& entry = entries.at(row);
226
208
 
227
209
        QTableWidgetItem* keyItem = new QTableWidgetItem(entry.conditionToString());
228
 
        keyItem->setData( Qt::UserRole , QVariant::fromValue(entry) );
 
210
        keyItem->setData(Qt::UserRole , QVariant::fromValue(entry));
229
211
 
230
212
        QTableWidgetItem* textItem = new QTableWidgetItem(QString(entry.resultToString()));
231
213
 
232
 
        _ui->keyBindingTable->setItem(row,0,keyItem);
233
 
        _ui->keyBindingTable->setItem(row,1,textItem);
 
214
        _ui->keyBindingTable->setItem(row, 0, keyItem);
 
215
        _ui->keyBindingTable->setItem(row, 1, textItem);
234
216
    }
235
217
    _ui->keyBindingTable->sortItems(0);
236
218
 
237
 
    connect( _ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this , 
238
 
            SLOT(bindingTableItemChanged(QTableWidgetItem*)) );
 
219
    connect(_ui->keyBindingTable , SIGNAL(itemChanged(QTableWidgetItem*)) , this ,
 
220
            SLOT(bindingTableItemChanged(QTableWidgetItem*)));
239
221
}
240
222
 
241
223
#include "KeyBindingEditor.moc"