ctkDICOMQuery.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.commontk.org/LICENSE
  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 <QVariant>
  18. #include <QDate>
  19. #include <QStringList>
  20. #include <QSet>
  21. #include <QFile>
  22. #include <QDirIterator>
  23. #include <QFileInfo>
  24. #include <QDebug>
  25. // ctkDICOMCore includes
  26. #include "ctkDICOMQuery.h"
  27. #include "ctkLogger.h"
  28. // DCMTK includes
  29. #ifndef WIN32
  30. #define HAVE_CONFIG_H
  31. #endif
  32. #include "dcmtk/dcmnet/dimse.h"
  33. #include "dcmtk/dcmnet/diutil.h"
  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/dcmnet/scu.h>
  43. static ctkLogger logger ( "org.commontk.dicom.DICOMQuery" );
  44. //------------------------------------------------------------------------------
  45. class ctkDICOMQueryPrivate
  46. {
  47. public:
  48. ctkDICOMQueryPrivate();
  49. ~ctkDICOMQueryPrivate();
  50. QString CallingAETitle;
  51. QString CalledAETitle;
  52. QString Host;
  53. int Port;
  54. DcmSCU SCU;
  55. DcmDataset* query;
  56. QStringList StudyInstanceUIDList;
  57. };
  58. //------------------------------------------------------------------------------
  59. // ctkDICOMQueryPrivate methods
  60. //------------------------------------------------------------------------------
  61. ctkDICOMQueryPrivate::ctkDICOMQueryPrivate()
  62. {
  63. query = new DcmDataset();
  64. }
  65. //------------------------------------------------------------------------------
  66. ctkDICOMQueryPrivate::~ctkDICOMQueryPrivate()
  67. {
  68. delete query;
  69. }
  70. //------------------------------------------------------------------------------
  71. // ctkDICOMQuery methods
  72. //------------------------------------------------------------------------------
  73. ctkDICOMQuery::ctkDICOMQuery()
  74. : d_ptr(new ctkDICOMQueryPrivate)
  75. {
  76. }
  77. //------------------------------------------------------------------------------
  78. ctkDICOMQuery::~ctkDICOMQuery()
  79. {
  80. }
  81. void ctkDICOMQuery::addStudyInstanceUID ( QString s )
  82. {
  83. Q_D(ctkDICOMQuery);
  84. d->StudyInstanceUIDList.append ( s );
  85. }
  86. /// Set methods for connectivity
  87. void ctkDICOMQuery::setCallingAETitle ( QString callingAETitle )
  88. {
  89. Q_D(ctkDICOMQuery);
  90. d->CallingAETitle = callingAETitle;
  91. }
  92. const QString& ctkDICOMQuery::callingAETitle()
  93. {
  94. Q_D(ctkDICOMQuery);
  95. return d->CallingAETitle;
  96. }
  97. void ctkDICOMQuery::setCalledAETitle ( QString calledAETitle )
  98. {
  99. Q_D(ctkDICOMQuery);
  100. d->CalledAETitle = calledAETitle;
  101. }
  102. const QString& ctkDICOMQuery::calledAETitle()
  103. {
  104. Q_D(ctkDICOMQuery);
  105. return d->CalledAETitle;
  106. }
  107. void ctkDICOMQuery::setHost ( QString host )
  108. {
  109. Q_D(ctkDICOMQuery);
  110. d->Host = host;
  111. }
  112. const QString& ctkDICOMQuery::host()
  113. {
  114. Q_D(ctkDICOMQuery);
  115. return d->Host;
  116. }
  117. void ctkDICOMQuery::setPort ( int port )
  118. {
  119. Q_D(ctkDICOMQuery);
  120. d->Port = port;
  121. }
  122. int ctkDICOMQuery::port()
  123. {
  124. Q_D(ctkDICOMQuery);
  125. return d->Port;
  126. }
  127. //------------------------------------------------------------------------------
  128. void ctkDICOMQuery::query(QSqlDatabase database )
  129. {
  130. ctkDICOMIndexerBase::setDatabase ( database );
  131. Q_D(ctkDICOMQuery);
  132. if ( this->database().isOpen() )
  133. {
  134. logger.debug ( "DB open in Query" );
  135. }
  136. else
  137. {
  138. logger.debug ( "DB not open in Query" );
  139. }
  140. d->StudyInstanceUIDList.clear();
  141. d->SCU.setAETitle ( OFString(this->callingAETitle().toStdString().c_str()) );
  142. d->SCU.setPeerAETitle ( OFString(this->calledAETitle().toStdString().c_str()) );
  143. d->SCU.setPeerHostName ( OFString(this->host().toStdString().c_str()) );
  144. d->SCU.setPeerPort ( this->port() );
  145. logger.error ( "Setting Transfer Syntaxes" );
  146. OFList<OFString> transferSyntaxes;
  147. transferSyntaxes.push_back ( UID_LittleEndianExplicitTransferSyntax );
  148. transferSyntaxes.push_back ( UID_BigEndianExplicitTransferSyntax );
  149. transferSyntaxes.push_back ( UID_LittleEndianImplicitTransferSyntax );
  150. d->SCU.addPresentationContext ( UID_FINDStudyRootQueryRetrieveInformationModel, transferSyntaxes );
  151. // d->SCU.addPresentationContext ( UID_VerificationSOPClass, transferSyntaxes );
  152. if ( !d->SCU.initNetwork().good() )
  153. {
  154. std::cerr << "Error initializing the network" << std::endl;
  155. return;
  156. }
  157. logger.debug ( "Negotiating Association" );
  158. d->SCU.negotiateAssociation();
  159. // Clear the query
  160. unsigned long elements = d->query->card();
  161. // Clean it out
  162. for ( unsigned long i = 0; i < elements; i++ )
  163. {
  164. d->query->remove ( 0ul );
  165. }
  166. d->query->insertEmptyElement ( DCM_PatientID );
  167. d->query->insertEmptyElement ( DCM_PatientsName );
  168. d->query->insertEmptyElement ( DCM_PatientsBirthDate );
  169. d->query->insertEmptyElement ( DCM_StudyID );
  170. d->query->insertEmptyElement ( DCM_StudyInstanceUID );
  171. d->query->insertEmptyElement ( DCM_StudyDescription );
  172. d->query->insertEmptyElement ( DCM_StudyDate );
  173. d->query->insertEmptyElement ( DCM_StudyID );
  174. d->query->insertEmptyElement ( DCM_PatientID );
  175. d->query->insertEmptyElement ( DCM_PatientsName );
  176. d->query->insertEmptyElement ( DCM_SeriesNumber );
  177. d->query->insertEmptyElement ( DCM_SeriesDescription );
  178. d->query->insertEmptyElement ( DCM_StudyInstanceUID );
  179. d->query->insertEmptyElement ( DCM_SeriesInstanceUID );
  180. d->query->insertEmptyElement ( DCM_StudyTime );
  181. d->query->insertEmptyElement ( DCM_SeriesDate );
  182. d->query->insertEmptyElement ( DCM_SeriesTime );
  183. d->query->insertEmptyElement ( DCM_Modality );
  184. d->query->insertEmptyElement ( DCM_ModalitiesInStudy );
  185. d->query->insertEmptyElement ( DCM_AccessionNumber );
  186. d->query->insertEmptyElement ( DCM_NumberOfSeriesRelatedInstances ); // Number of images in the series
  187. d->query->insertEmptyElement ( DCM_NumberOfStudyRelatedInstances ); // Number of images in the series
  188. d->query->insertEmptyElement ( DCM_NumberOfStudyRelatedSeries ); // Number of images in the series
  189. d->query->putAndInsertString ( DCM_QueryRetrieveLevel, "STUDY" );
  190. FINDResponses *responses = new FINDResponses();
  191. Uint16 presentationContex = 0;
  192. presentationContex = d->SCU.findPresentationContextID ( UID_FINDStudyRootQueryRetrieveInformationModel, UID_LittleEndianExplicitTransferSyntax );
  193. if ( presentationContex == 0 )
  194. {
  195. presentationContex = d->SCU.findPresentationContextID ( UID_FINDStudyRootQueryRetrieveInformationModel, UID_BigEndianExplicitTransferSyntax );
  196. }
  197. if ( presentationContex == 0 )
  198. {
  199. presentationContex = d->SCU.findPresentationContextID ( UID_FINDStudyRootQueryRetrieveInformationModel, UID_LittleEndianImplicitTransferSyntax );
  200. }
  201. if ( presentationContex == 0 )
  202. {
  203. logger.error ( "Failed to find acceptable presentation context" );
  204. }
  205. else
  206. {
  207. logger.info ( "Found useful presentation context" );
  208. }
  209. OFCondition status = d->SCU.sendFINDRequest ( presentationContex, d->query, responses );
  210. if ( status.good() )
  211. {
  212. logger.debug ( "Find succeded" );
  213. }
  214. else
  215. {
  216. logger.error ( "Find failed" );
  217. }
  218. for ( OFListIterator(FINDResponse*) it = responses->begin(); it != responses->end(); it++ )
  219. {
  220. DcmDataset *dataset = (*it)->m_dataset;
  221. if ( dataset != NULL )
  222. {
  223. this->insert ( dataset );
  224. OFString StudyInstanceUID;
  225. dataset->findAndGetOFString ( DCM_StudyInstanceUID, StudyInstanceUID );
  226. this->addStudyInstanceUID ( QString ( StudyInstanceUID.c_str() ) );
  227. }
  228. }
  229. delete responses;
  230. // Now search each Study
  231. d->query->putAndInsertString ( DCM_QueryRetrieveLevel, "SERIES" );
  232. foreach ( QString StudyInstanceUID, d->StudyInstanceUIDList )
  233. {
  234. logger.debug ( "Starting Series C-FIND for Series: " + StudyInstanceUID );
  235. d->query->putAndInsertString ( DCM_StudyInstanceUID, StudyInstanceUID.toStdString().c_str() );
  236. responses = new FINDResponses();
  237. status = d->SCU.sendFINDRequest ( 0, d->query, responses );
  238. if ( status.good() )
  239. {
  240. logger.debug ( "Find succeded for Series: " + StudyInstanceUID );
  241. for ( OFListIterator(FINDResponse*) it = responses->begin(); it != responses->end(); it++ )
  242. {
  243. DcmDataset *dataset = (*it)->m_dataset;
  244. if ( dataset != NULL )
  245. {
  246. this->insert ( dataset );
  247. }
  248. }
  249. }
  250. else
  251. {
  252. logger.error ( "Find failed for Series: " + StudyInstanceUID );
  253. }
  254. delete responses;
  255. }
  256. d->SCU.closeAssociation ( DUL_PEERREQUESTEDRELEASE );
  257. }