ctkDICOMAppWidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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.commontk.org/LICENSE
  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. #include <dcmimage.h>
  17. // Qt includes
  18. #include <QDebug>
  19. #include <QTreeView>
  20. #include <QTabBar>
  21. #include <QSettings>
  22. #include <QAction>
  23. #include <QModelIndex>
  24. #include <QCheckBox>
  25. // ctkWidgets includes
  26. #include "ctkDirectoryButton.h"
  27. #include "ctkFileDialog.h"
  28. // ctkDICOMCore includes
  29. #include "ctkDICOMDatabase.h"
  30. #include "ctkDICOMIndexer.h"
  31. #include "ctkDICOMModel.h"
  32. #include "ctkDICOMFilterProxyModel.h"
  33. // ctkDICOMWidgets includes
  34. #include "ctkDICOMAppWidget.h"
  35. #include "ctkDICOMQueryResultsTabWidget.h"
  36. #include "ctkDICOMQueryRetrieveWidget.h"
  37. #include "ctkDICOMQueryWidget.h"
  38. #include "ctkDICOMImportWidget.h"
  39. #include "ctkDICOMThumbnailWidget.h"
  40. #include "ctkDICOMThumbnailGenerator.h"
  41. #include "ui_ctkDICOMAppWidget.h"
  42. //logger
  43. #include <ctkLogger.h>
  44. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMAppWidget");
  45. //----------------------------------------------------------------------------
  46. class ctkDICOMAppWidgetPrivate: public Ui_ctkDICOMAppWidget
  47. {
  48. public:
  49. ctkDICOMAppWidgetPrivate();
  50. ctkFileDialog* importDIalog;
  51. ctkDICOMQueryRetrieveWidget* QueryRetrieveWidget;
  52. QSharedPointer<ctkDICOMDatabase> dicomDatabase;
  53. QSharedPointer<ctkDICOMThumbnailGenerator> thumbnailGenerator;
  54. ctkDICOMModel dicomModel;
  55. ctkDICOMFilterProxyModel dicomProxyModel;
  56. QSharedPointer<ctkDICOMIndexer> dicomIndexer;
  57. };
  58. //----------------------------------------------------------------------------
  59. // ctkDICOMAppWidgetPrivate methods
  60. ctkDICOMAppWidgetPrivate::ctkDICOMAppWidgetPrivate(){
  61. dicomDatabase = QSharedPointer<ctkDICOMDatabase> (new ctkDICOMDatabase);
  62. thumbnailGenerator = QSharedPointer <ctkDICOMThumbnailGenerator> (new ctkDICOMThumbnailGenerator);
  63. dicomDatabase->setThumbnailGenerator(thumbnailGenerator.data());
  64. dicomIndexer = QSharedPointer<ctkDICOMIndexer> (new ctkDICOMIndexer);
  65. dicomIndexer->setThumbnailGenerator(thumbnailGenerator.data());
  66. }
  67. //----------------------------------------------------------------------------
  68. // ctkDICOMAppWidget methods
  69. //----------------------------------------------------------------------------
  70. ctkDICOMAppWidget::ctkDICOMAppWidget(QWidget* _parent):Superclass(_parent),
  71. d_ptr(new ctkDICOMAppWidgetPrivate)
  72. {
  73. Q_D(ctkDICOMAppWidget);
  74. d->setupUi(this);
  75. //Hide image previewer buttons
  76. d->nextImageButton->hide();
  77. d->prevImageButton->hide();
  78. d->nextSeriesButton->hide();
  79. d->prevSeriesButton->hide();
  80. d->nextStudyButton->hide();
  81. d->prevStudyButton->hide();
  82. //Enable sorting in tree view
  83. d->treeView->setSortingEnabled(true);
  84. d->treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
  85. d->dicomProxyModel.setSourceModel(&d->dicomModel);
  86. d->treeView->setModel(&d->dicomProxyModel);
  87. connect(d->treeView, SIGNAL(collapsed(QModelIndex)), this, SLOT(onTreeCollapsed(QModelIndex)));
  88. connect(d->treeView, SIGNAL(expanded(QModelIndex)), this, SLOT(onTreeExpanded(QModelIndex)));
  89. //Set toolbar button style
  90. d->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  91. //Initialize Q/R widget
  92. d->QueryRetrieveWidget = new ctkDICOMQueryRetrieveWidget();
  93. d->QueryRetrieveWidget->setWindowModality ( Qt::ApplicationModal );
  94. //initialize directory from settings, then listen for changes
  95. QSettings settings;
  96. if ( settings.value("DatabaseDirectory", "") == "" )
  97. {
  98. QString directory = QString("./ctkDICOM-Database");
  99. settings.setValue("DatabaseDirectory", directory);
  100. settings.sync();
  101. }
  102. QString databaseDirectory = settings.value("DatabaseDirectory").toString();
  103. this->setDatabaseDirectory(databaseDirectory);
  104. d->directoryButton->setDirectory(databaseDirectory);
  105. connect(d->directoryButton, SIGNAL(directoryChanged(const QString&)), this, SLOT(setDatabaseDirectory(const QString&)));
  106. //Initialize import widget
  107. d->importDIalog = new ctkFileDialog();
  108. QCheckBox* importCheckbox = new QCheckBox("Copy on import", d->importDIalog);
  109. d->importDIalog->setBottomWidget(importCheckbox);
  110. d->importDIalog->setFileMode(QFileDialog::Directory);
  111. d->importDIalog->setLabelText(QFileDialog::Accept,"Import");
  112. d->importDIalog->setWindowTitle("Import DICOM files from directory ...");
  113. d->importDIalog->setWindowModality(Qt::ApplicationModal);
  114. //connect signal and slots
  115. connect(d->treeView, SIGNAL(clicked(const QModelIndex&)), d->thumbnailsWidget, SLOT(onModelSelected(const QModelIndex &)));
  116. connect(d->treeView, SIGNAL(clicked(const QModelIndex&)), d->imagePreview, SLOT(onModelSelected(const QModelIndex &)));
  117. connect(d->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(onModelSelected(const QModelIndex &)));
  118. connect(d->thumbnailsWidget, SIGNAL(selected(const ctkDICOMThumbnailWidget&)), this, SLOT(onThumbnailSelected(const ctkDICOMThumbnailWidget&)));
  119. connect(d->thumbnailsWidget, SIGNAL(doubleClicked(const ctkDICOMThumbnailWidget&)), this, SLOT(onThumbnailDoubleClicked(const ctkDICOMThumbnailWidget&)));
  120. connect(d->importDIalog, SIGNAL(fileSelected(QString)),this,SLOT(onImportDirectory(QString)));
  121. connect(d->dicomDatabase.data(), SIGNAL( databaseChanged() ), &(d->dicomModel), SLOT( reset() ) );
  122. connect(d->QueryRetrieveWidget, SIGNAL( canceled() ), d->QueryRetrieveWidget, SLOT( hide() ) );
  123. connect(d->imagePreview, SIGNAL(requestNextImage()), this, SLOT(onNextImage()));
  124. connect(d->imagePreview, SIGNAL(requestPreviousImage()), this, SLOT(onPreviousImage()));
  125. connect(d->searchOption, SIGNAL(nameSearchTextChanged(QString)), &(d->dicomProxyModel), SLOT(setNameSearchText(QString)));
  126. connect(d->searchOption, SIGNAL(studySearchTextChanged(QString)), &(d->dicomProxyModel), SLOT(setStudySearchText(QString)));
  127. connect(d->searchOption, SIGNAL(seriesSearchTextChanged(QString)), &(d->dicomProxyModel), SLOT(setSeriesSearchText(QString)));
  128. connect(d->searchOption, SIGNAL(idSearchTextChanged(QString)), &(d->dicomProxyModel), SLOT(setIdSearchText(QString)));
  129. }
  130. //----------------------------------------------------------------------------
  131. ctkDICOMAppWidget::~ctkDICOMAppWidget()
  132. {
  133. Q_D(ctkDICOMAppWidget);
  134. d->QueryRetrieveWidget->deleteLater();
  135. d->importDIalog->deleteLater();
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkDICOMAppWidget::setDatabaseDirectory(const QString& directory)
  139. {
  140. Q_D(ctkDICOMAppWidget);
  141. QSettings settings;
  142. settings.setValue("DatabaseDirectory", directory);
  143. settings.sync();
  144. //close the active DICOM database
  145. d->dicomDatabase->closeDatabase();
  146. //open DICOM database on the directory
  147. QString databaseFileName = directory + QString("/ctkDICOM.sql");
  148. try { d->dicomDatabase->openDatabase( databaseFileName ); }
  149. catch (std::exception e)
  150. {
  151. std::cerr << "Database error: " << qPrintable(d->dicomDatabase->lastError()) << "\n";
  152. d->dicomDatabase->closeDatabase();
  153. return;
  154. }
  155. d->dicomModel.setDatabase(d->dicomDatabase->database());
  156. d->dicomModel.setDisplayLevel(ctkDICOMModel::SeriesType);
  157. d->treeView->resizeColumnToContents(0);
  158. //pass DICOM database instance to Import widget
  159. // d->importDIalog->setdicomDatabase(d->dicomDatabase);
  160. d->QueryRetrieveWidget->setRetrieveDatabase(d->dicomDatabase);
  161. // update the button and let any connected slots know about the change
  162. d->directoryButton->setDirectory(directory);
  163. d->thumbnailsWidget->setDatabaseDirectory(directory);
  164. d->imagePreview->setDatabaseDirectory(directory);
  165. emit databaseDirectoryChanged(directory);
  166. }
  167. //----------------------------------------------------------------------------
  168. QString ctkDICOMAppWidget::databaseDirectory() const
  169. {
  170. QSettings settings;
  171. return settings.value("DatabaseDirectory").toString();
  172. }
  173. void ctkDICOMAppWidget::onAddToDatabase()
  174. {
  175. //Q_D(ctkDICOMAppWidget);
  176. //d->
  177. }
  178. //----------------------------------------------------------------------------
  179. void ctkDICOMAppWidget::openImportDialog()
  180. {
  181. Q_D(ctkDICOMAppWidget);
  182. d->importDIalog->show();
  183. d->importDIalog->raise();
  184. }
  185. //----------------------------------------------------------------------------
  186. void ctkDICOMAppWidget::openExportDialog()
  187. {
  188. }
  189. //----------------------------------------------------------------------------
  190. void ctkDICOMAppWidget::openQueryDialog()
  191. {
  192. Q_D(ctkDICOMAppWidget);
  193. d->QueryRetrieveWidget->show();
  194. d->QueryRetrieveWidget->raise();
  195. }
  196. //----------------------------------------------------------------------------
  197. void ctkDICOMAppWidget::onThumbnailSelected(const ctkDICOMThumbnailWidget& widget)
  198. {
  199. Q_D(ctkDICOMAppWidget);
  200. d->imagePreview->onModelSelected(widget.sourceIndex());
  201. }
  202. //----------------------------------------------------------------------------
  203. void ctkDICOMAppWidget::onThumbnailDoubleClicked(const ctkDICOMThumbnailWidget& widget)
  204. {
  205. Q_D(ctkDICOMAppWidget);
  206. logger.debug("double clicked");
  207. QModelIndex index = widget.sourceIndex();
  208. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(index.model()));
  209. QModelIndex index0 = index.sibling(index.row(), 0);
  210. if(model && (model->data(index0,ctkDICOMModel::TypeRole) != ctkDICOMModel::ImageType)){
  211. this->onModelSelected(index0);
  212. d->treeView->setCurrentIndex(index0);
  213. d->thumbnailsWidget->onModelSelected(index0);
  214. d->imagePreview->onModelSelected(index0);
  215. }
  216. }
  217. //----------------------------------------------------------------------------
  218. void ctkDICOMAppWidget::onImportDirectory(QString directory)
  219. {
  220. Q_D(ctkDICOMAppWidget);
  221. if (QDir(directory).exists())
  222. {
  223. QCheckBox* copyOnImport = qobject_cast<QCheckBox*>(d->importDIalog->bottomWidget());
  224. QString targetDirectory;
  225. if (copyOnImport->isEnabled())
  226. {
  227. targetDirectory = d->dicomDatabase->databaseDirectory();
  228. }
  229. d->dicomIndexer->addDirectory(*d->dicomDatabase,directory,targetDirectory);
  230. d->dicomModel.reset();
  231. }
  232. }
  233. //----------------------------------------------------------------------------
  234. void ctkDICOMAppWidget::onModelSelected(const QModelIndex &index){
  235. Q_D(ctkDICOMAppWidget);
  236. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(index.model()));
  237. if(model){
  238. QModelIndex index0 = index.sibling(index.row(), 0);
  239. if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::PatientType ){
  240. d->nextImageButton->show();
  241. d->prevImageButton->show();
  242. d->nextSeriesButton->show();
  243. d->prevSeriesButton->show();
  244. d->nextStudyButton->show();
  245. d->prevStudyButton->show();
  246. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::StudyType ){
  247. d->nextImageButton->show();
  248. d->prevImageButton->show();
  249. d->nextSeriesButton->show();
  250. d->prevSeriesButton->show();
  251. d->nextStudyButton->hide();
  252. d->prevStudyButton->hide();
  253. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::SeriesType ){
  254. d->nextImageButton->show();
  255. d->prevImageButton->show();
  256. d->nextSeriesButton->hide();
  257. d->prevSeriesButton->hide();
  258. d->nextStudyButton->hide();
  259. d->prevStudyButton->hide();
  260. }else{
  261. d->nextImageButton->hide();
  262. d->prevImageButton->hide();
  263. d->nextSeriesButton->hide();
  264. d->prevSeriesButton->hide();
  265. d->nextStudyButton->hide();
  266. d->prevStudyButton->hide();
  267. }
  268. }else{
  269. d->nextImageButton->hide();
  270. d->prevImageButton->hide();
  271. d->nextSeriesButton->hide();
  272. d->prevSeriesButton->hide();
  273. d->nextStudyButton->hide();
  274. d->prevStudyButton->hide();
  275. }
  276. }
  277. //----------------------------------------------------------------------------
  278. void ctkDICOMAppWidget::onNextImage(){
  279. Q_D(ctkDICOMAppWidget);
  280. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  281. if(currentIndex.isValid()){
  282. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  283. if(model){
  284. QModelIndex seriesIndex = currentIndex.parent();
  285. int imageCount = model->rowCount(seriesIndex);
  286. int imageID = currentIndex.row();
  287. imageID = (imageID+1)%imageCount;
  288. QModelIndex nextIndex = currentIndex.sibling(imageID, 0);
  289. d->imagePreview->onModelSelected(nextIndex);
  290. d->thumbnailsWidget->selectThumbnail(nextIndex);
  291. }
  292. }
  293. }
  294. //----------------------------------------------------------------------------
  295. void ctkDICOMAppWidget::onPreviousImage(){
  296. Q_D(ctkDICOMAppWidget);
  297. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  298. if(currentIndex.isValid()){
  299. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  300. if(model){
  301. QModelIndex seriesIndex = currentIndex.parent();
  302. int imageCount = model->rowCount(seriesIndex);
  303. int imageID = currentIndex.row();
  304. imageID--;
  305. if(imageID < 0) imageID += imageCount;
  306. QModelIndex prevIndex = currentIndex.sibling(imageID, 0);
  307. d->imagePreview->onModelSelected(prevIndex);
  308. d->thumbnailsWidget->selectThumbnail(prevIndex);
  309. }
  310. }
  311. }
  312. //----------------------------------------------------------------------------
  313. void ctkDICOMAppWidget::onNextSeries(){
  314. Q_D(ctkDICOMAppWidget);
  315. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  316. if(currentIndex.isValid()){
  317. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  318. if(model){
  319. QModelIndex seriesIndex = currentIndex.parent();
  320. QModelIndex studyIndex = seriesIndex.parent();
  321. int seriesCount = model->rowCount(studyIndex);
  322. int seriesID = seriesIndex.row();
  323. seriesID = (seriesID + 1)%seriesCount;
  324. QModelIndex nextIndex = seriesIndex.sibling(seriesID, 0);
  325. d->imagePreview->onModelSelected(nextIndex);
  326. d->thumbnailsWidget->selectThumbnail(nextIndex);
  327. }
  328. }
  329. }
  330. //----------------------------------------------------------------------------
  331. void ctkDICOMAppWidget::onPreviousSeries(){
  332. Q_D(ctkDICOMAppWidget);
  333. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  334. if(currentIndex.isValid()){
  335. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  336. if(model){
  337. QModelIndex seriesIndex = currentIndex.parent();
  338. QModelIndex studyIndex = seriesIndex.parent();
  339. int seriesCount = model->rowCount(studyIndex);
  340. int seriesID = seriesIndex.row();
  341. seriesID--;
  342. if(seriesID < 0) seriesID += seriesCount;
  343. QModelIndex prevIndex = seriesIndex.sibling(seriesID, 0);
  344. d->imagePreview->onModelSelected(prevIndex);
  345. d->thumbnailsWidget->selectThumbnail(prevIndex);
  346. }
  347. }
  348. }
  349. //----------------------------------------------------------------------------
  350. void ctkDICOMAppWidget::onNextStudy(){
  351. Q_D(ctkDICOMAppWidget);
  352. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  353. if(currentIndex.isValid()){
  354. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  355. if(model){
  356. QModelIndex seriesIndex = currentIndex.parent();
  357. QModelIndex studyIndex = seriesIndex.parent();
  358. QModelIndex patientIndex = studyIndex.parent();
  359. int studyCount = model->rowCount(patientIndex);
  360. int studyID = studyIndex.row();
  361. studyID = (studyID + 1)%studyCount;
  362. QModelIndex nextIndex = studyIndex.sibling(studyID, 0);
  363. d->imagePreview->onModelSelected(nextIndex);
  364. d->thumbnailsWidget->selectThumbnail(nextIndex);
  365. }
  366. }
  367. }
  368. //----------------------------------------------------------------------------
  369. void ctkDICOMAppWidget::onPreviousStudy(){
  370. Q_D(ctkDICOMAppWidget);
  371. QModelIndex currentIndex = d->imagePreview->currentImageIndex();
  372. if(currentIndex.isValid()){
  373. ctkDICOMFilterProxyModel* model = const_cast<ctkDICOMFilterProxyModel*>(qobject_cast<const ctkDICOMFilterProxyModel*>(currentIndex.model()));
  374. if(model){
  375. QModelIndex seriesIndex = currentIndex.parent();
  376. QModelIndex studyIndex = seriesIndex.parent();
  377. QModelIndex patientIndex = studyIndex.parent();
  378. int studyCount = model->rowCount(patientIndex);
  379. int studyID = studyIndex.row();
  380. studyID--;
  381. if(studyID < 0) studyID += studyCount;
  382. QModelIndex prevIndex = studyIndex.sibling(studyID, 0);
  383. d->imagePreview->onModelSelected(prevIndex);
  384. d->thumbnailsWidget->selectThumbnail(prevIndex);
  385. }
  386. }
  387. }
  388. //----------------------------------------------------------------------------
  389. void ctkDICOMAppWidget::onTreeCollapsed(const QModelIndex &index){
  390. Q_D(ctkDICOMAppWidget);
  391. d->treeView->resizeColumnToContents(0);
  392. }
  393. //----------------------------------------------------------------------------
  394. void ctkDICOMAppWidget::onTreeExpanded(const QModelIndex &index){
  395. Q_D(ctkDICOMAppWidget);
  396. d->treeView->resizeColumnToContents(0);
  397. }