ctkDICOMDatabase.h 13 KB

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