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

@@ -9,27 +9,27 @@
#include <QDebug>
#include <QToolButton>
#include <QPushButton>
#include <QLineEdit>
#include "ChangePasswordFormDialog.h"
#include "event/EventCenter.h"
#include "log/UserOperationLog.h"
#include "db/SQLHelper.h"
#include "models/User.h"
#include "AlertDialog.h"
#include "components/ULineEdit.h"
AccountFormDialog::AccountFormDialog(QWidget* parent, AccountEditMode mode, Qt::WindowFlags f)
: GUIFormBaseDialog(parent, f)
, mUserNameChanged(false)
, mCommentChanged(false)
, mRoleChanged(false)
, mMode(mode)
, mLeUserCode(new QLineEdit(this))
, mLeUserName(new QLineEdit(this))
, mLeComment(new QLineEdit(this))
, mLePwd(nullptr)
, mBtnPwd(nullptr)
, mLblError(new QLabel(this))
, mRefModel(nullptr)
: GUIFormBaseDialog(parent, f)
, mUserNameChanged(false)
, mCommentChanged(false)
, mRoleChanged(false)
, mMode(mode)
, mLeUserCode(new ULineEdit(this))
, mLeUserName(new ULineEdit(this))
, mLeComment(new ULineEdit(this))
, mLePwd(nullptr)
, mBtnPwd(nullptr)
, mLblError(new QLabel(this))
, mRefModel(nullptr)
{
auto layout = new QVBoxLayout(mFormWidget);
layout->setSpacing(10);
@@ -42,75 +42,85 @@ AccountFormDialog::AccountFormDialog(QWidget* parent, AccountEditMode mode, Qt::
auto hlayout = new QHBoxLayout;
layout->addLayout(hlayout);
if (mMode == Self)addSelfModeUI(hlayout);
if (mMode != New) {
if (mMode != New)
{
addButtonPwd(hlayout);
addEndLine(layout);
connect(mBtnPwd, &QPushButton::clicked, this, mMode == Self ?
&AccountFormDialog::changeSelfPassword : &AccountFormDialog::resetUserPassword);
&AccountFormDialog::changeSelfPassword : &AccountFormDialog::resetUserPassword);
}
connect(mLeComment, &QLineEdit::textChanged, [=](const QString &text) {
connect(mLeComment, &QLineEdit::textChanged, [=](const QString& text) {
mCommentChanged = true;
});
connect(mLeUserName, &QLineEdit::textChanged, [=](const QString &text) {
});
connect(mLeUserName, &QLineEdit::textChanged, [=](const QString& text) {
mNewUserName = text;
mUserNameChanged = true;
});
});
}
void AccountFormDialog::addEndLine(QVBoxLayout *layout) {
void AccountFormDialog::addEndLine(QVBoxLayout* layout)
{
auto lblEndline = new QLabel(this);
lblEndline->setFixedHeight(2);
lblEndline->setObjectName("endline");
layout->addWidget(lblEndline);
}
void AccountFormDialog::resetUserPassword() {
void AccountFormDialog::resetUserPassword()
{
AlertDialog dialog(this);
dialog.setGeometry(geometry());
dialog.setButtonMode(OkAndCancel);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAlertMessage(tr("Reset password to \"123456\" ?"));
if (dialog.exec() == Accepted) {
if (dialog.exec() == Accepted)
{
User user;
dialog.setButtonMode(OkOnly);
if (!User::getUser(mUserID, user)) {
if (!User::getUser(mUserID, user))
{
dialog.setAlertMessage(tr("Inner error, can't find reference user!"));
dialog.exec();
return;
}
if (!user.resetPassword()) {
if (!user.resetPassword())
{
dialog.setAlertMessage(tr("Submit change to database fail!"));
dialog.exec();
}
}
}
void AccountFormDialog::changeSelfPassword() {
void AccountFormDialog::changeSelfPassword()
{
ChangePasswordFormDialog dia(this);
dia.setGeometry(geometry());
dia.setWindowModality(Qt::WindowModal);
dia.exec();
}
void AccountFormDialog::addButtonPwd(QHBoxLayout *layout) {
void AccountFormDialog::addButtonPwd(QHBoxLayout* layout)
{
mBtnPwd = new QToolButton(this);
mBtnPwd->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
mBtnPwd->setObjectName("editvalBtn");
mBtnPwd->setIcon(QIcon(":/icons/edit.png"));
mBtnPwd->setIconSize({30, 30});
mBtnPwd->setIconSize({ 30, 30 });
mBtnPwd->setText(mMode == Self ? tr("Change Password") : tr("Reset Password"));
mBtnPwd->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addWidget(mBtnPwd);
}
void AccountFormDialog::addWarnLabel(QVBoxLayout *layout) {
void AccountFormDialog::addWarnLabel(QVBoxLayout* layout)
{
mLblError->setObjectName("warn");
mLblError->setVisible(false);
layout->addWidget(mLblError);
}
void AccountFormDialog::addCommentLabel(QVBoxLayout *layout) {
void AccountFormDialog::addCommentLabel(QVBoxLayout* layout)
{
auto lblComment = new QLabel(this);
lblComment->setText(tr("Comment"));
layout->addWidget(lblComment);
@@ -118,7 +128,8 @@ void AccountFormDialog::addCommentLabel(QVBoxLayout *layout) {
addEndLine(layout);
}
void AccountFormDialog::initUserNameUI(QVBoxLayout *layout) {
void AccountFormDialog::initUserNameUI(QVBoxLayout* layout)
{
auto lblUserName = new QLabel(this);
lblUserName->setText(tr("Name"));
@@ -128,7 +139,8 @@ void AccountFormDialog::initUserNameUI(QVBoxLayout *layout) {
addEndLine(layout);
}
void AccountFormDialog::initUserCodeUI(QVBoxLayout *layout) {
void AccountFormDialog::initUserCodeUI(QVBoxLayout* layout)
{
auto lblUserCode = new QLabel(this);
lblUserCode->setText(tr("User ID"));
@@ -139,7 +151,8 @@ void AccountFormDialog::initUserCodeUI(QVBoxLayout *layout) {
addEndLine(layout);
}
void AccountFormDialog::addTitleLabel(QVBoxLayout *layout) {
void AccountFormDialog::addTitleLabel(QVBoxLayout* layout)
{
auto lblTitle = new QLabel(this);
lblTitle->setAlignment(Qt::AlignCenter);
lblTitle->setText(tr("Account"));
@@ -147,11 +160,12 @@ void AccountFormDialog::addTitleLabel(QVBoxLayout *layout) {
layout->addWidget(lblTitle);
}
void AccountFormDialog::addSelfModeUI(QHBoxLayout *hlayout) {
void AccountFormDialog::addSelfModeUI(QHBoxLayout* hlayout)
{
auto btnLogout = new QToolButton(this);
btnLogout->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
btnLogout->setIcon(QIcon(":/icons/logout.png"));
btnLogout->setIconSize({30, 30});
btnLogout->setIconSize({ 30, 30 });
btnLogout->setText(tr("Logout"));
btnLogout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
btnLogout->setObjectName("editvalBtn");
@@ -160,10 +174,11 @@ void AccountFormDialog::addSelfModeUI(QHBoxLayout *hlayout) {
connect(btnLogout, &QAbstractButton::clicked, [=]() {
accept();
LOG_USER_OPERATION(Logout)
EventCenter::Default()->triggerEvent(RequestLogin, nullptr, nullptr);
});
EventCenter::Default()->triggerEvent(RequestLogin, nullptr, nullptr);
});
// load current user data
if (User::Current()) {
if (User::Current())
{
mLeUserCode->setText(User::Current()->getUserCode());
mLeUserName->setText(User::Current()->getUserName());
mUserID = User::Current()->getUserID();
@@ -171,11 +186,12 @@ void AccountFormDialog::addSelfModeUI(QHBoxLayout *hlayout) {
}
}
void AccountFormDialog::addNewModeUI(QVBoxLayout *layout) {
void AccountFormDialog::addNewModeUI(QVBoxLayout* layout)
{
auto lblPwd = new QLabel(this);
lblPwd->setText(tr("Password"));
layout->addWidget(lblPwd);
mLePwd = new QLineEdit(this);
mLePwd = new ULineEdit(this);
mLePwd->setPlaceholderText(tr("Input password"));
mLePwd->setEchoMode(QLineEdit::Password);
layout->addWidget(mLePwd);
@@ -183,66 +199,82 @@ void AccountFormDialog::addNewModeUI(QVBoxLayout *layout) {
addEndLine(layout);
}
bool AccountFormDialog::updateReferenceData() {
if (mMode == Self) {
if (!this->mUserNameChanged && !this->mCommentChanged) return true;
if (!this->mUserNameChanged) {
if (mNewUserName.isEmpty()) {
warn(tr("User Name can't be empty!"));
return false;
}
User::Current()->setUserName(mNewUserName);
}
if (!this->mCommentChanged) User::Current()->setUserName(mLeComment->text());
bool ret = User::Current()->submitChange();
if (ret) {
hideWarn();
LOG_USER_OPERATION(ChangeUserName)
}
else {
warn(tr("Submit change to database fail!"));
}
return ret;
}
else if (mMode == Admin) {
if (!this->mUserNameChanged && !this->mRoleChanged) return true;
User user;
if (!User::getUser(mUserID, user)) return true;
if (this->mUserNameChanged) {
if (mNewUserName.isEmpty()) {
warn(tr("User Name can't be empty!"));
return false;
}
user.setUserName(mNewUserName);
}
if (this->mRoleChanged) user.setRoleID(mRoleID);
if (!this->mCommentChanged) user.setComment(mLeComment->text());
bool ret = user.submitChange();
if (ret) {
LOG_USER_OPERATION(AdminChangeAcountInformation)
}
return ret;
}
else {
bool AccountFormDialog::updateReferenceData()
{
if (mMode == Self)
{
if (!this->mUserNameChanged && !this->mCommentChanged) return true;
if (!this->mUserNameChanged)
{
if (mNewUserName.isEmpty())
{
warn(tr("User Name can't be empty!"));
return false;
}
User::Current()->setUserName(mNewUserName);
}
if (!this->mCommentChanged) User::Current()->setUserName(mLeComment->text());
bool ret = User::Current()->submitChange();
if (ret)
{
hideWarn();
LOG_USER_OPERATION(ChangeUserName)
}
else
{
warn(tr("Submit change to database fail!"));
}
return ret;
}
else if (mMode == Admin)
{
if (!this->mUserNameChanged && !this->mRoleChanged) return true;
User user;
if (!User::getUser(mUserID, user)) return true;
if (this->mUserNameChanged)
{
if (mNewUserName.isEmpty())
{
warn(tr("User Name can't be empty!"));
return false;
}
user.setUserName(mNewUserName);
}
if (this->mRoleChanged) user.setRoleID(mRoleID);
if (!this->mCommentChanged) user.setComment(mLeComment->text());
bool ret = user.submitChange();
if (ret)
{
LOG_USER_OPERATION(AdminChangeAcountInformation)
}
return ret;
}
else
{
//add new
User user;
if (mLeUserCode->text().isEmpty()) {
if (mLeUserCode->text().isEmpty())
{
warn(tr("User ID can't be empty!"));
return false;
}
if (mLeUserName->text().isEmpty()) {
if (mLeUserName->text().isEmpty())
{
warn(tr("User Name can't be empty!"));
return false;
}
if (mLePwd->text().isEmpty()) {
if (mLePwd->text().isEmpty())
{
warn(tr("Password can't be empty!"));
return false;
}
if (!mRefModel) {
if (!mRefModel)
{
warn(tr("Inner error ,unset data model!"));
return false;
}
if (User::existsUser(mLeUserCode->text())) {
if (User::existsUser(mLeUserCode->text()))
{
warn(tr("User Id exists!"));
return false;
}
@@ -274,30 +306,36 @@ bool AccountFormDialog::updateReferenceData() {
#undef USER_READONLY_PROPERTY
#undef USER_PROPERTY
if (mRefModel->submit()) {
hideWarn();
return true;
} else {
warn(tr("Submit to data base fail!"));
return false;
}
if (mRefModel->submit())
{
hideWarn();
return true;
}
else
{
warn(tr("Submit to data base fail!"));
return false;
}
}
return true;
return true;
}
void AccountFormDialog::setAccountInformation(const QMap<QString, QVariant>& values) {
mLeUserCode->setText(values["UserCode"].toString());
mLeUserName->setText(values["UserName"].toString());
mLeComment->setText(values["Comment"].toString());
void AccountFormDialog::setAccountInformation(const QMap<QString, QVariant>& values)
{
mLeUserCode->setText(values["UserCode"].toString());
mLeUserName->setText(values["UserName"].toString());
mLeComment->setText(values["Comment"].toString());
mUserID = values["UserID"].toString();
mUserPwd = values["Password"].toString();
}
void AccountFormDialog::warn(const QString& msg) {
mLblError->setText(msg);
mLblError->setVisible(true);
void AccountFormDialog::warn(const QString& msg)
{
mLblError->setText(msg);
mLblError->setVisible(true);
}
void AccountFormDialog::hideWarn() {
mLblError->setVisible(false);
void AccountFormDialog::hideWarn()
{
mLblError->setVisible(false);
}