ctkDICOMQueryRetrieveWidget.cpp 15 KB

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