ctkDICOMBrowser.cpp 17 KB

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