ctkDICOMQueryRetrieveWidget.cpp 14 KB

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