ctkDICOMBrowser.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // std includes
  15. #include <iostream>
  16. // Qt includes
  17. #include <QAction>
  18. #include <QCoreApplication>
  19. #include <QCheckBox>
  20. #include <QDebug>
  21. #include <QMessageBox>
  22. #include <QProgressDialog>
  23. #include <QSettings>
  24. // ctkWidgets includes
  25. #include "ctkDirectoryButton.h"
  26. #include "ctkFileDialog.h"
  27. // ctkDICOMCore includes
  28. #include "ctkDICOMDatabase.h"
  29. #include "ctkDICOMIndexer.h"
  30. // ctkDICOMWidgets includes
  31. #include "ctkDICOMBrowser.h"
  32. #include "ctkDICOMQueryResultsTabWidget.h"
  33. #include "ctkDICOMQueryRetrieveWidget.h"
  34. #include "ctkDICOMQueryWidget.h"
  35. #include "ctkDICOMTableManager.h"
  36. #include "ui_ctkDICOMBrowser.h"
  37. //logger
  38. #include <ctkLogger.h>
  39. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMBrowser");
  40. //----------------------------------------------------------------------------
  41. class ctkDICOMBrowserPrivate: public Ui_ctkDICOMBrowser
  42. {
  43. public:
  44. ctkDICOMBrowser* const q_ptr;
  45. Q_DECLARE_PUBLIC(ctkDICOMBrowser);
  46. ctkDICOMBrowserPrivate(ctkDICOMBrowser* );
  47. ~ctkDICOMBrowserPrivate();
  48. ctkFileDialog* ImportDialog;
  49. ctkDICOMQueryRetrieveWidget* QueryRetrieveWidget;
  50. QSharedPointer<ctkDICOMDatabase> DICOMDatabase;
  51. QSharedPointer<ctkDICOMIndexer> DICOMIndexer;
  52. QProgressDialog *IndexerProgress;
  53. QProgressDialog *UpdateSchemaProgress;
  54. void showIndexerDialog();
  55. void showUpdateSchemaDialog();
  56. // used when suspending the ctkDICOMModel
  57. QSqlDatabase EmptyDatabase;
  58. // local count variables to keep track of the number of items
  59. // added to the database during an import operation
  60. bool DisplayImportSummary;
  61. int PatientsAddedDuringImport;
  62. int StudiesAddedDuringImport;
  63. int SeriesAddedDuringImport;
  64. int InstancesAddedDuringImport;
  65. };
  66. //----------------------------------------------------------------------------
  67. // ctkDICOMBrowserPrivate methods
  68. ctkDICOMBrowserPrivate::ctkDICOMBrowserPrivate(ctkDICOMBrowser* parent): q_ptr(parent){
  69. DICOMDatabase = QSharedPointer<ctkDICOMDatabase> (new ctkDICOMDatabase);
  70. DICOMIndexer = QSharedPointer<ctkDICOMIndexer> (new ctkDICOMIndexer);
  71. IndexerProgress = 0;
  72. UpdateSchemaProgress = 0;
  73. DisplayImportSummary = true;
  74. PatientsAddedDuringImport = 0;
  75. StudiesAddedDuringImport = 0;
  76. SeriesAddedDuringImport = 0;
  77. InstancesAddedDuringImport = 0;
  78. }
  79. ctkDICOMBrowserPrivate::~ctkDICOMBrowserPrivate()
  80. {
  81. if ( IndexerProgress )
  82. {
  83. delete IndexerProgress;
  84. }
  85. if ( UpdateSchemaProgress )
  86. {
  87. delete UpdateSchemaProgress;
  88. }
  89. }
  90. void ctkDICOMBrowserPrivate::showUpdateSchemaDialog()
  91. {
  92. Q_Q(ctkDICOMBrowser);
  93. if (UpdateSchemaProgress == 0)
  94. {
  95. //
  96. // Set up the Update Schema Progress Dialog
  97. //
  98. UpdateSchemaProgress = new QProgressDialog(
  99. q->tr("DICOM Schema Update"), "Cancel", 0, 100, q,
  100. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  101. // We don't want the progress dialog to resize itself, so we bypass the label
  102. // by creating our own
  103. QLabel* progressLabel = new QLabel(q->tr("Initialization..."));
  104. UpdateSchemaProgress->setLabel(progressLabel);
  105. UpdateSchemaProgress->setWindowModality(Qt::ApplicationModal);
  106. UpdateSchemaProgress->setMinimumDuration(0);
  107. UpdateSchemaProgress->setValue(0);
  108. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateStarted(int)),
  109. UpdateSchemaProgress, SLOT(setMaximum(int)));
  110. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateProgress(int)),
  111. UpdateSchemaProgress, SLOT(setValue(int)));
  112. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateProgress(QString)),
  113. progressLabel, SLOT(setText(QString)));
  114. // close the dialog
  115. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdated()),
  116. UpdateSchemaProgress, SLOT(close()));
  117. }
  118. UpdateSchemaProgress->show();
  119. }
  120. void ctkDICOMBrowserPrivate::showIndexerDialog()
  121. {
  122. Q_Q(ctkDICOMBrowser);
  123. if (IndexerProgress == 0)
  124. {
  125. //
  126. // Set up the Indexer Progress Dialog
  127. //
  128. IndexerProgress = new QProgressDialog( q->tr("DICOM Import"), "Cancel", 0, 100, q,
  129. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  130. // We don't want the progress dialog to resize itself, so we bypass the label
  131. // by creating our own
  132. QLabel* progressLabel = new QLabel(q->tr("Initialization..."));
  133. IndexerProgress->setLabel(progressLabel);
  134. IndexerProgress->setWindowModality(Qt::ApplicationModal);
  135. IndexerProgress->setMinimumDuration(0);
  136. IndexerProgress->setValue(0);
  137. q->connect(IndexerProgress, SIGNAL(canceled()),
  138. DICOMIndexer.data(), SLOT(cancel()));
  139. q->connect(DICOMIndexer.data(), SIGNAL(progress(int)),
  140. IndexerProgress, SLOT(setValue(int)));
  141. q->connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
  142. progressLabel, SLOT(setText(QString)));
  143. q->connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
  144. q, SLOT(onFileIndexed(QString)));
  145. // close the dialog
  146. q->connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
  147. IndexerProgress, SLOT(close()));
  148. // stop indexing and reset the database if canceled
  149. q->connect(IndexerProgress, SIGNAL(canceled()),
  150. DICOMIndexer.data(), SLOT(cancel()));
  151. // allow users of this widget to know that the process has finished
  152. q->connect(IndexerProgress, SIGNAL(canceled()),
  153. q, SIGNAL(directoryImported()));
  154. q->connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
  155. q, SIGNAL(directoryImported()));
  156. }
  157. IndexerProgress->show();
  158. }
  159. //----------------------------------------------------------------------------
  160. // ctkDICOMBrowser methods
  161. //----------------------------------------------------------------------------
  162. ctkDICOMBrowser::ctkDICOMBrowser(QWidget* _parent):Superclass(_parent),
  163. d_ptr(new ctkDICOMBrowserPrivate(this))
  164. {
  165. Q_D(ctkDICOMBrowser);
  166. d->setupUi(this);
  167. // signals related to tracking inserts
  168. connect(d->DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
  169. SLOT(onPatientAdded(int,QString,QString,QString)));
  170. connect(d->DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
  171. connect(d->DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
  172. connect(d->DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
  173. connect(d->tableDensityComboBox ,SIGNAL(currentIndexChanged (const QString&)),
  174. this, SLOT(onTablesDensityComboBox(QString)));
  175. //Set ToolBar button style
  176. d->ToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  177. //Initialize Q/R widget
  178. d->QueryRetrieveWidget = new ctkDICOMQueryRetrieveWidget();
  179. d->QueryRetrieveWidget->setWindowModality ( Qt::ApplicationModal );
  180. //initialize directory from settings, then listen for changes
  181. QSettings settings;
  182. if ( settings.value("DatabaseDirectory", "") == "" )
  183. {
  184. QString directory = QString("./ctkDICOM-Database");
  185. settings.setValue("DatabaseDirectory", directory);
  186. settings.sync();
  187. }
  188. QString databaseDirectory = settings.value("DatabaseDirectory").toString();
  189. this->setDatabaseDirectory(databaseDirectory);
  190. d->DirectoryButton->setDirectory(databaseDirectory);
  191. d->dicomTableManager->setDICOMDatabase(d->DICOMDatabase.data());
  192. // TableView signals
  193. connect(d->dicomTableManager, SIGNAL(patientsSelectionChanged(const QItemSelection&, const QItemSelection&)),
  194. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  195. connect(d->dicomTableManager, SIGNAL(studiesSelectionChanged(const QItemSelection&, const QItemSelection&)),
  196. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  197. connect(d->dicomTableManager, SIGNAL(seriesSelectionChanged(const QItemSelection&, const QItemSelection&)),
  198. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  199. connect(d->DirectoryButton, SIGNAL(directoryChanged(QString)), this, SLOT(setDatabaseDirectory(QString)));
  200. //Initialize import widget
  201. d->ImportDialog = new ctkFileDialog();
  202. QCheckBox* importCheckbox = new QCheckBox("Copy on import", d->ImportDialog);
  203. d->ImportDialog->setBottomWidget(importCheckbox);
  204. d->ImportDialog->setFileMode(QFileDialog::Directory);
  205. d->ImportDialog->setLabelText(QFileDialog::Accept,"Import");
  206. d->ImportDialog->setWindowTitle("Import DICOM files from directory ...");
  207. d->ImportDialog->setWindowModality(Qt::ApplicationModal);
  208. //connect signal and slots
  209. connect(d->ImportDialog, SIGNAL(fileSelected(QString)),this,SLOT(onImportDirectory(QString)));
  210. connect(d->QueryRetrieveWidget, SIGNAL(canceled()), d->QueryRetrieveWidget, SLOT(hide()) );
  211. connect(d->QueryRetrieveWidget, SIGNAL(canceled()), this, SLOT(onQueryRetrieveFinished()) );
  212. }
  213. //----------------------------------------------------------------------------
  214. ctkDICOMBrowser::~ctkDICOMBrowser()
  215. {
  216. Q_D(ctkDICOMBrowser);
  217. d->QueryRetrieveWidget->deleteLater();
  218. d->ImportDialog->deleteLater();
  219. }
  220. //----------------------------------------------------------------------------
  221. bool ctkDICOMBrowser::displayImportSummary()
  222. {
  223. Q_D(ctkDICOMBrowser);
  224. return d->DisplayImportSummary;
  225. }
  226. //----------------------------------------------------------------------------
  227. void ctkDICOMBrowser::setDisplayImportSummary(bool onOff)
  228. {
  229. Q_D(ctkDICOMBrowser);
  230. d->DisplayImportSummary = onOff;
  231. }
  232. //----------------------------------------------------------------------------
  233. int ctkDICOMBrowser::patientsAddedDuringImport()
  234. {
  235. Q_D(ctkDICOMBrowser);
  236. return d->PatientsAddedDuringImport;
  237. }
  238. //----------------------------------------------------------------------------
  239. int ctkDICOMBrowser::studiesAddedDuringImport()
  240. {
  241. Q_D(ctkDICOMBrowser);
  242. return d->StudiesAddedDuringImport;
  243. }
  244. //----------------------------------------------------------------------------
  245. int ctkDICOMBrowser::seriesAddedDuringImport()
  246. {
  247. Q_D(ctkDICOMBrowser);
  248. return d->SeriesAddedDuringImport;
  249. }
  250. //----------------------------------------------------------------------------
  251. int ctkDICOMBrowser::instancesAddedDuringImport()
  252. {
  253. Q_D(ctkDICOMBrowser);
  254. return d->InstancesAddedDuringImport;
  255. }
  256. //----------------------------------------------------------------------------
  257. void ctkDICOMBrowser::updateDatabaseSchemaIfNeeded()
  258. {
  259. Q_D(ctkDICOMBrowser);
  260. d->showUpdateSchemaDialog();
  261. d->DICOMDatabase->updateSchemaIfNeeded();
  262. }
  263. //----------------------------------------------------------------------------
  264. void ctkDICOMBrowser::setDatabaseDirectory(const QString& directory)
  265. {
  266. Q_D(ctkDICOMBrowser);
  267. QSettings settings;
  268. settings.setValue("DatabaseDirectory", directory);
  269. settings.sync();
  270. //close the active DICOM database
  271. d->DICOMDatabase->closeDatabase();
  272. //open DICOM database on the directory
  273. QString databaseFileName = directory + QString("/ctkDICOM.sql");
  274. try
  275. {
  276. d->DICOMDatabase->openDatabase( databaseFileName );
  277. }
  278. catch (std::exception e)
  279. {
  280. std::cerr << "Database error: " << qPrintable(d->DICOMDatabase->lastError()) << "\n";
  281. d->DICOMDatabase->closeDatabase();
  282. return;
  283. }
  284. // update the database schema if needed and provide progress
  285. this->updateDatabaseSchemaIfNeeded();
  286. //pass DICOM database instance to Import widget
  287. d->QueryRetrieveWidget->setRetrieveDatabase(d->DICOMDatabase);
  288. // update the button and let any connected slots know about the change
  289. d->DirectoryButton->setDirectory(directory);
  290. d->dicomTableManager->updateTableViews();
  291. emit databaseDirectoryChanged(directory);
  292. }
  293. //----------------------------------------------------------------------------
  294. QString ctkDICOMBrowser::databaseDirectory() const
  295. {
  296. QSettings settings;
  297. return settings.value("DatabaseDirectory").toString();
  298. }
  299. //------------------------------------------------------------------------------
  300. void ctkDICOMBrowser::setTagsToPrecache( const QStringList tags)
  301. {
  302. Q_D(ctkDICOMBrowser);
  303. d->DICOMDatabase->setTagsToPrecache(tags);
  304. }
  305. //------------------------------------------------------------------------------
  306. const QStringList ctkDICOMBrowser::tagsToPrecache()
  307. {
  308. Q_D(ctkDICOMBrowser);
  309. return d->DICOMDatabase->tagsToPrecache();
  310. }
  311. //----------------------------------------------------------------------------
  312. ctkDICOMDatabase* ctkDICOMBrowser::database(){
  313. Q_D(ctkDICOMBrowser);
  314. return d->DICOMDatabase.data();
  315. }
  316. //----------------------------------------------------------------------------
  317. ctkDICOMTableManager* ctkDICOMBrowser::dicomTableManager()
  318. {
  319. Q_D(ctkDICOMBrowser);
  320. return d->dicomTableManager;
  321. }
  322. //----------------------------------------------------------------------------
  323. void ctkDICOMBrowser::onFileIndexed(const QString& filePath)
  324. {
  325. // Update the progress dialog when the file name changes
  326. // - also allows for cancel button
  327. QCoreApplication::instance()->processEvents();
  328. qDebug() << "Indexing \n\n\n\n" << filePath <<"\n\n\n";
  329. }
  330. //----------------------------------------------------------------------------
  331. void ctkDICOMBrowser::openImportDialog()
  332. {
  333. Q_D(ctkDICOMBrowser);
  334. d->ImportDialog->show();
  335. d->ImportDialog->raise();
  336. }
  337. //----------------------------------------------------------------------------
  338. void ctkDICOMBrowser::openExportDialog()
  339. {
  340. }
  341. //----------------------------------------------------------------------------
  342. void ctkDICOMBrowser::openQueryDialog()
  343. {
  344. Q_D(ctkDICOMBrowser);
  345. d->QueryRetrieveWidget->show();
  346. d->QueryRetrieveWidget->raise();
  347. }
  348. //----------------------------------------------------------------------------
  349. void ctkDICOMBrowser::onQueryRetrieveFinished()
  350. {
  351. Q_D(ctkDICOMBrowser);
  352. emit this->queryRetrieveFinished();
  353. }
  354. //----------------------------------------------------------------------------
  355. void ctkDICOMBrowser::onRemoveAction()
  356. {
  357. Q_D(ctkDICOMBrowser);
  358. QStringList selectedSeriesUIDs = d->dicomTableManager->currentSeriesSelection();
  359. foreach (const QString& uid, selectedSeriesUIDs)
  360. {
  361. d->DICOMDatabase->removeSeries(uid);
  362. }
  363. QStringList selectedStudiesUIDs = d->dicomTableManager->currentStudiesSelection();
  364. foreach (const QString& uid, selectedStudiesUIDs)
  365. {
  366. d->DICOMDatabase->removeStudy(uid);
  367. }
  368. QStringList selectedPatientUIDs = d->dicomTableManager->currentPatientsSelection();
  369. foreach (const QString& uid, selectedPatientUIDs)
  370. {
  371. d->DICOMDatabase->removePatient(uid);
  372. }
  373. // Update the table views
  374. d->dicomTableManager->updateTableViews();
  375. }
  376. //----------------------------------------------------------------------------
  377. void ctkDICOMBrowser::onTablesDensityComboBox(QString density)
  378. {
  379. Q_D(ctkDICOMBrowser);
  380. if ( density == "Comfortable")
  381. {
  382. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Comfortable);
  383. }
  384. else if ( density == "Cozy")
  385. {
  386. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Cozy);
  387. }
  388. else if ( density == "Compact")
  389. {
  390. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Compact);
  391. }
  392. }
  393. //----------------------------------------------------------------------------
  394. void ctkDICOMBrowser::onPatientAdded(int databaseID, QString patientID, QString patientName, QString patientBirthDate )
  395. {
  396. Q_D(ctkDICOMBrowser);
  397. Q_UNUSED(databaseID);
  398. Q_UNUSED(patientID);
  399. Q_UNUSED(patientName);
  400. Q_UNUSED(patientBirthDate);
  401. ++d->PatientsAddedDuringImport;
  402. }
  403. //----------------------------------------------------------------------------
  404. void ctkDICOMBrowser::onStudyAdded(QString studyUID)
  405. {
  406. Q_D(ctkDICOMBrowser);
  407. Q_UNUSED(studyUID);
  408. ++d->StudiesAddedDuringImport;
  409. }
  410. //----------------------------------------------------------------------------
  411. void ctkDICOMBrowser::onSeriesAdded(QString seriesUID)
  412. {
  413. Q_D(ctkDICOMBrowser);
  414. Q_UNUSED(seriesUID);
  415. ++d->SeriesAddedDuringImport;
  416. }
  417. //----------------------------------------------------------------------------
  418. void ctkDICOMBrowser::onInstanceAdded(QString instanceUID)
  419. {
  420. Q_D(ctkDICOMBrowser);
  421. Q_UNUSED(instanceUID);
  422. ++d->InstancesAddedDuringImport;
  423. }
  424. //----------------------------------------------------------------------------
  425. void ctkDICOMBrowser::onImportDirectory(QString directory)
  426. {
  427. Q_D(ctkDICOMBrowser);
  428. if (QDir(directory).exists())
  429. {
  430. QCheckBox* copyOnImport = qobject_cast<QCheckBox*>(d->ImportDialog->bottomWidget());
  431. QString targetDirectory;
  432. if (copyOnImport->checkState() == Qt::Checked)
  433. {
  434. targetDirectory = d->DICOMDatabase->databaseDirectory();
  435. }
  436. // reset counts
  437. d->PatientsAddedDuringImport = 0;
  438. d->StudiesAddedDuringImport = 0;
  439. d->SeriesAddedDuringImport = 0;
  440. d->InstancesAddedDuringImport = 0;
  441. // show progress dialog and perform indexing
  442. d->showIndexerDialog();
  443. d->DICOMIndexer->addDirectory(*d->DICOMDatabase,directory,targetDirectory);
  444. // display summary result
  445. if (d->DisplayImportSummary)
  446. {
  447. QString message = "Directory import completed.\n\n";
  448. message += QString("%1 New Patients\n").arg(QString::number(d->PatientsAddedDuringImport));
  449. message += QString("%1 New Studies\n").arg(QString::number(d->StudiesAddedDuringImport));
  450. message += QString("%1 New Series\n").arg(QString::number(d->SeriesAddedDuringImport));
  451. message += QString("%1 New Instances\n").arg(QString::number(d->InstancesAddedDuringImport));
  452. QMessageBox::information(this,"DICOM Directory Import", message);
  453. }
  454. }
  455. }
  456. //----------------------------------------------------------------------------
  457. void ctkDICOMBrowser::onModelSelected(const QItemSelection &item1, const QItemSelection &item2)
  458. {
  459. Q_UNUSED(item1);
  460. Q_UNUSED(item2);
  461. Q_D(ctkDICOMBrowser);
  462. d->ActionRemove->setEnabled(true);
  463. }