Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkIGTLDeviceSourceManagementWidget.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 //mitk headers
16 #include <mitkSurface.h>
17 #include <mitkIGTLDeviceSource.h>
18 #include <mitkDataStorage.h>
19 #include <mitkIGTLMessageFactory.h>
20 
21 //qt headers
22 #include <qfiledialog.h>
23 #include <qinputdialog.h>
24 #include <qmessagebox.h>
25 #include <qscrollbar.h>
26 
27 //igtl
28 #include <igtlStringMessage.h>
29 #include <igtlBindMessage.h>
30 #include <igtlQuaternionTrackingDataMessage.h>
31 #include <igtlTrackingDataMessage.h>
32 
33 //poco headers
34 #include <Poco/Path.h>
35 
37  "org.mitk.views.igtldevicesourcemanagementwidget";
38 
40  QWidget* parent, Qt::WindowFlags f)
41  : QWidget(parent, f), m_IsClient(false), m_MessageReceivedObserverTag(0), m_CommandReceivedObserverTag(0), m_LostConnectionObserverTag(0), m_NewConnectionObserverTag(0), m_StateModifiedObserverTag(0)
42 {
43  m_Controls = nullptr;
44  this->m_IGTLDevice = nullptr;
45  CreateQtPartControl(this);
46 }
47 
48 
50 {
56 }
57 
59 {
60  if (!m_Controls)
61  {
62  // create GUI widgets
63  m_Controls = new Ui::QmitkIGTLDeviceSourceManagementWidgetControls;
64  // setup GUI widgets
65  m_Controls->setupUi(parent);
66  }
67 
68  //connect slots with signals
70 }
71 
73 {
74  if (m_Controls)
75  {
76  connect( m_Controls->butSend, SIGNAL(clicked()),
77  this, SLOT(OnSendMessage()));
78  }
79  //this is used for thread seperation, otherwise the worker thread would change the ui elements
80  //which would cause an exception
81  connect(this, SIGNAL(AdaptGUIToStateSignal()), this, SLOT(AdaptGUIToState()));
82 }
83 
85 {
86  emit AdaptGUIToStateSignal();
87 }
88 
90 {
91  if (this->m_IGTLDeviceSource.IsNotNull())
92  {
93  //check the state of the device
95  this->m_IGTLDeviceSource->GetIGTLDevice()->GetState();
96 
97  switch (state) {
99  this->m_Controls->editSend->setEnabled(false);
100  this->m_Controls->butSend->setEnabled(false);
101  break;
103  this->m_Controls->editSend->setEnabled(false);
104  this->m_Controls->butSend->setEnabled(false);
105  break;
107  if ( this->m_IGTLDevice->GetNumberOfConnections() == 0 )
108  {
109  //just a server can run and have 0 connections
110  this->m_Controls->editSend->setEnabled(false);
111  this->m_Controls->butSend->setEnabled(false);
112  }
113  else
114  {
115  this->m_Controls->editSend->setEnabled(true);
116  this->m_Controls->butSend->setEnabled(true);
117  }
118  break;
119  default:
120  mitkThrow() << "Invalid Device State";
121  break;
122  }
123  m_Controls->selectedSourceLabel->setText(
124  m_IGTLDeviceSource->GetName().c_str());
125  }
126  else
127  {
128  this->DisableSourceControls();
129  }
130 }
131 
133  mitk::IGTLDeviceSource::Pointer sourceToLoad)
134 {
135  //reset the GUI
137  //reset the observers
138  if ( this->m_IGTLDevice.IsNotNull() )
139  {
145  }
146 
147  if(sourceToLoad.IsNotNull())
148  {
149  this->m_IGTLDeviceSource = sourceToLoad;
150 
151  //get the device
152  this->m_IGTLDevice = this->m_IGTLDeviceSource->GetIGTLDevice();
153 
154  //initialize the other GUI elements
155  this->m_Controls->connectionSetupWidget->Initialize(this->m_IGTLDevice);
156  this->m_Controls->commandWidget->Initialize(this->m_IGTLDevice);
157 
158  //check if the device is a server or a client
159  if ( dynamic_cast<mitk::IGTLClient*>(
160  this->m_IGTLDeviceSource->GetIGTLDevice()) == nullptr )
161  {
162  m_IsClient = false;
163  }
164  else
165  {
166  m_IsClient = true;
167  }
168 
169  typedef itk::SimpleMemberCommand< QmitkIGTLDeviceSourceManagementWidget > CurCommandType;
170  CurCommandType::Pointer messageReceivedCommand = CurCommandType::New();
171  messageReceivedCommand->SetCallbackFunction(
174  this->m_IGTLDevice->AddObserver(mitk::MessageReceivedEvent(), messageReceivedCommand);
175 
176  CurCommandType::Pointer commandReceivedCommand = CurCommandType::New();
177  commandReceivedCommand->SetCallbackFunction(
180  this->m_IGTLDevice->AddObserver(mitk::CommandReceivedEvent(), commandReceivedCommand);
181 
182  CurCommandType::Pointer connectionLostCommand = CurCommandType::New();
183  connectionLostCommand->SetCallbackFunction(
185  this->m_LostConnectionObserverTag = this->m_IGTLDevice->AddObserver(
186  mitk::LostConnectionEvent(), connectionLostCommand);
187 
188  CurCommandType::Pointer newConnectionCommand = CurCommandType::New();
189  newConnectionCommand->SetCallbackFunction(
191  this->m_NewConnectionObserverTag = this->m_IGTLDevice->AddObserver(
192  mitk::NewClientConnectionEvent(), newConnectionCommand);
193 
194  CurCommandType::Pointer stateModifiedCommand = CurCommandType::New();
195  stateModifiedCommand->SetCallbackFunction(
197  this->m_StateModifiedObserverTag = this->m_IGTLDevice->AddObserver(
198  itk::ModifiedEvent(), stateModifiedCommand);
199  }
200  else
201  {
202  m_IGTLDeviceSource = nullptr;
203  }
204  this->AdaptGUIToState();
205 }
206 
208 {
209  m_Controls->selectedSourceLabel->setText("<none>");
210  m_Controls->editSend->setEnabled(false);
211  m_Controls->butSend->setEnabled(false);
212 }
213 
214 
216 {
217  std::string toBeSend = m_Controls->editSend->text().toStdString();
218 
219  igtl::StringMessage::Pointer msg = igtl::StringMessage::New();
220  msg->SetString(toBeSend);
221  this->m_IGTLDevice->SendMessage(mitk::IGTLMessage::New((igtl::MessageBase::Pointer)msg));
222 }
223 
225 {
226 
227 }
228 
230 {
231 
232 }
233 
235 {
236  emit AdaptGUIToStateSignal();
237 }
238 
240 {
241  emit AdaptGUIToStateSignal();
242 }
mitk::IGTLDeviceSource::Pointer m_IGTLDeviceSource
holds the IGTLDeviceSource we are working with.
bool m_IsClient
flag to indicate if the IGTL device is a client or a server
virtual void CreateConnections()
Creation of the connections.
void OnMessageReceived()
Is called when the current device received a message.
void LoadSource(mitk::IGTLDeviceSource::Pointer sourceToLoad)
#define mitkThrow()
Ui::QmitkIGTLDeviceSourceManagementWidgetControls * m_Controls
void AdaptGUIToStateSignal()
used for thread seperation, the worker thread must not call AdaptGUIToState directly QT signals are t...
void OnLostConnection()
Is called when the current device lost a connection to one of its sockets.
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.
mitk::IGTLDevice::Pointer m_IGTLDevice
holds the OpenIGTLink device
static Pointer New()
void OnNewConnection()
Is called when the current device connected to another device.
void OnCommandReceived()
Is called when the current device received a command.
void AdaptGUIToState()
Adapts the GUI to the state of the device.
QmitkIGTLDeviceSourceManagementWidget(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)