ctkDICOMObjectModel.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) Brigham and Women's Hospital (BWH).
  4. Copyright (c) University of Sheffield.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // Qt include
  16. #include <QSharedData>
  17. #include <QStandardItem>
  18. #include <QString>
  19. #include <QStringList>
  20. // DCMTK includes
  21. #include "dcmtk/dcmdata/dcdeftag.h"
  22. #include "dcmtk/dcmdata/dcdatset.h"
  23. #include "dcmtk/dcmdata/dcfilefo.h"
  24. #include "dcmtk/dcmdata/dcmetinf.h"
  25. #include "dcmtk/dcmdata/dcitem.h"
  26. #include "dcmtk/ofstd/ofcond.h"
  27. #include "dcmtk/ofstd/ofstring.h"
  28. #include "dcmtk/ofstd/ofstd.h"
  29. // CTK DICOM Core
  30. #include "ctkDICOMObjectModel.h"
  31. //------------------------------------------------------------------------------
  32. class ctkDICOMObjectModelPrivate
  33. {
  34. Q_DECLARE_PUBLIC(ctkDICOMObjectModel);
  35. protected:
  36. ctkDICOMObjectModel* const q_ptr;
  37. public:
  38. ctkDICOMObjectModelPrivate(ctkDICOMObjectModel&);
  39. virtual ~ctkDICOMObjectModelPrivate();
  40. void init();
  41. void itemInsert( DcmItem *dataset, QStandardItem *parent);
  42. void seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent);
  43. QString getTagValue( DcmElement *dcmElem);
  44. QStandardItem* populateModelRow(const QString& tagName,const QString& tagHexName,
  45. const QString& tagValue, const QString& VRName,
  46. const QString& elementLengthQString, QStandardItem *parent);
  47. DcmFileFormat fileFormat;
  48. QStandardItem *rootItem;
  49. };
  50. //------------------------------------------------------------------------------
  51. ctkDICOMObjectModelPrivate::ctkDICOMObjectModelPrivate(ctkDICOMObjectModel& o):q_ptr(&o)
  52. {
  53. }
  54. //------------------------------------------------------------------------------
  55. ctkDICOMObjectModelPrivate::~ctkDICOMObjectModelPrivate()
  56. {
  57. }
  58. //------------------------------------------------------------------------------
  59. void ctkDICOMObjectModelPrivate::init()
  60. {
  61. Q_Q(ctkDICOMObjectModel);
  62. QStringList horizontalHeaderLabels;
  63. horizontalHeaderLabels.append( QString("Tag"));
  64. horizontalHeaderLabels.append( QString("Attribute"));
  65. horizontalHeaderLabels.append( QString("Value"));
  66. horizontalHeaderLabels.append( QString("VR"));
  67. horizontalHeaderLabels.append( QString("Length"));
  68. q->setHorizontalHeaderLabels(horizontalHeaderLabels);
  69. }
  70. //------------------------------------------------------------------------------
  71. void ctkDICOMObjectModelPrivate::itemInsert( DcmItem *dataset, QStandardItem *parent)
  72. {
  73. DcmStack stack;
  74. dataset->nextObject( stack, OFTrue);
  75. for( ; stack.top(); dataset->nextObject( stack, OFFalse))
  76. {
  77. DcmObject *dO = stack.top();
  78. // put in the visit node function
  79. QString tagValue = "";
  80. DcmTag tag = dO->getTag();
  81. // std::cout<<tag;
  82. QString tagName = tag.getTagName();
  83. //
  84. DcmTagKey tagX = tag.getXTag();
  85. QString tagHexName = tagX.toString().c_str();
  86. //
  87. DcmVR VR = dO->getVR();
  88. QString VRName = VR.getVRName();
  89. //
  90. // Getting length
  91. int elementLength;
  92. elementLength = dO->getLength();
  93. QString elementLengthQString = QString::number(elementLength);
  94. //
  95. DcmTag tagKey = tag.getXTag();
  96. if( tagKey == DCM_SequenceDelimitationItem
  97. || tagKey == DCM_ItemDelimitationItem
  98. || "Item" == tagName)
  99. {
  100. return;
  101. }
  102. DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
  103. tagValue = getTagValue(dcmElem);
  104. // Populate QStandardModel with current DICOM element tag name and value
  105. QStandardItem *tagItem = populateModelRow(tagName,tagHexName,tagValue,VRName,elementLengthQString,parent);
  106. // Check if the DICOM object is a SQ Data element and extract the nested DICOM objects
  107. if( dcmElem && !dcmElem->isLeaf())
  108. {
  109. // now dcmElem points to a sequence of items
  110. ctkDICOMObjectModelPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
  111. }
  112. }
  113. }
  114. //------------------------------------------------------------------------------
  115. void ctkDICOMObjectModelPrivate::seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent)
  116. {
  117. DcmObject *dO = dataset->nextInContainer(NULL);
  118. for( ; dO; dO = dataset->nextInContainer(dO))
  119. {
  120. DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
  121. QString tagValue = "";
  122. DcmTag tag = dO->getTag();
  123. DcmTag tagKey = tag.getXTag();
  124. if( tagKey == DCM_SequenceDelimitationItem
  125. || tagKey == DCM_ItemDelimitationItem)
  126. {
  127. return;
  128. }
  129. QString tagName = tag.getTagName();
  130. DcmTagKey tagX = tag.getXTag();
  131. QString tagHexName = tagX.toString().c_str();
  132. DcmVR VR = dO->getVR();
  133. QString VRName = VR.getVRName();
  134. // Getting length
  135. int elementLength;
  136. elementLength = dO->getLength();
  137. QString elementLengthQString = QString::number(elementLength);
  138. if( dcmElem)
  139. {
  140. tagValue = getTagValue(dcmElem);
  141. }
  142. QStandardItem *tagItem = populateModelRow(tagName,tagHexName,tagValue,VRName,elementLengthQString,parent);
  143. if( dcmElem && !dcmElem->isLeaf())
  144. {
  145. ctkDICOMObjectModelPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
  146. }
  147. else if( tag.getXTag() == DCM_Item)
  148. {
  149. itemInsert( dynamic_cast<DcmItem*> (dO), tagItem);
  150. }
  151. }
  152. }
  153. //------------------------------------------------------------------------------
  154. QString ctkDICOMObjectModelPrivate::getTagValue( DcmElement *dcmElem)
  155. {
  156. QString tagValue = "";
  157. std::ostringstream value;
  158. OFString part;
  159. std::string sep;
  160. int mult = dcmElem->getVM();
  161. int pos;
  162. if( mult>1)
  163. {
  164. value << "[" << mult << "] ";
  165. }
  166. for( pos=0; pos < mult; pos++)
  167. {
  168. value << sep;
  169. OFCondition status = dcmElem->getOFString( part, pos);
  170. if( status.good())
  171. {
  172. value << part.c_str();
  173. sep = ", ";
  174. }
  175. }
  176. if( pos < mult-1)
  177. {
  178. value << " ...";
  179. }
  180. tagValue = value.str().c_str();
  181. return tagValue;
  182. }
  183. //------------------------------------------------------------------------------
  184. QStandardItem* ctkDICOMObjectModelPrivate::populateModelRow(const QString& tagName,
  185. const QString& tagHexName, const QString& tagValue,const QString& VRName,
  186. const QString& elementLengthQString, QStandardItem *parent)
  187. {
  188. // Create items
  189. QStandardItem *VRItem = new QStandardItem( VRName);
  190. QStandardItem *tagItem = new QStandardItem( tagName);
  191. QStandardItem *tagHexItem = new QStandardItem( tagHexName);
  192. QStandardItem *lengthItem = new QStandardItem( elementLengthQString);
  193. QStandardItem *valItem = new QStandardItem( tagValue);
  194. VRItem->setFlags(VRItem->flags() & ~Qt::ItemIsEditable);
  195. tagItem->setFlags(tagItem->flags() & ~Qt::ItemIsEditable);
  196. tagHexItem->setFlags(tagHexItem->flags() & ~Qt::ItemIsEditable);
  197. lengthItem->setFlags(lengthItem->flags() & ~Qt::ItemIsEditable);
  198. valItem->setFlags(valItem->flags() & ~Qt::ItemIsEditable);
  199. // Insert items
  200. QList<QStandardItem *> modelRow;
  201. modelRow.append( tagHexItem);
  202. modelRow.append( tagItem);
  203. modelRow.append( valItem);
  204. modelRow.append( VRItem);
  205. modelRow.append( lengthItem);
  206. parent->appendRow( modelRow);
  207. return tagHexItem;
  208. }
  209. //------------------------------------------------------------------------------
  210. ctkDICOMObjectModel::ctkDICOMObjectModel(QObject* parentObject)
  211. : Superclass(parentObject)
  212. , d_ptr(new ctkDICOMObjectModelPrivate(*this))
  213. {
  214. Q_D(ctkDICOMObjectModel);
  215. d->init();
  216. }
  217. //------------------------------------------------------------------------------
  218. ctkDICOMObjectModel::~ctkDICOMObjectModel()
  219. {
  220. }
  221. //------------------------------------------------------------------------------
  222. void ctkDICOMObjectModel::setFile(const QString &fileName)
  223. {
  224. Q_D(ctkDICOMObjectModel);
  225. OFCondition status = d->fileFormat.loadFile( fileName.toLatin1().data());
  226. if( !status.good() )
  227. {
  228. // TODO: Through an error message.
  229. }
  230. DcmDataset *dataset = d->fileFormat.getDataset();
  231. d->rootItem = this->invisibleRootItem();
  232. if(d->rootItem->hasChildren())
  233. {
  234. d->rootItem->removeRows(0, d->rootItem->rowCount());
  235. }
  236. d->itemInsert( dataset, d->rootItem);
  237. }