diff -r 3d18f7f71d65 -r 3443e0de2c9d QTfrontend/ui/page/pageoptions.cpp --- a/QTfrontend/ui/page/pageoptions.cpp Fri Dec 28 23:54:42 2012 +0100 +++ b/QTfrontend/ui/page/pageoptions.cpp Sat Dec 29 22:50:10 2012 +0100 @@ -23,14 +23,17 @@ #include #include #include +#include #include #include #include -#include +#include +#include #include #include #include #include +#include #include "pageoptions.h" #include "gameuiconfig.h" @@ -40,6 +43,8 @@ #include "DataManager.h" #include "LibavInteraction.h" #include "AutoUpdater.h" +#include "HWApplication.h" +#include "keybinder.h" #ifdef __APPLE__ #ifdef SPARKLE_ENABLED @@ -56,9 +61,15 @@ pageLayout->addWidget(tabs); QWidget * page1 = new QWidget(this); QWidget * page2 = new QWidget(this); + binder = new KeyBinder(this, tr("Select an action to change what key controls it"), tr("Reset to default"), tr("Reset all binds")); + connect(binder, SIGNAL(bindUpdate(int)), this, SLOT(bindUpdated(int))); + connect(binder, SIGNAL(resetAllBinds()), this, SLOT(resetAllBinds())); tabs->addTab(page1, tr("General")); + binderTab = tabs->addTab(binder, tr("Controls")); tabs->addTab(page2, tr("Advanced")); + connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tabIndexChanged(int))); + #ifdef VIDEOREC QWidget * page3 = new QWidget(this); tabs->addTab(page3, tr("Video Recording")); @@ -914,3 +925,57 @@ return true; } + +// When the current tab is switched +void PageOptions::tabIndexChanged(int index) +{ + if (index == binderTab) // Switched to bind tab + { + binder->resetInterface(); + + if (!config) return; + + QStandardItemModel * binds = DataManager::instance().bindsModel(); + for(int i = 0; i < BINDS_NUMBER; i++) + { + QString value = config->bind(i); + QModelIndexList mdl = binds->match(binds->index(0, 0), Qt::UserRole + 1, value, 1, Qt::MatchExactly); + if(mdl.size() == 1) binder->setBindIndex(i, mdl[0].row()); + } + } + + currentTab = index; +} + +// When a key bind combobox is changed +void PageOptions::bindUpdated(int bindID) +{ + int bindIndex = binder->bindIndex(bindID); + + if (bindIndex == 0) bindIndex = resetBindToDefault(bindID); + + // Save bind + QStandardItemModel * binds = DataManager::instance().bindsModel(); + QString strbind = binds->index(binder->bindIndex(bindID), 0).data(Qt::UserRole + 1).toString(); + config->setBind(bindID, strbind); +} + +// Changes a key bind (bindID) to its default value. This updates the bind's combo-box in the UI. +// Returns: The bind model index of the default. +int PageOptions::resetBindToDefault(int bindID) +{ + QStandardItemModel * binds = DataManager::instance().bindsModel(); + QModelIndexList mdl = binds->match(binds->index(0, 0), Qt::UserRole + 1, cbinds[bindID].strbind, 1, Qt::MatchExactly); + if(mdl.size() == 1) binder->setBindIndex(bindID, mdl[0].row()); + return mdl[0].row(); +} + +// Called when "reset all binds" button is pressed +void PageOptions::resetAllBinds() +{ + for (int i = 0; i < BINDS_NUMBER; i++) + { + resetBindToDefault(i); + bindUpdated(i); + } +}