CustusX  15.3.3-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxDicomImporter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxDicomImporter.h"
34 
35 #include <QCheckBox>
36 #include <QMessageBox>
37 #include <QProgressDialog>
38 #include <QLabel>
39 // ctkWidgets includes
40 #include "ctkFileDialog.h"
41 // ctkDICOMCore includes
42 #include "ctkDICOMDatabase.h"
43 #include "ctkDICOMIndexer.h"
44 
45 namespace cx
46 {
47 
48 DicomImporter::DicomImporter(QObject* parent): QObject(parent)
49 {
50  DICOMIndexer = QSharedPointer<ctkDICOMIndexer> (new ctkDICOMIndexer);
51  IndexerProgress = 0;
52  DisplayImportSummary = true;
53  PatientsAddedDuringImport = 0;
54  StudiesAddedDuringImport = 0;
55  SeriesAddedDuringImport = 0;
56  InstancesAddedDuringImport = 0;
57 
58  //Initialize import widget
59  ImportDialog = new ctkFileDialog();
60  QCheckBox* importCheckbox = new QCheckBox("Copy on import", ImportDialog);
61  ImportDialog->setBottomWidget(importCheckbox);
62  ImportDialog->setFileMode(QFileDialog::Directory);
63  ImportDialog->setLabelText(QFileDialog::Accept,"Import");
64  ImportDialog->setWindowTitle("Import DICOM files from directory ...");
65  ImportDialog->setWindowModality(Qt::ApplicationModal);
66 
67  connect(ImportDialog, SIGNAL(fileSelected(QString)),this,SLOT(onImportDirectory(QString)));
68 }
69 
71 {
72  delete IndexerProgress;
73  ImportDialog->deleteLater();
74 }
75 
76 void DicomImporter::setDatabase(QSharedPointer<ctkDICOMDatabase> database)
77 {
78  if (DICOMDatabase)
79  {
80  disconnect(DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
81  SLOT(onPatientAdded(int,QString,QString,QString)));
82  disconnect(DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
83  disconnect(DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
84  disconnect(DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
85  }
86 
87  DICOMDatabase = database;
88 
89  if (DICOMDatabase)
90  {
91  connect(DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
92  SLOT(onPatientAdded(int,QString,QString,QString)));
93  connect(DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
94  connect(DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
95  connect(DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
96  }
97 }
98 
99 void DicomImporter::showIndexerDialog()
100 {
101  if (IndexerProgress == 0)
102  {
103  //
104  // Set up the Indexer Progress Dialog
105  //
106  IndexerProgress = new QProgressDialog( "DICOM Import", "Cancel", 0, 100, NULL,
107  Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
108 
109  // We don't want the progress dialog to resize itself, so we bypass the label
110  // by creating our own
111  QLabel* progressLabel = new QLabel("Initialization...");
112  IndexerProgress->setLabel(progressLabel);
113  IndexerProgress->setWindowModality(Qt::ApplicationModal);
114  IndexerProgress->setMinimumDuration(0);
115  IndexerProgress->setValue(0);
116 
117  connect(IndexerProgress, SIGNAL(canceled()),
118  DICOMIndexer.data(), SLOT(cancel()));
119 
120  connect(DICOMIndexer.data(), SIGNAL(progress(int)),
121  IndexerProgress, SLOT(setValue(int)));
122  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
123  progressLabel, SLOT(setText(QString)));
124  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
125  this, SIGNAL(fileIndexed(QString)));
126  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
127  this, SLOT(onFileIndexed(QString)));
128 
129  // close the dialog
130  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
131  IndexerProgress, SLOT(close()));
132  // reset the database to show new data
133  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
134  this, SIGNAL(indexingCompleted()));
135  // stop indexing and reset the database if canceled
136  connect(IndexerProgress, SIGNAL(canceled()),
137  DICOMIndexer.data(), SLOT(cancel()));
138  connect(IndexerProgress, SIGNAL(canceled()),
139  this, SLOT(indexingCompleted()));
140 
141  // allow users of this widget to know that the process has finished
142  connect(IndexerProgress, SIGNAL(canceled()),
143  this, SIGNAL(directoryImported()));
144  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
145  this, SIGNAL(directoryImported()));
146  }
147  IndexerProgress->show();
148 }
149 
151 {
152  return DisplayImportSummary;
153 }
154 
156 {
157  DisplayImportSummary = onOff;
158 }
159 
161 {
162  return PatientsAddedDuringImport;
163 }
164 
166 {
167  return StudiesAddedDuringImport;
168 }
169 
171 {
172  return SeriesAddedDuringImport;
173 }
174 
176 {
177  return InstancesAddedDuringImport;
178 }
179 
180 //----------------------------------------------------------------------------
181 void DicomImporter::onFileIndexed(const QString& filePath)
182 {
183  // Update the progress dialog when the file name changes
184  // - also allows for cancel button
185  QCoreApplication::instance()->processEvents();
186 // qDebug() << "Indexing \n\n\n\n" << filePath <<"\n\n\n";
187 
188 }
189 
190 //----------------------------------------------------------------------------
191 void DicomImporter::openImportDialog()
192 {
193  ImportDialog->show();
194  ImportDialog->raise();
195 }
196 
197 //----------------------------------------------------------------------------
198 void DicomImporter::onPatientAdded(int databaseID, QString patientID, QString patientName, QString patientBirthDate )
199 {
200  Q_UNUSED(databaseID);
201  Q_UNUSED(patientID);
202  Q_UNUSED(patientName);
203  Q_UNUSED(patientBirthDate);
204  ++PatientsAddedDuringImport;
205 }
206 
207 //----------------------------------------------------------------------------
208 void DicomImporter::onStudyAdded(QString studyUID)
209 {
210  Q_UNUSED(studyUID);
211  ++StudiesAddedDuringImport;
212 }
213 
214 //----------------------------------------------------------------------------
215 void DicomImporter::onSeriesAdded(QString seriesUID)
216 {
217  Q_UNUSED(seriesUID);
218  ++SeriesAddedDuringImport;
219 }
220 
221 //----------------------------------------------------------------------------
222 void DicomImporter::onInstanceAdded(QString instanceUID)
223 {
224  Q_UNUSED(instanceUID);
225  ++InstancesAddedDuringImport;
226 }
227 
228 //----------------------------------------------------------------------------
229 void DicomImporter::onImportDirectory(QString directory)
230 {
231  if (QDir(directory).exists())
232  {
233  QCheckBox* copyOnImport = qobject_cast<QCheckBox*>(ImportDialog->bottomWidget());
234  QString targetDirectory;
235  if (copyOnImport->checkState() == Qt::Checked)
236  {
237  targetDirectory = DICOMDatabase->databaseDirectory();
238  }
239 
240  // reset counts
241  PatientsAddedDuringImport = 0;
242  StudiesAddedDuringImport = 0;
243  SeriesAddedDuringImport = 0;
244  InstancesAddedDuringImport = 0;
245 
246  // show progress dialog and perform indexing
247  showIndexerDialog();
248  DICOMIndexer->addDirectory(*DICOMDatabase,directory,targetDirectory);
249 
250  // display summary result
251  if (DisplayImportSummary)
252  {
253  QString message = "Directory import completed.\n\n";
254  message += QString("%1 New Patients\n").arg(QString::number(PatientsAddedDuringImport));
255  message += QString("%1 New Studies\n").arg(QString::number(StudiesAddedDuringImport));
256  message += QString("%1 New Series\n").arg(QString::number(SeriesAddedDuringImport));
257  message += QString("%1 New Instances\n").arg(QString::number(InstancesAddedDuringImport));
258  QMessageBox::information(NULL,"DICOM Directory Import", message);
259  }
260  }
261 }
262 
263 } // namespace cx
264 
265 
void directoryImported()
void setDatabase(QSharedPointer< ctkDICOMDatabase > database)
void indexingCompleted()
int patientsAddedDuringImport()
Accessors to status of last directory import operation.
void setDisplayImportSummary(bool)
DicomImporter(QObject *parent=NULL)
void onImportDirectory(QString directory)
void fileIndexed(QString)