ctkDICOMHostMainLogic.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  38. ctkDICOMHostMainLogic::~ctkDICOMHostMainLogic()
  39. {
  40. delete Data;
  41. }
  42. void ctkDICOMHostMainLogic::configureHostedApp()
  43. {
  44. //qDebug() << "load button clicked";
  45. AppFileName = QFileDialog::getOpenFileName(PlaceHolderForHostedApp,"Choose hosted application",QApplication::applicationDirPath());
  46. HostControls->setAppFileName(AppFileName);
  47. emit SelectionValid(((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection)));
  48. }
  49. void ctkDICOMHostMainLogic::sendDataToHostedApp()
  50. {
  51. if ((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection))
  52. {
  53. foreach (const QString &str, SelectedFiles) {
  54. if (str.isEmpty())
  55. continue;
  56. qDebug() << str;
  57. ctkDicomAvailableDataHelper::addToAvailableData(*Data,
  58. Host->objectLocatorCache(),
  59. str);
  60. }
  61. SendData = true;
  62. if(this->Host->getApplicationState() == ctkDicomAppHosting::EXIT)
  63. {
  64. this->HostControls->StartApplication(this->AppFileName);
  65. }
  66. if(this->Host->getApplicationState() == ctkDicomAppHosting::IDLE)
  67. {
  68. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  69. }
  70. if(this->Host->getApplicationState() == ctkDicomAppHosting::INPROGRESS)
  71. {
  72. publishSelectedData();
  73. }
  74. }
  75. }
  76. void ctkDICOMHostMainLogic::onAppReady()
  77. {
  78. emit SelectionValid(ValidSelection);
  79. if(SendData)
  80. {
  81. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  82. qDebug() << " setState(INPROGRESS) returned: " << reply;
  83. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  84. this->Host->getDicomAppService()->bringToFront(rect);
  85. }
  86. }
  87. void ctkDICOMHostMainLogic::onTreeSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
  88. {
  89. ValidSelection=false;
  90. if(selected.indexes().count() > 0)
  91. {
  92. foreach (const QModelIndex &index, selected.indexes()) {
  93. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  94. QModelIndex index0 = index.sibling(index.row(), 0);
  95. if(model && (model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType)))
  96. {
  97. QString s = "Series selected: ";
  98. QString seriesUID = model->data(index0,ctkDICOMModel::UIDRole).toString();
  99. s.append(seriesUID);
  100. SelectedFiles = DicomAppWidget->database()->filesForSeries(seriesUID);
  101. emit TreeSelectionChanged(s);
  102. ValidSelection=true;
  103. }
  104. }
  105. }
  106. if (ValidSelection==false)
  107. emit TreeSelectionChanged("no series selected");
  108. emit SelectionValid(((this->Host) && (this->HostControls->validAppFileName()) && (ValidSelection)));
  109. }
  110. //----------------------------------------------------------------------------
  111. void ctkDICOMHostMainLogic::publishSelectedData()
  112. {
  113. if(SendData)
  114. {
  115. qDebug()<<"send dataDescriptors";
  116. bool success = Host->publishData(*Data, true);
  117. if(!success)
  118. {
  119. qCritical() << "Failed to publish data";
  120. }
  121. qDebug() << " notifyDataAvailable returned: " << success;
  122. SendData=false;
  123. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  124. this->Host->getDicomAppService()->bringToFront(rect);
  125. }
  126. }
  127. //----------------------------------------------------------------------------
  128. void ctkDICOMHostMainLogic::placeHolderResized()
  129. {
  130. ///the following does not work (yet)
  131. //if((this->Host) && (this->Host->getApplicationState() != ctkDicomAppHosting::EXIT))
  132. //{
  133. // QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  134. // this->Host->getDicomAppService()->bringToFront(rect);
  135. //}
  136. }