forked from hanxiaoluo/KeyboardLiuDianWu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmnum.cpp
348 lines (317 loc) · 12.3 KB
/
frmnum.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
#include "frmnum.h"
#include "ui_frmnum.h"
#include "qdesktopwidget.h"
frmNum *frmNum::_instance = 0;
frmNum::frmNum(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmNum)
{
ui->setupUi(this);
this->InitForm();
this->InitProperty();
this->ChangeStyle();
}
frmNum::~frmNum()
{
delete ui;
}
void frmNum::Init(QString style, int fontSize) {
this->currentStyle = style;
this->currentFontSize = fontSize;
this->ChangeStyle();
}
void frmNum::mouseMoveEvent(QMouseEvent *e)
{
if (mousePressed && (e->buttons() && Qt::LeftButton)) {
this->move(e->globalPos() - mousePoint);
e->accept();
}
}
void frmNum::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = e->globalPos() - this->pos();
e->accept();
}
}
void frmNum::mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}
void frmNum::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();
isFirst = true;
isPress = false;
timerPress = new QTimer(this);
connect(timerPress, SIGNAL(timeout()), this, SLOT(reClicked()));
currentWidget = 0;
//如果需要更改输入法面板的样式,改变currentStyle这个变量即可
//blue--淡蓝色 dev--dev风格 black--黑色 brown--灰黑色 lightgray--浅灰色 darkgray--深灰色 gray--灰色 silvery--银色
currentStyle = "";
//输入法面板字体大小,如果需要更改面板字体大小,该这里即可
currentFontSize = 10;
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 frmNum::InitProperty()
{
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->btnDot->setProperty("btnOther", true);
ui->btnSpace->setProperty("btnOther", true);
ui->btnDelete->setProperty("btnOther", true);
}
void frmNum::ShowPanel()
{
this->setVisible(true);
}
void frmNum::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 = "";
this->setVisible(false);
}
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);
}
}
bool frmNum::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
if (currentEditType != "") {
if (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;
}
return QWidget::eventFilter(obj, event);
}
bool frmNum::checkPress()
{
//只有属于数字键盘的合法按钮才继续处理
bool num_ok = btnPress->property("btnNum").toBool();
bool other_ok = btnPress->property("btnOther").toBool();
if (num_ok || other_ok) {
return true;
}
return false;
}
void frmNum::reClicked()
{
if (isPress) {
timerPress->setInterval(30);
btnPress->click();
}
}
void frmNum::btn_clicked()
{
//如果当前焦点控件类型为空,则返回不需要继续处理
if (currentEditType == "") {
return;
}
QPushButton *btn = (QPushButton *)sender();
QString objectName = btn->objectName();
if (objectName == "btnDelete") {
deleteValue();
} else if (objectName == "btnClose") {
this->setVisible(false);
} else if (objectName == "btnEnter") {
this->setVisible(false);
} else if (objectName == "btnSpace") {
insertValue(" ");
} else {
QString value = btn->text();
insertValue(value);
}
}
void frmNum::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 frmNum::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 frmNum::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 frmNum::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{font-size:%1pt;color:%2;}")
.arg(currentFontSize).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(""));
}