swift
hotkeycombination.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <algorithm>
7 
8 #include <QStringList>
9 #include <QtGlobal>
10 
12 
13 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::input, CHotkeyCombination)
14 
15 namespace swift::misc::input
16 {
18 
19  CHotkeyCombination::CHotkeyCombination(const CKeyboardKeyList &keys) : m_keyboardKeys(keys) {}
20 
22  {
23  if (m_keyboardKeys.contains(key)) { return; }
24  m_keyboardKeys.push_back(key);
25  m_keyboardKeys.sortBy(&CKeyboardKey::getKey);
26  }
27 
29  {
30  if (m_joystickButtons.contains(button)) { return; }
31  m_joystickButtons.push_back(button);
32  m_joystickButtons.sortBy(&CJoystickButton::getButtonIndex);
33  }
34 
35  bool CHotkeyCombination::containsKeyboardKey(const CKeyboardKey &key) const { return m_keyboardKeys.contains(key); }
36 
38  {
39  return m_joystickButtons.contains(button);
40  }
41 
43  {
44  if (oldKey.hasKey())
45  {
46  Q_ASSERT(!oldKey.isUnknown());
47  m_keyboardKeys.remove(oldKey);
48  }
49  if (!newKey.isUnknown()) { m_keyboardKeys.push_back(newKey); }
50  m_keyboardKeys.sortBy(&CKeyboardKey::getKey);
51  }
52 
54  {
55  m_joystickButtons.remove(oldButton);
56  if (newButton.isValid()) { m_joystickButtons.push_back(newButton); }
57  m_joystickButtons.sortBy(&CJoystickButton::getButtonIndex);
58  }
59 
61 
63 
65  {
66  return std::all_of(m_keyboardKeys.begin(), m_keyboardKeys.end(),
67  [&other](const CKeyboardKey &k) { return other.m_keyboardKeys.contains(k); }) &&
68  std::all_of(m_joystickButtons.begin(), m_joystickButtons.end(),
69  [&other](const CJoystickButton &b) { return other.m_joystickButtons.contains(b); });
70  }
71 
73  {
74  CHotkeyCombination combination(*this);
75  const CKeyboardKeyList otherKeys = other.getKeyboardKeys();
76  for (const CKeyboardKey &key : otherKeys)
77  {
78  if (containsKeyboardKey(key)) { combination.removeKeyboardKey(key); }
79  }
80 
81  const CJoystickButtonList otherButtons = other.getJoystickButtons();
82  for (const CJoystickButton &button : otherButtons)
83  {
84  if (containsJoystickButton(button)) { combination.removeJoystickButton(button); }
85  }
86  return combination;
87  }
88 
89  QString CHotkeyCombination::convertToQString(bool i18n) const
90  {
91  Q_UNUSED(i18n)
92  QStringList sl;
93  sl.reserve(m_keyboardKeys.size() + m_joystickButtons.size());
94  for (const auto &key : m_keyboardKeys) { sl << key.toQString(); }
95  for (const auto &button : m_joystickButtons) { sl << button.toQString(); }
96  return sl.join('+');
97  }
98 
100  {
101  QStringList sl;
102  sl.reserve(m_keyboardKeys.size() + m_joystickButtons.size());
103  for (const auto &key : m_keyboardKeys) { sl << key.toQString(); }
104  for (const auto &button : m_joystickButtons) { sl << button.getButtonAsStringWithDeviceName(); }
105  return sl.join('+');
106  }
107 } // namespace swift::misc::input
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
void sortBy(K1 key1, Keys... keys)
In-place sort by some particular key(s).
Definition: sequence.h:576
iterator begin()
Returns iterator at the beginning of the sequence.
Definition: sequence.h:163
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
int remove(const T &object)
Remove all elements equal to the given object, if it is contained.
Definition: sequence.h:435
iterator end()
Returns iterator one past the end of the sequence.
Definition: sequence.h:172
Value object representing hotkey sequence.
CHotkeyCombination getDeltaComparedTo(const CHotkeyCombination &other) const
Returns the delta (removing all keys and buttons contained in other)
bool isSubsetOf(const CHotkeyCombination &other) const
Is sequence a subset of other? E.g. CTRL would be a subset of CTRL+D.
void removeJoystickButton(CJoystickButton button)
Remove joystick button.
CHotkeyCombination()=default
Default constructor.
CJoystickButtonList getJoystickButtons() const
Get joystick buttons.
QString asStringWithDeviceNames() const
Returns the button name with the device name prefix.
void replaceKey(CKeyboardKey oldKey, CKeyboardKey newKey)
Replace oldKey with newKey.
bool containsJoystickButton(const CJoystickButton &button) const
Does combination contain button?
void addJoystickButton(const CJoystickButton &button)
Add joystick button.
CKeyboardKeyList getKeyboardKeys() const
Get keyboard keys.
bool containsKeyboardKey(const CKeyboardKey &key) const
Does combination contain key?
QString convertToQString(bool i18n=false) const
Cast as QString.
void removeKeyboardKey(CKeyboardKey key)
Remove keyboard key.
void addKeyboardKey(const CKeyboardKey &key)
Add keyboard key.
void replaceButton(CJoystickButton oldButton, CJoystickButton newButton)
Replace oldButton with newButton.
Value object representing a joystick button.
int getButtonIndex() const
Get button index.
Value object encapsulating a list of joystick buttons.
Value object representing a keyboard key.
Definition: keyboardkey.h:25
bool isUnknown() const
Is unknown?
Definition: keyboardkey.h:57
KeyCode getKey() const
Get key code.
Definition: keyboardkey.h:42
bool hasKey() const
with key?
Definition: keyboardkey.h:63
Value object encapsulating a list of keyboard keys.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67