123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- /*=============================================================================
- Library: CTK
- Copyright (c) University of Sheffield
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- // Qt include
- #include <QSharedData>
- #include <QStandardItem>
- #include <QString>
- #include <QStringList>
- // DCMTK includes
- #include "dcmtk/dcmdata/dcfilefo.h"
- #include "dcmtk/dcmdata/dcmetinf.h"
- #include "dcmtk/dcmdata/dcdeftag.h"
- #include "dcmtk/dcmdata/dcdatset.h"
- #include "dcmtk/dcmdata/dcitem.h"
- #include "dcmtk/ofstd/ofcond.h"
- #include "dcmtk/ofstd/ofstring.h"
- #include "dcmtk/ofstd/ofstd.h" /* for class OFStandard */
- // CTK DICOM Core
- #include "ctkDICOMModelObject.h"
- //------------------------------------------------------------------------------
- class ctkDICOMModelObjectPrivate
- {
-
- Q_DECLARE_PUBLIC(ctkDICOMModelObject);
- protected:
- ctkDICOMModelObject* const q_ptr;
- public:
- ctkDICOMModelObjectPrivate(ctkDICOMModelObject&);
- virtual ~ctkDICOMModelObjectPrivate();
- void init();
- void ctkDICOMModelObjectPrivate::itemInsert( DcmItem *dataset, QStandardItem *parent);
- void ctkDICOMModelObjectPrivate::seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent);
- DcmFileFormat fileFormat;
- QStandardItem *rootItem;
- };
- //------------------------------------------------------------------------------
- ctkDICOMModelObjectPrivate::ctkDICOMModelObjectPrivate(ctkDICOMModelObject& o):q_ptr(&o)
- {
- }
- //------------------------------------------------------------------------------
- ctkDICOMModelObjectPrivate::~ctkDICOMModelObjectPrivate()
- {
- }
- //------------------------------------------------------------------------------
- void ctkDICOMModelObjectPrivate::init()
- {
- Q_Q(ctkDICOMModelObject);
- QStringList horizontalHeaderLabels;
- horizontalHeaderLabels.append( QString("Tag"));
- horizontalHeaderLabels.append( QString("Value"));
- q->setHorizontalHeaderLabels(horizontalHeaderLabels);
-
- }
- //------------------------------------------------------------------------------
- #undef max
- #undef min
- void ctkDICOMModelObjectPrivate::itemInsert( DcmItem *dataset, QStandardItem *parent)
- {
- DcmStack stack;
- dataset->nextObject( stack, OFTrue);
- for( ; stack.top(); dataset->nextObject( stack, OFFalse))
- {
- DcmObject *dO = stack.top();
- // DcmElement *dcmElem = dataset->getElement( idx);
- DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
- QString tagValue = "";
- DcmTag tag = dO->getTag();
- QString tagName = tag.getTagName();
- if( tag.getXTag() == DCM_SequenceDelimitationItem
- || tag.getXTag() == DCM_ItemDelimitationItem
- || "Item" == tagName)
- {
- return;
- }
- // std::cerr << "node is dcmElem=" << dcmElem << "\n";
- if( dcmElem)
- {
- std::ostringstream value;
- OFString part;
- std::string sep;
- int mult = dcmElem->getVM();
- int pos;
- if( mult>1)
- {
- value << "[" << mult << "] ";
- }
- // TODO define max elem per line
- for( pos=0; pos < std::min(mult,10); pos++)
- {
- value << sep;
- OFCondition status = dcmElem->getOFString( part, pos);
- if( status.good())
- {
- value << part.c_str();
- sep = ", ";
- }
- }
- if( pos < mult-1)
- {
- value << " ...";
- }
- tagValue = value.str().c_str();
- }
- // create items ...
- QStandardItem *tagItem = new QStandardItem( tagName);
- QStandardItem *valItem = new QStandardItem( tagValue);
- // ... and insert them
- QList<QStandardItem *> modelRow;
- modelRow.append( tagItem);
- modelRow.append( valItem);
- parent->appendRow( modelRow);
- // std::cerr << " "
- // << tagName.toStdString() << " " << tagValue.toStdString();
- if( dcmElem)
- {
- // std::cerr << " >> l=" << dcmElem->isLeaf()
- // << " nx=" << dataset->nextInContainer( dcmElem);
- if( !dcmElem->isLeaf())
- {
- // now dcmElem points to a sequenceOfItems
- ctkDICOMModelObjectPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
- }
- }
- // std::cerr << "\n";
- }
- }
- //------------------------------------------------------------------------------
- void ctkDICOMModelObjectPrivate::seqInsert( DcmSequenceOfItems *dataset, QStandardItem *parent)
- {
- // std::cerr << "Entering seqInsert" << "\n";
- DcmObject *dO = dataset->nextInContainer( NULL);
- // std::cerr << "Entered nested level d0=" << dO << "\n";
- // std::cerr << "First node is dcmElem=" << dynamic_cast<DcmElement *> (dO) << "\n";
- for( ; dO; dO = dataset->nextInContainer(dO))
- {
- // DcmElement *dcmElem = dataset->getElement( idx);
- DcmElement *dcmElem = dynamic_cast<DcmElement *> (dO);
- QString tagValue = "";
- DcmTag tag = dO->getTag();
- if( tag.getXTag() == DCM_SequenceDelimitationItem
- || tag.getXTag() == DCM_ItemDelimitationItem)
- {
- return;
- }
- QString tagName = tag.getTagName();
- // std::cerr << "node is dcmElem=" << dcmElem << "\n";
- if( dcmElem)
- {
- std::ostringstream value;
- OFString part;
- std::string sep;
- int mult = dcmElem->getVM();
- int pos;
- if( mult>1)
- {
- value << "[" << mult << "] ";
- }
- // TODO define max elem per line
- for( pos=0; pos < std::min(mult,10); pos++)
- {
- value << sep;
- OFCondition status = dcmElem->getOFString( part, pos);
- if( status.good())
- {
- value << part.c_str();
- sep = ", ";
- }
- }
- if( pos < mult-1)
- {
- value << " ...";
- }
- tagValue = value.str().c_str();
- }
- // create items ...
- QStandardItem *tagItem = new QStandardItem( tagName);
- QStandardItem *valItem = new QStandardItem( tagValue);
- // ... and insert them
- QList<QStandardItem *> modelRow;
- modelRow.append( tagItem);
- modelRow.append( valItem);
- parent->appendRow( modelRow);
- // std::cerr << " "
- // << tagName.toStdString() << " " << tagValue.toStdString();
- if( dcmElem)
- {
- // std::cerr << " >> l=" << dcmElem->isLeaf()
- // << " nx=" << dataset->nextInContainer( dcmElem);
- if( !dcmElem->isLeaf())
- {
- // now dcmElem points to a sequenceOfItems
- ctkDICOMModelObjectPrivate::seqInsert( dynamic_cast<DcmSequenceOfItems*> (dcmElem), tagItem);
- }
- }
- else if( tag.getXTag() == DCM_Item)
- {
- itemInsert( dynamic_cast<DcmItem*> (dO), tagItem);
- }
- // std::cerr << "\n";
- }
- }
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- ctkDICOMModelObject::ctkDICOMModelObject(QObject* parentObject)
- : Superclass(parentObject)
- , d_ptr(new ctkDICOMModelObjectPrivate(*this))
- {
- Q_D(ctkDICOMModelObject);
- d->init();
-
- }
- //------------------------------------------------------------------------------
- ctkDICOMModelObject::ctkDICOMModelObject(const ctkDICOMModelObject& other)
- {
- }
- //------------------------------------------------------------------------------
- ctkDICOMModelObject::~ctkDICOMModelObject()
- {
- }
- //------------------------------------------------------------------------------
- void ctkDICOMModelObject::setFile(const QString &fileName)
- {
- Q_D(ctkDICOMModelObject);
-
- OFCondition status = d->fileFormat.loadFile( fileName.toLatin1().data());
- if( !status.good())
- {
- // TODO: Add through error
- }
-
- DcmDataset *dataset = d->fileFormat.getDataset();
- d->rootItem = ctkDICOMModelObject::invisibleRootItem();
- d->itemInsert( dataset, d->rootItem);
- }
|