ctkExampleDicomAppLogic.cpp 11 KB

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