Fraxinus  17.12
An IGT application
cxViewWrapperVideo.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 
33 #include "cxViewWrapperVideo.h"
34 //#include <vector>
35 #include <vtkCamera.h>
36 #include <vtkRenderer.h>
37 #include <vtkRenderWindow.h>
38 
39 #include <QAction>
40 #include <QMenu>
41 
42 #include "cxView.h"
43 #include "cxVideoRep.h"
44 #include "cxDisplayTextRep.h"
45 
46 #include "cxTypeConversions.h"
47 
48 #include "cxSettings.h"
49 #include "cxTrackingService.h"
50 #include "cxVideoService.h"
51 #include "cxVisServices.h"
52 #include "vtkRenderWindowInteractor.h"
53 #include "cxTool.h"
54 #include "cxVideoSource.h"
55 
56 namespace cx
57 {
58 
60  ViewWrapper(services)
61 {
62  mView = view;
63  this->connectContextMenu(mView);
64 
65  // disable vtk interactor: this wrapper IS an interactor
66  mView->getRenderWindow()->GetInteractor()->Disable();
67  mView->getRenderer()->GetActiveCamera()->SetParallelProjection(true);
68  double clipDepth = 1.0; // 1mm depth, i.e. all 3D props rendered outside this range is not shown.
69  mView->getRenderer()->GetActiveCamera()->SetClippingRange(-clipDepth / 2.0, clipDepth / 2.0);
70 
71  connect(mServices->tracking().get(), &TrackingService::stateChanged, this, &ViewWrapperVideo::connectStream);
72  connect(mServices->video().get(), SIGNAL(activeVideoSourceChanged()), this, SLOT(connectStream()));
73  connect(mServices->tracking().get(), SIGNAL(activeToolChanged(QString)), this, SLOT(connectStream()));
74 
75  this->addReps();
76 
77  this->connectStream();
78 
79  this->updateView();
80 }
81 
83 {
84  if (mView)
85  mView->removeReps();
86 }
87 
89 {
90  return mView;
91 }
92 
94 {
96  this->connectStream();
97 }
98 
99 void ViewWrapperVideo::appendToContextMenu(QMenu& contextMenu)
100 {
101  QAction* showSectorAction = new QAction("Show Sector", &contextMenu);
102  showSectorAction->setCheckable(true);
103  if (mStreamRep)
104  showSectorAction->setChecked(mStreamRep->getShowSector());
105  connect(showSectorAction, SIGNAL(triggered(bool)), this, SLOT(showSectorActionSlot(bool)));
106 
107  contextMenu.addSeparator();
108 
109  QMenu* sourceMenu = new QMenu("Video Source", &contextMenu);
110  std::vector<VideoSourcePtr> sources = mServices->video()->getVideoSources();
111  this->addStreamAction("active", sourceMenu);
112  for (unsigned i=0; i<sources.size(); ++i)
113  this->addStreamAction(sources[i]->getUid(), sourceMenu);
114  contextMenu.addMenu(sourceMenu);
115 
116  contextMenu.addAction(showSectorAction);
117 }
118 
119 void ViewWrapperVideo::showSectorActionSlot(bool checked)
120 {
121  mStreamRep->setShowSector(checked);
122  settings()->setValue("showSectorInRTView", checked);
123 }
124 
125 void ViewWrapperVideo::addStreamAction(QString uid, QMenu* contextMenu)
126 {
127  QAction* action = new QAction(uid, contextMenu);
128 
129  VideoSourcePtr selected = this->getSourceFromService(mGroupData->getVideoSource());
130  VideoSourcePtr current = this->getSourceFromService(uid);
131 
132  action->setData(QVariant(uid));
133  action->setCheckable(true);
134  if (uid=="active")
135  action->setChecked(mGroupData->getVideoSource()=="active");
136  else
137  action->setChecked(selected && (selected==current));
138 
139  connect(action, SIGNAL(triggered()), this, SLOT(streamActionSlot()));
140  contextMenu->addAction(action);
141 }
142 
143 void ViewWrapperVideo::streamActionSlot()
144 {
145  QAction* theAction = static_cast<QAction*>(sender());
146  if(!theAction)
147  return;
148  if (!theAction->isChecked())
149  return;
150 
151  QString uid = theAction->data().toString();
152  mGroupData->setVideoSource(uid);
153 }
154 
156 {
157  this->connectStream();
158 }
159 
160 void ViewWrapperVideo::connectStream()
161 {
162  if (!mGroupData)
163  return;
164  VideoSourcePtr source = this->getSourceFromService(mGroupData->getVideoSource());
165 
166  QString uid;
167  if (source)
168  uid = source->getUid();
169 
170  ToolPtr newTool;
171  ToolPtr tool = mServices->tracking()->getFirstProbe();
172  if (tool && tool->getProbe())
173  {
174  if (tool->getProbe()->getAvailableVideoSources().count(uid))
175  {
176  newTool = tool;
177  source = tool->getProbe()->getRTSource(uid);
178  }
179  }
180 
181  this->setupRep(source, newTool);
182 }
183 
184 VideoSourcePtr ViewWrapperVideo::getSourceFromService(QString uid)
185 {
186  if (uid=="active")
187  return mServices->video()->getActiveVideoSource();
188 
189  std::vector<VideoSourcePtr> source = mServices->video()->getVideoSources();
190 
191  for (unsigned i=0; i< source.size(); ++i)
192  {
193  if (source[i]->getUid()==uid)
194  return source[i];
195  }
196  return VideoSourcePtr();
197 }
198 
199 void ViewWrapperVideo::setupRep(VideoSourcePtr source, ToolPtr tool)
200 {
201 // std::cout << "ViewWrapperVideo::setupRep() " << source.get() << " " << source->getUid() << std::endl;
202 
203  //Don't do anything if source is the same
204  if (( mSource == source )&&( tool==mTool ))
205  return;
206  if (mSource)
207  {
208  disconnect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
209  }
210  mSource = source;
211  if (mSource)
212  {
213  connect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
214  }
215 
216  if (!mSource)
217  return;
218 
219  if (!mStreamRep)
220  {
221  mStreamRep.reset(new VideoFixedPlaneRep("rtrep", "rtrep"));
222  mView->addRep(mStreamRep);
223  }
224 
225  mStreamRep->setRealtimeStream(mSource);
226  mStreamRep->setTool(tool);
227  mStreamRep->setShowSector(settings()->value("showSectorInRTView").toBool());
228 
229 // report(
230 // "Setup video rep with source="
231 // + source->getName() + " and tool="
232 // + (tool ? tool->getName() : "none"));
233 }
234 
235 void ViewWrapperVideo::updateSlot()
236 {
237  this->updateView();
238 }
239 
241 {
242  if (mSource)
243  return mSource->getName();
244  return "not initialized";
245 }
246 
248 {
249  return "RT";
250 }
251 
252 //------------------------------------------------------------------------------
253 }
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:50
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:61
Superclass for ViewWrappers.
virtual ViewPtr getView()
virtual void videoSourceChangedSlot(QString uid)
boost::shared_ptr< class View > ViewPtr
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:79
virtual void setViewGroup(ViewGroupDataPtr group)
virtual QString getViewDescription()
virtual void setViewGroup(ViewGroupDataPtr group)
ViewGroupDataPtr mGroupData
boost::shared_ptr< class VideoSource > VideoSourcePtr
Display a VideoSource in a View.
Definition: cxVideoRep.h:66
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
virtual void addReps()
VisServicesPtr mServices
virtual void updateView()
ViewWrapperVideo(ViewPtr view, VisServicesPtr services)
void connectContextMenu(ViewPtr view)
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr
virtual QString getDataDescription()