ctkDICOMHostMainLogic.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Qt includes
  2. #include <QDebug>
  3. #include <QFileDialog>
  4. #include <QApplication>
  5. #include <QModelIndex>
  6. #include <QTreeView>
  7. #include <QItemSelectionModel>
  8. // ctk includes
  9. #include "ctkDICOMHostMainLogic.h"
  10. #include "ctkHostedAppPlaceholderWidget.h"
  11. #include "ctkExampleHostControlWidget.h"
  12. #include "ctkExampleDicomHost.h"
  13. #include "ctkDICOMAppWidget.h"
  14. #include "ctkDICOMModel.h"
  15. #include "ctkDICOMDatabase.h"
  16. #include "ctkDicomAvailableDataHelper.h"
  17. ctkDICOMHostMainLogic::ctkDICOMHostMainLogic(ctkHostedAppPlaceholderWidget* placeHolder, ctkDICOMAppWidget* dicomAppWidget,
  18. QWidget* placeHolderForControls) :
  19. QObject(placeHolder),
  20. PlaceHolderForHostedApp(placeHolder),
  21. DicomAppWidget(dicomAppWidget),
  22. PlaceHolderForControls(placeHolderForControls),
  23. ValidSelection(false),
  24. SendData(false)
  25. {
  26. this->Host = new ctkExampleDicomHost(PlaceHolderForHostedApp);
  27. this->HostControls = new ctkExampleHostControlWidget(Host, PlaceHolderForControls);
  28. Data = new ctkDicomAppHosting::AvailableData;
  29. disconnect(this->Host,SIGNAL(startProgress()),this->Host,SLOT(onStartProgress()));
  30. connect(this->Host,SIGNAL(appReady()),this,SLOT(onAppReady()), Qt::QueuedConnection);
  31. connect(this->Host,SIGNAL(startProgress()),this,SLOT(publishSelectedData()), Qt::QueuedConnection);
  32. connect(this->PlaceHolderForHostedApp,SIGNAL(resized()),this,SLOT(placeHolderResized()));
  33. QTreeView * treeview = dicomAppWidget->findChild<QTreeView*>("TreeView");
  34. QItemSelectionModel* selectionmodel = treeview->selectionModel();
  35. connect(selectionmodel, SIGNAL(selectionChanged ( const QItemSelection &, const QItemSelection & )),
  36. this, SLOT(onTreeSelectionChanged(const QItemSelection &, const QItemSelection &)));
  37. connect(this->Host, SIGNAL(dataAvailable()), this, SLOT(onDataAvailable()));
  38. connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()) );
  39. }
  40. ctkDICOMHostMainLogic::~ctkDICOMHostMainLogic()
  41. {
  42. delete Data;
  43. }
  44. void ctkDICOMHostMainLogic::aboutToQuit()
  45. {
  46. this->Host->exitApplicationBlocking();
  47. delete this->Host;
  48. this->Host = 0;
  49. }
  50. void ctkDICOMHostMainLogic::configureHostedApp()
  51. {
  52. //qDebug() << "load button clicked";
  53. AppFileName = QFileDialog::getOpenFileName(PlaceHolderForHostedApp,"Choose hosted application",QApplication::applicationDirPath());
  54. HostControls->setAppFileName(AppFileName);
  55. emit SelectionValid(((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection)));
  56. }
  57. void ctkDICOMHostMainLogic::sendDataToHostedApp()
  58. {
  59. if ((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection))
  60. {
  61. *Data = ctkDicomAppHosting::AvailableData(); // empty AvailableData structure (at least not with the same id...)
  62. foreach (const QString &str, SelectedFiles) {
  63. if (str.isEmpty())
  64. continue;
  65. qDebug() << str;
  66. ctkDicomAvailableDataHelper::addToAvailableData(*Data,
  67. Host->objectLocatorCache(),
  68. str);
  69. }
  70. SendData = true;
  71. if(this->Host->getApplicationState() == ctkDicomAppHosting::EXIT)
  72. {
  73. this->HostControls->StartApplication(this->AppFileName);
  74. }
  75. if(this->Host->getApplicationState() == ctkDicomAppHosting::IDLE)
  76. {
  77. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  78. }
  79. if(this->Host->getApplicationState() == ctkDicomAppHosting::INPROGRESS)
  80. {
  81. publishSelectedData();
  82. }
  83. }
  84. }
  85. void ctkDICOMHostMainLogic::onAppReady()
  86. {
  87. emit SelectionValid(ValidSelection);
  88. if(SendData)
  89. {
  90. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  91. qDebug() << " setState(INPROGRESS) returned: " << reply;
  92. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  93. this->Host->getDicomAppService()->bringToFront(rect);
  94. }
  95. }
  96. void ctkDICOMHostMainLogic::onTreeSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
  97. {
  98. ValidSelection=false;
  99. if(selected.indexes().count() > 0)
  100. {
  101. foreach (const QModelIndex &index, selected.indexes()) {
  102. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  103. QModelIndex index0 = index.sibling(index.row(), 0);
  104. if(model && (model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType)))
  105. {
  106. QString s = "Series selected: ";
  107. QString seriesUID = model->data(index0,ctkDICOMModel::UIDRole).toString();
  108. s.append(seriesUID);
  109. SelectedFiles = DicomAppWidget->database()->filesForSeries(seriesUID);
  110. emit TreeSelectionChanged(s);
  111. ValidSelection=true;
  112. }
  113. }
  114. }
  115. if (ValidSelection==false)
  116. emit TreeSelectionChanged("no series selected");
  117. emit SelectionValid(((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection)));
  118. }
  119. //----------------------------------------------------------------------------
  120. void ctkDICOMHostMainLogic::publishSelectedData()
  121. {
  122. if(SendData)
  123. {
  124. qDebug()<<"send dataDescriptors";
  125. bool success = Host->publishData(*Data, true);
  126. if(!success)
  127. {
  128. qCritical() << "Failed to publish data";
  129. }
  130. qDebug() << " notifyDataAvailable returned: " << success;
  131. SendData=false;
  132. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  133. this->Host->getDicomAppService()->bringToFront(rect);
  134. }
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkDICOMHostMainLogic::placeHolderResized()
  138. {
  139. ///the following does not work (yet)
  140. //if((this->Host) && (this->Host->getApplicationState() != ctkDicomAppHosting::EXIT))
  141. //{
  142. // QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  143. // this->Host->getDicomAppService()->bringToFront(rect);
  144. //}
  145. }
  146. //----------------------------------------------------------------------------
  147. void ctkDICOMHostMainLogic::onDataAvailable()
  148. {
  149. const ctkDicomAppHosting::AvailableData& data = this->Host->getIncomingAvailableData();
  150. QList<QUuid> uuidlist = ctkDicomAvailableDataHelper::getAllUuids(data);
  151. if(uuidlist.count()==0)
  152. return;
  153. QString transfersyntax("1.2.840.10008.1.2.1");
  154. QList<QString> transfersyntaxlist;
  155. transfersyntaxlist.append(transfersyntax);
  156. QList<ctkDicomAppHosting::ObjectLocator> locators;
  157. locators = this->Host->getOtherSideExchangeService()->getData(uuidlist, transfersyntaxlist, false);
  158. qDebug() << "got locators! " << QString().setNum(locators.count());
  159. QString s;
  160. s=s+" loc.count:"+QString().setNum(locators.count());
  161. if(locators.count()>0)
  162. {
  163. s=s+" URI: "+locators.begin()->URI +" locatorUUID: "+locators.begin()->locator+" sourceUUID: "+locators.begin()->source;
  164. qDebug() << "URI: " << locators.begin()->URI;
  165. QString filename = locators.begin()->URI;
  166. if(filename.startsWith("file:/",Qt::CaseInsensitive))
  167. filename=filename.remove(0,8);
  168. qDebug()<<filename;
  169. }
  170. }