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

@@ -0,0 +1,23 @@
#include "KeyboardManager.h"
#include "KeyboardPanel.h"
KeyboardManager* KeyboardManager::getInstance()
{
static KeyboardManager instance;
return &instance;
}
KeyboardManager::KeyboardManager()
: mKeyboardPanel(new KeyboardPanel())
{
}
KeyboardManager::~KeyboardManager()
{
}
void KeyboardManager::showKeyboardPanel(QWidget* aInputWidget)
{
mKeyboardPanel->showPanel(aInputWidget);
}

View File

@@ -0,0 +1,27 @@
#ifndef KEYBOARDMANAGER_H
#define KEYBOARDMANAGER_H
#include <QObject>
#include <memory>
class KeyboardPanel;
typedef std::shared_ptr<KeyboardPanel> KeyboardPanelPointer;
class KeyboardManager : public QObject
{
Q_OBJECT
public:
static KeyboardManager* getInstance();
void showKeyboardPanel(QWidget* aInputWidget);
private:
explicit KeyboardManager();
~KeyboardManager();
private:
KeyboardPanelPointer mKeyboardPanel;
};
#endif // KEYBOARDMANAGER_H

View File

@@ -0,0 +1,151 @@
#include "KeyboardPanel.h"
#include "ui_KeyboardPanel.h"
#include <QEvent>
#include <QKeyEvent>
#include <QComboBox>
#include <QDateEdit>
#include <QKeyEvent>
#include <QLineEdit>
#include <QAbstractSpinBox>
KeyboardPanel::KeyboardPanel(QWidget* aParent)
: QWidget(aParent)
, mUI(new Ui::KeyboardPanel)
, mCurrentLineEdit(nullptr)
, mCurrentTextEdit(nullptr)
{
mUI->setupUi(this);
initializeStyle();
initUi();
setVisible(false);
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
}
KeyboardPanel::~KeyboardPanel()
{
delete mUI;
}
void KeyboardPanel::initializeStyle()
{
//#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);
}
void KeyboardPanel::initUi()
{
connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() {
if (QGuiApplication::inputMethod()->isVisible())
{
return;
}
hidePanel();
hide();
});
}
void KeyboardPanel::showPanel(QWidget* aInputWidget)
{
if (!aInputWidget)
{
return;
}
if (aInputWidget->inherits("QLineEdit"))
{
mCurrentLineEdit = qobject_cast<QLineEdit*>(aInputWidget);
mUI->dateEdit->setVisible(false);
mUI->lineEdit->setVisible(true);
mUI->textEdit->setVisible(false);
}
else if (aInputWidget->inherits("QTextEdit"))
{
mCurrentTextEdit = qobject_cast<QTextEdit*>(aInputWidget);
mUI->dateEdit->setVisible(false);
mUI->lineEdit->setVisible(false);
mUI->textEdit->setVisible(true);
}
showFullScreen();
setVisible(true);
activateWindow(); //it is quite important!!!
while (QApplication::activeWindow() != this)
{
QApplication::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);
}
else 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 KeyboardPanel::hidePanel()
{
if (mCurrentLineEdit)
{
mCurrentLineEdit->setText(mUI->lineEdit->text());
mUI->lineEdit->clearFocus();
mUI->lineEdit->clear();
mCurrentLineEdit = nullptr;
}
else if (mCurrentTextEdit)
{
mCurrentTextEdit->setText(mUI->textEdit->toPlainText());
mUI->textEdit->clearFocus();
mUI->textEdit->clear();
mCurrentTextEdit = nullptr;
}
}

View File

@@ -0,0 +1,35 @@
#ifndef KEYBOARDPANEL_H
#define KEYBOARDPANEL_H
#include <QWidget>
class QLineEdit;
class QDateEdit;
class QTextEdit;
namespace Ui
{
class KeyboardPanel;
}
class KeyboardPanel : public QWidget
{
Q_OBJECT
public:
explicit KeyboardPanel(QWidget* aParent = nullptr);
~KeyboardPanel();
void showPanel(QWidget* aInputWidget);
void hidePanel();
private:
void initializeStyle();
void initUi();
private:
Ui::KeyboardPanel* mUI;
QLineEdit* mCurrentLineEdit;
QTextEdit* mCurrentTextEdit;
};
#endif // KEYBOARDPANEL_H

View File

@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>KeyboardPanel</class>
<widget class="QWidget" name="KeyboardPanel">
<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>