Refactor DialogManager.2

This commit is contained in:
Krad
2022-07-20 16:53:46 +08:00
parent acc53c463c
commit 0291c7d995
6 changed files with 4 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
//
// Created by Krad on 2022/3/24.
//
#include "DateSelectDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QDate>
#include "components/DateSlidePickerBox.h"
DateSelectDialog::DateSelectDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
this->setFixedSize(460, 380);
QVBoxLayout* layout = new QVBoxLayout(mFormWidget);
box = new DateSlidePickerBox(mFormWidget);
box->setObjectName("slidePicker");
box->setSelectedValue("1990-01-01");
lbl_error = new QLabel(this);
lbl_error->setObjectName("warn");
lbl_error->setVisible(false);
lbl_error->setText(tr("Date exceeded!"));
layout->addWidget(box);
layout->addWidget(lbl_error);
}
DateSelectDialog::~DateSelectDialog() {
}
QString DateSelectDialog::getSelectedValue() {
return box->getSelectedValue();
}
void DateSelectDialog::setSelectedValue(const QString &val) {
box->setSelectedValue(val);
box->resizeLabel();
}
bool DateSelectDialog::updateReferenceData() {
if (onlyBackward){
QDate v = QDate::fromString(box->getSelectedValue(),"yyyy-MM-dd");
bool flag = QDate::currentDate()>=v;
this->setFixedHeight(flag? 380:410);
lbl_error->setVisible(!flag);
return flag;
}
return true;
}
void DateSelectDialog::showEvent(QShowEvent * event) {
QDialog::showEvent(event);
}

View File

@@ -0,0 +1,30 @@
//
// Created by Krad on 2022/3/24.
//
#ifndef GUI_DATESELECTDIALOG_H
#define GUI_DATESELECTDIALOG_H
#include "dialogs/GUIFormBaseDialog.h"
class DateSlidePickerBox;
class QLabel;
class DateSelectDialog:public GUIFormBaseDialog{
Q_OBJECT
public:
explicit DateSelectDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~DateSelectDialog() override;
QString getSelectedValue();
void setSelectedValue(const QString& val);
void showEvent(QShowEvent *) override;
void setOnlyBackward(bool val){
onlyBackward = val;
}
protected:
bool updateReferenceData() override;
DateSlidePickerBox* box;
QLabel* lbl_error = nullptr;
bool onlyBackward = true;
};
#endif //GUI_DATESELECTDIALOG_H

View File

@@ -106,7 +106,6 @@ void DialogManager::raiseDeviceError(QObject *parent, QObject *msg) {
topWidgetStore.push(dialog);
dialog->setWindowModality(Qt::NonModal);
dialog->exec();
mErrorCount.fetch_sub(1);
auto lastDialog = topWidgetStore.pop();
lastDialog->deleteLater();
}

View File

@@ -0,0 +1,218 @@
//
// Created by Krad on 2022/3/21.
//
#include "EditPatientDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QDateTime>
#include <QToolButton>
#include <QSqlTableModel>
#include <QUuid>
#include "dialogs/SelectDialog.h"
#include "DateSelectDialog.h"
#include "components/ListBox.h"
#include "components/ULineEdit.h"
#include "components/UTextEdit.h"
int queryValue(QSqlTableModel* model, int colID, const QVariant& var)
{
for (int i = 0; i < model->rowCount(); ++i)
{
if (model->data(model->index(i, colID)) == var) return i;
}
return -1;
}
EditPatientDialog::EditPatientDialog(QWidget* parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f)
{
QVBoxLayout* layout = new QVBoxLayout(mFormWidget);
layout->setSpacing(10);
// add title
QLabel* lbl_title = new QLabel(this);
lbl_title->setAlignment(Qt::AlignCenter);
lbl_title->setText(tr("Edit Patient"));
lbl_title->setObjectName("title");
layout->addWidget(lbl_title);
//add old password
QLabel* lbl_id = new QLabel(this);
lbl_id->setText(tr("ID"));
le_id = new ULineEdit(this);
layout->addWidget(lbl_id);
layout->addWidget(le_id);
QLabel* lbl_endline1 = new QLabel(this);
lbl_endline1->setObjectName("endline");
layout->addWidget(lbl_endline1);
QLabel* lbl_name = new QLabel(this);
lbl_name->setText(tr("Name"));
le_name = new ULineEdit(this);
layout->addWidget(lbl_name);
layout->addWidget(le_name);
QLabel* lbl_endline2 = new QLabel(this);
lbl_endline2->setObjectName("endline");
layout->addWidget(lbl_endline2);
QLabel* lbl_sex = new QLabel(this);
lbl_sex->setText(tr("Gender"));
layout->addWidget(lbl_sex);
btnSex = new ListBox(this);
btnSex->setText(tr("Female"));
btnSex->setProperty("idx", 0);
btnSex->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
connect(btnSex, &QToolButton::clicked, [=]() {
SelectDialog dialog(this);
QStringList items;
items << tr("Female") << tr("Male") << tr("Other");
dialog.setValues(items);
dialog.setSelectedValue(items[btnSex->property("idx").toInt()]);
dialog.setWindowModality(Qt::WindowModal);
if (dialog.exec() == QDialog::Accepted)
{
btnSex->setText(dialog.getSelectedValue());
btnSex->setProperty("idx", items.indexOf(dialog.getSelectedValue()));
}
});
layout->addWidget(btnSex);
QLabel* lbl_endline9 = new QLabel(this);
lbl_endline9->setFixedHeight(2);
lbl_endline9->setObjectName("endline");
layout->addWidget(lbl_endline9);
QLabel* lbl_date = new QLabel(this);
lbl_date->setText(tr("Birth Date"));
layout->addWidget(lbl_date);
btnDate = new ListBox(this);
btnDate->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
btnDate->setText("1990-06-15");
connect(btnDate, &QToolButton::clicked, [=]() {
DateSelectDialog dialog(this);
dialog.setSelectedValue(btnDate->text());
dialog.setWindowModality(Qt::WindowModal);
if (dialog.exec() == QDialog::Accepted)
{
btnDate->setText(dialog.getSelectedValue());
}
});
layout->addWidget(btnDate);
QLabel* lbl_endline5 = new QLabel(this);
lbl_endline5->setObjectName("endline");
layout->addWidget(lbl_endline5);
QLabel* lbl_comment = new QLabel(this);
lbl_comment->setText(tr("Comment"));
te_comment = new UTextEdit(this);
layout->addWidget(lbl_comment);
layout->addWidget(te_comment);
QLabel* lbl_endline6 = new QLabel(this);
lbl_endline6->setObjectName("endline");
layout->addWidget(lbl_endline6);
lbl_error = new QLabel(this);
lbl_error->setObjectName("warn");
layout->addWidget(lbl_error);
}
EditPatientDialog::~EditPatientDialog()
{
}
void EditPatientDialog::setPatientInformation(PatientInformation* information)
{
if (information)
{
le_id->setText(information->ID);
btnSex->setText(information->Sex == tr("F") ? tr("Female") : (information->Sex == tr("M") ? tr("Male") : tr("Other")));
le_name->setText(information->Name);
te_comment->setText(information->Comment);
btnDate->setText(information->BirthDate);
currentPatientUID = information->PatientUID;
AddDate = information->AddDate;
le_id->setEnabled(false);
}
}
void EditPatientDialog::clearPatientInformation()
{
le_id->setText("");
// le_date->setText("");
le_name->setText("");
te_comment->setText("");
}
void EditPatientDialog::storePatientInformation()
{
store.PatientUID = currentPatientUID;
// store.AddDate = AddDate;
store.ID = le_id->text();
store.BirthDate = le_date->text();
store.Name = le_name->text();
store.Sex = le_sex->text();
store.Comment = te_comment->toPlainText();
}
bool EditPatientDialog::updateReferenceData()
{
PatientInformation* inf = getPatientInformation();
if (le_id->text().isEmpty())
{
lbl_error->setText("ID can't be empty!");
lbl_error->setVisible(true);
return false;
}
inf->ID = le_id->text().trimmed();
if (le_name->text().isEmpty())
{
lbl_error->setText("Patient Name can't be empty!");
lbl_error->setVisible(true);
return false;
}
inf->Name = le_name->text().trimmed();
int selectedRow = 0;
bool isAdd = currentPatientUID.isEmpty();
if (isAdd)
{
int ref_rowid = queryValue(model, 1, inf->ID);
if (ref_rowid >= 0)
{
lbl_error->setText("The ID is already existed!");
lbl_error->setVisible(true);
return false;
}
inf->PatientUID = QUuid::createUuid().toString();
inf->AddDate = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
model->insertRow(0);
}
else
{
inf->PatientUID = currentPatientUID;
selectedRow = queryValue(model, 1, inf->ID);
inf->AddDate = AddDate;
}
inf->Sex = btnSex->text() == tr("Female") ? "F" : (tr("Male") == btnSex->text() ? "M" : "O");
inf->BirthDate = btnDate->text();
inf->Comment = te_comment->toPlainText();
#define ADD_PATIENT_PROPERTY(val)\
model->setData(model->index(selectedRow,PatientInformationEnum:: val),inf-> val);
EDIT_PATIENT()
#undef ADD_PATIENT_PROPERTY
if (model->submitAll())
{
return true;
}
else
{
lbl_error->setText("Submit to database error!");
lbl_error->setVisible(true);
return false;
}
}

View File

@@ -0,0 +1,55 @@
//
// Created by Krad on 2022/3/21.
//
#ifndef GUI_EDITPATIENTDIALOG_H
#define GUI_EDITPATIENTDIALOG_H
#include "dialogs/GUIFormBaseDialog.h"
#include "forms/select/PatientDetailForm.h"
class ULineEdit;
class UTextEdit;
class QLabel;
class QToolButton;
class QSqlTableModel;
class EditPatientDialog :public GUIFormBaseDialog {
Q_OBJECT
public:
explicit EditPatientDialog(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~EditPatientDialog();
void setModel(QSqlTableModel* m)
{
model = m;
}
PatientInformation* getPatientInformation()
{
return &store;
}
void setPatientInformation(PatientInformation* information);
void clearPatientInformation();
void storePatientInformation();
protected:
bool updateReferenceData() override;
private:
ULineEdit* le_id = nullptr;
ULineEdit* le_name = nullptr;
ULineEdit* le_sex = nullptr;
ULineEdit* le_date = nullptr;
UTextEdit* te_comment = nullptr;
QLabel* lbl_error = nullptr;
PatientInformation store;
QString currentPatientUID;
QString AddDate;
QToolButton* btnSex = nullptr;
QToolButton* btnDate = nullptr;
QSqlTableModel* model = nullptr;
};
#endif //GUI_EDITPATIENTDIALOG_H