Refactor InputObject(EventFilter and VirtualKeyboard).

This commit is contained in:
sunwen
2022-06-14 14:03:19 +08:00
parent 589262781d
commit 020b40c55d
36 changed files with 783 additions and 598 deletions

View File

@@ -1,245 +0,0 @@
#include "InputObject.h"
#include "ui_InputObject.h"
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>
#include <QComboBox>
#include <QDateEdit>
#include <QtVirtualKeyboard>
#include <QKeyEvent>
#include <QLineEdit>
#include <QAbstractSpinBox>
InputObject::InputObject(QWidget* aParent)
: QWidget(aParent)
, mUI(new Ui::InputObject)
, mCurrentDateEdit(nullptr)
, mCurrentLineEdit(nullptr)
, mCurrentTextEdit(nullptr)
{
mUI->setupUi(this);
initUi();
setVisible(false);
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
}
InputObject::~InputObject()
{
delete mUI;
}
void InputObject::initUi()
{
//#3c3c3c
#ifdef CUTE_STYLE
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;\
color:white;margin:0;font-size:36px;}"
"QTextEdit{background-color: rgba(0,0,0,0.5);\
min-height:300px;max-height:300px; min-width:700px;max-width:700px;\
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;}"
;
#else
QString style =
"QWidget#WiBack{background-color: #383533;}"
"QWidget#WiBo{background-color: black;}"
"QLineEdit,QDateEdit,QTextEdit{\
background:qlineargradient(x1:0,y1:0,x2:1,y2:1,stop: 0.0 silver, stop: 1.0 grey);\
border: 1px solid #1e1b18; border-radius:20px;\
color:black;margin:0;font-size:36px;}"
"QLineEdit,QDateEdit{min-height:100px; max-height:100px; min-width:500px; max-width:500px;}"
"QTextEdit{min-height:300px;max-height:300px; min-width:700px;max-width:700px;}"
"QDateEdit::up-button{image: url(:/up.png);height:15px;}"
"QDateEdit::down-button{image:url(:/down.png);height:15px;}"
;
#endif
setStyleSheet(style);
mUI->dateEdit->setVisible(false);
mUI->dateEdit->setMaximumDate(QDate::currentDate());
mUI->dateEdit->setDisplayFormat("yyyy/MM/dd");
mUI->dateEdit->setStyleSheet(style);
connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() {
if (QGuiApplication::inputMethod()->isVisible())
{
return;
}
HidePanel();
hide();
});
}
bool InputObject::eventFilter(QObject* aObject, QEvent* aEvent)
{
if (aEvent->type() == QEvent::MouseButtonPress)
{
emit touchScreen();
}
if (aEvent->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(aEvent);
if (keyEvent->key() == Qt::Key_Return && mCurrentDateEdit)
{
HidePanel();
hide();
}
}
//mouse button release means setfocus is useless
if (aEvent->type() == QEvent::MouseButtonRelease)
{
if (aObject->objectName() == QString("qt_spinbox_lineedit"))
{
QDateEdit* dateEdit = qobject_cast<QDateEdit*>(aObject->parent());
if (dateEdit == mUI->dateEdit)
{
return true;
}
if (dateEdit->isEnabled())
{
ShowPanel();
return true;
}
}
if (aObject->objectName() == QString("qt_scrollarea_viewport"))
{
QTextEdit* textEdit = qobject_cast<QTextEdit*>(aObject->parent());
if (!textEdit)
{
return false;
}
if (textEdit == mUI->textEdit)
{
return true;
}
if (textEdit->isEnabled())
{
ShowPanel();
return true;
}
}
if (aObject->inherits("QLineEdit"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(aObject);
//you have to tell whether it is coming from qdateedit!!!
if (mUI->lineEdit != lineEdit && lineEdit->isEnabled())
{
ShowPanel();
return true;
}
}
}
return QObject::eventFilter(aObject, aEvent);
}
void InputObject::ShowPanel()
{
if (!qApp->focusWidget())
{
return;
}
if (qApp->focusWidget()->inherits("QLineEdit"))
{
mCurrentLineEdit = qobject_cast<QLineEdit*>(qApp->focusWidget());
mUI->dateEdit->setVisible(false);
mUI->lineEdit->setVisible(true);
mUI->textEdit->setVisible(false);
}
if (qApp->focusWidget()->inherits("QDateEdit"))
{
mCurrentDateEdit = qobject_cast<QDateEdit*>(qApp->focusWidget());
mUI->dateEdit->setVisible(true);
mUI->lineEdit->setVisible(false);
mUI->textEdit->setVisible(false);
}
if (qApp->focusWidget()->inherits("QTextEdit"))
{
mCurrentTextEdit = qobject_cast<QTextEdit*>(qApp->focusWidget());
mUI->dateEdit->setVisible(false);
mUI->lineEdit->setVisible(false);
mUI->textEdit->setVisible(true);
}
showFullScreen();
setVisible(true);
activateWindow(); //it is quite important!!!
while (QApplication::activeWindow() != this)
{
qApp->processEvents();
}
if (mCurrentLineEdit)
{
mUI->lineEdit->setText(mCurrentLineEdit->text());
mUI->lineEdit->setEchoMode(mCurrentLineEdit->echoMode());
mUI->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase);
mUI->lineEdit->setFocus();
QEvent event(QEvent::RequestSoftwareInputPanel);
QApplication::sendEvent(mUI->lineEdit, &event);
}
if (mCurrentDateEdit)
{
mUI->dateEdit->setDateTime(mCurrentDateEdit->dateTime());
mUI->dateEdit->setFocus();
while (QGuiApplication::focusObject() != mUI->dateEdit)
{
qApp->processEvents();
}
qDebug() << QGuiApplication::focusObject();
QEvent event(QEvent::RequestSoftwareInputPanel);
QApplication::sendEvent(mUI->dateEdit, &event);
}
if (mCurrentTextEdit)
{
mUI->textEdit->setPlainText(mCurrentTextEdit->toPlainText());
mUI->textEdit->setFocus();
while (QGuiApplication::focusObject() != mUI->textEdit)
{
qApp->processEvents();
}
QEvent event(QEvent::RequestSoftwareInputPanel);
QApplication::sendEvent(mUI->textEdit, &event);
}
}
void InputObject::HidePanel()
{
if (mCurrentLineEdit)
{
mCurrentLineEdit->setText(mUI->lineEdit->text());
mUI->lineEdit->clearFocus();
mUI->lineEdit->clear();
mCurrentLineEdit = nullptr;
}
else if (mCurrentDateEdit)
{
mCurrentDateEdit->setDateTime(mUI->dateEdit->dateTime());
mUI->dateEdit->clearFocus();
mUI->dateEdit->clear();
mCurrentDateEdit = nullptr;
}
else if (mCurrentTextEdit)
{
mCurrentTextEdit->setText(mUI->textEdit->toPlainText());
mUI->textEdit->clearFocus();
mUI->textEdit->clear();
mCurrentTextEdit = nullptr;
}
}

View File

@@ -1,37 +0,0 @@
#ifndef INPUTOBJECT_H
#define INPUTOBJECT_H
#include <QWidget>
class QLineEdit;
class QDateEdit;
class QTextEdit;
namespace Ui
{
class InputObject;
}
class InputObject : public QWidget
{
Q_OBJECT
public:
explicit InputObject(QWidget* aParent = nullptr);
~InputObject();
bool eventFilter(QObject* aObject, QEvent* aEvent);
signals:
void touchScreen();
private:
Ui::InputObject* mUI;
void initUi();
void ShowPanel();
void HidePanel();
QDateEdit* mCurrentDateEdit = nullptr;
QLineEdit* mCurrentLineEdit = nullptr;
QTextEdit* mCurrentTextEdit = nullptr;
};
#endif // INPUTOBJECT_H

View File

@@ -1,149 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>InputObject</class>
<widget class="QWidget" name="InputObject">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>952</width>
<height>660</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="WiBack" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>235</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QWidget" name="widget_3" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>214</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QDateEdit" name="dateEdit"/>
</item>
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>800</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>235</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="WiBo" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,20 @@
#include "TouchScreenSignalSender.h"
TouchScreenSignalSender* TouchScreenSignalSender::getInstance()
{
static TouchScreenSignalSender instance;
return &instance;
}
TouchScreenSignalSender::TouchScreenSignalSender()
{
}
TouchScreenSignalSender::~TouchScreenSignalSender()
{
}
void TouchScreenSignalSender::sendTouchScreenSignal()
{
emit touchScreen();
}

View File

@@ -0,0 +1,22 @@
#ifndef TOUCHSCREENSIGNALSENDER_H
#define TOUCHSCREENSIGNALSENDER_H
#include <QObject>
class TouchScreenSignalSender : public QObject
{
Q_OBJECT
public:
static TouchScreenSignalSender* getInstance();
void sendTouchScreenSignal();
signals:
void touchScreen();
private:
explicit TouchScreenSignalSender();
~TouchScreenSignalSender();
};
#endif // TOUCHSCREENSIGNALSENDER_H