Introduce InputObject to project, it's a modal dialog for text input.
This commit is contained in:
@@ -1,22 +1,24 @@
|
||||
#include "InputObject.h"
|
||||
#include "ui_InputObject.h"
|
||||
#include "qdesktopwidget.h"
|
||||
#include "ui_inputobject.h"
|
||||
//#include "qdesktopwidget.h"
|
||||
#include <QDebug>
|
||||
#include <QEvent>
|
||||
#include <QKeyEvent>
|
||||
#include <QComboBox>
|
||||
#include <QDateEdit>
|
||||
#include <QtVirtualKeyboard>
|
||||
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QAbstractSpinBox>
|
||||
|
||||
InputObject::InputObject(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::InputObject)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
||||
initUi();
|
||||
|
||||
|
||||
this->setVisible(false);
|
||||
this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
|
||||
|
||||
@@ -28,73 +30,86 @@ InputObject::~InputObject()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void InputObject::initUi()
|
||||
void InputObject::initUi()
|
||||
{
|
||||
//#3c3c3c
|
||||
//
|
||||
|
||||
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}"
|
||||
QString style =
|
||||
"QWidget#WiBack{background-image: url(:/icons/bg.jpeg);}"
|
||||
"QWidget#WiBo{background-color: black;}"
|
||||
"QLineEdit,QDateEdit{background-color: rgba(0,0,0,0.5);\
|
||||
min-height:100px;max-height:100px; min-width:500px;max-width:500px;\
|
||||
border: 2px solid #ef9cba; border-radius:20px;\
|
||||
font-family:Arial; color:white;margin:0;font-size:36px;}"
|
||||
"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);
|
||||
//this->setWindowOpacity(0.8);
|
||||
|
||||
//ui->lineEdit->setFont(QFont("Arial", 25));
|
||||
//ui->dateEdit->setFont(QFont("Arial", 25, QFont::Bold));
|
||||
connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() {
|
||||
if (QGuiApplication::inputMethod()->isVisible())return;
|
||||
//qDebug() << "HidePanel";
|
||||
//qDebug() << "focusWidget" << qApp->focusWidget();
|
||||
this->HidePanel();
|
||||
this->hide();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
//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"))
|
||||
//mouse button release means setfocus is useless
|
||||
if (event->type() == QEvent::MouseButtonRelease)
|
||||
{
|
||||
if (_visible != QGuiApplication::inputMethod()->isVisible())
|
||||
//qDebug() << obj << event->type();
|
||||
|
||||
if (obj->objectName() == QString("qt_spinbox_lineedit"))
|
||||
{
|
||||
|
||||
if (QGuiApplication::inputMethod()->isVisible())
|
||||
QDateEdit *_date = qobject_cast<QDateEdit*>(obj->parent());
|
||||
if (_date == ui->dateEdit)return true;
|
||||
if (_date->isEnabled())
|
||||
{
|
||||
if (excution == false) {
|
||||
excution = true;
|
||||
//make sure vkb hide and show again,without activating InputObject(both show/hide)
|
||||
this->ShowPanel();
|
||||
excution = false;
|
||||
}
|
||||
this->ShowPanel();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (excution == false) {
|
||||
excution = true;
|
||||
//make sure will not give rise to another vkb
|
||||
this->HidePanel();
|
||||
excution = false;
|
||||
}
|
||||
}
|
||||
_visible = QGuiApplication::inputMethod()->isVisible();
|
||||
}
|
||||
if (obj->objectName() == QString("qt_scrollarea_viewport"))
|
||||
{
|
||||
QTextEdit *_text= qobject_cast<QTextEdit*>(obj->parent());
|
||||
if (_text == ui->textEdit) return true; //
|
||||
if (_text->isEnabled())
|
||||
{
|
||||
this->ShowPanel();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (obj->inherits("QLineEdit")) {
|
||||
QLineEdit *tbx = qobject_cast<QLineEdit*>(obj);
|
||||
//you have to tell whether it is coming from qdateedit!!!
|
||||
if (ui->lineEdit != tbx && tbx->isEnabled())
|
||||
{
|
||||
//qDebug() << "ShowPanel";
|
||||
//qDebug() << "focusWidget" << qApp->focusWidget();
|
||||
//qDebug() << "lineEdit" << ui->lineEdit;
|
||||
//qDebug() << "obj" << obj;
|
||||
this->ShowPanel();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
@@ -102,70 +117,125 @@ bool InputObject::eventFilter(QObject *obj, QEvent *event)
|
||||
void InputObject::ShowPanel() {
|
||||
if (qApp->focusWidget()->inherits("QLineEdit"))
|
||||
{
|
||||
curEdit = qobject_cast<QLineEdit*>(qApp->focusWidget());
|
||||
curLine = qobject_cast<QLineEdit*>(qApp->focusWidget());
|
||||
ui->dateEdit->setVisible(false);
|
||||
ui->lineEdit->setVisible(true);
|
||||
ui->textEdit->setVisible(false);
|
||||
}
|
||||
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"))
|
||||
if (qApp->focusWidget()->inherits("QDateEdit"))
|
||||
{
|
||||
curDateEdit = qobject_cast<QDateEdit*>(qApp->focusWidget());
|
||||
ui->dateEdit->setVisible(true);
|
||||
ui->lineEdit->setVisible(false);
|
||||
ui->textEdit->setVisible(false);
|
||||
}
|
||||
else
|
||||
|
||||
if (qApp->focusWidget()->inherits("QTextEdit"))
|
||||
{
|
||||
return;
|
||||
curText = qobject_cast<QTextEdit*>(qApp->focusWidget());
|
||||
ui->dateEdit->setVisible(false);
|
||||
ui->lineEdit->setVisible(false);
|
||||
ui->textEdit->setVisible(true);
|
||||
}
|
||||
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);
|
||||
|
||||
//else
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
//QDesktopWidget w;
|
||||
//int deskWidth = w.availableGeometry().width();
|
||||
//int deskHeight = w.availableGeometry().height();
|
||||
//int vkbHeight = QGuiApplication::inputMethod()->keyboardRectangle().height();
|
||||
|
||||
this->showFullScreen();
|
||||
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();
|
||||
|
||||
|
||||
if (curLine) {
|
||||
ui->lineEdit->setText(curLine->text());
|
||||
ui->lineEdit->setEchoMode(curLine->echoMode());
|
||||
ui->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase);
|
||||
//qDebug() << "begin";
|
||||
ui->lineEdit->setFocus();
|
||||
//QFocusEvent event0(QEvent::FocusIn);
|
||||
//QCoreApplication::sendEvent(ui->lineEdit, &event0);
|
||||
while (QGuiApplication::focusObject() != ui->lineEdit)
|
||||
{
|
||||
qApp->processEvents();
|
||||
}
|
||||
|
||||
//qDebug() << QGuiApplication::focusObject();
|
||||
QEvent event(QEvent::RequestSoftwareInputPanel);
|
||||
QApplication::sendEvent(ui->lineEdit, &event);
|
||||
QCoreApplication::sendEvent(ui->lineEdit, &event);
|
||||
|
||||
// QMouseEvent pressed(QEvent::MouseButtonPress, QPointF(10,10),Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
|
||||
// QApplication::sendEvent(ui->lineEdit, &pressed);
|
||||
// qDebug()<<"release";
|
||||
// QMouseEvent release(QEvent::MouseButtonRelease, QPointF(10,10),Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
|
||||
// QApplication::sendEvent(ui->lineEdit, &release);
|
||||
// qDebug() <<QGuiApplication::focusObject();
|
||||
//qDebug() << "end";
|
||||
|
||||
}
|
||||
if (curDateEdit)
|
||||
if (curDateEdit)
|
||||
{
|
||||
|
||||
ui->dateEdit->setDateTime(curDateEdit->dateTime());
|
||||
ui->dateEdit->setFocus();
|
||||
while (QGuiApplication::focusObject() != ui->dateEdit)
|
||||
{
|
||||
qApp->processEvents();
|
||||
}
|
||||
qDebug() << QGuiApplication::focusObject();
|
||||
QEvent event(QEvent::RequestSoftwareInputPanel);
|
||||
QApplication::sendEvent(ui->dateEdit, &event);
|
||||
QCoreApplication::sendEvent(ui->dateEdit, &event);
|
||||
}
|
||||
|
||||
|
||||
if (curText)
|
||||
{
|
||||
ui->textEdit->setPlainText(curText->toPlainText());
|
||||
ui->textEdit->setFocus();
|
||||
while (QGuiApplication::focusObject() != ui->textEdit)
|
||||
{
|
||||
qApp->processEvents();
|
||||
}
|
||||
//qDebug() << QGuiApplication::focusObject();
|
||||
QEvent event(QEvent::RequestSoftwareInputPanel);
|
||||
QCoreApplication::sendEvent(ui->textEdit, &event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void InputObject::HidePanel()
|
||||
{
|
||||
if (curEdit) {
|
||||
curEdit->setText(ui->lineEdit->text());
|
||||
if (curLine) {
|
||||
curLine->setText(ui->lineEdit->text());
|
||||
ui->lineEdit->clearFocus();
|
||||
ui->lineEdit->clear();
|
||||
this->setVisible(false);
|
||||
curEdit = nullptr;
|
||||
//this->setVisible(false);
|
||||
curLine = nullptr;
|
||||
}
|
||||
else if (curDateEdit)
|
||||
else if (curDateEdit)
|
||||
{
|
||||
//curDateEdit->setDateTime(QDateTime::currentDateTime());
|
||||
curDateEdit->setDateTime(ui->dateEdit->dateTime());
|
||||
ui->dateEdit->clearFocus();
|
||||
ui->dateEdit->clear();
|
||||
this->setVisible(false);
|
||||
//this->setVisible(false);
|
||||
curDateEdit = nullptr;
|
||||
}
|
||||
}
|
||||
else if (curText)
|
||||
{
|
||||
curText->setText(ui->textEdit->toPlainText());
|
||||
ui->textEdit->clearFocus();
|
||||
ui->textEdit->clear();
|
||||
//this->setVisible(false);
|
||||
curText = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user