QtDcmFindDicomdir.cpp
Go to the documentation of this file.
1 /*
2  QtDcm is a C++ Qt based library for communication and conversion of Dicom images.
3  Copyright (C) 2011 Alexandre Abadie <Alexandre.Abadie@univ-rennes1.fr>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #define QT_NO_CAST_TO_ASCII
22 
23 #include <dcmtk/dcmdata/dcdeftag.h>
24 #include <dcmtk/dcmdata/dcelem.h>
25 
26 #include "dcmtk/dcmnet/dfindscu.h"
27 
28 #include <QtDcmManager.h>
29 #include <QtDcmFindDicomdir.h>
30 
32 {
33 
34 public:
35  DcmItem * dcmObject;
36  DcmStack dicomdirItems;
37 };
38 
40  : QObject(parent),
41  d ( new QtDcmFindDicomdirPrivate )
42 {
43  d->dcmObject = NULL;
44 }
45 
47 {
48  delete d;
49  d = NULL;
50 }
51 
52 void QtDcmFindDicomdir::setDcmItem ( DcmItem * item )
53 {
54  d->dcmObject = item;
55 }
56 
58 {
59  static const OFString Patient ( "PATIENT" );
60 
61  // Loading all the dicomdir items in a stack
62  DcmStack itemsTmp;
63  if ( !d->dcmObject->findAndGetElements ( DCM_Item, itemsTmp ).good() ) {
64  return;
65  }
66 
67  while ( itemsTmp.card() > 0 ) {
68  d->dicomdirItems.push ( itemsTmp.top() );
69  itemsTmp.pop();
70  }
71 
72  //Unstacking and loading the different lists
73  while ( d->dicomdirItems.card() > 0 ) {
74  DcmItem* lobj = ( DcmItem* ) d->dicomdirItems.top();
75  DcmStack dirent;
76  OFCondition condition = lobj->findAndGetElements ( DCM_DirectoryRecordType, dirent );
77 
78  if ( !condition.good() ) {
79  d->dicomdirItems.pop();
80  continue;
81  }
82 
83  while ( dirent.card() ) {
84  DcmElement* elt = ( DcmElement* ) dirent.top();
85  OFString cur;
86  elt->getOFStringArray ( cur );
87  if ( cur == Patient ) {
88  DcmElement* lelt;
89  QMap<QString, QString> infosMap;
90  if ( lobj->findAndGetElement ( DCM_PatientName, lelt ).good() ){
91  OFString strName;
92  lelt->getOFStringArray ( strName );
93  infosMap.insert ( "Name", QString ( strName.c_str() ) );
94  }
95 
96  if ( lobj->findAndGetElement ( DCM_PatientID, lelt ).good() ){
97  OFString strID;
98  lelt->getOFStringArray ( strID );
99  infosMap.insert ( "ID", QString ( strID.c_str() ) );
100  }
101 
102  if ( lobj->findAndGetElement ( DCM_PatientBirthDate, lelt ).good() ){
103  OFString strBirth;
104  lelt->getOFStringArray ( strBirth );
105  infosMap.insert ( "Birthdate", QString ( strBirth.c_str() ) );
106  }
107 
108  if ( lobj->findAndGetElement ( DCM_PatientSex, lelt ).good() ) {
109  OFString strSex;
110  lelt->getOFStringArray ( strSex );
111  infosMap.insert ( "Sex", QString ( strSex.c_str() ) );
112  }
113 
114  QtDcmManager::instance()->foundPatient ( infosMap );
115  }
116 
117  dirent.pop();
118  }
119 
120  d->dicomdirItems.pop();
121  }
122 
123  d->dicomdirItems.clear();
124 }
125 
126 void QtDcmFindDicomdir::findStudies (const QString &patientName)
127 {
128  bool proceed = false;
129  static const OFString Patient ( "PATIENT" );
130  static const OFString Study ( "STUDY" );
131 
132  // Loading all the dicomdir items in a stack
133  DcmStack itemsTmp;
134  if ( !d->dcmObject->findAndGetElements ( DCM_Item, itemsTmp ).good() ) {
135  return;
136  }
137 
138  while ( itemsTmp.card() > 0 ){
139  d->dicomdirItems.push ( itemsTmp.top() );
140  itemsTmp.pop();
141  }
142 
143  //Unstacking and loading the different lists
144  while ( d->dicomdirItems.card() > 0 ){
145  DcmItem* lobj = ( DcmItem* ) d->dicomdirItems.top();
146  DcmStack dirent;
147  OFCondition condition = lobj->findAndGetElements ( DCM_DirectoryRecordType, dirent );
148  if ( !condition.good() ) {
149  d->dicomdirItems.pop();
150  continue;
151  }
152 
153  while ( dirent.card() ) {
154  DcmElement* elt = ( DcmElement* ) dirent.top();
155  OFString cur;
156  elt->getOFStringArray ( cur );
157 
158  if ( cur == Patient ){
159  DcmElement* lelt;
160  if ( lobj->findAndGetElement ( DCM_PatientName, lelt ).good() ) {
161  OFString strName;
162  lelt->getOFStringArray ( strName );
163  proceed = ( QString ( strName.c_str() ) == patientName );
164  }
165  }
166 
167  if ( ( cur == Study ) && proceed ){
168  DcmElement* lelt;
169  QMap<QString, QString> infosMap;
170  if ( lobj->findAndGetElement ( DCM_StudyInstanceUID, lelt ).good() ) {
171  OFString strUID;
172  lelt->getOFStringArray ( strUID );
173  infosMap.insert ( "UID", QString ( strUID.c_str() ) );
174  }
175 
176  if ( lobj->findAndGetElement ( DCM_StudyID, lelt ).good() ) {
177  OFString strID;
178  lelt->getOFStringArray ( strID );
179  infosMap.insert ( "ID", QString ( strID.c_str() ) );
180  }
181 
182  if ( lobj->findAndGetElement ( DCM_StudyDescription, lelt ).good() ) {
183  OFString strDescription;
184  lelt->getOFStringArray ( strDescription );
185  infosMap.insert ( "Description", QString ( strDescription.c_str() ) );
186  }
187 
188  if ( lobj->findAndGetElement ( DCM_StudyDate, lelt ).good() ) {
189  OFString strDate;
190  lelt->getOFStringArray ( strDate );
191  infosMap.insert ( "Date", QString ( strDate.c_str() ) );
192  }
193  QtDcmManager::instance()->foundStudy ( infosMap );
194  }
195 
196  dirent.pop();
197  }
198 
199  d->dicomdirItems.pop();
200  }
201 
202  d->dicomdirItems.clear();
203 }
204 
205 void QtDcmFindDicomdir::findSeries (const QString &patientName, const QString &studyUid)
206 {
207  bool proceed = false;
208  static const OFString Patient ( "PATIENT" );
209  static const OFString Study ( "STUDY" );
210  static const OFString Series ( "SERIES" );
211 
212  // Loading all the dicomdir items in a stack
213  DcmStack itemsTmp;
214  if ( !d->dcmObject->findAndGetElements ( DCM_Item, itemsTmp ).good() ) {
215  return;
216  }
217 
218  while ( itemsTmp.card() > 0 ) {
219  d->dicomdirItems.push ( itemsTmp.top() );
220  itemsTmp.pop();
221  }
222 
223  OFString strName;
224 
225  OFString strDate;
226  //Unstacking and loading the different lists
227 
228  while ( d->dicomdirItems.card() > 0 ) {
229  DcmItem* lobj = ( DcmItem* ) d->dicomdirItems.top();
230  DcmStack dirent;
231 
232  OFCondition condition = lobj->findAndGetElements ( DCM_DirectoryRecordType, dirent );
233 
234  if ( !condition.good() ) {
235  d->dicomdirItems.pop();
236  continue;
237  }
238 
239  while ( dirent.card() ) {
240  DcmElement* elt = ( DcmElement* ) dirent.top();
241  OFString cur;
242  elt->getOFStringArray ( cur );
243 
244  if ( cur == Patient ) {
245  DcmElement* lelt;
246  if ( lobj->findAndGetElement ( DCM_PatientName, lelt ).good() )
247  lelt->getOFStringArray ( strName );
248  }
249 
250  if ( cur == Study ) {
251  DcmElement* lelt;
252  if ( lobj->findAndGetElement ( DCM_StudyInstanceUID, lelt ).good() ) {
253  OFString strUid;
254  lelt->getOFStringArray ( strUid );
255  proceed = ( ( QString ( strName.c_str() ) == patientName ) && ( QString ( strUid.c_str() ) == studyUid ) );
256  }
257 
258  if ( lobj->findAndGetElement ( DCM_StudyDate, lelt ).good() ) {
259  lelt->getOFStringArray ( strDate );
260  }
261  }
262 
263  if ( ( cur == Series ) && proceed ) {
264  DcmElement* lelt;
265  QMap<QString, QString> infosMap;
266 
267  if ( lobj->findAndGetElement ( DCM_SeriesInstanceUID, lelt ).good() ) {
268  OFString strID;
269  lelt->getOFStringArray ( strID );
270  infosMap.insert ( "ID", QString ( strID.c_str() ) );
271  }
272 
273  if ( lobj->findAndGetElement ( DCM_SeriesDescription, lelt ).good() ) {
274  OFString strDescription;
275  lelt->getOFStringArray ( strDescription );
276  infosMap.insert ( "Description", QString ( strDescription.c_str() ) );
277  }
278 
279  if ( lobj->findAndGetElement ( DCM_Modality, lelt ).good() ) {
280  OFString strModality;
281  lelt->getOFStringArray ( strModality );
282  infosMap.insert ( "Modality", QString ( strModality.c_str() ) );
283  }
284 
285  if ( lobj->findAndGetElement ( DCM_InstitutionName, lelt ).good() ) {
286  OFString strInstitution;
287  lelt->getOFStringArray ( strInstitution );
288  infosMap.insert ( "Institution", QString ( strInstitution.c_str() ) );
289  }
290 
291  if ( lobj->findAndGetElement ( DCM_AcquisitionNumber, lelt ).good() ) {
292  OFString strCount;
293  lelt->getOFStringArray ( strCount );
294  infosMap.insert ( "InstanceCount", QString ( strCount.c_str() ) );
295  }
296 
297  if ( lobj->findAndGetElement ( DCM_PerformingPhysicianName, lelt ).good() ) {
298  OFString strOperator;
299  lelt->getOFStringArray ( strOperator );
300  infosMap.insert ( "Operator", QString ( strOperator.c_str() ) );
301  }
302 
303  infosMap.insert ( "Date", QString ( strDate.c_str() ) );
304 
305  QtDcmManager::instance()->foundSerie ( infosMap );
306  }
307 
308  dirent.pop();
309  }
310 
311  d->dicomdirItems.pop();
312  }
313 
314  d->dicomdirItems.clear();
315 }
316 
317 void QtDcmFindDicomdir::findImages (const QString &seriesUID)
318 {
319  bool proceed = false;
320  static const OFString Series ( "SERIES" );
321  static const OFString Image ( "IMAGE" );
322 
323  // Loading all the dicomdir items in a stack
324  DcmStack itemsTmp;
325  if ( !d->dcmObject->findAndGetElements ( DCM_Item, itemsTmp ).good() ) {
326  return;
327  }
328 
329  while ( itemsTmp.card() > 0 ) {
330  d->dicomdirItems.push ( itemsTmp.top() );
331  itemsTmp.pop();
332  }
333 
334  //Unstacking and loading the different lists
335  while ( d->dicomdirItems.card() > 0 )
336  {
337  DcmItem* lobj = ( DcmItem* ) d->dicomdirItems.top();
338  DcmStack dirent;
339 
340  OFCondition condition = lobj->findAndGetElements ( DCM_DirectoryRecordType, dirent );
341 
342  if ( !condition.good() ) {
343  d->dicomdirItems.pop();
344  continue;
345  }
346 
347  while ( dirent.card() ) {
348  DcmElement* elt = ( DcmElement* ) dirent.top();
349  OFString cur;
350  elt->getOFStringArray ( cur );
351 
352  if ( cur == Series ) {
353  DcmElement* lelt;
354  if ( lobj->findAndGetElement ( DCM_SeriesInstanceUID, lelt ).good() ) {
355  OFString strID;
356  lelt->getOFStringArray ( strID );
357  proceed = ( QString ( strID.c_str() ) == seriesUID );
358  }
359  }
360 
361  if ( ( cur == Image ) && proceed ) {
362  DcmElement* lelt;
363 
364  OFString strUID;
365  OFString strNumber ( "0" );
366  if ( lobj->findAndGetElement ( DCM_InstanceNumber, lelt ).good() )
367  lelt->getOFStringArray ( strNumber );
368 
369  if ( lobj->findAndGetElement ( DCM_ReferencedSOPInstanceUIDInFile, lelt ).good() ) {
370  lelt->getOFStringArray ( strUID );
371  }
372  QtDcmManager::instance()->foundImage ( QString ( strUID.c_str() ), QString ( strNumber.c_str() ).toInt() );
373  }
374 
375  dirent.pop();
376  }
377 
378  d->dicomdirItems.pop();
379  }
380 
381  d->dicomdirItems.clear();
382 }
static QtDcmManager * instance()
void findImages(const QString &seriesUID)
void foundSerie(const QMap< QString, QString > &infosMap)
void foundPatient(const QMap< QString, QString > &infosMap)
QtDcmFindDicomdir(QObject *parent=0)
void setDcmItem(DcmItem *item)
void findStudies(const QString &patientName)
void findSeries(const QString &patientName, const QString &studyUid)
void foundStudy(const QMap< QString, QString > &infosMap)
void foundImage(const QString &image, int number)