ctkExampleDicomAppLogic.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // CTK includes
  23. #include "ctkDICOMImage.h"
  24. #include "ctkExampleDicomAppLogic_p.h"
  25. #include "ctkExampleDicomAppPlugin_p.h"
  26. // DCMTK includes
  27. #include <dcmimage.h>
  28. //----------------------------------------------------------------------------
  29. ctkExampleDicomAppLogic::ctkExampleDicomAppLogic():
  30. ctkDicomAbstractApp(ctkExampleDicomAppPlugin::getPluginContext()), Button(0)
  31. {
  32. connect(this, SIGNAL(startProgress()), this, SLOT(onStartProgress()), Qt::QueuedConnection);
  33. connect(this, SIGNAL(resumeProgress()), this, SLOT(onResumeProgress()), Qt::QueuedConnection);
  34. connect(this, SIGNAL(suspendProgress()), this, SLOT(onSuspendProgress()), Qt::QueuedConnection);
  35. connect(this, SIGNAL(cancelProgress()), this, SLOT(onCancelProgress()), Qt::QueuedConnection);
  36. connect(this, SIGNAL(exitHostedApp()), this, SLOT(onExitHostedApp()), Qt::QueuedConnection);
  37. //notify Host we are ready.
  38. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
  39. }
  40. //----------------------------------------------------------------------------
  41. ctkExampleDicomAppLogic::~ctkExampleDicomAppLogic()
  42. {
  43. ctkPluginContext* context = ctkExampleDicomAppPlugin::getPluginContext();
  44. QList <QSharedPointer<ctkPlugin> > plugins = context->getPlugins();
  45. for (int i = 0; i < plugins.size(); ++i)
  46. {
  47. qDebug() << plugins.at(i)->getSymbolicName ();
  48. }
  49. }
  50. //----------------------------------------------------------------------------
  51. bool ctkExampleDicomAppLogic::bringToFront(const QRect& /*requestedScreenArea*/)
  52. {
  53. return false;
  54. }
  55. //----------------------------------------------------------------------------
  56. void ctkExampleDicomAppLogic::do_something()
  57. {
  58. this->Button = new QPushButton("Button from App");
  59. connect(this->Button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
  60. try
  61. {
  62. QRect preferred(50,50,100,100);
  63. qDebug() << " Asking:getAvailableScreen";
  64. QRect rect = getHostInterface()->getAvailableScreen(preferred);
  65. qDebug() << " got sth:" << rect.top();
  66. this->Button->move(rect.topLeft());
  67. this->Button->resize(rect.size());
  68. }
  69. catch (const std::runtime_error& e)
  70. {
  71. qCritical() << e.what();
  72. return;
  73. }
  74. this->Button->show();
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkExampleDicomAppLogic::onStartProgress()
  78. {
  79. setInternalState(ctkDicomAppHosting::INPROGRESS);
  80. // we need to create the button before we receive data from
  81. // the host, which happens immediately after calling
  82. // getHostInterface()->notifyStateChanged
  83. do_something();
  84. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
  85. }
  86. //----------------------------------------------------------------------------
  87. void ctkExampleDicomAppLogic::onResumeProgress()
  88. {
  89. //reclame all resources.
  90. //notify state changed
  91. setInternalState(ctkDicomAppHosting::INPROGRESS);
  92. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
  93. //we're rolling
  94. //do something else normally, but this is an example
  95. this->Button->setEnabled(true);
  96. }
  97. //----------------------------------------------------------------------------
  98. void ctkExampleDicomAppLogic::onSuspendProgress()
  99. {
  100. //release resources it can reclame later to resume work
  101. this->Button->setEnabled(false);
  102. //notify state changed
  103. setInternalState(ctkDicomAppHosting::SUSPENDED);
  104. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::SUSPENDED);
  105. //we're rolling
  106. //do something else normally, but this is an example
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkExampleDicomAppLogic::onCancelProgress()
  110. {
  111. //release all resources
  112. onReleaseResources();
  113. //update state
  114. setInternalState(ctkDicomAppHosting::IDLE);
  115. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
  116. }
  117. //----------------------------------------------------------------------------
  118. void ctkExampleDicomAppLogic::onExitHostedApp()
  119. {
  120. //useless move, but correct:
  121. setInternalState(ctkDicomAppHosting::EXIT);
  122. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
  123. //die
  124. qApp->exit(0);
  125. }
  126. //----------------------------------------------------------------------------
  127. void ctkExampleDicomAppLogic::onReleaseResources()
  128. {
  129. this->Button->hide();
  130. delete (this->Button);
  131. this->Button = 0 ;
  132. }
  133. //----------------------------------------------------------------------------
  134. bool ctkExampleDicomAppLogic::notifyDataAvailable(const ctkDicomAppHosting::AvailableData& data, bool lastData)
  135. {
  136. Q_UNUSED(lastData)
  137. QString s;
  138. if(this->Button == 0)
  139. {
  140. qCritical() << "Button is null!";
  141. return false;
  142. }
  143. s = "Received notifyDataAvailable with patients.count()= " + QString().setNum(data.patients.count());
  144. if(data.patients.count()>0)
  145. {
  146. s=s+" name:"+data.patients.begin()->name+" studies.count(): "+QString().setNum(data.patients.begin()->studies.count());
  147. if(data.patients.begin()->studies.count()>0)
  148. {
  149. s=s+" series.count():" + QString().setNum(data.patients.begin()->studies.begin()->series.count());
  150. if(data.patients.begin()->studies.begin()->series.count()>0)
  151. {
  152. s=s+" uid:" + data.patients.begin()->studies.begin()->series.begin()->seriesUID;
  153. // QUuid uuid("93097dc1-caf9-43a3-a814-51a57f8d861d");//data.patients.begin()->studies.begin()->series.begin()->seriesUID);
  154. uuid = data.patients.begin()->studies.begin()->series.begin()->objectDescriptors.begin()->descriptorUUID;
  155. s=s+" uuid:"+uuid.toString();
  156. }
  157. }
  158. }
  159. this->Button->setText(s);
  160. return false;
  161. }
  162. //----------------------------------------------------------------------------
  163. QList<ctkDicomAppHosting::ObjectLocator> ctkExampleDicomAppLogic::getData(
  164. const QList<QUuid>& objectUUIDs,
  165. const QList<QString>& acceptableTransferSyntaxUIDs,
  166. bool includeBulkData)
  167. {
  168. Q_UNUSED(objectUUIDs)
  169. Q_UNUSED(acceptableTransferSyntaxUIDs)
  170. Q_UNUSED(includeBulkData)
  171. return QList<ctkDicomAppHosting::ObjectLocator>();
  172. }
  173. //----------------------------------------------------------------------------
  174. void ctkExampleDicomAppLogic::releaseData(const QList<QUuid>& objectUUIDs)
  175. {
  176. Q_UNUSED(objectUUIDs)
  177. }
  178. void ctkExampleDicomAppLogic::buttonClicked()
  179. {
  180. QList<QUuid> uuidlist;
  181. uuidlist.append(uuid);
  182. QString transfersyntax("1.2.840.10008.1.2.1");
  183. QList<QString> transfersyntaxlist;
  184. transfersyntaxlist.append(transfersyntax);
  185. QList<ctkDicomAppHosting::ObjectLocator> locators;
  186. locators = getHostInterface()->getData(uuidlist, transfersyntaxlist, false);
  187. qDebug() << "got locators! " << QString().setNum(locators.count());
  188. QString s;
  189. s=s+" loc.count:"+QString().setNum(locators.count());
  190. if(locators.count()>0)
  191. {
  192. s=s+" URI: "+locators.begin()->URI +" locatorUUID: "+locators.begin()->locator+" sourceUUID: "+locators.begin()->source;
  193. qDebug() << "URI: " << locators.begin()->URI;
  194. QString filename = locators.begin()->URI;
  195. if(filename.startsWith("file:/",Qt::CaseInsensitive))
  196. filename=filename.remove(0,8);
  197. qDebug()<<filename;
  198. DicomImage dcmtkImage(filename.toLatin1().data());
  199. ctkDICOMImage ctkImage(&dcmtkImage);
  200. QLabel* qtImage = new QLabel;
  201. QPixmap pixmap = QPixmap::fromImage(ctkImage.frame(0),Qt::AvoidDither);
  202. if (pixmap.isNull())
  203. {
  204. qCritical() << "Failed to convert QImage to QPixmap" ;
  205. }
  206. else
  207. {
  208. qtImage->setPixmap(pixmap);
  209. qtImage->show();
  210. }
  211. }
  212. this->Button->setText(s);
  213. }