ctkExampleDicomAppLogic.cpp 10 KB

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