Files
GUI/src/SelectFormWidget.cpp

327 lines
11 KiB
C++
Raw Normal View History

2021-10-09 16:38:34 +08:00
//
// Created by Krad on 2021/10/8.
//
#include "ui_tabformwidget.h"
#include "SelectFormWidget.h"
#include <QHBoxLayout>
#include <QToolButton>
#include <QTableWidget>
#include "components/SlideableTableView.h"
2021-10-09 16:38:34 +08:00
#include <QHeaderView>
2021-10-12 14:11:19 +08:00
#include <QUuid>
#include <QDate>
#include "db/SQLHelper.h"
2021-10-09 16:38:34 +08:00
#include "editpatientform.h"
#include "guimacros.h"
#include "event/EventCenter.h"
2021-11-11 14:11:29 +08:00
#include "AccountFormDialog.h"
2021-12-13 17:01:17 +08:00
#include <QDebug>
2021-11-19 15:36:12 +08:00
#include "log/UserOperationLog.h"
2021-12-29 14:56:48 +08:00
#include <QSortFilterProxyModel>
2022-01-12 11:24:37 +08:00
#include "src/components/VerticalTextToolButton.h"
2022-03-08 10:09:02 +08:00
#include "AlertDialog.h"
2021-10-09 16:38:34 +08:00
2022-01-25 17:07:34 +08:00
#include <QScroller>
2021-10-09 16:38:34 +08:00
SelectFormWidget::SelectFormWidget(QWidget* parent) :
TabFormWidget(parent)
2021-10-09 16:38:34 +08:00
{
2022-01-12 11:24:37 +08:00
//init command bar
QHBoxLayout* layout = new QHBoxLayout();
ui->commandWidget->setLayout(layout);
ADD_TOOL_BTN(Account, ":/icons/account.png");
ADD_TOOL_BTN(Worklist, ":/icons/setting.png");
2021-12-28 11:49:07 +08:00
btnAccount->setText(tr("Account"));
btnWorklist->setText(tr("Worklist"));
2021-12-23 14:42:50 +08:00
layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
QWidget* spacerLine = new QWidget(this);
spacerLine->setFixedWidth(2);
spacerLine->setObjectName("verSpaceLine");
layout->addWidget(spacerLine);
ADD_TOOL_BTN(Add, ":/icons/add.png");
ADD_TOOL_BTN(Edit, ":/icons/details.png");
ADD_TOOL_BTN(Delete, ":/icons/close_circle.png");
ADD_TOOL_BTN(Select, ":/icons/selected.png");
2021-12-28 11:49:07 +08:00
btnAdd->setText(tr("Add"));
btnEdit->setText(tr("Edit"));
btnDelete->setText(tr("Delete"));
btnSelect->setText(tr("Select"));
//Init content widget
QHBoxLayout* contentLayout = new QHBoxLayout();
2022-01-25 17:07:34 +08:00
contentLayout->setContentsMargins(5, 5, 0, 5);
this->ui->contentWidget->setLayout(contentLayout);
// TableView for patient
2022-03-15 16:22:58 +08:00
SlideableTableView* table = new SlideableTableView(this);
table->setAlternatingRowColors(true);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->setDefaultSectionSize(38);
table->horizontalHeader()->setStretchLastSection(true);
//data from SQLITE
2022-01-25 17:07:34 +08:00
//
//avoid pan comsumed by tableview!
table->viewport()->ungrabGesture(Qt::PanGesture);
2021-12-29 14:56:48 +08:00
auto model = SQLHelper::getTable("Patient");
model->setFilter("Flag=0");
model->sort(5, Qt::DescendingOrder);
model->select();
model->setHeaderData(1, Qt::Horizontal, "ID");
2021-12-28 11:49:07 +08:00
model->setHeaderData(2, Qt::Horizontal, tr("Name"));
model->setHeaderData(3, Qt::Horizontal, tr("Birth Date"));
model->setHeaderData(4, Qt::Horizontal, tr("Gender"));
model->setHeaderData(5, Qt::Horizontal, tr("Add Date"));
2021-12-28 18:23:02 +08:00
model->setHeaderData(6, Qt::Horizontal, tr("Comment"));
2021-12-29 14:56:48 +08:00
table->setSortingEnabled(true); // enable sortingEnabled
table->setModel((QAbstractItemModel*)model);
table->hideColumn(0);
table->hideColumn(7);
table->show();
table->setColumnWidth(1, 250);
table->setColumnWidth(2, 250);
2022-03-14 18:07:28 +08:00
table->setColumnWidth(3, 160);
table->setColumnWidth(4, 120);
table->setColumnWidth(5, 250);
contentLayout->addWidget(table);
QWidget* spacerLine2 = new QWidget(this);
spacerLine2->setFixedWidth(2);
spacerLine2->setObjectName("verSpaceLine");
contentLayout->addWidget(spacerLine2);
2022-03-15 16:22:58 +08:00
// prepare edit panel
EditPatientForm* edit_patient = new EditPatientForm(this);
edit_patient->setObjectName("edit_patient");
2022-01-25 17:07:34 +08:00
edit_patient->hide();
contentLayout->addWidget(edit_patient);
2022-01-25 17:07:34 +08:00
auto* btnShowEdit = new VerticalTextToolButton(this);
btnShowEdit->setObjectName("showeditBtn");
btnShowEdit->setIcon(QIcon(":/icons/edit.png"));
btnShowEdit->setIconSize(QSize(30, 30));
btnShowEdit->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btnShowEdit->setFixedHeight(225);
btnShowEdit->setVerticalText("Patient Detail");
contentLayout->addWidget(btnShowEdit);
contentLayout->setAlignment(btnShowEdit, Qt::AlignmentFlag::AlignTop);
2022-03-15 16:22:58 +08:00
// btn show slot
2022-01-25 17:07:34 +08:00
connect(btnShowEdit, &QToolButton::clicked, [=]() {
edit_patient->show();
btnShowEdit->hide();
});
2022-03-15 16:22:58 +08:00
//btn hide slot
2022-01-25 17:07:34 +08:00
connect(edit_patient, &EditPatientForm::hideBtnClicked, [=]() {
edit_patient->hide();
btnShowEdit->show();
});
2022-01-12 11:24:37 +08:00
2022-01-25 17:07:34 +08:00
//select default row 0
if (model->rowCount() > 0)
{
table->selectRow(0);
PatientInformation pat;
#define ADD_PATIENT_PROPERTY(val)\
2022-03-15 16:22:58 +08:00
pat. val = model->data(model->index(table->currentIndex().row(),PatientInformationEnum:: val)).toString();
EDIT_PATIENT()
#undef ADD_PATIENT_PROPERTY
2022-01-25 17:07:34 +08:00
edit_patient->setPatientInformation(&pat);
}
//events----------------------------------------------------------------------
2022-03-15 16:22:58 +08:00
//prepare button state
auto prepareButtons = [=](bool disableALL){
bool state_flag = (table->currentIndex().row()>=0);
btnSelect->setEnabled(state_flag && !disableALL);
btnDelete->setEnabled(state_flag && !disableALL);
btnEdit->setEnabled(state_flag && !disableALL);
btnAdd->setEnabled(!disableALL);
};
//table current row selection changing event
2022-03-15 16:22:58 +08:00
connect(table, &SlideableTableView::currentRowChanged, [=](int row) {
PatientInformation pat;
#define ADD_PATIENT_PROPERTY(val)\
2022-03-15 16:22:58 +08:00
pat. val = model->data(model->index(row,PatientInformationEnum:: val)).toString();
EDIT_PATIENT()
#undef ADD_PATIENT_PROPERTY
edit_patient->setPatientInformation(&pat);
2022-03-15 16:22:58 +08:00
prepareButtons(false);
});
2022-03-15 16:22:58 +08:00
// after sort by column
connect(table->horizontalHeader(),&QHeaderView::sectionClicked,[=](int index){
edit_patient->clearPatientInformation();
prepareButtons(false);
});
// btn add slot
connect(btnAdd, &QToolButton::clicked, [=]() {
EditPatientDialog dialog(this);
dialog.clearPatientInformation();
dialog.setWindowModality(Qt::WindowModal);
dialog.setModel(model);
// accept change
if (dialog.exec() == QDialog::Accepted) {
table->selectRow(0);
model->selectRow(0);
}
LOG_USER_OPERATION(AddPatient);
btnSelect->setEnabled(true);
});
2022-03-15 16:22:58 +08:00
// btn edit slot
connect(btnEdit, &QToolButton::clicked, [=]() {
EditPatientDialog dialog(this);
dialog.setPatientInformation(edit_patient->getPatientInformation());
dialog.setWindowModality(Qt::WindowModal);
dialog.setModel(model);
// accept change
if (dialog.exec() == QDialog::Accepted) {
table->clicked(table->currentIndex());
LOG_USER_OPERATION(AddPatient);
btnSelect->setEnabled(true);
}
});
2022-03-15 16:22:58 +08:00
// btn add slot
connect(edit_patient, &EditPatientForm::editCancel, [=]() {
2022-03-15 16:22:58 +08:00
if (table->currentIndex().row()<0) return;
btnSelect->setEnabled(true);
2022-03-15 16:22:58 +08:00
});
// btn add slot
connect(edit_patient, &EditPatientForm::editAccept, [=](PatientInformation* inf, bool& result) {
2022-03-15 16:22:58 +08:00
int selectedRow = table->currentIndex().row();
bool isAdd = inf->PatientUID.isEmpty();
if (isAdd) {
selectedRow = model->rowCount();
inf->PatientUID = QUuid::createUuid().toString();
inf->AddDate = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
model->insertRow(0);
selectedRow = 0;
}
qDebug() << inf->PatientUID << inf->AddDate;
#define ADD_PATIENT_PROPERTY(val)\
2021-10-12 15:07:06 +08:00
model->setData(model->index(selectedRow,PatientInformationEnum:: val),inf-> val);
EDIT_PATIENT()
#undef ADD_PATIENT_PROPERTY
if (model->submitAll())
{
table->selectRow(selectedRow);
model->selectRow(selectedRow);
}
else {
//TODO:add some error handle logic
}
if (isAdd) {
LOG_USER_OPERATION(AddPatient);
}
else
{
LOG_USER_OPERATION(ChangePatientInfo);
}
btnSelect->setEnabled(true);
});
2022-03-15 16:22:58 +08:00
// btn delete slot
connect(btnDelete, &QToolButton::clicked, [=]() {
2022-03-15 16:22:58 +08:00
if (table->currentIndex().row()<0) return;
2022-03-08 10:09:02 +08:00
AlertDialog dialog(this);
2022-03-15 16:22:58 +08:00
dialog.setWindowModality(Qt::WindowModal);
QString pUid = model->index(table->currentIndex().row(), PatientInformationEnum::PatientUID).data().toString();
// patient has been selected as the scan patient!
if (selectedPatientUID == pUid){
dialog.setButtonMode(OkOnly);
dialog.setTitle(tr("Alert"));
dialog.setAlertMessage(QString(tr("Can't delete selected Patient !")));
dialog.exec();
return;
}
// not the selected one, confirm!
2022-03-08 10:09:02 +08:00
dialog.setButtonMode(OkAndCancel);
dialog.setTitle("Confirm");
2022-03-15 16:22:58 +08:00
QString pat_name = model->index(table->currentIndex().row(), PatientInformationEnum::Name).data().toString();
2022-03-08 10:09:02 +08:00
dialog.setAlertMessage(QString(tr("Delete Patient \"%1\" ?")).arg(pat_name));
2022-03-15 16:22:58 +08:00
if (dialog.exec() != QDialog::Accepted) return;
// need delete clear edit panel detail
edit_patient->clearPatientInformation();
model->setData(model->index(table->currentIndex().row(), PatientInformationEnum::Flag), 9);
if (model->submitAll()) {
model->select();
if (model->rowCount() > 0) {
table->selectRow(0);
model->selectRow(0);
PatientInformation pat;
#define ADD_PATIENT_PROPERTY(val)\
2021-11-19 15:36:12 +08:00
pat. val = model->data(model->index(0,PatientInformationEnum:: val)).toString();
2022-03-15 16:22:58 +08:00
EDIT_PATIENT()
#undef ADD_PATIENT_PROPERTY
2022-03-15 16:22:58 +08:00
edit_patient->setPatientInformation(&pat);
LOG_USER_OPERATION(DeletePatient);
}
} else {
//TODO:error handle
}
prepareButtons(false);
2022-03-15 16:22:58 +08:00
});
2022-03-15 16:22:58 +08:00
// btn select slot
connect(btnSelect, &QToolButton::clicked, [=]() {
2022-03-15 16:22:58 +08:00
EventCenter::Default()->triggerEvent(GUIEvents::PatientSelected, nullptr, edit_patient->getPatientInformation()->Copy());
2022-03-15 16:22:58 +08:00
selectedPatientUID = edit_patient->getPatientInformation()->PatientUID;
LOG_USER_OPERATION(SelectPatient);
});
2022-03-15 16:22:58 +08:00
// btn account slot
connect(btnAccount, &QToolButton::clicked, [=]() {
AccountFormDialog dia(this);
dia.setWindowModality(Qt::WindowModal);
dia.exec();
});
2022-03-15 16:22:58 +08:00
// event ResponsePreview slot
connect(EventCenter::Default(), &EventCenter::ResponsePreview, [=](QObject* sender, QObject* data) {
2022-03-15 16:22:58 +08:00
prepareButtons(true);
});
2022-03-15 16:22:58 +08:00
// event ResponseStop slot
connect(EventCenter::Default(), &EventCenter::ResponseStop, [=](QObject* sender, QObject* data) {
2022-03-15 16:22:58 +08:00
prepareButtons(false);
});
// event ReloadLanguage slot;
2021-12-28 18:23:02 +08:00
connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {
2021-10-26 13:08:11 +08:00
2021-12-28 18:23:02 +08:00
model->setHeaderData(1, Qt::Horizontal, "ID");
model->setHeaderData(2, Qt::Horizontal, tr("Name"));
model->setHeaderData(3, Qt::Horizontal, tr("Birth Date"));
model->setHeaderData(4, Qt::Horizontal, tr("Gender"));
model->setHeaderData(5, Qt::Horizontal, tr("Add Date"));
model->setHeaderData(6, Qt::Horizontal, tr("Comment"));
2021-12-23 10:09:35 +08:00
2021-12-28 18:23:02 +08:00
btnAccount->setText(tr("Account"));
//btnWorklist->setText(tr("Worklist"));
btnAdd->setText(tr("Add"));
btnEdit->setText(tr("Edit"));
btnDelete->setText(tr("Delete"));
btnSelect->setText(tr("Select"));
});
2021-12-23 10:09:35 +08:00
2022-03-15 16:22:58 +08:00
//first prepare buttons!
prepareButtons(false);
2021-10-09 16:38:34 +08:00
}
2021-10-09 16:38:34 +08:00
SelectFormWidget::~SelectFormWidget()
{
2021-12-13 17:01:17 +08:00
}
2022-03-15 16:22:58 +08:00