ctkExampleDicomAppLogic.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // Qt includes
  16. #include <QtPlugin>
  17. #include <QRect>
  18. #include <QDebug>
  19. #include <QPushButton>
  20. #include <QApplication>
  21. #include <QLabel>
  22. #include <QRect>
  23. // CTK includes
  24. #include "ctkDICOMImage.h"
  25. #include "ctkExampleDicomAppLogic_p.h"
  26. #include "ctkExampleDicomAppPlugin_p.h"
  27. // DCMTK includes
  28. #include <dcmimage.h>
  29. //----------------------------------------------------------------------------
  30. ctkExampleDicomAppLogic::ctkExampleDicomAppLogic():
  31. ctkDicomAbstractApp(ctkExampleDicomAppPlugin::getPluginContext()), AppWidget(0)
  32. {
  33. connect(this, SIGNAL(startProgress()), this, SLOT(onStartProgress()), Qt::QueuedConnection);
  34. connect(this, SIGNAL(resumeProgress()), this, SLOT(onResumeProgress()), Qt::QueuedConnection);
  35. connect(this, SIGNAL(suspendProgress()), this, SLOT(onSuspendProgress()), Qt::QueuedConnection);
  36. connect(this, SIGNAL(cancelProgress()), this, SLOT(onCancelProgress()), Qt::QueuedConnection);
  37. connect(this, SIGNAL(exitHostedApp()), this, SLOT(onExitHostedApp()), Qt::QueuedConnection);
  38. //notify Host we are ready.
  39. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
  40. }
  41. //----------------------------------------------------------------------------
  42. ctkExampleDicomAppLogic::~ctkExampleDicomAppLogic()
  43. {
  44. ctkPluginContext* context = ctkExampleDicomAppPlugin::getPluginContext();
  45. QList <QSharedPointer<ctkPlugin> > plugins = context->getPlugins();
  46. for (int i = 0; i < plugins.size(); ++i)
  47. {
  48. qDebug() << plugins.at(i)->getSymbolicName ();
  49. }
  50. }
  51. //----------------------------------------------------------------------------
  52. bool ctkExampleDicomAppLogic::bringToFront(const QRect& requestedScreenArea)
  53. {
  54. if(this->AppWidget!=NULL)
  55. {
  56. this->AppWidget->move(requestedScreenArea.topLeft());
  57. this->AppWidget->resize(requestedScreenArea.size());
  58. this->AppWidget->activateWindow();
  59. this->AppWidget->raise();
  60. }
  61. return true;
  62. }
  63. //----------------------------------------------------------------------------
  64. void ctkExampleDicomAppLogic::do_something()
  65. {
  66. AppWidget = new QWidget;
  67. ui.setupUi(AppWidget);
  68. connect(ui.GetDataButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
  69. try
  70. {
  71. QRect preferred(50,50,100,100);
  72. qDebug() << " Asking:getAvailableScreen";
  73. QRect rect = getHostInterface()->getAvailableScreen(preferred);
  74. qDebug() << " got sth:" << rect.top();
  75. this->AppWidget->move(rect.topLeft());
  76. this->AppWidget->resize(rect.size());
  77. }
  78. catch (const std::runtime_error& e)
  79. {
  80. qCritical() << e.what();
  81. return;
  82. }
  83. this->AppWidget->show();
  84. }
  85. //----------------------------------------------------------------------------
  86. void ctkExampleDicomAppLogic::onStartProgress()
  87. {
  88. setInternalState(ctkDicomAppHosting::INPROGRESS);
  89. // we need to create the button before we receive data from
  90. // the host, which happens immediately after calling
  91. // getHostInterface()->notifyStateChanged
  92. do_something();
  93. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
  94. }
  95. //----------------------------------------------------------------------------
  96. void ctkExampleDicomAppLogic::onResumeProgress()
  97. {
  98. //reclame all resources.
  99. //notify state changed
  100. setInternalState(ctkDicomAppHosting::INPROGRESS);
  101. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
  102. //we're rolling
  103. //do something else normally, but this is an example
  104. ui.GetDataButton->setEnabled(true);
  105. }
  106. //----------------------------------------------------------------------------
  107. void ctkExampleDicomAppLogic::onSuspendProgress()
  108. {
  109. //release resources it can reclame later to resume work
  110. ui.GetDataButton->setEnabled(false);
  111. //notify state changed
  112. setInternalState(ctkDicomAppHosting::SUSPENDED);
  113. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::SUSPENDED);
  114. //we're rolling
  115. //do something else normally, but this is an example
  116. }
  117. //----------------------------------------------------------------------------
  118. void ctkExampleDicomAppLogic::onCancelProgress()
  119. {
  120. //release all resources
  121. onReleaseResources();
  122. //update state
  123. setInternalState(ctkDicomAppHosting::IDLE);
  124. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
  125. }
  126. //----------------------------------------------------------------------------
  127. void ctkExampleDicomAppLogic::onExitHostedApp()
  128. {
  129. //useless move, but correct:
  130. setInternalState(ctkDicomAppHosting::EXIT);
  131. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
  132. //die
  133. qApp->exit(0);
  134. }
  135. //----------------------------------------------------------------------------
  136. void ctkExampleDicomAppLogic::onReleaseResources()
  137. {
  138. this->AppWidget->hide();
  139. delete (this->AppWidget);
  140. this->AppWidget = 0 ;
  141. }
  142. //----------------------------------------------------------------------------
  143. bool ctkExampleDicomAppLogic::notifyDataAvailable(const ctkDicomAppHosting::AvailableData& data, bool lastData)
  144. {
  145. Q_UNUSED(lastData)
  146. QString s;
  147. if(this->AppWidget == 0)
  148. {
  149. qCritical() << "Button is null!";
  150. return false;
  151. }
  152. s = "Received notifyDataAvailable with patients.count()= " + QString().setNum(data.patients.count());
  153. if(data.patients.count()>0)
  154. {
  155. s=s+" name:"+data.patients.begin()->name+" studies.count(): "+QString().setNum(data.patients.begin()->studies.count());
  156. if(data.patients.begin()->studies.count()>0)
  157. {
  158. s=s+" series.count():" + QString().setNum(data.patients.begin()->studies.begin()->series.count());
  159. if(data.patients.begin()->studies.begin()->series.count()>0)
  160. {
  161. s=s+" uid:" + data.patients.begin()->studies.begin()->series.begin()->seriesUID;
  162. // QUuid uuid("93097dc1-caf9-43a3-a814-51a57f8d861d");//data.patients.begin()->studies.begin()->series.begin()->seriesUID);
  163. uuid = data.patients.begin()->studies.begin()->series.begin()->objectDescriptors.begin()->descriptorUUID;
  164. s=s+" uuid:"+uuid.toString();
  165. }
  166. }
  167. }
  168. ui.ReceivedDataInformation->setText(s);
  169. ui.GetDataButton->setEnabled(true);
  170. return false;
  171. }
  172. //----------------------------------------------------------------------------
  173. QList<ctkDicomAppHosting::ObjectLocator> ctkExampleDicomAppLogic::getData(
  174. const QList<QUuid>& objectUUIDs,
  175. const QList<QString>& acceptableTransferSyntaxUIDs,
  176. bool includeBulkData)
  177. {
  178. Q_UNUSED(objectUUIDs)
  179. Q_UNUSED(acceptableTransferSyntaxUIDs)
  180. Q_UNUSED(includeBulkData)
  181. return QList<ctkDicomAppHosting::ObjectLocator>();
  182. }
  183. //----------------------------------------------------------------------------
  184. void ctkExampleDicomAppLogic::releaseData(const QList<QUuid>& objectUUIDs)
  185. {
  186. Q_UNUSED(objectUUIDs)
  187. }
  188. void ctkExampleDicomAppLogic::buttonClicked()
  189. {
  190. QList<QUuid> uuidlist;
  191. uuidlist.append(uuid);
  192. QString transfersyntax("1.2.840.10008.1.2.1");
  193. QList<QString> transfersyntaxlist;
  194. transfersyntaxlist.append(transfersyntax);
  195. QList<ctkDicomAppHosting::ObjectLocator> locators;
  196. locators = getHostInterface()->getData(uuidlist, transfersyntaxlist, false);
  197. qDebug() << "got locators! " << QString().setNum(locators.count());
  198. QString s;
  199. s=s+" loc.count:"+QString().setNum(locators.count());
  200. if(locators.count()>0)
  201. {
  202. s=s+" URI: "+locators.begin()->URI +" locatorUUID: "+locators.begin()->locator+" sourceUUID: "+locators.begin()->source;
  203. qDebug() << "URI: " << locators.begin()->URI;
  204. QString filename = locators.begin()->URI;
  205. if(filename.startsWith("file:/",Qt::CaseInsensitive))
  206. filename=filename.remove(0,8);
  207. qDebug()<<filename;
  208. DicomImage dcmtkImage(filename.toLatin1().data());
  209. ctkDICOMImage ctkImage(&dcmtkImage);
  210. QPixmap pixmap = QPixmap::fromImage(ctkImage.frame(0),Qt::AvoidDither);
  211. if (pixmap.isNull())
  212. {
  213. qCritical() << "Failed to convert QImage to QPixmap" ;
  214. }
  215. else
  216. {
  217. ui.PlaceHolderForImage->setPixmap(pixmap);
  218. }
  219. }
  220. ui.ReceivedDataInformation->setText(s);
  221. }