8
8
9
9
#include " actions.h"
10
10
#include " editor.h"
11
+ #include " inputproc.h"
12
+ #include " keycons.h"
11
13
#include " model.h"
12
14
#include " usrkeymap.h"
15
+ #include < QAbstractItemModel>
16
+ #include < QObject>
17
+ #include < QString>
18
+ #include < Qt>
19
+ #include < algorithm>
20
+ #include < cstddef>
13
21
#include < fcitx-utils/charutils.h>
22
+ #include < fcitx-utils/fs.h>
14
23
#include < fcitx-utils/i18n.h>
15
- #include < fcitx-utils/standardpath .h>
24
+ #include < fcitx-utils/standardpaths .h>
16
25
#include < fcitx-utils/unixfd.h>
17
26
#include < fcntl.h>
27
+ #include < iterator>
28
+ #include < utility>
18
29
19
- namespace fcitx {
20
- namespace unikey {
30
+ namespace fcitx ::unikey {
21
31
22
- typedef QPair <QString, QString> ItemType ;
32
+ using ItemType = std::pair <QString, QString>;
23
33
24
34
KeymapModel::KeymapModel (QObject *parent)
25
35
: QAbstractTableModel(parent), needSave_(false ) {}
@@ -29,17 +39,21 @@ KeymapModel::~KeymapModel() {}
29
39
QVariant KeymapModel::headerData (int section, Qt::Orientation orientation,
30
40
int role) const {
31
41
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
32
- if (section == 0 )
42
+ if (section == 0 ) {
33
43
return _ (" Keymap" );
34
- else if (section == 1 )
44
+ }
45
+ if (section == 1 ) {
35
46
return _ (" Word" );
47
+ }
36
48
}
37
- return QVariant () ;
49
+ return {} ;
38
50
}
39
51
40
- int KeymapModel::rowCount (const QModelIndex &) const { return list_.size (); }
52
+ int KeymapModel::rowCount (const QModelIndex & /* parent*/ ) const {
53
+ return list_.size ();
54
+ }
41
55
42
- int KeymapModel::columnCount (const QModelIndex &) const { return 2 ; }
56
+ int KeymapModel::columnCount (const QModelIndex & /* parent */ ) const { return 2 ; }
43
57
44
58
QVariant KeymapModel::data (const QModelIndex &index, int role) const {
45
59
if (index.row () >= static_cast <int >(list_.size ()) || index.row () < 0 ) {
@@ -49,14 +63,16 @@ QVariant KeymapModel::data(const QModelIndex &index, int role) const {
49
63
if (role == Qt::DisplayRole) {
50
64
if (index.column () == 0 ) {
51
65
return QString (QChar (list_[index.row ()].key ));
52
- } else if (index.column () == 1 ) {
66
+ }
67
+ if (index.column () == 1 ) {
53
68
return QString::fromStdString (
54
69
_ (actionName (list_[index.row ()].action )));
55
70
}
56
71
} else if (role == Qt::UserRole) {
57
72
if (index.column () == 0 ) {
58
73
return QChar (list_[index.row ()].key );
59
- } else if (index.column () == 1 ) {
74
+ }
75
+ if (index.column () == 1 ) {
60
76
return list_[index.row ()].action ;
61
77
}
62
78
}
@@ -70,7 +86,7 @@ QModelIndex KeymapModel::addItem(unsigned char key, int action) {
70
86
key = charutils::toupper (key);
71
87
checkBoth = true ;
72
88
}
73
- const auto lower = charutils::tolower (key);
89
+ const unsigned char lower = charutils::tolower (key);
74
90
bool updated = false ;
75
91
auto match = [key, checkBoth, lower](const UkKeyMapping &item) {
76
92
if (item.action < vneCount &&
@@ -83,7 +99,7 @@ QModelIndex KeymapModel::addItem(unsigned char key, int action) {
83
99
auto iter = list_.begin ();
84
100
for (; iter != list_.end (); iter++) {
85
101
if (match (*iter)) {
86
- *iter = UkKeyMapping{key, action};
102
+ *iter = UkKeyMapping{. key = key, . action = action};
87
103
updated = true ;
88
104
break ;
89
105
}
@@ -128,17 +144,19 @@ void KeymapModel::moveDown(int row) {
128
144
}
129
145
130
146
void KeymapModel::deleteItem (int row) {
131
- if (row >= static_cast <int >(list_.size ()))
147
+ if (row >= static_cast <int >(list_.size ())) {
132
148
return ;
149
+ }
133
150
beginRemoveRows (QModelIndex (), row, row);
134
151
list_.erase (list_.begin () + row);
135
152
endRemoveRows ();
136
153
setNeedSave (true );
137
154
}
138
155
139
156
void KeymapModel::deleteAllItem () {
140
- if (!list_.empty ())
157
+ if (!list_.empty ()) {
141
158
setNeedSave (true );
159
+ }
142
160
beginResetModel ();
143
161
list_.clear ();
144
162
endResetModel ();
@@ -151,12 +169,12 @@ void KeymapModel::setNeedSave(bool needSave) {
151
169
}
152
170
}
153
171
154
- bool KeymapModel::needSave () { return needSave_; }
172
+ bool KeymapModel::needSave () const { return needSave_; }
155
173
156
174
void KeymapModel::load () {
157
175
beginResetModel ();
158
- auto keymapFile = StandardPath ::global ().open (
159
- StandardPath::Type::PkgConfig, " unikey/keymap.txt" , O_RDONLY );
176
+ auto keymapFile = StandardPaths ::global ().open (StandardPathsType::PkgConfig,
177
+ " unikey/keymap.txt" );
160
178
if (keymapFile.isValid ()) {
161
179
list_ = UkLoadKeyOrderMap (keymapFile.fd ());
162
180
} else {
@@ -166,9 +184,9 @@ void KeymapModel::load() {
166
184
}
167
185
168
186
void KeymapModel::save () {
169
- StandardPath ::global ().safeSave (StandardPath::Type ::PkgConfig,
170
- " unikey/keymap.txt" ,
171
- [this ](int fd) { return saveToFd (fd); });
187
+ StandardPaths ::global ().safeSave (StandardPathsType ::PkgConfig,
188
+ " unikey/keymap.txt" ,
189
+ [this ](int fd) { return saveToFd (fd); });
172
190
setNeedSave (false );
173
191
}
174
192
@@ -189,9 +207,9 @@ void KeymapModel::save(const QString &file) {
189
207
if (!file.startsWith (" /" )) {
190
208
return ;
191
209
}
192
- StandardPath ::global ().safeSave (StandardPath::Type ::PkgConfig,
193
- file.toLocal8Bit ().constData (),
194
- [this ](int fd) { return saveToFd (fd); });
210
+ StandardPaths ::global ().safeSave (StandardPathsType ::PkgConfig,
211
+ file.toLocal8Bit ().constData (),
212
+ [this ](int fd) { return saveToFd (fd); });
195
213
setNeedSave (false );
196
214
}
197
215
@@ -226,6 +244,8 @@ void KeymapModel::load(int profile) {
226
244
case UkMsVi:
227
245
mapping = MsViMethodMapping;
228
246
break ;
247
+ default :
248
+ break ;
229
249
}
230
250
if (!mapping) {
231
251
return ;
@@ -240,5 +260,4 @@ void KeymapModel::load(int profile) {
240
260
setNeedSave (true );
241
261
}
242
262
243
- } // namespace unikey
244
- } // namespace fcitx
263
+ } // namespace fcitx::unikey
0 commit comments