ctkDICOMModelObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/dcfilefo.h"
  21. #include "dcmtk/dcmdata/dcmetinf.h"
  22. #include "dcmtk/dcmdata/dcdeftag.h"
  23. #include "dcmtk/dcmdata/dcdatset.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 "ctkDICOMModelObject.h"
  30. //------------------------------------------------------------------------------
  31. class ctkDICOMModelObjectPrivate
  32. {
  33. Q_DECLARE_PUBLIC(ctkDICOMModelObject);
  34. protected:
  35. ctkDICOMModelObject* const q_ptr;
  36. public:
  37. ctkDICOMModelObjectPrivate(ctkDICOMModelObject&);
  38. virtual ~ctkDICOMModelObjectPrivate();
  39. void init();
  40. void ctkDICOMModelObjectPrivate::itemInsert( DcmItem *dataset, QStandardItem *parent);
  41. void ctkDICOMModelObjectPrivate::seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent);
  42. QString ctkDICOMModelObjectPrivate::getTagValue( DcmElement *dcmElem);
  43. QStandardItem* ctkDICOMModelObjectPrivate::populateModelRow(const QString& tagName,const QString& tagValue, QStandardItem *parent);
  44. DcmFileFormat fileFormat;
  45. QStandardItem *rootItem;
  46. };
  47. //------------------------------------------------------------------------------
  48. ctkDICOMModelObjectPrivate::ctkDICOMModelObjectPrivate(ctkDICOMModelObject& o):q_ptr(&o)
  49. {
  50. }
  51. //------------------------------------------------------------------------------
  52. ctkDICOMModelObjectPrivate::~ctkDICOMModelObjectPrivate()
  53. {
  54. }
  55. //------------------------------------------------------------------------------
  56. void ctkDICOMModelObjectPrivate::init()
  57. {
  58. Q_Q(ctkDICOMModelObject);
  59. QStringList horizontalHeaderLabels;
  60. horizontalHeaderLabels.append( QString("Tag"));
  61. horizontalHeaderLabels.append( QString("Value"));
  62. q->setHorizontalHeaderLabels(horizontalHeaderLabels);
  63. }
  64. //------------------------------------------------------------------------------
  65. #undef max
  66. #undef min
  67. void ctkDICOMModelObjectPrivate::itemInsert( DcmItem *dataset, QStandardItem *parent)
  68. {
  69. DcmStack stack;
  70. dataset->nextObject( stack, OFTrue);
  71. for( ; stack.top(); dataset->nextObject( stack, OFFalse))
  72. {
  73. DcmObject *dO = stack.top();
  74. // DcmElement *dcmElem = dataset->getElement( idx);
  75. DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
  76. QString tagValue = "";
  77. DcmTag tag = dO->getTag();
  78. QString tagName = tag.getTagName();
  79. if( tag.getXTag() == DCM_SequenceDelimitationItem
  80. || tag.getXTag() == DCM_ItemDelimitationItem
  81. || "Item" == tagName)
  82. {
  83. return;
  84. }
  85. // std::cerr << "node is dcmElem=" << dcmElem << "\n";
  86. if( dcmElem)
  87. {
  88. tagValue = getTagValue(dcmElem);
  89. }
  90. // create items ...
  91. QStandardItem *tagItem = populateModelRow(tagName,tagValue,parent);
  92. // std::cerr << " "
  93. // << tagName.toStdString() << " " << tagValue.toStdString();
  94. if( dcmElem)
  95. {
  96. // std::cerr << " >> l=" << dcmElem->isLeaf()
  97. // << " nx=" << dataset->nextInContainer( dcmElem);
  98. if( !dcmElem->isLeaf())
  99. {
  100. // now dcmElem points to a sequenceOfItems
  101. ctkDICOMModelObjectPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
  102. }
  103. }
  104. // std::cerr << "\n";
  105. }
  106. }
  107. //------------------------------------------------------------------------------
  108. void ctkDICOMModelObjectPrivate::seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent)
  109. {
  110. // std::clog << "Entering seqInsert" << "\n";
  111. DcmObject *dO = dataset->nextInContainer( NULL);
  112. //std::clog << "Entered nested level d0=" << dO << "\n";
  113. //std::clog << "First node is dcmElem=" << dynamic_cast<DcmElement *> (dO) << "\n";
  114. for( ; dO; dO = dataset->nextInContainer(dO))
  115. {
  116. // DcmElement *dcmElem = dataset->getElement( idx);
  117. DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
  118. QString tagValue = "";
  119. DcmTag tag = dO->getTag();
  120. if( tag.getXTag() == DCM_SequenceDelimitationItem
  121. || tag.getXTag() == DCM_ItemDelimitationItem)
  122. {
  123. return;
  124. }
  125. QString tagName = tag.getTagName();
  126. // std::cerr << "node is dcmElem=" << dcmElem << "\n";
  127. if( dcmElem)
  128. {
  129. tagValue = getTagValue(dcmElem);
  130. }
  131. QStandardItem *tagItem = populateModelRow(tagName,tagValue,parent);
  132. // std::cerr << " "
  133. // << tagName.toStdString() << " " << tagValue.toStdString();
  134. if( dcmElem)
  135. {
  136. // std::cerr << " >> l=" << dcmElem->isLeaf()
  137. // << " nx=" << dataset->nextInContainer( dcmElem);
  138. if( !dcmElem->isLeaf())
  139. {
  140. // now dcmElem points to a sequenceOfItems
  141. ctkDICOMModelObjectPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
  142. }
  143. }
  144. else if( tag.getXTag() == DCM_Item)
  145. {
  146. itemInsert( dynamic_cast<DcmItem*> (dO), tagItem);
  147. }
  148. // std::cerr << "\n";
  149. }
  150. }
  151. //------------------------------------------------------------------------------
  152. QString ctkDICOMModelObjectPrivate::getTagValue( DcmElement *dcmElem)
  153. {
  154. QString tagValue = "";
  155. std::ostringstream value;
  156. OFString part;
  157. std::string sep;
  158. int mult = dcmElem->getVM();
  159. int pos;
  160. if( mult>1)
  161. {
  162. value << "[" << mult << "] ";
  163. }
  164. // TODO define max elem per line
  165. for( pos=0; pos < std::min(mult,10); pos++)
  166. {
  167. value << sep;
  168. OFCondition status = dcmElem->getOFString( part, pos);
  169. if( status.good())
  170. {
  171. value << part.c_str();
  172. sep = ", ";
  173. }
  174. }
  175. if( pos < mult-1)
  176. {
  177. value << " ...";
  178. }
  179. tagValue = value.str().c_str();
  180. return tagValue;
  181. }
  182. //------------------------------------------------------------------------------
  183. QStandardItem* ctkDICOMModelObjectPrivate::populateModelRow(const QString& tagName,const QString& tagValue, QStandardItem *parent)
  184. {
  185. // create items ...
  186. QStandardItem *tagItem = new QStandardItem( tagName);
  187. QStandardItem *valItem = new QStandardItem( tagValue);
  188. // ... and insert them
  189. QList<QStandardItem *> modelRow;
  190. modelRow.append( tagItem);
  191. modelRow.append( valItem);
  192. parent->appendRow( modelRow);
  193. return tagItem;
  194. }
  195. //------------------------------------------------------------------------------
  196. //------------------------------------------------------------------------------
  197. ctkDICOMModelObject::ctkDICOMModelObject(QObject* parentObject)
  198. : Superclass(parentObject)
  199. , d_ptr(new ctkDICOMModelObjectPrivate(*this))
  200. {
  201. Q_D(ctkDICOMModelObject);
  202. d->init();
  203. }
  204. //------------------------------------------------------------------------------
  205. ctkDICOMModelObject::ctkDICOMModelObject(const ctkDICOMModelObject& other)
  206. {
  207. }
  208. //------------------------------------------------------------------------------
  209. ctkDICOMModelObject::~ctkDICOMModelObject()
  210. {
  211. }
  212. //------------------------------------------------------------------------------
  213. void ctkDICOMModelObject::setFile(const QString &fileName)
  214. {
  215. Q_D(ctkDICOMModelObject);
  216. OFCondition status = d->fileFormat.loadFile( fileName.toLatin1().data());
  217. if( !status.good())
  218. {
  219. // TODO: Add through error
  220. }
  221. DcmDataset *dataset = d->fileFormat.getDataset();
  222. d->rootItem = ctkDICOMModelObject::invisibleRootItem();
  223. d->itemInsert( dataset, d->rootItem);
  224. }