ctkDICOMModelObject.cpp 7.9 KB

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