2222
2323namespace dcc_fcitx_configtool {
2424namespace widgets {
25- FcitxKeyLabelWidget::FcitxKeyLabelWidget (QStringList list, QWidget *p)
25+ FcitxKeyLabelWidget::FcitxKeyLabelWidget (fcitx::Key list, QWidget *p)
2626 : QWidget(p)
2727 , m_curlist(list)
2828{
2929 m_eidtFlag = true ;
30- if (m_curlist.isEmpty ()) {
31- m_curlist << tr (" None" );
32- }
3330 m_keyEdit = new QLineEdit (this );
3431 m_keyEdit->installEventFilter (this );
3532 m_keyEdit->setReadOnly (true );
@@ -49,32 +46,19 @@ FcitxKeyLabelWidget::~FcitxKeyLabelWidget()
4946 clearShortcutKey ();
5047}
5148
52- void FcitxKeyLabelWidget::setKeyId (const QString &id)
53- {
54- m_id = id;
55- }
56-
57- void FcitxKeyLabelWidget::setList (const QStringList &list)
49+ void FcitxKeyLabelWidget::setList (const fcitx::Key &list)
5850{
5951 m_curlist = list;
60- m_curlist.removeDuplicates ();
6152 initLableList (m_curlist);
6253}
6354
64- void FcitxKeyLabelWidget::initLableList (const QStringList &list)
55+ void FcitxKeyLabelWidget::initLableList (const fcitx::Key &list)
6556{
6657 // qDebug() << "initLableList " << list;
6758 clearShortcutKey ();
68- for (const QString &key : list) {
69- QString tmpKey = key.toLower ();
70- if (tmpKey.compare (" control" ) == 0 ){
71- tmpKey = " ctrl" ;
72- }
73- if (!tmpKey.isEmpty ()) {
74- tmpKey[0 ] = tmpKey[0 ].toUpper ();
75- }
76- FcitxKeyLabel *label = new FcitxKeyLabel (tmpKey);
77- label->setAccessibleName (tmpKey);
59+ for (const QString &key : QString::fromStdString (list.toString (fcitx::KeyStringFormat::Localized)).split (" +" )) {
60+ FcitxKeyLabel *label = new FcitxKeyLabel (key);
61+ label->setAccessibleName (key);
7862 label->setBackgroundRole (DPalette::DarkLively);
7963 m_list << label;
8064 m_mainLayout->addWidget (label);
@@ -85,15 +69,7 @@ void FcitxKeyLabelWidget::initLableList(const QStringList &list)
8569
8670QString FcitxKeyLabelWidget::getKeyToStr ()
8771{
88- QString key;
89- for (int i = 0 ; i < m_list.count (); ++i) {
90- if (i == m_list.count () - 1 ) {
91- key += m_list[i]->text ();
92- } else {
93- key += (m_list[i]->text () + " _" );
94- }
95- }
96- return key.toUpper ();
72+ return QString::fromStdString (m_curlist.toString ());
9773}
9874
9975void FcitxKeyLabelWidget::setEnableEdit (bool flag)
@@ -125,70 +101,51 @@ bool FcitxKeyLabelWidget::eventFilter(QObject *watched, QEvent *event)
125101 setShortcutShow (false );
126102 return true ;
127103 }
128- if (event->type () == QEvent::KeyPress) {
129- // Dynamic_Cast(QKeyEvent, e, event);
130- QKeyEvent* e = dynamic_cast <QKeyEvent*>(event);
131-
132- auto func = [=](QStringList &list, const QString &key) {
133- clearShortcutKey ();
134- list.clear ();
135- // if((key == "Ctrl" && m_curlist.contains("CTRL", Qt::CaseInsensitive)) || (key == "Alt" && m_curlist.contains("ALT", Qt::CaseInsensitive))){
136- // return;
137- // }
138- list << key;
139- qDebug () << " func: " << m_curlist;
140- initLableList (list);
141- setShortcutShow (true );
142- };
143-
144- if (e) {
145- if (e->key () == Qt::Key_Delete || e->key () == Qt::Key_Backspace) {
146- func (m_curlist, tr (" None" ));
147-
148- } else if (e->key () == Qt::Key_Control || e->key () == Qt::Key_Alt || m_isSingle) {
149- setFocus ();
150- func (m_newlist, publisherFunc::getKeyValue (e->key ()));
151- } else {
152- setShortcutShow (true );
153- }
154- return true ;
155- }
156- return false ;
157- }
158104 }
159105 return false ;
160106}
161107
162108void FcitxKeyLabelWidget::keyPressEvent (QKeyEvent *event)
163109{
164- if (!m_eidtFlag)
110+ if (!m_eidtFlag) {
165111 return ;
166- quint32 tmpScanCode = event->nativeScanCode ();
167- if (tmpScanCode == 64 ){
168- m_newlist << publisherFunc::getKeyValue ( Qt::Key_Alt);
169112 }
170- else {
171- m_newlist << publisherFunc::getKeyValue ( event->key ());
113+
114+ bool done = true ;
115+ auto newlist = fcitx::Key (
116+ static_cast <fcitx::KeySym>(event->nativeVirtualKey ()),
117+ fcitx::KeyStates (event->nativeModifiers ()), event->nativeScanCode ()).normalize ();
118+ if (newlist.toString () == " " ) {
119+ done = false ;
172120 }
173- initLableList (m_newlist);
174- if (m_newlist.count () >= 2 && !checkNewKey ()) {
175- initLableList (m_curlist);
176- qDebug () << " m_newlist.count() >= 2 && !checkNewKey()" ;
121+
122+ auto modifiers =
123+ event->modifiers () & (Qt::ShiftModifier | Qt::ControlModifier |
124+ Qt::AltModifier | Qt::MetaModifier);
125+ if (modifiers == 0 ) {
126+ done = false ;
127+ }
128+
129+ if (done) {
130+ m_curlist = newlist;
177131 }
132+
133+ initLableList (m_curlist);
178134 setShortcutShow (true );
135+ emit editedFinish ();
179136 QWidget::keyPressEvent (event);
180137}
181138
182139void FcitxKeyLabelWidget::keyReleaseEvent (QKeyEvent *event)
183140{
184- qDebug () << " keyReleaseEvent" ;
185- if (!m_eidtFlag)
186- return ;
187- if ((m_newlist.count () < 2 && !m_isSingle) || !checkNewKey (true )) {
188- m_curlist.removeDuplicates ();
189- initLableList (m_curlist);
190- }
191- setShortcutShow (true );
141+ // qDebug() << "keyReleaseEvent";
142+ // if (!m_eidtFlag)
143+ // return;
144+ // if ((m_newlist.count() < 2 && !m_isSingle) || !checkNewKey(true)) {
145+ // m_curlist.removeDuplicates();
146+ // initLableList(m_curlist);
147+ // }
148+ // setShortcutShow(true);
192149 // QWidget::keyReleaseEvent(event);
193150}
194151
@@ -227,70 +184,8 @@ void FcitxKeyLabelWidget::setShortcutShow(bool flag)
227184 update ();
228185}
229186
230- bool FcitxKeyLabelWidget::checkNewKey (bool isRelease)
231- {
232- QStringList list {publisherFunc::getKeyValue (Qt::Key_Control),
233- publisherFunc::getKeyValue (Qt::Key_Alt),
234- publisherFunc::getKeyValue (Qt::Key_Shift),
235- publisherFunc::getKeyValue (Qt::Key_Super_L)};
236-
237- if (m_newlist.count () == 2 ) {
238- for (int i = 0 ; i < list.count (); ++i) {
239- if (m_newlist.at (0 ) == list.at (i)) {
240- if (list.indexOf (m_newlist[1 ]) != -1 ) {
241- if (m_newlist[1 ] != m_newlist[0 ]) {
242- return !isRelease;
243- }
244- return false ;
245- }
246- if (list.indexOf (m_newlist[1 ]) == -1 ) {
247- QStringList tmpList;
248- for (const QString &key : m_newlist) {
249- QString tmpKey = key.toUpper ();
250- tmpList.append (tmpKey);
251- }
252-
253- QString configName;
254- if (m_curlist == tmpList) {
255- emit shortCutError (m_newlist, configName);
256- return false ;
257- }
258- setList (m_newlist);
259- focusNextChild ();
260- emit editedFinish ();
261- return true ;
262- }
263- }
264- }
265- }
266- if (m_newlist.count () >= 3 ) {
267- if (list.indexOf (m_newlist[0 ]) == -1 || list.indexOf (m_newlist[1 ]) == -1 || list.indexOf (m_newlist[2 ]) != -1 ) {
268- focusNextChild ();
269- return false ;
270- }
271- QStringList tmpList;
272- for (const QString &key : m_newlist) {
273- QString tmpKey = key.toUpper ();
274- tmpList.append (tmpKey);
275- }
276- QString configName;
277- if (m_curlist != tmpList /* && !IMConfig::checkShortKey(m_newlist, configName)*/ ) {
278- emit shortCutError (m_newlist, configName);
279- return false ;
280- }
281- setList (m_newlist);
282- focusNextChild ();
283- emit editedFinish ();
284- return true ;
285- }
286- if (m_newlist.count () == 1 && m_isSingle) {
287- emit editedFinish ();
288- }
289- return true ;
290- }
291-
292187FcitxKeySettingsItem::FcitxKeySettingsItem (const QString &text,
293- const QStringList &list,
188+ const fcitx::Key &list,
294189 QFrame *parent)
295190 : SettingsItem(parent)
296191{
@@ -329,12 +224,7 @@ void FcitxKeySettingsItem::setEnableEdit(bool flag)
329224 m_keyWidget->setEnableEdit (flag);
330225}
331226
332- void FcitxKeySettingsItem::setKeyId (const QString &id)
333- {
334- m_keyWidget->setKeyId (id);
335- }
336-
337- void FcitxKeySettingsItem::setList (const QStringList &list)
227+ void FcitxKeySettingsItem::setList (const fcitx::Key &list)
338228{
339229 m_keyWidget->setList (list);
340230}
@@ -345,7 +235,7 @@ void FcitxKeySettingsItem::doShortCutError(const QStringList &list, QString &nam
345235}
346236
347237FcitxHotKeySettingsItem::FcitxHotKeySettingsItem (const QString &text,
348- const QStringList &list,
238+ const fcitx::Key &list,
349239 QFrame *parent)
350240 : SettingsItem(parent)
351241{
@@ -384,12 +274,7 @@ void FcitxHotKeySettingsItem::setEnableEdit(bool flag)
384274 m_keyWidget->setEnableEdit (flag);
385275}
386276
387- void FcitxHotKeySettingsItem::setKeyId (const QString &id)
388- {
389- m_keyWidget->setKeyId (id);
390- }
391-
392- void FcitxHotKeySettingsItem::setList (const QStringList &list)
277+ void FcitxHotKeySettingsItem::setList (const fcitx::Key &list)
393278{
394279 m_keyWidget->setList (list);
395280}
0 commit comments