ctkDICOMHostMainLogic.cpp 6.5 KB

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