ctkDICOMDatabase.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010
  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. #ifndef __ctkDICOMDatabase_h
  15. #define __ctkDICOMDatabase_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QStringList>
  19. #include <QSqlDatabase>
  20. #include "ctkDICOMDataset.h"
  21. #include "ctkDICOMCoreExport.h"
  22. class ctkDICOMDatabasePrivate;
  23. class DcmDataset;
  24. class ctkDICOMAbstractThumbnailGenerator;
  25. /// Class handling a database of DICOM objects. So far, an underlying
  26. /// SQLITE database is used for that. Usually, added DICOM objects are also
  27. /// stored within the file system.
  28. /// The SQLITE database file can be specified by the user. SQLITE (and this
  29. /// class) also support a special in memory mode, where no database file is created
  30. /// but the database is completely kept in memory (and after exiting the program,
  31. /// vanishes). If in "memory mode", the objects are not written to disk,
  32. /// otherwise they are stored in a subdirectory of the SQLITE database file
  33. /// directory called "dicom". Inside, a folder structure created which contains
  34. /// a directoy for each study, containing a directory for each series, containing
  35. /// a file for each object. The corresponding UIDs are used as filenames.
  36. /// Thumbnais for each image can be created; if so, they are stored in a directory
  37. /// parallel to "dicom" directory called "thumbs".
  38. class CTK_DICOM_CORE_EXPORT ctkDICOMDatabase : public QObject
  39. {
  40. Q_OBJECT
  41. Q_PROPERTY(bool isOpen READ isOpen)
  42. Q_PROPERTY(QString lastError READ lastError)
  43. Q_PROPERTY(QString databaseFilename READ databaseFilename)
  44. public:
  45. explicit ctkDICOMDatabase(QObject *parent = 0);
  46. explicit ctkDICOMDatabase(QString databaseFile);
  47. virtual ~ctkDICOMDatabase();
  48. const QSqlDatabase& database() const;
  49. const QString lastError() const;
  50. const QString databaseFilename() const;
  51. ///
  52. /// Returns the absolute path of the database directory
  53. /// (where the database file resides in) in OS-prefered path format.
  54. /// @return Absolute path to database directory
  55. const QString databaseDirectory() const;
  56. ///
  57. /// Should be checked after trying to open the database
  58. /// @Returns true if database is open
  59. bool isOpen() const;
  60. ///
  61. /// Returns whether the database only resides in memory, i.e. the
  62. /// SQLITE DB is not written to stored to disk and DICOM objects are not
  63. /// stored to the file system.
  64. /// @return True if in memory mode, false otherwise.
  65. bool isInMemory() const;
  66. ///
  67. /// set thumbnail generator object
  68. void setThumbnailGenerator(ctkDICOMAbstractThumbnailGenerator* generator);
  69. ///
  70. /// get thumbnail genrator object
  71. ctkDICOMAbstractThumbnailGenerator* thumbnailGenerator();
  72. ///
  73. /// open the SQLite database in @param databaseFile . If the file does not
  74. /// exist, a new database is created and initialized with the
  75. /// default schema
  76. ///
  77. /// @param databaseFile The file to store the SQLITE database should be
  78. /// stored to. If specified with ":memory:", the database is not
  79. /// written to disk at all but instead only kept in memory (and
  80. /// thus expires after destruction of this object).
  81. /// @param connectionName The database connection name.
  82. Q_INVOKABLE virtual void openDatabase(const QString databaseFile,
  83. const QString& connectionName = "DICOM-DB" );
  84. ///
  85. /// close the database. It must not be used afterwards.
  86. Q_INVOKABLE void closeDatabase();
  87. ///
  88. /// delete all data and reinitialize the database.
  89. Q_INVOKABLE bool initializeDatabase(const char* schemaFile = ":/dicom/dicom-schema.sql");
  90. ///
  91. /// \brief database accessors
  92. Q_INVOKABLE QStringList patients ();
  93. Q_INVOKABLE QStringList studiesForPatient (QString patientUID);
  94. Q_INVOKABLE QStringList seriesForStudy (QString studyUID);
  95. Q_INVOKABLE QStringList filesForSeries (QString seriesUID);
  96. ///
  97. /// \brief load the header from a file and allow access to elements
  98. Q_INVOKABLE void loadInstanceHeader (QString sopInstanceUID);
  99. Q_INVOKABLE void loadFileHeader (QString fileName);
  100. Q_INVOKABLE QStringList headerKeys ();
  101. Q_INVOKABLE QString headerValue (QString key);
  102. /// Insert into the database if not already exsting.
  103. /// @param dataset The dataset to store into the database. Usually, this is
  104. /// is a complete DICOM object, like a complete image. However
  105. /// the database also inserts partial objects, like studyl
  106. /// information to the database, even if no image data is
  107. /// contained. This can be helpful to store results from
  108. /// querying the PACS for patient/study/series or image
  109. /// information, where a full hierarchy is only constructed
  110. /// after some queries.
  111. /// @param storeFile If store file is set (default), then the dataset will
  112. /// be stored to disk. Note that in case of a memory-only
  113. /// database, this flag is ignored. Usually, this flag
  114. /// does only make sense if a full object is received.
  115. /// @param @generateThumbnail If true, a thumbnail is generated.
  116. ///
  117. void insert( const ctkDICOMDataset& ctkDataset, bool storeFile, bool generateThumbnail);
  118. void insert ( DcmDataset *dataset, bool storeFile = true, bool generateThumbnail = true);
  119. void insert ( const QString& filePath, bool storeFile = true, bool generateThumbnail = true, bool createHierarchy = true, const QString& destinationDirectoryName = QString() );
  120. /// Check if file is already in database and up-to-date
  121. bool fileExistsAndUpToDate(const QString& filePath);
  122. /// remove the series from the database, including images and
  123. /// thumbnails
  124. bool removeSeries(const QString& seriesInstanceUID);
  125. bool removeStudy(const QString& studyInstanceUID);
  126. bool removePatient(const QString& patientID);
  127. bool cleanup();
  128. Q_SIGNALS:
  129. void databaseChanged();
  130. protected:
  131. QScopedPointer<ctkDICOMDatabasePrivate> d_ptr;
  132. private:
  133. Q_DECLARE_PRIVATE(ctkDICOMDatabase);
  134. Q_DISABLE_COPY(ctkDICOMDatabase);
  135. };
  136. #endif