ctkCommandLineModuleAppLogic.cpp 11 KB

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