-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathqkeyboardctrl.cpp
400 lines (354 loc) · 10.4 KB
/
qkeyboardctrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "qkeyboardctrl.h"
#include <QPushButton>
#include <QPainter>
#include <QDebug>
QKeyboardCtrl::QKeyboardCtrl(QWidget *parent)
: QWidget(parent)
{
m_kb_width = 0;
m_kb_height = 0;
m_kbimagetype = DT_KB_PRO_87;
m_kb_ctrl_mode = KBM_LIGHT_NONE;
m_bkgcolor = QColor(255, 225, 0);
m_hover_item = -1;
m_select_item = -1;
m_shift_status = false;
// 激活鼠标移动消息
setMouseTracking(true);
//grabKeyboard();
// 初始化键盘按键
initKeyboardKey();
}
QKeyboardCtrl::~QKeyboardCtrl()
{
// 清空所有按键
clearAllKeys();
}
// 设置键盘背景
void QKeyboardCtrl::setKeyboardBkg(QString bkg_path)
{
m_pix_kb_bkg.load(bkg_path);
m_kb_width = m_pix_kb_bkg.width();
m_kb_height = m_pix_kb_bkg.height();
}
// 设置键盘类型
void QKeyboardCtrl::setKeyboardMode(int kb_mode)
{
m_kb_ctrl_mode = kb_mode;
}
// 设置按键选中
void QKeyboardCtrl::setKeyCheck(int key_value, bool key_check)
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_value == key_value)
{
key_info->key_check = key_check;
break;
}
}
}
// 设置所有按键状态
void QKeyboardCtrl::setAllKeyCheck(bool key_check)
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info)
key_info->key_check = key_check;
}
}
// 设置按键颜色
void QKeyboardCtrl::setKeyColor(int key_value, QColor color)
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_value == key_value)
{
key_info->key_color = color;
break;
}
}
}
// 设置当前选中按键颜色
void QKeyboardCtrl::setCheckKeyColor(QColor color)
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_check)
{
key_info->key_color = color;
}
}
update();
}
// 设置灯光颜色
void QKeyboardCtrl::setLightColor(QColor color)
{
m_bkgcolor = color;
update();
}
// 设置按键颜色
void QKeyboardCtrl::setAllKeysColor(QColor color)
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info)
key_info->key_color = color;
}
update();
}
// get key info
KEY_INFO* QKeyboardCtrl::getKeyInfo(int key_index)
{
return m_vecKey.at(key_index);
}
// 清空所有按键
void QKeyboardCtrl::clearAllKeys()
{
int key_count = m_vecKey.size();
for (int i = 0; i < key_count; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info)
{
delete key_info;
key_info = NULL;
}
}
m_vecKey.clear();
}
void QKeyboardCtrl::enterEvent(QEvent *)
{
if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
return ;
m_hover_item = -1;
update();
}
void QKeyboardCtrl::leaveEvent(QEvent *)
{
if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
return ;
m_hover_item = -1;
update();
}
void QKeyboardCtrl::mousePressEvent(QMouseEvent *event)
{
if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
return ;
setFocus();
int old_select_item = m_select_item;
if (m_hover_item != -1)
{
m_select_item = m_hover_item;
if (m_kb_ctrl_mode == KBM_CUSTOMKEY)
{
emit slot_keyboard_mousePressKey(m_select_item);
}
else if (m_kb_ctrl_mode == KBM_LIGHT_CUSTOM)
{
if (m_shift_status == false)
{
qDebug() << "m_shift_status = " << m_shift_status;
// 设置所有按键状态
setAllKeyCheck(false);
}
KEY_INFO* key_info = m_vecKey.at(m_select_item);
if (key_info && key_info->key_rect.contains(event->pos()))
{
key_info->key_check = true;
}
update();
return;
}
}
if (old_select_item != m_select_item)
{
update();
}
}
//void QKeyboardCtrl::mouseReleaseEvent(QMouseEvent *event)
//{
// if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
// return ;
//}
void QKeyboardCtrl::mouseMoveEvent(QMouseEvent *event)
{
if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
return ;
int old_hover_item = m_hover_item;
if (m_hover_item != -1)
{
KEY_INFO* key_info = m_vecKey.at(m_hover_item);
if (key_info && key_info->key_rect.contains(event->pos()))
{
return ;
}
m_hover_item = -1;
}
if (m_hover_item == -1)
{
int keyCount = m_vecKey.size();
for (int i = 0; i < keyCount; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info == NULL)
continue ;
if (key_info->key_rect.contains(event->pos()))
{
m_hover_item = i;
break;
}
}
}
if (old_hover_item != m_hover_item)
{
update();
}
}
void QKeyboardCtrl::keyPressEvent(QKeyEvent *event)
{
qDebug() << event->key();
if (event->key() == Qt::Key_Shift)
{
m_shift_status = true;
qDebug() << "m_shift_status = true;" << m_kb_ctrl_mode;
}
}
void QKeyboardCtrl::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Shift)
{
m_shift_status = false;
qDebug() << "m_shift_status = false;" << m_kb_ctrl_mode;
}
}
void QKeyboardCtrl::paintEvent(QPaintEvent*)
{
if (m_kb_ctrl_mode == KBM_LIGHT_NONE || m_kb_ctrl_mode == KBM_LIGHT_STATIC)
{
QPainter painter(this);
// 绘制矩形
QBrush brush(m_bkgcolor);
painter.fillRect(QRect(20, 10, m_kb_width - 40, m_kb_height - 20), brush);
if (m_kb_width > 10 && m_kb_height > 10)
{
painter.drawPixmap(0, 0, m_kb_width, m_kb_height, m_pix_kb_bkg);
}
}
else if (m_kb_ctrl_mode == KBM_LIGHT_CUSTOM)
{
paintLightCustom();
}
else if (m_kb_ctrl_mode == KBM_CUSTOMKEY)
{
paintKeyCustom();
}
}
// 绘制灯光自定义
void QKeyboardCtrl::paintLightCustom()
{
QPainter painter(this);
int keyCount = m_vecKey.size();
for (int i = 0; i < keyCount; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_color.alpha() != 0)
{
painter.fillRect(key_info->key_rect.left() + 1,
key_info->key_rect.top(),
key_info->key_rect.width() - 2,
key_info->key_rect.height() - 6,
QBrush(key_info->key_color));
}
}
if (m_kb_width > 10 && m_kb_height > 10)
{
painter.drawPixmap(0, 0, m_kb_width, m_kb_height, m_pix_kb_bkg);
}
QPen pen(QColor(255,225,0), 2, Qt::SolidLine);
// 绘制鼠标选择效果
painter.setPen(pen);
for (int i = 0; i < keyCount; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_check)
{
//绘制一个矩形
painter.drawRect(key_info->key_rect.left(),
key_info->key_rect.top(),
key_info->key_rect.width(),
key_info->key_rect.height());
}
}
}
void QKeyboardCtrl::paintKeyCustom()
{
QPainter painter(this);
// 绘制鼠标滑过
if (m_hover_item != -1)
{
KEY_INFO* key_info = m_vecKey.at(m_hover_item);
if (key_info)
{
painter.fillRect(key_info->key_rect.left() + 1,
key_info->key_rect.top(),
key_info->key_rect.width() - 2,
key_info->key_rect.height() - 6,
QBrush(QColor(255,225,0)));
}
}
if (m_kb_width > 10 && m_kb_height > 10)
{
painter.drawPixmap(0, 0, m_kb_width, m_kb_height, m_pix_kb_bkg);
}
QPen pen(QColor(51,51, 51), 1, Qt::SolidLine);
// 绘制鼠标选择效果
painter.setPen(pen);
if (m_select_item != -1)
{
KEY_INFO* key_info = m_vecKey.at(m_select_item);
if (key_info)
{
painter.fillRect(key_info->key_rect.left() + 1,
key_info->key_rect.top() ,
key_info->key_rect.width() - 2,
key_info->key_rect.height() - 2,
QBrush(QColor(255,255,255,150)));
//绘制一个矩形
painter.drawRect(key_info->key_rect.left() + 2,
key_info->key_rect.top() + 2,
key_info->key_rect.width() - 5,
key_info->key_rect.height() - 5);
}
}
// 绘制鼠标选中效果
painter.setPen(pen);
int keyCount = m_vecKey.size();
for (int i = 0; i < keyCount; i++)
{
KEY_INFO* key_info = m_vecKey.at(i);
if (key_info && key_info->key_check)
{
painter.fillRect(key_info->key_rect.left() + 1,
key_info->key_rect.top(),
key_info->key_rect.width() - 2,
key_info->key_rect.height() - 2,
QBrush(QColor(255,225,0,150)));
//绘制一个矩形
painter.drawRect(key_info->key_rect.left() + 2,
key_info->key_rect.top() + 2,
key_info->key_rect.width() - 5,
key_info->key_rect.height() - 5);
}
}
}