CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxVideoConnectionWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include <boost/bind.hpp>
36 
37 #include <QDir>
38 #include <QStackedWidget>
39 #include <QPushButton>
40 #include <QFileDialog>
41 
42 #include "vtkImageData.h"
43 
44 #include "cxTime.h"
45 #include "cxLogger.h"
46 #include "cxProbeSector.h"
48 #include "cxStringProperty.h"
49 #include "cxHelperWidgets.h"
50 #include "cxDataInterface.h"
51 #include "cxTrackingService.h"
52 #include "cxOptionsWidget.h"
53 #include "cxVideoService.h"
54 #include "cxPatientModelService.h"
56 #include "cxStreamerService.h"
57 #include "cxVideoSource.h"
58 #include "cxViewService.h"
59 #include "cxImage.h"
60 #include "cxProfile.h"
61 
62 namespace cx
63 {
64 
66  BaseWidget(parent, "IGTLinkWidget", "Video Connection"),
67  mServices(services)
68 {
69  this->setToolTip("Connect to a video source");
70  mOptions = profile()->getXmlSettings().descend("video");
71 
72  QString defaultConnection = mServices->videoService->getConnectionMethod();
73  mConnectionSelector = StringProperty::initialize("Connection", "", "Method for connecting to Video Server", defaultConnection, QStringList(), mOptions.getElement("video"));
74  connect(mConnectionSelector.get(), SIGNAL(changed()), this, SLOT(selectGuiForConnectionMethodSlot()));
75 
77 
78  mStackedWidget = new QStackedWidget(this);
84 
85  mToptopLayout = new QVBoxLayout(this);
88  mToptopLayout->addWidget(mConnectButton);
91  mToptopLayout->addStretch();
92 
93  connect(mServices->videoService.get(), SIGNAL(StreamerServiceAdded(StreamerService*)), this, SLOT(onServiceAdded(StreamerService*)));
94  connect(mServices->videoService.get(), SIGNAL(StreamerServiceRemoved(StreamerService*)), this, SLOT(onServiceRemoved(StreamerService*)));
95 
96  this->addExistingStreamerServices(); //Need to add StreamerServices already existing at this point, since we will only get signals when new Services are added
97 
99 }
100 
102 {
103  if (mServices->videoService)
104  {
105  disconnect(mServices->videoService.get(), SIGNAL(StreamerServiceAdded(StreamerService*)), this, SLOT(onServiceAdded(StreamerService*)));
106  disconnect(mServices->videoService.get(), SIGNAL(StreamerServiceRemoved(StreamerService*)), this, SLOT(onServiceRemoved(StreamerService*)));
107  }
108 }
109 
110 void VideoConnectionWidget::addExistingStreamerServices()
111 {
112  QList<StreamerService *> services = mServices->videoService->getStreamerServices();
113  foreach(StreamerService* service, services)
114  {
115  this->onServiceAdded(service);
116  }
117 }
118 
120 {
121  QWidget* widget = this->createStreamerWidget(service);
122  QWidget* serviceWidget = this->wrapVerticalStretch(widget);
123  mStackedWidget->addWidget(serviceWidget);
124  mStreamerServiceWidgets[service->getName()] = serviceWidget;
125 
126  this->addServiceToSelector(service->getName());
127 }
128 
129 QWidget* VideoConnectionWidget::createStreamerWidget(StreamerService* service)
130 {
131  QString serviceName = service->getName();
132  QDomElement element = mOptions.getElement("video");
133  std::vector<PropertyPtr> adapters = service->getSettings(element);
134 
135  OptionsWidget* widget = new OptionsWidget(mServices->visualizationService, mServices->patientModelService, this);
136  widget->setOptions(serviceName, adapters, false);
137  widget->setObjectName(service->getType());
138  widget->setFocusPolicy(Qt::StrongFocus); // needed for help system: focus is used to display help text
139 
140  connect(mConnectionSelectionWidget, SIGNAL(detailsTriggered()), widget, SLOT(toggleAdvanced()));
141 
142  return widget;
143 }
144 
146 {
147  this->removeServiceFromSelector(service->getName());
148  this->removeServiceWidget(service->getName());
149 }
150 
151 void VideoConnectionWidget::addServiceToSelector(QString name)
152 {
153  QStringList range = mConnectionSelector->getValueRange();
154  range.append(name);
155  range.removeDuplicates();
156  mConnectionSelector->setValueRange(range);
157 }
158 
159 void VideoConnectionWidget::removeServiceFromSelector(QString name)
160 {
161  QStringList range = mConnectionSelector->getValueRange();
162  int index = range.indexOf(name);
163  if(mConnectionSelector->getValue() == name)
164  mConnectionSelector->setValue(range[0]);
165  range.removeAt(index);
166  mConnectionSelector->setValueRange(range);
167 }
168 
169 void VideoConnectionWidget::removeServiceWidget(QString name)
170 {
171  QWidget* serviceWidget = mStreamerServiceWidgets[name];
172  mStackedWidget->removeWidget(serviceWidget);
173  delete serviceWidget;
174  mStreamerServiceWidgets.erase(name);
175 }
176 
178 {
180 }
181 
183 {
184  QFrame* frame = new QFrame(this);
185  frame->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
186  frame->setSizePolicy(frame->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
187  QVBoxLayout* frameLayout = new QVBoxLayout(frame);
188  frameLayout->addWidget(mStackedWidget);
189  frame->setFocusPolicy(Qt::StrongFocus); // needed for help system: focus is used to display help text
190 
191  return frame;
192 }
193 
195 {
196  QWidget* retval = new QWidget(this);
197  QVBoxLayout* layout = new QVBoxLayout(retval);
198  layout->addWidget(input);
199  retval->setObjectName(input->objectName()); // help propagation
200  layout->addStretch();
201  layout->setMargin(0);
202  layout->setSpacing(0);
203  return retval;
204 }
205 
207 {
208  QString name = mConnectionSelector->getValue();
209  //Need to set connection method in VideoConnectionManager before calling useDirectLink(), useLocalServer() and useRemoteServer()
210  mServices->videoService->setConnectionMethod(mConnectionSelector->getValue());
211 
212  QWidget* serviceWidget = mStreamerServiceWidgets[name];
213  if(serviceWidget)
214  {
215  mStackedWidget->setCurrentWidget(serviceWidget);
216  mStackedWidgetFrame->setObjectName(serviceWidget->objectName()); // for improved help
217  }
218 }
219 
221 {
222  if (mServices->videoService->isConnected())
223  mServices->videoService->closeConnection();
224  else
225  mServices->videoService->openConnection();
226 }
227 
229 {
230  QPushButton* connectButton = new QPushButton("Connect", this);
231  connectButton->setToolTip("Connect/disconnect to the video server using the seleted method");
232  connect(connectButton, SIGNAL(clicked()), this, SLOT(toggleConnectServer()));
233  return connectButton;
234 }
235 
237 {
238  QPushButton* importstreamimagebutton = new QPushButton("Import image from stream", this);
239  importstreamimagebutton->setToolTip("Import a single image/volume from the real time stream");
240  importstreamimagebutton->setDisabled(true);
241  connect(importstreamimagebutton, SIGNAL(clicked()), this, SLOT(importStreamImageSlot()));
242 
243  return importstreamimagebutton;
244 }
245 
247 {
248  mImportStreamImageButton->setEnabled(mServices->videoService->isConnected());
249  if (mServices->videoService->isConnected())
250  mConnectButton->setText("Disconnect Server");
251  else
252  mConnectButton->setText("Connect Server");
253 
254  this->adjustSize();
255 }
256 
258 {
259  if (!mServices->videoService->isConnected())
260  {
261  reportWarning("Video is not connected");
262  return;
263  }
264  Transform3D rMd = Transform3D::Identity();
265  ToolPtr probe = mServices->trackingService->getFirstProbe();
266  VideoSourcePtr videoSource;
267  if (probe)
268  {
269  videoSource = probe->getProbe()->getRTSource();
270  rMd = calculate_rMd_ForAProbeImage(probe);
271  }
272  else
273  videoSource = mServices->videoService->getActiveVideoSource();
274 
275  if (!videoSource)
276  {
277  reportWarning("No Video data source");
278  return;
279  }
280  if (!videoSource->validData())
281  {
282  reportWarning("No valid video data");
283  return;
284  }
285 
286  vtkImageDataPtr input;
287  input = videoSource->getVtkImageData();
288  if (!input)
289  {
290  reportWarning("No Video data");
291  return;
292  }
293  QString filename = generateFilename(videoSource);
294 
295  this->saveAndImportSnapshot(input, filename, rMd);
296 
297 }
298 
300 {
301  Transform3D rMd = Transform3D::Identity();
302  Transform3D rMpr = mServices->patientModelService->get_rMpr();
303  Transform3D prMt = probe->get_prMt();
304  Transform3D tMu = probe->getProbe()->getSector()->get_tMu();
305  Transform3D uMv = probe->getProbe()->getSector()->get_uMv();
306  rMd = rMpr * prMt * tMu * uMv;
307  return rMd;
308 }
309 
311 {
312  vtkImageDataPtr input = videoSource->getVtkImageData();
313  int* extent = input->GetExtent();
314  QString filename;
315  QString format = timestampSecondsFormat();
316  if (extent[5] - extent[4] > 0)
317  filename = "3DRTSnapshot_";
318  else
319  filename = "2DRTSnapshot_";
320 
321  filename += videoSource->getName() + QDateTime::currentDateTime().toString(format);
322  return filename;
323 }
324 
326 {
327  vtkImageDataPtr copiedImage = vtkImageDataPtr::New();
328  copiedImage->DeepCopy(input);
329 
330  ImagePtr output = mServices->patientModelService->createSpecificData<Image>(filename);
331  output->setVtkImageData(input);
332  output->get_rMd_History()->setRegistration(rMd);
333  mServices->patientModelService->insertData(output);
334 
335  mServices->visualizationService->autoShowData(output);
336  report(QString("Saved snapshot %1 from active video source").arg(output->getName()));
337 }
338 
339 } //end namespace cx
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
QString generateFilename(VideoSourcePtr videoSource)
boost::shared_ptr< class StringPropertyActiveVideoSource > StringPropertyActiveVideoSourcePtr
DetailedLabeledComboBoxWidget * mConnectionSelectionWidget
void onServiceAdded(StreamerService *service)
void connected(bool on)
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
virtual void setVtkImageData(const vtkImageDataPtr &data, bool resetTransferFunctions=true)
Definition: cxImage.cpp:289
virtual std::vector< PropertyPtr > getSettings(QDomElement root)=0
virtual QString getType() const =0
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
static StringPropertyActiveVideoSourcePtr New()
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
QDomElement getElement()
return the current element
void onServiceRemoved(StreamerService *service)
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
Composite widget for string selection with .
A volumetric data set.
Definition: cxImage.h:66
boost::shared_ptr< class VideoSource > VideoSourcePtr
virtual QString getName()=0
StringPropertyActiveVideoSourcePtr mActiveVideoSourceSelector
QWidget * wrapVerticalStretch(QWidget *input)
Transform3D calculate_rMd_ForAProbeImage(ToolPtr probe)
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
void saveAndImportSnapshot(vtkImageDataPtr input, QString filename, Transform3D rMd)
void report(QString msg)
Definition: cxLogger.cpp:90
VideoConnectionWidget(VisServicesPtr services, QWidget *parent)
QPushButton * initializeImportStreamImageButton()
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
StringPropertyActiveVideoSourcePtr initializeActiveVideoSourceSelector()
Abstract class. Interface to Streamers.
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void setOptions(QString uid, std::vector< PropertyPtr > options, bool showAdvanced)
boost::shared_ptr< class Tool > ToolPtr