ctkDICOMHostMainLogic.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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()));
  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. foreach (const QString &str, SelectedFiles) {
  62. if (str.isEmpty())
  63. continue;
  64. qDebug() << str;
  65. ctkDicomAvailableDataHelper::addToAvailableData(*Data,
  66. Host->objectLocatorCache(),
  67. str);
  68. }
  69. SendData = true;
  70. if(this->Host->getApplicationState() == ctkDicomAppHosting::EXIT)
  71. {
  72. this->HostControls->StartApplication(this->AppFileName);
  73. }
  74. if(this->Host->getApplicationState() == ctkDicomAppHosting::IDLE)
  75. {
  76. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  77. }
  78. if(this->Host->getApplicationState() == ctkDicomAppHosting::INPROGRESS)
  79. {
  80. publishSelectedData();
  81. }
  82. }
  83. }
  84. void ctkDICOMHostMainLogic::onAppReady()
  85. {
  86. emit SelectionValid(ValidSelection);
  87. if(SendData)
  88. {
  89. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  90. qDebug() << " setState(INPROGRESS) returned: " << reply;
  91. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  92. this->Host->getDicomAppService()->bringToFront(rect);
  93. }
  94. }
  95. void ctkDICOMHostMainLogic::onTreeSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
  96. {
  97. ValidSelection=false;
  98. if(selected.indexes().count() > 0)
  99. {
  100. foreach (const QModelIndex &index, selected.indexes()) {
  101. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  102. QModelIndex index0 = index.sibling(index.row(), 0);
  103. if(model && (model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType)))
  104. {
  105. QString s = "Series selected: ";
  106. QString seriesUID = model->data(index0,ctkDICOMModel::UIDRole).toString();
  107. s.append(seriesUID);
  108. SelectedFiles = DicomAppWidget->database()->filesForSeries(seriesUID);
  109. emit TreeSelectionChanged(s);
  110. ValidSelection=true;
  111. }
  112. }
  113. }
  114. if (ValidSelection==false)
  115. emit TreeSelectionChanged("no series selected");
  116. emit SelectionValid(((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection)));
  117. }
  118. //----------------------------------------------------------------------------
  119. void ctkDICOMHostMainLogic::publishSelectedData()
  120. {
  121. if(SendData)
  122. {
  123. qDebug()<<"send dataDescriptors";
  124. bool success = Host->publishData(*Data, true);
  125. if(!success)
  126. {
  127. qCritical() << "Failed to publish data";
  128. }
  129. qDebug() << " notifyDataAvailable returned: " << success;
  130. SendData=false;
  131. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  132. this->Host->getDicomAppService()->bringToFront(rect);
  133. }
  134. }
  135. //----------------------------------------------------------------------------
  136. void ctkDICOMHostMainLogic::placeHolderResized()
  137. {
  138. ///the following does not work (yet)
  139. //if((this->Host) && (this->Host->getApplicationState() != ctkDicomAppHosting::EXIT))
  140. //{
  141. // QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  142. // this->Host->getDicomAppService()->bringToFront(rect);
  143. //}
  144. }
  145. //----------------------------------------------------------------------------
  146. void ctkDICOMHostMainLogic::onDataAvailable()
  147. {
  148. const ctkDicomAppHosting::AvailableData& data = this->Host->getIncomingAvailableData();
  149. QList<QUuid> uuidlist = ctkDicomAvailableDataHelper::getAllUuids(data);
  150. if(uuidlist.count()==0)
  151. return;
  152. QString transfersyntax("1.2.840.10008.1.2.1");
  153. QList<QString> transfersyntaxlist;
  154. transfersyntaxlist.append(transfersyntax);
  155. QList<ctkDicomAppHosting::ObjectLocator> locators;
  156. locators = this->Host->getOtherSideExchangeService()->getData(uuidlist, transfersyntaxlist, false);
  157. qDebug() << "got locators! " << QString().setNum(locators.count());
  158. QString s;
  159. s=s+" loc.count:"+QString().setNum(locators.count());
  160. if(locators.count()>0)
  161. {
  162. s=s+" URI: "+locators.begin()->URI +" locatorUUID: "+locators.begin()->locator+" sourceUUID: "+locators.begin()->source;
  163. qDebug() << "URI: " << locators.begin()->URI;
  164. QString filename = locators.begin()->URI;
  165. if(filename.startsWith("file:/",Qt::CaseInsensitive))
  166. filename=filename.remove(0,8);
  167. qDebug()<<filename;
  168. }
  169. }