Account Table and form update, add edit and add function .

This commit is contained in:
Krad
2021-12-10 10:17:45 +08:00
parent aed07807a6
commit dce0ad563c
4 changed files with 330 additions and 44 deletions

View File

@@ -5,6 +5,8 @@
#include "ChangePasswordFormDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QUuid>
#include <QDebug>
#include <QToolButton>
#include <QPushButton>
#include <QLineEdit>
@@ -12,7 +14,11 @@
#include <src/log/UserOperationLog.h>
#include "db/SQLHelper.h"
#include "models/User.h"
AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
#include "components/SlidePickerBox.h"
#include "SelectDialog.h"
AccountFormDialog::AccountFormDialog(QWidget *parent,AccountEditMode mode, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
m_mode = mode;
QVBoxLayout* layout = new QVBoxLayout(formWidget);
layout->setSpacing(10);
// add title
@@ -25,8 +31,9 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
//add usercode
QLabel* lbl_UserCode = new QLabel(this);
lbl_UserCode->setText(tr("User ID"));
QLineEdit* le_UserCode = new QLineEdit(this);
le_UserCode->setEnabled(false);
le_UserCode = new QLineEdit(this);
le_UserCode->setPlaceholderText(tr("Input User ID"));
if(m_mode != New)le_UserCode->setEnabled(false);
layout->addWidget(lbl_UserCode);
layout->addWidget(le_UserCode);
QLabel* lbl_endline1 = new QLabel(this);
@@ -36,7 +43,8 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
//add username
QLabel* lbl_UserName = new QLabel(this);
lbl_UserName->setText(tr("Name"));
QLineEdit* le_UserName = new QLineEdit(this);
le_UserName = new QLineEdit(this);
le_UserName->setPlaceholderText(tr("Input User name"));
layout->addWidget(lbl_UserName);
layout->addWidget(le_UserName);
QLabel* lbl_endline2 = new QLabel(this);
@@ -46,48 +54,113 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
//add password
QLabel* lbl_Pwd = new QLabel(this);
lbl_Pwd->setText(tr("Password"));
QPushButton* btn_Pwd = new QPushButton(this);
btn_Pwd->setText(tr("Change Password"));
layout->addWidget(lbl_Pwd);
layout->addWidget(btn_Pwd);
// add new mode
if (m_mode == New)
{
le_Pwd = new QLineEdit(this);
le_Pwd->setPlaceholderText(tr("Input password"));
le_Pwd->setEchoMode(QLineEdit::Password);
layout->addWidget(le_Pwd);
} else{//edit mode
btn_Pwd = new QPushButton(this);
btn_Pwd->setText(tr(m_mode==Self?"Change Password":"Reset Password"));
layout->addWidget(btn_Pwd);
}
QLabel* lbl_endline3 = new QLabel(this);
lbl_endline3->setObjectName("endline");
layout->addWidget(lbl_endline3);
//add logout
QLabel* lbl_Logout = new QLabel(this);
lbl_Logout->setText(tr("Logout"));
QPushButton* btn_Logout= new QPushButton(this);
btn_Logout->setText(tr("Logout"));
layout->addWidget(lbl_Logout);
layout->addWidget(btn_Logout);
if (m_mode == Self)
{
//add logout
QLabel* lbl_Logout = new QLabel(this);
lbl_Logout->setText(tr("Logout"));
QPushButton* btn_Logout= new QPushButton(this);
btn_Logout->setText(tr("Logout"));
layout->addWidget(lbl_Logout);
layout->addWidget(btn_Logout);
QLabel* lbl_endline0 = new QLabel(this);
lbl_endline0->setObjectName("endline");
layout->addWidget(lbl_endline0);
// load current user data
if (User::Current())
{
le_UserCode->setText(User::Current()->getUserCode());
le_UserName->setText(User::Current()->getUserName());
m_UserID = User::Current()->getUserID();
m_UserPwd = User::Current()->getPassword();
}
connect(btn_Pwd, &QPushButton::clicked,[t=this](){
ChangePasswordFormDialog dia(t->parentWidget());
dia.setWindowModality(Qt::WindowModal);
dia.exec();
});
connect(btn_Logout, &QAbstractButton::clicked, [=](){
this->accept();
LOG_USER_OPERATION(Logout);
EventCenter::Default()->triggerEvent(GUIEvents::RequestLogin, nullptr, nullptr);
});
}
else
{
//add change role
QLabel* lbl_Role = new QLabel(this);
lbl_Role->setText(tr("Change Role"));
btn_Role= new QPushButton(this);
layout->addWidget(lbl_Role);
layout->addWidget(btn_Role);
QLabel* lbl_endline0 = new QLabel(this);
lbl_endline0->setObjectName("endline");
layout->addWidget(lbl_endline0);
//set default value
if (m_mode == New) {
btn_Role->setText("doctor");
m_RoleID = User::getRoleID("doctor");
}
connect(btn_Role,&QAbstractButton::clicked,[=](){
SelectDialog selectDialog(this);
selectDialog.setAvailableDates(User::getAllRoleName());
selectDialog.setSelectedValue(btn_Role->text());
selectDialog.setWindowModality(Qt::WindowModal);
// submit change
if (selectDialog.exec() == Accepted)
{
if (selectDialog.getSelectedValue() != btn_Role->text())
{
QString newRoleID = User::getRoleID(selectDialog.getSelectedValue());
btn_Role->setText(selectDialog.getSelectedValue());
m_RoleID = newRoleID;
qDebug()<<"new Role ID:"<<newRoleID;
this->roleChanged = true;
}
}
});
}
//add Comment
QLabel* lbl_Comment = new QLabel(this);
lbl_Comment->setText(tr("Comment"));
le_Comment = new QLineEdit(this);
layout->addWidget(lbl_Comment);
layout->addWidget(le_Comment);
QLabel* lbl_endline0 = new QLabel(this);
lbl_endline0->setObjectName("endline");
layout->addWidget(lbl_endline0);
// load current user data
if (User::Current())
{
le_UserCode->setText(User::Current()->getUserCode());
le_UserName->setText(User::Current()->getUserName());
m_UserID = User::Current()->getUserID();
m_UserPwd = User::Current()->getPassword();
}
connect(le_UserName, &QLineEdit::textChanged, [t=this](const QString& text){
t->m_NewUserName = text;
t->userNameChanged = true;
lbl_error = new QLabel(this);
lbl_error->setObjectName("warn");
lbl_error->setVisible(false);
layout->addWidget(lbl_error);
connect(le_Comment, &QLineEdit::textChanged, [=](const QString& text){
commentChanged = true;
});
connect(btn_Pwd, &QPushButton::clicked,[t=this](){
ChangePasswordFormDialog dia(t->parentWidget());
dia.setWindowModality(Qt::WindowModal);
dia.exec();
connect(le_UserName, &QLineEdit::textChanged, [=](const QString& text){
m_NewUserName = text;
userNameChanged = true;
});
connect(btn_Logout, &QAbstractButton::clicked, [=](){
this->accept();
LOG_USER_OPERATION(Logout);
EventCenter::Default()->triggerEvent(GUIEvents::RequestLogin, nullptr, nullptr);
});
}
AccountFormDialog::~AccountFormDialog() {
@@ -95,12 +168,122 @@ AccountFormDialog::~AccountFormDialog() {
}
bool AccountFormDialog::updateReferenceData() {
if (!this->userNameChanged) return true;
User::Current()->setUserName(m_NewUserName);
bool ret = User::Current()->submitChange();
if(ret)
{
LOG_USER_OPERATION(ChangeUserName);
if (m_mode == Self) {
if (!this->userNameChanged && !this->commentChanged) return true;
if (!this->userNameChanged) {
if (m_NewUserName.isEmpty()){
warn(tr("User Name can't be empty!"));
return false;
}
User::Current()->setUserName(m_NewUserName);
}
if (!this->commentChanged) User::Current()->setUserName(le_Comment->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 (m_mode == Admin){
if (!this->userNameChanged && !this->roleChanged) return true;
User user;
if (!User::getUser(m_UserID, user)) return true;
if (this->userNameChanged){
if (m_NewUserName.isEmpty()){
warn(tr("User Name can't be empty!"));
return false;
}
user.setUserName(m_NewUserName);
}
if (this->roleChanged) user.setRoleID(m_RoleID);
if (!this->commentChanged) user.setUserName(le_Comment->text());
bool ret = user.submitChange();
if (ret) {
LOG_USER_OPERATION(AdminChangeAcountInformation);
}
return ret;
} else {
//add new
User user;
if (le_UserCode->text().isEmpty()) {
warn(tr("User ID can't be empty!"));
return false;
}
if (le_UserName->text().isEmpty()) {
warn(tr("User Name can't be empty!"));
return false;
}
if (le_Pwd->text().isEmpty()) {
warn(tr("Password can't be empty!"));
return false;
}
if (!refmodel){
warn(tr("Inner error ,unset data model!"));
return false;
}
if (User::existsUser(le_UserCode->text())){
warn(tr("User Id exists!"));
return false;
}
hideWarn();
user.setUserName(le_UserName->text());
user.setPassword(User::getEncryptedPassword(le_Pwd->text()));
user.setRoleID(m_RoleID);
user.setComment(le_Comment->text());
// User::insertUser(le_UserCode->text(),user);
refmodel->insertRow(0);
refmodel->setData(refmodel->index(0, 0), QUuid::createUuid().toString());
refmodel->setData(refmodel->index(0, 1), le_UserCode->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)\
refmodel->setData(refmodel->index(0, name),user.get##name());
USER_PROPERTIES_MACRO()
#undef USER_READONLY_PROPERTY
#undef USER_PROPERTY
if (refmodel->submit()) {
hideWarn();
return true;
} else {
warn(tr("Submit to data base fail!"));
return false;
}
}
return ret;
return true;
}
void AccountFormDialog::setAccountInformation(const QMap<QString, QVariant> & values) {
le_UserCode->setText(values["UserCode"].toString());
le_UserName->setText(values["UserName"].toString());
le_Comment->setText(values["Comment"].toString());
m_UserID = values["UserID"].toString();
m_UserPwd = values["Password"].toString();
if (btn_Role){
m_RoleID = values["RoleID"].toString();
btn_Role->setText(User::getRoleName(m_RoleID));
}
}
void AccountFormDialog::warn(QString msg) {
lbl_error->setText(msg);
lbl_error->setVisible(true);
}
void AccountFormDialog::hideWarn() {
this->lbl_error->setVisible(false);
}

View File

@@ -4,21 +4,45 @@
#ifndef GUI_ACCOUNTFORMDIALOG_H
#define GUI_ACCOUNTFORMDIALOG_H
class QLabel;
class QLineEdit;
class QPushButton;
class QSqlTableModel;
#include "GUIFormBaseDialog.h"
enum AccountEditMode{
Self, Admin, New
};
class AccountFormDialog:public GUIFormBaseDialog{
Q_OBJECT
public:
explicit AccountFormDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
explicit AccountFormDialog(QWidget *parent = nullptr,AccountEditMode mode = Self,Qt::WindowFlags f = Qt::WindowFlags());
~AccountFormDialog();
void setAccountInformation(const QMap<QString,QVariant>& values);
void setReferenceModel(QSqlTableModel* model){
refmodel = model;
}
protected:
bool updateReferenceData() override;
void warn(QString msg);
void hideWarn();
private:
QString m_UserID;
QString m_UserPwd;
QString m_RoleID;
QString m_NewUserName;
bool userNameChanged = false;
bool commentChanged = false;
bool roleChanged = false;
AccountEditMode m_mode = Self;
QLineEdit* le_UserCode = nullptr;
QLineEdit* le_UserName = nullptr;
QLineEdit* le_Comment = nullptr;
QLineEdit* le_Pwd = nullptr;
QPushButton* btn_Pwd = nullptr;
QPushButton* btn_Role = nullptr;
QLabel* lbl_error = nullptr;
QSqlTableModel* refmodel = nullptr;
};

View File

@@ -4,13 +4,21 @@
#include "AccountTableForm.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolButton>
#include <QHeaderView>
#include <src/components/AccountRoleComboDelegate.h>
#include "guimacros.h"
#include "AlertDialog.h"
#include "db/SQLHelper.h"
#include "models/User.h"
#include <QSqlRecord>
#include "components/SlideableTableView.h"
#include "AccountFormDialog.h"
AccountTableForm::AccountTableForm(QWidget *parent) {
layout = new QVBoxLayout(this);
layout->setMargin(0);
QTableView* table = new SlideableTableView(this);
layout->addWidget(table);
// TableView for patient
@@ -40,6 +48,76 @@ AccountTableForm::AccountTableForm(QWidget *parent) {
table->setColumnWidth(1,250);
table->setColumnWidth(2,250);
table->setColumnWidth(4,150);
QWidget* cmdPanel = new QWidget(this);
cmdPanel->setObjectName("commandWidget");
QHBoxLayout* cmdLayout = new QHBoxLayout(cmdPanel);
cmdLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
QWidget* spacerLine= new QWidget(this);
spacerLine->setFixedWidth(2);
spacerLine->setObjectName("verSpaceLine");
cmdLayout->addWidget(spacerLine);
ADD_TOOL_BTN_TO_LAYOUT(Add,":/icons/add.png",cmdLayout);
ADD_TOOL_BTN_TO_LAYOUT(Edit,":/icons/details.png",cmdLayout);
ADD_TOOL_BTN_TO_LAYOUT(Delete,":/icons/close_circle.png",cmdLayout);
layout->addWidget(cmdPanel);
//index change
connect(table,&QTableView::clicked,[=](const QModelIndex & modelIndex){
if (currentRow!=modelIndex.row())
{
currentRow=modelIndex.row();
}
});
//add new account
connect(btnAdd, &QToolButton::clicked,[=](){
AccountFormDialog dialog(this,New);
dialog.setWindowModality(Qt::WindowModal);
dialog.setReferenceModel(model);
if (dialog.exec() == QDialog::Accepted)
{
table->selectRow(0);
}
});
connect(btnEdit, &QToolButton::clicked,[=](){
if (currentRow<0)return;
QMap<QString,QVariant> map;
auto record = model->record(currentRow);
for (int i = 0; i < model->columnCount(); i++)
{
map[record.fieldName(i)] = record.value(i);
}
auto mode = map["UserID"] == User::Current()->getUserID()?Self:Admin;
AccountFormDialog dialog(this,mode);
dialog.setWindowModality(Qt::WindowModal);
if (mode == Admin)dialog.setAccountInformation(map);
if (dialog.exec() == QDialog::Accepted)
{
model->select();
table->selectRow(currentRow);
}
});
connect(btnDelete, &QToolButton::clicked,[=](){
if (currentRow<0)return;
QString id = model->data(model->index(currentRow,1)).toString();
if (User::Current()->getUserID() == id)
{
//尝试删除自己
AlertDialog dialog(this);
dialog.setButtonMode(OkOnly);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAlertMessage(tr("Can't delete current log in account!"));
dialog.exec();
return;
}
AlertDialog dialog(this);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAlertMessage(QString(tr("Delete account with ID:\"%1\"!")).arg(id));
if(dialog.exec()!=QDialog::Accepted) return;
model->removeRow(currentRow);
model->select();
table->selectRow(model->rowCount()>currentRow+1?currentRow:currentRow-1);
});
}
AccountTableForm::~AccountTableForm() {

View File

@@ -17,6 +17,7 @@ public:
~AccountTableForm();
private:
QVBoxLayout* layout = nullptr;
int currentRow = -1;
};