Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
QmitkUSDeviceManagerWidget.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 
13 //#define _USE_MATH_DEFINES
15 #include <usModuleContext.h>
16 #include <usGetModuleContext.h>
17 #include <QMessageBox>
18 #include "mitkUSVideoDevice.h"
19 #include <mitkUSIGTLDevice.h>
20 
21 const std::string QmitkUSDeviceManagerWidget::VIEW_ID =
22 "org.mitk.views.QmitkUSDeviceManagerWidget";
23 
25  Qt::WindowFlags f)
26  : QWidget(parent, f)
27 {
28  m_Controls = nullptr;
29  CreateQtPartControl(this);
30 }
31 
33 
35 
37 {
38  if (!m_Controls)
39  {
40  // create GUI widgets
41  m_Controls = new Ui::QmitkUSDeviceManagerWidgetControls;
42  m_Controls->setupUi(parent);
43  this->CreateConnections();
44 
45  m_Controls->m_ConnectedDevices->SetAutomaticallySelectFirstEntry(true);
46  }
47 
48  // Initializations
49  std::string empty = "";
50  m_Controls->m_ConnectedDevices->Initialize<mitk::USDevice>(
52 }
53 
55 {
56  if (m_Controls)
57  {
58  connect(m_Controls->m_BtnActivate, SIGNAL(clicked()), this,
59  SLOT(OnClickedActivateDevice()));
60  // connect( m_Controls->m_BtnDisconnect, SIGNAL( clicked() ), this,
61  // SLOT(OnClickedDisconnectDevice()) );
62  connect(m_Controls->m_BtnRemove, SIGNAL(clicked()), this,
63  SLOT(OnClickedRemoveDevice()));
64  connect(m_Controls->m_BtnNewDevice, SIGNAL(clicked()), this,
65  SLOT(OnClickedNewDevice()));
66  connect(m_Controls->m_ConnectedDevices,
67  SIGNAL(ServiceSelectionChanged(us::ServiceReferenceU)), this,
69  connect(m_Controls->m_BtnEdit, SIGNAL(clicked()), this,
70  SLOT(OnClickedEditDevice()));
71  }
72 }
73 
75 
77 {
79  m_Controls->m_ConnectedDevices->GetSelectedService<mitk::USDevice>();
80  if (device.IsNull())
81  {
82  return;
83  }
84 
85  if (device->GetIsActive())
86  {
87  device->Deactivate();
88  device->Disconnect();
89  }
90  else
91  {
92  QApplication::setOverrideCursor(Qt::WaitCursor);
93  if (device->GetDeviceState() < mitk::USDevice::State_Connected)
94  {
95  device->Connect();
96  }
97  if (device->GetIsConnected())
98  {
99  device->Activate();
100  }
101  QApplication::restoreOverrideCursor();
102 
103  if (!device->GetIsActive())
104  {
105  QMessageBox::warning(
106  this, "Activation failed",
107  "Could not activate device. Check logging for details.");
108  }
109  else
110  {
111  emit DeviceActivated();
112  }
113  }
114 
115  // Manually reevaluate Button logic
117  m_Controls->m_ConnectedDevices->GetSelectedServiceReference());
118 }
119 
121 {
122  mitk::USDevice::Pointer device =
123  m_Controls->m_ConnectedDevices->GetSelectedService<mitk::USDevice>();
124  if (device.IsNull())
125  {
126  return;
127  }
128 
129  if (device->GetIsConnected())
130  {
131  device->Disconnect();
132  }
133  else
134  {
135  if (!device->Connect())
136  {
137  QMessageBox::warning(
138  this, "Connecting failed",
139  "Could not connect to device. Check logging for details.");
140  }
141  }
142 }
143 
145 {
146  mitk::USDevice::Pointer device =
147  m_Controls->m_ConnectedDevices->GetSelectedService<mitk::USDevice>();
148 
149  if (device.IsNull())
150  {
151  return;
152  }
153 
154  if (device->GetDeviceClass() == "org.mitk.modules.us.USVideoDevice")
155  {
156  if (device->GetIsActive())
157  {
158  device->Deactivate();
159  }
160  if (device->GetIsConnected())
161  {
162  device->Disconnect();
163  }
164 
165  dynamic_cast<mitk::USVideoDevice*>(device.GetPointer())
166  ->UnregisterOnService();
167  }
168  else if (device->GetDeviceClass() == "IGTL Client")
169  {
170  mitk::USIGTLDevice::Pointer ultrasoundIGTLDevice = dynamic_cast<mitk::USIGTLDevice*>(device.GetPointer());
171  if (ultrasoundIGTLDevice->GetIsActive())
172  {
173  ultrasoundIGTLDevice->Deactivate();
174  }
175  if (ultrasoundIGTLDevice->GetIsConnected())
176  {
177  ultrasoundIGTLDevice->Disconnect();
178  }
179  ultrasoundIGTLDevice->UnregisterOnService();
180  }
181 }
182 
184 {
185  emit NewDeviceButtonClicked();
186 }
187 
189 {
190  mitk::USDevice::Pointer device =
191  m_Controls->m_ConnectedDevices->GetSelectedService<mitk::USDevice>();
192  emit EditDeviceButtonClicked(device);
193 }
194 
196  us::ServiceReferenceU reference)
197 {
198  if (!reference)
199  {
200  m_Controls->m_BtnActivate->setEnabled(false);
201  m_Controls->m_BtnRemove->setEnabled(false);
202  m_Controls->m_BtnEdit->setEnabled(false);
203  return;
204  }
205  std::string isConnected =
206  reference.GetProperty(
207  mitk::USDevice::GetPropertyKeys().US_PROPKEY_ISCONNECTED)
208  .ToString();
209  std::string isActive =
210  reference.GetProperty(
211  mitk::USDevice::GetPropertyKeys().US_PROPKEY_ISACTIVE)
212  .ToString();
213 
214  if (isActive.compare("false") == 0)
215  {
216  m_Controls->m_BtnActivate->setEnabled(true);
217  m_Controls->m_BtnActivate->setText("Activate");
218  }
219  else
220  {
221  m_Controls->m_BtnActivate->setEnabled(true);
222  m_Controls->m_BtnActivate->setText("Deactivate");
223  }
224 
225  std::string deviceClass =
226  reference.GetProperty(mitk::USDevice::GetPropertyKeys().US_PROPKEY_CLASS)
227  .ToString();
228  m_Controls->m_BtnRemove->setEnabled(deviceClass ==
229  "org.mitk.modules.us.USVideoDevice" || deviceClass == "IGTL Client");
230  m_Controls->m_BtnEdit->setEnabled(((deviceClass == "org.mitk.modules.us.USVideoDevice") && (isActive.compare("false") == 0)) ||
231  ((deviceClass == "IGTL Client") && (isActive.compare("false") == 0)));
232 }
233 
235 {
236  // at the moment disconnects ALL devices. Maybe we only want to disconnect the
237  // devices handled by this widget?
238  us::ModuleContext* thisContext = us::GetModuleContext();
239  std::vector<us::ServiceReference<mitk::USDevice> > services =
240  thisContext->GetServiceReferences<mitk::USDevice>();
241  for (std::vector<us::ServiceReference<mitk::USDevice> >::iterator it =
242  services.begin();
243  it != services.end(); ++it)
244  {
245  mitk::USDevice* currentDevice = thisContext->GetService(*it);
246  currentDevice->Disconnect();
247  }
248  MITK_INFO << "Disconnected ALL US devises!";
249 }
Ui::QmitkUSDeviceManagerWidgetControls * m_Controls
member holding the UI elements of this widget
QmitkUSDeviceManagerWidget(QWidget *p=nullptr, Qt::WindowFlags f1=nullptr)
A device holds information about it&#39;s model, make and the connected probes. It is the common super cl...
Definition: mitkUSDevice.h:73
#define MITK_INFO
Definition: mitkLogMacros.h:18
const std::string US_PROPKEY_LABEL
Definition: mitkUSDevice.h:103
static mitk::USDevice::PropertyKeys GetPropertyKeys()
static const std::string VIEW_ID
void OnDeviceSelectionChanged(us::ServiceReferenceU reference)
virtual void CreateQtPartControl(QWidget *parent)
void EditDeviceButtonClicked(mitk::USDevice::Pointer)
A mitk::USIGTLDevice is a USDevice to receive images over an OpenIGTLink connection. It registers an OIGTL device as a Microservice to receive image messages and transforms them to mitk::Images. It can act both as a server (listening for incoming connections) and as a client (connecting to an existing OIGTL server).
A mitk::USVideoDevice is the common class for video only devices. They capture video input either fro...
bool Disconnect()
Works analogously to mitk::USDevice::Connect(). Don&#39;t override this Method, but onDisconnection inste...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.