swift
keyboardkey.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QChar>
7 #include <QHash>
8 #include <QtDebug>
9 #include <QtGlobal>
10 
11 #include "misc/propertyindexref.h"
12 
13 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::input, CKeyboardKey)
14 
15 namespace swift::misc::input
16 {
17  CKeyboardKey::CKeyboardKey() : m_keyCode(Key_Unknown) {}
18 
19  CKeyboardKey::CKeyboardKey(KeyCode keyCode) : m_keyCode(keyCode) {}
20 
21  QString CKeyboardKey::convertToQString(bool /* i18n */) const { return this->getKeyAsString(); }
22 
23  void CKeyboardKey::setKeyObject(CKeyboardKey key) { m_keyCode = key.m_keyCode; }
24 
26  {
27  if (m_keyCode == Key_Unknown) return QString();
28 
29  static const QHash<KeyCode, QString> keyStrings = {
30  { Key_ShiftLeft, QStringLiteral("ShiftLeft") }, { Key_ShiftRight, QStringLiteral("ShiftRight") },
31  { Key_ControlLeft, QStringLiteral("CtrlLeft") }, { Key_ControlRight, QStringLiteral("CtrlRight") },
32  { Key_AltLeft, QStringLiteral("AltLeft") }, { Key_AltRight, QStringLiteral("AltRight") }
33  };
34 
35  if (isModifier()) { return keyStrings.value(m_keyCode); }
36 
37  switch (m_keyCode)
38  {
39  case Key_Multiply: return QStringLiteral("NumMultiply"); // return QStringLiteral(u"×");
40  case Key_Divide: return QStringLiteral("NumDivide"); // return QStringLiteral(u"÷");
41  case Key_Numpad0: return QStringLiteral("Num0");
42  case Key_Numpad1: return QStringLiteral("Num1");
43  case Key_Numpad2: return QStringLiteral("Num2");
44  case Key_Numpad3: return QStringLiteral("Num3");
45  case Key_Numpad4: return QStringLiteral("Num4");
46  case Key_Numpad5: return QStringLiteral("Num5");
47  case Key_Numpad6: return QStringLiteral("Num6");
48  case Key_Numpad7: return QStringLiteral("Num7");
49  case Key_Numpad8: return QStringLiteral("Num8");
50  case Key_Numpad9: return QStringLiteral("Num9");
51  case Key_NumpadEqual: return QStringLiteral("Num=");
52  case Key_NumpadMinus: return QStringLiteral("Num-");
53  case Key_NumpadPlus: return QStringLiteral("Num+");
54  case Key_NumpadDelete: return QStringLiteral("Num.");
55  case Key_Esc: return QStringLiteral("Esc");
56  case Key_Space: return QStringLiteral("Space");
57  case Key_Tab: return QStringLiteral("Tab");
58  case Key_Back: return QStringLiteral("Bksp");
59  case Key_Insert: return QStringLiteral("Ins");
60  case Key_Delete: return QStringLiteral("Del");
61  case Key_Home: return QStringLiteral("Home");
62  case Key_End: return QStringLiteral("End");
63  case Key_PageUp: return QStringLiteral("PgUp");
64  case Key_PageDown: return QStringLiteral("PgDn");
65  case Key_CapsLock: return QStringLiteral("CapsLock");
66  case Key_Enter: return QStringLiteral("Enter");
67  case Key_OEM1: return QStringLiteral("OEM1");
68  case Key_OEM2: return QStringLiteral("OEM2");
69  case Key_OEM3: return QStringLiteral("OEM3");
70  case Key_OEM4: return QStringLiteral("OEM4");
71  case Key_OEM5: return QStringLiteral("OEM5");
72  case Key_OEM6: return QStringLiteral("OEM6");
73  case Key_OEM7: return QStringLiteral("OEM7");
74  case Key_OEM8: return QStringLiteral("OEM8");
75  case Key_OEM102: return QStringLiteral("OEM102");
76  case Key_DeadGrave: return QStringLiteral("DeadGrave");
77  case Key_Function1: return QStringLiteral("F1");
78  case Key_Function2: return QStringLiteral("F2");
79  case Key_Function3: return QStringLiteral("F3");
80  case Key_Function4: return QStringLiteral("F4");
81  case Key_Function5: return QStringLiteral("F5");
82  case Key_Function6: return QStringLiteral("F6");
83  case Key_Function7: return QStringLiteral("F7");
84  case Key_Function8: return QStringLiteral("F8");
85  case Key_Function9: return QStringLiteral("F9");
86  case Key_Function10: return QStringLiteral("F10");
87  case Key_Function11: return QStringLiteral("F11");
88  case Key_Function12: return QStringLiteral("F12");
89  case Key_Function13: return QStringLiteral("F13");
90  case Key_Function14: return QStringLiteral("F14");
91  case Key_Function15: return QStringLiteral("F15");
92  case Key_Function16: return QStringLiteral("F16");
93  case Key_Function17: return QStringLiteral("F17");
94  case Key_Function18: return QStringLiteral("F18");
95  case Key_Function19: return QStringLiteral("F19");
96  case Key_Function20: return QStringLiteral("F20");
97  case Key_Function21: return QStringLiteral("F21");
98  case Key_Function22: return QStringLiteral("F22");
99  case Key_Function23: return QStringLiteral("F23");
100  case Key_Function24: return QStringLiteral("F24");
101  default: return QChar::fromLatin1(static_cast<char>(m_keyCode));
102  }
103  }
104 
105  const QList<KeyCode> &CKeyboardKey::allModifiers()
106  {
107  static const QList<KeyCode> allModifiers = {
108  Key_ShiftLeft, Key_ShiftRight, Key_ControlLeft, Key_ControlRight, Key_AltLeft, Key_AltRight,
109  };
110  return allModifiers;
111  }
112 
114  {
115  if (index.isMyself()) { return QVariant::fromValue(*this); }
116  const ColumnIndex i = index.frontCasted<ColumnIndex>();
117  switch (i)
118  {
119  case IndexKey: return QVariant::fromValue(m_keyCode);
120  case IndexKeyAsString: return QVariant::fromValue(getKeyAsString());
121  default: break;
122  }
123 
124  Q_ASSERT_X(false, "CKeyboardKey", "index unknown");
125  QString m = QString("no property, index ").append(index.toQString());
126  return QVariant::fromValue(m);
127  }
128 
129  void CKeyboardKey::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
130  {
131  if (index.isMyself())
132  {
133  (*this) = variant.value<CKeyboardKey>();
134  return;
135  }
136  const ColumnIndex i = index.frontCasted<ColumnIndex>();
137  switch (i)
138  {
139  case IndexKey:
140  case IndexKeyAsString: qFatal("Not implemented"); break;
141  case IndexKeyObject: this->setKeyObject(variant.value<CKeyboardKey>()); break;
142  default: Q_ASSERT_X(false, "CKeyboardKey", "index unknown (setter)"); break;
143  }
144  }
145 } // namespace swift::misc::input
Non-owning reference to a CPropertyIndex with a subset of its features.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Value object representing a keyboard key.
Definition: keyboardkey.h:25
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: keyboardkey.cpp:21
CKeyboardKey()
Default constructor.
Definition: keyboardkey.cpp:17
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool isModifier() const
Modifier?
Definition: keyboardkey.h:60
ColumnIndex
Properties by index.
Definition: keyboardkey.h:29
QString getKeyAsString() const
Get key code.
Definition: keyboardkey.cpp:25
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setKeyObject(CKeyboardKey key)
Set key object.
Definition: keyboardkey.cpp:23
KeyCode
Key code http://www.kbdlayout.info/.
Definition: keycodes.h:16
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67