ctkDICOMDatabase.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /// \ingroup DICOM_Core
  26. ///
  27. /// Class handling a database of DICOM objects. So far, an underlying
  28. /// SQLITE database is used for that. Usually, added DICOM objects are also
  29. /// stored within the file system.
  30. /// The SQLITE database file can be specified by the user. SQLITE (and this
  31. /// class) also support a special in memory mode, where no database file is created
  32. /// but the database is completely kept in memory (and after exiting the program,
  33. /// vanishes). If in "memory mode", the objects are not written to disk,
  34. /// otherwise they are stored in a subdirectory of the SQLITE database file
  35. /// directory called "dicom". Inside, a folder structure created which contains
  36. /// a directoy for each study, containing a directory for each series, containing
  37. /// a file for each object. The corresponding UIDs are used as filenames.
  38. /// Thumbnais for each image can be created; if so, they are stored in a directory
  39. /// parallel to "dicom" directory called "thumbs".
  40. class CTK_DICOM_CORE_EXPORT ctkDICOMDatabase : public QObject
  41. {
  42. Q_OBJECT
  43. Q_PROPERTY(bool isOpen READ isOpen)
  44. Q_PROPERTY(QString lastError READ lastError)
  45. Q_PROPERTY(QString databaseFilename READ databaseFilename)
  46. public:
  47. explicit ctkDICOMDatabase(QObject *parent = 0);
  48. explicit ctkDICOMDatabase(QString databaseFile);
  49. virtual ~ctkDICOMDatabase();
  50. const QSqlDatabase& database() const;
  51. const QString lastError() const;
  52. const QString databaseFilename() const;
  53. ///
  54. /// Returns the absolute path of the database directory
  55. /// (where the database file resides in) in OS-prefered path format.
  56. /// @return Absolute path to database directory
  57. const QString databaseDirectory() const;
  58. ///
  59. /// Should be checked after trying to open the database
  60. /// @Returns true if database is open
  61. bool isOpen() const;
  62. ///
  63. /// Returns whether the database only resides in memory, i.e. the
  64. /// SQLITE DB is not written to stored to disk and DICOM objects are not
  65. /// stored to the file system.
  66. /// @return True if in memory mode, false otherwise.
  67. bool isInMemory() const;
  68. ///
  69. /// set thumbnail generator object
  70. void setThumbnailGenerator(ctkDICOMAbstractThumbnailGenerator* generator);
  71. ///
  72. /// get thumbnail genrator object
  73. ctkDICOMAbstractThumbnailGenerator* thumbnailGenerator();
  74. ///
  75. /// open the SQLite database in @param databaseFile . If the file does not
  76. /// exist, a new database is created and initialized with the
  77. /// default schema
  78. ///
  79. /// @param databaseFile The file to store the SQLITE database should be
  80. /// stored to. If specified with ":memory:", the database is not
  81. /// written to disk at all but instead only kept in memory (and
  82. /// thus expires after destruction of this object).
  83. /// @param connectionName The database connection name.
  84. /// @param update the schema if it is found to be out of date
  85. Q_INVOKABLE virtual void openDatabase(const QString databaseFile,
  86. const QString& connectionName = "DICOM-DB");
  87. ///
  88. /// close the database. It must not be used afterwards.
  89. Q_INVOKABLE void closeDatabase();
  90. ///
  91. /// delete all data and (re-)initialize the database.
  92. Q_INVOKABLE bool initializeDatabase(const char* schemaFile = ":/dicom/dicom-schema.sql");
  93. /// updates the database schema and reinserts all existing files
  94. Q_INVOKABLE bool updateSchema(const char* schemaFile = ":/dicom/dicom-schema.sql");
  95. /// updates the database schema only if the versions don't match
  96. /// Returns true if schema was updated
  97. Q_INVOKABLE bool updateSchemaIfNeeded(const char* schemaFile = ":/dicom/dicom-schema.sql");
  98. /// returns the schema version needed by the current version of this code
  99. Q_INVOKABLE QString schemaVersion();
  100. /// returns the schema version for the currently open database
  101. /// in order to support schema updating
  102. Q_INVOKABLE QString schemaVersionLoaded();
  103. ///
  104. /// \brief database accessors
  105. Q_INVOKABLE QStringList patients ();
  106. Q_INVOKABLE QStringList studiesForPatient (const QString patientUID);
  107. Q_INVOKABLE QStringList seriesForStudy (const QString studyUID);
  108. Q_INVOKABLE QStringList filesForSeries (const QString seriesUID);
  109. Q_INVOKABLE QString fileForInstance (const QString sopInstanceUID);
  110. Q_INVOKABLE QString instanceForFile (const QString fileName);
  111. Q_INVOKABLE QStringList allFiles ();
  112. ///
  113. /// \brief load the header from a file and allow access to elements
  114. /// @param sopInstanceUID A string with the uid for a given instance
  115. /// (corresponding file will be found via database)
  116. /// @param fileName Full path to a dicom file to load.
  117. /// @param key A group,element tag in zero-filled hex
  118. Q_INVOKABLE void loadInstanceHeader (const QString sopInstanceUID);
  119. Q_INVOKABLE void loadFileHeader (const QString fileName);
  120. Q_INVOKABLE QStringList headerKeys ();
  121. Q_INVOKABLE QString headerValue (const QString key);
  122. /// Insert into the database if not already exsting.
  123. /// @param dataset The dataset to store into the database. Usually, this is
  124. /// is a complete DICOM object, like a complete image. However
  125. /// the database also inserts partial objects, like studyl
  126. /// information to the database, even if no image data is
  127. /// contained. This can be helpful to store results from
  128. /// querying the PACS for patient/study/series or image
  129. /// information, where a full hierarchy is only constructed
  130. /// after some queries.
  131. /// @param storeFile If store file is set (default), then the dataset will
  132. /// be stored to disk. Note that in case of a memory-only
  133. /// database, this flag is ignored. Usually, this flag
  134. /// does only make sense if a full object is received.
  135. /// @param @generateThumbnail If true, a thumbnail is generated.
  136. ///
  137. Q_INVOKABLE void insert( const ctkDICOMDataset& ctkDataset, bool storeFile, bool generateThumbnail);
  138. void insert ( DcmDataset *dataset, bool storeFile = true, bool generateThumbnail = true);
  139. Q_INVOKABLE void insert ( const QString& filePath, bool storeFile = true, bool generateThumbnail = true, bool createHierarchy = true, const QString& destinationDirectoryName = QString() );
  140. /// Check if file is already in database and up-to-date
  141. bool fileExistsAndUpToDate(const QString& filePath);
  142. /// remove the series from the database, including images and
  143. /// thumbnails
  144. Q_INVOKABLE bool removeSeries(const QString& seriesInstanceUID);
  145. Q_INVOKABLE bool removeStudy(const QString& studyInstanceUID);
  146. Q_INVOKABLE bool removePatient(const QString& patientID);
  147. bool cleanup();
  148. ///
  149. /// \brief access element values for given instance
  150. /// @param sopInstanceUID A string with the uid for a given instance
  151. /// (corresponding file will be found via database)
  152. /// @param fileName Full path to a dicom file to load.
  153. /// @param key A group,element tag in zero-filled hex
  154. /// @param group The group portion of the tag as an integer
  155. /// @param element The element portion of the tag as an integer
  156. /// @Returns empty string if element is missing
  157. Q_INVOKABLE QString instanceValue (const QString sopInstanceUID, const QString tag);
  158. Q_INVOKABLE QString instanceValue (const QString sopInstanceUID, const unsigned short group, const unsigned short element);
  159. Q_INVOKABLE QString fileValue (const QString fileName, const QString tag);
  160. Q_INVOKABLE QString fileValue (const QString fileName, const unsigned short group, const unsigned short element);
  161. Q_INVOKABLE bool tagToGroupElement (const QString tag, unsigned short& group, unsigned short& element);
  162. Q_INVOKABLE QString groupElementToTag (const unsigned short& group, const unsigned short& element);
  163. ///
  164. /// \brief store values of previously requested instance elements
  165. /// These are meant to be internal methods used by the instanceValue and fileValue
  166. /// methods, but they can be used by calling classes to populate or access
  167. /// instance tag values as needed.
  168. /// @param sopInstanceUID A string with the uid for a given instance
  169. /// (corresponding file will be found via database)
  170. /// @param key A group,element tag in zero-filled hex
  171. /// @Returns empty string if element for uid is missing from cache
  172. ///
  173. /// Lightweight check of tag cache existence (once db check per runtime)
  174. Q_INVOKABLE bool tagCacheExists ();
  175. /// Create a tagCache in the current database. Delete the existing one if it exists.
  176. Q_INVOKABLE bool initializeTagCache ();
  177. /// Return the value of a cached tag
  178. Q_INVOKABLE QString cachedTag (const QString sopInstanceUID, const QString tag);
  179. /// Insert an instance tag's value into to the cache
  180. Q_INVOKABLE bool cacheTag (const QString sopInstanceUID, const QString tag, const QString value);
  181. Q_SIGNALS:
  182. void databaseChanged();
  183. /// Indicates that the schema is about to be updated and how many files will be processed
  184. void schemaUpdateStarted(int);
  185. /// Indicates progress in updating schema (int is file number, string is file name)
  186. void schemaUpdateProgress(int);
  187. void schemaUpdateProgress(QString);
  188. /// Indicates schema update finished
  189. void schemaUpdated();
  190. protected:
  191. QScopedPointer<ctkDICOMDatabasePrivate> d_ptr;
  192. private:
  193. Q_DECLARE_PRIVATE(ctkDICOMDatabase);
  194. Q_DISABLE_COPY(ctkDICOMDatabase);
  195. };
  196. #endif