ctkDICOMQueryRetrieveWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <QDebug>
  16. #include <QLabel>
  17. #include <QMessageBox>
  18. #include <QProgressDialog>
  19. #include <QSettings>
  20. #include <QTreeView>
  21. #include <QTabBar>
  22. /// CTK includes
  23. #include <ctkCheckableHeaderView.h>
  24. #include <ctkCheckableModelHelper.h>
  25. #include <ctkLogger.h>
  26. // ctkDICOMCore includes
  27. #include "ctkDICOMDatabase.h"
  28. #include "ctkDICOMModel.h"
  29. #include "ctkDICOMQuery.h"
  30. #include "ctkDICOMRetrieve.h"
  31. // ctkDICOMWidgets includes
  32. #include "ctkDICOMQueryRetrieveWidget.h"
  33. #include "ctkDICOMQueryResultsTabWidget.h"
  34. #include "ui_ctkDICOMQueryRetrieveWidget.h"
  35. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMQueryRetrieveWidget");
  36. //----------------------------------------------------------------------------
  37. class ctkDICOMQueryRetrieveWidgetPrivate: public Ui_ctkDICOMQueryRetrieveWidget
  38. {
  39. Q_DECLARE_PUBLIC( ctkDICOMQueryRetrieveWidget );
  40. protected:
  41. ctkDICOMQueryRetrieveWidget* const q_ptr;
  42. public:
  43. ctkDICOMQueryRetrieveWidgetPrivate(ctkDICOMQueryRetrieveWidget& obj);
  44. ~ctkDICOMQueryRetrieveWidgetPrivate();
  45. void init();
  46. QMap<QString, ctkDICOMQuery*> QueriesByServer;
  47. QMap<QString, ctkDICOMQuery*> QueriesByStudyUID;
  48. QMap<QString, ctkDICOMRetrieve*> RetrievalsByStudyUID;
  49. ctkDICOMDatabase QueryResultDatabase;
  50. QSharedPointer<ctkDICOMDatabase> RetrieveDatabase;
  51. ctkDICOMModel Model;
  52. QProgressDialog* ProgressDialog;
  53. QString CurrentServer;
  54. };
  55. //----------------------------------------------------------------------------
  56. // ctkDICOMQueryRetrieveWidgetPrivate methods
  57. //----------------------------------------------------------------------------
  58. ctkDICOMQueryRetrieveWidgetPrivate::ctkDICOMQueryRetrieveWidgetPrivate(
  59. ctkDICOMQueryRetrieveWidget& obj)
  60. : q_ptr(&obj)
  61. {
  62. this->ProgressDialog = 0;
  63. }
  64. //----------------------------------------------------------------------------
  65. ctkDICOMQueryRetrieveWidgetPrivate::~ctkDICOMQueryRetrieveWidgetPrivate()
  66. {
  67. foreach(ctkDICOMQuery* query, this->QueriesByServer.values())
  68. {
  69. delete query;
  70. }
  71. foreach(ctkDICOMRetrieve* retrieval, this->RetrievalsByStudyUID.values())
  72. {
  73. delete retrieval;
  74. }
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkDICOMQueryRetrieveWidgetPrivate::init()
  78. {
  79. Q_Q(ctkDICOMQueryRetrieveWidget);
  80. this->setupUi(q);
  81. QObject::connect(this->QueryButton, SIGNAL(clicked()), q, SLOT(query()));
  82. QObject::connect(this->RetrieveButton, SIGNAL(clicked()), q, SLOT(retrieve()));
  83. QObject::connect(this->CancelButton, SIGNAL(clicked()), q, SLOT(cancel()));
  84. this->results->setModel(&this->Model);
  85. this->results->setSelectionMode(QAbstractItemView::ExtendedSelection);
  86. this->results->setSelectionBehavior(QAbstractItemView::SelectRows);
  87. QObject::connect(this->results->selectionModel(),
  88. SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
  89. q, SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
  90. }
  91. //----------------------------------------------------------------------------
  92. // ctkDICOMQueryRetrieveWidget methods
  93. //----------------------------------------------------------------------------
  94. ctkDICOMQueryRetrieveWidget::ctkDICOMQueryRetrieveWidget(QWidget* parentWidget)
  95. : Superclass(parentWidget)
  96. , d_ptr(new ctkDICOMQueryRetrieveWidgetPrivate(*this))
  97. {
  98. Q_D(ctkDICOMQueryRetrieveWidget);
  99. d->init();
  100. }
  101. //----------------------------------------------------------------------------
  102. ctkDICOMQueryRetrieveWidget::~ctkDICOMQueryRetrieveWidget()
  103. {
  104. }
  105. //----------------------------------------------------------------------------
  106. void ctkDICOMQueryRetrieveWidget::setRetrieveDatabase(QSharedPointer<ctkDICOMDatabase> dicomDatabase)
  107. {
  108. Q_D(ctkDICOMQueryRetrieveWidget);
  109. d->RetrieveDatabase = dicomDatabase;
  110. }
  111. //----------------------------------------------------------------------------
  112. QSharedPointer<ctkDICOMDatabase> ctkDICOMQueryRetrieveWidget::retrieveDatabase()const
  113. {
  114. Q_D(const ctkDICOMQueryRetrieveWidget);
  115. return d->RetrieveDatabase;
  116. }
  117. //----------------------------------------------------------------------------
  118. void ctkDICOMQueryRetrieveWidget::query()
  119. {
  120. Q_D(ctkDICOMQueryRetrieveWidget);
  121. d->RetrieveButton->setEnabled(false);
  122. // create a database in memory to hold query results
  123. try { d->QueryResultDatabase.openDatabase( ":memory:", "QUERY-DB" ); }
  124. catch (std::exception e)
  125. {
  126. logger.error ( "Database error: " + d->QueryResultDatabase.lastError() );
  127. d->QueryResultDatabase.closeDatabase();
  128. return;
  129. }
  130. d->QueriesByStudyUID.clear();
  131. // for each of the selected server nodes, send the query
  132. QProgressDialog progress("Query DICOM servers", "Cancel", 0, 100, this,
  133. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  134. // We don't want the progress dialog to resize itself, so we bypass the label
  135. // by creating our own
  136. QLabel* progressLabel = new QLabel(tr("Initialization..."));
  137. progress.setLabel(progressLabel);
  138. d->ProgressDialog = &progress;
  139. progress.setWindowModality(Qt::WindowModal);
  140. progress.setMinimumDuration(0);
  141. progress.setValue(0);
  142. foreach (d->CurrentServer, d->ServerNodeWidget->selectedServerNodes())
  143. {
  144. if (progress.wasCanceled())
  145. {
  146. break;
  147. }
  148. QMap<QString, QVariant> parameters =
  149. d->ServerNodeWidget->serverNodeParameters(d->CurrentServer);
  150. // if we are here it's because the server node was checked
  151. Q_ASSERT(parameters["CheckState"] == static_cast<int>(Qt::Checked) );
  152. // create a query for the current server
  153. ctkDICOMQuery* query = new ctkDICOMQuery;
  154. query->setCallingAETitle(d->ServerNodeWidget->callingAETitle());
  155. query->setCalledAETitle(parameters["AETitle"].toString());
  156. query->setHost(parameters["Address"].toString());
  157. query->setPort(parameters["Port"].toInt());
  158. // populate the query with the current search options
  159. query->setFilters( d->QueryWidget->parameters() );
  160. try
  161. {
  162. connect(query, SIGNAL(progress(QString)),
  163. //&progress, SLOT(setLabelText(QString)));
  164. progressLabel, SLOT(setText(QString)));
  165. // for some reasons, setLabelText() doesn't refresh the dialog.
  166. connect(query, SIGNAL(progress(int)),
  167. this, SLOT(onQueryProgressChanged(int)));
  168. // run the query against the selected server and put results in database
  169. query->query ( d->QueryResultDatabase );
  170. disconnect(query, SIGNAL(progress(QString)),
  171. //&progress, SLOT(setLabelText(QString)));
  172. progressLabel, SLOT(setText(QString)));
  173. disconnect(query, SIGNAL(progress(int)),
  174. this, SLOT(onQueryProgressChanged(int)));
  175. }
  176. catch (std::exception e)
  177. {
  178. logger.error ( "Query error: " + parameters["Name"].toString() );
  179. progress.setLabelText("Query error: " + parameters["Name"].toString());
  180. delete query;
  181. }
  182. d->QueriesByServer[d->CurrentServer] = query;
  183. foreach( QString studyUID, query->studyInstanceUIDQueried() )
  184. {
  185. d->QueriesByStudyUID[studyUID] = query;
  186. }
  187. }
  188. // checkable headers - allow user to select the patient/studies to retrieve
  189. d->Model.setDatabase(d->QueryResultDatabase.database());
  190. progress.setValue(progress.maximum());
  191. d->ProgressDialog = 0;
  192. }
  193. //----------------------------------------------------------------------------
  194. void ctkDICOMQueryRetrieveWidget::retrieve()
  195. {
  196. Q_D(ctkDICOMQueryRetrieveWidget);
  197. if (!d->RetrieveButton->isEnabledTo(this))
  198. {
  199. return;
  200. }
  201. // for each of the selected server nodes, send the query
  202. QProgressDialog progress("Retrieve from DICOM servers", "Cancel", 0, 100, this,
  203. Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  204. // We don't want the progress dialog to resize itself, so we bypass the label
  205. // by creating our own
  206. QLabel* progressLabel = new QLabel(tr("Initialization..."));
  207. progress.setLabel(progressLabel);
  208. d->ProgressDialog = &progress;
  209. progress.setWindowModality(Qt::WindowModal);
  210. progress.setMinimumDuration(0);
  211. progress.setValue(0);
  212. QMap<QString,QVariant> serverParameters = d->ServerNodeWidget->parameters();
  213. ctkDICOMRetrieve *retrieve = new ctkDICOMRetrieve;
  214. // only start new association if connection parameters change
  215. retrieve->setKeepAssociationOpen(true);
  216. // pull from GUI
  217. retrieve->setMoveDestinationAETitle( serverParameters["StorageAETitle"].toString() );
  218. int step = 0;
  219. progress.setMaximum(d->QueriesByStudyUID.keys().size());
  220. progress.open();
  221. progress.setValue(1);
  222. // do the rerieval for each selected series
  223. foreach( QString studyUID, d->QueriesByStudyUID.keys() )
  224. {
  225. if (progress.wasCanceled())
  226. {
  227. break;
  228. }
  229. progressLabel->setText(QString(tr("Retrieving:\n%1")).arg(studyUID));
  230. this->updateRetrieveProgress( step );
  231. ++step;
  232. // Get information which server we want to get the study from and prepare request accordingly
  233. ctkDICOMQuery *query = d->QueriesByStudyUID[studyUID];
  234. retrieve->setDatabase( d->RetrieveDatabase );
  235. retrieve->setCallingAETitle( query->callingAETitle() );
  236. retrieve->setCalledAETitle( query->calledAETitle() );
  237. retrieve->setPort( query->port() );
  238. retrieve->setHost( query->host() );
  239. // TODO: check the model item to see if it is checked
  240. // for now, assume all studies queried and shown to the user will be retrieved
  241. logger.debug("About to retrieve " + studyUID + " from " + d->QueriesByStudyUID[studyUID]->host());
  242. logger.info ( "Starting to retrieve" );
  243. try
  244. {
  245. // perform the retrieve
  246. // TODO: give the option to use MOVE instead of CGET
  247. //retrieve->moveStudy ( studyUID );
  248. retrieve->getStudy ( studyUID );
  249. }
  250. catch (std::exception e)
  251. {
  252. logger.error ( "Retrieve failed" );
  253. if ( QMessageBox::question ( this,
  254. tr("Query Retrieve"), tr("Retrieve failed. Keep trying?"),
  255. QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
  256. {
  257. continue;
  258. }
  259. else
  260. {
  261. break;
  262. }
  263. }
  264. // Store retrieve structure for later use.
  265. // Comment MO: I do not think that makes much sense; you store per study one fat
  266. // structure including an SCU. Also, I switched the code to re-use the retrieve
  267. // SCU in order to not start/stop the association for every study. In general,
  268. // it would make most sense in my opinion to have one SCU for each server you
  269. // like to retrieve from. There is no good reason to have one for each study.
  270. // d->RetrievalsByStudyUID[studyUID] = retrieve;
  271. logger.info ( "Retrieve success" );
  272. }
  273. progressLabel->setText(tr("Retrieving Finished"));
  274. this->updateRetrieveProgress(progress.maximum());
  275. delete retrieve;
  276. d->ProgressDialog = 0;
  277. QMessageBox::information ( this, tr("Query Retrieve"), tr("Retrieve Process Finished.") );
  278. emit studiesRetrieved(d->RetrievalsByStudyUID.keys());
  279. }
  280. //----------------------------------------------------------------------------
  281. void ctkDICOMQueryRetrieveWidget::cancel()
  282. {
  283. emit studiesRetrieved(QStringList());
  284. emit canceled();
  285. }
  286. //----------------------------------------------------------------------------
  287. void ctkDICOMQueryRetrieveWidget::onQueryProgressChanged(int value)
  288. {
  289. Q_D(ctkDICOMQueryRetrieveWidget);
  290. if (d->ProgressDialog == 0)
  291. {
  292. return;
  293. }
  294. QStringList servers = d->ServerNodeWidget->selectedServerNodes();
  295. int serverIndex = servers.indexOf(d->CurrentServer);
  296. if (serverIndex < 0)
  297. {
  298. return;
  299. }
  300. if (d->ProgressDialog->width() != 500)
  301. {
  302. QPoint pp = this->mapToGlobal(QPoint(0,0));
  303. pp = QPoint(pp.x() + (this->width() - d->ProgressDialog->width()) / 2,
  304. pp.y() + (this->height() - d->ProgressDialog->height())/ 2);
  305. d->ProgressDialog->move(pp - QPoint((500 - d->ProgressDialog->width())/2, 0));
  306. d->ProgressDialog->resize(500, d->ProgressDialog->height());
  307. }
  308. float serverProgress = 100. / servers.size();
  309. d->ProgressDialog->setValue( (serverIndex + (value / 101.)) * serverProgress);
  310. }
  311. //----------------------------------------------------------------------------
  312. void ctkDICOMQueryRetrieveWidget::updateRetrieveProgress(int value)
  313. {
  314. Q_D(ctkDICOMQueryRetrieveWidget);
  315. if (d->ProgressDialog == 0)
  316. {
  317. return;
  318. }
  319. if (d->ProgressDialog->width() != 500)
  320. {
  321. QPoint pp = this->mapToGlobal(QPoint(0,0));
  322. pp = QPoint(pp.x() + (this->width() - d->ProgressDialog->width()) / 2,
  323. pp.y() + (this->height() - d->ProgressDialog->height())/ 2);
  324. d->ProgressDialog->move(pp - QPoint((500 - d->ProgressDialog->width())/2, 0));
  325. d->ProgressDialog->resize(500, d->ProgressDialog->height());
  326. }
  327. d->ProgressDialog->setValue( value );
  328. logger.error(QString("setting value to %1").arg(value) );
  329. }
  330. //----------------------------------------------------------------------------
  331. void ctkDICOMQueryRetrieveWidget::onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
  332. {
  333. Q_UNUSED(selected);
  334. Q_UNUSED(deselected);
  335. Q_D(ctkDICOMQueryRetrieveWidget);
  336. logger.debug("Selection change");
  337. d->RetrieveButton->setEnabled(d->results->selectionModel()->hasSelection());
  338. }