CustusX  15.8
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<StreamerServicePtr> services = mServices->videoService->getStreamerServices();
113  foreach(StreamerServicePtr service, services)
114  {
115  this->onServiceAdded(service.get());
116  }
117 }
118 
120 {
121  QWidget* widget = this->createStreamerWidget(service);
122  QWidget* serviceWidget = this->wrapVerticalStretch(widget);
123  mStackedWidget->addWidget(serviceWidget);
124  mStreamerServiceWidgets[service->getType()] = serviceWidget;
125 
126  this->addServiceToSelector(service);
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(service->getType(), 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);
148  this->removeServiceWidget(service->getType());
149 }
150 
151 void VideoConnectionWidget::addServiceToSelector(StreamerService *service)
152 {
153  QStringList range = mConnectionSelector->getValueRange();
154  std::map<QString, QString> display = mConnectionSelector->getDisplayNames();
155 
156  range.append(service->getType());
157  range.removeDuplicates();
158  display[service->getType()] = service->getName();
159 
160  mConnectionSelector->setValueRange(range);
161  mConnectionSelector->setDisplayNames(display);
162 }
163 
164 void VideoConnectionWidget::removeServiceFromSelector(StreamerService *service)
165 {
166  QStringList range = mConnectionSelector->getValueRange();
167 
168  int index = range.indexOf(service->getType());
169  if(mConnectionSelector->getValue() == service->getType())
170  mConnectionSelector->setValue(range[0]);
171  range.removeAt(index);
172  mConnectionSelector->setValueRange(range);
173 }
174 
175 void VideoConnectionWidget::removeServiceWidget(QString name)
176 {
177  QWidget* serviceWidget = mStreamerServiceWidgets[name];
178  mStackedWidget->removeWidget(serviceWidget);
179  delete serviceWidget;
180  mStreamerServiceWidgets.erase(name);
181 }
182 
184 {
186 }
187 
189 {
190  QFrame* frame = new QFrame(this);
191  frame->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
192  frame->setSizePolicy(frame->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
193  QVBoxLayout* frameLayout = new QVBoxLayout(frame);
194  frameLayout->addWidget(mStackedWidget);
195  frame->setFocusPolicy(Qt::StrongFocus); // needed for help system: focus is used to display help text
196 
197  return frame;
198 }
199 
201 {
202  QWidget* retval = new QWidget(this);
203  QVBoxLayout* layout = new QVBoxLayout(retval);
204  layout->addWidget(input);
205  retval->setObjectName(input->objectName()); // help propagation
206  layout->addStretch();
207  layout->setMargin(0);
208  layout->setSpacing(0);
209  return retval;
210 }
211 
213 {
214  QString name = mConnectionSelector->getValue();
215  //Need to set connection method in VideoConnectionManager before calling
216  //useDirectLink(), useLocalServer() and useRemoteServer()
217  mServices->videoService->setConnectionMethod(name);
218 
219  QWidget* serviceWidget = mStreamerServiceWidgets[name];
220  if(serviceWidget)
221  {
222  mStackedWidget->setCurrentWidget(serviceWidget);
223  mStackedWidgetFrame->setObjectName(serviceWidget->objectName()); // for improved help
224  }
225 }
226 
228 {
229  if (mServices->videoService->isConnected())
230  mServices->videoService->closeConnection();
231  else
232  mServices->videoService->openConnection();
233 }
234 
236 {
237  QPushButton* connectButton = new QPushButton("Connect", this);
238  connectButton->setToolTip("Connect/disconnect to the video server using the seleted method");
239  connect(connectButton, SIGNAL(clicked()), this, SLOT(toggleConnectServer()));
240  return connectButton;
241 }
242 
244 {
245  QPushButton* importstreamimagebutton = new QPushButton("Import image from stream", this);
246  importstreamimagebutton->setToolTip("Import a single image/volume from the real time stream");
247  importstreamimagebutton->setDisabled(true);
248  connect(importstreamimagebutton, SIGNAL(clicked()), this, SLOT(importStreamImageSlot()));
249 
250  return importstreamimagebutton;
251 }
252 
254 {
255  mImportStreamImageButton->setEnabled(mServices->videoService->isConnected());
256  if (mServices->videoService->isConnected())
257  mConnectButton->setText("Disconnect Server");
258  else
259  mConnectButton->setText("Connect Server");
260 
261  this->adjustSize();
262 }
263 
265 {
266  if (!mServices->videoService->isConnected())
267  {
268  reportWarning("Video is not connected");
269  return;
270  }
271  Transform3D rMd = Transform3D::Identity();
272  ToolPtr probe = mServices->trackingService->getFirstProbe();
273  VideoSourcePtr videoSource;
274  if (probe)
275  {
276  videoSource = probe->getProbe()->getRTSource();
277  rMd = calculate_rMd_ForAProbeImage(probe);
278  }
279  else
280  videoSource = mServices->videoService->getActiveVideoSource();
281 
282  if (!videoSource)
283  {
284  reportWarning("No Video data source");
285  return;
286  }
287  if (!videoSource->validData())
288  {
289  reportWarning("No valid video data");
290  return;
291  }
292 
293  vtkImageDataPtr input;
294  input = videoSource->getVtkImageData();
295  if (!input)
296  {
297  reportWarning("No Video data");
298  return;
299  }
300  QString filename = generateFilename(videoSource);
301 
302  this->saveAndImportSnapshot(input, filename, rMd);
303 
304 }
305 
307 {
308  Transform3D rMd = Transform3D::Identity();
309  Transform3D rMpr = mServices->patientModelService->get_rMpr();
310  Transform3D prMt = probe->get_prMt();
311  Transform3D tMu = probe->getProbe()->getSector()->get_tMu();
312  Transform3D uMv = probe->getProbe()->getSector()->get_uMv();
313  rMd = rMpr * prMt * tMu * uMv;
314  return rMd;
315 }
316 
318 {
319  vtkImageDataPtr input = videoSource->getVtkImageData();
320  int* extent = input->GetExtent();
321  QString filename;
322  QString format = timestampSecondsFormat();
323  if (extent[5] - extent[4] > 0)
324  filename = "3DRTSnapshot_";
325  else
326  filename = "2DRTSnapshot_";
327 
328  filename += videoSource->getName() + QDateTime::currentDateTime().toString(format);
329  return filename;
330 }
331 
333 {
334  vtkImageDataPtr copiedImage = vtkImageDataPtr::New();
335  copiedImage->DeepCopy(input);
336 
337  ImagePtr output = mServices->patientModelService->createSpecificData<Image>(filename);
338  output->setVtkImageData(input);
339  output->get_rMd_History()->setRegistration(rMd);
340  mServices->patientModelService->insertData(output);
341 
342  mServices->visualizationService->autoShowData(output);
343  report(QString("Saved snapshot %1 from active video source").arg(output->getName()));
344 }
345 
346 } //end namespace cx
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:169
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:288
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.
boost::shared_ptr< class StreamerService > StreamerServicePtr
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