ctkDICOMIndexer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include <QtConcurrentRun>
  28. // ctkDICOM includes
  29. #include "ctkLogger.h"
  30. #include "ctkDICOMIndexer.h"
  31. #include "ctkDICOMIndexerPrivate.h"
  32. #include "ctkDICOMDatabase.h"
  33. // DCMTK includes
  34. #include <dcmtk/dcmdata/dcfilefo.h>
  35. #include <dcmtk/dcmdata/dcfilefo.h>
  36. #include <dcmtk/dcmdata/dcdeftag.h>
  37. #include <dcmtk/dcmdata/dcdatset.h>
  38. #include <dcmtk/ofstd/ofcond.h>
  39. #include <dcmtk/ofstd/ofstring.h>
  40. #include <dcmtk/ofstd/ofstd.h> /* for class OFStandard */
  41. #include <dcmtk/dcmdata/dcddirif.h> /* for class DicomDirInterface */
  42. #include <dcmtk/dcmimgle/dcmimage.h> /* for class DicomImage */
  43. #include <dcmtk/dcmimage/diregist.h> /* include support for color images */
  44. class AddFileFunctor
  45. {
  46. public:
  47. AddFileFunctor(ctkDICOMIndexer* indexer, ctkDICOMDatabase& database,
  48. const QString& destinationDirectoryName = "")
  49. : Indexer(indexer), Database(database), DestinationDirectoryName(destinationDirectoryName) { }
  50. bool operator()(const QString &filePath)
  51. {
  52. Indexer->addFile(Database,filePath,DestinationDirectoryName);
  53. return false; // make sure it is removed;
  54. }
  55. ctkDICOMIndexer* Indexer;
  56. ctkDICOMDatabase& Database;
  57. QString DestinationDirectoryName;
  58. };
  59. //------------------------------------------------------------------------------
  60. static ctkLogger logger("org.commontk.dicom.DICOMIndexer" );
  61. //------------------------------------------------------------------------------
  62. //------------------------------------------------------------------------------
  63. // ctkDICOMIndexerPrivate methods
  64. //------------------------------------------------------------------------------
  65. ctkDICOMIndexerPrivate::ctkDICOMIndexerPrivate(ctkDICOMIndexer& o) : q_ptr(&o), Canceled(false), CurrentPercentageProgress(-1)
  66. {
  67. Q_Q(ctkDICOMIndexer);
  68. connect(&DirectoryImportWatcher,SIGNAL(progressValueChanged(int)),this,SLOT(OnProgress(int)));
  69. connect(&DirectoryImportWatcher,SIGNAL(finished()),q,SIGNAL(indexingComplete()));
  70. connect(&DirectoryImportWatcher,SIGNAL(canceled()),q,SIGNAL(indexingComplete()));
  71. }
  72. //------------------------------------------------------------------------------
  73. ctkDICOMIndexerPrivate::~ctkDICOMIndexerPrivate()
  74. {
  75. DirectoryImportWatcher.cancel();
  76. DirectoryImportWatcher.waitForFinished();
  77. }
  78. void ctkDICOMIndexerPrivate::OnProgress(int progress)
  79. {
  80. Q_Q(ctkDICOMIndexer);
  81. int newPercentageProgress = ( 100 * DirectoryImportFuture.progressValue() ) / DirectoryImportFuture.progressMaximum();
  82. if (newPercentageProgress != CurrentPercentageProgress)
  83. {
  84. CurrentPercentageProgress = newPercentageProgress;
  85. emit q->progress(newPercentageProgress);
  86. }
  87. }
  88. //------------------------------------------------------------------------------
  89. //------------------------------------------------------------------------------
  90. // ctkDICOMIndexer methods
  91. //------------------------------------------------------------------------------
  92. ctkDICOMIndexer::ctkDICOMIndexer(QObject *parent):d_ptr(new ctkDICOMIndexerPrivate(*this))
  93. {
  94. Q_UNUSED(parent);
  95. }
  96. //------------------------------------------------------------------------------
  97. ctkDICOMIndexer::~ctkDICOMIndexer()
  98. {
  99. }
  100. //------------------------------------------------------------------------------
  101. void ctkDICOMIndexer::addFile(ctkDICOMDatabase& database,
  102. const QString filePath,
  103. const QString& destinationDirectoryName)
  104. {
  105. std::cout << filePath.toStdString();
  106. if (!destinationDirectoryName.isEmpty())
  107. {
  108. logger.warn("Ignoring destinationDirectoryName parameter, just taking it as indication we should copy!");
  109. }
  110. emit indexingFilePath(filePath);
  111. database.insert(filePath, !destinationDirectoryName.isEmpty(), true);
  112. }
  113. //------------------------------------------------------------------------------
  114. void ctkDICOMIndexer::addDirectory(ctkDICOMDatabase& ctkDICOMDatabase,
  115. const QString& directoryName,
  116. const QString& destinationDirectoryName)
  117. {
  118. Q_D(ctkDICOMIndexer);
  119. // currently it is not supported to have multiple
  120. // parallel directory imports so the second call blocks
  121. //
  122. d->DirectoryImportWatcher.waitForFinished();
  123. const std::string src_directory(directoryName.toStdString());
  124. OFList<OFString> originalDcmtkFileNames;
  125. OFList<OFString> dcmtkFileNames;
  126. OFStandard::searchDirectoryRecursively( QDir::toNativeSeparators(src_directory.c_str()).toAscii().data(), originalDcmtkFileNames, "", "");
  127. int totalNumberOfFiles = originalDcmtkFileNames.size();
  128. // hack to reverse list of filenames (not neccessary when image loading works correctly)
  129. for ( OFListIterator(OFString) iter = originalDcmtkFileNames.begin(); iter != originalDcmtkFileNames.end(); ++iter )
  130. {
  131. dcmtkFileNames.push_front( *iter );
  132. }
  133. OFListIterator(OFString) iter = dcmtkFileNames.begin();
  134. OFListIterator(OFString) last = dcmtkFileNames.end();
  135. if(iter == last) return;
  136. emit foundFilesToIndex(totalNumberOfFiles);
  137. /* iterate over all input filenames */
  138. int fileNumber = 0;
  139. int currentProgress = -1;
  140. d->Canceled = false;
  141. while (iter != last)
  142. {
  143. if (d->Canceled)
  144. {
  145. break;
  146. }
  147. emit indexingFileNumber(++fileNumber);
  148. int newProgress = ( fileNumber * 100 ) / totalNumberOfFiles;
  149. if (newProgress != currentProgress)
  150. {
  151. currentProgress = newProgress;
  152. emit progress( currentProgress );
  153. }
  154. QString filePath((*iter).c_str());
  155. d->FilesToIndex << filePath;
  156. ++iter;
  157. }
  158. d->DirectoryImportFuture = QtConcurrent::filter(d->FilesToIndex,AddFileFunctor(this,ctkDICOMDatabase,destinationDirectoryName));
  159. d->DirectoryImportWatcher.setFuture(d->DirectoryImportFuture);
  160. }
  161. //------------------------------------------------------------------------------
  162. void ctkDICOMIndexer::refreshDatabase(ctkDICOMDatabase& dicomDatabase, const QString& directoryName)
  163. {
  164. Q_UNUSED(dicomDatabase);
  165. Q_UNUSED(directoryName);
  166. /*
  167. * Probably this should go to the database class as well
  168. * Or we have to extend the interface to make possible what we do here
  169. * without using SQL directly
  170. /// get all filenames from the database
  171. QSqlQuery allFilesQuery(dicomDatabase.database());
  172. QStringList databaseFileNames;
  173. QStringList filesToRemove;
  174. this->loggedExec(allFilesQuery, "SELECT Filename from Images;");
  175. while (allFilesQuery.next())
  176. {
  177. QString fileName = allFilesQuery.value(0).toString();
  178. databaseFileNames.append(fileName);
  179. if (! QFile::exists(fileName) )
  180. {
  181. filesToRemove.append(fileName);
  182. }
  183. }
  184. QSet<QString> filesytemFiles;
  185. QDirIterator dirIt(directoryName);
  186. while (dirIt.hasNext())
  187. {
  188. filesytemFiles.insert(dirIt.next());
  189. }
  190. // TODO: it looks like this function was never finished...
  191. //
  192. // I guess the next step is to remove all filesToRemove from the database
  193. // and also to add filesystemFiles into the database tables
  194. */
  195. }
  196. //----------------------------------------------------------------------------
  197. void ctkDICOMIndexer::cancel()
  198. {
  199. Q_D(ctkDICOMIndexer);
  200. d->DirectoryImportWatcher.cancel();
  201. }