CustusX  22.04-rc5
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxDicomImporter.h"
13 
14 #include <QCheckBox>
15 #include <QMessageBox>
16 #include <QProgressDialog>
17 #include <QLabel>
18 #include <QFileDialog>
19 // ctkWidgets includes
20 // ctkDICOMCore includes
21 #include "ctkDICOMDatabase.h"
22 #include "ctkDICOMIndexer.h"
23 
24 #include "cxReporter.h"
25 
26 namespace cx
27 {
28 
29 DicomImporter::DicomImporter(QObject* parent): QObject(parent)
30 {
31  DICOMIndexer = QSharedPointer<ctkDICOMIndexer> (new ctkDICOMIndexer);
32  IndexerProgress = 0;
33  DisplayImportSummary = true;
34  PatientsAddedDuringImport = 0;
35  StudiesAddedDuringImport = 0;
36  SeriesAddedDuringImport = 0;
37  InstancesAddedDuringImport = 0;
38 }
39 
41 {
42  delete IndexerProgress;
43 }
44 
45 void DicomImporter::setDatabase(QSharedPointer<ctkDICOMDatabase> database)
46 {
47  if (DICOMDatabase)
48  {
49  disconnect(DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
50  SLOT(onPatientAdded(int,QString,QString,QString)));
51  disconnect(DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
52  disconnect(DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
53  disconnect(DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
54  }
55 
56  DICOMDatabase = database;
57 
58  if (DICOMDatabase)
59  {
60  connect(DICOMDatabase.data(), SIGNAL(patientAdded(int,QString,QString,QString)), this,
61  SLOT(onPatientAdded(int,QString,QString,QString)));
62  connect(DICOMDatabase.data(), SIGNAL(studyAdded(QString)), this, SLOT(onStudyAdded(QString)));
63  connect(DICOMDatabase.data(), SIGNAL(seriesAdded(QString)), this, SLOT(onSeriesAdded(QString)));
64  connect(DICOMDatabase.data(), SIGNAL(instanceAdded(QString)), this, SLOT(onInstanceAdded(QString)));
65  }
66 }
67 
68 void DicomImporter::showIndexerDialog()
69 {
70  if (IndexerProgress == 0)
71  {
72  //
73  // Set up the Indexer Progress Dialog
74  //
75  IndexerProgress = new QProgressDialog( "DICOM Import", "Cancel", 0, 100, NULL,
76  Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
77 
78  // We don't want the progress dialog to resize itself, so we bypass the label
79  // by creating our own
80  QLabel* progressLabel = new QLabel("Initialization...");
81  IndexerProgress->setLabel(progressLabel);
82  IndexerProgress->setWindowModality(Qt::ApplicationModal);
83  IndexerProgress->setMinimumDuration(0);
84  IndexerProgress->setValue(0);
85 
86  connect(IndexerProgress, SIGNAL(canceled()),
87  DICOMIndexer.data(), SLOT(cancel()));
88 
89  connect(DICOMIndexer.data(), SIGNAL(progress(int)),
90  IndexerProgress, SLOT(setValue(int)));
91  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
92  progressLabel, SLOT(setText(QString)));
93  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
94  this, SIGNAL(fileIndexed(QString)));
95  connect(DICOMIndexer.data(), SIGNAL(indexingFilePath(QString)),
96  this, SLOT(onFileIndexed(QString)));
97 
98  // close the dialog
99  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
100  IndexerProgress, SLOT(close()));
101  // reset the database to show new data
102  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
103  this, SIGNAL(indexingCompleted()));
104  // stop indexing and reset the database if canceled
105  connect(IndexerProgress, SIGNAL(canceled()),
106  DICOMIndexer.data(), SLOT(cancel()));
107  connect(IndexerProgress, SIGNAL(canceled()),
108  this, SIGNAL(indexingCompleted()));
109 
110  // allow users of this widget to know that the process has finished
111  connect(IndexerProgress, SIGNAL(canceled()),
112  this, SIGNAL(directoryImported()));
113  connect(DICOMIndexer.data(), SIGNAL(indexingComplete()),
114  this, SIGNAL(directoryImported()));
115  }
116  IndexerProgress->show();
117 }
118 
120 {
121  return DisplayImportSummary;
122 }
123 
125 {
126  DisplayImportSummary = onOff;
127 }
128 
130 {
131  return PatientsAddedDuringImport;
132 }
133 
135 {
136  return StudiesAddedDuringImport;
137 }
138 
140 {
141  return SeriesAddedDuringImport;
142 }
143 
145 {
146  return InstancesAddedDuringImport;
147 }
148 
149 //----------------------------------------------------------------------------
150 void DicomImporter::onFileIndexed(const QString& filePath)
151 {
152  // Update the progress dialog when the file name changes
153  // - also allows for cancel button
154  QCoreApplication::instance()->processEvents();
155 // qDebug() << "Indexing \n\n\n\n" << filePath <<"\n\n\n";
156 
157 }
158 
159 //----------------------------------------------------------------------------
160 void DicomImporter::openImportDialog()
161 {
162  QString folder = QFileDialog::getExistingDirectory(NULL, "Import DICOM files from directory ...", "", QFileDialog::ShowDirsOnly);
163  if (!folder.isEmpty())
164  onImportDirectory(folder);
165  else
166  reportWarning("No DICOM folder selected");
167 }
168 
169 //----------------------------------------------------------------------------
170 void DicomImporter::onPatientAdded(int databaseID, QString patientID, QString patientName, QString patientBirthDate )
171 {
172  Q_UNUSED(databaseID);
173  Q_UNUSED(patientID);
174  Q_UNUSED(patientName);
175  Q_UNUSED(patientBirthDate);
176  ++PatientsAddedDuringImport;
177 }
178 
179 //----------------------------------------------------------------------------
180 void DicomImporter::onStudyAdded(QString studyUID)
181 {
182  Q_UNUSED(studyUID);
183  ++StudiesAddedDuringImport;
184 }
185 
186 //----------------------------------------------------------------------------
187 void DicomImporter::onSeriesAdded(QString seriesUID)
188 {
189  Q_UNUSED(seriesUID);
190  ++SeriesAddedDuringImport;
191 }
192 
193 //----------------------------------------------------------------------------
194 void DicomImporter::onInstanceAdded(QString instanceUID)
195 {
196  Q_UNUSED(instanceUID);
197  ++InstancesAddedDuringImport;
198 }
199 
200 //----------------------------------------------------------------------------
201 void DicomImporter::onImportDirectory(QString directory)
202 {
203  if (QDir(directory).exists())
204  {
205  // reset counts
206  PatientsAddedDuringImport = 0;
207  StudiesAddedDuringImport = 0;
208  SeriesAddedDuringImport = 0;
209  InstancesAddedDuringImport = 0;
210 
211  // show progress dialog and perform indexing
212  showIndexerDialog();
213  DICOMIndexer->addDirectory(*DICOMDatabase,directory);
214 
215  // display summary result
216  if (DisplayImportSummary)
217  {
218  QString message = "Directory import completed.\n\n";
219  message += QString("%1 New Patients\n").arg(QString::number(PatientsAddedDuringImport));
220  message += QString("%1 New Studies\n").arg(QString::number(StudiesAddedDuringImport));
221  message += QString("%1 New Series\n").arg(QString::number(SeriesAddedDuringImport));
222  message += QString("%1 New Instances\n").arg(QString::number(InstancesAddedDuringImport));
223  QMessageBox::information(NULL,"DICOM Directory Import", message);
224  }
225  }
226 }
227 
228 } // namespace cx
229 
230 
void directoryImported()
void setDatabase(QSharedPointer< ctkDICOMDatabase > database)
void indexingCompleted()
int patientsAddedDuringImport()
Accessors to status of last directory import operation.
void setDisplayImportSummary(bool)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
DicomImporter(QObject *parent=NULL)
void onImportDirectory(QString directory)
void fileIndexed(QString)
Namespace for all CustusX production code.