Hotkey adding also via combo box

* do not split by "+", directly use string list (fixes hotkey "+" problem)
* encapsulated "addSelectionBox"
* renamed to "captureHotkey"
This commit is contained in:
Klaus Basan
2019-02-17 22:59:17 +01:00
committed by Mat Sutcliffe
parent 21acef505c
commit a56ae0302a
2 changed files with 70 additions and 33 deletions

View File

@@ -41,6 +41,7 @@
using namespace BlackMisc; using namespace BlackMisc;
using namespace BlackMisc::Input; using namespace BlackMisc::Input;
using namespace BlackCore; using namespace BlackCore;
using namespace BlackGui::Models;
namespace BlackGui namespace BlackGui
{ {
@@ -96,13 +97,13 @@ namespace BlackGui
} }
connect(ui->pb_AdvancedMode, &QPushButton::clicked, this, &CHotkeyDialog::advancedModeChanged); connect(ui->pb_AdvancedMode, &QPushButton::clicked, this, &CHotkeyDialog::advancedModeChanged);
connect(ui->pb_SelectedHotkey, &QPushButton::clicked, this, &CHotkeyDialog::selectHotkey); connect(ui->pb_SelectedHotkey, &QPushButton::clicked, this, &CHotkeyDialog::captureHotkey);
connect(ui->pb_Accept, &QPushButton::clicked, this, &CHotkeyDialog::accept); connect(ui->pb_Accept, &QPushButton::clicked, this, &CHotkeyDialog::accept);
connect(ui->pb_Cancel, &QPushButton::clicked, this, &CHotkeyDialog::reject); connect(ui->pb_Cancel, &QPushButton::clicked, this, &CHotkeyDialog::reject);
connect(ui->tv_Actions->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CHotkeyDialog::changeSelectedAction); connect(ui->tv_Actions->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CHotkeyDialog::changeSelectedAction);
connect(ui->cb_Identifier, qOverload<int>(&QComboBox::currentIndexChanged), this, &CHotkeyDialog::changeApplicableMachine); connect(ui->cb_Identifier, qOverload<int>(&QComboBox::currentIndexChanged), this, &CHotkeyDialog::changeApplicableMachine);
if (sGui) if (sGui && sGui->getInputManager())
{ {
connect(sGui->getInputManager(), &BlackCore::CInputManager::combinationSelectionChanged, this, &CHotkeyDialog::combinationSelectionChanged); connect(sGui->getInputManager(), &BlackCore::CInputManager::combinationSelectionChanged, this, &CHotkeyDialog::combinationSelectionChanged);
connect(sGui->getInputManager(), &BlackCore::CInputManager::combinationSelectionFinished, this, &CHotkeyDialog::combinationSelectionFinished); connect(sGui->getInputManager(), &BlackCore::CInputManager::combinationSelectionFinished, this, &CHotkeyDialog::combinationSelectionFinished);
@@ -153,7 +154,7 @@ namespace BlackGui
void CHotkeyDialog::advancedModeChanged() void CHotkeyDialog::advancedModeChanged()
{ {
if (m_actionHotkey.getCombination().isEmpty()) return; // if (m_actionHotkey.getCombination().isEmpty()) return;
if (!ui->qf_Advanced->isVisible()) if (!ui->qf_Advanced->isVisible())
{ {
setupAdvancedFrame(); setupAdvancedFrame();
@@ -168,8 +169,9 @@ namespace BlackGui
} }
} }
void CHotkeyDialog::selectHotkey() void CHotkeyDialog::captureHotkey()
{ {
if (!sApp || sApp->isShuttingDown()) { return; }
ui->pb_SelectedHotkey->setText("Press any key/button..."); ui->pb_SelectedHotkey->setText("Press any key/button...");
sApp->getInputManager()->startCapture(); sApp->getInputManager()->startCapture();
} }
@@ -190,7 +192,36 @@ namespace BlackGui
Q_UNUSED(deselected); Q_UNUSED(deselected);
if (selected.indexes().isEmpty()) { return; } if (selected.indexes().isEmpty()) { return; }
const auto index = selected.indexes().first(); const auto index = selected.indexes().first();
m_actionHotkey.setAction(index.data(Models::CActionModel::ActionRole).toString()); m_actionHotkey.setAction(index.data(CActionModel::ActionRole).toString());
}
CKeySelectionBox *CHotkeyDialog::addSelectionBox(const CKeyboardKeyList &allSupportedKeys, const QString &currentKey)
{
int currentIndex = -1;
const int width = qRound(1.5 * this->width());
const bool select = !currentKey.isEmpty();
CKeySelectionBox *ksb = new CKeySelectionBox(ui->qf_Advanced);
ksb->addItem(noKey()); // at front
for (const CKeyboardKey &supportedKey : allSupportedKeys)
{
QString supportedKeyAsString = supportedKey.toQString();
ksb->addItem(supportedKeyAsString, QVariant::fromValue(supportedKey));
if (select && supportedKeyAsString == currentKey)
{
currentIndex = ksb->count() - 1;
}
}
ksb->setSelectedIndex(currentIndex);
ksb->setPopupWidth(qMin(width, 600));
ksb->addItem(noKey()); // at back (easier to find it is there twice)
ui->qf_Advanced->layout()->addWidget(ksb);
const int position = ui->qf_Advanced->layout()->count() - 1;
ksb->setProperty("position", position);
connect(ksb, &CKeySelectionBox::keySelectionChanged, this, &CHotkeyDialog::advancedKeyChanged);
return ksb;
} }
void CHotkeyDialog::changeApplicableMachine(int index) void CHotkeyDialog::changeApplicableMachine(int index)
@@ -205,19 +236,19 @@ namespace BlackGui
{ {
if (m_actionHotkey.getApplicableMachine().getMachineName().isEmpty()) if (m_actionHotkey.getApplicableMachine().getMachineName().isEmpty())
{ {
CLogMessage().validationWarning(u"Missing hotkey '%1'") << ui->gb_Machine->title(); CLogMessage(this).validationWarning(u"Missing hotkey '%1'") << ui->gb_Machine->title();
return; return;
} }
if (m_actionHotkey.getCombination().isEmpty()) if (m_actionHotkey.getCombination().isEmpty())
{ {
CLogMessage().validationWarning(u"Missing hotkey '%1'") << ui->gb_Hotkey->title(); CLogMessage(this).validationWarning(u"Missing hotkey '%1'") << ui->gb_Hotkey->title();
return; return;
} }
if (m_actionHotkey.getAction().isEmpty()) if (m_actionHotkey.getAction().isEmpty())
{ {
CLogMessage().validationWarning(u"Missing hotkey '%1'") << ui->gb_Action->title(); CLogMessage(this).validationWarning(u"Missing hotkey '%1'") << ui->gb_Action->title();
return; return;
} }
@@ -245,30 +276,19 @@ namespace BlackGui
this->clearAdvancedFrame(); this->clearAdvancedFrame();
const CKeyboardKeyList allSupportedKeys = CKeyboardKeyList::allSupportedKeys(); const CKeyboardKeyList allSupportedKeys = CKeyboardKeyList::allSupportedKeys();
const QStringList splitKeys = m_actionHotkey.getCombination().toQString().split('+', QString::SkipEmptyParts); const QStringList splitKeys = m_actionHotkey.getCombination().getKeyStrings();
int c = 0;
for (const QString &splitKey : splitKeys) for (const QString &splitKey : splitKeys)
{ {
if (splitKey == "+") continue; this->addSelectionBox(allSupportedKeys, splitKey);
c++;
int currentIndex = -1;
CKeySelectionBox *ksb = new CKeySelectionBox(ui->qf_Advanced);
for (const CKeyboardKey &supportedKey : allSupportedKeys)
{
QString supportedKeyAsString = supportedKey.toQString();
ksb->addItem(supportedKeyAsString, QVariant::fromValue(supportedKey));
if (supportedKeyAsString == splitKey)
{
currentIndex = ksb->count() - 1;
} }
}
ksb->setSelectedIndex(currentIndex);
ui->qf_Advanced->layout()->addWidget(ksb);
const int position = ui->qf_Advanced->layout()->count() - 1;
ksb->setProperty("position", position);
connect(ksb, &CKeySelectionBox::keySelectionChanged, this, &CHotkeyDialog::advancedKeyChanged);
const int width = qRound(1.5 * this->width()); // add one box more so we can add keys
ksb->setPopupWidth(qMin(width, 600)); if (c < 2)
{
this->addSelectionBox(allSupportedKeys);
} }
} }
@@ -315,5 +335,11 @@ namespace BlackGui
QItemSelectionModel *selectionModel = ui->tv_Actions->selectionModel(); QItemSelectionModel *selectionModel = ui->tv_Actions->selectionModel();
selectionModel->select(parentIndex, QItemSelectionModel::Select); selectionModel->select(parentIndex, QItemSelectionModel::Select);
} }
const QString &CHotkeyDialog::noKey()
{
static const QString k = "[none]";
return k;
}
} // ns } // ns
} // ns } // ns

View File

@@ -15,8 +15,8 @@
#include "blackgui/models/actionmodel.h" #include "blackgui/models/actionmodel.h"
#include "blackgui/horizontalcombobox.h" #include "blackgui/horizontalcombobox.h"
#include "blackgui/blackguiexport.h" #include "blackgui/blackguiexport.h"
#include "blackmisc/identifierlist.h"
#include "blackmisc/input/actionhotkey.h" #include "blackmisc/input/actionhotkey.h"
#include "blackmisc/identifierlist.h"
#include <QDialog> #include <QDialog>
#include <QObject> #include <QObject>
@@ -24,9 +24,16 @@
class QItemSelection; class QItemSelection;
namespace BlackCore { class CInputManager; }
namespace BlackMisc { namespace Input { class CHotkeyCombination; } }
namespace Ui { class CHotkeyDialog; } namespace Ui { class CHotkeyDialog; }
namespace BlackCore { class CInputManager; }
namespace BlackMisc
{
namespace Input
{
class CHotkeyCombination;
class CKeyboardKeyList;
}
}
namespace BlackGui namespace BlackGui
{ {
namespace Components namespace Components
@@ -89,10 +96,11 @@ namespace BlackGui
private: private:
void advancedModeChanged(); void advancedModeChanged();
void selectHotkey(); void captureHotkey();
void combinationSelectionChanged(const BlackMisc::Input::CHotkeyCombination &combination); void combinationSelectionChanged(const BlackMisc::Input::CHotkeyCombination &combination);
void combinationSelectionFinished(const BlackMisc::Input::CHotkeyCombination &combination); void combinationSelectionFinished(const BlackMisc::Input::CHotkeyCombination &combination);
void changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected); void changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected);
CKeySelectionBox *addSelectionBox(const BlackMisc::Input::CKeyboardKeyList &allSupportedKeys, const QString &currentKey = {});
void changeApplicableMachine(int index); void changeApplicableMachine(int index);
void synchronize(); void synchronize();
@@ -106,6 +114,9 @@ namespace BlackGui
QScopedPointer<Ui::CHotkeyDialog> ui; QScopedPointer<Ui::CHotkeyDialog> ui;
BlackMisc::Input::CActionHotkey m_actionHotkey; BlackMisc::Input::CActionHotkey m_actionHotkey;
BlackGui::Models::CActionModel m_actionModel; BlackGui::Models::CActionModel m_actionModel;
//! "No key"
static const QString &noKey();
}; };
} // ns } // ns
} // ns } // ns