ctkDICOMAppWidget.cpp 18 KB

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