Changeset View
Changeset View
Standalone View
Standalone View
src/KeyBindingEditor.cpp
Show All 10 Lines | 1 | /* | |||
---|---|---|---|---|---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | 12 | GNU General Public License for more details. | ||
13 | 13 | | |||
14 | You should have received a copy of the GNU General Public License | 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 | 15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301 USA. | 17 | 02110-1301 USA. | ||
18 | */ | 18 | */ | ||
19 | | ||||
20 | // Own | 19 | // Own | ||
21 | #include "KeyBindingEditor.h" | 20 | #include "KeyBindingEditor.h" | ||
22 | 21 | | |||
23 | // Qt | 22 | // Qt | ||
23 | #include <QDialog> | ||||
24 | #include <QDialogButtonBox> | ||||
24 | #include <QKeyEvent> | 25 | #include <QKeyEvent> | ||
25 | #include <QIcon> | 26 | #include <QIcon> | ||
26 | 27 | | |||
27 | // KDE | 28 | // KDE | ||
28 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
30 | #include <KMessageBox> | ||||
29 | 31 | | |||
30 | // Konsole | 32 | // Konsole | ||
31 | #include "ui_KeyBindingEditor.h" | 33 | #include "ui_KeyBindingEditor.h" | ||
34 | #include "EditProfileDialog.h" | ||||
32 | #include "KeyboardTranslator.h" | 35 | #include "KeyboardTranslator.h" | ||
36 | #include "KeyboardTranslatorManager.h" | ||||
33 | 37 | | |||
34 | using namespace Konsole; | 38 | using namespace Konsole; | ||
35 | 39 | | |||
36 | KeyBindingEditor::KeyBindingEditor(QWidget *parent) : | 40 | KeyBindingEditor::KeyBindingEditor(QWidget *parent) : | ||
37 | QWidget(parent), | 41 | QDialog(parent), | ||
38 | _ui(nullptr), | 42 | _ui(nullptr), | ||
39 | _translator(new KeyboardTranslator(QString())) | 43 | _translator(new KeyboardTranslator(QString())), | ||
44 | _isNewTranslator(false) | ||||
40 | { | 45 | { | ||
46 | auto layout = new QVBoxLayout; | ||||
47 | | ||||
48 | auto mainWidget = new QWidget(this); | ||||
49 | layout->addWidget(mainWidget); | ||||
50 | | ||||
51 | auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); | ||||
52 | buttonBox->button(QDialogButtonBox::Cancel)->setDefault(true); | ||||
53 | layout->addWidget(buttonBox); | ||||
54 | | ||||
55 | setLayout(layout); | ||||
56 | | ||||
57 | connect(buttonBox, &QDialogButtonBox::accepted, this, &Konsole::KeyBindingEditor::accept); | ||||
58 | connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); | ||||
59 | | ||||
60 | setAttribute(Qt::WA_DeleteOnClose); | ||||
61 | | ||||
41 | _ui = new Ui::KeyBindingEditor(); | 62 | _ui = new Ui::KeyBindingEditor(); | ||
42 | _ui->setupUi(this); | 63 | _ui->setupUi(mainWidget); | ||
43 | 64 | | |||
44 | // description edit | 65 | // description edit | ||
66 | _ui->descriptionEdit->setPlaceholderText(i18nc("@label:textbox", "Enter descriptive label")); | ||||
hindenburg: I used this for the profile name entry - think it is OK here as well?
setPlaceholderText(i18nc… | |||||
45 | connect(_ui->descriptionEdit, &QLineEdit::textChanged, this, | 67 | connect(_ui->descriptionEdit, &QLineEdit::textChanged, this, | ||
46 | &Konsole::KeyBindingEditor::setTranslatorDescription); | 68 | &Konsole::KeyBindingEditor::setTranslatorDescription); | ||
69 | // filter edit | ||||
70 | connect(_ui->filterEdit, &QLineEdit::textChanged, this, | ||||
71 | &Konsole::KeyBindingEditor::filterRows); | ||||
47 | 72 | | |||
48 | // key bindings table | 73 | // key bindings table | ||
49 | _ui->keyBindingTable->setColumnCount(2); | 74 | _ui->keyBindingTable->setColumnCount(2); | ||
50 | 75 | | |||
51 | QStringList labels; | 76 | QStringList labels; | ||
52 | labels << i18n("Key Combination") << i18n("Output"); | 77 | labels << i18n("Key Combination") << i18n("Output"); | ||
53 | 78 | | |||
54 | _ui->keyBindingTable->setHorizontalHeaderLabels(labels); | 79 | _ui->keyBindingTable->setHorizontalHeaderLabels(labels); | ||
55 | _ui->keyBindingTable->horizontalHeader()->setStretchLastSection(true); | 80 | _ui->keyBindingTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||
56 | | ||||
57 | // see also the sizes set in EditProfileDialog::showKeyBindingEditor() | | |||
58 | _ui->keyBindingTable->setColumnWidth(0, 300); | | |||
59 | 81 | | |||
60 | _ui->keyBindingTable->verticalHeader()->hide(); | 82 | _ui->keyBindingTable->verticalHeader()->hide(); | ||
61 | _ui->keyBindingTable->setSelectionBehavior(QAbstractItemView::SelectRows); | 83 | _ui->keyBindingTable->setSelectionBehavior(QAbstractItemView::SelectRows); | ||
62 | 84 | | |||
63 | // add and remove buttons | 85 | // add and remove buttons | ||
64 | _ui->addEntryButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); | 86 | _ui->addEntryButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); | ||
65 | _ui->removeEntryButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); | 87 | _ui->removeEntryButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); | ||
66 | 88 | | |||
67 | connect(_ui->removeEntryButton, &QPushButton::clicked, this, | 89 | connect(_ui->removeEntryButton, &QPushButton::clicked, this, | ||
68 | &Konsole::KeyBindingEditor::removeSelectedEntry); | 90 | &Konsole::KeyBindingEditor::removeSelectedEntry); | ||
69 | connect(_ui->addEntryButton, &QPushButton::clicked, this, | 91 | connect(_ui->addEntryButton, &QPushButton::clicked, this, | ||
70 | &Konsole::KeyBindingEditor::addNewEntry); | 92 | &Konsole::KeyBindingEditor::addNewEntry); | ||
71 | 93 | | |||
72 | // test area | 94 | // test area | ||
73 | _ui->testAreaInputEdit->installEventFilter(this); | 95 | _ui->testAreaInputEdit->installEventFilter(this); | ||
74 | } | 96 | } | ||
75 | 97 | | |||
76 | KeyBindingEditor::~KeyBindingEditor() | 98 | KeyBindingEditor::~KeyBindingEditor() | ||
77 | { | 99 | { | ||
78 | delete _ui; | 100 | delete _ui; | ||
79 | delete _translator; | 101 | delete _translator; | ||
80 | } | 102 | } | ||
81 | 103 | | |||
104 | void KeyBindingEditor::filterRows(const QString &text) | ||||
105 | { | ||||
106 | const int rows = _ui->keyBindingTable->rowCount(); | ||||
107 | | ||||
108 | QList<int> matchedRows; | ||||
109 | | ||||
110 | for (QTableWidgetItem *matchedItem : _ui->keyBindingTable->findItems(text, Qt::MatchContains)) { | ||||
111 | matchedRows.append(matchedItem->row()); | ||||
112 | } | ||||
113 | | ||||
114 | for (int i = 0; i < rows; i++) { | ||||
115 | if (matchedRows.contains(i) && _ui->keyBindingTable->isRowHidden(i)) { | ||||
116 | _ui->keyBindingTable->showRow(i); | ||||
117 | } else if (!matchedRows.contains(i)) { | ||||
118 | _ui->keyBindingTable->hideRow(i); | ||||
119 | } | ||||
120 | } | ||||
121 | } | ||||
122 | | ||||
82 | void KeyBindingEditor::removeSelectedEntry() | 123 | void KeyBindingEditor::removeSelectedEntry() | ||
83 | { | 124 | { | ||
84 | QList<QTableWidgetItem *> uniqueList; | 125 | QList<QTableWidgetItem *> uniqueList; | ||
85 | 126 | | |||
86 | foreach (QTableWidgetItem *item, _ui->keyBindingTable->selectedItems()) { | 127 | foreach (QTableWidgetItem *item, _ui->keyBindingTable->selectedItems()) { | ||
87 | if (item->column() == 1) { //Select item at the first column | 128 | if (item->column() == 1) { //Select item at the first column | ||
88 | item = _ui->keyBindingTable->item(item->row(), 0); | 129 | item = _ui->keyBindingTable->item(item->row(), 0); | ||
89 | } | 130 | } | ||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 211 | { | |||
173 | } | 214 | } | ||
174 | } | 215 | } | ||
175 | 216 | | |||
176 | QString KeyBindingEditor::description() const | 217 | QString KeyBindingEditor::description() const | ||
177 | { | 218 | { | ||
178 | return _ui->descriptionEdit->text(); | 219 | return _ui->descriptionEdit->text(); | ||
179 | } | 220 | } | ||
180 | 221 | | |||
181 | void KeyBindingEditor::setup(const KeyboardTranslator *translator) | 222 | void KeyBindingEditor::setup(const KeyboardTranslator *translator, | ||
223 | const QString ¤tProfileTranslator, bool isNewTranslator) | ||||
182 | { | 224 | { | ||
183 | delete _translator; | 225 | delete _translator; | ||
184 | 226 | | |||
227 | _isNewTranslator = isNewTranslator; | ||||
228 | | ||||
229 | _currentProfileTranslator = currentProfileTranslator; | ||||
230 | | ||||
185 | _translator = new KeyboardTranslator(*translator); | 231 | _translator = new KeyboardTranslator(*translator); | ||
186 | 232 | | |||
187 | // setup description edit | 233 | // setup description edit line | ||
188 | _ui->descriptionEdit->setClearButtonEnabled(true); | 234 | _ui->descriptionEdit->setClearButtonEnabled(true); | ||
235 | // setup filter edit line | ||||
236 | _ui->filterEdit->setClearButtonEnabled(true); | ||||
237 | | ||||
238 | if (_isNewTranslator) { | ||||
239 | setDescription(i18n("New Key Binding List")); | ||||
240 | setWindowTitle(i18n("New Key Binding List")); | ||||
241 | } else { | ||||
189 | _ui->descriptionEdit->setText(translator->description()); | 242 | _ui->descriptionEdit->setText(translator->description()); | ||
243 | setWindowTitle(i18n("Edit Key Binding List")); | ||||
244 | } | ||||
190 | 245 | | |||
191 | // setup key binding table | 246 | // setup key binding table | ||
192 | setupKeyBindingTable(translator); | 247 | setupKeyBindingTable(translator); | ||
193 | } | 248 | } | ||
194 | 249 | | |||
195 | KeyboardTranslator *KeyBindingEditor::translator() const | 250 | KeyboardTranslator *KeyBindingEditor::translator() const | ||
196 | { | 251 | { | ||
197 | return _translator; | 252 | return _translator; | ||
Show All 37 Lines | 282 | for (int row = 0; row < entries.count(); row++) { | |||
235 | _ui->keyBindingTable->setItem(row, 0, keyItem); | 290 | _ui->keyBindingTable->setItem(row, 0, keyItem); | ||
236 | _ui->keyBindingTable->setItem(row, 1, textItem); | 291 | _ui->keyBindingTable->setItem(row, 1, textItem); | ||
237 | } | 292 | } | ||
238 | _ui->keyBindingTable->sortItems(0); | 293 | _ui->keyBindingTable->sortItems(0); | ||
239 | 294 | | |||
240 | connect(_ui->keyBindingTable, &QTableWidget::itemChanged, this, | 295 | connect(_ui->keyBindingTable, &QTableWidget::itemChanged, this, | ||
241 | &Konsole::KeyBindingEditor::bindingTableItemChanged); | 296 | &Konsole::KeyBindingEditor::bindingTableItemChanged); | ||
242 | } | 297 | } | ||
298 | | ||||
299 | void KeyBindingEditor::accept() | ||||
300 | { | ||||
301 | if (_translator == nullptr) { | ||||
302 | return; | ||||
303 | } | ||||
304 | | ||||
305 | const auto newTranslator = new KeyboardTranslator(*_translator); | ||||
306 | | ||||
307 | if (newTranslator->description().isEmpty()) { | ||||
308 | KMessageBox::sorry(this, i18n("A key bindings scheme cannot be saved with an empty description.")); | ||||
309 | return; | ||||
310 | } | ||||
311 | | ||||
312 | if (_isNewTranslator) { | ||||
313 | newTranslator->setName(newTranslator->description()); | ||||
314 | } | ||||
315 | | ||||
316 | KeyboardTranslatorManager::instance()->addTranslator(newTranslator); | ||||
317 | | ||||
318 | const QString ¤tTranslatorName = newTranslator->name(); | ||||
319 | | ||||
320 | emit updateKeyBindingsListRequest(currentTranslatorName); | ||||
321 | | ||||
322 | if (currentTranslatorName == _currentProfileTranslator) { | ||||
323 | emit updateTempProfileKeyBindingsRequest(Profile::KeyBindings, currentTranslatorName); | ||||
324 | } | ||||
325 | | ||||
326 | QDialog::accept(); | ||||
327 | } | ||||
328 | | ||||
329 | QSize KeyBindingEditor::sizeHint() const | ||||
330 | { | ||||
331 | const auto parent = parentWidget(); | ||||
332 | if (parent != nullptr) { | ||||
333 | return QSize(parent->width() * 0.9, parent->height() * 0.95); | ||||
334 | } | ||||
335 | | ||||
336 | return QSize(); | ||||
337 | } |
I used this for the profile name entry - think it is OK here as well?
setPlaceholderText(i18nc("@label:textbox", "Enter descriptive label"));