Introduce input panel to project.
This commit is contained in:
171
src/InputObject.cpp
Normal file
171
src/InputObject.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
#include "InputObject.h"
|
||||
#include "ui_InputObject.h"
|
||||
#include "qdesktopwidget.h"
|
||||
#include <QDebug>
|
||||
#include <QKeyEvent>
|
||||
#include <QComboBox>
|
||||
#include <QDateEdit>
|
||||
#include <QtVirtualKeyboard>
|
||||
|
||||
|
||||
|
||||
InputObject::InputObject(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::InputObject)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
initUi();
|
||||
|
||||
this->setVisible(false);
|
||||
this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
|
||||
|
||||
|
||||
}
|
||||
|
||||
InputObject::~InputObject()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void InputObject::initUi()
|
||||
{
|
||||
|
||||
QString style = "*{background-color:#3c3c3c; font-family:Arial; color:white;margin:0;font-size:16px;}"
|
||||
"QLabel#title{font-Size:98px;color:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
|
||||
"stop: 0.0 darkgray, stop: 0.5 lightgray, stop: 1.0 darkgray);}"
|
||||
"QLabel#warn{color:#930000;background:transparent;font-size:20px;}"
|
||||
"QFrame#login_frame_username{min-width:700px;max-width:700px;max-height:500px;min-height:500px;"
|
||||
"border:1px solid #0078d8;border-radius:20px;"
|
||||
"background:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
|
||||
" stop: 0.0 darkgray, stop: 0.5 gray, stop: 1.0 darkgray);"
|
||||
"}"
|
||||
|
||||
|
||||
"QLineEdit,QDateEdit{min-height:100px;max-height:100px; min-width:250px;max-width:250px;border: 3px solid pink; border-radius:10px;font-size:26px;}"
|
||||
"QLineEdit,QDateEdit:enabled{background-color: #515151}"
|
||||
"QDateEdit::up-button{image: url(:/up.png);height:15px;}"
|
||||
"QDateEdit::down-button{image:url(:/down.png);height:15px;}"
|
||||
;
|
||||
setStyleSheet(style);
|
||||
this->setWindowOpacity(0.8);
|
||||
|
||||
ui->dateEdit->setVisible(false);
|
||||
ui->dateEdit->setMaximumDate(QDate::currentDate());
|
||||
ui->dateEdit->setDisplayFormat("yyyy/MM/dd");
|
||||
ui->dateEdit->setStyleSheet(style);
|
||||
|
||||
//ui->lineEdit->setFont(QFont("Arial", 25));
|
||||
//ui->dateEdit->setFont(QFont("Arial", 25, QFont::Bold));
|
||||
}
|
||||
|
||||
|
||||
//QLineEdit* InputObject::lineEdit()
|
||||
//{
|
||||
// return ui->lineEdit;
|
||||
//}
|
||||
|
||||
bool InputObject::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
static bool _visible = false;
|
||||
static bool excution = false;
|
||||
|
||||
if (obj->inherits("QQuickView"))
|
||||
{
|
||||
if (_visible != QGuiApplication::inputMethod()->isVisible())
|
||||
{
|
||||
|
||||
if (QGuiApplication::inputMethod()->isVisible())
|
||||
{
|
||||
if (excution == false) {
|
||||
excution = true;
|
||||
//make sure vkb hide and show again,without activating InputObject(both show/hide)
|
||||
this->ShowPanel();
|
||||
excution = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (excution == false) {
|
||||
excution = true;
|
||||
//make sure will not give rise to another vkb
|
||||
this->HidePanel();
|
||||
excution = false;
|
||||
}
|
||||
}
|
||||
_visible = QGuiApplication::inputMethod()->isVisible();
|
||||
}
|
||||
}
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void InputObject::ShowPanel() {
|
||||
if (qApp->focusWidget()->inherits("QLineEdit"))
|
||||
{
|
||||
curEdit = qobject_cast<QLineEdit*>(qApp->focusWidget());
|
||||
ui->dateEdit->setVisible(false);
|
||||
ui->lineEdit->setVisible(true);
|
||||
}
|
||||
else if (qApp->focusWidget()->inherits("QComboBox"))
|
||||
{
|
||||
curEdit = qobject_cast<QComboBox*>(qApp->focusWidget())->lineEdit();
|
||||
ui->dateEdit->setVisible(false);
|
||||
ui->lineEdit->setVisible(true);
|
||||
}
|
||||
else if(qApp->focusWidget()->inherits("QDateEdit"))
|
||||
{
|
||||
curDateEdit = qobject_cast<QDateEdit*>(qApp->focusWidget());
|
||||
ui->dateEdit->setVisible(true);
|
||||
ui->lineEdit->setVisible(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
QDesktopWidget w;
|
||||
int deskWidth = w.availableGeometry().width();
|
||||
int deskHeight = w.availableGeometry().height();
|
||||
int vkbHeight = QGuiApplication::inputMethod()->keyboardRectangle().height();
|
||||
this->setGeometry(0, 0, deskWidth, deskHeight - vkbHeight);
|
||||
this->setVisible(true);
|
||||
this->activateWindow(); //it is quite important!!!
|
||||
|
||||
if (curEdit) {
|
||||
ui->lineEdit->setText(curEdit->text());
|
||||
ui->lineEdit->setEchoMode(curEdit->echoMode());
|
||||
ui->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase);
|
||||
ui->lineEdit->setFocus();
|
||||
|
||||
QEvent event(QEvent::RequestSoftwareInputPanel);
|
||||
QApplication::sendEvent(ui->lineEdit, &event);
|
||||
}
|
||||
if (curDateEdit)
|
||||
{
|
||||
ui->dateEdit->setDateTime(curDateEdit->dateTime());
|
||||
ui->dateEdit->setFocus();
|
||||
QEvent event(QEvent::RequestSoftwareInputPanel);
|
||||
QApplication::sendEvent(ui->dateEdit, &event);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void InputObject::HidePanel()
|
||||
{
|
||||
if (curEdit) {
|
||||
curEdit->setText(ui->lineEdit->text());
|
||||
ui->lineEdit->clearFocus();
|
||||
ui->lineEdit->clear();
|
||||
this->setVisible(false);
|
||||
curEdit = nullptr;
|
||||
}
|
||||
else if (curDateEdit)
|
||||
{
|
||||
curDateEdit->setDateTime(ui->dateEdit->dateTime());
|
||||
ui->dateEdit->clearFocus();
|
||||
ui->dateEdit->clear();
|
||||
this->setVisible(false);
|
||||
curDateEdit = nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user