ctkDICOMHostMainLogic.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. QObject(placeHolder),
  19. PlaceHolderForHostedApp(placeHolder),
  20. DicomAppWidget(dicomAppWidget),
  21. ValidSelection(false),
  22. SendData(false)
  23. {
  24. this->Host = new ctkExampleDicomHost(PlaceHolderForHostedApp);
  25. this->HostControls = new ctkExampleHostControlWidget(Host, placeHolder);
  26. this->HostControls->show();
  27. Data = new ctkDicomAppHosting::AvailableData;
  28. disconnect(this->Host,SIGNAL(startProgress()),this->Host,SLOT(onStartProgress()));
  29. connect(this->Host,SIGNAL(appReady()),this,SLOT(onAppReady()), Qt::QueuedConnection);
  30. connect(this->Host,SIGNAL(startProgress()),this,SLOT(publishSelectedData()));
  31. QTreeView * treeview = dicomAppWidget->findChild<QTreeView*>("TreeView");
  32. QItemSelectionModel* selectionmodel = treeview->selectionModel();
  33. connect(selectionmodel, SIGNAL(selectionChanged ( const QItemSelection &, const QItemSelection & )),
  34. this, SLOT(onTreeSelectionChanged(const QItemSelection &, const QItemSelection &)));
  35. }
  36. ctkDICOMHostMainLogic::~ctkDICOMHostMainLogic()
  37. {
  38. delete Data;
  39. }
  40. void ctkDICOMHostMainLogic::configureHostedApp()
  41. {
  42. //qDebug() << "load button clicked";
  43. AppFileName = QFileDialog::getOpenFileName(PlaceHolderForHostedApp,"Choose hosted application",QApplication::applicationDirPath());
  44. HostControls->setAppFileName(AppFileName);
  45. }
  46. void ctkDICOMHostMainLogic::sendDataToHostedApp()
  47. {
  48. if ((this->Host) && (this->AppFileName.isEmpty()==false) && (ValidSelection))
  49. {
  50. foreach (const QString &str, SelectedFiles) {
  51. if (str.isEmpty())
  52. continue;
  53. qDebug() << str;
  54. ctkDicomAvailableDataHelper::addToAvailableData(*Data,
  55. Host->objectLocatorCache(),
  56. str);
  57. }
  58. SendData = true;
  59. if(this->Host->getApplicationState() == ctkDicomAppHosting::EXIT)
  60. {
  61. this->HostControls->StartApplication(this->AppFileName);
  62. }
  63. if(this->Host->getApplicationState() == ctkDicomAppHosting::IDLE)
  64. {
  65. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  66. }
  67. if(this->Host->getApplicationState() == ctkDicomAppHosting::INPROGRESS)
  68. {
  69. publishSelectedData();
  70. }
  71. }
  72. }
  73. void ctkDICOMHostMainLogic::onAppReady()
  74. {
  75. if(SendData)
  76. {
  77. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  78. qDebug() << " setState(INPROGRESS) returned: " << reply;
  79. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  80. this->Host->getDicomAppService()->bringToFront(rect);
  81. }
  82. }
  83. void ctkDICOMHostMainLogic::onTreeSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
  84. {
  85. ValidSelection=false;
  86. if(selected.indexes().count() > 0)
  87. {
  88. foreach (const QModelIndex &index, selected.indexes()) {
  89. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  90. QModelIndex index0 = index.sibling(index.row(), 0);
  91. if(model && (model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType)))
  92. {
  93. QString s = "Series selected: ";
  94. QString seriesUID = model->data(index0,ctkDICOMModel::UIDRole).toString();
  95. s.append(seriesUID);
  96. SelectedFiles = DicomAppWidget->database()->filesForSeries(seriesUID);
  97. emit TreeSelectionChanged(s);
  98. ValidSelection=true;
  99. }
  100. }
  101. }
  102. if (ValidSelection==false)
  103. emit TreeSelectionChanged("no series selected");
  104. emit SelectionValid(ValidSelection);
  105. }
  106. //----------------------------------------------------------------------------
  107. void ctkDICOMHostMainLogic::publishSelectedData()
  108. {
  109. if(SendData)
  110. {
  111. qDebug()<<"send dataDescriptors";
  112. bool success = Host->publishData(*Data, true);
  113. if(!success)
  114. {
  115. qCritical() << "Failed to publish data";
  116. }
  117. qDebug() << " notifyDataAvailable returned: " << success;
  118. SendData=false;
  119. QRect rect (this->PlaceHolderForHostedApp->getAbsolutePosition());
  120. this->Host->getDicomAppService()->bringToFront(rect);
  121. }
  122. }