QtDcmMoveDicomdir.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 #define QT_NO_CAST_TO_ASCII
21 
22 #include <dcmtk/dcmdata/dcdeftag.h>
23 #include <dcmtk/dcmdata/dcstack.h>
24 #include <dcmtk/dcmdata/dcitem.h>
25 #include <dcmtk/dcmdata/dcelem.h>
26 
27 #include <QtDcmManager.h>
28 #include <QtDcmMoveDicomdir.h>
29 #include <QtDcmConvert.h>
30 
32 {
33 
34 public:
35  QString outputDir;
36  QString importDir;
37  DcmItem * dcmObject;
38  DcmStack dicomdirItems;
39  QStringList filenames;
40  QStringList series;
42  int index;
43  QString uid;
44 };
45 
47  : QThread(parent),
48  d ( new QtDcmMoveDicomdirPrivate )
49 {
51 }
52 
54 {
55  delete d;
56  d = NULL;
57 }
58 
60 {
61  d->mode = mode;
62 }
63 
64 void QtDcmMoveDicomdir::setDcmItem ( DcmItem * item )
65 {
66  d->dcmObject = item;
67 }
68 
69 void QtDcmMoveDicomdir::setSeries ( const QStringList & series )
70 {
71  d->series = series;
72 }
73 
74 void QtDcmMoveDicomdir::setIndex ( int index )
75 {
76  d->index = index;
77 }
78 
79 void QtDcmMoveDicomdir::setImageId ( const QString & uid)
80 {
81  d->uid = uid;
82 }
83 
84 
85 void QtDcmMoveDicomdir::setOutputDir ( const QString & dir )
86 {
87  d->outputDir = dir;
88 }
89 
90 void QtDcmMoveDicomdir::setImportDir ( const QString & dir )
91 {
92  d->importDir = dir;
93 }
94 
96 {
97  int step = ( int ) ( 100.0 / d->series.size() );
98  int progress = 0;
99 
100  for ( int s = 0; s < d->series.size(); s++ ) {
101  QDir serieDir ( d->outputDir + QDir::separator() + d->series.at ( s ) );
102 
103  if ( !serieDir.exists() )
104  QDir ( d->outputDir ).mkdir ( d->series.at ( s ) );
105 
106  d->filenames.clear();
107 
108  bool proceed = false;
109  bool proceedIndex = false;
110 
111  static const OFString Patient ( "PATIENT" );
112  static const OFString Study ( "STUDY" );
113  static const OFString Series ( "SERIES" );
114  static const OFString Image ( "IMAGE" );
115 
116  // Loading all the dicomdir items in a stack
117  DcmStack itemsTmp;
118 
119  if ( !d->dcmObject->findAndGetElements ( DCM_Item, itemsTmp ).good() )
120  return;
121 
122  while ( itemsTmp.card() > 0 ) {
123  d->dicomdirItems.push ( itemsTmp.top() );
124  itemsTmp.pop();
125  }
126 
127  OFString strName;
128  OFString strDate;
129  OFString strDesc;
130 
131  //Unstacking and loading the different lists
132 
133  while ( d->dicomdirItems.card() > 0 ) {
134  DcmItem* lobj = ( DcmItem* ) d->dicomdirItems.top();
135  DcmStack dirent;
136 
137  OFCondition condition = lobj->findAndGetElements ( DCM_DirectoryRecordType, dirent );
138 
139  if ( !condition.good() ) {
140  d->dicomdirItems.pop();
141  continue;
142  }
143 
144  while ( dirent.card() ) {
145  DcmElement* elt = ( DcmElement* ) dirent.top();
146  OFString cur;
147  elt->getOFStringArray ( cur );
148 
149  if ( cur ==Patient ) {
150  DcmElement* lelt;
151 
152  if ( lobj->findAndGetElement ( DCM_PatientName, lelt ).good() )
153  lelt->getOFStringArray ( strName );
154  }
155 
156  if ( cur == Study ) {
157  DcmElement* lelt;
158 
159  if ( lobj->findAndGetElement ( DCM_StudyDate, lelt ).good() )
160  lelt->getOFStringArray ( strDate );
161  }
162 
163  if ( cur == Series ) {
164  DcmElement* lelt;
165 
166  if ( lobj->findAndGetElement ( DCM_SeriesInstanceUID, lelt ).good() ) {
167  OFString strID;
168  lelt->getOFStringArray ( strID );
169  proceed = ( QString ( strID.c_str() ) == d->series.at ( s ) );
170  }
171 
172  if ( proceed ) {
173  if ( lobj->findAndGetElement ( DCM_SeriesDescription, lelt ).good() )
174  lelt->getOFStringArray ( strDesc );
175  }
176  }
177 
178  if ( ( cur == Image ) && proceed ) {
179  DcmElement* lelt;
180 
181  if ( lobj->findAndGetElement ( DCM_ReferencedSOPInstanceUIDInFile, lelt ).good() ) {
182  OFString strNumber;
183  lelt->getOFStringArray ( strNumber );
184 
185  if ( d->mode == QtDcmMoveDicomdir::PREVIEW ) {
186  proceedIndex = ( QString ( strNumber.c_str() ) == d->uid );
187  }
188  }
189 
190 // if ( lobj->findAndGetElement ( DCM_InstanceNumber, lelt ).good() )
191 // {
192 // OFString strNumber;
193 // lelt->getOFStringArray ( strNumber );
194 //
195 // if ( d->mode == QtDcmMoveDicomdir::PREVIEW )
196 // proceedIndex = ( QString ( strNumber.c_str() ).toInt() == d->index );
197 // }
198 
199  if ( lobj->findAndGetElement ( DCM_ReferencedFileID, lelt ).good() ) {
200  OFString strFilename;
201  lelt->getOFStringArray ( strFilename );
202 
203  if ( d->mode == QtDcmMoveDicomdir::IMPORT ) {
204  d->filenames.append ( this->fixFilename ( QString ( strFilename.c_str() ) ) );
205  }
206  else {
207  if ( proceedIndex ) {
208  d->filenames.append ( this->fixFilename ( QString ( strFilename.c_str() ) ) );
209  }
210  }
211 
212  }
213 
214  if ( lobj->findAndGetElement ( DCM_SeriesDescription, lelt ).good() )
215  lelt->getOFStringArray ( strDesc );
216  }
217 
218  dirent.pop();
219  }
220 
221  d->dicomdirItems.pop();
222  }
223 
224  d->dicomdirItems.clear();
225 
226  if ( d->mode == QtDcmMoveDicomdir::IMPORT ) {
227  for ( int i = 0; i < d->filenames.size(); i++ ) {
228  QFile image ( d->filenames.at ( i ) );
229 
230  if ( image.exists() ) {
231  QString zeroStr;
232  zeroStr.fill ( QChar ( '0' ), 5 - QString::number ( i ).size() );
233  QString newFile(serieDir.absolutePath() + QDir::separator() + "ima" + zeroStr + QString::number ( i ));
234  image.copy(newFile);
235  QFile(newFile).setPermissions(QFileDevice::WriteOwner);
236 
237  emit updateProgress ( progress + ( int ) ( ( ( float ) ( step * ( i + 1 ) / d->filenames.size() ) ) ) );
238  }
239  }
240 
241  progress += step;
242  emit updateProgress(progress);
243  emit serieMoved ( serieDir.absolutePath(), d->series.at ( s ) , s );
244  }
245  else {
246  if ( !d->filenames.isEmpty() ) {
247  QFile filename ( d->filenames.first() );
248  emit previewSlice ( filename.fileName() );
249  }
250  }
251  }
252 }
253 
254 QString QtDcmMoveDicomdir::fixFilename ( const QString & name ) const
255 {
256  QString tmpName(name);
257  const QString basename = QFileInfo ( QtDcmManager::instance()->dicomdir() ).path();
258  tmpName.replace ( QChar ( '/' ), QDir::separator() ).replace ( QChar ( '\\' ), QDir::separator() );
259  QString filename = basename + QDir::separator() + tmpName.toUpper();
260 
261  if ( ! QFile ( filename ).exists() ) {
262  filename = basename + QDir::separator() + tmpName.toLower();
263  }
264 
265  return filename;
266 }
void setDcmItem(DcmItem *item)
QtDcmMoveDicomdir(QObject *parent)
static QtDcmManager * instance()
void setSeries(const QStringList &series)
void previewSlice(const QString &filename)
void updateProgress(int i)
QtDcmMoveDicomdir::eMoveMode mode
void setIndex(int index)
void setMode(eMoveMode mode)
void setImageId(const QString &uid)
void setOutputDir(const QString &dir)
void setImportDir(const QString &dir)
void serieMoved(const QString &directory, const QString &serie, int number)