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);
}

View File

@@ -5,23 +5,24 @@
#ifndef GUI_ACCOUNTFORMDIALOG_H
#define GUI_ACCOUNTFORMDIALOG_H
class QLabel;
class QLineEdit;
class ULineEdit;
class QToolButton;
class QSqlTableModel;
#include <QtWidgets/QVBoxLayout>
#include "GUIFormBaseDialog.h"
enum AccountEditMode{
enum AccountEditMode {
Self, Admin, New
};
class AccountFormDialog:public GUIFormBaseDialog{
class AccountFormDialog :public GUIFormBaseDialog {
Q_OBJECT
public:
explicit AccountFormDialog(QWidget *parent = nullptr,AccountEditMode mode = Self,Qt::WindowFlags f = Qt::WindowFlags());
explicit AccountFormDialog(QWidget* parent = nullptr, AccountEditMode mode = Self, Qt::WindowFlags f = Qt::WindowFlags());
~AccountFormDialog() override = default;
void setAccountInformation(const QMap<QString,QVariant>& values);
void setReferenceModel(QSqlTableModel* model){
void setAccountInformation(const QMap<QString, QVariant>& values);
void setReferenceModel(QSqlTableModel* model)
{
mRefModel = model;
}
protected:
@@ -29,15 +30,15 @@ protected:
void warn(const QString& msg);
void hideWarn();
private:
void addEndLine(QVBoxLayout *layout);
void addNewModeUI(QVBoxLayout *layout);
void addSelfModeUI(QHBoxLayout *layout);
void addTitleLabel(QVBoxLayout *layout);
void initUserCodeUI(QVBoxLayout *layout);
void initUserNameUI(QVBoxLayout *layout);
void addCommentLabel(QVBoxLayout *layout);
void addWarnLabel(QVBoxLayout *layout);
void addButtonPwd(QHBoxLayout *layout);
void addEndLine(QVBoxLayout* layout);
void addNewModeUI(QVBoxLayout* layout);
void addSelfModeUI(QHBoxLayout* layout);
void addTitleLabel(QVBoxLayout* layout);
void initUserCodeUI(QVBoxLayout* layout);
void initUserNameUI(QVBoxLayout* layout);
void addCommentLabel(QVBoxLayout* layout);
void addWarnLabel(QVBoxLayout* layout);
void addButtonPwd(QHBoxLayout* layout);
void changeSelfPassword();
void resetUserPassword();
QString mUserID;
@@ -48,10 +49,10 @@ private:
bool mCommentChanged;
bool mRoleChanged;
AccountEditMode mMode;
QLineEdit* mLeUserCode;
QLineEdit* mLeUserName;
QLineEdit* mLeComment;
QLineEdit* mLePwd;
ULineEdit* mLeUserCode;
ULineEdit* mLeUserName;
ULineEdit* mLeComment;
ULineEdit* mLePwd;
QToolButton* mBtnPwd;
QLabel* mLblError;

View File

@@ -4,22 +4,23 @@
#include <QVBoxLayout>
#include <QLabel>
#include <QtWidgets/QLineEdit>
#include "components/ULineEdit.h"
#include "models/User.h"
#include "log/UserOperationLog.h"
#include "ChangePasswordFormDialog.h"
ChangePasswordFormDialog::ChangePasswordFormDialog(QWidget* parent, Qt::WindowFlags f)
: GUIFormBaseDialog(parent, f)
, mLEPasswd(new QLineEdit(this))
, mLENewPasswd(new QLineEdit(this))
, mLEConfirmPasswd(new QLineEdit(this))
, mLblError(new QLabel(this))
: GUIFormBaseDialog(parent, f)
, mLEPasswd(new ULineEdit(this))
, mLENewPasswd(new ULineEdit(this))
, mLEConfirmPasswd(new ULineEdit(this))
, mLblError(new QLabel(this))
{
initLayout();
}
void ChangePasswordFormDialog::initLayout() {
void ChangePasswordFormDialog::initLayout()
{
auto layout = new QVBoxLayout(mFormWidget);
layout->setSpacing(10);
// add title
@@ -67,33 +68,36 @@ void ChangePasswordFormDialog::initLayout() {
}
bool ChangePasswordFormDialog::updateReferenceData() {
if (mLEPasswd->text().isEmpty())
{
mLblError->setText(tr("Please enter your old password!"));
return false;
}
if (mLENewPasswd->text().length() < 6) {
mLblError->setText(tr("New password should at least 6 characters!"));
return false;
}
QString encryptPwd = User::getEncryptedPassword(mLEPasswd->text());
if (encryptPwd != User::Current()->getPassword())
{
mLblError->setText(tr("Wrong password!"));
return false;
}
if (mLENewPasswd->text() != mLEConfirmPasswd->text())
{
mLblError->setText(tr("Your new password does not match!"));
return false;
}
User::Current()->setPassword(User::getEncryptedPassword(mLENewPasswd->text()));
if (!User::Current()->submitChange()) {
mLblError->setText(tr("Database update error!"));
User::Current()->restorePassword(encryptPwd);
return false;
}
LOG_USER_OPERATION(ChangePassword);
return true;
bool ChangePasswordFormDialog::updateReferenceData()
{
if (mLEPasswd->text().isEmpty())
{
mLblError->setText(tr("Please enter your old password!"));
return false;
}
if (mLENewPasswd->text().length() < 6)
{
mLblError->setText(tr("New password should at least 6 characters!"));
return false;
}
QString encryptPwd = User::getEncryptedPassword(mLEPasswd->text());
if (encryptPwd != User::Current()->getPassword())
{
mLblError->setText(tr("Wrong password!"));
return false;
}
if (mLENewPasswd->text() != mLEConfirmPasswd->text())
{
mLblError->setText(tr("Your new password does not match!"));
return false;
}
User::Current()->setPassword(User::getEncryptedPassword(mLENewPasswd->text()));
if (!User::Current()->submitChange())
{
mLblError->setText(tr("Database update error!"));
User::Current()->restorePassword(encryptPwd);
return false;
}
LOG_USER_OPERATION(ChangePassword);
return true;
}

View File

@@ -6,12 +6,12 @@
#define GUI_CHANGEPASSWORDFORMDIALOG_H
#include "GUIFormBaseDialog.h"
class QLineEdit;
class ULineEdit;
class QLabel;
class ChangePasswordFormDialog:public GUIFormBaseDialog{
class ChangePasswordFormDialog :public GUIFormBaseDialog {
Q_OBJECT
public:
explicit ChangePasswordFormDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
explicit ChangePasswordFormDialog(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~ChangePasswordFormDialog() override = default;
protected:
@@ -19,9 +19,9 @@ protected:
private:
void initLayout();
QLineEdit* mLEPasswd;
QLineEdit* mLENewPasswd;
QLineEdit* mLEConfirmPasswd;
ULineEdit* mLEPasswd;
ULineEdit* mLENewPasswd;
ULineEdit* mLEConfirmPasswd;
QLabel* mLblError;