Fraxinus  2023.01.05-dev+develop.0da12
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxViewWrapperVideo.h"
13 //#include <vector>
14 #include <vtkCamera.h>
15 #include <vtkRenderer.h>
16 #include <vtkRenderWindow.h>
17 
18 #include <QAction>
19 #include <QMenu>
20 
21 #include "cxView.h"
22 #include "cxVideoRep.h"
23 #include "cxDisplayTextRep.h"
24 
25 #include "cxTypeConversions.h"
26 
27 #include "cxSettings.h"
28 #include "cxTrackingService.h"
29 #include "cxVideoService.h"
30 #include "cxVisServices.h"
31 #include "vtkRenderWindowInteractor.h"
32 #include "cxTool.h"
33 #include "cxVideoSource.h"
34 
35 namespace cx
36 {
37 
39  ViewWrapper(services)
40 {
41  mView = view;
42  this->connectContextMenu(mView);
43 
44  // disable vtk interactor: this wrapper IS an interactor
45  mView->getRenderWindow()->GetInteractor()->Disable();
46  mView->getRenderer()->GetActiveCamera()->SetParallelProjection(true);
47  double clipDepth = 1.0; // 1mm depth, i.e. all 3D props rendered outside this range is not shown.
48  mView->getRenderer()->GetActiveCamera()->SetClippingRange(-clipDepth / 2.0, clipDepth / 2.0);
49 
50  connect(mServices->tracking().get(), &TrackingService::stateChanged, this, &ViewWrapperVideo::connectStream);
51  connect(mServices->video().get(), SIGNAL(activeVideoSourceChanged()), this, SLOT(connectStream()));
52  connect(mServices->tracking().get(), SIGNAL(activeToolChanged(QString)), this, SLOT(connectStream()));
53 
54  this->addReps();
55 
56  this->connectStream();
57 
58  this->updateView();
59 }
60 
62 {
63  if (mView)
64  mView->removeReps();
65 }
66 
68 {
69  return mView;
70 }
71 
73 {
75  this->connectStream();
76 }
77 
78 void ViewWrapperVideo::appendToContextMenu(QMenu& contextMenu)
79 {
80  QAction* showSectorAction = new QAction("Show Sector", &contextMenu);
81  showSectorAction->setCheckable(true);
82  if (mStreamRep)
83  showSectorAction->setChecked(mStreamRep->getShowSector());
84  connect(showSectorAction, SIGNAL(triggered(bool)), this, SLOT(showSectorActionSlot(bool)));
85 
86  contextMenu.addSeparator();
87 
88  QMenu* sourceMenu = new QMenu("Video Source", &contextMenu);
89  std::vector<VideoSourcePtr> sources = mServices->video()->getVideoSources();
90  this->addStreamAction("active", sourceMenu);
91  for (unsigned i=0; i<sources.size(); ++i)
92  this->addStreamAction(sources[i]->getUid(), sourceMenu);
93  contextMenu.addMenu(sourceMenu);
94 
95  contextMenu.addAction(showSectorAction);
96 }
97 
98 void ViewWrapperVideo::showSectorActionSlot(bool checked)
99 {
100  mStreamRep->setShowSector(checked);
101  settings()->setValue("showSectorInRTView", checked);
102 }
103 
104 void ViewWrapperVideo::addStreamAction(QString uid, QMenu* contextMenu)
105 {
106  QAction* action = new QAction(uid, contextMenu);
107 
108  VideoSourcePtr selected = this->getSourceFromService(mGroupData->getVideoSource());
109  VideoSourcePtr current = this->getSourceFromService(uid);
110 
111  action->setData(QVariant(uid));
112  action->setCheckable(true);
113  if (uid=="active")
114  action->setChecked(mGroupData->getVideoSource()=="active");
115  else
116  action->setChecked(selected && (selected==current));
117 
118  connect(action, SIGNAL(triggered()), this, SLOT(streamActionSlot()));
119  contextMenu->addAction(action);
120 }
121 
122 void ViewWrapperVideo::streamActionSlot()
123 {
124  QAction* theAction = static_cast<QAction*>(sender());
125  if(!theAction)
126  return;
127  if (!theAction->isChecked())
128  return;
129 
130  QString uid = theAction->data().toString();
131  mGroupData->setVideoSource(uid);
132 }
133 
135 {
136  this->connectStream();
137 }
138 
139 void ViewWrapperVideo::connectStream()
140 {
141  if (!mGroupData)
142  return;
143  VideoSourcePtr source = this->getSourceFromService(mGroupData->getVideoSource());
144 
145  QString uid;
146  if (source)
147  uid = source->getUid();
148 
149  ToolPtr newTool;
150  ToolPtr tool = mServices->tracking()->getFirstProbe();
151  if (tool && tool->getProbe())
152  {
153  if (tool->getProbe()->getAvailableVideoSources().count(uid))
154  {
155  newTool = tool;
156  source = tool->getProbe()->getRTSource(uid);
157  }
158  }
159 
160  this->setupRep(source, newTool);
161 }
162 
163 VideoSourcePtr ViewWrapperVideo::getSourceFromService(QString uid)
164 {
165  if (uid=="active")
166  return mServices->video()->getActiveVideoSource();
167 
168  std::vector<VideoSourcePtr> source = mServices->video()->getVideoSources();
169 
170  for (unsigned i=0; i< source.size(); ++i)
171  {
172  if (source[i]->getUid()==uid)
173  return source[i];
174  }
175  return VideoSourcePtr();
176 }
177 
178 void ViewWrapperVideo::setupRep(VideoSourcePtr source, ToolPtr tool)
179 {
180 // std::cout << "ViewWrapperVideo::setupRep() " << source.get() << " " << source->getUid() << std::endl;
181 
182  //Don't do anything if source is the same
183  if (( mSource == source )&&( tool==mTool ))
184  return;
185  if (mSource)
186  {
187  disconnect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
188  }
189  mSource = source;
190  if (mSource)
191  {
192  connect(mSource.get(), &VideoSource::newFrame, this, &ViewWrapperVideo::updateSlot);
193  }
194 
195  if (!mSource)
196  return;
197 
198  if (!mStreamRep)
199  {
200  mStreamRep.reset(new VideoFixedPlaneRep("rtrep", "rtrep"));
201  mView->addRep(mStreamRep);
202  }
203 
204  mStreamRep->setRealtimeStream(mSource);
205  mStreamRep->setTool(tool);
206  mStreamRep->setShowSector(settings()->value("showSectorInRTView").toBool());
207 
208 // report(
209 // "Setup video rep with source="
210 // + source->getName() + " and tool="
211 // + (tool ? tool->getName() : "none"));
212 }
213 
214 void ViewWrapperVideo::updateSlot()
215 {
216  this->updateView();
217 }
218 
220 {
221  if (mSource)
222  return mSource->getName();
223  return "not initialized";
224 }
225 
227 {
228  return "RT";
229 }
230 
231 //------------------------------------------------------------------------------
232 }
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:29
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
Superclass for ViewWrappers.
Definition: cxViewWrapper.h:89
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:58
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:45
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
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()