ctkDICOMDatasetView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include <iostream>
  15. // DCMTK includes
  16. #include <dcmimage.h>
  17. // CTK includes
  18. #include "ctkLogger.h"
  19. #include "ctkQImageView.h"
  20. #include "ctkDICOMModel.h"
  21. #include "ctkDICOMDatasetView.h"
  22. // Qt includes
  23. #include <QLabel>
  24. #include <QHBoxLayout>
  25. #include <QDebug>
  26. #include <QResizeEvent>
  27. #include <QMouseEvent>
  28. #include <QKeyEvent>
  29. #include <QPainter>
  30. #include <QFile>
  31. #include <QMouseEvent>
  32. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMDatasetView");
  33. //--------------------------------------------------------------------------
  34. class ctkDICOMDatasetViewPrivate
  35. {
  36. Q_DECLARE_PUBLIC( ctkDICOMDatasetView );
  37. protected:
  38. ctkDICOMDatasetView* const q_ptr;
  39. public:
  40. ctkDICOMDatasetViewPrivate( ctkDICOMDatasetView& object );
  41. QString databaseDirectory;
  42. QModelIndex currentImageIndex;
  43. QPoint oldMousePos;
  44. void init();
  45. void setImage(const QModelIndex& imageIndex);
  46. void onPatientModelSelected(const QModelIndex& index);
  47. void onStudyModelSelected(const QModelIndex& index);
  48. void onSeriesModelSelected(const QModelIndex& index);
  49. void onImageModelSelected(const QModelIndex& index);
  50. };
  51. //--------------------------------------------------------------------------
  52. ctkDICOMDatasetViewPrivate::ctkDICOMDatasetViewPrivate(
  53. ctkDICOMDatasetView& object )
  54. : q_ptr( & object )
  55. {
  56. }
  57. //--------------------------------------------------------------------------
  58. void ctkDICOMDatasetViewPrivate::init()
  59. {
  60. Q_Q( ctkDICOMDatasetView );
  61. q->setMouseTracking(true);
  62. /*
  63. this->Window->setParent(q);
  64. QHBoxLayout* layout = new QHBoxLayout(q);
  65. layout->addWidget(this->Window);
  66. layout->setContentsMargins(0,0,0,0);
  67. q->setLayout(layout);
  68. */
  69. }
  70. // -------------------------------------------------------------------------
  71. void ctkDICOMDatasetViewPrivate::setImage(const QModelIndex &imageIndex){
  72. Q_Q(ctkDICOMDatasetView);
  73. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(imageIndex.model()));
  74. if(model){
  75. QModelIndex seriesIndex = imageIndex.parent();
  76. QModelIndex studyIndex = seriesIndex.parent();
  77. QString dicomPath = this->databaseDirectory;
  78. dicomPath.append("/dicom/").append(model->data(studyIndex ,ctkDICOMModel::UIDRole).toString());
  79. dicomPath.append("/").append(model->data(seriesIndex ,ctkDICOMModel::UIDRole).toString());
  80. dicomPath.append("/").append(model->data(imageIndex ,ctkDICOMModel::UIDRole).toString());
  81. if (QFile(dicomPath).exists()){
  82. DicomImage dcmImage( dicomPath.toStdString().c_str() );
  83. q->clearImages();
  84. q->addImage( dcmImage );
  85. this->currentImageIndex = imageIndex;
  86. }else{
  87. q->clearImages();
  88. }
  89. }
  90. }
  91. // -------------------------------------------------------------------------
  92. void ctkDICOMDatasetViewPrivate::onPatientModelSelected(const QModelIndex &index){
  93. Q_Q(ctkDICOMDatasetView);
  94. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  95. if(model){
  96. QModelIndex patientIndex = index;
  97. model->fetchMore(patientIndex);
  98. QModelIndex studyIndex = patientIndex.child(0,0);
  99. model->fetchMore(studyIndex);
  100. QModelIndex seriesIndex = studyIndex.child(0,0);
  101. model->fetchMore(seriesIndex);
  102. int imageCount = model->rowCount(seriesIndex);
  103. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  104. this->setImage(imageIndex);
  105. }else{
  106. q->clearImages();
  107. }
  108. }
  109. // -------------------------------------------------------------------------
  110. void ctkDICOMDatasetViewPrivate::onStudyModelSelected(const QModelIndex &index){
  111. Q_Q(ctkDICOMDatasetView);
  112. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  113. if(model){
  114. QModelIndex studyIndex = index;
  115. model->fetchMore(studyIndex);
  116. QModelIndex seriesIndex = studyIndex.child(0,0);
  117. model->fetchMore(seriesIndex);
  118. int imageCount = model->rowCount(seriesIndex);
  119. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  120. this->setImage(imageIndex);
  121. }else{
  122. q->clearImages();
  123. }
  124. }
  125. // -------------------------------------------------------------------------
  126. void ctkDICOMDatasetViewPrivate::onSeriesModelSelected(const QModelIndex &index){
  127. Q_Q(ctkDICOMDatasetView);
  128. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  129. if(model){
  130. QModelIndex seriesIndex = index;
  131. model->fetchMore(seriesIndex);
  132. int imageCount = model->rowCount(seriesIndex);
  133. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  134. this->setImage(imageIndex);
  135. }else{
  136. q->clearImages();
  137. }
  138. }
  139. // -------------------------------------------------------------------------
  140. void ctkDICOMDatasetViewPrivate::onImageModelSelected(const QModelIndex &index){
  141. Q_Q(ctkDICOMDatasetView);
  142. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  143. if(model){
  144. QModelIndex imageIndex = index;
  145. this->setImage(imageIndex);
  146. }else{
  147. q->clearImages();
  148. }
  149. }
  150. // -------------------------------------------------------------------------
  151. ctkDICOMDatasetView::ctkDICOMDatasetView( QWidget* _parent )
  152. : Superclass( _parent ),
  153. d_ptr( new ctkDICOMDatasetViewPrivate( *this ) )
  154. {
  155. Q_D( ctkDICOMDatasetView );
  156. d->init();
  157. }
  158. // -------------------------------------------------------------------------
  159. ctkDICOMDatasetView::ctkDICOMDatasetView(
  160. ctkDICOMDatasetViewPrivate& pvt,
  161. QWidget* _parent)
  162. : Superclass(_parent), d_ptr(&pvt)
  163. {
  164. Q_D(ctkDICOMDatasetView);
  165. d->init();
  166. }
  167. // -------------------------------------------------------------------------
  168. ctkDICOMDatasetView::~ctkDICOMDatasetView()
  169. {
  170. }
  171. // -------------------------------------------------------------------------
  172. void ctkDICOMDatasetView::setDatabaseDirectory(const QString &directory){
  173. Q_D(ctkDICOMDatasetView);
  174. d->databaseDirectory = directory;
  175. }
  176. // -------------------------------------------------------------------------
  177. QModelIndex ctkDICOMDatasetView::currentImageIndex(){
  178. Q_D(ctkDICOMDatasetView);
  179. return d->currentImageIndex;
  180. }
  181. // -------------------------------------------------------------------------
  182. void ctkDICOMDatasetView::addImage( const QImage & image )
  183. {
  184. Superclass::addImage( image );
  185. }
  186. // -------------------------------------------------------------------------
  187. void ctkDICOMDatasetView::addImage( DicomImage & dcmImage )
  188. {
  189. QImage image;
  190. if ((dcmImage.getStatus() == EIS_Normal)){
  191. dcmImage.setWindow(0);
  192. /* get image extension */
  193. const unsigned long width = dcmImage.getWidth();
  194. const unsigned long height = dcmImage.getHeight();
  195. QString header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  196. const unsigned long offset = header.length();
  197. const unsigned long length = width * height + offset;
  198. /* create output buffer for DicomImage class */
  199. QByteArray buffer;
  200. buffer.append(header);
  201. buffer.resize(length);
  202. /* copy PGM header to buffer */
  203. if (dcmImage.getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0)){
  204. if (!image.loadFromData( buffer )){
  205. logger.error("QImage couldn't created");
  206. }
  207. }
  208. }
  209. this->addImage(image);
  210. }
  211. // -------------------------------------------------------------------------
  212. void ctkDICOMDatasetView::update( bool zoomChanged,
  213. bool sizeChanged )
  214. {
  215. Superclass::update( zoomChanged, sizeChanged );
  216. }
  217. // -------------------------------------------------------------------------
  218. void ctkDICOMDatasetView::mousePressEvent(QMouseEvent* event){
  219. Q_D(ctkDICOMDatasetView);
  220. event->accept();
  221. d->oldMousePos = event->pos();
  222. }
  223. // -------------------------------------------------------------------------
  224. void ctkDICOMDatasetView::mouseMoveEvent(QMouseEvent* event){
  225. Q_D(ctkDICOMDatasetView);
  226. if(event->buttons() == Qt::RightButton){
  227. event->accept();
  228. QPoint nowPos = event->pos();
  229. if(nowPos.y() > d->oldMousePos.y()){
  230. emit requestNextImage();
  231. d->oldMousePos = event->pos();
  232. }else if(nowPos.y() < d->oldMousePos.y()){
  233. emit requestPreviousImage();
  234. d->oldMousePos = event->pos();
  235. }
  236. }else if(event->buttons() == Qt::MidButton){
  237. event->accept();
  238. QPoint nowPos = event->pos();
  239. this->setZoom(this->zoom() - (nowPos.y()-d->oldMousePos.y())/100.0);
  240. d->oldMousePos = event->pos();
  241. }else if(event->buttons() == Qt::LeftButton){
  242. event->accept();
  243. QPoint nowPos = event->pos();
  244. double iW = this->intensityWindow();
  245. double iL = this->intensityLevel();
  246. this->setIntensityWindowLevel(iW + (nowPos.x()-d->oldMousePos.x()),
  247. iL - (nowPos.y()-d->oldMousePos.y()));
  248. d->oldMousePos = event->pos();
  249. }
  250. Superclass::mouseMoveEvent(event);
  251. }
  252. // -------------------------------------------------------------------------
  253. void ctkDICOMDatasetView::onModelSelected(const QModelIndex &index){
  254. Q_D(ctkDICOMDatasetView);
  255. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  256. if(model){
  257. QModelIndex index0 = index.sibling(index.row(), 0);
  258. if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::PatientType ){
  259. d->onPatientModelSelected(index0);
  260. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::StudyType ){
  261. d->onStudyModelSelected(index0);
  262. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::SeriesType ){
  263. d->onSeriesModelSelected(index0);
  264. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == ctkDICOMModel::ImageType ){
  265. d->onImageModelSelected(index0);
  266. }
  267. }
  268. }