ctkExampleDicomAppLogic.cpp 9.9 KB

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