forked from hanxiaoluo/KeyboardLiuDianWu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frminput.cpp
785 lines (733 loc) · 28.5 KB
/
frminput.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#include "frminput.h"
#include "ui_frminput.h"
#include "qdesktopwidget.h"
frmInput *frmInput::_instance = 0;
frmInput::frmInput(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmInput)
{
ui->setupUi(this);
this->InitProperty();
this->InitForm();
this->ChangeStyle();
}
frmInput::~frmInput()
{
delete ui;
}
void frmInput::Init(QString position, QString style, int btnFontSize, int labFontSize)
{
this->currentPosition = position;
this->currentStyle = style;
this->btnFontSize = btnFontSize;
this->labFontSize = labFontSize;
this->ChangeStyle();
this->ChangeFont();
}
void frmInput::mouseMoveEvent(QMouseEvent *e)
{
if (mousePressed && (e->buttons() && Qt::LeftButton)) {
this->move(e->globalPos() - mousePoint);
e->accept();
}
}
void frmInput::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = e->globalPos() - this->pos();
e->accept();
}
}
void frmInput::mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}
void frmInput::InitForm()
{
this->mousePressed = false;
this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
QDesktopWidget w;
deskWidth = w.availableGeometry().width();
deskHeight = w.availableGeometry().height();
frmWidth = this->width();
frmHeight = this->height();
QSqlDatabase DbConn;
DbConn = QSqlDatabase::addDatabase("QSQLITE", "py");
DbConn.setDatabaseName(qApp->applicationDirPath() + "/py.db");
DbConn.open();
isFirst = true;
isPress = false;
timerPress = new QTimer(this);
connect(timerPress, SIGNAL(timeout()), this, SLOT(reClicked()));
currentWidget = 0;
currentLineEdit = 0;
currentTextEdit = 0;
currentPlain = 0;
currentBrowser = 0;
currentEditType = "";
//如果需要更改输入法面板的显示位置,改变currentPosition这个变量即可
//control--显示在对应输入框的正下方 bottom--填充显示在底部 center--窗体居中显示
currentPosition = "";
//如果需要更改输入法面板的样式,改变currentStyle这个变量即可
//blue--淡蓝色 dev--dev风格 black--黑色 brown--灰黑色 lightgray--浅灰色 darkgray--深灰色 gray--灰色 silvery--银色
currentStyle = "";
//输入法面板字体大小,如果需要更改面板字体大小,该这里即可
btnFontSize = 10;
labFontSize = 10;
//如果需要更改输入法初始模式,改变currentType这个变量即可
//min--小写模式 max--大写模式 chinese--中文模式
currentType = "min";
changeType(currentType);
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach (QPushButton * b, btn) {
connect(b, SIGNAL(clicked()), this, SLOT(btn_clicked()));
}
//绑定全局改变焦点信号槽
connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
this, SLOT(focusChanged(QWidget *, QWidget *)));
//绑定按键事件过滤器
qApp->installEventFilter(this);
}
void frmInput::InitProperty()
{
ui->btnOther1->setProperty("btnOther", true);
ui->btnOther2->setProperty("btnOther", true);
ui->btnOther3->setProperty("btnOther", true);
ui->btnOther4->setProperty("btnOther", true);
ui->btnOther5->setProperty("btnOther", true);
ui->btnOther6->setProperty("btnOther", true);
ui->btnOther7->setProperty("btnOther", true);
ui->btnOther8->setProperty("btnOther", true);
ui->btnOther9->setProperty("btnOther", true);
ui->btnOther10->setProperty("btnOther", true);
ui->btnOther11->setProperty("btnOther", true);
ui->btnOther12->setProperty("btnOther", true);
ui->btnOther13->setProperty("btnOther", true);
ui->btnOther14->setProperty("btnOther", true);
ui->btnOther15->setProperty("btnOther", true);
ui->btnOther16->setProperty("btnOther", true);
ui->btnOther17->setProperty("btnOther", true);
ui->btnOther18->setProperty("btnOther", true);
ui->btnOther19->setProperty("btnOther", true);
ui->btnOther20->setProperty("btnOther", true);
ui->btnOther21->setProperty("btnOther", true);
ui->btnDot->setProperty("btnOther", true);
ui->btnSpace->setProperty("btnOther", true);
ui->btnEnter->setProperty("btnOther", true);
ui->btnDelete->setProperty("btnOther", true);
ui->btn0->setProperty("btnNum", true);
ui->btn1->setProperty("btnNum", true);
ui->btn2->setProperty("btnNum", true);
ui->btn3->setProperty("btnNum", true);
ui->btn4->setProperty("btnNum", true);
ui->btn5->setProperty("btnNum", true);
ui->btn6->setProperty("btnNum", true);
ui->btn7->setProperty("btnNum", true);
ui->btn8->setProperty("btnNum", true);
ui->btn9->setProperty("btnNum", true);
ui->btn00->setProperty("btnNum", true);
ui->btna->setProperty("btnLetter", true);
ui->btnb->setProperty("btnLetter", true);
ui->btnc->setProperty("btnLetter", true);
ui->btnd->setProperty("btnLetter", true);
ui->btne->setProperty("btnLetter", true);
ui->btnf->setProperty("btnLetter", true);
ui->btng->setProperty("btnLetter", true);
ui->btnh->setProperty("btnLetter", true);
ui->btni->setProperty("btnLetter", true);
ui->btnj->setProperty("btnLetter", true);
ui->btnk->setProperty("btnLetter", true);
ui->btnl->setProperty("btnLetter", true);
ui->btnm->setProperty("btnLetter", true);
ui->btnn->setProperty("btnLetter", true);
ui->btno->setProperty("btnLetter", true);
ui->btnp->setProperty("btnLetter", true);
ui->btnq->setProperty("btnLetter", true);
ui->btnr->setProperty("btnLetter", true);
ui->btns->setProperty("btnLetter", true);
ui->btnt->setProperty("btnLetter", true);
ui->btnu->setProperty("btnLetter", true);
ui->btnv->setProperty("btnLetter", true);
ui->btnw->setProperty("btnLetter", true);
ui->btnx->setProperty("btnLetter", true);
ui->btny->setProperty("btnLetter", true);
ui->btnz->setProperty("btnLetter", true);
labCh.append(ui->labCh0);
labCh.append(ui->labCh1);
labCh.append(ui->labCh2);
labCh.append(ui->labCh3);
labCh.append(ui->labCh4);
labCh.append(ui->labCh5);
labCh.append(ui->labCh6);
labCh.append(ui->labCh7);
labCh.append(ui->labCh8);
labCh.append(ui->labCh9);
for (int i = 0; i < 10; i++) {
labCh[i]->installEventFilter(this);
}
}
void frmInput::ShowPanel()
{
this->setVisible(true);
int width = ui->btn0->width();
width = width > 60 ? width : 60;
ui->btnPre->setMinimumWidth(width);
ui->btnPre->setMaximumWidth(width);
ui->btnNext->setMinimumWidth(width);
ui->btnNext->setMaximumWidth(width);
ui->btnClose->setMinimumWidth(width);
ui->btnClose->setMaximumWidth(width);
}
//事件过滤器,用于识别鼠标单击汉字标签处获取对应汉字
bool frmInput::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton) {
if (obj == ui->labCh0) {
setChinese(0);
} else if (obj == ui->labCh1) {
setChinese(1);
} else if (obj == ui->labCh2) {
setChinese(2);
} else if (obj == ui->labCh3) {
setChinese(3);
} else if (obj == ui->labCh4) {
setChinese(4);
} else if (obj == ui->labCh5) {
setChinese(5);
} else if (obj == ui->labCh6) {
setChinese(6);
} else if (obj == ui->labCh7) {
setChinese(7);
} else if (obj == ui->labCh8) {
setChinese(8);
} else if (obj == ui->labCh9) {
setChinese(9);
} else if (currentEditType != "" && obj != ui->btnClose) {
QString objName = obj->objectName();
if (!obj->property("noinput").toBool() && objName != "frmMainWindow"
&& objName != "frmInputWindow" && objName != "qt_edit_menu") {
ShowPanel();
}
}
btnPress = (QPushButton *)obj;
if (checkPress()) {
isPress = true;
timerPress->start(500);
}
return false;
}
} else if (event->type() == QEvent::MouseButtonRelease) {
btnPress = (QPushButton *)obj;
if (checkPress()) {
isPress = false;
timerPress->stop();
}
return false;
} else if (event->type() == QEvent::KeyPress) {
//如果输入法窗体不可见,则不需要处理
if (!isVisible()) {
return QWidget::eventFilter(obj, event);
}
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
//Shift切换输入法模式,esc键关闭输入法面板,空格取第一个汉字,退格键删除
//中文模式下回车键取拼音,中文模式下当没有拼音时可以输入空格
if (keyEvent->key() == Qt::Key_Space) {
if (ui->labPY->text() != "") {
setChinese(0);
return true;
} else {
return false;
}
} else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
insertValue(ui->labPY->text());
ui->labPY->setText("");
selectChinese();
return true;
} else if (keyEvent->key() == Qt::Key_Shift) {
ui->btnType->click();
return true;
} else if (keyEvent->key() == Qt::Key_Escape) {
ui->btnClose->click();
return true;
} else if (keyEvent->key() == Qt::Key_Backspace) {
ui->btnDelete->click();
return true;
} else if (keyEvent->text() == "+" || keyEvent->text() == "=") {
if (ui->labPY->text() != "") {
ui->btnNext->click();
return true;
} else {
return false;
}
} else if (keyEvent->text() == "-" || keyEvent->text() == "_") {
if (ui->labPY->text() != "") {
ui->btnPre->click();
return true;
} else {
return false;
}
} else if (keyEvent->key() == Qt::Key_CapsLock) {
if (currentType != "max") {
currentType = "max";
} else {
currentType = "min";
}
changeType(currentType);
return true;
} else {
if (currentEditType == "QWidget") {
return false;
}
QString key;
if (currentType == "chinese") {
key = keyEvent->text();
} else if (currentType == "min") {
key = keyEvent->text().toLower();
} else if (currentType == "max") {
key = keyEvent->text().toUpper();
}
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach (QPushButton * b, btn) {
if (b->text() == key) {
b->click();
return true;
}
}
}
return false;
}
return QWidget::eventFilter(obj, event);
}
bool frmInput::checkPress()
{
//只有属于输入法键盘的合法按钮才继续处理
bool num_ok = btnPress->property("btnNum").toBool();
bool other_ok = btnPress->property("btnOther").toBool();
bool letter_ok = btnPress->property("btnLetter").toBool();
if (num_ok || other_ok || letter_ok) {
return true;
}
return false;
}
void frmInput::reClicked()
{
if (isPress) {
timerPress->setInterval(30);
btnPress->click();
}
}
void frmInput::focusChanged(QWidget *oldWidget, QWidget *nowWidget)
{
//qDebug() << "oldWidget:" << oldWidget << " nowWidget:" << nowWidget;
if (nowWidget != 0 && !this->isAncestorOf(nowWidget)) {
//在Qt5和linux系统中(嵌入式linux除外),当输入法面板关闭时,焦点会变成无,然后焦点会再次移到焦点控件处
//这样导致输入法面板的关闭按钮不起作用,关闭后马上有控件获取焦点又显示.
//为此,增加判断,当焦点是从有对象转为无对象再转为有对象时不要显示.
//这里又要多一个判断,万一首个窗体的第一个焦点就是落在可输入的对象中,则要过滤掉
#ifndef __arm__
if (oldWidget == 0x0 && !isFirst) {
QTimer::singleShot(0, this, SLOT(hide()));
return;
}
#endif
//如果对应属性noinput为真则不显示
if (nowWidget->property("noinput").toBool()) {
QTimer::singleShot(0, this, SLOT(hide()));
return;
}
isFirst = false;
if (nowWidget->inherits("QLineEdit")) {
currentLineEdit = (QLineEdit *)nowWidget;
currentEditType = "QLineEdit";
ShowPanel();
} else if (nowWidget->inherits("QTextEdit")) {
currentTextEdit = (QTextEdit *)nowWidget;
currentEditType = "QTextEdit";
ShowPanel();
} else if (nowWidget->inherits("QPlainTextEdit")) {
currentPlain = (QPlainTextEdit *)nowWidget;
currentEditType = "QPlainTextEdit";
ShowPanel();
} else if (nowWidget->inherits("QTextBrowser")) {
currentBrowser = (QTextBrowser *)nowWidget;
currentEditType = "QTextBrowser";
ShowPanel();
} else if (nowWidget->inherits("QComboBox")) {
QComboBox *cbox = (QComboBox *)nowWidget;
//只有当下拉选择框处于编辑模式才可以输入
if (cbox->isEditable()) {
currentLineEdit = cbox->lineEdit() ;
currentEditType = "QLineEdit";
ShowPanel();
}
} else if (nowWidget->inherits("QSpinBox") ||
nowWidget->inherits("QDoubleSpinBox") ||
nowWidget->inherits("QDateEdit") ||
nowWidget->inherits("QTimeEdit") ||
nowWidget->inherits("QDateTimeEdit")) {
currentWidget = nowWidget;
currentEditType = "QWidget";
ShowPanel();
} else {
//需要将输入法切换到最初的原始状态--小写,同时将之前的对象指针置为零
currentWidget = 0;
currentLineEdit = 0;
currentTextEdit = 0;
currentPlain = 0;
currentBrowser = 0;
currentEditType = "";
currentType = "min";
changeType(currentType);
this->setVisible(false);
}
//根据用户选择的输入法位置设置-居中显示-底部填充-显示在输入框正下方
if (currentPosition == "center") {
QPoint pos = QPoint(deskWidth / 2 - frmWidth / 2, deskHeight / 2 - frmHeight / 2);
this->setGeometry(pos.x(), pos.y(), frmWidth, frmHeight);
} else if (currentPosition == "bottom") {
this->setGeometry(0, deskHeight - frmHeight, deskWidth, frmHeight);
} else if (currentPosition == "control") {
QRect rect = nowWidget->rect();
QPoint pos = QPoint(rect.left(), rect.bottom() + 2);
pos = nowWidget->mapToGlobal(pos);
int x = pos.x();
int y = pos.y();
if (pos.x() + frmWidth > deskWidth) {
x = deskWidth - frmWidth;
}
if (pos.y() + frmHeight > deskHeight) {
y = pos.y() - frmHeight - rect.height() - 35;
}
this->setGeometry(x, y, frmWidth, frmHeight);
}
}
}
void frmInput::changeType(QString type)
{
if (type == "max") {
changeLetter(true);
ui->btnType->setText("大写");
ui->lab_Title->setText(" 中文输入法--大写");
ui->btnOther12->setText("/");
ui->btnOther14->setText(":");
ui->btnOther17->setText(",");
ui->btnOther18->setText("\\");
ui->btnOther21->setText("\"");
} else if (type == "min") {
changeLetter(false);
ui->btnType->setText("小写");
ui->lab_Title->setText(" 中文输入法--小写");
ui->btnOther12->setText("/");
ui->btnOther14->setText(":");
ui->btnOther17->setText(",");
ui->btnOther18->setText("\\");
ui->btnOther21->setText("\"");
} else {
changeLetter(false);
ui->btnType->setText("中文");
ui->lab_Title->setText(" 中文输入法--中文");
ui->btnOther12->setText("。");
ui->btnOther14->setText(":");
ui->btnOther17->setText(",");
ui->btnOther18->setText(";");
ui->btnOther21->setText("“");
}
//每次切换到模式,都要执行清空之前中文模式下的信息
clearChinese();
ui->labPY->setText("");
}
void frmInput::changeLetter(bool isUpper)
{
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach (QPushButton * b, btn) {
if (b->property("btnLetter").toBool()) {
if (isUpper) {
b->setText(b->text().toUpper());
} else {
b->setText(b->text().toLower());
}
}
}
}
void frmInput::selectChinese()
{
clearChinese();
QSqlQuery query(QSqlDatabase::database("py"));
QString currentPY = ui->labPY->text();
QString sql = QString("select word from pinyin where pinyin='%1'").arg(currentPY);
query.exec(sql);
//逐个将查询到的字词加入汉字队列
while(query.next()) {
QString result = query.value(0).toString();
QStringList text = result.split(" ");
foreach (QString txt, text) {
if (txt.length() > 0) {
allPY.append(txt);
currentPY_count++;
}
}
}
showChinese();
}
void frmInput::showChinese()
{
//每个版面最多显示10个汉字
int count = 0;
currentPY.clear();
for (int i = 0; i < 10; i++) {
labCh[i]->setText("");
}
for (int i = currentPY_index; i < currentPY_count; i++) {
if (count == 10) {
break;
}
QString txt = QString("%1.%2").arg(count).arg(allPY[currentPY_index]);
currentPY.append(allPY[currentPY_index]);
labCh[count]->setText(txt);
count++;
currentPY_index++;
}
//qDebug() << "currentPY_index:" << currentPY_index << "currentPY_count:" << currentPY_count;
}
void frmInput::btn_clicked()
{
//如果当前焦点控件类型为空,则返回不需要继续处理
if (currentEditType == "") {
return;
}
QPushButton *btn = (QPushButton *)sender();
QString objectName = btn->objectName();
if (objectName == "btnType") {
if (currentType == "min") {
currentType = "max";
} else if (currentType == "max") {
currentType = "chinese";
} else if (currentType == "chinese") {
currentType = "min";
}
changeType(currentType);
} else if (objectName == "btnDelete") {
//如果当前是中文模式,则删除对应拼音,删除完拼音之后再删除对应文本输入框的内容
if (currentType == "chinese") {
QString txt = ui->labPY->text();
int len = txt.length();
if (len > 0) {
ui->labPY->setText(txt.left(len - 1));
selectChinese();
} else {
deleteValue();
}
} else {
deleteValue();
}
} else if (objectName == "btnPre") {
if (currentPY_index >= 20) {
//每次最多显示10个汉字,所以每次向前的时候索引要减20
if (currentPY_index % 10 == 0) {
currentPY_index -= 20;
} else {
currentPY_index = currentPY_count - (currentPY_count % 10) - 10;
}
} else {
currentPY_index = 0;
}
showChinese();
} else if (objectName == "btnNext") {
if (currentPY_index < currentPY_count - 1) {
showChinese();
}
} else if (objectName == "btnClose") {
this->setVisible(false);
} else if (objectName == "btnSpace") {
if (currentType == "chinese") {
setChinese(0);
} else {
insertValue(" ");
}
} else if (objectName == "btnEnter") {
QWidget *widget;
widget = 0;
if (currentEditType == "QLineEdit") {
widget = currentLineEdit;
} else if (currentEditType == "QTextEdit") {
widget = currentTextEdit;
} else if (currentEditType == "QPlainTextEdit") {
widget = currentPlain;
} else if (currentEditType == "QTextBrowser") {
widget = currentBrowser;
} else if (currentEditType == "QWidget") {
widget = currentWidget;
}
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QApplication::sendEvent(widget, &keyPress);
this->setVisible(false);
} else {
QString value = btn->text();
//如果是&按钮,因为对应&被过滤,所以真实的text为去除前面一个&字符
if (objectName == "btnOther7") {
value = value.right(1);
}
//当前不是中文模式,则单击按钮对应text为传递参数
if (currentType != "chinese") {
insertValue(value);
} else {
//中文模式下,不允许输入特殊字符,单击对应数字按键取得当前索引的汉字
if (btn->property("btnOther").toBool()) {
if (ui->labPY->text().length() == 0) {
insertValue(value);
}
} else if (btn->property("btnNum").toBool()) {
if (ui->labPY->text().length() == 0) {
insertValue(value);
} else if (objectName == "btn0") {
setChinese(0);
} else if (objectName == "btn1") {
setChinese(1);
} else if (objectName == "btn2") {
setChinese(2);
} else if (objectName == "btn3") {
setChinese(3);
} else if (objectName == "btn4") {
setChinese(4);
} else if (objectName == "btn5") {
setChinese(5);
} else if (objectName == "btn6") {
setChinese(6);
} else if (objectName == "btn7") {
setChinese(7);
} else if (objectName == "btn8") {
setChinese(8);
} else if (objectName == "btn9") {
setChinese(9);
}
} else if (btn->property("btnLetter").toBool()) {
ui->labPY->setText(ui->labPY->text() + value);
selectChinese();
}
}
}
}
void frmInput::insertValue(QString value)
{
if (currentEditType == "QLineEdit") {
currentLineEdit->insert(value);
} else if (currentEditType == "QTextEdit") {
currentTextEdit->insertPlainText(value);
} else if (currentEditType == "QPlainTextEdit") {
currentPlain->insertPlainText(value);
} else if (currentEditType == "QTextBrowser") {
currentBrowser->insertPlainText(value);
} else if (currentEditType == "QWidget") {
QKeyEvent keyPress(QEvent::KeyPress, 0, Qt::NoModifier, QString(value));
QApplication::sendEvent(currentWidget, &keyPress);
}
}
void frmInput::deleteValue()
{
if (currentEditType == "QLineEdit") {
currentLineEdit->backspace();
} else if (currentEditType == "QTextEdit") {
//获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
QTextCursor cursor = currentTextEdit->textCursor();
if(cursor.hasSelection()) {
cursor.removeSelectedText();
} else {
cursor.deletePreviousChar();
}
} else if (currentEditType == "QPlainTextEdit") {
//获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
QTextCursor cursor = currentPlain->textCursor();
if(cursor.hasSelection()) {
cursor.removeSelectedText();
} else {
cursor.deletePreviousChar();
}
} else if (currentEditType == "QTextBrowser") {
//获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
QTextCursor cursor = currentBrowser->textCursor();
if(cursor.hasSelection()) {
cursor.removeSelectedText();
} else {
cursor.deletePreviousChar();
}
} else if (currentEditType == "QWidget") {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier, QString());
QApplication::sendEvent(currentWidget, &keyPress);
}
}
void frmInput::setChinese(int index)
{
int count = currentPY.count();
if (count > index) {
insertValue(currentPY[index]);
//添加完一个汉字后,清空当前汉字信息,等待重新输入
clearChinese();
ui->labPY->setText("");
}
}
void frmInput::clearChinese()
{
//清空汉字,重置索引
for (int i = 0; i < 10; i++) {
labCh[i]->setText("");
}
allPY.clear();
currentPY.clear();
currentPY_index = 0;
currentPY_count = 0;
}
void frmInput::ChangeStyle()
{
if (currentStyle == "blue") {
changeStyle("#DEF0FE", "#C0DEF6", "#C0DCF2", "#386487");
} else if (currentStyle == "dev") {
changeStyle("#C0D3EB", "#BCCFE7", "#B4C2D7", "#324C6C");
} else if (currentStyle == "gray") {
changeStyle("#E4E4E4", "#A2A2A2", "#A9A9A9", "#000000");
} else if (currentStyle == "lightgray") {
changeStyle("#EEEEEE", "#E5E5E5", "#D4D0C8", "#6F6F6F");
} else if (currentStyle == "darkgray") {
changeStyle("#D8D9DE", "#C8C8D0", "#A9ACB5", "#5D5C6C");
} else if (currentStyle == "black") {
changeStyle("#4D4D4D", "#292929", "#D9D9D9", "#CACAD0");
} else if (currentStyle == "brown") {
changeStyle("#667481", "#566373", "#C2CCD8", "#E7ECF0");
} else if (currentStyle == "silvery") {
changeStyle("#E1E4E6", "#CCD3D9", "#B2B6B9", "#000000");
}
}
void frmInput::ChangeFont()
{
QFont btnFont(this->font().family(), btnFontSize);
QFont labFont(this->font().family(), labFontSize);
QList<QPushButton *> btns = ui->widgetMain->findChildren<QPushButton *>();
foreach (QPushButton * btn, btns) {
btn->setFont(btnFont);
}
QList<QLabel *> labs = ui->widgetTop->findChildren<QLabel *>();
foreach (QLabel * lab, labs) {
lab->setFont(labFont);
}
ui->btnPre->setFont(labFont);
ui->btnNext->setFont(labFont);
ui->btnClose->setFont(labFont);
}
void frmInput::changeStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor)
{
QStringList qss;
qss.append(QString("QWidget#widget_title{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
.arg(topColor).arg(bottomColor));
qss.append("QPushButton{padding:5px;border-radius:3px;}");
qss.append(QString("QPushButton:hover{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
.arg(topColor).arg(bottomColor));
qss.append(QString("QLabel,QPushButton{color:%1;}").arg(textColor));
qss.append(QString("QPushButton#btnPre,QPushButton#btnNext,QPushButton#btnClose{padding:5px;}"));
qss.append(QString("QPushButton{border:1px solid %1;background:rgba(0,0,0,0);}")
.arg(borderColor));
qss.append(QString("QLineEdit{border:1px solid %1;border-radius:5px;padding:2px;background:none;selection-background-color:%2;selection-color:%3;}")
.arg(borderColor).arg(bottomColor).arg(topColor));
this->setStyleSheet(qss.join(""));
}