ctkCommandLineModuleAppLogic.cpp 12 KB

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