#include "keyboard.h" #include "ui_keyboard.h" #include "qdesktopwidget.h" Keyboard *Keyboard::_instance = 0; Keyboard::Keyboard(QWidget *parent) : QWidget(parent), ui(new Ui::Keyboard) { ui->setupUi(this); this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool); this->InitStyle(); this->InitProperty(); this->InitForm(); ui->slDiaphaneity->setValue(50); this->setGeometry(578,370,2,2); threadK = new MyThread(this); } Keyboard::~Keyboard() { delete ui; } void Keyboard::mouseMoveEvent(QMouseEvent *e) { if (mousePressed && (e->buttons() && Qt::LeftButton)) { this->move(e->globalPos() - mousePoint); e->accept(); } } //void Keyboard::mousePressEvent(QMouseEvent *e) //{ // if (e->button() == Qt::LeftButton) // { // mousePressed = true; // mousePoint = e->globalPos() - this->pos(); // e->accept(); // } //} //void Keyboard::mouseReleaseEvent(QMouseEvent *) //{ // mousePressed = false; //} void Keyboard::InitStyle() { this->setProperty("Form", true); this->mousePressed = false; this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); changeStyle("#4D4D4D", "#292929", "#C2CCD8", "#F0F0F0"); QDesktopWidget w; deskWidth = w.availableGeometry().width(); deskHeight = w.availableGeometry().height(); // frmWidth = this->width(); // frmHeight = this->height(); //自动填充在底部 this->setGeometry(0,0,deskWidth,deskHeight); } void Keyboard::InitForm() { menuStyle = new QMenu(this); menuStyle->addAction("Pale blue", this, SLOT(change_style())); menuStyle->addAction("Blue", this, SLOT(change_style())); menuStyle->addAction("Grey", this, SLOT(change_style())); menuStyle->addAction("Black", this, SLOT(change_style())); menuStyle->addAction("Dark gray", this, SLOT(change_style())); menuStyle->addAction("White", this, SLOT(change_style())); menuStyle->addAction("Silvery", this, SLOT(change_style())); currentType = "min"; currentTextEdit = 0; changeType("min"); QList btn = this->findChildren(); 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 Keyboard::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->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->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); ui->labInfo->setProperty("labVideo", true); } void Keyboard::change_style() { QAction *action = (QAction *)sender(); QString style = action->text(); if (style == "Pale blue") changeStyle("#C0D3EB", "#BCCFE7", "#B4C2D7", "#324C6C"); else if (style == "Blue") changeStyle("#DEF0FE", "#C0DEF6", "#C0DCF2", "#386487"); else if (style == "Grey") changeStyle("#E4E4E4", "#A2A2A2", "#DCDCDC", "#000000"); else if (style == "Black") changeStyle("#4D4D4D", "#292929", "#C2CCD8", "#F0F0F0"); else if (style == "Dark gray") changeStyle("#667481", "#566373", "#C2CCD8", "#E7ECF0"); else if (style == "White") changeStyle("#EEEEEE", "#E5E5E5", "#DCDCDC;", "#000000"); else if (style == "Silvery") changeStyle("#E1E4E6", "#CCD3D9", "#B2B6B9;", "#000000"); } void Keyboard::changeStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor) { QStringList qss; qss.append(QString("QWidget#Keyboard{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{border:1px solid %1;}") .arg(borderColor)); qss.append(QString("QMenu{color:%1;background-color:%2;}") .arg(textColor).arg(topColor)); qss.append("QMenu::item{padding:3px 10px 3px 10px;}"); qss.append(QString("QMenu::item:selected{background-color:%1;}") .arg(bottomColor)); 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("")); } void Keyboard::on_slDiaphaneity_valueChanged(int value) { this->setWindowOpacity((100-value)*0.01); } void Keyboard::changeType(QString type) { if (type == "max") { changeLetter(true); ui->btnType->setText("Uppercase"); ui->labInfo->setText("Current Input--Uppercase"); } else if (type == "min") { changeLetter(false); ui->btnType->setText("Lowercase"); ui->labInfo->setText("Current Input--Lowercase"); } } void Keyboard::changeLetter(bool isUpper) { QList btn = this->findChildren(); foreach (QPushButton *b, btn) { if (b->property("btnLetter").toBool()) { if (isUpper) { b->setText(b->text().toUpper()); } else { b->setText(b->text().toLower()); } } } } void Keyboard::focusChanged(QWidget */*oldWidget*/, QWidget *nowWidget) { //qDebug() << "oldWidget:" << oldWidget << "nowWidget:" << nowWidget; if (nowWidget != 0 && !this->isAncestorOf(nowWidget)) { if (nowWidget->inherits("QTextEdit")) { currentTextEdit = (QTextEdit *)nowWidget; this->setVisible(true); } else { currentTextEdit = 0; this->setVisible(false); currentType="min"; changeType(currentType); } } } void Keyboard::btn_clicked() { QPushButton *btn = (QPushButton *)sender(); QString objectName = btn->objectName(); cursor = currentTextEdit->textCursor(); if (objectName == "btnType") { if (currentType == "min") { currentType = "max"; } else if (currentType == "max") { currentType = "min"; } changeType(currentType); } else if (objectName == "btnDelete" || objectName == "btnDeleteMin") { if (currentTextEdit != 0) { if(cursor.hasSelection()) cursor.removeSelectedText(); cursor.deletePreviousChar(); currentTextEdit->show(); } } else if (objectName == "btnEnter") { // int countNum = currentTextEdit->document()->lineCount(); // QString currentLineText = currentTextEdit->document()->findBlockByLineNumber(countNum).text(); threadK->uartSend(cursor.block().text(), currentTtyPort); currentTextEdit->insertPlainText("\r\n"); } else if (objectName == "btnClose") { this->setGeometry(578,370,2,2); } else if (objectName == "btnStyle") { menuStyle->exec(QCursor::pos()); } else { QString value = btn->text(); //如果是&按钮,因为对应&被过滤,所以真实的text为去除前面一个&字符 if (objectName == "btnOther7") { value = value.right(1); } //单击按钮对应text为传递参数 if (currentTextEdit != 0) { currentTextEdit->insertPlainText(value); } } }