Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  addReps();
76 
77  this->connectStream();
78 }
79 
81 {
82  if (mView)
83  mView->removeReps();
84 }
85 
87 {
88  return mView;
89 }
90 
92 {
94  this->connectStream();
95 }
96 
97 void ViewWrapperVideo::appendToContextMenu(QMenu& contextMenu)
98 {
99  QAction* showSectorAction = new QAction("Show Sector", &contextMenu);
100  showSectorAction->setCheckable(true);
101  if (mStreamRep)
102  showSectorAction->setChecked(mStreamRep->getShowSector());
103  connect(showSectorAction, SIGNAL(triggered(bool)), this, SLOT(showSectorActionSlot(bool)));
104 
105  contextMenu.addSeparator();
106 
107 // QActionGroup sourceGroup = new QActionGroup(&contextMenu);
108  QMenu* sourceMenu = new QMenu("Video Source", &contextMenu);
109  std::vector<VideoSourcePtr> sources = mServices->video()->getVideoSources();
110  this->addStreamAction("active", sourceMenu);
111  for (unsigned i=0; i<sources.size(); ++i)
112  this->addStreamAction(sources[i]->getUid(), sourceMenu);
113  contextMenu.addMenu(sourceMenu);
114 
115 // contextMenu.addSeparator();
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 // std::cout << "ViewWrapperVideo::connectStream() selected=" << mViewGroup->getVideoSource() << std::endl;
165  VideoSourcePtr source = this->getSourceFromService(mGroupData->getVideoSource());
166 // if (source)
167 // std::cout << "ViewWrapperVideo::connectStream() " << source->getUid() << std::endl;
168 // else
169 // std::cout << "ViewWrapperVideo::connectStream() NULL" << std::endl;
170 
171 
172  QString uid;
173  if (source)
174  uid = source->getUid();
175 
176  ToolPtr newTool;
177  ToolPtr tool = mServices->tracking()->getFirstProbe();
178  if (tool && tool->getProbe())
179  {
180  if (tool->getProbe()->getAvailableVideoSources().count(uid))
181  {
182  newTool = tool;
183  source = tool->getProbe()->getRTSource(uid);
184 
185 // if (source)
186 // std::cout << "ViewWrapperVideo::connectStream() from probe " << source->getUid() << std::endl;
187 // else
188 // std::cout << "ViewWrapperVideo::connectStream() from probe NULL" << std::endl;
189  }
190  }
191 
192  this->setupRep(source, newTool);
193 }
194 
195 VideoSourcePtr ViewWrapperVideo::getSourceFromService(QString uid)
196 {
197  if (uid=="active")
198  return mServices->video()->getActiveVideoSource();
199 
200  std::vector<VideoSourcePtr> source = mServices->video()->getVideoSources();
201 
202  for (unsigned i=0; i< source.size(); ++i)
203  {
204  if (source[i]->getUid()==uid)
205  return source[i];
206  }
207  return VideoSourcePtr();
208 }
209 
210 void ViewWrapperVideo::setupRep(VideoSourcePtr source, ToolPtr tool)
211 {
212 // std::cout << "ViewWrapperVideo::setupRep() " << source.get() << " " << source->getUid() << std::endl;
213 
214  //Don't do anything if source is the same
215  if (( mSource == source )&&( tool==mTool ))
216  return;
217  if (mSource)
218  {
219  disconnect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
220  }
221  mSource = source;
222  if (mSource)
223  {
224  connect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
225  }
226 
227  if (!mSource)
228  return;
229 
230  if (!mStreamRep)
231  {
232  mStreamRep.reset(new VideoFixedPlaneRep("rtrep", "rtrep"));
233  mView->addRep(mStreamRep);
234  }
235 
236  mStreamRep->setRealtimeStream(mSource);
237  mStreamRep->setTool(tool);
238 // mDataNameText->setText(0, "initialized");
239  mDataNameText->setText(0, mSource->getName());
240  mStreamRep->setShowSector(settings()->value("showSectorInRTView").toBool());
241 
242 // report(
243 // "Setup video rep with source="
244 // + source->getName() + " and tool="
245 // + (tool ? tool->getName() : "none"));
246 }
247 
248 void ViewWrapperVideo::updateSlot()
249 {
250  if (!mSource)
251  return;
252  mDataNameText->setText(0, mSource->getName());
253 }
254 
255 void ViewWrapperVideo::addReps()
256 {
257  // plane type text rep
258  mPlaneTypeText = DisplayTextRep::New();
259  mPlaneTypeText->addText(QColor(Qt::green), "RT", Vector3D(0.98, 0.02, 0.0));
260  mView->addRep(mPlaneTypeText);
261 
262  //data name text rep
263  mDataNameText = DisplayTextRep::New();
264  mDataNameText->addText(QColor(Qt::green), "not initialized", Vector3D(0.02, 0.02, 0.0));
265  mView->addRep(mDataNameText);
266 }
267 
268 //------------------------------------------------------------------------------
269 }
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:50
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
Superclass for ViewWrappers.
Definition: cxViewWrapper.h:93
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 void setViewGroup(ViewGroupDataPtr group)
ViewGroupDataPtr mGroupData
boost::shared_ptr< class VideoSource > VideoSourcePtr
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
VisServicesPtr mServices
static DisplayTextRepPtr New(const QString &uid="")
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...
boost::shared_ptr< class Tool > ToolPtr