ctkDICOMThumbnailListWidget.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // Qt include
  15. #include <QDateTime>
  16. #include <QDir>
  17. #include <QFile>
  18. #include <QFileInfo>
  19. #include <QGridLayout>
  20. #include <QMetaType>
  21. #include <QPersistentModelIndex>
  22. #include <QPixmap>
  23. #include <QPushButton>
  24. #include <QResizeEvent>
  25. // ctk includes
  26. #include "ctkLogger.h"
  27. // ctkWidgets includes
  28. #include "ctkFlowLayout.h"
  29. #include "ctkThumbnailListWidget_p.h"
  30. #include "ui_ctkThumbnailListWidget.h"
  31. //ctkDICOMCore includes
  32. #include "ctkDICOMDatabase.h"
  33. #include "ctkDICOMFilterProxyModel.h"
  34. #include "ctkDICOMModel.h"
  35. // ctkDICOMWidgets includes
  36. #include "ctkDICOMThumbnailListWidget.h"
  37. #include "ctkThumbnailLabel.h"
  38. // STD includes
  39. #include <iostream>
  40. // DCMTK includes
  41. #include <dcmtk/dcmimgle/dcmimage.h>
  42. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMThumbnailListWidget");
  43. Q_DECLARE_METATYPE(QPersistentModelIndex);
  44. //----------------------------------------------------------------------------
  45. class ctkDICOMThumbnailListWidgetPrivate : ctkThumbnailListWidgetPrivate
  46. {
  47. Q_DECLARE_PUBLIC(ctkDICOMThumbnailListWidget);
  48. public:
  49. typedef ctkThumbnailListWidgetPrivate Superclass;
  50. ctkDICOMThumbnailListWidgetPrivate(ctkDICOMThumbnailListWidget* parent);
  51. QString DatabaseDirectory;
  52. QModelIndex CurrentSelectedModel;
  53. void addThumbnailWidget(const QModelIndex &imageIndex, const QModelIndex& sourceIndex, const QString& text);
  54. void addPatientThumbnails(const QModelIndex& patientIndex);
  55. void addStudyThumbnails(const QModelIndex& studyIndex);
  56. void addSeriesThumbnails(const QModelIndex& seriesIndex);
  57. private:
  58. Q_DISABLE_COPY( ctkDICOMThumbnailListWidgetPrivate );
  59. };
  60. //----------------------------------------------------------------------------
  61. // ctkDICOMThumbnailListWidgetPrivate methods
  62. //----------------------------------------------------------------------------
  63. ctkDICOMThumbnailListWidgetPrivate
  64. ::ctkDICOMThumbnailListWidgetPrivate(ctkDICOMThumbnailListWidget* parent)
  65. : Superclass(parent)
  66. {
  67. }
  68. //----------------------------------------------------------------------------
  69. void ctkDICOMThumbnailListWidgetPrivate
  70. ::addPatientThumbnails(const QModelIndex &index)
  71. {
  72. QModelIndex patientIndex = index;
  73. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(
  74. qobject_cast<const ctkDICOMModel*>(index.model()));
  75. if(model)
  76. {
  77. model->fetchMore(patientIndex);
  78. const int studyCount = model->rowCount(patientIndex);
  79. for(int i=0; i<studyCount; i++)
  80. {
  81. QModelIndex studyIndex = patientIndex.child(i, 0);
  82. QModelIndex seriesIndex = studyIndex.child(0, 0);
  83. model->fetchMore(seriesIndex);
  84. const int imageCount = model->rowCount(seriesIndex);
  85. QModelIndex imageIndex = seriesIndex.child(imageCount/2, 0);
  86. QString study = model->data(studyIndex, Qt::DisplayRole).toString();
  87. this->addThumbnailWidget(imageIndex, studyIndex, study);
  88. }
  89. }
  90. }
  91. //----------------------------------------------------------------------------
  92. void ctkDICOMThumbnailListWidgetPrivate
  93. ::addStudyThumbnails(const QModelIndex &index)
  94. {
  95. QModelIndex studyIndex = index;
  96. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  97. if (!model)
  98. {
  99. return;
  100. }
  101. model->fetchMore(studyIndex);
  102. const int seriesCount = model->rowCount(studyIndex);
  103. for(int i=0; i<seriesCount; i++)
  104. {
  105. QModelIndex seriesIndex = studyIndex.child(i, 0);
  106. model->fetchMore(seriesIndex);
  107. int imageCount = model->rowCount(seriesIndex);
  108. QModelIndex imageIndex = seriesIndex.child(imageCount/2, 0);
  109. this->addThumbnailWidget(imageIndex, seriesIndex, model->data(seriesIndex, Qt::DisplayRole).toString());
  110. }
  111. }
  112. //----------------------------------------------------------------------------
  113. void ctkDICOMThumbnailListWidgetPrivate
  114. ::addSeriesThumbnails(const QModelIndex &index)
  115. {
  116. QModelIndex studyIndex = index.parent();
  117. QModelIndex seriesIndex = index;
  118. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  119. if (!model)
  120. {
  121. return;
  122. }
  123. model->fetchMore(seriesIndex);
  124. const int imageCount = model->rowCount(seriesIndex);
  125. logger.debug(QString("Thumbs: %1").arg(imageCount));
  126. for (int i = 0 ; i < imageCount ; i++ )
  127. {
  128. QModelIndex imageIndex = seriesIndex.child(i,0);
  129. this->addThumbnailWidget(imageIndex, imageIndex, QString("Image %1").arg(i));
  130. }
  131. }
  132. //----------------------------------------------------------------------------
  133. void ctkDICOMThumbnailListWidgetPrivate
  134. ::addThumbnailWidget(const QModelIndex& imageIndex,
  135. const QModelIndex& sourceIndex, const QString &text)
  136. {
  137. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(
  138. qobject_cast<const ctkDICOMModel*>(imageIndex.model()));
  139. if(!model)
  140. {
  141. return;
  142. }
  143. QModelIndex seriesIndex = imageIndex.parent();
  144. QModelIndex studyIndex = seriesIndex.parent();
  145. QString thumbnailPath = this->DatabaseDirectory +
  146. "/thumbs/" + model->data(studyIndex ,ctkDICOMModel::UIDRole).toString() + "/" +
  147. model->data(seriesIndex ,ctkDICOMModel::UIDRole).toString() + "/" +
  148. model->data(imageIndex, ctkDICOMModel::UIDRole).toString() + ".png";
  149. if(!QFileInfo(thumbnailPath).exists())
  150. {
  151. return;
  152. }
  153. ctkThumbnailLabel* widget = new ctkThumbnailLabel(this->ScrollAreaContentWidget);
  154. QString widgetLabel = text;
  155. widget->setText( widgetLabel );
  156. QPixmap pix(thumbnailPath);
  157. logger.debug("Setting pixmap to " + thumbnailPath);
  158. if(this->ThumbnailSize.isValid())
  159. {
  160. widget->setFixedSize(this->ThumbnailSize);
  161. }
  162. widget->setPixmap(pix);
  163. QVariant var;
  164. var.setValue(QPersistentModelIndex(sourceIndex));
  165. widget->setProperty("sourceIndex", var);
  166. this->addThumbnail(widget);
  167. }
  168. //----------------------------------------------------------------------------
  169. // ctkDICOMThumbnailListWidget methods
  170. //----------------------------------------------------------------------------
  171. ctkDICOMThumbnailListWidget::ctkDICOMThumbnailListWidget(QWidget* _parent)
  172. : Superclass(new ctkDICOMThumbnailListWidgetPrivate(this), _parent)
  173. {
  174. }
  175. //----------------------------------------------------------------------------
  176. ctkDICOMThumbnailListWidget::~ctkDICOMThumbnailListWidget()
  177. {
  178. }
  179. //----------------------------------------------------------------------------
  180. void ctkDICOMThumbnailListWidget::setDatabaseDirectory(const QString &directory){
  181. Q_D(ctkDICOMThumbnailListWidget);
  182. d->DatabaseDirectory = directory;
  183. }
  184. //----------------------------------------------------------------------------
  185. void ctkDICOMThumbnailListWidget::selectThumbnailFromIndex(const QModelIndex &index){
  186. Q_D(ctkDICOMThumbnailListWidget);
  187. if(!d->CurrentSelectedModel.isValid())
  188. {
  189. return;
  190. }
  191. if(index.parent() != d->CurrentSelectedModel)
  192. {
  193. return;
  194. }
  195. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  196. if(model)
  197. {
  198. int count = d->ScrollAreaContentWidget->layout()->count();
  199. for(int i=0; i<count; i++)
  200. {
  201. ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(d->ScrollAreaContentWidget->layout()->itemAt(i)->widget());
  202. if(thumbnailWidget->property("sourceIndex").value<QPersistentModelIndex>() == index)
  203. {
  204. thumbnailWidget->setSelected(true);
  205. d->ScrollArea->ensureWidgetVisible(thumbnailWidget);
  206. }
  207. else
  208. {
  209. thumbnailWidget->setSelected(false);
  210. }
  211. }
  212. }
  213. }
  214. //----------------------------------------------------------------------------
  215. void ctkDICOMThumbnailListWidget::addThumbnails(const QModelIndex &index)
  216. {
  217. Q_D(ctkDICOMThumbnailListWidget);
  218. this->clearThumbnails();
  219. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  220. if(model)
  221. {
  222. QModelIndex index0 = index.sibling(index.row(), 0);
  223. d->CurrentSelectedModel = index0;
  224. if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::PatientType) )
  225. {
  226. d->addPatientThumbnails(index0);
  227. }
  228. else if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::StudyType) )
  229. {
  230. d->addStudyThumbnails(index0);
  231. }
  232. else if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType) )
  233. {
  234. d->addSeriesThumbnails(index0);
  235. }
  236. }
  237. this->setCurrentThumbnail(0);
  238. }