Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxViewRepCollection.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 "cxViewRepCollection.h"
13 
14 #include <vtkImageActor.h>
15 #include <vtkImageData.h>
16 #include "vtkRenderWindow.h"
17 #include "vtkRenderer.h"
18 
19 #include "cxRep.h"
20 #include "cxTypeConversions.h"
21 
22 namespace cx
23 {
24 
25 
26 ViewRepCollection::ViewRepCollection(vtkRenderWindowPtr renderWindow, const QString& uid, const QString& name)
27 {
28  QString myuid = uid;
29  if (myuid.isEmpty())
30  {
31  myuid = QString::number(reinterpret_cast<long>(this));
32  }
33  mRenderWindow = renderWindow;
34  mBackgroundColor = QColor("black");
35  mUid = myuid;
36  mName = name;
37  mType = View::VIEW;
38 
39  this->clear();
40 }
41 
43 {
44  removeReps();
45 
46  if (mRenderer)
47  mRenderWindow->RemoveRenderer(mRenderer);
48 }
49 
51 {
52  switch (this->getType())
53  {
54  case View::VIEW:
55  return "View";
56  case View::VIEW_2D:
57  return "View2D";
58  case View::VIEW_3D:
59  return "View3D";
61  return "ViewRealTime";
62  }
63  return "";
64 }
65 
67 {
68  return mUid;
69 }
70 
72 {
73  return mName;
74 }
75 
77 {
78  return mRenderer;
79 }
80 
82 {
83  if (hasRep(rep))
84  {
85  return;
86  }
87 
88  rep->connectToView(mSelf.lock());
89  mReps.push_back(rep);
90 }
91 
93 {
94  mBackgroundColor = color;
95  if (mRenderer)
96  {
97  mRenderer->SetBackground(mBackgroundColor.redF(), mBackgroundColor.greenF(), mBackgroundColor.blueF());
98  }
99 }
100 
107 {
108  removeReps();
109 
110  if (mRenderer)
111  mRenderWindow->RemoveRenderer(mRenderer);
112 
113  mRenderer = vtkRendererPtr::New();
114  mRenderer->SetBackground(mBackgroundColor.redF(), mBackgroundColor.greenF(), mBackgroundColor.blueF());
115  mRenderWindow->AddRenderer(mRenderer);
116 }
117 
119 {
120  for (RepsIter it = mReps.begin(); it != mReps.end(); ++it)
121  {
122  (*it)->disconnectFromView(mSelf.lock());
123  }
124  mReps.clear();
125 }
126 
128 {
129  RepsIter it = std::find(mReps.begin(), mReps.end(), rep);
130 
131  if (it == mReps.end())
132  {
133  return;
134  }
135 
136  rep->disconnectFromView(mSelf.lock());
137  mReps.erase(it);
138 }
139 
140 std::vector<RepPtr> ViewRepCollection::getReps()
141 {
142  return mReps;
143 }
144 
145 bool ViewRepCollection::hasRep(const RepPtr& rep) const
146 {
147  return std::count(mReps.begin(), mReps.end(), rep);
148 }
149 
150 void ViewRepCollection::print(std::ostream& os)
151 {
152  Indent ind;
153  printSelf(os, ind);
154 }
155 
156 void ViewRepCollection::printSelf(std::ostream & os, Indent indent)
157 {
158  os << indent << "mUid: " << mUid << std::endl;
159  os << indent << "mName: " << mName << std::endl;
160  os << indent << "NumberOfReps: " << mReps.size() << std::endl;
161 
162  for (unsigned i = 0; i < mReps.size(); ++i)
163  {
164  os << indent << "<Rep child " << i << ">" << std::endl;
165  mReps[i]->printSelf(os, indent.stepDown());
166  os << indent << "</Rep child " << i << ">" << std::endl;
167  }
168 
169  if (indent.includeDetails())
170  {
171  os << indent << "<RenderWindow>" << std::endl;
172  mRenderWindow->PrintSelf(os, indent.getVtkIndent().GetNextIndent());
173  os << indent << "</RenderWindow>" << std::endl;
174  os << indent << "<Renderer>" << std::endl;
175  mRenderer->PrintSelf(os, indent.getVtkIndent().GetNextIndent());
176  os << indent << "</Renderer>" << std::endl;
177  os << indent << "<Props>" << std::endl;
178  vtkPropCollection* collection = mRenderer->GetViewProps();
179  collection->InitTraversal();
180  vtkProp* prop = collection->GetNextProp();
181  while (prop)
182  {
183  os << indent << indent << "<Prop>" << std::endl;
184  prop->PrintSelf(os, indent.getVtkIndent().GetNextIndent().GetNextIndent());
185  os << indent << indent << "</Prop>" << std::endl;
186  prop = collection->GetNextProp();
187  }
188  os << indent << "</Props>" << std::endl;
189  }
190 }
191 
193 {
194  this->getRenderer()->Modified();
195  this->getRenderWindow()->Modified();
196 }
197 
199 {
200  unsigned long hash = 0;
201 
202  hash += this->getRenderer()->GetMTime();
203  hash += this->getRenderWindow()->GetMTime();
204  vtkPropCollection* props = this->getRenderer()->GetViewProps();
205  props->InitTraversal();
206  for (vtkProp* prop = props->GetNextProp(); prop != NULL; prop = props->GetNextProp())
207  {
208  vtkImageActor* imageActor = vtkImageActor::SafeDownCast(prop);
209  if (imageActor && imageActor->GetInput())
210  {
211  hash += imageActor->GetInput()->GetMTime();
212  }
213  hash += prop->GetMTime();
214  hash += prop->GetRedrawMTime();
215  }
216 
217  return hash;
218 }
219 
220 } // namespace cx
boost::weak_ptr< class View > mSelf
virtual View::Type getType() const
virtual bool hasRep(const RepPtr &rep) const
Checks if the view already have the rep.
virtual QString getTypeString() const
virtual vtkRenderWindowPtr getRenderWindow() const
Get the vtkRenderWindow used by this View.
vtkIndent getVtkIndent() const
Definition: cxIndent.cpp:25
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
std::vector< RepPtr >::iterator RepsIter
Iterator typedef for the internal rep vector.
virtual std::vector< RepPtr > getReps()
Returns all reps in the view.
virtual void removeReps()
Removes all reps in the view.
virtual void addRep(const RepPtr &rep)
Adds and connects a rep to the view.
bool includeDetails() const
Definition: cxIndent.cpp:32
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
virtual void setBackgroundColor(QColor color)
QString mName
The view&#39;s name.
vtkRenderWindowPtr mRenderWindow
QString mUid
The view&#39;s unique id.
Formatting class for debug printing of the ssc library.
Definition: cxIndent.h:28
virtual QString getName()
Get a views name.
void print(std::ostream &os)
virtual void removeRep(const RepPtr &rep)
Removes and disconnects the rep from the view.
Indent stepDown() const
Definition: cxIndent.cpp:27
std::vector< RepPtr > mReps
Storage for internal reps.
ViewRepCollection(vtkRenderWindowPtr renderWindow, const QString &uid, const QString &name="")
constructor
virtual QString getUid()
Get a views unique id.
virtual void clear()
Removes everything in the view, inluding reps.
boost::shared_ptr< class Rep > RepPtr
Definition: cxRepManager.h:24
virtual vtkRendererPtr getRenderer() const
Get the renderer used by this View.
virtual void printSelf(std::ostream &os, Indent indent)
Namespace for all CustusX production code.