From 63c7c6be0dc6d8378bfc88c855d576eeec1a238e Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Fri, 14 Aug 2015 19:35:13 +0200 Subject: [PATCH] refs #453 Refactor Input value classes All previous Hardware classes have been refactored and moved into the Input namespace. --- src/blackmisc/blackmisc.pro | 2 + src/blackmisc/blackmiscfreefunctions.cpp | 13 +- src/blackmisc/blackmiscfreefunctions.h | 5 +- src/blackmisc/input/actionhotkey.cpp | 94 +++++++++++ src/blackmisc/input/actionhotkey.h | 100 ++++++++++++ src/blackmisc/input/actionhotkeylist.cpp | 46 ++++++ src/blackmisc/input/actionhotkeylist.h | 66 ++++++++ src/blackmisc/input/hotkeycombination.cpp | 80 ++++++++++ src/blackmisc/input/hotkeycombination.h | 90 +++++++++++ src/blackmisc/input/input.h | 18 +++ src/blackmisc/input/joystickbutton.cpp | 94 +++++++++++ src/blackmisc/input/joystickbutton.h | 87 ++++++++++ src/blackmisc/input/joystickbuttonlist.cpp | 22 +++ src/blackmisc/input/joystickbuttonlist.h | 49 ++++++ src/blackmisc/input/keyboardkey.cpp | 109 +++++++++++++ src/blackmisc/input/keyboardkey.h | 97 ++++++++++++ src/blackmisc/input/keyboardkeylist.cpp | 75 +++++++++ src/blackmisc/input/keyboardkeylist.h | 54 +++++++ src/blackmisc/input/keycodes.h | 73 +++++++++ tests/blackmisc/testblackmiscmain.cpp | 3 + tests/blackmisc/testinput.cpp | 175 +++++++++++++++++++++ tests/blackmisc/testinput.h | 45 ++++++ 22 files changed, 1388 insertions(+), 9 deletions(-) create mode 100644 src/blackmisc/input/actionhotkey.cpp create mode 100644 src/blackmisc/input/actionhotkey.h create mode 100644 src/blackmisc/input/actionhotkeylist.cpp create mode 100644 src/blackmisc/input/actionhotkeylist.h create mode 100644 src/blackmisc/input/hotkeycombination.cpp create mode 100644 src/blackmisc/input/hotkeycombination.h create mode 100644 src/blackmisc/input/input.h create mode 100644 src/blackmisc/input/joystickbutton.cpp create mode 100644 src/blackmisc/input/joystickbutton.h create mode 100644 src/blackmisc/input/joystickbuttonlist.cpp create mode 100644 src/blackmisc/input/joystickbuttonlist.h create mode 100644 src/blackmisc/input/keyboardkey.cpp create mode 100644 src/blackmisc/input/keyboardkey.h create mode 100644 src/blackmisc/input/keyboardkeylist.cpp create mode 100644 src/blackmisc/input/keyboardkeylist.h create mode 100644 src/blackmisc/input/keycodes.h create mode 100644 tests/blackmisc/testinput.cpp create mode 100644 tests/blackmisc/testinput.h diff --git a/src/blackmisc/blackmisc.pro b/src/blackmisc/blackmisc.pro index 2e586797c..fd0a16c4f 100644 --- a/src/blackmisc/blackmisc.pro +++ b/src/blackmisc/blackmisc.pro @@ -33,6 +33,7 @@ HEADERS += *.h \ $$PWD/math/*.h \ $$PWD/network/*.h \ $$PWD/geo/*.h \ + $$PWD/input/*.h \ $$PWD/hardware/*.h \ $$PWD/audio/*.h \ $$PWD/simulation/*.h \ @@ -45,6 +46,7 @@ SOURCES += *.cpp \ $$PWD/aviation/*.cpp \ $$PWD/math/*.cpp \ $$PWD/network/*.cpp \ + $$PWD/input/*.cpp \ $$PWD/hardware/*.cpp \ $$PWD/geo/*.cpp \ $$PWD/audio/*.cpp \ diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 2d66f01e2..1fdab65a5 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -12,6 +12,7 @@ #include "geo/geo.h" #include "audio/audio.h" #include "hardware/hardware.h" +#include "input/input.h" #include "settingsblackmiscclasses.h" #include "propertyindexlist.h" #include "propertyindexvariantmap.h" @@ -55,16 +56,15 @@ void BlackMisc::Audio::registerMetadata() CVoiceRoomList::registerMetadata(); } -void BlackMisc::Hardware::registerMetadata() +void BlackMisc::Input::registerMetadata() { CKeyboardKey::registerMetadata(); CKeyboardKeyList::registerMetadata(); CJoystickButton::registerMetadata(); -} - -void BlackMisc::Event::registerMetadata() -{ - CEventHotkeyFunction::registerMetadata(); + CJoystickButtonList::registerMetadata(); + CActionHotkey::registerMetadata(); + CActionHotkeyList::registerMetadata(); + CHotkeyCombination::registerMetadata(); } void BlackMisc::registerMetadata() @@ -101,6 +101,7 @@ void BlackMisc::registerMetadata() Hardware::registerMetadata(); Event::registerMetadata(); Weather::registerMetadata(); + Input::registerMetadata(); // needed by XBus proxy class qRegisterMetaType>(); diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index 536a5de49..6e539ebcc 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -90,9 +90,9 @@ namespace BlackMisc BLACKMISC_EXPORT bool startWindowsMixer(); } - namespace Hardware + namespace Input { - //! Register metadata for Hardware + //! Register metadata for Input BLACKMISC_EXPORT void registerMetadata(); } @@ -328,7 +328,6 @@ namespace BlackMisc //! Merges an incremental json object into an existing one BLACKMISC_EXPORT QJsonObject applyIncrementalObject(const QJsonObject &previousObject, const QJsonObject &incrementalObject); - } // BlackMisc #endif // guard diff --git a/src/blackmisc/input/actionhotkey.cpp b/src/blackmisc/input/actionhotkey.cpp new file mode 100644 index 000000000..35f06eeb6 --- /dev/null +++ b/src/blackmisc/input/actionhotkey.cpp @@ -0,0 +1,94 @@ +/* Copyright (C) 2015 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "actionhotkey.h" +#include "blackmiscfreefunctions.h" +#include "variant.h" + +namespace BlackMisc +{ + namespace Input + { + CActionHotkey::CActionHotkey(const QString &action) : + m_action(action) + {} + + CActionHotkey::CActionHotkey(const CIdentifier &identifier, const CHotkeyCombination &combination, const QString &action) : + m_identifier(identifier), m_combination(combination), m_action(action) + {} + + QString CActionHotkey::convertToQString(bool /* i18n */) const + { + QString s; + s += m_identifier.getMachineName(); + s += " "; + s += m_combination.toQString(); + s += " "; + s += m_action; + return s; + } + + void CActionHotkey::setCombination(const CHotkeyCombination &combination) + { + m_combination = combination; + } + + void CActionHotkey::setObject(const CActionHotkey &obj) + { + m_action = obj.m_action; + m_combination = obj.m_combination; + } + + CVariant CActionHotkey::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexIdentifier: + return CVariant::from(m_identifier); + case IndexIdentifierAsString: + return CVariant::from(m_identifier.getMachineName()); + case IndexAction: + return CVariant::from(m_action); + case IndexActionAsString: + return CVariant::from(m_action); + case IndexCombination: + return CVariant::from(m_combination); + case IndexCombinationAsString: + return CVariant::from(QString(m_combination.toQString())); + default: + return CValueObject::propertyByIndex(index); + } + } + + void CActionHotkey::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexAction: + { + m_action = variant.to(); + break; + } + case IndexCombination: + case IndexCombinationAsString: + m_combination = variant.to(); + case IndexObject: + this->setObject(variant.value()); + break; + default: + CValueObject::setPropertyByIndex(variant, index); + break; + } + } + } +} // BlackMisc diff --git a/src/blackmisc/input/actionhotkey.h b/src/blackmisc/input/actionhotkey.h new file mode 100644 index 000000000..6de2dd788 --- /dev/null +++ b/src/blackmisc/input/actionhotkey.h @@ -0,0 +1,100 @@ +/* Copyright (C) 2015 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_ACTIONHOTKEY_H +#define BLACKMISC_INPUT_ACTIONHOTKEY_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/input/hotkeycombination.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/identifier.h" +#include + +namespace BlackMisc +{ + namespace Input + { + //! Value object encapsulating a action hotkey + class BLACKMISC_EXPORT CActionHotkey : public CValueObject + { + public: + //! Properties by index + enum ColumnIndex + { + IndexIdentifier = BlackMisc::CPropertyIndex::GlobalIndexCSettingKeyboardHotkey, + IndexIdentifierAsString, + IndexCombination, + IndexCombinationAsString, + IndexAction, + IndexActionAsString, + IndexObject, // just for updates + }; + + //! Default constructor + CActionHotkey() = default; + + //! Constructor + CActionHotkey(const QString &action); + + //! Constructor + CActionHotkey(const CIdentifier &identifier, const CHotkeyCombination &combination, const QString &action); + + //! Get hotkey combination + const CHotkeyCombination &getCombination() const { return m_combination; } + + //! Set hotkey combination + void setCombination(const CHotkeyCombination &combination); + + //! Action + QString getAction() const { return m_action; } + + //! Set function + void setAction(const QString &action) { m_action = action; } + + //! Set applicable machine + void setApplicableMachine(const CIdentifier &identifier) { m_identifier = identifier; } + + //! Get applicable machine + CIdentifier getApplicableMachine() const { return m_identifier; } + + //! Set object + void setObject(const CActionHotkey &obj); + + //! Is hotkey valid? + bool isValid() const { return !m_identifier.getMachineName().isEmpty() && !m_combination.isEmpty() && !m_action.isEmpty(); } + + //! \copydoc CValueObject::setPropertyByIndex + void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index); + + //! \copydoc CValueObject::propertyByIndex + CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc CValueObject::convertToQString + QString convertToQString(bool i18n = false) const; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CActionHotkey) + CIdentifier m_identifier; //!< Identifier to which machine this hotkey belongs to + CHotkeyCombination m_combination; //!< hotkey combination + QString m_action; //!< hotkey action + }; + } +} + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Input::CActionHotkey, ( + attr(o.m_identifier), + attr(o.m_combination), + attr(o.m_action) + )) +Q_DECLARE_METATYPE(BlackMisc::Input::CActionHotkey) + +#endif // guard diff --git a/src/blackmisc/input/actionhotkeylist.cpp b/src/blackmisc/input/actionhotkeylist.cpp new file mode 100644 index 000000000..57b50d460 --- /dev/null +++ b/src/blackmisc/input/actionhotkeylist.cpp @@ -0,0 +1,46 @@ +/* Copyright (C) 2013 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "actionhotkeylist.h" + +namespace BlackMisc +{ + namespace Input + { + CActionHotkeyList::CActionHotkeyList(const CSequence &baseClass) : + CSequence(baseClass) + { } + + CActionHotkeyList CActionHotkeyList::findSubsetsOf(const CActionHotkey &other) + { + CActionHotkeyList subsets; + for (const auto &actionHotkey : *this) + { + if (actionHotkey.getCombination().isSubsetOf(other.getCombination())) + { + subsets.push_back(actionHotkey); + } + } + return subsets; + } + + CActionHotkeyList CActionHotkeyList::findSupersetsOf(const CActionHotkey &other) + { + CActionHotkeyList supersets; + for (const auto &actionHotkey : *this) + { + if (other.getCombination().isSubsetOf(actionHotkey.getCombination())) + { + supersets.push_back(actionHotkey); + } + } + return supersets; + } + } +} diff --git a/src/blackmisc/input/actionhotkeylist.h b/src/blackmisc/input/actionhotkeylist.h new file mode 100644 index 000000000..256d1e95f --- /dev/null +++ b/src/blackmisc/input/actionhotkeylist.h @@ -0,0 +1,66 @@ +/* Copyright (C) 2015 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_ACTIONHOTKEYLIST_H +#define BLACKMISC_INPUT_ACTIONHOTKEYLIST_H + +#include "blackmisc/blackmiscexport.h" +#include "actionhotkey.h" +#include "blackmisc/collection.h" +#include "blackmisc/sequence.h" + +namespace BlackMisc +{ + namespace Input + { + //! Value object encapsulating a list of hotkeys + class BLACKMISC_EXPORT CActionHotkeyList : + public CSequence, + public BlackMisc::Mixin::MetaType + { + public: + BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CActionHotkeyList) + + //! Default constructor + CActionHotkeyList() = default; + + //! Construct from std::initializer_list + CActionHotkeyList(std::initializer_list il) : CSequence(il) {} + + //! Construct from a base class object. + CActionHotkeyList(const CSequence &baseClass); + + //! Returns true if this list has a action hotkey with a combination which is a subset of other + //! Example: + //! List contains CTRL and other has combination CTRL-F + CActionHotkeyList findSubsetsOf(const CActionHotkey &other); + + //! Returns true if this list has a hotkey with a combination for which other is a subset + //! Example: + //! List contains CTRL-F and other has combination CTRL + CActionHotkeyList findSupersetsOf(const CActionHotkey &other); + + //! Compare two CHotkeyList + //! \todo Remove once https://dev.vatsim-germany.org/issues/459 is fixed + friend int compare(const CActionHotkeyList &a, const CActionHotkeyList &b) + { + return compare(static_cast &>(a), static_cast &>(b)); + } + }; + + } +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Input::CActionHotkeyList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif // guard diff --git a/src/blackmisc/input/hotkeycombination.cpp b/src/blackmisc/input/hotkeycombination.cpp new file mode 100644 index 000000000..621ae2a85 --- /dev/null +++ b/src/blackmisc/input/hotkeycombination.cpp @@ -0,0 +1,80 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/input/hotkeycombination.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/variant.h" +#include + +namespace BlackMisc +{ + namespace Input + { + void CHotkeyCombination::addKeyboardKey(const CKeyboardKey &key) + { + if (m_keyboardKeys.contains(key)) { return; } + m_keyboardKeys.push_back(key); + m_keyboardKeys.sortBy(&CKeyboardKey::getKey); + } + + void CHotkeyCombination::addJoystickButton(const CJoystickButton &button) + { + if (m_joystickButtons.contains(button)) { return; } + m_joystickButtons.push_back(button); + m_joystickButtons.sortBy(&CJoystickButton::getButtonIndex); + } + + void CHotkeyCombination::replaceKey(const CKeyboardKey &oldKey, const CKeyboardKey &newKey) + { + Q_ASSERT(!oldKey.isUnknown()); + m_keyboardKeys.remove(oldKey); + if (!newKey.isUnknown()) { m_keyboardKeys.push_back(newKey); } + m_keyboardKeys.sortBy(&CKeyboardKey::getKey); + } + + void CHotkeyCombination::replaceButton(const CJoystickButton &oldButton, const CJoystickButton &newButton) + { + m_joystickButtons.remove(oldButton); + if (newButton.isValid()) { m_joystickButtons.push_back(newButton); } + m_joystickButtons.sortBy(&CJoystickButton::getButtonIndex); + } + + void CHotkeyCombination::removeKeyboardKey(const CKeyboardKey &key) + { + replaceKey(key, CKeyboardKey()); + } + + void CHotkeyCombination::removeJoystickButton(const CJoystickButton &button) + { + replaceButton(button, CJoystickButton()); + } + + bool CHotkeyCombination::isSubsetOf(const CHotkeyCombination &other) const + { + return std::all_of(m_keyboardKeys.begin(), m_keyboardKeys.end(), [&other](const CKeyboardKey &k) { return other.m_keyboardKeys.contains(k); }) && + std::all_of(m_joystickButtons.begin(), m_joystickButtons.end(), [&other](const CJoystickButton &b) { return other.m_joystickButtons.contains(b); }); + } + + QString CHotkeyCombination::convertToQString(bool /* i18n */) const + { + QStringList sl; + for (const auto &key : m_keyboardKeys) + { + sl << key.toQString(); + } + for (const auto &button : m_joystickButtons) + { + sl << button.toQString(); + } + return sl.join('+'); + } + + } +} diff --git a/src/blackmisc/input/hotkeycombination.h b/src/blackmisc/input/hotkeycombination.h new file mode 100644 index 000000000..f52dd58a8 --- /dev/null +++ b/src/blackmisc/input/hotkeycombination.h @@ -0,0 +1,90 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_HOTKEYCOMBINATION_H +#define BLACKMISC_INPUT_HOTKEYCOMBINATION_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/input/keyboardkeylist.h" +#include "blackmisc/input/joystickbuttonlist.h" + +namespace BlackMisc +{ + namespace Input + { + //! Value object representing hotkey sequence + class BLACKMISC_EXPORT CHotkeyCombination : public CValueObject + { + public: + //! Default constructor + CHotkeyCombination() = default; + + //! Set keyboard keys + void setKeyboardKeys(const CKeyboardKeyList &list) { m_keyboardKeys = list; } + + //! Get keyboard keys + CKeyboardKeyList getKeyboardKeys() const { return m_keyboardKeys; } + + //! Set joystick buttons + void setJoystickButtons(const CJoystickButtonList &list) { m_joystickButtons = list; } + + //! Get joystick buttons + CJoystickButtonList getJoystickButtons() const { return m_joystickButtons; } + + //! Add keyboard key + void addKeyboardKey(const CKeyboardKey &key); + + //! Add joystick button + void addJoystickButton(const CJoystickButton &button); + + //! Replace oldKey with newKey + void replaceKey(const CKeyboardKey &oldKey, const CKeyboardKey &newKey); + + //! Replace oldButton with newButton + void replaceButton(const CJoystickButton &oldButton, const CJoystickButton &newButton); + + //! Remove keyboard key + void removeKeyboardKey(const CKeyboardKey &key); + + //! Remove joystick button + void removeJoystickButton(const CJoystickButton &button); + + //! Is sequence empty? + bool isEmpty() const { return m_keyboardKeys.isEmpty() && m_joystickButtons.isEmpty(); } + + //! Get size of sequence + int size() const { return m_keyboardKeys.size() + m_joystickButtons.size(); } + + //! Is sequence a subset of other? + //! E.g. CTRL would be a subset of CTRL+D + bool isSubsetOf(const CHotkeyCombination &other) const; + + //! \copydoc CValueObject::convertToQString + QString convertToQString(bool i18n = false) const; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CHotkeyCombination) + CKeyboardKeyList m_keyboardKeys; + CJoystickButtonList m_joystickButtons; + }; + } +} + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Input::CHotkeyCombination, ( + attr(o.m_keyboardKeys), + attr(o.m_joystickButtons) + )) +Q_DECLARE_METATYPE(BlackMisc::Input::CHotkeyCombination) + +#endif // guard diff --git a/src/blackmisc/input/input.h b/src/blackmisc/input/input.h new file mode 100644 index 000000000..c8092feb9 --- /dev/null +++ b/src/blackmisc/input/input.h @@ -0,0 +1,18 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKMISC_INPUT_HOTKEY_H +#define BLACKMISC_INPUT_HOTKEY_H + +#include "blackmisc/input/actionhotkeylist.h" +#include "blackmisc/input/hotkeycombination.h" +#include "blackmisc/input/keyboardkeylist.h" +#include "blackmisc/input/joystickbuttonlist.h" + +#endif // guard diff --git a/src/blackmisc/input/joystickbutton.cpp b/src/blackmisc/input/joystickbutton.cpp new file mode 100644 index 000000000..e6747115a --- /dev/null +++ b/src/blackmisc/input/joystickbutton.cpp @@ -0,0 +1,94 @@ +/* Copyright (C) 2014 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/input/joystickbutton.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/variant.h" +#include + +namespace BlackMisc +{ + namespace Input + { + CJoystickButton::CJoystickButton(int buttonIndex) : + m_buttonIndex(buttonIndex) + {} + + void CJoystickButton::setButtonIndex(int buttonIndex) + { + m_buttonIndex = buttonIndex; + } + + void CJoystickButton::setButtonObject(const CJoystickButton &button) + { + this->m_buttonIndex = button.m_buttonIndex; + } + + void CJoystickButton::setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexButton: + case IndexButtonAsString: + this->setButtonIndex(buttonIndexFromString(variant.value())); + break; + case IndeButtonObject: + this->setButtonObject(variant.value()); + break; + default: + Q_ASSERT_X(false, "CJoystickButton", "index unknown (setter)"); + break; + } + } + + CVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexButton: + return CVariant::from(this->getButtonIndex()); + case IndexButtonAsString: + return CVariant::from(this->getButtonAsString()); + default: + break; + } + + Q_ASSERT_X(false, "CJoystickButton", "index unknown"); + QString m = QString("no property, index ").append(index.toQString()); + return CVariant::fromValue(m); + } + + QString CJoystickButton::buttonIndexToString(qint32 buttonIndex) + { + QString buttonString("Button"); + return buttonString.append(QString("%1").arg(buttonIndex)); + } + + int CJoystickButton::buttonIndexFromString(const QString &buttonName) + { + QString name("Button"); + if (!buttonName.startsWith(name)) return getInvalidIndex(); + + name.remove("Button"); + return name.toInt(); + } + + QString CJoystickButton::convertToQString(bool /* i18n */) const + { + QString s = getButtonAsString(); + return s.trimmed(); + } + + } // namespace Hardware + +} // BlackMisc diff --git a/src/blackmisc/input/joystickbutton.h b/src/blackmisc/input/joystickbutton.h new file mode 100644 index 000000000..16e7bf7d1 --- /dev/null +++ b/src/blackmisc/input/joystickbutton.h @@ -0,0 +1,87 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_JOYSTICKBUTTON_H +#define BLACKMISC_INPUT_JOYSTICKBUTTON_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/blackmiscfreefunctions.h" + +namespace BlackMisc +{ + namespace Input + { + //! Value object representing a joystick button + class BLACKMISC_EXPORT CJoystickButton : public CValueObject + { + public: + //! Properties by index + enum ColumnIndex + { + IndexButton = 0, + IndexButtonAsString, + IndeButtonObject, // just for updates + }; + + //! Default constructor + CJoystickButton() = default; + + //! Constructor + CJoystickButton(int buttonIndex); + + //! Get button index + int getButtonIndex() const { return m_buttonIndex; } + + //! Get button as String + QString getButtonAsString() const { return buttonIndexToString(m_buttonIndex); } + + //! Set button index + void setButtonIndex(int buttonIndex); + + //! Is valid? + bool isValid() const { return m_buttonIndex >= 0 ? true : false; } + + //! Set button object + void setButtonObject(const CJoystickButton &button); + + //! \copydoc CValueObject::setPropertyByIndex + void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index); + + //! \copydoc CValueObject::propertyByIndex + CVariant propertyByIndex(const CPropertyIndex &index) const; + + //! Button index to string + static QString buttonIndexToString(qint32 buttonIndex); + + //! Button index from string + static int buttonIndexFromString(const QString &button); + + //! Invalid button index + static int getInvalidIndex() { return m_invalidIndex; } + + //! \copydoc CValueObject::convertToQString + QString convertToQString(bool i18n = false) const; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CJoystickButton) + int m_buttonIndex = m_invalidIndex; + + static const int m_invalidIndex = -1; + }; + } +} + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Input::CJoystickButton, (o.m_buttonIndex)) +Q_DECLARE_METATYPE(BlackMisc::Input::CJoystickButton) + +#endif // guard diff --git a/src/blackmisc/input/joystickbuttonlist.cpp b/src/blackmisc/input/joystickbuttonlist.cpp new file mode 100644 index 000000000..6b3895f53 --- /dev/null +++ b/src/blackmisc/input/joystickbuttonlist.cpp @@ -0,0 +1,22 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/input/joystickbuttonlist.h" +#include "blackmisc/predicates.h" + +namespace BlackMisc +{ + namespace Input + { + CJoystickButtonList::CJoystickButtonList(const CSequence &baseClass) : + CSequence(baseClass) + { } + + } // namespace +} // namespace diff --git a/src/blackmisc/input/joystickbuttonlist.h b/src/blackmisc/input/joystickbuttonlist.h new file mode 100644 index 000000000..8fd9e95fd --- /dev/null +++ b/src/blackmisc/input/joystickbuttonlist.h @@ -0,0 +1,49 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_JOYSTICKBUTTONLIST_H +#define BLACKMISC_INPUT_JOYSTICKBUTTONLIST_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/input/joystickbutton.h" +#include "blackmisc/collection.h" +#include "blackmisc/sequence.h" + +namespace BlackMisc +{ + namespace Input + { + //! Value object encapsulating a list of joystick buttons. + class BLACKMISC_EXPORT CJoystickButtonList : + public CSequence, + public BlackMisc::Mixin::MetaType + { + public: + BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CJoystickButtonList) + + //! Default constructor + CJoystickButtonList() = default; + + //! Construct from a base class object. + CJoystickButtonList(const CSequence &baseClass); + + //! Initializer list constructor. + CJoystickButtonList(std::initializer_list il) : CSequence(il) {} + }; + + } //namespace +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Input::CJoystickButtonList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard diff --git a/src/blackmisc/input/keyboardkey.cpp b/src/blackmisc/input/keyboardkey.cpp new file mode 100644 index 000000000..d41e7cc2d --- /dev/null +++ b/src/blackmisc/input/keyboardkey.cpp @@ -0,0 +1,109 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/input/keyboardkey.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/variant.h" + +namespace BlackMisc +{ + namespace Input + { + CKeyboardKey::CKeyboardKey() : + m_keyCode(Key_Unknown) + {} + + CKeyboardKey::CKeyboardKey(KeyCode keyCode) : + m_keyCode(keyCode) + {} + + QString CKeyboardKey::convertToQString(bool /* i18n */) const + { + return getKeyAsString(); + } + + void CKeyboardKey::setKeyObject(const CKeyboardKey &key) + { + this->m_keyCode = key.m_keyCode; + } + + QString CKeyboardKey::getKeyAsString() const + { + if (m_keyCode == Key_Unknown) return QString(); + + static const QHash keyStrings = + { + { Key_ShiftLeft, QStringLiteral("ShiftLeft") }, + { Key_ShiftRight, QStringLiteral("ShiftRight") }, + { Key_ControlLeft, QStringLiteral("CtrlLeft") }, + { Key_ControlRight, QStringLiteral("CtrlRight") }, + { Key_AltLeft, QStringLiteral("AltLeft") }, + { Key_AltRight, QStringLiteral("AltRight") } + }; + + if (isModifier()) { return keyStrings.value(m_keyCode); } + + QChar key = QChar::fromLatin1(m_keyCode); + return key; + } + + const QList &CKeyboardKey::allModifiers() + { + static const QList allModifiers = + { + Key_ShiftLeft, + Key_ShiftRight, + Key_ControlLeft, + Key_ControlRight, + Key_AltLeft, + Key_AltRight, + }; + return allModifiers; + } + + CVariant CKeyboardKey::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexKey: + return CVariant::from(static_cast(this->m_keyCode)); + case IndexKeyAsString: + return CVariant::from(getKeyAsString()); + default: + break; + } + + Q_ASSERT_X(false, "CKeyboardKey", "index unknown"); + QString m = QString("no property, index ").append(index.toQString()); + return CVariant::fromValue(m); + } + + void CKeyboardKey::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexKey: + case IndexKeyAsString: + qFatal("Not implemented"); + break; + case IndexKeyObject: + this->setKeyObject(variant.value()); + break; + default: + Q_ASSERT_X(false, "CKeyboardKey", "index unknown (setter)"); + break; + } + } + } // namespace Hardware +} // BlackMisc diff --git a/src/blackmisc/input/keyboardkey.h b/src/blackmisc/input/keyboardkey.h new file mode 100644 index 000000000..4658b1774 --- /dev/null +++ b/src/blackmisc/input/keyboardkey.h @@ -0,0 +1,97 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_KEYBOARDKEY_H +#define BLACKMISC_INPUT_KEYBOARDKEY_H + +#include "keycodes.h" +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/blackmiscfreefunctions.h" + +namespace BlackMisc +{ + namespace Input + { + //! Value object representing a keyboard key. + class BLACKMISC_EXPORT CKeyboardKey : public CValueObject + { + public: + //! Properties by index + enum ColumnIndex + { + IndexKey = 0, + IndexKeyAsString, + IndexKeyObject, // just for updates + }; + + //! Default constructor + CKeyboardKey(); + + //! Constructor + CKeyboardKey(BlackMisc::Input::KeyCode keyCode); + + //! Get key code + KeyCode getKey() const { return this->m_keyCode; } + + //! Get key code + QString getKeyAsString() const; + + //! Set key code + void setKey(const KeyCode key) { this->m_keyCode = key; } + + //! Set key code + void setKey(int key) { this->m_keyCode = static_cast(key); } + + //! Set key code + void setKey(char key) { this->m_keyCode = static_cast(key); } + + //! Is unknown? + bool isUnknown() const { return !this->hasKey(); } + + //! Modifier? + bool isModifier() const + { + return allModifiers().contains(m_keyCode); + } + + //! with key? + bool hasKey() const + { + return !(this->m_keyCode == KeyCode::Key_Unknown); + } + + //! Set key object + void setKeyObject(const CKeyboardKey &key); + + //! \copydoc CValueObject::setPropertyByIndex + void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index); + + //! \copydoc CValueObject::propertyByIndex + CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc CValueObject::convertToQString + QString convertToQString(bool i18n = false) const; + + private: + static const QList &allModifiers(); + + BLACK_ENABLE_TUPLE_CONVERSION(CKeyboardKey) + KeyCode m_keyCode; //!< Key code + }; + } +} + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Input::CKeyboardKey, (o.m_keyCode)) +Q_DECLARE_METATYPE(BlackMisc::Input::CKeyboardKey) + +#endif // guard diff --git a/src/blackmisc/input/keyboardkeylist.cpp b/src/blackmisc/input/keyboardkeylist.cpp new file mode 100644 index 000000000..6147c6c7f --- /dev/null +++ b/src/blackmisc/input/keyboardkeylist.cpp @@ -0,0 +1,75 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/input//keyboardkeylist.h" +#include "blackmisc/predicates.h" + +namespace BlackMisc +{ + namespace Input + { + + CKeyboardKeyList::CKeyboardKeyList() { } + + CKeyboardKeyList::CKeyboardKeyList(const CSequence &baseClass) : + CSequence(baseClass) + { } + + const CKeyboardKeyList &CKeyboardKeyList::allSupportedKeys() + { + static CKeyboardKeyList allKeys = { + CKeyboardKey(Key_ControlLeft), + CKeyboardKey(Key_ControlRight), + CKeyboardKey(Key_AltLeft), + CKeyboardKey(Key_AltRight), + CKeyboardKey(Key_ShiftLeft), + CKeyboardKey(Key_ShiftRight), + CKeyboardKey(Key_A), + CKeyboardKey(Key_B), + CKeyboardKey(Key_C), + CKeyboardKey(Key_D), + CKeyboardKey(Key_E), + CKeyboardKey(Key_F), + CKeyboardKey(Key_G), + CKeyboardKey(Key_H), + CKeyboardKey(Key_I), + CKeyboardKey(Key_J), + CKeyboardKey(Key_K), + CKeyboardKey(Key_L), + CKeyboardKey(Key_M), + CKeyboardKey(Key_N), + CKeyboardKey(Key_O), + CKeyboardKey(Key_P), + CKeyboardKey(Key_Q), + CKeyboardKey(Key_R), + CKeyboardKey(Key_S), + CKeyboardKey(Key_T), + CKeyboardKey(Key_U), + CKeyboardKey(Key_V), + CKeyboardKey(Key_W), + CKeyboardKey(Key_X), + CKeyboardKey(Key_Y), + CKeyboardKey(Key_Z), + CKeyboardKey(Key_0), + CKeyboardKey(Key_1), + CKeyboardKey(Key_2), + CKeyboardKey(Key_3), + CKeyboardKey(Key_4), + CKeyboardKey(Key_5), + CKeyboardKey(Key_6), + CKeyboardKey(Key_7), + CKeyboardKey(Key_8), + CKeyboardKey(Key_9), + }; + + return allKeys; + } + + } // namespace +} // namespace diff --git a/src/blackmisc/input/keyboardkeylist.h b/src/blackmisc/input/keyboardkeylist.h new file mode 100644 index 000000000..c0fe8eaff --- /dev/null +++ b/src/blackmisc/input/keyboardkeylist.h @@ -0,0 +1,54 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_KEYBOARDKEYLIST_H +#define BLACKMISC_INPUT_KEYBOARDKEYLIST_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/input/keyboardkey.h" +#include "blackmisc/collection.h" +#include "blackmisc/sequence.h" +#include + +namespace BlackMisc +{ + namespace Input + { + //! Value object encapsulating a list of keyboard keys. + class BLACKMISC_EXPORT CKeyboardKeyList : + public CSequence, + public BlackMisc::Mixin::MetaType + { + public: + BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CKeyboardKeyList) + + //! Default constructor + CKeyboardKeyList(); + + //! Construct from a base class object. + CKeyboardKeyList(const CSequence &baseClass); + + //! Initializer list constructor. + CKeyboardKeyList(std::initializer_list il) : CSequence(il) {} + + //! Get all supported keys + static const CKeyboardKeyList &allSupportedKeys(); + + }; + + } //namespace +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Input::CKeyboardKeyList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard diff --git a/src/blackmisc/input/keycodes.h b/src/blackmisc/input/keycodes.h new file mode 100644 index 000000000..0bca6c582 --- /dev/null +++ b/src/blackmisc/input/keycodes.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_INPUT_KEYCODES_H +#define BLACKMISC_INPUT_KEYCODES_H + +namespace BlackMisc +{ + namespace Input + { + //! Key code + enum KeyCode + { + Key_Unknown, + // Modifiers + Key_ShiftLeft, + Key_ShiftRight, + Key_ControlLeft, + Key_ControlRight, + Key_AltLeft, + Key_AltRight, + // 0 - 9 + Key_0 = 0x30, + Key_1, + Key_2, + Key_3, + Key_4, + Key_5, + Key_6, + Key_7, + Key_8, + Key_9, + // A - Z + Key_A = 0x41, + Key_B, + Key_C, + Key_D, + Key_E, + Key_F, + Key_G, + Key_H, + Key_I, + Key_J, + Key_K, + Key_L, + Key_M, + Key_N, + Key_O, + Key_P, + Key_Q, + Key_R, + Key_S, + Key_T, + Key_U, + Key_V, + Key_W, + Key_X, + Key_Y, + Key_Z, + }; + + } //namespace +} // namespace + +#endif //guard diff --git a/tests/blackmisc/testblackmiscmain.cpp b/tests/blackmisc/testblackmiscmain.cpp index a1440588c..e6d35ce8f 100644 --- a/tests/blackmisc/testblackmiscmain.cpp +++ b/tests/blackmisc/testblackmiscmain.cpp @@ -13,6 +13,7 @@ #include "testidentifier.h" #include "testcontainers.h" #include "testvariantandmap.h" +#include "testinput.h" #include "testhardware.h" #include "testvaluecache.h" #include "testblackmiscmain.h" @@ -32,6 +33,7 @@ namespace BlackMiscTest CTestGeo geoTests; CTestContainers containerTests; CTestVariantAndMap variantAndMap; + CTestInput inputTests; CTestHardware hardwareTests; CTestIdentifier identifierTests; CTestValueCache valueCacheTests; @@ -42,6 +44,7 @@ namespace BlackMiscTest status |= QTest::qExec(&identifierTests, argc, argv); status |= QTest::qExec(&containerTests, argc, argv); status |= QTest::qExec(&variantAndMap, argc, argv); + status |= QTest::qExec(&inputTests, argc, argv); status |= QTest::qExec(&hardwareTests, argc, argv); status |= QTest::qExec(&valueCacheTests, argc, argv); status |= QTest::qExec(&weatherTests, argc, argv); diff --git a/tests/blackmisc/testinput.cpp b/tests/blackmisc/testinput.cpp new file mode 100644 index 000000000..6a0fd56d2 --- /dev/null +++ b/tests/blackmisc/testinput.cpp @@ -0,0 +1,175 @@ +/* Copyright (C) 2013 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "testinput.h" +#include "blackmisc/input/keyboardkey.h" +#include "blackmisc/input/hotkeycombination.h" +#include "blackmisc/input/actionhotkeylist.h" + +using namespace BlackMisc; +using namespace BlackMisc::Input; + +namespace BlackMiscTest +{ + + /* + * Constructor + */ + CTestInput::CTestInput(QObject *parent): QObject(parent) + { + // void + } + + void CTestInput::keyboardKey() + { + CKeyboardKey key; + QVERIFY2(key.getKey() == Key_Unknown, "Default constructed object should have unknown key"); + QVERIFY2(key.getKeyAsString() == QString(), "Default constructed object should return empty string"); + QVERIFY2(key.hasKey() == false, "Default constructed object should return false"); + + key.setKey('C'); + QVERIFY2(key.getKey() == Key_C, "CKeyboardKey::setKey(char key) failed"); + + CKeyboardKey key2(Key_5); + QVERIFY2(key2.getKey() == Key_5, "Returned key is wrong"); + QVERIFY2(key2.getKeyAsString() == "5", "Key as string failed"); + QVERIFY2(key2.hasKey() == true, "Should return true for key != Key_Unknown"); + + CKeyboardKey key3(Key_AltRight); + QVERIFY2(key3.isModifier(), "RightAlt is a modifier"); + + } + + void CTestInput::hotkeyCombination() + { + CHotkeyCombination comb; + comb.addKeyboardKey(Key_5); + comb.addKeyboardKey(Key_AltLeft); + comb.addKeyboardKey(Key_ControlLeft); + QVERIFY2(comb.size() == 3, "Size is incorrect"); + QVERIFY2(comb.toQString() == "CtrlLeft+AltLeft+5", "String representation failed"); + + comb.replaceKey(Key_AltLeft, Key_AltRight); + QVERIFY2(comb.size() == 3, "Size is incorrect"); + QVERIFY2(comb.toQString() == "CtrlLeft+AltRight+5", "String representation failed"); + + comb.replaceKey(Key_AltRight, CKeyboardKey()); + QVERIFY2(comb.size() == 2, "Size is incorrect"); + QVERIFY2(comb.toQString() == "CtrlLeft+5", "String representation failed"); + + comb.removeKeyboardKey(Key_5); + QVERIFY2(comb.size() == 1, "Size is incorrect"); + QVERIFY2(comb.toQString() == "CtrlLeft", "String representation failed"); + } + + void CTestInput::hotKeyEqual() + { + CHotkeyCombination comb1; + comb1.addKeyboardKey(Key_ControlLeft); + comb1.addKeyboardKey(Key_D); + CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + + CHotkeyCombination comb2; + comb2.addKeyboardKey(Key_ControlLeft); + comb2.addKeyboardKey(Key_D); + CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + QVERIFY2(actionHotkey1 == actionHotkey2, "Objects should equal"); + } + + void CTestInput::hotKeyNonEqual() + { + CHotkeyCombination comb; + comb.addKeyboardKey(Key_ControlLeft); + comb.addKeyboardKey(Key_D); + CActionHotkey actionHotkey1 (CIdentifier(), comb, "action"); + + CHotkeyCombination comb2; + comb2.addKeyboardKey(Key_ControlLeft); + comb2.addKeyboardKey(Key_C); + CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + QVERIFY2(actionHotkey1 != actionHotkey2, "Objects should not be equal"); + + CHotkeyCombination comb3; + comb3.addKeyboardKey(Key_ShiftLeft); + comb3.addKeyboardKey(Key_D); + CActionHotkey actionHotkey3 (CIdentifier(), comb2, "action"); + QVERIFY2(actionHotkey1 != actionHotkey3, "Objects should not be equal"); + + CHotkeyCombination comb4; + comb4.addKeyboardKey(Key_ControlLeft); + comb4.addKeyboardKey(Key_D); + CActionHotkey actionHotkey4 (CIdentifier(), comb2, "other_action"); + QVERIFY2(actionHotkey1 != actionHotkey4, "Objects should not be equal"); + } + + void CTestInput::hotkeyEqualFromVariant() + { + CHotkeyCombination comb1; + comb1.addKeyboardKey(Key_ControlLeft); + comb1.addKeyboardKey(Key_C); + CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + CActionHotkeyList list1; + list1.push_back(actionHotkey1); + + CHotkeyCombination comb2; + comb2.addKeyboardKey(Key_ControlLeft); + comb2.addKeyboardKey(Key_D); + CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkeyList list2; + list2.push_back(actionHotkey2); + QVERIFY2(CVariant::fromValue(list1) != CVariant::fromValue(list2), "List should not be equal"); + } + + void CTestInput::hotkeyFindSupersetOf() + { + CHotkeyCombination comb1; + comb1.addKeyboardKey(Key_ControlLeft); + comb1.addKeyboardKey(Key_D); + CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + + CHotkeyCombination comb2; + comb2.addKeyboardKey(Key_ControlLeft); + CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + + CHotkeyCombination comb3; + comb3.addKeyboardKey(Key_ShiftLeft); + comb3.addKeyboardKey(Key_D); + CActionHotkey actionHotkey3 (CIdentifier(), comb3, "action"); + + CActionHotkeyList list1; + list1.push_back(actionHotkey1); + + QVERIFY2(!list1.findSupersetsOf(actionHotkey2).isEmpty(), "CTRL+F is a superset of CTRL"); + QVERIFY2(list1.findSupersetsOf(actionHotkey3).isEmpty(), "Shift+F is not a superset of CTRL"); + } + + void CTestInput::hotkeyFindSubsetOf() + { + CHotkeyCombination comb1; + comb1.addKeyboardKey(Key_ControlLeft); + CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + + CHotkeyCombination comb2; + comb2.addKeyboardKey(Key_ControlLeft); + comb2.addKeyboardKey(Key_D); + CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + + CHotkeyCombination comb3; + comb3.addKeyboardKey(Key_ShiftLeft); + comb3.addKeyboardKey(Key_D); + CActionHotkey actionHotkey3 (CIdentifier(), comb3, "action"); + + CActionHotkeyList list1; + list1.push_back(actionHotkey1); + + QVERIFY2(!list1.findSubsetsOf(actionHotkey2).isEmpty(), "CTRL+F is a subset of CTRL"); + QVERIFY2(list1.findSubsetsOf(actionHotkey3).isEmpty(), "Shift+F is not a subset of CTRL"); + } + +} // namespace diff --git a/tests/blackmisc/testinput.h b/tests/blackmisc/testinput.h new file mode 100644 index 000000000..24e5caba9 --- /dev/null +++ b/tests/blackmisc/testinput.h @@ -0,0 +1,45 @@ +/* Copyright (C) 2014 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISCTEST_TESTINPUT_H +#define BLACKMISCTEST_TESTINPUT_H + +#include + +namespace BlackMiscTest +{ + //! Input classes basic tests + class CTestInput : public QObject + { + Q_OBJECT + public: + //! Standard test case constructor + explicit CTestInput(QObject *parent = nullptr); + + signals: + + private slots: + + //! CKeyboardKey basic tests + void keyboardKey(); + + //! CHotkeyCombination tests + void hotkeyCombination(); + + void hotKeyEqual(); + void hotKeyNonEqual(); + void hotkeyEqualFromVariant(); + void hotkeyFindSupersetOf(); + void hotkeyFindSubsetOf(); + }; +} // namespace + +#endif // guard