ctkDICOMIndexer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // DCMTK includes
  31. #include <dcmtk/dcmdata/dcfilefo.h>
  32. #include <dcmtk/dcmdata/dcfilefo.h>
  33. #include <dcmtk/dcmdata/dcdeftag.h>
  34. #include <dcmtk/dcmdata/dcdatset.h>
  35. #include <dcmtk/ofstd/ofcond.h>
  36. #include <dcmtk/ofstd/ofstring.h>
  37. #include <dcmtk/ofstd/ofstd.h> /* for class OFStandard */
  38. #include <dcmtk/dcmdata/dcddirif.h> /* for class DicomDirInterface */
  39. #include <dcmtk/dcmimgle/dcmimage.h> /* for class DicomImage */
  40. #include <dcmtk/dcmimage/diregist.h> /* include support for color images */
  41. //------------------------------------------------------------------------------
  42. static ctkLogger logger("org.commontk.dicom.DICOMIndexer" );
  43. //------------------------------------------------------------------------------
  44. //------------------------------------------------------------------------------
  45. class ctkDICOMIndexerPrivate
  46. {
  47. public:
  48. ctkDICOMIndexerPrivate();
  49. ~ctkDICOMIndexerPrivate();
  50. ctkDICOMAbstractThumbnailGenerator* thumbnailGenerator;
  51. };
  52. //------------------------------------------------------------------------------
  53. // ctkDICOMIndexerPrivate methods
  54. //------------------------------------------------------------------------------
  55. ctkDICOMIndexerPrivate::ctkDICOMIndexerPrivate()
  56. {
  57. }
  58. //------------------------------------------------------------------------------
  59. ctkDICOMIndexerPrivate::~ctkDICOMIndexerPrivate()
  60. {
  61. }
  62. //------------------------------------------------------------------------------
  63. //------------------------------------------------------------------------------
  64. // ctkDICOMIndexer methods
  65. //------------------------------------------------------------------------------
  66. ctkDICOMIndexer::ctkDICOMIndexer(QObject *parent):d_ptr(new ctkDICOMIndexerPrivate)
  67. {
  68. Q_UNUSED(parent);
  69. }
  70. //------------------------------------------------------------------------------
  71. ctkDICOMIndexer::~ctkDICOMIndexer()
  72. {
  73. }
  74. //------------------------------------------------------------------------------
  75. void ctkDICOMIndexer::addFile(ctkDICOMDatabase& ctkDICOMDatabase,
  76. const QString& filePath,
  77. const QString& destinationDirectoryName)
  78. {
  79. if (!destinationDirectoryName.isEmpty())
  80. {
  81. logger.warn("Ignoring destinationDirectoryName parameter, just taking it as indication we should copy!");
  82. }
  83. emit indexingFilePath(filePath);
  84. ctkDICOMDatabase.insert(filePath, !destinationDirectoryName.isEmpty(), true);
  85. }
  86. //------------------------------------------------------------------------------
  87. void ctkDICOMIndexer::addDirectory(ctkDICOMDatabase& ctkDICOMDatabase,
  88. const QString& directoryName,
  89. const QString& destinationDirectoryName)
  90. {
  91. const std::string src_directory(directoryName.toStdString());
  92. OFList<OFString> originalDcmtkFileNames;
  93. OFList<OFString> dcmtkFileNames;
  94. OFStandard::searchDirectoryRecursively( QDir::toNativeSeparators(src_directory.c_str()).toAscii().data(), originalDcmtkFileNames, "", "");
  95. int totalNumberOfFiles = originalDcmtkFileNames.size();
  96. // hack to reverse list of filenames (not neccessary when image loading works correctly)
  97. for ( OFListIterator(OFString) iter = originalDcmtkFileNames.begin(); iter != originalDcmtkFileNames.end(); ++iter )
  98. {
  99. dcmtkFileNames.push_front( *iter );
  100. }
  101. OFListIterator(OFString) iter = dcmtkFileNames.begin();
  102. OFListIterator(OFString) last = dcmtkFileNames.end();
  103. if(iter == last) return;
  104. emit foundFilesToIndex(totalNumberOfFiles);
  105. /* iterate over all input filenames */
  106. int fileNumber = 0;
  107. int currentProgress = -1;
  108. while (iter != last)
  109. {
  110. emit indexingFileNumber(++fileNumber);
  111. int newProgress = ( fileNumber * 100 ) / totalNumberOfFiles;
  112. if (newProgress != currentProgress)
  113. {
  114. currentProgress = newProgress;
  115. emit progress( currentProgress );
  116. }
  117. QString filePath((*iter).c_str());
  118. this->addFile(ctkDICOMDatabase, filePath, destinationDirectoryName);
  119. ++iter;
  120. }
  121. }
  122. //------------------------------------------------------------------------------
  123. void ctkDICOMIndexer::refreshDatabase(ctkDICOMDatabase& dicomDatabase, const QString& directoryName)
  124. {
  125. Q_UNUSED(dicomDatabase);
  126. Q_UNUSED(directoryName);
  127. /*
  128. * Probably this should go to the database class as well
  129. * Or we have to extend the interface to make possible what we do here
  130. * without using SQL directly
  131. /// get all filenames from the database
  132. QSqlQuery allFilesQuery(dicomDatabase.database());
  133. QStringList databaseFileNames;
  134. QStringList filesToRemove;
  135. this->loggedExec(allFilesQuery, "SELECT Filename from Images;");
  136. while (allFilesQuery.next())
  137. {
  138. QString fileName = allFilesQuery.value(0).toString();
  139. databaseFileNames.append(fileName);
  140. if (! QFile::exists(fileName) )
  141. {
  142. filesToRemove.append(fileName);
  143. }
  144. }
  145. QSet<QString> filesytemFiles;
  146. QDirIterator dirIt(directoryName);
  147. while (dirIt.hasNext())
  148. {
  149. filesytemFiles.insert(dirIt.next());
  150. }
  151. // TODO: it looks like this function was never finished...
  152. //
  153. // I guess the next step is to remove all filesToRemove from the database
  154. // and also to add filesystemFiles into the database tables
  155. */
  156. }