ctkCommandLineModuleAppLogic.cpp 12 KB

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