Vuo 2.4.4
Loading...
Searching...
No Matches
VuoKeyComboBox.cc
Go to the documentation of this file.
1
10#include "VuoKeyComboBox.hh"
11
12#include "VuoHeap.h"
13
14extern "C"
15{
16 #include "VuoKey.h"
17}
18
22bool VuoKeyComboBox::isPairOfItemsSorted(pair<QString, QVariant> item1, pair<QString, QVariant> item2)
23{
24 if (item1.first == item2.first)
25 return false;
26
27 QString texts[] = { item1.first, item2.first };
28 Group groups[2];
29 for (int i = 0; i < 2; ++i)
30 {
31 if (texts[i] == VuoKey_getSummary(VuoKey_Any))
32 {
33 groups[i] = Any;
34 }
35 else if (texts[i].length() == 1)
36 {
37 QRegExp numberCheck("\\d");
38 if (numberCheck.exactMatch(texts[i]))
39 groups[i] = Numbers;
40 else
41 {
42 QRegExp punctuationCheck("[=\\-\\]\\[';\\\\,/.`]");
43 if (punctuationCheck.exactMatch(texts[i]))
44 groups[i] = Punctuation;
45 else
46 groups[i] = Letters;
47 }
48 }
49 else
50 {
51 if (texts[i].endsWith("Arrow"))
52 groups[i] = Arrows;
53 else if (texts[i].startsWith("Keypad"))
54 groups[i] = Keypad;
55 else
56 {
57 QRegExp fKeyCheck("F\\d\\d?");
58 if (fKeyCheck.exactMatch(texts[i]))
59 groups[i] = FKeys;
60 else
61 groups[i] = OtherKeys;
62 }
63 }
64 }
65
66 if (groups[0] != groups[1])
67 return groups[0] < groups[1];
68
69 if (groups[0] == Numbers)
70 {
71 int n1 = texts[0].toInt();
72 int n2 = texts[1].toInt();
73 if (n1 == 0)
74 return false;
75 if (n2 == 0)
76 return true;
77 return n1 < n2;
78 }
79
80 if (groups[0] == FKeys)
81 {
82 int n1 = texts[0].mid(1).toInt();
83 int n2 = texts[1].mid(1).toInt();
84 return n1 < n2;
85 }
86
87 return texts[0] < texts[1];
88}
89
94 VuoComboBox(parent)
95{
96 view()->installEventFilter(this);
97
98 vector<pair<QString, QVariant>> itemTextAndData;
99 for (int i = VuoKey_Any; i <= VuoKey_Kana; ++i)
100 {
101 VuoKey key = (VuoKey)i;
102 char *summary = VuoKey_getSummary(key);
103 QString summaryAsUnicode = QString::fromUtf8(summary);
104 itemTextAndData.push_back(make_pair(summaryAsUnicode, key));
105 free(summary);
106 }
107
108 sort(itemTextAndData.begin(), itemTextAndData.end(), isPairOfItemsSorted);
109
110 for (vector<pair<QString, QVariant>>::iterator i = itemTextAndData.begin(); i != itemTextAndData.end(); ++i)
111 {
112 addItem(i->first, i->second);
113 }
114}
115
120{
121 int index = findData(key);
122 if (index >= 0)
123 setCurrentIndex(index);
124}
125
130{
131 QVariant currentKeyAsData = itemData(currentIndex());
132 return (VuoKey)currentKeyAsData.toInt();
133}
134
139{
140 if ((e->nativeVirtualKey() == 0 && e->text().isEmpty()) || e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter || e->key() == Qt::Key_Escape)
141 {
142 QComboBox::keyPressEvent(e);
143 return;
144 }
145
146 VuoKey key = VuoKey_makeFromMacVirtualKeyCode(e->nativeVirtualKey());
147 setCurrentKey(key);
148}
149
154bool VuoKeyComboBox::eventFilter(QObject *object, QEvent *e)
155{
156 if (e->type() == QEvent::KeyPress)
157 {
158 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
159 keyPressEvent(keyEvent);
160 QModelIndex index = model()->index(currentIndex(), 0);
161 ((QAbstractItemView *)object)->setCurrentIndex(index);
162 return true;
163 }
164 return false;
165}