ctkDICOMBrowser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 <QApplication>
  19. #include <QCoreApplication>
  20. #include <QCheckBox>
  21. #include <QDebug>
  22. #include <QFile>
  23. #include <QListView>
  24. #include <QMenu>
  25. #include <QMessageBox>
  26. #include <QProgressDialog>
  27. #include <QSettings>
  28. #include <QStringListModel>
  29. #include <QWidgetAction>
  30. // ctkWidgets includes
  31. #include "ctkDirectoryButton.h"
  32. #include "ctkFileDialog.h"
  33. #include "ctkMessageBox.h"
  34. // ctkDICOMCore includes
  35. #include "ctkDICOMDatabase.h"
  36. #include "ctkDICOMIndexer.h"
  37. // ctkDICOMWidgets includes
  38. #include "ctkDICOMBrowser.h"
  39. #include "ctkDICOMQueryResultsTabWidget.h"
  40. #include "ctkDICOMQueryRetrieveWidget.h"
  41. #include "ctkDICOMQueryWidget.h"
  42. #include "ctkDICOMTableManager.h"
  43. #include "ui_ctkDICOMBrowser.h"
  44. //logger
  45. #include <ctkLogger.h>
  46. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMBrowser");
  47. //----------------------------------------------------------------------------
  48. class ctkDICOMBrowserPrivate: public Ui_ctkDICOMBrowser
  49. {
  50. public:
  51. ctkDICOMBrowser* const q_ptr;
  52. Q_DECLARE_PUBLIC(ctkDICOMBrowser);
  53. ctkDICOMBrowserPrivate(ctkDICOMBrowser* );
  54. ~ctkDICOMBrowserPrivate();
  55. ctkFileDialog* ImportDialog;
  56. ctkDICOMQueryRetrieveWidget* QueryRetrieveWidget;
  57. QSharedPointer<ctkDICOMDatabase> DICOMDatabase;
  58. QSharedPointer<ctkDICOMIndexer> DICOMIndexer;
  59. QProgressDialog *IndexerProgress;
  60. QProgressDialog *UpdateSchemaProgress;
  61. void showIndexerDialog();
  62. void showUpdateSchemaDialog();
  63. // used when suspending the ctkDICOMModel
  64. QSqlDatabase EmptyDatabase;
  65. // local count variables to keep track of the number of items
  66. // added to the database during an import operation
  67. bool DisplayImportSummary;
  68. int PatientsAddedDuringImport;
  69. int StudiesAddedDuringImport;
  70. int SeriesAddedDuringImport;
  71. int InstancesAddedDuringImport;
  72. };
  73. //----------------------------------------------------------------------------
  74. // ctkDICOMBrowserPrivate methods
  75. ctkDICOMBrowserPrivate::ctkDICOMBrowserPrivate(ctkDICOMBrowser* parent): q_ptr(parent){
  76. DICOMDatabase = QSharedPointer<ctkDICOMDatabase> (new ctkDICOMDatabase);
  77. DICOMIndexer = QSharedPointer<ctkDICOMIndexer> (new ctkDICOMIndexer);
  78. IndexerProgress = 0;
  79. UpdateSchemaProgress = 0;
  80. DisplayImportSummary = true;
  81. PatientsAddedDuringImport = 0;
  82. StudiesAddedDuringImport = 0;
  83. SeriesAddedDuringImport = 0;
  84. InstancesAddedDuringImport = 0;
  85. }
  86. ctkDICOMBrowserPrivate::~ctkDICOMBrowserPrivate()
  87. {
  88. if ( IndexerProgress )
  89. {
  90. delete IndexerProgress;
  91. }
  92. if ( UpdateSchemaProgress )
  93. {
  94. delete UpdateSchemaProgress;
  95. }
  96. }
  97. void ctkDICOMBrowserPrivate::showUpdateSchemaDialog()
  98. {
  99. Q_Q(ctkDICOMBrowser);
  100. if (UpdateSchemaProgress == 0)
  101. {
  102. //
  103. // Set up the Update Schema Progress Dialog
  104. //
  105. UpdateSchemaProgress = new QProgressDialog(
  106. q->tr("DICOM Schema Update"), "Cancel", 0, 100, q,
  107. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  108. // We don't want the progress dialog to resize itself, so we bypass the label
  109. // by creating our own
  110. QLabel* progressLabel = new QLabel(q->tr("Initialization..."));
  111. UpdateSchemaProgress->setLabel(progressLabel);
  112. UpdateSchemaProgress->setWindowModality(Qt::ApplicationModal);
  113. UpdateSchemaProgress->setMinimumDuration(0);
  114. UpdateSchemaProgress->setValue(0);
  115. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateStarted(int)),
  116. UpdateSchemaProgress, SLOT(setMaximum(int)));
  117. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateProgress(int)),
  118. UpdateSchemaProgress, SLOT(setValue(int)));
  119. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdateProgress(QString)),
  120. progressLabel, SLOT(setText(QString)));
  121. // close the dialog
  122. q->connect(DICOMDatabase.data(), SIGNAL(schemaUpdated()),
  123. UpdateSchemaProgress, SLOT(close()));
  124. }
  125. UpdateSchemaProgress->show();
  126. }
  127. void ctkDICOMBrowserPrivate::showIndexerDialog()
  128. {
  129. Q_Q(ctkDICOMBrowser);
  130. if (IndexerProgress == 0)
  131. {
  132. //
  133. // Set up the Indexer Progress Dialog
  134. //
  135. IndexerProgress = new QProgressDialog( q->tr("DICOM Import"), "Cancel", 0, 100, q,
  136. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  137. // We don't want the progress dialog to resize itself, so we bypass the label
  138. // by creating our own
  139. QLabel* progressLabel = new QLabel(q->tr("Initialization..."));
  140. IndexerProgress->setLabel(progressLabel);
  141. IndexerProgress->setWindowModality(Qt::ApplicationModal);
  142. IndexerProgress->setMinimumDuration(0);
  143. IndexerProgress->setValue(0);
  144. q->connect(IndexerProgress, SIGNAL(canceled()),
  145. DICOMIndexer.data(), SLOT(cancel()));
  146. q->connect(DICOMIndexer.data(), SIGNAL(progress(int)),
  147. IndexerProgress, SLOT(setValue(int)));
  148. q->connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
  149. progressLabel, SLOT(setText(QString)));
  150. q->connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
  151. q, SLOT(onFileIndexed(QString)));
  152. // close the dialog
  153. q->connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
  154. IndexerProgress, SLOT(close()));
  155. // stop indexing and reset the database if canceled
  156. q->connect(IndexerProgress, SIGNAL(canceled()),
  157. DICOMIndexer.data(), SLOT(cancel()));
  158. // allow users of this widget to know that the process has finished
  159. q->connect(IndexerProgress, SIGNAL(canceled()),
  160. q, SIGNAL(directoryImported()));
  161. q->connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
  162. q, SIGNAL(directoryImported()));
  163. }
  164. IndexerProgress->show();
  165. }
  166. //----------------------------------------------------------------------------
  167. // ctkDICOMBrowser methods
  168. //----------------------------------------------------------------------------
  169. ctkDICOMBrowser::ctkDICOMBrowser(QWidget* _parent):Superclass(_parent),
  170. d_ptr(new ctkDICOMBrowserPrivate(this))
  171. {
  172. Q_D(ctkDICOMBrowser);
  173. d->setupUi(this);
  174. // signals related to tracking inserts
  175. connect(d->DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
  176. SLOT(onPatientAdded(int,QString,QString,QString)));
  177. connect(d->DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
  178. connect(d->DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
  179. connect(d->DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
  180. connect(d->tableDensityComboBox ,SIGNAL(currentIndexChanged (const QString&)),
  181. this, SLOT(onTablesDensityComboBox(QString)));
  182. //Set ToolBar button style
  183. d->ToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  184. //Initialize Q/R widget
  185. d->QueryRetrieveWidget = new ctkDICOMQueryRetrieveWidget();
  186. d->QueryRetrieveWidget->setWindowModality ( Qt::ApplicationModal );
  187. //initialize directory from settings, then listen for changes
  188. QSettings settings;
  189. if ( settings.value("DatabaseDirectory", "") == "" )
  190. {
  191. QString directory = QString("./ctkDICOM-Database");
  192. settings.setValue("DatabaseDirectory", directory);
  193. settings.sync();
  194. }
  195. QString databaseDirectory = settings.value("DatabaseDirectory").toString();
  196. this->setDatabaseDirectory(databaseDirectory);
  197. d->DirectoryButton->setDirectory(databaseDirectory);
  198. d->dicomTableManager->setDICOMDatabase(d->DICOMDatabase.data());
  199. // TableView signals
  200. connect(d->dicomTableManager, SIGNAL(patientsSelectionChanged(const QItemSelection&, const QItemSelection&)),
  201. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  202. connect(d->dicomTableManager, SIGNAL(studiesSelectionChanged(const QItemSelection&, const QItemSelection&)),
  203. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  204. connect(d->dicomTableManager, SIGNAL(seriesSelectionChanged(const QItemSelection&, const QItemSelection&)),
  205. this, SLOT(onModelSelected(const QItemSelection&,const QItemSelection&)));
  206. // set up context menus for working on selected patients, studies, series
  207. connect(d->dicomTableManager, SIGNAL(patientsRightClicked(const QPoint&)),
  208. this, SLOT(onPatientsRightClicked(const QPoint&)));
  209. connect(d->dicomTableManager, SIGNAL(studiesRightClicked(const QPoint&)),
  210. this, SLOT(onStudiesRightClicked(const QPoint&)));
  211. connect(d->dicomTableManager, SIGNAL(seriesRightClicked(const QPoint&)),
  212. this, SLOT(onSeriesRightClicked(const QPoint&)));
  213. connect(d->DirectoryButton, SIGNAL(directoryChanged(QString)), this, SLOT(setDatabaseDirectory(QString)));
  214. //Initialize import widget
  215. d->ImportDialog = new ctkFileDialog();
  216. QCheckBox* importCheckbox = new QCheckBox("Copy on import", d->ImportDialog);
  217. importCheckbox->setCheckState(Qt::Checked);
  218. d->ImportDialog->setBottomWidget(importCheckbox);
  219. d->ImportDialog->setFileMode(QFileDialog::Directory);
  220. d->ImportDialog->setLabelText(QFileDialog::Accept,"Import");
  221. d->ImportDialog->setWindowTitle("Import DICOM files from directory ...");
  222. d->ImportDialog->setWindowModality(Qt::ApplicationModal);
  223. //connect signal and slots
  224. connect(d->ImportDialog, SIGNAL(fileSelected(QString)),this,SLOT(onImportDirectory(QString)));
  225. connect(d->QueryRetrieveWidget, SIGNAL(canceled()), d->QueryRetrieveWidget, SLOT(hide()) );
  226. connect(d->QueryRetrieveWidget, SIGNAL(canceled()), this, SLOT(onQueryRetrieveFinished()) );
  227. }
  228. //----------------------------------------------------------------------------
  229. ctkDICOMBrowser::~ctkDICOMBrowser()
  230. {
  231. Q_D(ctkDICOMBrowser);
  232. d->QueryRetrieveWidget->deleteLater();
  233. d->ImportDialog->deleteLater();
  234. }
  235. //----------------------------------------------------------------------------
  236. bool ctkDICOMBrowser::displayImportSummary()
  237. {
  238. Q_D(ctkDICOMBrowser);
  239. return d->DisplayImportSummary;
  240. }
  241. //----------------------------------------------------------------------------
  242. void ctkDICOMBrowser::setDisplayImportSummary(bool onOff)
  243. {
  244. Q_D(ctkDICOMBrowser);
  245. d->DisplayImportSummary = onOff;
  246. }
  247. //----------------------------------------------------------------------------
  248. int ctkDICOMBrowser::patientsAddedDuringImport()
  249. {
  250. Q_D(ctkDICOMBrowser);
  251. return d->PatientsAddedDuringImport;
  252. }
  253. //----------------------------------------------------------------------------
  254. int ctkDICOMBrowser::studiesAddedDuringImport()
  255. {
  256. Q_D(ctkDICOMBrowser);
  257. return d->StudiesAddedDuringImport;
  258. }
  259. //----------------------------------------------------------------------------
  260. int ctkDICOMBrowser::seriesAddedDuringImport()
  261. {
  262. Q_D(ctkDICOMBrowser);
  263. return d->SeriesAddedDuringImport;
  264. }
  265. //----------------------------------------------------------------------------
  266. int ctkDICOMBrowser::instancesAddedDuringImport()
  267. {
  268. Q_D(ctkDICOMBrowser);
  269. return d->InstancesAddedDuringImport;
  270. }
  271. //----------------------------------------------------------------------------
  272. void ctkDICOMBrowser::updateDatabaseSchemaIfNeeded()
  273. {
  274. Q_D(ctkDICOMBrowser);
  275. d->showUpdateSchemaDialog();
  276. d->DICOMDatabase->updateSchemaIfNeeded();
  277. }
  278. //----------------------------------------------------------------------------
  279. void ctkDICOMBrowser::setDatabaseDirectory(const QString& directory)
  280. {
  281. Q_D(ctkDICOMBrowser);
  282. QSettings settings;
  283. settings.setValue("DatabaseDirectory", directory);
  284. settings.sync();
  285. //close the active DICOM database
  286. d->DICOMDatabase->closeDatabase();
  287. //open DICOM database on the directory
  288. QString databaseFileName = directory + QString("/ctkDICOM.sql");
  289. try
  290. {
  291. d->DICOMDatabase->openDatabase( databaseFileName );
  292. }
  293. catch (std::exception e)
  294. {
  295. std::cerr << "Database error: " << qPrintable(d->DICOMDatabase->lastError()) << "\n";
  296. d->DICOMDatabase->closeDatabase();
  297. return;
  298. }
  299. // update the database schema if needed and provide progress
  300. this->updateDatabaseSchemaIfNeeded();
  301. //pass DICOM database instance to Import widget
  302. d->QueryRetrieveWidget->setRetrieveDatabase(d->DICOMDatabase);
  303. // update the button and let any connected slots know about the change
  304. d->DirectoryButton->setDirectory(directory);
  305. d->dicomTableManager->updateTableViews();
  306. emit databaseDirectoryChanged(directory);
  307. }
  308. //----------------------------------------------------------------------------
  309. QString ctkDICOMBrowser::databaseDirectory() const
  310. {
  311. QSettings settings;
  312. return settings.value("DatabaseDirectory").toString();
  313. }
  314. //------------------------------------------------------------------------------
  315. void ctkDICOMBrowser::setTagsToPrecache( const QStringList tags)
  316. {
  317. Q_D(ctkDICOMBrowser);
  318. d->DICOMDatabase->setTagsToPrecache(tags);
  319. }
  320. //------------------------------------------------------------------------------
  321. const QStringList ctkDICOMBrowser::tagsToPrecache()
  322. {
  323. Q_D(ctkDICOMBrowser);
  324. return d->DICOMDatabase->tagsToPrecache();
  325. }
  326. //----------------------------------------------------------------------------
  327. ctkDICOMDatabase* ctkDICOMBrowser::database(){
  328. Q_D(ctkDICOMBrowser);
  329. return d->DICOMDatabase.data();
  330. }
  331. //----------------------------------------------------------------------------
  332. ctkDICOMTableManager* ctkDICOMBrowser::dicomTableManager()
  333. {
  334. Q_D(ctkDICOMBrowser);
  335. return d->dicomTableManager;
  336. }
  337. //----------------------------------------------------------------------------
  338. void ctkDICOMBrowser::onFileIndexed(const QString& filePath)
  339. {
  340. // Update the progress dialog when the file name changes
  341. // - also allows for cancel button
  342. QCoreApplication::instance()->processEvents();
  343. qDebug() << "Indexing \n\n\n\n" << filePath <<"\n\n\n";
  344. }
  345. //----------------------------------------------------------------------------
  346. void ctkDICOMBrowser::openImportDialog()
  347. {
  348. Q_D(ctkDICOMBrowser);
  349. d->ImportDialog->show();
  350. d->ImportDialog->raise();
  351. }
  352. //----------------------------------------------------------------------------
  353. void ctkDICOMBrowser::openExportDialog()
  354. {
  355. }
  356. //----------------------------------------------------------------------------
  357. void ctkDICOMBrowser::openQueryDialog()
  358. {
  359. Q_D(ctkDICOMBrowser);
  360. d->QueryRetrieveWidget->show();
  361. d->QueryRetrieveWidget->raise();
  362. }
  363. //----------------------------------------------------------------------------
  364. void ctkDICOMBrowser::onQueryRetrieveFinished()
  365. {
  366. emit this->queryRetrieveFinished();
  367. }
  368. //----------------------------------------------------------------------------
  369. void ctkDICOMBrowser::onRemoveAction()
  370. {
  371. Q_D(ctkDICOMBrowser);
  372. QStringList selectedSeriesUIDs = d->dicomTableManager->currentSeriesSelection();
  373. foreach (const QString& uid, selectedSeriesUIDs)
  374. {
  375. d->DICOMDatabase->removeSeries(uid);
  376. }
  377. QStringList selectedStudiesUIDs = d->dicomTableManager->currentStudiesSelection();
  378. foreach (const QString& uid, selectedStudiesUIDs)
  379. {
  380. d->DICOMDatabase->removeStudy(uid);
  381. }
  382. QStringList selectedPatientUIDs = d->dicomTableManager->currentPatientsSelection();
  383. foreach (const QString& uid, selectedPatientUIDs)
  384. {
  385. d->DICOMDatabase->removePatient(uid);
  386. }
  387. // Update the table views
  388. d->dicomTableManager->updateTableViews();
  389. }
  390. //----------------------------------------------------------------------------
  391. void ctkDICOMBrowser::onRepairAction()
  392. {
  393. Q_D(ctkDICOMBrowser);
  394. QMessageBox* repairMessageBox;
  395. repairMessageBox = new QMessageBox;
  396. repairMessageBox->setWindowTitle("Database Repair");
  397. QStringList allFiles(d->DICOMDatabase->allFiles());
  398. QSet<QString> corruptedSeries;
  399. QStringList::const_iterator it;
  400. for (it = allFiles.constBegin(); it!= allFiles.constEnd();++it)
  401. {
  402. QString fileName(*it);
  403. QFile dicomFile(fileName);
  404. if(!dicomFile.exists())
  405. {
  406. QString seriesUid = d->DICOMDatabase->seriesForFile(fileName);
  407. corruptedSeries.insert(seriesUid);
  408. }
  409. }
  410. if (corruptedSeries.size() == 0)
  411. {
  412. repairMessageBox->setText("All the files in the local database are available.");
  413. repairMessageBox->addButton(QMessageBox::Ok);
  414. repairMessageBox->exec();
  415. }
  416. else
  417. {
  418. repairMessageBox->addButton(QMessageBox::Yes);
  419. repairMessageBox->addButton(QMessageBox::No);
  420. QSet<QString>::iterator i;
  421. for (i = corruptedSeries.begin(); i != corruptedSeries.end(); ++i)
  422. {
  423. QStringList fileList (d->DICOMDatabase->filesForSeries(*i));
  424. QString unavailableFileNames;
  425. QStringList::const_iterator it;
  426. for (it= fileList.constBegin(); it!= fileList.constEnd();++it)
  427. {
  428. unavailableFileNames.append(*it+"\n");
  429. }
  430. QString firstFile (*(fileList.constBegin()));
  431. QHash<QString,QString> descriptions (d->DICOMDatabase->descriptionsForFile(firstFile));
  432. repairMessageBox->setText("The files for the following series are not available on the disk: \nPatient Name: "
  433. + descriptions["PatientsName"]+ "\n"+
  434. "Study Desciption: " + descriptions["StudyDescription"]+ "\n"+
  435. "Series Desciption: " + descriptions["SeriesDescription"]+ "\n"+
  436. "Do you want to remove the series from the DICOM database? ");
  437. repairMessageBox->setDetailedText(unavailableFileNames);
  438. int selection = repairMessageBox->exec();
  439. if (selection == QMessageBox::Yes)
  440. {
  441. d->DICOMDatabase->removeSeries(*i);
  442. d->dicomTableManager->updateTableViews();
  443. }
  444. }
  445. }
  446. }
  447. //----------------------------------------------------------------------------
  448. void ctkDICOMBrowser::onTablesDensityComboBox(QString density)
  449. {
  450. Q_D(ctkDICOMBrowser);
  451. if ( density == "Comfortable")
  452. {
  453. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Comfortable);
  454. }
  455. else if ( density == "Cozy")
  456. {
  457. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Cozy);
  458. }
  459. else if ( density == "Compact")
  460. {
  461. d->dicomTableManager->setDisplayDensity(ctkDICOMTableManager::Compact);
  462. }
  463. }
  464. //----------------------------------------------------------------------------
  465. void ctkDICOMBrowser::onPatientAdded(int databaseID, QString patientID, QString patientName, QString patientBirthDate )
  466. {
  467. Q_D(ctkDICOMBrowser);
  468. Q_UNUSED(databaseID);
  469. Q_UNUSED(patientID);
  470. Q_UNUSED(patientName);
  471. Q_UNUSED(patientBirthDate);
  472. ++d->PatientsAddedDuringImport;
  473. }
  474. //----------------------------------------------------------------------------
  475. void ctkDICOMBrowser::onStudyAdded(QString studyUID)
  476. {
  477. Q_D(ctkDICOMBrowser);
  478. Q_UNUSED(studyUID);
  479. ++d->StudiesAddedDuringImport;
  480. }
  481. //----------------------------------------------------------------------------
  482. void ctkDICOMBrowser::onSeriesAdded(QString seriesUID)
  483. {
  484. Q_D(ctkDICOMBrowser);
  485. Q_UNUSED(seriesUID);
  486. ++d->SeriesAddedDuringImport;
  487. }
  488. //----------------------------------------------------------------------------
  489. void ctkDICOMBrowser::onInstanceAdded(QString instanceUID)
  490. {
  491. Q_D(ctkDICOMBrowser);
  492. Q_UNUSED(instanceUID);
  493. ++d->InstancesAddedDuringImport;
  494. }
  495. //----------------------------------------------------------------------------
  496. void ctkDICOMBrowser::onImportDirectory(QString directory)
  497. {
  498. Q_D(ctkDICOMBrowser);
  499. if (QDir(directory).exists())
  500. {
  501. QString targetDirectory;
  502. QCheckBox* copyOnImport = qobject_cast<QCheckBox*>(d->ImportDialog->bottomWidget());
  503. ctkMessageBox importTypeDialog;
  504. QString message("Do you want to copy the files to the local database directory or just add the links?");
  505. importTypeDialog.setText(message);
  506. importTypeDialog.setIcon(QMessageBox::Question);
  507. importTypeDialog.addButton("Copy",QMessageBox::AcceptRole);
  508. importTypeDialog.addButton("Add Link",QMessageBox::RejectRole);
  509. importTypeDialog.setDontShowAgainSettingsKey( "MainWindow/DontConfirmCopyOnImport" );
  510. int selection = importTypeDialog.exec();
  511. if (selection== QMessageBox::AcceptRole)
  512. {
  513. copyOnImport->setCheckState(Qt::Checked);
  514. }
  515. else
  516. {
  517. copyOnImport->setCheckState(Qt::Unchecked);
  518. }
  519. // reset counts
  520. d->PatientsAddedDuringImport = 0;
  521. d->StudiesAddedDuringImport = 0;
  522. d->SeriesAddedDuringImport = 0;
  523. d->InstancesAddedDuringImport = 0;
  524. if (copyOnImport->checkState() == Qt::Checked)
  525. {
  526. targetDirectory = d->DICOMDatabase->databaseDirectory();
  527. }
  528. // show progress dialog and perform indexing
  529. d->showIndexerDialog();
  530. d->DICOMIndexer->addDirectory(*d->DICOMDatabase,directory,targetDirectory);
  531. // display summary result
  532. if (d->DisplayImportSummary)
  533. {
  534. QString message = "Directory import completed.\n\n";
  535. message += QString("%1 New Patients\n").arg(QString::number(d->PatientsAddedDuringImport));
  536. message += QString("%1 New Studies\n").arg(QString::number(d->StudiesAddedDuringImport));
  537. message += QString("%1 New Series\n").arg(QString::number(d->SeriesAddedDuringImport));
  538. message += QString("%1 New Instances\n").arg(QString::number(d->InstancesAddedDuringImport));
  539. QMessageBox::information(this,"DICOM Directory Import", message);
  540. }
  541. }
  542. }
  543. //----------------------------------------------------------------------------
  544. void ctkDICOMBrowser::onModelSelected(const QItemSelection &item1, const QItemSelection &item2)
  545. {
  546. Q_UNUSED(item1);
  547. Q_UNUSED(item2);
  548. Q_D(ctkDICOMBrowser);
  549. d->ActionRemove->setEnabled(true);
  550. }
  551. //----------------------------------------------------------------------------
  552. bool ctkDICOMBrowser::confirmDeleteSelectedIUDs(QStringList uids)
  553. {
  554. Q_D(ctkDICOMBrowser);
  555. if (uids.isEmpty())
  556. {
  557. return false;
  558. }
  559. ctkMessageBox confirmDeleteDialog;
  560. QString message("Do you want to delete the following selected items?");
  561. // add the information about the selected UIDs
  562. int numUIDs = uids.size();
  563. for (int i = 0; i < numUIDs; ++i)
  564. {
  565. QString uid = uids.at(i);
  566. // try using the given UID to find a descriptive string
  567. QString patientName = d->DICOMDatabase->nameForPatient(uid);
  568. QString studyDescription = d->DICOMDatabase->descriptionForStudy(uid);
  569. QString seriesDescription = d->DICOMDatabase->descriptionForSeries(uid);
  570. if (!patientName.isEmpty())
  571. {
  572. message += QString("\n") + patientName;
  573. }
  574. else if (!studyDescription.isEmpty())
  575. {
  576. message += QString("\n") + studyDescription;
  577. }
  578. else if (!seriesDescription.isEmpty())
  579. {
  580. message += QString("\n") + seriesDescription;
  581. }
  582. else
  583. {
  584. // if all other descriptors are empty, use the UID
  585. message += QString("\n") + uid;
  586. }
  587. }
  588. confirmDeleteDialog.setText(message);
  589. confirmDeleteDialog.setIcon(QMessageBox::Question);
  590. confirmDeleteDialog.addButton("Delete", QMessageBox::AcceptRole);
  591. confirmDeleteDialog.addButton("Cancel", QMessageBox::RejectRole);
  592. confirmDeleteDialog.setDontShowAgainSettingsKey( "MainWindow/DontConfirmDeleteSelected");
  593. int response = confirmDeleteDialog.exec();
  594. if (response == QMessageBox::AcceptRole)
  595. {
  596. return true;
  597. }
  598. else
  599. {
  600. return false;
  601. }
  602. }
  603. //----------------------------------------------------------------------------
  604. void ctkDICOMBrowser::onPatientsRightClicked(const QPoint &point)
  605. {
  606. Q_D(ctkDICOMBrowser);
  607. // get the list of patients that are selected
  608. QStringList selectedPatientsUIDs = d->dicomTableManager->currentPatientsSelection();
  609. int numPatients = selectedPatientsUIDs.size();
  610. if (numPatients == 0)
  611. {
  612. qDebug() << "No patients selected!";
  613. return;
  614. }
  615. QMenu *patientsMenu = new QMenu(d->dicomTableManager);
  616. QString deleteString = QString("Delete ")
  617. + QString::number(numPatients)
  618. + QString(" selected patients");
  619. QAction *deleteAction = new QAction(deleteString, patientsMenu);
  620. patientsMenu->addAction(deleteAction);
  621. // the table took care of mapping it to a global position so that the
  622. // menu will pop up at the correct place over this table.
  623. QAction *selectedAction = patientsMenu->exec(point);
  624. if (selectedAction == deleteAction
  625. && this->confirmDeleteSelectedIUDs(selectedPatientsUIDs))
  626. {
  627. qDebug() << "Deleting " << numPatients << " patients";
  628. foreach (const QString& uid, selectedPatientsUIDs)
  629. {
  630. d->DICOMDatabase->removePatient(uid);
  631. }
  632. }
  633. }
  634. //----------------------------------------------------------------------------
  635. void ctkDICOMBrowser::onStudiesRightClicked(const QPoint &point)
  636. {
  637. Q_D(ctkDICOMBrowser);
  638. // get the list of studies that are selected
  639. QStringList selectedStudiesUIDs = d->dicomTableManager->currentStudiesSelection();
  640. int numStudies = selectedStudiesUIDs.size();
  641. if (numStudies == 0)
  642. {
  643. qDebug() << "No studies selected!";
  644. return;
  645. }
  646. QMenu *studiesMenu = new QMenu(d->dicomTableManager);
  647. QString deleteString = QString("Delete ")
  648. + QString::number(numStudies)
  649. + QString(" selected studies");
  650. QAction *deleteAction = new QAction(deleteString, studiesMenu);
  651. studiesMenu->addAction(deleteAction);
  652. // the table took care of mapping it to a global position so that the
  653. // menu will pop up at the correct place over this table.
  654. QAction *selectedAction = studiesMenu->exec(point);
  655. if (selectedAction == deleteAction
  656. && this->confirmDeleteSelectedIUDs(selectedStudiesUIDs))
  657. {
  658. foreach (const QString& uid, selectedStudiesUIDs)
  659. {
  660. d->DICOMDatabase->removeStudy(uid);
  661. }
  662. }
  663. }
  664. //----------------------------------------------------------------------------
  665. void ctkDICOMBrowser::onSeriesRightClicked(const QPoint &point)
  666. {
  667. Q_D(ctkDICOMBrowser);
  668. // get the list of series that are selected
  669. QStringList selectedSeriesUIDs = d->dicomTableManager->currentSeriesSelection();
  670. int numSeries = selectedSeriesUIDs.size();
  671. if (numSeries == 0)
  672. {
  673. qDebug() << "No series selected!";
  674. return;
  675. }
  676. QMenu *seriesMenu = new QMenu(d->dicomTableManager);
  677. QString deleteString = QString("Delete ")
  678. + QString::number(numSeries)
  679. + QString(" selected series");
  680. QAction *deleteAction = new QAction(deleteString, seriesMenu);
  681. seriesMenu->addAction(deleteAction);
  682. // the table took care of mapping it to a global position so that the
  683. // menu will pop up at the correct place over this table.
  684. QAction *selectedAction = seriesMenu->exec(point);
  685. if (selectedAction == deleteAction
  686. && this->confirmDeleteSelectedIUDs(selectedSeriesUIDs))
  687. {
  688. foreach (const QString& uid, selectedSeriesUIDs)
  689. {
  690. d->DICOMDatabase->removeSeries(uid);
  691. }
  692. }
  693. }