Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 //mitk headers
20 #include <mitkSurface.h>
21 #include <mitkIGTLDeviceSource.h>
22 #include <mitkDataStorage.h>
23 #include <mitkIGTLMessageFactory.h>
24 
25 //qt headers
26 #include <qfiledialog.h>
27 #include <qinputdialog.h>
28 #include <qmessagebox.h>
29 #include <qscrollbar.h>
30 
31 //igtl
32 #include <igtlStringMessage.h>
33 #include <igtlBindMessage.h>
34 #include <igtlQuaternionTrackingDataMessage.h>
35 #include <igtlTrackingDataMessage.h>
36 
37 //poco headers
38 #include <Poco/Path.h>
39 
41  "org.mitk.views.igtldevicesourcemanagementwidget";
42 
44  QWidget* parent, Qt::WindowFlags f)
45  : QWidget(parent, f), m_IsClient(false), m_MessageReceivedObserverTag(0), m_CommandReceivedObserverTag(0), m_LostConnectionObserverTag(0), m_NewConnectionObserverTag(0), m_StateModifiedObserverTag(0)
46 {
47  m_Controls = NULL;
48  this->m_IGTLDevice = NULL;
49  CreateQtPartControl(this);
50 }
51 
52 
54 {
60 }
61 
63 {
64  if (!m_Controls)
65  {
66  // create GUI widgets
67  m_Controls = new Ui::QmitkIGTLDeviceSourceManagementWidgetControls;
68  // setup GUI widgets
69  m_Controls->setupUi(parent);
70  }
71 
72  //connect slots with signals
74 }
75 
77 {
78  if (m_Controls)
79  {
80  connect( m_Controls->butSend, SIGNAL(clicked()),
81  this, SLOT(OnSendMessage()));
82  }
83  //this is used for thread seperation, otherwise the worker thread would change the ui elements
84  //which would cause an exception
85  connect(this, SIGNAL(AdaptGUIToStateSignal()), this, SLOT(AdaptGUIToState()));
86 }
87 
89 {
90  emit AdaptGUIToStateSignal();
91 }
92 
94 {
95  if (this->m_IGTLDeviceSource.IsNotNull())
96  {
97  //check the state of the device
99  this->m_IGTLDeviceSource->GetIGTLDevice()->GetState();
100 
101  switch (state) {
103  this->m_Controls->editSend->setEnabled(false);
104  this->m_Controls->butSend->setEnabled(false);
105  break;
107  this->m_Controls->editSend->setEnabled(false);
108  this->m_Controls->butSend->setEnabled(false);
109  break;
111  if ( this->m_IGTLDevice->GetNumberOfConnections() == 0 )
112  {
113  //just a server can run and have 0 connections
114  this->m_Controls->editSend->setEnabled(false);
115  this->m_Controls->butSend->setEnabled(false);
116  }
117  else
118  {
119  this->m_Controls->editSend->setEnabled(true);
120  this->m_Controls->butSend->setEnabled(true);
121  }
122  break;
123  default:
124  mitkThrow() << "Invalid Device State";
125  break;
126  }
127  m_Controls->selectedSourceLabel->setText(
128  m_IGTLDeviceSource->GetName().c_str());
129  }
130  else
131  {
132  this->DisableSourceControls();
133  }
134 }
135 
137  mitk::IGTLDeviceSource::Pointer sourceToLoad)
138 {
139  //reset the GUI
141  //reset the observers
142  if ( this->m_IGTLDevice.IsNotNull() )
143  {
149  }
150 
151  if(sourceToLoad.IsNotNull())
152  {
153  this->m_IGTLDeviceSource = sourceToLoad;
154 
155  //get the device
156  this->m_IGTLDevice = this->m_IGTLDeviceSource->GetIGTLDevice();
157 
158  //initialize the other GUI elements
159  this->m_Controls->connectionSetupWidget->Initialize(this->m_IGTLDevice);
160  this->m_Controls->commandWidget->Initialize(this->m_IGTLDevice);
161 
162  //check if the device is a server or a client
163  if ( dynamic_cast<mitk::IGTLClient*>(
164  this->m_IGTLDeviceSource->GetIGTLDevice()) == NULL )
165  {
166  m_IsClient = false;
167  }
168  else
169  {
170  m_IsClient = true;
171  }
172 
173  typedef itk::SimpleMemberCommand< QmitkIGTLDeviceSourceManagementWidget > CurCommandType;
174  CurCommandType::Pointer messageReceivedCommand = CurCommandType::New();
175  messageReceivedCommand->SetCallbackFunction(
178  this->m_IGTLDevice->AddObserver(mitk::MessageReceivedEvent(), messageReceivedCommand);
179 
180  CurCommandType::Pointer commandReceivedCommand = CurCommandType::New();
181  commandReceivedCommand->SetCallbackFunction(
184  this->m_IGTLDevice->AddObserver(mitk::CommandReceivedEvent(), commandReceivedCommand);
185 
186  CurCommandType::Pointer connectionLostCommand = CurCommandType::New();
187  connectionLostCommand->SetCallbackFunction(
189  this->m_LostConnectionObserverTag = this->m_IGTLDevice->AddObserver(
190  mitk::LostConnectionEvent(), connectionLostCommand);
191 
192  CurCommandType::Pointer newConnectionCommand = CurCommandType::New();
193  newConnectionCommand->SetCallbackFunction(
195  this->m_NewConnectionObserverTag = this->m_IGTLDevice->AddObserver(
196  mitk::NewClientConnectionEvent(), newConnectionCommand);
197 
198  CurCommandType::Pointer stateModifiedCommand = CurCommandType::New();
199  stateModifiedCommand->SetCallbackFunction(
201  this->m_StateModifiedObserverTag = this->m_IGTLDevice->AddObserver(
202  itk::ModifiedEvent(), stateModifiedCommand);
203  }
204  else
205  {
206  m_IGTLDeviceSource = NULL;
207  }
208  this->AdaptGUIToState();
209 }
210 
212 {
213  m_Controls->selectedSourceLabel->setText("<none>");
214  m_Controls->editSend->setEnabled(false);
215  m_Controls->butSend->setEnabled(false);
216 }
217 
218 
220 {
221  std::string toBeSend = m_Controls->editSend->text().toStdString();
222 
224  msg->SetString(toBeSend);
225  this->m_IGTLDevice->SendMessage(msg.GetPointer());
226 }
227 
229 {
230 
231 }
232 
234 {
235 
236 }
237 
239 {
240  emit AdaptGUIToStateSignal();
241 }
242 
244 {
245  emit AdaptGUIToStateSignal();
246 }
itk::SmartPointer< Self > Pointer
QmitkIGTLDeviceSourceManagementWidget(QWidget *parent=0, Qt::WindowFlags f=0)
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
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.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.