MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
QmitkIGTLDeviceSourceManagementWidget.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15//mitk headers
16#include <mitkSurface.h>
18#include <mitkDataStorage.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;
46}
47
48
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
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
228
233
238
void LoadSource(mitk::IGTLDeviceSource::Pointer sourceToLoad)
void AdaptGUIToStateSignal()
used for thread seperation, the worker thread must not call AdaptGUIToState directly QT signals are t...
mitk::IGTLDevice::Pointer m_IGTLDevice
holds the OpenIGTLink device
mitk::IGTLDeviceSource::Pointer m_IGTLDeviceSource
holds the IGTLDeviceSource we are working with.
void OnMessageReceived()
Is called when the current device received a message.
void OnNewConnection()
Is called when the current device connected to another device.
void OnCommandReceived()
Is called when the current device received a command.
bool m_IsClient
flag to indicate if the IGTL device is a client or a server
virtual void CreateConnections()
Creation of the connections.
void AdaptGUIToState()
Adapts the GUI to the state of the device.
Ui::QmitkIGTLDeviceSourceManagementWidgetControls * m_Controls
void OnLostConnection()
Is called when the current device lost a connection to one of its sockets.
QmitkIGTLDeviceSourceManagementWidget(QWidget *parent=nullptr, Qt::WindowFlags f={})
Superclass for OpenIGTLink clients.
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.