Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkUSNewVideoDeviceWidget.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 
20 // QT headers
21 #include <QFileDialog>
22 
23 // mitk headers
24 
25 // itk headers
26 
27 const std::string QmitkUSNewVideoDeviceWidget::VIEW_ID =
28 "org.mitk.views.QmitkUSNewVideoDeviceWidget";
29 
31  Qt::WindowFlags f)
32  : QWidget(parent, f)
33 {
34  m_Controls = NULL;
35  CreateQtPartControl(this);
36 }
37 
39 
41 
43 {
44  if (!m_Controls)
45  {
46  // create GUI widgets
47  m_Controls = new Ui::QmitkUSNewVideoDeviceWidgetControls;
48  m_Controls->setupUi(parent);
49  this->CreateConnections();
50  }
51 }
52 
54 {
55  if (m_Controls)
56  {
57  // connect(m_Controls->m_BtnDone, SIGNAL(clicked()), this,
58  // SLOT(OnClickedDone()));
59  connect(m_Controls->m_BtnCancel, SIGNAL(clicked()), this,
60  SLOT(OnClickedCancel()));
61  connect(m_Controls->m_RadioDeviceSource, SIGNAL(clicked()), this,
62  SLOT(OnDeviceTypeSelection()));
63  connect(m_Controls->m_RadioFileSource, SIGNAL(clicked()), this,
64  SLOT(OnDeviceTypeSelection()));
65  connect(m_Controls->m_RadioOIGTLClientSource, SIGNAL(clicked()), this,
66  SLOT(OnDeviceTypeSelection()));
67  connect(m_Controls->m_RadioOIGTLServerSource, SIGNAL(clicked()), this,
68  SLOT(OnDeviceTypeSelection()));
69  connect(m_Controls->m_OpenFileButton, SIGNAL(clicked()), this,
70  SLOT(OnOpenFileButtonClicked()));
71 
72  //Connect buttons and functions for editing of probes
73  connect(m_Controls->m_BtnRemoveProbe, SIGNAL(clicked()), this, SLOT(OnClickedRemoveProbe()));
74  connect(m_Controls->m_BtnRemoveDepth, SIGNAL(clicked()), this, SLOT(OnClickedRemoveDepth()));
75  connect(m_Controls->m_BtnAddDepths, SIGNAL(clicked()), this, SLOT(OnClickedAddDepths()));
76  connect(m_Controls->m_Probes, SIGNAL(currentTextChanged(const QString &)), this, SLOT(OnProbeChanged(const QString &)));
77  }
78 }
79 
81 
83 {
84  m_Active = false;
85 
86  // Create Device
88  if (m_Controls->m_RadioDeviceSource->isChecked())
89  {
90  newDevice = mitk::USVideoDevice::New(
91  m_Controls->m_DeviceSelector->value(),
92  m_Controls->m_Manufacturer->text().toStdString(),
93  m_Controls->m_Model->text().toStdString());
94  newDevice->SetComment(m_Controls->m_Comment->text().toStdString());
95  }
96  else if (m_Controls->m_RadioFileSource->isChecked())
97  {
98  newDevice = mitk::USVideoDevice::New(
99  m_Controls->m_FilePathSelector->text().toStdString(),
100  m_Controls->m_Manufacturer->text().toStdString(),
101  m_Controls->m_Model->text().toStdString());
102  newDevice->SetComment(m_Controls->m_Comment->text().toStdString());
103  }
104  else if (m_Controls->m_RadioOIGTLClientSource->isChecked())
105  {
106  std::string host = m_Controls->m_OIGTLClientHost->text().toStdString();
107  int port = m_Controls->m_OIGTLClientPort->value();
108 
109  // Create a new USIGTLDevice. The last parameter tells the device that it should be a client.
111  mitk::USIGTLDevice::New(m_Controls->m_Manufacturer->text().toStdString(),
112  m_Controls->m_Model->text().toStdString(), host, port, false);
113  device->Initialize();
114  emit Finished();
115  // The rest of this method does stuff that is specific to USVideoDevices,
116  // which we don't need. So we return directly.
117  return;
118  }
119  else
120  {
121  std::string host = m_Controls->m_OIGTLServerHost->text().toStdString();
122  int port = m_Controls->m_OIGTLServerPort->value();
123 
124  // Create a new USIGTLDevice. The last parameter tells the device that it should be a server.
126  mitk::USIGTLDevice::New(m_Controls->m_Manufacturer->text().toStdString(),
127  m_Controls->m_Model->text().toStdString(), host, port, true);
128  device->Initialize();
129  emit Finished();
130  // The rest of this method does stuff that is specific to USVideoDevices,
131  // which we don't need. So we return directly.
132  return;
133  }
134 
135  // get USImageVideoSource from new device
137  dynamic_cast<mitk::USImageVideoSource*>(
138  newDevice->GetUSImageSource().GetPointer());
139  if (!imageSource)
140  {
141  MITK_ERROR << "There is no USImageVideoSource at the current device.";
142  mitkThrow() << "There is no USImageVideoSource at the current device.";
143  }
144 
145  // Set Video Options
146  imageSource->SetColorOutput(!m_Controls->m_CheckGreyscale->isChecked());
147 
148  // If Resolution override is activated, apply it
149  if (m_Controls->m_CheckResolutionOverride->isChecked())
150  {
151  int width = m_Controls->m_ResolutionWidth->value();
152  int height = m_Controls->m_ResolutionHeight->value();
153  imageSource->OverrideResolution(width, height);
154  imageSource->SetResolutionOverride(true);
155  }
156  if (!m_Controls->m_ProbesInformation->text().isEmpty()) //there are informations about the probes of the device, so create the probes
157  {
158  AddProbesToDevice(newDevice);
159  }
160  else //no information about the probes of the device, so set default value
161  {
162  mitk::USProbe::Pointer probe = mitk::USProbe::New("default");
163  probe->SetDepth(0);
164  newDevice->AddNewProbe(probe);
165  }
166  newDevice->Initialize();
168 
169  emit Finished();
170 }
171 
173 {
174  m_Active = false;
175  m_TargetDevice->SetManufacturer(m_Controls->m_Manufacturer->text().toStdString());
176  m_TargetDevice->SetName(m_Controls->m_Model->text().toStdString());
177  m_TargetDevice->SetComment(m_Controls->m_Comment->text().toStdString());
178 
179  if (!m_Controls->m_ProbesInformation->text().isEmpty()){ //there is information about probes to add, so add them
181  }
183  dynamic_cast<mitk::USImageVideoSource*>(
184  m_TargetDevice->GetUSImageSource().GetPointer());
185  if (!imageSource)
186  {
187  MITK_ERROR << "There is no USImageVideoSource at the current device.";
188  mitkThrow() << "There is no USImageVideoSource at the current device.";
189  }
190 
191  // Set Video Options
192  imageSource->SetColorOutput(!m_Controls->m_CheckGreyscale->isChecked());
193 
194  // If Resolution override is activated, apply it
195  if (m_Controls->m_CheckResolutionOverride->isChecked())
196  {
197  int width = m_Controls->m_ResolutionWidth->value();
198  int height = m_Controls->m_ResolutionHeight->value();
199  imageSource->OverrideResolution(width, height);
200  imageSource->SetResolutionOverride(true);
201  }
203  MITK_INFO << "Finished Editing";
204  emit Finished();
205 }
206 
208 {
209  m_TargetDevice = 0;
210  m_Active = false;
213  emit Finished();
214 }
215 
217 {
218  m_Controls->m_FilePathSelector->setEnabled(
219  m_Controls->m_RadioFileSource->isChecked());
220  m_Controls->m_DeviceSelector->setEnabled(
221  m_Controls->m_RadioDeviceSource->isChecked());
222  m_Controls->m_OIGTLClientHost->setEnabled(
223  m_Controls->m_RadioOIGTLClientSource->isChecked());
224  m_Controls->m_OIGTLClientPort->setEnabled(
225  m_Controls->m_RadioOIGTLClientSource->isChecked());
226  m_Controls->m_OIGTLServerHost->setEnabled(
227  m_Controls->m_RadioOIGTLServerSource->isChecked());
228  m_Controls->m_OIGTLServerPort->setEnabled(
229  m_Controls->m_RadioOIGTLServerSource->isChecked());
230 }
231 
233 {
234  QString fileName = QFileDialog::getOpenFileName(NULL, "Open Video File");
235  if (fileName.isNull())
236  {
237  return;
238  } // user pressed cancel
239 
240  m_Controls->m_FilePathSelector->setText(fileName);
241 
242  m_Controls->m_RadioFileSource->setChecked(true);
243  this->OnDeviceTypeSelection();
244 }
245 
247 
249 {
250  // If no VideoDevice is given, throw an exception
251  if (device->GetDeviceClass().compare("org.mitk.modules.us.USVideoDevice") !=
252  0)
253  {
254  // TODO Alert if bad path
255  mitkThrow() << "NewVideoDeviceWidget recieved an incompatible device type "
256  "to edit. Type was: " << device->GetDeviceClass();
257  }
258  m_TargetDevice = static_cast<mitk::USVideoDevice*>(device.GetPointer());
259  m_Active = true;
260 
262 }
263 
265 {
266  //When new device is created there are no probes to edit, therefore disable the Groupbox
267  m_Controls->m_GroupBoxEditProbes->setEnabled(false);
268 
269  //Toggle functionality of Btn_Done
270  connect(m_Controls->m_BtnDone, SIGNAL(clicked()), this, SLOT(OnClickedDone()));
271  m_Controls->m_BtnDone->setText("Add Video Device");
272 
273  //Fill Metadata with default information
274  m_Controls->m_Manufacturer->setText("Unknown Manufacturer");
275  m_Controls->m_Model->setText("Unknown Model");
276  m_Controls->m_Comment->setText("None");
277 
278  m_TargetDevice = 0;
279  m_Active = true;
280 }
281 
283 
286 {
287  QListWidgetItem* result = new QListWidgetItem;
288  std::string text =
289  device->GetDeviceManufacturer() + "|" + device->GetDeviceModel();
290  result->setText(text.c_str());
291  return result;
292 }
293 
295 {
296  //deactivate the group box containing Videosource options because they should not be changed
297  m_Controls->m_GroupBoxVideoSource->setEnabled(false);
298 
299  //activate the groupbox contaning the options to edit the probes of the device and fill it with information
300  m_Controls->m_GroupBoxEditProbes->setEnabled(true);
301  std::vector<mitk::USProbe::Pointer> probes = m_TargetDevice->GetAllProbes();
302  for (std::vector<mitk::USProbe::Pointer>::iterator it = probes.begin(); it != probes.end(); it++)
303  {
304  std::string probeName = (*it)->GetName();
305  m_Controls->m_Probes->addItem(QString::fromUtf8(probeName.data(), probeName.size()));
306  }
307  OnProbeChanged(m_Controls->m_Probes->currentText());
308 
309  //Toggle functionality of Btn_Done
310  m_Controls->m_BtnDone->setText("Save Changes");
311  connect(m_Controls->m_BtnDone, SIGNAL(clicked()), this, SLOT(OnClickedFinishedEditing()));
312 
313  //Fill Metadata with Information provided by the Device selected to edit
314  m_Controls->m_Manufacturer->setText(m_TargetDevice->GetManufacturer().c_str());
315  m_Controls->m_Model->setText(m_TargetDevice->GetName().c_str());
316  m_Controls->m_Comment->setText(m_TargetDevice->GetComment().c_str());
317 }
318 
320 {
321  if (!m_Controls->m_Probes->currentText().isEmpty())
322  {
323  std::string probename = m_Controls->m_Probes->currentText().toStdString();
324  mitk::USProbe::Pointer currentProbe = m_TargetDevice->GetProbeByName(probename);
325  QString depths = m_Controls->m_AddDepths->text();
326  QStringList singleDepths = depths.split(',');
327  for (int i = 0; i < singleDepths.size(); i++)
328  {
329  currentProbe->SetDepth(singleDepths.at(i).toInt());
330  }
331  m_Controls->m_AddDepths->clear();
332  OnProbeChanged(m_Controls->m_Probes->currentText());
333  }
334 }
335 
337 {
338  if (!m_Controls->m_Probes->currentText().isEmpty() && !m_Controls->m_Depths->currentText().isEmpty())
339  {
340  std::string probename = m_Controls->m_Probes->currentText().toStdString();
341  int indexOfDepthToRemove = m_Controls->m_Depths->currentIndex();
342  mitk::USProbe::Pointer currentProbe = m_TargetDevice->GetProbeByName(probename);
343  currentProbe->RemoveDepth(m_Controls->m_Depths->currentText().toInt());
344  m_Controls->m_Depths->removeItem(indexOfDepthToRemove);
345  }
346 }
347 
349 {
350  if (!m_Controls->m_Probes->currentText().isEmpty())
351  {
352  std::string probename = m_Controls->m_Probes->currentText().toStdString();
353  int indexOfProbeToRemove = m_Controls->m_Probes->currentIndex();
354  m_TargetDevice->RemoveProbeByName(probename);
355  m_Controls->m_Probes->removeItem(indexOfProbeToRemove);
356  }
357 }
358 
359 void QmitkUSNewVideoDeviceWidget::OnProbeChanged(const QString & probename)
360 {
361  if (!probename.isEmpty())
362  {
363  std::string name = probename.toStdString();
364  mitk::USProbe::Pointer probe = m_TargetDevice->GetProbeByName(name);
365  std::map<int, mitk::Vector3D> depths = probe->GetDepthsAndSpacing();
366  m_Controls->m_Depths->clear();
367  for (std::map<int, mitk::Vector3D>::iterator it = depths.begin(); it != depths.end(); it++)
368  {
369  m_Controls->m_Depths->addItem(QString::number(it->first));
370  }
371  }
372 }
373 
375 {
376  disconnect(m_Controls->m_BtnDone, SIGNAL(clicked()), this, SLOT(OnClickedDone()));
377  m_Controls->m_ProbesInformation->clear();
378 }
379 
381 {
382  disconnect(m_Controls->m_BtnDone, SIGNAL(clicked()), this, SLOT(OnClickedFinishedEditing()));
383  m_Controls->m_Probes->clear();
384  m_Controls->m_Depths->clear();
385  m_Controls->m_AddDepths->clear();
386  m_Controls->m_ProbesInformation->clear();
387 }
388 
390 {
391  QString probesInformation = m_Controls->m_ProbesInformation->text();
392  QStringList probes = probesInformation.split(';'); //split the different probes
393  for (int i = 0; i < probes.size(); i++)
394  {
395  QStringList depths = probes.at(i).split(','); //now for every probe split the probe name and the different depths
397  probe->SetName(depths.at(0).toStdString()); //first element is the probe name
398  for (int i = 1; i < depths.size(); i++) //all the other elements are the depths for the specific probe so add them to the probe
399  {
400  probe->SetDepth(depths.at(i).toInt());
401  }
402  device->AddNewProbe(probe);
403  }
404 }
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
QmitkUSNewVideoDeviceWidget(QWidget *p=0, Qt::WindowFlags f1=0)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
QListWidgetItem * ConstructItemFromDevice(mitk::USDevice::Pointer device)
static Pointer New()
Method for creation through the object factory.
This class can be pointed to a video file or a videodevice and delivers USImages. ...
void AddProbesToDevice(mitk::USVideoDevice::Pointer device)
virtual void CreateQtPartControl(QWidget *parent)
#define mitkThrow()
void OnProbeChanged(const QString &probename)
static Pointer New()
mitk::USVideoDevice::Pointer m_TargetDevice
This is the device to edit. It is either the device transmitted in the "EditDevice" signal...
A mitk::USVideoDevice is the common class for video only devices. They capture video input either fro...
Ui::QmitkUSNewVideoDeviceWidgetControls * m_Controls
member holding the UI elements of this widget
void EditDevice(mitk::USDevice::Pointer device)