123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*=============================================================================
- Library: CTK
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- // Qt includes
- #include <QProcess>
- #include <QtDebug>
- #include <QRect>
- #include <QStringList>
- #include <QDir>
- // CTK includes
- #include "ctkExampleDicomHost.h"
- #include "ctkDicomAppHostingTypesHelper.h"
- #include "ctkDicomAvailableDataHelper.h"
- // STD includes
- #include <iostream>
- // DCMTK includes
- #include <dcmtk/dcmdata/dcuid.h>
- //----------------------------------------------------------------------------
- ctkExampleDicomHost::ctkExampleDicomHost(ctkHostedAppPlaceholderWidget* placeholderWidget, int hostPort, int appPort) :
- ctkDicomAbstractHost(hostPort, appPort),
- PlaceholderWidget(placeholderWidget),
- exitingApplication(false)
- {
- connect(this,SIGNAL(appReady()),SLOT(onAppReady()), Qt::QueuedConnection);
- connect(this,SIGNAL(startProgress()),this,SLOT(onStartProgress()));
- connect(this,SIGNAL(releaseAvailableResources()),this,SLOT(onReleaseAvailableResources()));
- connect(this,SIGNAL(resumed()),this,SLOT(onResumed()));
- connect(this,SIGNAL(completed()),this,SLOT(onCompleted()));
- connect(this,SIGNAL(suspended()),this,SLOT(onSuspended()));
- connect(this,SIGNAL(canceled()),this,SLOT(onCanceled()));
- connect(this,SIGNAL(exited()),this,SLOT(onExited()));
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::StartApplication(QString AppPath)
- {
- QStringList arguments;
- arguments.append("--hostURL");
- arguments.append(QString("http://localhost:") + QString::number(this->getHostPort()) + "/HostInterface" );
- arguments.append("--applicationURL");
- arguments.append(QString("http://localhost:") + QString::number(this->getAppPort()) + "/ApplicationInterface" );
- //by default, the ctkExampleHostedApp uses the org.commontk.dah.exampleapp plugin
- //arguments.append("dicomapp"); // the app plugin to use - has to be changed later
- //if (!QProcess::startDetached (
- //{
- // qCritical() << "application failed to start!";
- //}
- //qDebug() << "starting application: " << AppPath << " " << arguments;
- qDebug() << "starting application: " << AppPath << " " << arguments;
- this->AppProcess.setProcessChannelMode(QProcess::MergedChannels);
- this->AppProcess.start(AppPath, arguments);
- }
- //----------------------------------------------------------------------------
- QRect ctkExampleDicomHost::getAvailableScreen(const QRect& preferredScreen)
- {
- qDebug()<< "Application asked for this area:"<< preferredScreen;
- QRect rect (this->PlaceholderWidget->getAbsolutePosition());
- emit giveAvailableScreen(rect);
- return rect;
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::notifyStatus(const ctkDicomAppHosting::Status& status)
- {
- qDebug()<< "new status received:"<<status.codeMeaning;
- emit statusReceived(status);;
- }
- //----------------------------------------------------------------------------
- ctkExampleDicomHost::~ctkExampleDicomHost()
- {
- if(this->AppProcess.state()!=QProcess::NotRunning)
- {
- qDebug() << "Exiting host: trying to terminate app";
- this->AppProcess.terminate();
- qDebug() << "Exiting host: trying to kill app";
- this->AppProcess.kill();
- }
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onAppReady()
- {
- //prepare some resources...
- //tell app to start
- //getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
- qDebug() << "App ready to work";
- if (this->exitingApplication)
- {
- this->exitingApplication = false;
- getDicomAppService ()->setState (ctkDicomAppHosting::EXIT);
- }
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onStartProgress()
- {
- ctkDicomAppHosting::AvailableData data;
- ctkDicomAvailableDataHelper::addToAvailableData(data,
- this->objectLocatorCache(),
- "C:/XIP/XIPHost/dicom-dataset-demo/1.3.6.1.4.1.9328.50.1.10698.dcm");
- qDebug()<<"send dataDescriptors";
- bool success = this->publishData(data, true);
- if(!success)
- {
- qCritical() << "Failed to publish data";
- }
- qDebug() << " notifyDataAvailable(1111) returned: " << success;
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onResumed()
- {
- qDebug() << "App resumed work";
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onCompleted()
- {
- qDebug() << "App finished processing";
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onSuspended()
- {
- qDebug() << "App paused";
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onCanceled()
- {
- qDebug() << "App canceled";
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onExited()
- {
- qDebug() << "App exited";
- cleanIncomingData();
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onReleaseAvailableResources()
- {
- qDebug() << "Should release resources put at the disposition of the app";
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::exitApplication()
- {
- this->exitingApplication=true;
- if(this->getAppProcess().state() == QProcess::Running)
- {
- if(this->getApplicationState() == ctkDicomAppHosting::EXIT)
- return;
- if(this->getApplicationState() == ctkDicomAppHosting::IDLE)
- {
- getDicomAppService ()->setState (ctkDicomAppHosting::EXIT);
- return;
- }
- getDicomAppService ()->setState (ctkDicomAppHosting::CANCELED);
- }
- }
- //----------------------------------------------------------------------------
- QString ctkExampleDicomHost::getOutputLocation(const QStringList& preferredProtocols)
- {
- if(preferredProtocols.indexOf("file:")>=0)
- return QDir::temp().absolutePath();
- return "";
- }
- //----------------------------------------------------------------------------
- ctkDicomAppHosting::State ctkExampleDicomHost::getApplicationState()const
- {
- if(this->getAppProcess().state() == QProcess::NotRunning)
- return ctkDicomAppHosting::EXIT;
- return ctkDicomAbstractHost::getApplicationState();
- }
- //----------------------------------------------------------------------------
- QString ctkExampleDicomHost::generateUID()
- {
- char uid[100];
- dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
- return uid;
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::exitApplicationBlocking(int timeout)
- {
- connect(&this->AppProcess,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(onBlockingExiting(QProcess::ProcessState)));
- if(this->getApplicationState() != ctkDicomAppHosting::EXIT)
- {
- this->exitApplication();
- QTimer::singleShot(timeout,this,SLOT(onBlockingExiting()));
- BlockingLoopForExiting.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);
- }
- if(this->AppProcess.state()!=QProcess::NotRunning)
- {
- this->AppProcess.kill();
- }
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onBlockingExiting(QProcess::ProcessState)
- {
- this->AppProcess.disconnect(SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onBlockingExiting(QProcess::ProcessState)));
- BlockingLoopForExiting.exit(0);
- }
- //----------------------------------------------------------------------------
- void ctkExampleDicomHost::onBlockingExiting()
- {
- BlockingLoopForExiting.exit(0);
- }
|