ctkDICOMHostMainLogic.cpp 4.7 KB

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