Skip to content

Commit

Permalink
ENH: Select last inserted patient when importing DICOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Punzo committed May 14, 2024
1 parent 0be20af commit f37bd3b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 18 deletions.
57 changes: 57 additions & 0 deletions Libs/DICOM/Core/ctkDICOMDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,63 @@ QString ctkDICOMDatabase::displayedNameForPatient(const QString patientUID)
return result;
}

//------------------------------------------------------------------------------
QDateTime ctkDICOMDatabase::insertDateTimeForPatient(const QString patientUID)
{
Q_D(ctkDICOMDatabase);
QDateTime result;
QSqlQuery query(d->Database);
query.prepare("SELECT InsertTimestamp FROM Patients WHERE UID=?");
query.addBindValue(patientUID);
if (!d->loggedExec(query))
{
return result;
}
if (query.next())
{
result = QDateTime::fromString(query.value(0).toString(), Qt::ISODate);
}
return result;
}

//------------------------------------------------------------------------------
QDateTime ctkDICOMDatabase::insertDateTimeForStudy(const QString studyInstanceUID)
{
Q_D(ctkDICOMDatabase);
QDateTime result;
QSqlQuery query(d->Database);
query.prepare("SELECT InsertTimestamp FROM Studies WHERE StudyInstanceUID=?");
query.addBindValue(studyInstanceUID);
if (!d->loggedExec(query))
{
return result;
}
if (query.next())
{
result = QDateTime::fromString(query.value(0).toString(), Qt::ISODate);
}
return result;
}

//------------------------------------------------------------------------------
QDateTime ctkDICOMDatabase::insertDateTimeForSeries(const QString seriesInstanceUID)
{
Q_D(ctkDICOMDatabase);
QDateTime result;
QSqlQuery query(d->Database);
query.prepare("SELECT InsertTimestamp FROM Series WHERE SeriesInstanceUID=?");
query.addBindValue(seriesInstanceUID);
if (!d->loggedExec(query))
{
return result;
}
if (query.next())
{
result = QDateTime::fromString(query.value(0).toString(), Qt::ISODate);
}
return result;
}

//------------------------------------------------------------------------------
QString ctkDICOMDatabase::fieldForPatient(const QString field, const QString patientUID)
{
Expand Down
3 changes: 3 additions & 0 deletions Libs/DICOM/Core/ctkDICOMDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ class CTK_DICOM_CORE_EXPORT ctkDICOMDatabase : public QObject
Q_INVOKABLE QString descriptionForStudy(const QString studyUID);
Q_INVOKABLE QString nameForPatient(const QString patientUID);
Q_INVOKABLE QString displayedNameForPatient(const QString patientUID);
Q_INVOKABLE QDateTime insertDateTimeForPatient(const QString patientUID);
Q_INVOKABLE QDateTime insertDateTimeForStudy(const QString studyInstanceUID);
Q_INVOKABLE QDateTime insertDateTimeForSeries(const QString seriesInstanceUID);
Q_INVOKABLE QString fieldForPatient(const QString field, const QString patientUID);
Q_INVOKABLE QString fieldForStudy(const QString field, const QString studyInstanceUID);
Q_INVOKABLE QString fieldForSeries(const QString field, const QString seriesInstanceUID);
Expand Down
55 changes: 37 additions & 18 deletions Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ctkDICOMVisualBrowserWidgetPrivate : public Ui_ctkDICOMVisualBrowserWidget
void retrieveSeries();
bool updateServer(ctkDICOMServer* server);
void removeAllPatientItemWidgets();
int isPatientTabAlreadyAdded(const QString& patientItem);
int findPatientTabIndexFromPatientItem(const QString& patientItem);
void updateSeriesTablesSelection(ctkDICOMSeriesItemWidget* selectedSeriesItemWidget);
QStringList getPatientItemsFromWidgets(ctkDICOMModel::IndexType level,
QList<QWidget*> selectedWidgets);
Expand Down Expand Up @@ -689,29 +689,17 @@ void ctkDICOMVisualBrowserWidgetPrivate::createPatients(QString selectedPatientI
return;
}

// Save current patient selection
if (selectedPatientID.isEmpty() && selectedPatientName.isEmpty() &&
this->PatientsTabWidget->count() != 0)
{
ctkDICOMPatientItemWidget* currentPatientItemWidget =
qobject_cast<ctkDICOMPatientItemWidget*>(this->PatientsTabWidget->currentWidget());
if (currentPatientItemWidget)
{
selectedPatientID = currentPatientItemWidget->patientID();
selectedPatientName = currentPatientItemWidget->patientName();
}
}

this->patientsTabMenuToolButton->show();
this->IsGUIUpdating = true;
int wasBlocking = this->PatientsTabWidget->blockSignals(true);
int selectedIndex = -1;
QMap<QString, QDateTime> patientsInsertDateTimeList;
foreach (QString patientItem, patientList)
{
QString patientID = this->DicomDatabase->fieldForPatient("PatientID", patientItem);
QString patientName = this->DicomDatabase->fieldForPatient("PatientsName", patientItem);
patientName.replace(R"(^)", R"( )");
int index = this->isPatientTabAlreadyAdded(patientItem);
int index = this->findPatientTabIndexFromPatientItem(patientItem);
if (index != -1)
{
if (selectedPatientID == patientID &&
Expand All @@ -737,9 +725,29 @@ void ctkDICOMVisualBrowserWidgetPrivate::createPatients(QString selectedPatientI
{
selectedIndex = index;
}

QDateTime patientInsertDateTime = this->DicomDatabase->insertDateTimeForPatient(patientItem);
patientsInsertDateTimeList[patientItem] = patientInsertDateTime;
}

this->PatientsTabWidget->setCurrentIndex(selectedIndex != -1 ? selectedIndex : 0);
if (selectedIndex == -1 && patientsInsertDateTimeList.count() > 0)
{
QList<QPair<QString, QDateTime>> list;
for (QMap<QString, QDateTime>::const_iterator it = patientsInsertDateTimeList.cbegin();
it != patientsInsertDateTimeList.cend(); ++it)
{
list.append(qMakePair(it.key(), it.value()));
}

std::sort(list.begin(), list.end(), [](const QPair<QString, QDateTime>& a, const QPair<QString, QDateTime>& b)
{
return a.second < b.second;
});

QString mostRecentPatientItem = list.last().first;
selectedIndex = this->findPatientTabIndexFromPatientItem(mostRecentPatientItem);
}
this->PatientsTabWidget->setCurrentIndex(selectedIndex);
this->PatientsTabWidget->blockSignals(wasBlocking);

ctkDICOMPatientItemWidget* currentPatientItemWidget =
Expand Down Expand Up @@ -1091,7 +1099,7 @@ void ctkDICOMVisualBrowserWidgetPrivate::removeAllPatientItemWidgets()
}

//----------------------------------------------------------------------------
int ctkDICOMVisualBrowserWidgetPrivate::isPatientTabAlreadyAdded(const QString& patientItem)
int ctkDICOMVisualBrowserWidgetPrivate::findPatientTabIndexFromPatientItem(const QString& patientItem)
{
int patientIndex = -1;
for (int index = 0; index < this->PatientsTabWidget->count(); ++index)
Expand Down Expand Up @@ -2768,7 +2776,18 @@ void ctkDICOMVisualBrowserWidget::updateGUIFromScheduler(const QVariant& data)
d->WarningPushButton->show();
}

d->createPatients();
// Save current patient selection
QString patientID;
QString patientName;
ctkDICOMPatientItemWidget* currentPatientItemWidget =
qobject_cast<ctkDICOMPatientItemWidget*>(d->PatientsTabWidget->currentWidget());
if (currentPatientItemWidget)
{
patientID = currentPatientItemWidget->patientID();
patientName = currentPatientItemWidget->patientName();
}

d->createPatients(patientID, patientName);
}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit f37bd3b

Please sign in to comment.