Qt: Add clear button for shortcut editor

This commit is contained in:
Jeffrey Pfau 2015-01-05 01:57:25 -08:00
parent 3854c7e401
commit c90d105e34
5 changed files with 49 additions and 43 deletions

View File

@ -193,7 +193,9 @@ void ShortcutController::updateKey(const QModelIndex& index, const QKeySequence&
if (!oldShortcut.isEmpty()) { if (!oldShortcut.isEmpty()) {
m_heldKeys.take(oldShortcut); m_heldKeys.take(oldShortcut);
} }
m_heldKeys[keySequence] = item; if (!keySequence.isEmpty()) {
m_heldKeys[keySequence] = item;
}
} }
item->setShortcut(keySequence); item->setShortcut(keySequence);
if (m_config) { if (m_config) {
@ -216,13 +218,23 @@ void ShortcutController::updateButton(const QModelIndex& index, int button) {
if (oldButton >= 0) { if (oldButton >= 0) {
m_buttons.take(oldButton); m_buttons.take(oldButton);
} }
m_buttons[button] = item; if (button >= 0) {
m_buttons[button] = item;
}
if (m_config) { if (m_config) {
m_config->setQtOption(item->name(), button, BUTTON_SECTION); m_config->setQtOption(item->name(), button, BUTTON_SECTION);
} }
emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer())); emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer()));
} }
void ShortcutController::clearKey(const QModelIndex& index) {
updateKey(index, QKeySequence());
}
void ShortcutController::clearButton(const QModelIndex& index) {
updateButton(index, -1);
}
bool ShortcutController::eventFilter(QObject*, QEvent* event) { bool ShortcutController::eventFilter(QObject*, QEvent* event) {
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);

View File

@ -92,6 +92,9 @@ public:
void updateKey(const QModelIndex& index, const QKeySequence& keySequence); void updateKey(const QModelIndex& index, const QKeySequence& keySequence);
void updateButton(const QModelIndex& index, int button); void updateButton(const QModelIndex& index, int button);
void clearKey(const QModelIndex& index);
void clearButton(const QModelIndex& index);
protected: protected:
bool eventFilter(QObject*, QEvent*) override; bool eventFilter(QObject*, QEvent*) override;

View File

@ -21,6 +21,7 @@ ShortcutView::ShortcutView(QWidget* parent)
connect(m_ui.keySequenceEdit, SIGNAL(editingFinished()), this, SLOT(updateKey())); connect(m_ui.keySequenceEdit, SIGNAL(editingFinished()), this, SLOT(updateKey()));
connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int))); connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&))); connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
} }
void ShortcutView::setController(ShortcutController* controller) { void ShortcutView::setController(ShortcutController* controller) {
@ -51,9 +52,29 @@ void ShortcutView::load(const QModelIndex& index) {
return; return;
} }
if (m_ui.gamepadButton->isChecked()) { if (m_ui.gamepadButton->isChecked()) {
loadButton(); m_ui.keyEdit->setFocus();
m_ui.keyEdit->setValueButton(-1); // There are no default bindings
} else { } else {
loadKey(action); m_ui.keySequenceEdit->setFocus();
m_ui.keySequenceEdit->setKeySequence(action->shortcut());
}
}
void ShortcutView::clear() {
if (!m_controller) {
return;
}
QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
const QAction* action = m_controller->actionAt(index);
if (!action || m_controller->isMenuAt(index)) {
return;
}
if (m_ui.gamepadButton->isChecked()) {
m_controller->clearButton(index);
m_ui.keyEdit->setValueButton(-1);
} else {
m_controller->clearKey(index);
m_ui.keySequenceEdit->setKeySequence(QKeySequence());
} }
} }
@ -69,15 +90,7 @@ void ShortcutView::updateButton(int button) {
if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) { if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
return; return;
} }
m_ui.keyEdit->clearFocus();
m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button); m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
} }
void ShortcutView::loadKey(const QAction* action) {
m_ui.keySequenceEdit->setFocus();
m_ui.keySequenceEdit->setKeySequence(action->shortcut());
}
void ShortcutView::loadButton() {
m_ui.keyEdit->setFocus();
m_ui.keyEdit->setValueButton(-1); // There are no default bindings
}

View File

@ -29,6 +29,7 @@ protected:
private slots: private slots:
void load(const QModelIndex&); void load(const QModelIndex&);
void clear();
void updateKey(); void updateKey();
void updateButton(int button); void updateButton(int button);

View File

@ -40,6 +40,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="clearButton">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="horizontalSpacer_2">
<property name="orientation"> <property name="orientation">
@ -61,18 +68,6 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>40</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
</widget> </widget>
</item> </item>
<item> <item>
@ -83,27 +78,9 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>40</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="visible"> <property name="visible">
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="placeholderText">
<string>Press button</string>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>