ctkCommandLineModuleAppLogic.cpp 12 KB

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