Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkIGTLDeviceCommandWidget.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 
40 const std::string QmitkIGTLDeviceCommandWidget::VIEW_ID =
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::QmitkIGTLDeviceCommandWidgetControls;
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 the widget items with the methods
81  connect( m_Controls->butSendCommand, SIGNAL(clicked()),
82  this, SLOT(OnSendCommand()));
83  connect( m_Controls->commandsComboBox,
84  SIGNAL(currentIndexChanged(const QString &)),
85  this, SLOT(OnCommandChanged(const QString &)));
86  }
87  //this is used for thread seperation, otherwise the worker thread would change the ui elements
88  //which would cause an exception
89  connect(this, SIGNAL(AdaptGUIToStateSignal()), this, SLOT(AdaptGUIToState()));
90 }
91 
92 
94 {
95  //this->AdaptGUIToState();
96  emit AdaptGUIToStateSignal();
97 }
98 
100 {
101  if (this->m_IGTLDevice.IsNotNull())
102  {
103  //check the state of the device
104  mitk::IGTLDevice::IGTLDeviceState state = this->m_IGTLDevice->GetState();
105 
106  switch (state) {
108  this->m_Controls->commandsComboBox->setEnabled(false);
109  this->m_Controls->butSendCommand->setEnabled(false);
110  this->m_Controls->fpsSpinBox->setEnabled(false);
111  break;
113  this->m_Controls->commandsComboBox->setEnabled(true);
114  this->m_Controls->butSendCommand->setEnabled(true);
115  this->m_Controls->fpsSpinBox->setEnabled(false);
116  break;
118  if ( this->m_IGTLDevice->GetNumberOfConnections() == 0 )
119  {
120  //just a server can run and have 0 connections
121  this->m_Controls->butSendCommand->setEnabled(false);
122  this->m_Controls->fpsSpinBox->setEnabled(false);
123  this->m_Controls->commandsComboBox->setEnabled(false);
124  }
125  else
126  {
127  this->m_Controls->commandsComboBox->setEnabled(true);
128  this->m_Controls->butSendCommand->setEnabled(true);
129  // this->m_Controls->fpsSpinBox->setEnabled(true);
130  }
131  break;
132  default:
133  mitkThrow() << "Invalid Device State";
134  break;
135  }
136  }
137  else
138  {
139  this->DisableSourceControls();
140  }
141 }
142 
144 {
145  //reset the GUI
147  //reset the observers
148  if ( this->m_IGTLDevice.IsNotNull() )
149  {
150  this->m_IGTLDevice->RemoveObserver(m_MessageReceivedObserverTag);
151  this->m_IGTLDevice->RemoveObserver(m_CommandReceivedObserverTag);
152  this->m_IGTLDevice->RemoveObserver(m_LostConnectionObserverTag);
153  this->m_IGTLDevice->RemoveObserver(m_NewConnectionObserverTag);
154  this->m_IGTLDevice->RemoveObserver(m_StateModifiedObserverTag);
155  }
156 
157  if(device.IsNotNull())
158  {
159  //get the device
160  this->m_IGTLDevice = device;
161 
162  //check if the device is a server or a client
163  if ( dynamic_cast<mitk::IGTLClient*>(
164  this->m_IGTLDevice.GetPointer()) == NULL )
165  {
166  m_IsClient = false;
167  }
168  else
169  {
170  m_IsClient = true;
171  }
172 
173  typedef itk::SimpleMemberCommand< QmitkIGTLDeviceCommandWidget > CurCommandType;
174 // CurCommandType::Pointer messageReceivedCommand = CurCommandType::New();
175 // messageReceivedCommand->SetCallbackFunction(
176 // this, &QmitkIGTLDeviceCommandWidget::OnMessageReceived );
177 // this->m_MessageReceivedObserverTag =
178 // this->m_IGTLDevice->AddObserver(mitk::MessageReceivedEvent(), messageReceivedCommand);
179 
180 // CurCommandType::Pointer commandReceivedCommand = CurCommandType::New();
181 // commandReceivedCommand->SetCallbackFunction(
182 // this, &QmitkIGTLDeviceCommandWidget::OnCommandReceived );
183 // this->m_CommandReceivedObserverTag =
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  //Fill the commands combo box with all available commands
206  }
207  else
208  {
209  m_IGTLDevice = NULL;
210  }
211 
212  this->AdaptGUIToState();
213 }
214 
216 {
217  this->m_Controls->commandsComboBox->setEnabled(false);
218  this->m_Controls->butSendCommand->setEnabled(false);
219  this->m_Controls->fpsSpinBox->setEnabled(false);
220 }
221 
222 
223 
224 
226 {
227  //Set the frames per second of the current command in case of a STT_ command
228  if ( std::strcmp( m_CurrentCommand->GetDeviceType(), "STT_BIND" ) == 0 )
229  {
230  ((igtl::StartBindMessage*)this->m_CurrentCommand.GetPointer())->
231  SetResolution(this->m_Controls->fpsSpinBox->value());
232  }
233  else if ( std::strcmp( m_CurrentCommand->GetDeviceType(), "STT_QTDATA" ) == 0 )
234  {
235  ((igtl::StartQuaternionTrackingDataMessage*)m_CurrentCommand.GetPointer())->
236  SetResolution(this->m_Controls->fpsSpinBox->value());
237  }
238  else if ( std::strcmp( m_CurrentCommand->GetDeviceType(), "STT_TDATA" ) == 0 )
239  {
240  ((igtl::StartTrackingDataMessage*)this->m_CurrentCommand.GetPointer())->
241  SetResolution(this->m_Controls->fpsSpinBox->value());
242  }
243 
244  m_IGTLDevice->SendMessage(m_CurrentCommand.GetPointer());
245 }
246 
248  const QString & curCommand)
249 {
250  if ( curCommand.isEmpty() )
251  return;
252 
254  this->m_IGTLDevice->GetMessageFactory();
255  //create a new message that fits to the selected get message type command
256  this->m_CurrentCommand = msgFactory->CreateInstance( curCommand.toStdString());
257  //enable/disable the FPS spinbox
258  this->m_Controls->fpsSpinBox->setEnabled(curCommand.contains("STT_"));
259 }
260 
262 {
263  //get the IGTL device that invoked this event
264 // mitk::IGTLDevice* dev = (mitk::IGTLDevice*)caller;
265 
266  //this->AdaptGUIToState();
267  emit AdaptGUIToStateSignal();
268 }
269 
271 {
272  //this->AdaptGUIToState();
273  emit AdaptGUIToStateSignal();
274 }
275 
277 {
278  //load the msg factory from the client (maybe this will be moved later on)
280  this->m_IGTLDevice->GetMessageFactory();
281  //get the available commands as std::list<std::string>
282  std::list<std::string> commandsList_ =
283  msgFactory->GetAvailableMessageRequestTypes();
284  //create a string list to convert the std::list
285  this->m_Controls->commandsComboBox->clear();
286  while ( commandsList_.size() )
287  {
288  //fill the combo box with life
289  this->m_Controls->commandsComboBox->addItem(
290  QString::fromStdString(commandsList_.front()));
291  commandsList_.pop_front();
292  }
293 }
void AdaptGUIToState()
Adapts the GUI to the state of the device.
void OnNewConnection()
Is called when the current device connected to another device.
mitk::IGTLDevice::Pointer m_IGTLDevice
holds the OpenIGTLink device
void OnCommandChanged(const QString &curCommand)
itk::SmartPointer< Self > Pointer
void OnDeviceStateChanged()
Calls AdaptGUIToState()
virtual void CreateQtPartControl(QWidget *parent)
bool m_IsClient
flag to indicate if the IGTL device is a client or a server
void AdaptGUIToStateSignal()
used for thread seperation, the worker thread must not call AdaptGUIToState directly QT signals are t...
QmitkIGTLDeviceCommandWidget(QWidget *parent=0, Qt::WindowFlags f=0)
virtual void CreateConnections()
Creation of the connections.
Ui::QmitkIGTLDeviceCommandWidgetControls * m_Controls
void Initialize(mitk::IGTLDevice::Pointer device)
Initializes the widget with the given device.
#define mitkThrow()
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.
void FillCommandsComboBox()
Fills the commands combo box with available commands.
igtl::MessageBase::Pointer m_CurrentCommand
void OnLostConnection()
Is called when the current device received a message.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.