diff --git a/src/qt/qt_settingsinput.cpp b/src/qt/qt_settingsinput.cpp index a7000414c..6d89c511a 100644 --- a/src/qt/qt_settingsinput.cpp +++ b/src/qt/qt_settingsinput.cpp @@ -38,7 +38,7 @@ extern "C" { #include "qt_keybind.hpp" extern MainWindow *main_window; - + // Temporary working copy of key list accelKey acc_keys_t[NUM_ACCELS]; @@ -48,41 +48,40 @@ SettingsInput::SettingsInput(QWidget *parent) { ui->setupUi(this); - QStringList horizontalHeader; - QStringList verticalHeader; - - horizontalHeader.append(tr("Action")); + QStringList horizontalHeader; + QStringList verticalHeader; + + horizontalHeader.append(tr("Action")); horizontalHeader.append(tr("Keybind")); - QTableWidget *keyTable = ui->tableKeys; - keyTable->setRowCount(10); - keyTable->setColumnCount(3); - keyTable->setColumnHidden(2, true); - keyTable->setColumnWidth(0, 200); - keyTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); - QStringList headers; - //headers << "Action" << "Bound key"; - keyTable->setHorizontalHeaderLabels(horizontalHeader); - keyTable->verticalHeader()->setVisible(false); - keyTable->setEditTriggers(QAbstractItemView::NoEditTriggers); - keyTable->setSelectionBehavior(QAbstractItemView::SelectRows); - keyTable->setSelectionMode(QAbstractItemView::SingleSelection); - keyTable->setShowGrid(true); - - // Make a working copy of acc_keys so we can check for dupes later without getting - // confused - for(int x=0;xtableKeys; + keyTable->setRowCount(10); + keyTable->setColumnCount(3); + keyTable->setColumnHidden(2, true); + keyTable->setColumnWidth(0, 200); + keyTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + QStringList headers; + //headers << "Action" << "Bound key"; + keyTable->setHorizontalHeaderLabels(horizontalHeader); + keyTable->verticalHeader()->setVisible(false); + keyTable->setEditTriggers(QAbstractItemView::NoEditTriggers); + keyTable->setSelectionBehavior(QAbstractItemView::SelectRows); + keyTable->setSelectionMode(QAbstractItemView::SingleSelection); + keyTable->setShowGrid(true); - refreshInputList(); + // Make a working copy of acc_keys so we can check for dupes later without getting + // confused + for(int x = 0; x < NUM_ACCELS; x++) { + strcpy(acc_keys_t[x].name, acc_keys[x].name); + strcpy(acc_keys_t[x].desc, acc_keys[x].desc); + strcpy(acc_keys_t[x].seq, acc_keys[x].seq); + } + + refreshInputList(); onCurrentMachineChanged(machine); } - SettingsInput::~SettingsInput() { delete ui; @@ -93,16 +92,16 @@ SettingsInput::save() { mouse_type = ui->comboBoxMouse->currentData().toInt(); joystick_type = ui->comboBoxJoystick->currentData().toInt(); - - // Copy accelerators from working set to global set - for(int x=0;xrowCount(); @@ -131,9 +128,8 @@ SettingsInput::onCurrentMachineChanged(int machineId) mouseModel->setData(idx, name, Qt::DisplayRole); mouseModel->setData(idx, i, Qt::UserRole); - if (i == mouse_type) { + if (i == mouse_type) selectedRow = row - removeRows; - } } mouseModel->removeRows(0, removeRows); ui->comboBoxMouse->setCurrentIndex(selectedRow); @@ -141,13 +137,12 @@ SettingsInput::onCurrentMachineChanged(int machineId) int i = 0; const char *joyName = joystick_get_name(i); auto *joystickModel = ui->comboBoxJoystick->model(); - removeRows = joystickModel->rowCount(); - selectedRow = 0; + removeRows = joystickModel->rowCount(); + selectedRow = 0; while (joyName) { int row = Models::AddEntry(joystickModel, tr(joyName).toUtf8().data(), i); - if (i == joystick_type) { + if (i == joystick_type) selectedRow = row - removeRows; - } ++i; joyName = joystick_get_name(i); @@ -159,95 +154,95 @@ SettingsInput::onCurrentMachineChanged(int machineId) void SettingsInput::refreshInputList() { - - for (int x=0;xtableKeys->setItem(x, 0, new QTableWidgetItem(tr(acc_keys_t[x].desc))); - ui->tableKeys->setItem(x, 1, new QTableWidgetItem(QKeySequence(acc_keys_t[x].seq, QKeySequence::PortableText).toString(QKeySequence::NativeText))); - ui->tableKeys->setItem(x, 2, new QTableWidgetItem(acc_keys_t[x].name)); - } + for (int x = 0; x < NUM_ACCELS; x++) { + ui->tableKeys->setItem(x, 0, new QTableWidgetItem(tr(acc_keys_t[x].desc))); + ui->tableKeys->setItem(x, 1, new QTableWidgetItem(QKeySequence(acc_keys_t[x].seq, QKeySequence::PortableText).toString(QKeySequence::NativeText))); + ui->tableKeys->setItem(x, 2, new QTableWidgetItem(acc_keys_t[x].name)); + } } void SettingsInput::on_tableKeys_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) { - // Enable/disable bind/clear buttons if user clicked valid row - QTableWidgetItem *cell = ui->tableKeys->item(currentRow,1); - if (!cell) - { - ui->pushButtonBind->setEnabled(false); - ui->pushButtonClearBind->setEnabled(false); - } - else - { - ui->pushButtonBind->setEnabled(true); - ui->pushButtonClearBind->setEnabled(true); - } + // Enable/disable bind/clear buttons if user clicked valid row + QTableWidgetItem *cell = ui->tableKeys->item(currentRow,1); + if (!cell) { + ui->pushButtonBind->setEnabled(false); + ui->pushButtonClearBind->setEnabled(false); + } else { + ui->pushButtonBind->setEnabled(true); + ui->pushButtonClearBind->setEnabled(true); + } } void SettingsInput::on_tableKeys_cellDoubleClicked(int row, int col) { - // Edit bind - QTableWidgetItem *cell = ui->tableKeys->item(row,1); - if (!cell) return; - - QKeySequence keyseq = KeyBinder::BindKey(this, cell->text()); - if (keyseq != false) { - // If no change was made, don't change anything. - if (keyseq.toString(QKeySequence::NativeText) == cell->text()) return; - - // Otherwise, check for conflicts. - // Check against the *working* copy - NOT the one in use by the app, - // so we don't test against shortcuts the user already changed. - for(int x=0;xshowMessage(MBX_ANSI & MBX_INFO, "Bind conflict", "This key combo is already in use", false); - return; - } - } - // If we made it here, there were no conflicts. - // Go ahead and apply the bind. - - // Find the correct accelerator key entry - int accKeyID = FindAccelerator(ui->tableKeys->item(row,2)->text().toUtf8().constData()); - if (accKeyID < 0) return; // this should never happen - - // Make the change - cell->setText(keyseq.toString(QKeySequence::NativeText)); - strcpy(acc_keys_t[accKeyID].seq, keyseq.toString(QKeySequence::PortableText).toUtf8().constData()); - - refreshInputList(); - } + // Edit bind + QTableWidgetItem *cell = ui->tableKeys->item(row,1); + if (!cell) + return; + + QKeySequence keyseq = KeyBinder::BindKey(this, cell->text()); + if (keyseq != false) { + // If no change was made, don't change anything. + if (keyseq.toString(QKeySequence::NativeText) == cell->text()) + return; + + // Otherwise, check for conflicts. + // Check against the *working* copy - NOT the one in use by the app, + // so we don't test against shortcuts the user already changed. + for(int x = 0; x < NUM_ACCELS; x++) { + if(QString::fromStdString(acc_keys_t[x].seq) == keyseq.toString(QKeySequence::PortableText)) { + // That key is already in use + main_window->showMessage(MBX_ANSI & MBX_INFO, "Bind conflict", "This key combo is already in use", false); + return; + } + } + // If we made it here, there were no conflicts. + // Go ahead and apply the bind. + + // Find the correct accelerator key entry + int accKeyID = FindAccelerator(ui->tableKeys->item(row,2)->text().toUtf8().constData()); + if (accKeyID < 0) + return; // this should never happen + + // Make the change + cell->setText(keyseq.toString(QKeySequence::NativeText)); + strcpy(acc_keys_t[accKeyID].seq, keyseq.toString(QKeySequence::PortableText).toUtf8().constData()); + + refreshInputList(); + } } void SettingsInput::on_pushButtonBind_clicked() { - // Edit bind - QTableWidgetItem *cell = ui->tableKeys->currentItem(); - if (!cell) return; - - on_tableKeys_cellDoubleClicked(cell->row(), cell->column()); + // Edit bind + QTableWidgetItem *cell = ui->tableKeys->currentItem(); + if (!cell) + return; + + on_tableKeys_cellDoubleClicked(cell->row(), cell->column()); } void SettingsInput::on_pushButtonClearBind_clicked() { - // Wipe bind - QTableWidgetItem *cell = ui->tableKeys->item(ui->tableKeys->currentRow(), 1); - if (!cell) return; - - cell->setText(""); - // Find the correct accelerator key entry - int accKeyID = FindAccelerator(ui->tableKeys->item(cell->row(),2)->text().toUtf8().constData()); - if (accKeyID < 0) return; // this should never happen - - // Make the change - cell->setText(""); - strcpy(acc_keys_t[accKeyID].seq, ""); + // Wipe bind + QTableWidgetItem *cell = ui->tableKeys->item(ui->tableKeys->currentRow(), 1); + if (!cell) + return; + + cell->setText(""); + // Find the correct accelerator key entry + int accKeyID = FindAccelerator(ui->tableKeys->item(cell->row(),2)->text().toUtf8().constData()); + if (accKeyID < 0) + return; // this should never happen + + // Make the change + cell->setText(""); + strcpy(acc_keys_t[accKeyID].seq, ""); } void @@ -263,9 +258,9 @@ SettingsInput::on_comboBoxJoystick_currentIndexChanged(int index) int joystickId = ui->comboBoxJoystick->currentData().toInt(); for (int i = 0; i < MAX_JOYSTICKS; ++i) { auto *btn = findChild(QString("pushButtonJoystick%1").arg(i + 1)); - if (btn == nullptr) { + if (btn == nullptr) continue; - } + btn->setEnabled(joystick_get_max_joysticks(joystickId) > i); } } @@ -283,9 +278,8 @@ get_axis(JoystickConfiguration &jc, int axis, int joystick_nr) int axis_sel = jc.selectedAxis(axis); int nr_axes = plat_joystick_state[joystick_state[0][joystick_nr].plat_joystick_nr - 1].nr_axes; - if (axis_sel < nr_axes) { + if (axis_sel < nr_axes) return axis_sel; - } axis_sel -= nr_axes; if (axis_sel & 1) diff --git a/src/qt/qt_settingsinput.hpp b/src/qt/qt_settingsinput.hpp index 742421f64..a51ad2564 100644 --- a/src/qt/qt_settingsinput.hpp +++ b/src/qt/qt_settingsinput.hpp @@ -26,17 +26,20 @@ public slots: void onCurrentMachineChanged(int machineId); private slots: - void on_pushButtonConfigureMouse_clicked(); - void on_comboBoxJoystick_currentIndexChanged(int index); void on_comboBoxMouse_currentIndexChanged(int index); + void on_pushButtonConfigureMouse_clicked(); + + void on_comboBoxJoystick_currentIndexChanged(int index); void on_pushButtonJoystick1_clicked(); void on_pushButtonJoystick2_clicked(); void on_pushButtonJoystick3_clicked(); void on_pushButtonJoystick4_clicked(); + void on_tableKeys_cellDoubleClicked(int row, int col); void on_tableKeys_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn); - void on_pushButtonBind_clicked(); + void on_pushButtonClearBind_clicked(); + void on_pushButtonBind_clicked(); private: Ui::SettingsInput *ui; diff --git a/src/qt/qt_settingsinput.ui b/src/qt/qt_settingsinput.ui index b7074eeaa..6ac6cf38a 100644 --- a/src/qt/qt_settingsinput.ui +++ b/src/qt/qt_settingsinput.ui @@ -23,6 +23,13 @@ 0 + + + + Mouse: + + + @@ -36,75 +43,6 @@ - - - - Joystick 4... - - - - - - - Joystick 3... - - - - - - - false - - - Bind - - - - - - - Joystick 1... - - - - - - - 30 - - - - - - - Mouse: - - - - - - - false - - - Clear binding - - - - - - - Joystick: - - - - - - - Joystick 2... - - - @@ -118,8 +56,50 @@ + + + + Joystick: + + + + + + + 30 + + + + + + + Joystick 1... + + + + + + + Joystick 2... + + + + + + + Joystick 3... + + + + + + + Joystick 4... + + + - + Key Bindings: @@ -144,6 +124,26 @@ + + + + false + + + Clear binding + + + + + + + false + + + Bind + + +