ctkExampleDicomAppLogic.cpp 10 KB

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