ctkDICOMObjectModel.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) University of Sheffield
  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
  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 <QSharedData>
  16. #include <QStandardItem>
  17. #include <QString>
  18. #include <QStringList>
  19. // DCMTK includes
  20. #include "dcmtk/dcmdata/dcdeftag.h"
  21. #include "dcmtk/dcmdata/dcdatset.h"
  22. #include "dcmtk/dcmdata/dcfilefo.h"
  23. #include "dcmtk/dcmdata/dcmetinf.h"
  24. #include "dcmtk/dcmdata/dcitem.h"
  25. #include "dcmtk/ofstd/ofcond.h"
  26. #include "dcmtk/ofstd/ofstring.h"
  27. #include "dcmtk/ofstd/ofstd.h" /* for class OFStandard */
  28. // CTK DICOM Core
  29. #include "ctkDICOMObjectModel.h"
  30. //------------------------------------------------------------------------------
  31. class ctkDICOMObjectModelPrivate
  32. {
  33. Q_DECLARE_PUBLIC(ctkDICOMObjectModel);
  34. protected:
  35. ctkDICOMObjectModel* const q_ptr;
  36. public:
  37. ctkDICOMObjectModelPrivate(ctkDICOMObjectModel&);
  38. virtual ~ctkDICOMObjectModelPrivate();
  39. void init();
  40. void ctkDICOMObjectModelPrivate::traverseDataElements( DcmItem *dataset, QStandardItem *parent);
  41. QString ctkDICOMObjectModelPrivate::getTagValue( DcmElement *dcmElem);
  42. QStandardItem* ctkDICOMObjectModelPrivate::populateModelRow(const QString& tagName,const QString& tagValue, QStandardItem *parent);
  43. DcmFileFormat fileFormat;
  44. QStandardItem *rootItem;
  45. };
  46. //------------------------------------------------------------------------------
  47. ctkDICOMObjectModelPrivate::ctkDICOMObjectModelPrivate(ctkDICOMObjectModel& o):q_ptr(&o)
  48. {
  49. }
  50. //------------------------------------------------------------------------------
  51. ctkDICOMObjectModelPrivate::~ctkDICOMObjectModelPrivate()
  52. {
  53. }
  54. //------------------------------------------------------------------------------
  55. void ctkDICOMObjectModelPrivate::init()
  56. {
  57. Q_Q(ctkDICOMObjectModel);
  58. QStringList horizontalHeaderLabels;
  59. horizontalHeaderLabels.append( QString("Tag"));
  60. horizontalHeaderLabels.append( QString("Value"));
  61. q->setHorizontalHeaderLabels(horizontalHeaderLabels);
  62. }
  63. //------------------------------------------------------------------------------
  64. void ctkDICOMObjectModelPrivate::traverseDataElements( DcmItem *dataset, QStandardItem *parent)
  65. {
  66. DcmStack stack;
  67. dataset->nextObject( stack, OFTrue);
  68. for( ; stack.top(); dataset->nextObject( stack, OFFalse))
  69. {
  70. DcmObject *dO = stack.top();
  71. // put in the visit node function
  72. QString tagValue = "";
  73. DcmTag tag = dO->getTag();
  74. // std::cout<<tag;
  75. QString tagName = tag.getTagName();
  76. DcmTag tagKey = tag.getXTag();
  77. // std::cout<< tagName.toUtf8().constData()<<std::endl;
  78. if( tagKey == DCM_SequenceDelimitationItem
  79. || tagKey == DCM_ItemDelimitationItem
  80. || "Item" == tagName)
  81. {
  82. return;
  83. }
  84. DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
  85. tagValue = getTagValue(dcmElem);
  86. // Populate QStandardModel with current DICOM element tag name and value
  87. QStandardItem *tagItem = populateModelRow(tagName,tagValue,parent);
  88. // check if the DICOM object is a SQ Data element and extract the nested DICOM objects
  89. if( dcmElem && !dcmElem->isLeaf())
  90. {
  91. DcmSequenceOfItems* newNode = dynamic_cast<DcmSequenceOfItems*> (dcmElem);
  92. dO = newNode->nextInContainer( NULL);
  93. for( ; dO; dO = newNode->nextInContainer( dO))
  94. {
  95. DcmElement *dcmElem2 = dynamic_cast<DcmElement *> (dO);
  96. tag = dO->getTag();
  97. if( tag.getXTag() == DCM_Item)
  98. {
  99. traverseDataElements( dynamic_cast<DcmItem*> (dO),parent);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. //------------------------------------------------------------------------------
  106. QString ctkDICOMObjectModelPrivate::getTagValue( DcmElement *dcmElem)
  107. {
  108. QString tagValue = "";
  109. std::ostringstream value;
  110. OFString part;
  111. std::string sep;
  112. int mult = dcmElem->getVM();
  113. int pos;
  114. if( mult>1)
  115. {
  116. value << "[" << mult << "] ";
  117. }
  118. // TODO define max elem per line
  119. for( pos=0; pos < mult; pos++)
  120. {
  121. value << sep;
  122. OFCondition status = dcmElem->getOFString( part, pos);
  123. if( status.good())
  124. {
  125. value << part.c_str();
  126. sep = ", ";
  127. }
  128. }
  129. if( pos < mult-1)
  130. {
  131. value << " ...";
  132. }
  133. tagValue = value.str().c_str();
  134. return tagValue;
  135. }
  136. //------------------------------------------------------------------------------
  137. QStandardItem* ctkDICOMObjectModelPrivate::populateModelRow(const QString& tagName,const QString& tagValue, QStandardItem *parent)
  138. {
  139. // Create items
  140. QStandardItem *tagItem = new QStandardItem( tagName);
  141. QStandardItem *valItem = new QStandardItem( tagValue);
  142. // Insert items
  143. QList<QStandardItem *> modelRow;
  144. modelRow.append( tagItem);
  145. modelRow.append( valItem);
  146. parent->appendRow( modelRow);
  147. return tagItem;
  148. }
  149. //------------------------------------------------------------------------------
  150. ctkDICOMObjectModel::ctkDICOMObjectModel(QObject* parentObject)
  151. : Superclass(parentObject)
  152. , d_ptr(new ctkDICOMObjectModelPrivate(*this))
  153. {
  154. Q_D(ctkDICOMObjectModel);
  155. d->init();
  156. }
  157. //------------------------------------------------------------------------------
  158. ctkDICOMObjectModel::ctkDICOMObjectModel(const ctkDICOMObjectModel& other)
  159. {
  160. }
  161. //------------------------------------------------------------------------------
  162. ctkDICOMObjectModel::~ctkDICOMObjectModel()
  163. {
  164. }
  165. //------------------------------------------------------------------------------
  166. void ctkDICOMObjectModel::setFile(const QString &fileName)
  167. {
  168. Q_D(ctkDICOMObjectModel);
  169. OFCondition status = d->fileFormat.loadFile( fileName.toLatin1().data());
  170. if( !status.good())
  171. {
  172. // TODO: Add through error
  173. }
  174. DcmDataset *dataset = d->fileFormat.getDataset();
  175. d->rootItem = ctkDICOMObjectModel::invisibleRootItem();
  176. d->traverseDataElements( dataset, d->rootItem);
  177. }