Clean QT Input UI

This commit is contained in:
Jasmine Iwanek
2025-05-09 21:01:54 -04:00
parent 8aae022441
commit 59c3254daf
3 changed files with 191 additions and 194 deletions

View File

@@ -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;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);
}
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);
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;x<NUM_ACCELS;x++) {
strcpy(acc_keys[x].name, acc_keys_t[x].name);
strcpy(acc_keys[x].desc, acc_keys_t[x].desc);
strcpy(acc_keys[x].seq, acc_keys_t[x].seq);
}
ProgSettings::reloadStrings();
// Copy accelerators from working set to global set
for(int x = 0; x < NUM_ACCELS; x++) {
strcpy(acc_keys[x].name, acc_keys_t[x].name);
strcpy(acc_keys[x].desc, acc_keys_t[x].desc);
strcpy(acc_keys[x].seq, acc_keys_t[x].seq);
}
ProgSettings::reloadStrings();
}
void
SettingsInput::onCurrentMachineChanged(int machineId)
{
@@ -115,13 +114,11 @@ SettingsInput::onCurrentMachineChanged(int machineId)
int selectedRow = 0;
for (int i = 0; i < mouse_get_ndev(); ++i) {
const auto *dev = mouse_get_device(i);
if ((i == MOUSE_TYPE_INTERNAL) && (machine_has_flags(machineId, MACHINE_MOUSE) == 0)) {
if ((i == MOUSE_TYPE_INTERNAL) && (machine_has_flags(machineId, MACHINE_MOUSE) == 0))
continue;
}
if (device_is_valid(dev, machineId) == 0) {
if (device_is_valid(dev, machineId) == 0)
continue;
}
QString name = DeviceConfig::DeviceName(dev, mouse_get_internal_name(i), 0);
int row = mouseModel->rowCount();
@@ -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;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));
}
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;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();
}
// 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<QPushButton *>(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)