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