[Hotkey] Style

This commit is contained in:
Klaus Basan
2019-11-24 19:32:44 +01:00
committed by Mat Sutcliffe
parent 9dbb441e44
commit ef4640e66f
10 changed files with 54 additions and 60 deletions

View File

@@ -76,6 +76,7 @@ namespace BlackCore
void CInputManager::processKeyCombinationChanged(const CHotkeyCombination &combination) void CInputManager::processKeyCombinationChanged(const CHotkeyCombination &combination)
{ {
// Merge in the joystick part // Merge in the joystick part
// mixed on purpose, as any joystick keyboard combination is possible
CHotkeyCombination copy(combination); CHotkeyCombination copy(combination);
copy.setJoystickButtons(m_lastCombination.getJoystickButtons()); copy.setJoystickButtons(m_lastCombination.getJoystickButtons());
processCombination(copy); processCombination(copy);
@@ -83,7 +84,8 @@ namespace BlackCore
void CInputManager::processButtonCombinationChanged(const CHotkeyCombination &combination) void CInputManager::processButtonCombinationChanged(const CHotkeyCombination &combination)
{ {
// Merge in the keyboard dkeys // Merge in the keyboard keys
// mixed on purpose, as any joystick keyboard combination is possible
CHotkeyCombination copy(combination); CHotkeyCombination copy(combination);
copy.setKeyboardKeys(m_lastCombination.getKeyboardKeys()); copy.setKeyboardKeys(m_lastCombination.getKeyboardKeys());
processCombination(copy); processCombination(copy);
@@ -124,7 +126,7 @@ namespace BlackCore
{ {
m_keyboard = IKeyboard::create(this); m_keyboard = IKeyboard::create(this);
m_joystick = IJoystick::create(this); m_joystick = IJoystick::create(this);
connect(m_keyboard.get(), &IKeyboard::keyCombinationChanged, this, &CInputManager::processKeyCombinationChanged, Qt::QueuedConnection); connect(m_keyboard.get(), &IKeyboard::keyCombinationChanged, this, &CInputManager::processKeyCombinationChanged, Qt::QueuedConnection);
connect(m_joystick.get(), &IJoystick::buttonCombinationChanged, this, &CInputManager::processButtonCombinationChanged, Qt::QueuedConnection); connect(m_joystick.get(), &IJoystick::buttonCombinationChanged, this, &CInputManager::processButtonCombinationChanged, Qt::QueuedConnection);
} }

View File

@@ -113,7 +113,7 @@ namespace BlackGui
if (sGui && sGui->getInputManager()) 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);
} }
@@ -199,7 +199,7 @@ namespace BlackGui
void CHotkeyDialog::changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected) void CHotkeyDialog::changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected)
{ {
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(CActionModel::ActionRole).toString()); m_actionHotkey.setAction(index.data(CActionModel::ActionRole).toString());
@@ -265,7 +265,7 @@ namespace BlackGui
void CHotkeyDialog::changeApplicableMachine(int index) void CHotkeyDialog::changeApplicableMachine(int index)
{ {
Q_UNUSED(index); Q_UNUSED(index)
const QVariant userData = ui->cb_Identifier->currentData(); const QVariant userData = ui->cb_Identifier->currentData();
Q_ASSERT(userData.canConvert<CIdentifier>()); Q_ASSERT(userData.canConvert<CIdentifier>());
m_actionHotkey.setApplicableMachine(userData.value<CIdentifier>()); m_actionHotkey.setApplicableMachine(userData.value<CIdentifier>());

View File

@@ -24,8 +24,7 @@ namespace BlackGui
{ {
CActionHotkeyListModel::CActionHotkeyListModel(QObject *parent) : CActionHotkeyListModel::CActionHotkeyListModel(QObject *parent) :
QAbstractTableModel(parent) QAbstractTableModel(parent)
{ { }
}
int CActionHotkeyListModel::rowCount(const QModelIndex & /** parent **/) const int CActionHotkeyListModel::rowCount(const QModelIndex & /** parent **/) const
{ {
@@ -40,14 +39,13 @@ namespace BlackGui
QVariant CActionHotkeyListModel::data(const QModelIndex &index, int role) const QVariant CActionHotkeyListModel::data(const QModelIndex &index, int role) const
{ {
if (!index.isValid()) { return QVariant(); } if (!index.isValid()) { return QVariant(); }
if (index.row() >= m_actionHotkeys.size() || index.row() < 0) { return QVariant(); } if (index.row() >= m_actionHotkeys.size() || index.row() < 0) { return QVariant(); }
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
{ {
if (index.column() == 0) if (index.column() == 0)
{ {
BlackMisc::CIdentifier identifier = m_actionHotkeys[index.row()].getApplicableMachine(); const CIdentifier identifier = m_actionHotkeys[index.row()].getApplicableMachine();
return identifier.getMachineName(); return identifier.getMachineName();
} }
if (index.column() == 1) if (index.column() == 1)
@@ -62,7 +60,7 @@ namespace BlackGui
} }
else if (role == ActionHotkeyRole) else if (role == ActionHotkeyRole)
{ {
auto hotkey = m_actionHotkeys[index.row()]; const auto hotkey = m_actionHotkeys[index.row()];
return QVariant::fromValue(hotkey); return QVariant::fromValue(hotkey);
} }
return {}; return {};
@@ -72,15 +70,13 @@ namespace BlackGui
{ {
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
{ {
if (orientation == Qt::Horizontal) { if (orientation == Qt::Horizontal)
{
switch (section) switch (section)
{ {
case 0: case 0: return QStringLiteral("Machine");
return QStringLiteral("Machine"); case 1: return QStringLiteral("Combination");
case 1: case 2: return QStringLiteral("Action");
return QStringLiteral("Combination");
case 2:
return QStringLiteral("Action");
} }
} }
} }
@@ -89,7 +85,7 @@ namespace BlackGui
bool CActionHotkeyListModel::insertRows(int position, int rows, const QModelIndex &index) bool CActionHotkeyListModel::insertRows(int position, int rows, const QModelIndex &index)
{ {
Q_UNUSED(index); Q_UNUSED(index)
beginInsertRows(QModelIndex(), position, position + rows - 1); beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) for (int row = 0; row < rows; ++row)
@@ -103,7 +99,7 @@ namespace BlackGui
bool CActionHotkeyListModel::removeRows(int position, int rows, const QModelIndex &index) bool CActionHotkeyListModel::removeRows(int position, int rows, const QModelIndex &index)
{ {
Q_UNUSED(index); Q_UNUSED(index)
beginRemoveRows(QModelIndex(), position, position + rows - 1); beginRemoveRows(QModelIndex(), position, position + rows - 1);
Q_ASSERT(position + rows - 1 < m_actionHotkeys.size()); Q_ASSERT(position + rows - 1 < m_actionHotkeys.size());
@@ -136,6 +132,5 @@ namespace BlackGui
m_actionHotkeys.clear(); m_actionHotkeys.clear();
endResetModel(); endResetModel();
} }
} }
} // namespace } // namespace

View File

@@ -71,6 +71,7 @@ namespace BlackGui
private: private:
BlackMisc::Input::CActionHotkeyList m_actionHotkeys; BlackMisc::Input::CActionHotkeyList m_actionHotkeys;
}; };
} } // ns
} } // ns
#endif // guard #endif // guard

View File

@@ -69,13 +69,13 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexIdentifier: return CVariant::from(m_identifier); case IndexIdentifier: return CVariant::from(m_identifier);
case IndexIdentifierAsString: return CVariant::from(m_identifier.getMachineName()); case IndexIdentifierAsString: return CVariant::from(m_identifier.getMachineName());
case IndexAction: return CVariant::from(m_action); case IndexAction: return CVariant::from(m_action);
case IndexActionAsString: return CVariant::from(m_action); case IndexActionAsString: return CVariant::from(m_action);
case IndexCombination: return CVariant::from(m_combination); case IndexCombination: return CVariant::from(m_combination);
case IndexCombinationAsString: return CVariant::from(QString(m_combination.toQString())); case IndexCombinationAsString: return CVariant::from(QString(m_combination.toQString()));
default: return CValueObject::propertyByIndex(index); default: return CValueObject::propertyByIndex(index);
} }
} }

View File

@@ -100,9 +100,9 @@ namespace BlackMisc
QString convertToQString(bool i18n = false) const; QString convertToQString(bool i18n = false) const;
private: private:
CIdentifier m_identifier; //!< Identifier to which machine this hotkey belongs to CIdentifier m_identifier; //!< Identifier to which machine this hotkey belongs to
CHotkeyCombination m_combination; //!< hotkey combination CHotkeyCombination m_combination; //!< hotkey combination
QString m_action; //!< hotkey action QString m_action; //!< hotkey action
BLACK_METACLASS( BLACK_METACLASS(
CActionHotkey, CActionHotkey,

View File

@@ -102,7 +102,7 @@ namespace BlackMisc
QString CHotkeyCombination::convertToQString(bool i18n) const QString CHotkeyCombination::convertToQString(bool i18n) const
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n)
QStringList sl; QStringList sl;
sl.reserve(m_keyboardKeys.size() + m_joystickButtons.size()); sl.reserve(m_keyboardKeys.size() + m_joystickButtons.size());
for (const auto &key : m_keyboardKeys) for (const auto &key : m_keyboardKeys)

View File

@@ -98,7 +98,7 @@ namespace BlackMisc
QString asStringWithDeviceNames() const; QString asStringWithDeviceNames() const;
private: private:
CKeyboardKeyList m_keyboardKeys; CKeyboardKeyList m_keyboardKeys;
CJoystickButtonList m_joystickButtons; CJoystickButtonList m_joystickButtons;
BLACK_METACLASS( BLACK_METACLASS(

View File

@@ -7,7 +7,6 @@
*/ */
#include "blackmisc/input/joystickbutton.h" #include "blackmisc/input/joystickbutton.h"
#include "blackmisc/variant.h" #include "blackmisc/variant.h"
namespace BlackMisc namespace BlackMisc
@@ -20,7 +19,7 @@ namespace BlackMisc
QString CJoystickButton::getButtonAsStringWithDeviceName() const QString CJoystickButton::getButtonAsStringWithDeviceName() const
{ {
return QString("%1 Button%2").arg(m_deviceName).arg(m_buttonIndex); return QStringLiteral("%1%2 - %3").arg(buttonIndentifier()).arg(m_buttonIndex).arg(m_deviceName);
} }
void CJoystickButton::setButtonIndex(int buttonIndex) void CJoystickButton::setButtonIndex(int buttonIndex)
@@ -30,20 +29,19 @@ namespace BlackMisc
bool CJoystickButton::isValid() const bool CJoystickButton::isValid() const
{ {
if (!m_deviceName.isEmpty() && m_buttonIndex >= 0) { return true; } return (!m_deviceName.isEmpty() && m_buttonIndex >= 0);
else { return false; }
} }
void CJoystickButton::setButtonObject(CJoystickButton button) void CJoystickButton::setButtonObject(CJoystickButton button)
{ {
m_deviceName = button.m_deviceName; m_deviceName = button.m_deviceName;
m_buttonIndex = button.m_buttonIndex; m_buttonIndex = button.m_buttonIndex;
} }
void CJoystickButton::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) void CJoystickButton::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{ {
if (index.isMyself()) { (*this) = variant.to<CJoystickButton>(); return; } if (index.isMyself()) { (*this) = variant.to<CJoystickButton>(); return; }
ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexDeviceName: case IndexDeviceName:
@@ -65,13 +63,13 @@ namespace BlackMisc
CVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const CVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{ {
if (index.isMyself()) { return CVariant::from(*this); } if (index.isMyself()) { return CVariant::from(*this); }
ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexDeviceName: return CVariant::from(this->getDeviceName()); case IndexDeviceName: return CVariant::from(this->getDeviceName());
case IndexButton: return CVariant::from(this->getButtonIndex()); case IndexButton: return CVariant::from(this->getButtonIndex());
case IndexButtonAsString: return CVariant::from(this->getButtonAsString()); case IndexButtonAsString: return CVariant::from(this->getButtonAsString());
case IndexButtonObject: return CVariant::from(*this); case IndexButtonObject: return CVariant::from(*this);
} }
Q_ASSERT_X(false, "CJoystickButton", "index unknown"); Q_ASSERT_X(false, "CJoystickButton", "index unknown");
@@ -81,25 +79,22 @@ namespace BlackMisc
QString CJoystickButton::buttonIndexToString(qint32 buttonIndex) QString CJoystickButton::buttonIndexToString(qint32 buttonIndex)
{ {
QString buttonString("Button"); return buttonIndentifier() + QString::number(buttonIndex);
return buttonString.append(QStringLiteral("%1").arg(buttonIndex));
} }
int CJoystickButton::buttonIndexFromString(const QString &buttonName) int CJoystickButton::buttonIndexFromString(const QString &buttonName)
{ {
QString name("Button"); if (!buttonName.startsWith(buttonIndentifier())) { return getInvalidIndex(); }
if (!buttonName.startsWith(name)) return getInvalidIndex(); QString name(buttonName);
name.remove(buttonIndentifier());
name.remove("Button"); if (name.contains('-')) { name = name.mid(0, name.indexOf('-')); }
return name.toInt(); return name.trimmed().toInt();
} }
QString CJoystickButton::convertToQString(bool /* i18n */) const QString CJoystickButton::convertToQString(bool /* i18n */) const
{ {
QString s = getButtonAsString(); return getButtonAsString().trimmed();
return s.trimmed();
} }
} // namespace Hardware } // namespace
} // namespacexs
} // BlackMisc

View File

@@ -81,16 +81,17 @@ namespace BlackMisc
static int buttonIndexFromString(const QString &button); static int buttonIndexFromString(const QString &button);
//! Invalid button index //! Invalid button index
static int getInvalidIndex() { return m_invalidIndex; } static int getInvalidIndex() { return InvalidButtonIndex; }
//! \copydoc BlackMisc::Mixin::String::toQString //! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const; QString convertToQString(bool i18n = false) const;
private: private:
QString m_deviceName; static const QString &buttonIndentifier() { static const QString bn("Button"); return bn; }
int m_buttonIndex = m_invalidIndex; static constexpr int InvalidButtonIndex = -1;
static constexpr int m_invalidIndex = -1; QString m_deviceName;
int m_buttonIndex = InvalidButtonIndex;
BLACK_METACLASS( BLACK_METACLASS(
CJoystickButton, CJoystickButton,
@@ -98,8 +99,8 @@ namespace BlackMisc
BLACK_METAMEMBER(buttonIndex) BLACK_METAMEMBER(buttonIndex)
); );
}; };
} } // ns
} } // ns
Q_DECLARE_METATYPE(BlackMisc::Input::CJoystickButton) Q_DECLARE_METATYPE(BlackMisc::Input::CJoystickButton)