| 1 | /* |
| 2 | Copyright (C) 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "keyboardshortcuts.h" |
| 24 | #include <QAction> |
| 25 | |
| 26 | KeyboardShortcuts::KeyboardShortcuts(QWidget *parent, const QVector<QAction *> keyActions) |
| 27 | : QDialog(parent), m_keyActions(keyActions) |
| 28 | { |
| 29 | m_modified = false; |
| 30 | setupUi(this); |
| 31 | setWindowTitle(tr("Keyboard Shortcuts")); |
| 32 | refreshTable(); |
| 33 | // QDialog::setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed)); |
| 34 | connect(restorePushButton, SIGNAL(released()), this, SLOT(restoreDefaults())); |
| 35 | connect(tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(assignShortcut(int, int))); |
| 36 | } |
| 37 | |
| 38 | KeyboardShortcuts::~KeyboardShortcuts() |
| 39 | { |
| 40 | for (int i = 0; i < m_keyActions.size(); i++) { |
| 41 | m_keyActions[i]->setShortcut(getShortcut(m_keyActions[i]->text().remove("&"))); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void KeyboardShortcuts::refreshTable() |
| 46 | { |
| 47 | tableWidget->setColumnWidth(0, 320); |
| 48 | tableWidget->setColumnWidth(1, 170); |
| 49 | tableWidget->setRowCount(m_keyActions.size()); |
| 50 | for (int i = 0; i < m_keyActions.size(); i++) { |
| 51 | QTableWidgetItem *name = tableWidget->item(i, 0); |
| 52 | QTableWidgetItem *key = tableWidget->item(i, 1); |
| 53 | if (name == 0) { |
| 54 | name = new QTableWidgetItem(); |
| 55 | tableWidget->setItem(i,0, name); |
| 56 | } |
| 57 | if (key == 0) { |
| 58 | key = new QTableWidgetItem(); |
| 59 | tableWidget->setItem(i,1, key); |
| 60 | } |
| 61 | name->setText(m_keyActions[i]->text().remove("&")); |
| 62 | key->setText(m_keyActions[i]->shortcut().toString()); |
| 63 | } |
| 64 | tableWidget->sortItems(0); |
| 65 | } |
| 66 | |
| 67 | bool KeyboardShortcuts::shortcutTaken(QString shortcut) |
| 68 | { |
| 69 | int rows = tableWidget->rowCount(); |
| 70 | for (int i = 0; i < rows; i++) { |
| 71 | QTableWidgetItem *key = tableWidget->item(i, 1); |
| 72 | if (key != 0 && key->text() == shortcut) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | QString KeyboardShortcuts::getShortcut(QString action) |
| 80 | { |
| 81 | int rows = tableWidget->rowCount(); |
| 82 | for (int i = 0; i < rows; i++) { |
| 83 | QTableWidgetItem *name = tableWidget->item(i, 0); |
| 84 | QTableWidgetItem *key = tableWidget->item(i, 1); |
| 85 | if (name != 0 && name->text() == action) { |
| 86 | if (key != 0) { |
| 87 | return key->text(); |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | return QString(); |
| 93 | } |
| 94 | |
| 95 | void KeyboardShortcuts::restoreDefaults() |
| 96 | { |
| 97 | emit restoreDefaultShortcuts(); // To have the main application restore the default shortcuts in the m_keyActions pointer |
| 98 | refreshTable(); |
| 99 | } |
| 100 | |
| 101 | void KeyboardShortcuts::assignShortcut(int row, int /*column*/) |
| 102 | { |
| 103 | Q_ASSERT(m_keyActions.size() > row); |
| 104 | Q_ASSERT(tableWidget->rowCount() > row); |
| 105 | QTableWidgetItem *nameItem = tableWidget->item(row, 0); |
| 106 | QTableWidgetItem *keyitem = tableWidget->item(row, 1); |
| 107 | if (nameItem == 0) { |
| 108 | nameItem = new QTableWidgetItem(); |
| 109 | tableWidget->setItem(row,0, nameItem); |
| 110 | } |
| 111 | if (keyitem == 0) { |
| 112 | keyitem = new QTableWidgetItem(); |
| 113 | tableWidget->setItem(row,1, keyitem); |
| 114 | } |
| 115 | QString name = nameItem->text(); |
| 116 | QString key = keyitem->text(); |
| 117 | KeySelector dialog(this, name, key); |
| 118 | if (dialog.exec() == QDialog::Accepted) { |
| 119 | if (dialog.newShortcut == "") |
| 120 | return; |
| 121 | if (shortcutTaken(dialog.newShortcut)) { |
| 122 | QMessageBox::warning(this, tr("Invalid shortcut"), |
| 123 | tr("Shortcut cannot be assigned.\nIt is already used."), |
| 124 | QMessageBox::Ok, |
| 125 | QMessageBox::Ok); |
| 126 | return; |
| 127 | } |
| 128 | keyitem->setText(dialog.newShortcut); |
| 129 | m_modified = true; |
| 130 | // refreshTable(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* ---------------------------------------------------*/ |
| 135 | |
| 136 | KeySelector::KeySelector(QWidget *parent, QString command, QString currentShortcut) |
| 137 | : QDialog(parent) |
| 138 | { |
| 139 | setupUi(this); |
| 140 | setWindowTitle(tr("Press Key Combination")); |
| 141 | commandLabel->setText(command); |
| 142 | currentLabel->setText(currentShortcut != "" ? currentShortcut: tr("None")); |
| 143 | newLabel->setText(""); |
| 144 | } |
| 145 | |
| 146 | KeySelector::~KeySelector() |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | void KeySelector::keyPressEvent(QKeyEvent *event) |
| 151 | { |
| 152 | int key = event->key(); |
| 153 | int modifiers = event->modifiers(); |
| 154 | // qDebug() << key << " " << modifiers; |
| 155 | if (key == 16777248 || key == 16777249 || key == 16777249 || key == 16777250 || key == 16777251) |
| 156 | return; // Only a modifier was pressed |
| 157 | QString keyName; |
| 158 | if (modifiers != 0) |
| 159 | keyName += QKeySequence(modifiers).toString(); |
| 160 | keyName += QKeySequence(key).toString(); |
| 161 | newLabel->setText(keyName); |
| 162 | newShortcut = keyName; |
| 163 | } |