Skip to content

Commit 835760e

Browse files
committed
fix: setting shortcut failed
Fix setting shortcut failed when the delete key is included Issues linuxdeepin/developer-center#6550, linuxdeepin/developer-center#6554, linuxdeepin/developer-center#6546
1 parent 7355eda commit 835760e

6 files changed

Lines changed: 53 additions & 200 deletions

File tree

src/fcitx5Interface/advanceconfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void AdvanceConfig::disableSwitchIMShortCutsFunc(const bool isDisable)
276276

277277
void AdvanceConfig::switchFirstIMShortCuts(const QString &shortCuts)
278278
{
279-
m_switchFirstIMShortCuts = publisherFunc::transFirstUpper(shortCuts);
279+
m_switchFirstIMShortCuts = shortCuts;
280280
if (isDisableSwitchIMShortCutsFunc) {
281281
m_switchFirstIMShortCuts = "Control+ctrl";
282282
}

src/publisher/publisherfunc.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,29 +266,4 @@ QString getKeyValue(const int &key)
266266
return keyValue_QT_KEY;
267267
}
268268

269-
QString transFirstUpper(const QString &str)
270-
{
271-
QString upStr = "";
272-
QString ss = str.toLower();
273-
QStringList strlist = ss.split("_");
274-
for(int i=0; i < strlist.count(); i++) {
275-
QString s = strlist.at(i);
276-
if(!s.isEmpty()) {
277-
if(s == "ctrl") {
278-
s = "control";
279-
}
280-
if(i == 0 || s.count() == 1) {
281-
QString uper = s.at(0).toUpper();
282-
s.replace(0, 1, uper);
283-
strlist.replace(i, s);
284-
}
285-
}
286-
upStr.append(strlist.at(i));
287-
if(i < strlist.count() - 1) {
288-
upStr.append("+");
289-
}
290-
}
291-
return upStr;
292-
}
293-
294269
} // namespace publisherFunc

src/publisher/publisherfunc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ QPair<int, QString> startPopen(const QString &cmd, const QString &model = QStrin
3131
int fontSize(const QString &str, const QFont &font = qApp->font());
3232
QString getKeyValue(const QKeyEvent *event);
3333
QString getKeyValue(const int &key);
34-
QString transFirstUpper(const QString &str); //字符串转换成首字母大写
3534

3635

3736
}; // namespace publisherFunc

src/widgets/keysettingsitem.cpp

Lines changed: 40 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@
2222

2323
namespace dcc_fcitx_configtool {
2424
namespace 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

8670
QString 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

9975
void 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

162108
void 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

182139
void 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-
292187
FcitxKeySettingsItem::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

347237
FcitxHotKeySettingsItem::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
}

src/widgets/keysettingsitem.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "keylabel.h"
1010
#include "labels/shortenlabel.h"
1111

12+
#include <fcitx-utils/key.h>
1213
#include <widgets/settingsitem.h>
1314

1415
#include <QComboBox>
@@ -23,10 +24,9 @@ class FcitxKeyLabelWidget : public QWidget
2324
{
2425
Q_OBJECT
2526
public:
26-
FcitxKeyLabelWidget(QStringList list = {}, QWidget *p = nullptr);
27+
FcitxKeyLabelWidget(fcitx::Key list = fcitx::Key(""), QWidget *p = nullptr);
2728
virtual ~FcitxKeyLabelWidget();
28-
void setKeyId(const QString &id);
29-
void setList(const QStringList &list);
29+
void setList(const fcitx::Key &list);
3030
QString getKeyToStr();
3131
void setEnableEdit(bool flag);
3232
/**
@@ -39,23 +39,20 @@ class FcitxKeyLabelWidget : public QWidget
3939

4040
protected:
4141
void mousePressEvent(QMouseEvent *event) override;
42-
virtual bool eventFilter(QObject *watched, QEvent *event) override;
42+
bool eventFilter(QObject *watched, QEvent *event) override;
4343
void keyPressEvent(QKeyEvent *event) override;
4444
void keyReleaseEvent(QKeyEvent *event) override;
4545

4646
private:
4747
void clearShortcutKey();
4848
void setShortcutShow(bool flag);
49-
bool checkNewKey(bool isRelease = false);
50-
void initLableList(const QStringList &list);
49+
void initLableList(const fcitx::Key &list);
5150

5251
private:
5352
QHBoxLayout *m_mainLayout {nullptr};
5453
QLineEdit *m_keyEdit {nullptr};
5554
QList<FcitxKeyLabel *> m_list;
56-
QString m_id;
57-
QStringList m_curlist;
58-
QStringList m_newlist;
55+
fcitx::Key m_curlist;
5956
bool m_eidtFlag;
6057
bool m_isSingle {false};
6158
};
@@ -64,9 +61,8 @@ class FcitxKeySettingsItem : public DCC_NAMESPACE::SettingsItem
6461
{
6562
Q_OBJECT
6663
public:
67-
FcitxKeySettingsItem(const QString &text = "", const QStringList &list = {}, QFrame *parent = nullptr);
68-
void setKeyId(const QString &id);
69-
void setList(const QStringList &list);
64+
FcitxKeySettingsItem(const QString &text = "", const fcitx::Key &list = fcitx::Key(""), QFrame *parent = nullptr);
65+
void setList(const fcitx::Key &list);
7066
QString getKeyToStr() { return m_keyWidget->getKeyToStr(); }
7167
void setEnableEdit(bool flag);
7268
QString getLabelText();
@@ -93,9 +89,8 @@ class FcitxHotKeySettingsItem : public DCC_NAMESPACE::SettingsItem
9389
{
9490
Q_OBJECT
9591
public:
96-
FcitxHotKeySettingsItem(const QString &text = "", const QStringList &list = {}, QFrame *parent = nullptr);
97-
void setKeyId(const QString &id);
98-
void setList(const QStringList &list);
92+
FcitxHotKeySettingsItem(const QString &text = "", const fcitx::Key &list = fcitx::Key(""), QFrame *parent = nullptr);
93+
void setList(const fcitx::Key &list);
9994
QString getKeyToStr() { return m_keyWidget->getKeyToStr(); }
10095
void setEnableEdit(bool flag);
10196
QString getLabelText();

0 commit comments

Comments
 (0)