Files
GUI/src/dialogs/AccountFormDialog.cpp
2025-05-30 14:22:46 +08:00

369 lines
11 KiB
C++

#include "AccountFormDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QUuid>
#include <QDebug>
#include <QToolButton>
#include <QPushButton>
#include <QRegExpValidator>
#include "event/EventCenter.h"
#include "log/LogManager.h"
#include "db/SQLHelper.h"
#include "models/User.h"
#include "components/ULineEdit.h"
#include "DialogManager.h"
AccountFormDialog::AccountFormDialog(const QString& aTitle, QWidget* parent, AccountEditMode mode, Qt::WindowFlags f)
: 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)
, mConfirmPwd(nullptr)
, mBtnPwd(nullptr)
, mLblError(new QLabel(this))
, mRefModel(nullptr)
{
auto layout = new QVBoxLayout(mFormWidget);
layout->setSpacing(10);
addTitleLabel(aTitle, layout);
initUserCodeUI(layout);
initUserNameUI(layout);
if (mMode == New) addNewModeUI(layout);
addCommentLabel(layout);
addWarnLabel(layout);
auto hlayout = new QHBoxLayout;
layout->addLayout(hlayout);
if (mMode == Self)addSelfModeUI(hlayout);
if (mMode != New)
{
addButtonPwd(hlayout);
addEndLine(layout);
connect(mBtnPwd, &QPushButton::clicked, this, mMode == Self ?
&AccountFormDialog::changeSelfPassword : &AccountFormDialog::resetUserPassword);
}
mLeUserName->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9\u4e00-\u9fa5]+$"), mLeUserName));
connect(mLeComment, &QLineEdit::textChanged, [=](const QString& text) {
mNewComment = text;
mCommentChanged = true;
});
connect(mLeUserName, &QLineEdit::textChanged, [=](const QString& text) {
mNewUserName = text;
mUserNameChanged = true;
});
mLeUserCode->setMaxLength(30);
}
void AccountFormDialog::addEndLine(QVBoxLayout* layout)
{
auto lblEndline = new QLabel(this);
lblEndline->setFixedHeight(2);
lblEndline->setObjectName("endline");
layout->addWidget(lblEndline);
}
void AccountFormDialog::resetUserPassword()
{
if (DialogManager::Default()->requestAlertMessage(tr("Reset to default password?"),DialogButtonMode::OkAndCancel) == Accepted)
{
LOG_USER_OPERATION("Password Reset, ID:" + mLeUserCode->text());
User user;
if (!User::getUser(mUserID, user))
{
DialogManager::Default()->requestAlertMessage(tr("Inner error, can't find reference user!"), DialogButtonMode::OkAndCancel);
return;
}
if (!user.resetPassword())
{
DialogManager::Default()->requestAlertMessage(tr("Submit change to database fail!"), DialogButtonMode::OkAndCancel);
}
}
}
void AccountFormDialog::changeSelfPassword()
{
DialogManager::Default()->requestChangePassword();
}
void AccountFormDialog::addButtonPwd(QHBoxLayout* layout)
{
mBtnPwd = new QToolButton(this);
mBtnPwd->setObjectName("changePwdBtn");
mBtnPwd->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
mBtnPwd->setText(mMode == Self ? tr("Change Password") : tr("Reset Password"));
layout->addWidget(mBtnPwd);
}
void AccountFormDialog::addWarnLabel(QVBoxLayout* layout)
{
mLblError->setObjectName("warn");
mLblError->setVisible(false);
layout->addWidget(mLblError);
}
void AccountFormDialog::addCommentLabel(QVBoxLayout* layout)
{
auto lblComment = new QLabel(this);
lblComment->setText(tr("Comment"));
layout->addWidget(lblComment);
layout->addWidget(mLeComment);
addEndLine(layout);
}
void AccountFormDialog::initUserNameUI(QVBoxLayout* layout)
{
auto lblUserName = new QLabel(this);
lblUserName->setText(tr("Name"));
mLeUserName->setPlaceholderText(tr("Input User name"));
layout->addWidget(lblUserName);
layout->addWidget(mLeUserName);
addEndLine(layout);
}
void AccountFormDialog::initUserCodeUI(QVBoxLayout* layout)
{
auto lblUserCode = new QLabel(this);
lblUserCode->setText(tr("User ID"));
mLeUserCode->setPlaceholderText(tr("Input User ID"));
if (mMode != New)mLeUserCode->setEnabled(false);
layout->addWidget(lblUserCode);
layout->addWidget(mLeUserCode);
addEndLine(layout);
}
void AccountFormDialog::addTitleLabel(const QString& aTitle, QVBoxLayout* layout)
{
auto lblTitle = new QLabel(this);
lblTitle->setAlignment(Qt::AlignCenter);
lblTitle->setText(aTitle);
lblTitle->setObjectName("title");
layout->addWidget(lblTitle);
}
void AccountFormDialog::addSelfModeUI(QHBoxLayout* hlayout)
{
auto btnLogout = new QToolButton(this);
btnLogout->setText(tr("Logout"));
btnLogout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
btnLogout->setObjectName("logoutBtn");
hlayout->addWidget(btnLogout);
connect(btnLogout, &QAbstractButton::clicked, [=]() {
if(DialogManager::Default()->requestAlertMessage(tr("Do you want to logout the current user?"), OkAndCancel, tr("Logout")) == QDialog::Rejected)
{
return;
}
accept();
LOG_USER_OPERATION("Logout")
EventCenter::Default()->triggerEvent(RequestLogin, nullptr, nullptr);
});
// load current user data
if (User::Current())
{
mLeUserCode->setText(User::Current()->getUserCode());
mUserPwd = User::Current()->getPassword();
mLeComment->setText(User::Current()->getComment());
mLeUserName->setText(User::Current()->getUserName());
mUserID = User::Current()->getUserID();
}
}
void AccountFormDialog::addNewModeUI(QVBoxLayout* layout)
{
auto lblPwd = new QLabel(this);
lblPwd->setText(tr("Password"));
layout->addWidget(lblPwd);
mLePwd = new ULineEdit(this);
mLePwd->setPlaceholderText(tr("Input password"));
mLePwd->setEchoMode(QLineEdit::Password);
mLePwd->setValidator(new QRegExpValidator(QRegExp("[a-zA-z0-9]+$"), mLePwd));
mLePwd->setMaxLength(30);
layout->addWidget(mLePwd);
mRoleID = User::getRoleID("doctor");
addEndLine(layout);
lblPwd = new QLabel(this);
lblPwd->setText(tr("Confirm Password"));
layout->addWidget(lblPwd);
mConfirmPwd = new ULineEdit(this);
mConfirmPwd->setPlaceholderText(tr("Input password"));
mConfirmPwd->setEchoMode(QLineEdit::Password);
mConfirmPwd->setValidator(new QRegExpValidator(QRegExp("[a-zA-z0-9]+$"), mConfirmPwd));
mConfirmPwd->setMaxLength(30);
layout->addWidget(mConfirmPwd);
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;
}
QString oldUserName = User::Current()->getUserName();
User::Current()->setUserName(mNewUserName);
LOG_USER_OPERATION(QString("Change User Name from %1 to %2").arg(oldUserName).arg(mNewUserName))
}
if (this->mCommentChanged)
{
User::Current()->setComment(mNewComment);
}
bool ret = User::Current()->submitChange();
if (ret)
{
hideWarn();
EventCenter::Default()->triggerEvent(CurrentUserInfoChanged, nullptr, nullptr);
}
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;
QString oldUserName;
if (this->mUserNameChanged)
{
if (mNewUserName.isEmpty())
{
warn(tr("User Name can't be empty!"));
return false;
}
oldUserName = user.getUserName();
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("Admin Change Acount Information");
if(mUserNameChanged)
{
LOG_USER_OPERATION(QString("Admin Change User Name from %1 to %2").arg(oldUserName).arg(mNewUserName));
}
}
return ret;
}
else
{
//add new
User user;
if (mLeUserCode->text().isEmpty())
{
warn(tr("User ID can't be empty!"));
return false;
}
if (mLeUserName->text().isEmpty())
{
warn(tr("User Name can't be empty!"));
return false;
}
if (mLePwd->text().isEmpty())
{
warn(tr("Password can't be empty!"));
return false;
}
if (mLePwd->text().length() < 6)
{
warn(tr("Password should at least 6 characters!"));
return false;
}
if (mLePwd->text() != mConfirmPwd->text())
{
warn(tr("Password and confirm password do not match!"));
return false;
}
if (!mRefModel)
{
warn(tr("Inner error ,unset data model!"));
return false;
}
if (User::existsUser(mLeUserCode->text()))
{
warn(tr("User Id exists!"));
return false;
}
hideWarn();
user.setUserName(mLeUserName->text());
user.setPassword(User::getEncryptedPassword(mLePwd->text()));
user.setRoleID(mRoleID);
user.setComment(mLeComment->text());
// User::insertUser(le_UserCode->text(),user);
mRefModel->insertRow(0);
mRefModel->setData(mRefModel->index(0, 0), QUuid::createUuid().toString());
mRefModel->setData(mRefModel->index(0, 1), mLeUserCode->text());
#define USER_READONLY_PROPERTY(name) name,
#define USER_PROPERTY(name)\
USER_READONLY_PROPERTY(name)
enum user_index {
USER_PROPERTIES_MACRO()
};
#undef USER_READONLY_PROPERTY
#undef USER_PROPERTY
#define USER_READONLY_PROPERTY(name)
#define USER_PROPERTY(name)\
USER_READONLY_PROPERTY(name)\
mRefModel->setData(mRefModel->index(0, name),user.get##name());
USER_PROPERTIES_MACRO()
#undef USER_READONLY_PROPERTY
#undef USER_PROPERTY
if (mRefModel->submit())
{
hideWarn();
LOG_USER_OPERATION("Add Account, ID:" + mLeUserCode->text());
return true;
}
else
{
warn(tr("Submit to data base fail!"));
return false;
}
}
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());
mUserID = values["UserID"].toString();
mUserPwd = values["Password"].toString();
}
void AccountFormDialog::warn(const QString& msg)
{
mLblError->setText(msg);
mLblError->setVisible(true);
}
void AccountFormDialog::hideWarn()
{
mLblError->setVisible(false);
}