ctkDICOMIndexer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QSqlQuery>
  16. #include <QSqlRecord>
  17. #include <QSqlError>
  18. #include <QVariant>
  19. #include <QDate>
  20. #include <QStringList>
  21. #include <QSet>
  22. #include <QFile>
  23. #include <QDirIterator>
  24. #include <QFileInfo>
  25. #include <QDebug>
  26. #include <QPixmap>
  27. // ctkDICOM includes
  28. #include "ctkLogger.h"
  29. #include "ctkDICOMIndexer.h"
  30. #include "ctkDICOMIndexer_p.h"
  31. #include "ctkDICOMDatabase.h"
  32. // DCMTK includes
  33. #include <dcmtk/dcmdata/dcfilefo.h>
  34. #include <dcmtk/dcmdata/dcfilefo.h>
  35. #include <dcmtk/dcmdata/dcdeftag.h>
  36. #include <dcmtk/dcmdata/dcdatset.h>
  37. #include <dcmtk/ofstd/ofcond.h>
  38. #include <dcmtk/ofstd/ofstring.h>
  39. #include <dcmtk/ofstd/ofstd.h> /* for class OFStandard */
  40. #include <dcmtk/dcmdata/dcddirif.h> /* for class DicomDirInterface */
  41. #include <dcmtk/dcmimgle/dcmimage.h> /* for class DicomImage */
  42. #include <dcmtk/dcmimage/diregist.h> /* include support for color images */
  43. //------------------------------------------------------------------------------
  44. static ctkLogger logger("org.commontk.dicom.DICOMIndexer" );
  45. //------------------------------------------------------------------------------
  46. //------------------------------------------------------------------------------
  47. // ctkDICOMIndexerPrivate methods
  48. //------------------------------------------------------------------------------
  49. ctkDICOMIndexerPrivate::ctkDICOMIndexerPrivate(ctkDICOMIndexer& o) : q_ptr(&o), Canceled(false)
  50. {
  51. Q_Q(ctkDICOMIndexer);
  52. }
  53. //------------------------------------------------------------------------------
  54. ctkDICOMIndexerPrivate::~ctkDICOMIndexerPrivate()
  55. {
  56. }
  57. //------------------------------------------------------------------------------
  58. //------------------------------------------------------------------------------
  59. // ctkDICOMIndexer methods
  60. //------------------------------------------------------------------------------
  61. ctkDICOMIndexer::ctkDICOMIndexer(QObject *parent):d_ptr(new ctkDICOMIndexerPrivate(*this))
  62. {
  63. Q_UNUSED(parent);
  64. }
  65. //------------------------------------------------------------------------------
  66. ctkDICOMIndexer::~ctkDICOMIndexer()
  67. {
  68. }
  69. //------------------------------------------------------------------------------
  70. void ctkDICOMIndexer::addFile(ctkDICOMDatabase& database,
  71. const QString filePath,
  72. const QString& destinationDirectoryName)
  73. {
  74. std::cout << filePath.toStdString();
  75. if (!destinationDirectoryName.isEmpty())
  76. {
  77. logger.warn("Ignoring destinationDirectoryName parameter, just taking it as indication we should copy!");
  78. }
  79. emit indexingFilePath(filePath);
  80. database.insert(filePath, !destinationDirectoryName.isEmpty(), true);
  81. }
  82. //------------------------------------------------------------------------------
  83. void ctkDICOMIndexer::addDirectory(ctkDICOMDatabase& ctkDICOMDatabase,
  84. const QString& directoryName,
  85. const QString& destinationDirectoryName)
  86. {
  87. Q_D(ctkDICOMIndexer);
  88. QStringList listOfFiles;
  89. QDir directory(directoryName);
  90. if(directory.exists("DICOMDIR"))
  91. {
  92. addDicomdir(ctkDICOMDatabase,directoryName,destinationDirectoryName);
  93. }
  94. else
  95. {
  96. QDirIterator it(directoryName,QDir::Files,QDirIterator::Subdirectories);
  97. while(it.hasNext())
  98. {
  99. listOfFiles << it.next();
  100. }
  101. emit foundFilesToIndex(listOfFiles.count());
  102. addListOfFiles(ctkDICOMDatabase,listOfFiles,destinationDirectoryName);
  103. }
  104. }
  105. //------------------------------------------------------------------------------
  106. void ctkDICOMIndexer::addListOfFiles(ctkDICOMDatabase& ctkDICOMDatabase,
  107. const QStringList& listOfFiles,
  108. const QString& destinationDirectoryName)
  109. {
  110. Q_D(ctkDICOMIndexer);
  111. d->Canceled = false;
  112. int CurrentFileIndex = 0;
  113. foreach(QString filePath, listOfFiles)
  114. {
  115. int percent = ( 100 * CurrentFileIndex ) / listOfFiles.size();
  116. emit this->progress(percent);
  117. this->addFile(ctkDICOMDatabase, filePath, destinationDirectoryName);
  118. CurrentFileIndex++;
  119. if( d->Canceled )
  120. {
  121. break;
  122. }
  123. }
  124. emit this->indexingComplete();
  125. }
  126. //------------------------------------------------------------------------------
  127. void ctkDICOMIndexer::addDicomdir(ctkDICOMDatabase& ctkDICOMDatabase,
  128. const QString& directoryName,
  129. const QString& destinationDirectoryName
  130. )
  131. {
  132. Q_D(ctkDICOMIndexer);
  133. //Initialize dicomdir with directory path
  134. QString dcmFilePath = directoryName;
  135. dcmFilePath.append("/DICOMDIR");
  136. DcmDicomDir* dicomDir = new DcmDicomDir(dcmFilePath.toStdString().c_str());
  137. //Values to store records data at the moment only uid needed
  138. OFString patientsName, studyInstanceUID, seriesInstanceUID, sopInstanceUID, referencedFileName ;
  139. //Variables for progress operations
  140. QString instanceFilePath;
  141. QStringList listOfInstances;
  142. DcmDirectoryRecord* rootRecord = &(dicomDir->getRootRecord());
  143. DcmDirectoryRecord* patientRecord = NULL;
  144. DcmDirectoryRecord* studyRecord = NULL;
  145. DcmDirectoryRecord* seriesRecord = NULL;
  146. DcmDirectoryRecord* fileRecord = NULL;
  147. /*Iterate over all records in dicomdir and setup path to the dataset of the filerecord
  148. then insert. the filerecord into the database.
  149. If any UID is missing the record and all of it's subelements won't be added to the database*/
  150. if(rootRecord != NULL)
  151. {
  152. while (((patientRecord = rootRecord->nextSub(patientRecord)) != NULL)
  153. &&(patientRecord->findAndGetOFString(DCM_PatientName, patientsName).good()))
  154. {
  155. logger.debug( "Reading new Patients:" );
  156. logger.debug( "Patient's Name: " + QString(patientsName.c_str()) );
  157. while (((studyRecord = patientRecord->nextSub(studyRecord)) != NULL)
  158. && (studyRecord->findAndGetOFString(DCM_StudyInstanceUID, studyInstanceUID).good()))
  159. {
  160. logger.debug( "Reading new Studys:" );
  161. logger.debug( "Studies Name: " + QString(studyInstanceUID.c_str()) );
  162. while (((seriesRecord = studyRecord->nextSub(seriesRecord)) != NULL)
  163. &&(seriesRecord->findAndGetOFString(DCM_SeriesInstanceUID, seriesInstanceUID).good()))
  164. {
  165. logger.debug( "Reading new Series:" );
  166. logger.debug( "Series Instance Name: " + QString(seriesInstanceUID.c_str()) );
  167. while (((fileRecord = seriesRecord->nextSub(fileRecord)) != NULL)
  168. &&(fileRecord->findAndGetOFStringArray(DCM_ReferencedSOPInstanceUIDInFile, sopInstanceUID).good())
  169. &&(fileRecord->findAndGetOFStringArray(DCM_ReferencedFileID,referencedFileName).good()))
  170. {
  171. //Get the filepath of the instance and insert it into a list
  172. instanceFilePath = directoryName;
  173. instanceFilePath.append("/");
  174. instanceFilePath.append(QString( referencedFileName.c_str() ));
  175. instanceFilePath.replace("\\","/");
  176. listOfInstances << instanceFilePath;
  177. }
  178. }
  179. }
  180. }
  181. emit foundFilesToIndex(listOfInstances.count());
  182. addListOfFiles(ctkDICOMDatabase,listOfInstances,destinationDirectoryName);
  183. }
  184. }
  185. //------------------------------------------------------------------------------
  186. void ctkDICOMIndexer::refreshDatabase(ctkDICOMDatabase& dicomDatabase, const QString& directoryName)
  187. {
  188. Q_UNUSED(dicomDatabase);
  189. Q_UNUSED(directoryName);
  190. /*
  191. * Probably this should go to the database class as well
  192. * Or we have to extend the interface to make possible what we do here
  193. * without using SQL directly
  194. /// get all filenames from the database
  195. QSqlQuery allFilesQuery(dicomDatabase.database());
  196. QStringList databaseFileNames;
  197. QStringList filesToRemove;
  198. this->loggedExec(allFilesQuery, "SELECT Filename from Images;");
  199. while (allFilesQuery.next())
  200. {
  201. QString fileName = allFilesQuery.value(0).toString();
  202. databaseFileNames.append(fileName);
  203. if (! QFile::exists(fileName) )
  204. {
  205. filesToRemove.append(fileName);
  206. }
  207. }
  208. QSet<QString> filesytemFiles;
  209. QDirIterator dirIt(directoryName);
  210. while (dirIt.hasNext())
  211. {
  212. filesytemFiles.insert(dirIt.next());
  213. }
  214. // TODO: it looks like this function was never finished...
  215. //
  216. // I guess the next step is to remove all filesToRemove from the database
  217. // and also to add filesystemFiles into the database tables
  218. */
  219. }
  220. //----------------------------------------------------------------------------
  221. void ctkDICOMIndexer::cancel()
  222. {
  223. Q_D(ctkDICOMIndexer);
  224. d->Canceled = true;
  225. }