From d03de1009da375da1423464891c9ba9219003c8f Mon Sep 17 00:00:00 2001 From: Krad Date: Tue, 12 Oct 2021 10:28:30 +0800 Subject: [PATCH] Using SQLHelper to retrieve data to dataView, add simple control to edit patient panel. --- src/SelectFormWidget.cpp | 68 +++++++++++++++++++++++++--------------- src/SelectFormWidget.h | 2 ++ src/editpatientform.cpp | 61 ++++++++++++++++++++++++++++++++--- src/editpatientform.h | 35 ++++++++++++++++++++- 4 files changed, 135 insertions(+), 31 deletions(-) diff --git a/src/SelectFormWidget.cpp b/src/SelectFormWidget.cpp index 58150ff..8403d86 100644 --- a/src/SelectFormWidget.cpp +++ b/src/SelectFormWidget.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "db/SQLHelper.h" #include "editpatientform.h" #include "guimacros.h" @@ -24,11 +25,12 @@ SelectFormWidget::SelectFormWidget(QWidget *parent) : "QHeaderView::section:horizontal{border-bottom: 1px solid rgb(0,170,255);}" "QHeaderView::section:vertical{min-height:36px;max-height:36px;}" "QWidget#edit_patient{min-width:300px;max-width:300px;}" -// "QTableWidget{border:1px solid #323232}" "QTableWidget{border:none}" "QTableView{alternate-background-color: #595959;selection-color:white;selection-background-color:#0078d8}" ; + this->setStyleSheet(this->styleSheet().append(style)); + //init command bar QHBoxLayout* layout =new QHBoxLayout(); ui->commandWidget->setLayout(layout); ADD_TOOL_BTN(Account,":/icons/account.png"); @@ -42,46 +44,60 @@ SelectFormWidget::SelectFormWidget(QWidget *parent) : ADD_TOOL_BTN(Edit,":/icons/details.png"); ADD_TOOL_BTN(Delete,":/icons/close_circle.png"); ADD_TOOL_BTN(Select,":/icons/selected.png"); + + //Init content widget QHBoxLayout* contentLayout =new QHBoxLayout(); this->ui->contentWidget->setLayout(contentLayout); - QStringList header; - header<<"ID"<<"Name"<<"Sex"<<"DOB"<<"Status"<<"Comment"; - QTableWidget* table= new QTableWidget(this); + // TableView for patient + QTableView* table= new QTableView(this); table->setAlternatingRowColors(true); table->setSelectionMode(QAbstractItemView::SingleSelection); + table->setEditTriggers(QAbstractItemView::NoEditTriggers); table->setSelectionBehavior(QAbstractItemView::SelectRows); - table->setColumnCount(6); table->verticalHeader()->setDefaultSectionSize(38); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); - table->setHorizontalHeaderLabels(header); - table->setRowCount(3); - QTableWidgetItem* item ; - ADD_CENTER_ITEM(0,0,"Pat010012313"); - ADD_CENTER_ITEM(0,1,"XXX"); - ADD_CENTER_ITEM(0,2,"Female"); - ADD_CENTER_ITEM(0,3,"1978/09/06"); - ADD_CENTER_ITEM(0,4,"Added"); - ADD_CENTER_ITEM(0,5,""); - ADD_CENTER_ITEM(1,0,"Pat0222222"); - ADD_CENTER_ITEM(1,1,"XXX2"); - ADD_CENTER_ITEM(1,2,"Female"); - ADD_CENTER_ITEM(1,3,"1993/08/16"); - ADD_CENTER_ITEM(1,4,"Scanned"); - ADD_CENTER_ITEM(1,5,""); - ADD_CENTER_ITEM(2,0,"Pat3"); - ADD_CENTER_ITEM(2,1,"XX3"); - ADD_CENTER_ITEM(2,2,"Female"); - ADD_CENTER_ITEM(2,3,"1999/08/16"); - ADD_CENTER_ITEM(2,4,"Scanned"); - ADD_CENTER_ITEM(2,5,""); + //dat from SQLITE + SQLHelper::Open(); + auto model = SQLHelper::getTable("Patient"); + model->select(); + model->setHeaderData(1,Qt::Horizontal,"ID"); + model->setHeaderData(2,Qt::Horizontal,"Name"); + model->setHeaderData(3,Qt::Horizontal,"Birth Date"); + table->setModel((QAbstractItemModel*)model); + table->hideColumn(0); + table->show(); contentLayout->addWidget(table); QWidget* spacerLine2= new QWidget(this); spacerLine2->setFixedWidth(2); spacerLine2->setObjectName("verSpaceLine"); contentLayout->addWidget(spacerLine2); + //edit panel EditPatientForm* edit_patient= new EditPatientForm(this); edit_patient->setObjectName("edit_patient"); contentLayout->addWidget(edit_patient); + + //events---------------------------------------------------------------------- + //table current row selection changing event + connect(table,&QTableView::clicked,[=](const QModelIndex & modelIndex){ + if (currentRow!=modelIndex.row()) + { + PatientInformation pat; +#define ADD_PATIENT_PROPERTY(val)\ + pat. val = model->data(model->index(modelIndex.row(),PatientInformationEnum:: val)).toString(); + EDIT_PATIENT() +#undef ADD_PATIENT_PROPERTY + edit_patient->setPatientInformation(&pat); + } + }); + + connect(btnAdd, &QToolButton::clicked,[=](){ + edit_patient->clearPatientInformation(); + edit_patient->setEditEnable(true); + }); + connect(btnEdit, &QToolButton::clicked,[=](){ + table->clicked(table->currentIndex()); + edit_patient->setEditEnable(true); + }); } SelectFormWidget::~SelectFormWidget() diff --git a/src/SelectFormWidget.h b/src/SelectFormWidget.h index 8df261d..4fc12cd 100644 --- a/src/SelectFormWidget.h +++ b/src/SelectFormWidget.h @@ -12,6 +12,8 @@ public: explicit SelectFormWidget(QWidget *parent = nullptr); ~SelectFormWidget(); +private: + int currentRow = -1; }; diff --git a/src/editpatientform.cpp b/src/editpatientform.cpp index 8781c12..d8d99b4 100644 --- a/src/editpatientform.cpp +++ b/src/editpatientform.cpp @@ -16,15 +16,68 @@ EditPatientForm::EditPatientForm(QWidget *parent) : this->ui->editcmdWidget->setLayout(layout); ADD_TOOL_BTN(Cancel,":/icons/close_circle.png"); ADD_TOOL_BTN(Accpet,":/icons/selected.png"); - btnCancel->setEnabled(false); + btnCancel->setEnabled(editEnable); btnCancel->setToolButtonStyle(Qt::ToolButtonIconOnly); - btnCancel->setIcon(QIcon(":/icons/close_circle_d.png")); - btnAccpet->setEnabled(false); + btnCancel->setIcon(QIcon(editEnable?":/icons/close_circle.png":":/icons/close_circle_d.png")); + btnAccpet->setEnabled(editEnable); btnAccpet->setToolButtonStyle(Qt::ToolButtonIconOnly); - btnAccpet->setIcon(QIcon(":/icons/selected_d.png")); + btnAccpet->setIcon(QIcon(editEnable?":/icons/selected.png":":/icons/selected_d.png")); + btnEditAccept = btnAccpet; + btnEditCancel = btnCancel; + connect(btnEditCancel,&QToolButton::clicked,[=](){ + clearPatientInformation(); + this->setEditEnable(false); + restorePatientInformation(); + }); } EditPatientForm::~EditPatientForm() { delete ui; } + +void EditPatientForm::setPatientInformation(PatientInformation *information) { + if (information) + { + ui->tbx_ID->setText(information->ID); + ui->tbx_Dob->setDate(QDate::fromString(information->BirthDate,"yyyy-MM-dd")); + ui->tbx_Name->setText(information->Name=="F"?"Female":(information->Name=="M"?"Male":"Other")); + currentPatientUID = information->PatientUID; + storePatientInformation(); + } +} + +void EditPatientForm::clearPatientInformation() { + ui->tbx_ID->setText(""); + ui->tbx_Dob->setDate(QDate::currentDate()); + ui->tbx_Name->setText(""); + currentPatientUID = ""; +} + +void EditPatientForm::setEditEnable(bool enable) { + ui->tbx_ID->setEnabled(enable); + ui->tbx_Dob->setEnabled(enable); + ui->tbx_Name->setEnabled(enable); + ui->cb_Sex->setEnabled(enable); + ui->rtbx_Comment->setEnabled(enable); + btnEditAccept->setEnabled(enable); + btnEditCancel->setEnabled(enable); + btnEditCancel->setIcon(QIcon(enable?":/icons/close_circle.png":":/icons/close_circle_d.png")); + btnEditAccept->setIcon(QIcon(enable?":/icons/selected.png":":/icons/selected_d.png")); + editEnable = enable; +// ui->->setEnabled(enable); +} + +void EditPatientForm::storePatientInformation() { + store.PatientUID =currentPatientUID; + store.ID = ui->tbx_ID->text(); + store.BirthDate = ui->tbx_Dob->date().toString("yyyy-MM-dd"); + store.Name = ui->tbx_Name->text(); +} + +void EditPatientForm::restorePatientInformation() { + currentPatientUID = store.PatientUID; + ui->tbx_ID->setText(store.ID); + ui->tbx_Dob->setDate(QDate::fromString(store.BirthDate,"yyyy-MM-dd")); + ui->tbx_Name->setText(store.Name); +} diff --git a/src/editpatientform.h b/src/editpatientform.h index b81e81c..1775a9c 100644 --- a/src/editpatientform.h +++ b/src/editpatientform.h @@ -6,7 +6,31 @@ namespace Ui { class EditPatientForm; } +#define EDIT_PATIENT()\ +ADD_PATIENT_PROPERTY(PatientUID)\ +ADD_PATIENT_PROPERTY(ID)\ +ADD_PATIENT_PROPERTY(Name)\ +ADD_PATIENT_PROPERTY(BirthDate)\ +ADD_PATIENT_PROPERTY(Sex)\ +ADD_PATIENT_PROPERTY(Comment)\ + + + +enum PatientInformationEnum{ +#define ADD_PATIENT_PROPERTY(val) val, + EDIT_PATIENT() +#undef ADD_PATIENT_PROPERTY +}; + +class PatientInformation{ +public: +#define ADD_PATIENT_PROPERTY(val) QString val; + EDIT_PATIENT(); +#undef ADD_PATIENT_PROPERTY +}; + +class QToolButton; class EditPatientForm : public QWidget { Q_OBJECT @@ -14,9 +38,18 @@ class EditPatientForm : public QWidget public: explicit EditPatientForm(QWidget *parent = nullptr); ~EditPatientForm(); - + void setPatientInformation(PatientInformation * information); + void clearPatientInformation(); + void storePatientInformation(); + void restorePatientInformation(); + void setEditEnable(bool enable); private: Ui::EditPatientForm *ui; + QString currentPatientUID; + PatientInformation store; + bool editEnable=false; + QToolButton* btnEditAccept; + QToolButton* btnEditCancel; }; #endif // EDITPATIENTFORM_H