ctkExampleDicomAppLogic.cpp 9.5 KB

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