// // Created by Krad on 2021/10/8. // #include "ui_tabformwidget.h" #include "SelectFormWidget.h" #include #include #include #include #include #include #include #include #include "components/SlideTableView.h" #include "db/SQLHelper.h" #include "guimacros.h" #include "event/EventCenter.h" #include "dialogs/AccountFormDialog.h" #include "dialogs/AlertDialog.h" #include "log/UserOperationLog.h" #include "components/VerticalTextToolButton.h" SelectFormWidget::SelectFormWidget(QWidget* parent) : TabFormWidget(parent) , mBtnAccount(new QToolButton(this)) , mBtnWorklist(new QToolButton(this)) , mBtnAdd(new QToolButton(this)) , mBtnEdit(new QToolButton(this)) , mBtnDelete(new QToolButton(this)) , mBtnSelect(new QToolButton(this)) { //init command bar auto layout = new QHBoxLayout(); ui->commandWidget->setLayout(layout); INIT_TOOL_BTN(Account, ":/icons/account.png") INIT_TOOL_BTN(Worklist, ":/icons/setting.png") mBtnAccount->setText(tr("Account")); mBtnWorklist->setText(tr("Worklist")); layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); addVerticalLine(layout); INIT_TOOL_BTN(Add, ":/icons/add.png") INIT_TOOL_BTN(Edit, ":/icons/details.png") INIT_TOOL_BTN(Delete, ":/icons/close_circle.png") INIT_TOOL_BTN(Select, ":/icons/selected.png") mBtnAdd->setText(tr("Add")); mBtnEdit->setText(tr("Edit")); mBtnDelete->setText(tr("Delete")); mBtnSelect->setText(tr("Select")); //Init content widget auto* contentLayout = new QHBoxLayout(); contentLayout->setContentsMargins(5, 5, 0, 5); this->ui->contentWidget->setLayout(contentLayout); // TableView for patient auto table = new SlideTableView(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 // //avoid pan comsumed by tableview! table->viewport()->ungrabGesture(Qt::PanGesture); auto model = SQLHelper::getTable("Patient"); model->setFilter("Flag=0"); model->sort(5, Qt::DescendingOrder); model->select(); 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")); 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); table->setColumnWidth(3, 160); table->setColumnWidth(4, 120); table->setColumnWidth(5, 250); contentLayout->addWidget(table); addVerticalLine(layout); // prepare edit panel auto edit_patient = new EditPatientForm(this); edit_patient->setObjectName("edit_patient"); edit_patient->hide(); contentLayout->addWidget(edit_patient); 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); // btn show slot connect(btnShowEdit, &QToolButton::clicked, [=]() { edit_patient->show(); btnShowEdit->hide(); }); //btn hide slot connect(edit_patient, &EditPatientForm::hideBtnClicked, [=]() { edit_patient->hide(); btnShowEdit->show(); }); //select default row 0 if (model->rowCount() > 0) { table->selectRow(0); setPatientDetail(table, model, edit_patient); } //events---------------------------------------------------------------------- //prepare button state auto prepareButtons = [=](bool disableALL){ bool state_flag = (table->currentIndex().row()>=0); mBtnSelect->setEnabled(state_flag && !disableALL); mBtnDelete->setEnabled(state_flag && !disableALL); mBtnEdit->setEnabled(state_flag && !disableALL); mBtnAdd->setEnabled(!disableALL); }; //table current row selection changing event connect(table, &SlideTableView::currentRowChanged, [=](int row) { setPatientDetail(table, model, edit_patient); prepareButtons(false); }); // after sort by column connect(table->horizontalHeader(),&QHeaderView::sectionClicked,[=](int index){ edit_patient->clearPatientInformation(); prepareButtons(false); if(model->rowCount()>0){ table->selectRow(0); model->selectRow(0); } }); // btn add slot connect(mBtnAdd, &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) mBtnSelect->setEnabled(true); } }); // mBtn edit slot connect(mBtnEdit, &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()); setPatientDetail(table, model, edit_patient); LOG_USER_OPERATION(AddPatient) mBtnSelect->setEnabled(true); } }); // mBtn add slot connect(edit_patient, &EditPatientForm::editCancel, [=]() { if (table->currentIndex().row()<0) return; mBtnSelect->setEnabled(true); }); // EditPatientForm editAccept slot connect(edit_patient, &EditPatientForm::editAccept, [=](PatientInformation* inf, bool& result) { 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)\ 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) } mBtnSelect->setEnabled(true); }); // btn delete slot connect(mBtnDelete, &QToolButton::clicked, [=]() { if (table->currentIndex().row()<0) return; AlertDialog dialog(this); 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! dialog.setButtonMode(OkAndCancel); dialog.setTitle("Confirm"); QString pat_name = model->index(table->currentIndex().row(), PatientInformationEnum::Name).data().toString(); dialog.setAlertMessage(QString(tr("Delete Patient \"%1\" ?")).arg(pat_name)); 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); setPatientDetail(table, model, edit_patient); LOG_USER_OPERATION(DeletePatient) } } else { //TODO:error handle dialog.setButtonMode(OkOnly); dialog.setTitle(tr("Alert")); dialog.setAlertMessage(QString(tr("Can't delete selected Patient , db submit error!"))); dialog.exec(); } prepareButtons(false); }); // mBtn select slot connect(mBtnSelect, &QToolButton::clicked, [=]() { EventCenter::Default()->triggerEvent(GUIEvents::PatientSelected, nullptr, edit_patient->getPatientInformation()->Copy()); selectedPatientUID = edit_patient->getPatientInformation()->PatientUID; LOG_USER_OPERATION(SelectPatient) }); // mBtn account slot connect(mBtnAccount, &QToolButton::clicked, [=]() { AccountFormDialog dia(this); dia.setWindowModality(Qt::WindowModal); dia.exec(); }); // event ResponsePreview slot connect(EventCenter::Default(), &EventCenter::ResponsePreview, [=](QObject* sender, QObject* data) { prepareButtons(true); }); // event ResponseStop slot connect(EventCenter::Default(), &EventCenter::ResponseStop, [=](QObject* sender, QObject* data) { prepareButtons(false); }); // event ReloadLanguage slot; connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() { 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")); mBtnAccount->setText(tr("Account")); //mBtnWorklist->setText(tr("Worklist")); mBtnAdd->setText(tr("Add")); mBtnEdit->setText(tr("Edit")); mBtnDelete->setText(tr("Delete")); mBtnSelect->setText(tr("Select")); }); //first prepare buttons! prepareButtons(false); } void SelectFormWidget::setPatientDetail(const SlideTableView *table, const QSqlTableModel *model, EditPatientForm *edit_patient) const { PatientInformation pat; #define ADD_PATIENT_PROPERTY(val)\ pat. val = model->data(model->index(table->currentIndex().row(),PatientInformationEnum:: val),Qt::EditRole).toString(); EDIT_PATIENT() #undef ADD_PATIENT_PROPERTY edit_patient->setPatientInformation(&pat); }