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