Fraxinus  17.12-rc4
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) 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 "cxViewRepCollection.h"
34 
35 #include <vtkImageActor.h>
36 #include <vtkImageData.h>
37 #include "vtkRenderWindow.h"
38 #include "vtkRenderer.h"
39 
40 #include "cxRep.h"
41 #include "cxTypeConversions.h"
42 
43 namespace cx
44 {
45 
46 
47 ViewRepCollection::ViewRepCollection(vtkRenderWindowPtr renderWindow, const QString& uid, const QString& name)
48 {
49  QString myuid = uid;
50  if (myuid.isEmpty())
51  {
52  myuid = QString::number(reinterpret_cast<long>(this));
53  }
54  mRenderWindow = renderWindow;
55  mBackgroundColor = QColor("black");
56  mUid = myuid;
57  mName = name;
58  mType = View::VIEW;
59 
60  this->clear();
61 }
62 
64 {
65  removeReps();
66 
67  if (mRenderer)
68  mRenderWindow->RemoveRenderer(mRenderer);
69 }
70 
72 {
73  switch (this->getType())
74  {
75  case View::VIEW:
76  return "View";
77  case View::VIEW_2D:
78  return "View2D";
79  case View::VIEW_3D:
80  return "View3D";
82  return "ViewRealTime";
83  }
84  return "";
85 }
86 
88 {
89  return mUid;
90 }
91 
93 {
94  return mName;
95 }
96 
98 {
99  return mRenderer;
100 }
101 
103 {
104  if (hasRep(rep))
105  {
106  return;
107  }
108 
109  rep->connectToView(mSelf.lock());
110  mReps.push_back(rep);
111 }
112 
114 {
115  mBackgroundColor = color;
116  if (mRenderer)
117  {
118  mRenderer->SetBackground(mBackgroundColor.redF(), mBackgroundColor.greenF(), mBackgroundColor.blueF());
119  }
120 }
121 
128 {
129  removeReps();
130 
131  if (mRenderer)
132  mRenderWindow->RemoveRenderer(mRenderer);
133 
134  mRenderer = vtkRendererPtr::New();
135  mRenderer->SetBackground(mBackgroundColor.redF(), mBackgroundColor.greenF(), mBackgroundColor.blueF());
136  mRenderWindow->AddRenderer(mRenderer);
137 }
138 
140 {
141  for (RepsIter it = mReps.begin(); it != mReps.end(); ++it)
142  {
143  (*it)->disconnectFromView(mSelf.lock());
144  }
145  mReps.clear();
146 }
147 
149 {
150  RepsIter it = std::find(mReps.begin(), mReps.end(), rep);
151 
152  if (it == mReps.end())
153  {
154  return;
155  }
156 
157  rep->disconnectFromView(mSelf.lock());
158  mReps.erase(it);
159 }
160 
161 std::vector<RepPtr> ViewRepCollection::getReps()
162 {
163  return mReps;
164 }
165 
166 bool ViewRepCollection::hasRep(const RepPtr& rep) const
167 {
168  return std::count(mReps.begin(), mReps.end(), rep);
169 }
170 
171 void ViewRepCollection::print(std::ostream& os)
172 {
173  Indent ind;
174  printSelf(os, ind);
175 }
176 
177 void ViewRepCollection::printSelf(std::ostream & os, Indent indent)
178 {
179  os << indent << "mUid: " << mUid << std::endl;
180  os << indent << "mName: " << mName << std::endl;
181  os << indent << "NumberOfReps: " << mReps.size() << std::endl;
182 
183  for (unsigned i = 0; i < mReps.size(); ++i)
184  {
185  os << indent << "<Rep child " << i << ">" << std::endl;
186  mReps[i]->printSelf(os, indent.stepDown());
187  os << indent << "</Rep child " << i << ">" << std::endl;
188  }
189 
190  if (indent.includeDetails())
191  {
192  os << indent << "<RenderWindow>" << std::endl;
193  mRenderWindow->PrintSelf(os, indent.getVtkIndent().GetNextIndent());
194  os << indent << "</RenderWindow>" << std::endl;
195  os << indent << "<Renderer>" << std::endl;
196  mRenderer->PrintSelf(os, indent.getVtkIndent().GetNextIndent());
197  os << indent << "</Renderer>" << std::endl;
198  os << indent << "<Props>" << std::endl;
199  vtkPropCollection* collection = mRenderer->GetViewProps();
200  collection->InitTraversal();
201  vtkProp* prop = collection->GetNextProp();
202  while (prop)
203  {
204  os << indent << indent << "<Prop>" << std::endl;
205  prop->PrintSelf(os, indent.getVtkIndent().GetNextIndent().GetNextIndent());
206  os << indent << indent << "</Prop>" << std::endl;
207  prop = collection->GetNextProp();
208  }
209  os << indent << "</Props>" << std::endl;
210  }
211 }
212 
214 {
215  this->getRenderer()->Modified();
216  this->getRenderWindow()->Modified();
217 }
218 
220 {
221  unsigned long hash = 0;
222 
223  hash += this->getRenderer()->GetMTime();
224  hash += this->getRenderWindow()->GetMTime();
225  vtkPropCollection* props = this->getRenderer()->GetViewProps();
226  props->InitTraversal();
227  for (vtkProp* prop = props->GetNextProp(); prop != NULL; prop = props->GetNextProp())
228  {
229  vtkImageActor* imageActor = vtkImageActor::SafeDownCast(prop);
230  if (imageActor && imageActor->GetInput())
231  {
232  hash += imageActor->GetInput()->GetMTime();
233  }
234  hash += prop->GetMTime();
235  hash += prop->GetRedrawMTime();
236  }
237 
238  return hash;
239 }
240 
241 } // 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:46
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:53
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:49
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:48
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:45
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.