ctkDICOMHostMainLogic.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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->Host->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. }
  80. }
  81. void ctkDICOMHostMainLogic::onTreeSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
  82. {
  83. ValidSelection=false;
  84. if(selected.indexes().count() > 0)
  85. {
  86. foreach (const QModelIndex &index, selected.indexes()) {
  87. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  88. QModelIndex index0 = index.sibling(index.row(), 0);
  89. if(model && (model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType)))
  90. {
  91. QString s = "Series selected: ";
  92. QString seriesUID = model->data(index0,ctkDICOMModel::UIDRole).toString();
  93. s.append(seriesUID);
  94. SelectedFiles = DicomAppWidget->database()->filesForSeries(seriesUID);
  95. emit TreeSelectionChanged(s);
  96. ValidSelection=true;
  97. }
  98. }
  99. }
  100. if (ValidSelection==false)
  101. emit TreeSelectionChanged("no series selected");
  102. emit SelectionValid(ValidSelection);
  103. }
  104. //----------------------------------------------------------------------------
  105. void ctkDICOMHostMainLogic::publishSelectedData()
  106. {
  107. if(SendData)
  108. {
  109. qDebug()<<"send dataDescriptors";
  110. bool success = Host->publishData(*Data, true);
  111. if(!success)
  112. {
  113. qCritical() << "Failed to publish data";
  114. }
  115. qDebug() << " notifyDataAvailable returned: " << success;
  116. SendData=false;
  117. }
  118. }