ctkExampleDicomAppLogic.cpp 8.9 KB

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