CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxOpenIGTLinkStreamerService.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <QFileInfo>
15 
16 #include "cxNetworkHandler.h"
17 #include "cxLogger.h"
18 #include "cxProfile.h"
19 #include "cxTrackingService.h"
21 
22 namespace cx
23 {
24 
26  mConnection(networkHandler),
27  mTrackingService(trackingService),
28  mStartedTrackingAndOpenIGTLinkFromHere(false)
29 {
31 
32  connect(mConnection.get(), &NetworkHandler::connected, mStreamer.get(), &OpenIGTLinkStreamer::receivedConnected);
33  connect(mConnection.get(), &NetworkHandler::disconnected, mStreamer.get(), &OpenIGTLinkStreamer::receivedDisconnected);
34  connect(mConnection.get(), &NetworkHandler::image, mStreamer.get(), &OpenIGTLinkStreamer::receivedImage);
35  connect(mConnection.get(), &NetworkHandler::probedefinition, mStreamer.get(), &OpenIGTLinkStreamer::receiveProbedefinition);
36 
37  connect(mStreamer.get(), &OpenIGTLinkStreamer::stoppedStreaming, this, &OpenIGTLinkStreamerService::stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject);
38 }
39 
41 {
42  this->stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject();
43 }
44 
46 {
47  return "OpenIGTLink 3 streamer";
48 }
49 
51 {
52  return OPENIGTLINK3_STREAMER;
53 }
54 
55 std::vector<PropertyPtr> OpenIGTLinkStreamerService::getSettings(QDomElement root)
56 {
57  std::vector<PropertyPtr> retval;
58  retval.push_back(this->getIPOption(root));
59  retval.push_back(this->getStreamPortOption(root));
60  retval.push_back(this->trackAndStream(root));
61 
62  return retval;
63 }
64 
66 {
67  this->startTracking(root);
68  return mStreamer;
69 }
70 
72 {
73  this->stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject();
74 }
75 
76 void OpenIGTLinkStreamerService::stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject()
77 {
78  if(mStartedTrackingAndOpenIGTLinkFromHere)
79  {
80  mConnection->disconnectFromServer();
81 
82  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = this->getOpenIGTLinkTrackingSystemService();
83  if(trackingSystemService)
84  trackingSystemService->setState(Tool::tsNONE);
85 
86  mStartedTrackingAndOpenIGTLinkFromHere = false;
87  }
88 }
89 
90 void OpenIGTLinkStreamerService::startTracking(QDomElement root)
91 {
92  if(this->trackAndStream(root)->getValue())
93  {
94  mStartedTrackingAndOpenIGTLinkFromHere = true;
95  this->configureTracking(root);
96  // Trying to connect will cause several tests to fail,
97  // because ClientSocket::ConnectToServer in OpenIGTLink/OpenIGTLink/Source/igtlClientSocket.cxx
98  // will print error messages when it cannot connect.
99  // Default value for trackAndStream (named start_tracking in settings xml file,
100  // and defined as OPENIGTLINK3_STREAMER_START_TRACKING in code) is therefore set to false.
101  mConnection->requestConnectToServer(this->getIPOption(root)->getValue().toStdString(),
102  int(this->getStreamPortOption(root)->getValue()));
103  }
104 }
105 
106 void OpenIGTLinkStreamerService::configureTracking(QDomElement root)
107 {
108  Q_UNUSED(root);
109  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = this->getOpenIGTLinkTrackingSystemService();
110  if(trackingSystemService)
111  {
112  QFileInfo fileInfo(profile()->getToolConfigFilePath());
113  trackingSystemService->setConfigurationFile(fileInfo.filePath());//Set full path to current tool config file
114  trackingSystemService->setState(Tool::tsCONFIGURED);
115  }
116 }
117 
118 OpenIGTLinkTrackingSystemServicePtr OpenIGTLinkStreamerService::getOpenIGTLinkTrackingSystemService()
119 {
120  std::vector<TrackingSystemServicePtr> trackingSystems = mTrackingService->getTrackingSystems();
121  for (unsigned i = 0; i < trackingSystems.size(); ++i)
122  {
123  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = boost::dynamic_pointer_cast<OpenIGTLinkTrackingSystemService>(trackingSystems[i]);
124  if(trackingSystemService)
125  return trackingSystemService;
126  }
128 }
129 
130 BoolPropertyBasePtr OpenIGTLinkStreamerService::trackAndStream(QDomElement root)
131 {
132  BoolPropertyPtr retval;
133  // Default value need to be false to prevent tests from failing
134  retval = BoolProperty::initialize(OPENIGTLINK3_STREAMER_START_TRACKING, "Also Start OpenIGTLink client and Tracking",
135  "Combined functionality: \n"
136  "Run both tracking and streaming over OpenIGTLink",
137  false, root);
138  retval->setGroup("Connection");
139 
140  return retval;
141 }
142 
143 StringPropertyBasePtr OpenIGTLinkStreamerService::getIPOption(QDomElement root)
144 {
145  StringPropertyPtr retval;
146  QString defaultValue = "127.0.0.1";
147  retval = StringProperty::initialize(OPENIGTLINK3_STREAMER_IP, "Address", "TCP/IP Address",
148  defaultValue, root);
149  retval->setGroup("Connection");
150  return retval;
151 }
152 
153 DoublePropertyBasePtr OpenIGTLinkStreamerService::getStreamPortOption(QDomElement root)
154 {
155  DoublePropertyPtr retval;
156  retval = DoubleProperty::initialize("ip_port", "Port", "TCP/IP Port (default 18944)",
157  18944, DoubleRange(1024, 49151, 1), 0, root);
158  retval->setGuiRepresentation(DoublePropertyBase::grSPINBOX);
159  retval->setAdvanced(true);
160  retval->setGroup("Connection");
161  return retval;
162 }
163 
164 } //namespace cx
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
boost::shared_ptr< class TrackingService > TrackingServicePtr
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
boost::shared_ptr< class NetworkHandler > NetworkHandlerPtr
boost::shared_ptr< OpenIGTLinkStreamer > OpenIGTLinkStreamerPtr
configured with basic info
Definition: cxTool.h:75
boost::shared_ptr< class StringProperty > StringPropertyPtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
boost::shared_ptr< class BoolPropertyBase > BoolPropertyBasePtr
void probedefinition(QString devicename, ProbeDefinitionPtr definition)
boost::shared_ptr< class DoublePropertyBase > DoublePropertyBasePtr
not available
Definition: cxTool.h:74
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
boost::shared_ptr< class OpenIGTLinkTrackingSystemService > OpenIGTLinkTrackingSystemServicePtr
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< class Streamer > StreamerPtr
void image(ImagePtr image)
Namespace for all CustusX production code.