GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoSynchronizedView2DCallbacks.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  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  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
35 
36 #include "QGoSynchronizedView.h"
37 #include "vtkCommand.h"
38 #include "vtkCallbackCommand.h"
39 #include "vtkCamera.h"
40 #include "vtkRenderer.h"
41 #include "vtkSmartPointer.h"
42 #include <algorithm>
43 
45  std::vector< QGoSynchronizedView2D * > iOpenSynchronizedViews,
46  QObject *iParent) : QObject(iParent), m_vtkCallBackCamSync( NULL )
47 {
48  Initialize( iOpenSynchronizedViews.begin(), iOpenSynchronizedViews.end() );
49 }
50 
51 //--------------------------------------------------------------------------
52 // the destructor is very important here, we want to leave clean
53 // SynchronizedViews behind
56 {
57  // we pop out the element of the vector until it is empty
58  while ( !m_openSynchronizedView.empty() )
59  {
60  // remove (AND NOT DELETE, this is the Manager's business)
61  // all pointers in the vector
62 
63  // We remove the observer if any
64  if ( m_openSynchronizedView.back()->HasViewer() )
65  {
66  // remove the callback object from each object's camera
67  m_openSynchronizedView.back()->GetCamera()->RemoveObserver(
70  }
71  // we remove the SynchronizedView from the vector
72  m_openSynchronizedView.pop_back();
73  }
74 
75  // we can now delete the callback !
76  m_vtkCallBackCamSync->Delete();
77 }
78 
79 //--------------------------------------------------------------------------
80 // this is the callback function : do deep copies to keep track of
81 // master's camera position
82 void
84  long unsigned int eventId,
85  void *clientData,
86  void *callData)
87 {
88  (void)eventId;
89  (void)callData;
90 
91  // client data is a pointer to std::vector<QGoImageView2D*>
92  // so client data is a std::vector<QGoSynchronizedView2D*>*
93  // we get the p_m_QGoSynchronizedView2D array by the following cast :
94  std::vector< QGoSynchronizedView2D * > p_m_QGoSynchronizedViews =
95  *static_cast< std::vector< QGoSynchronizedView2D * > * >
96  ( clientData );
97 
98  // the observer are set on cameras, so that the caller is a vtk camera*
99  vtkCamera *movedCamera =
100  static_cast< vtkCamera * >( caller );
101 
102  // for every opened SynchronizedView :
103  std::vector< QGoSynchronizedView2D * >::iterator SynchronizedViewIt =
104  p_m_QGoSynchronizedViews.begin();
105  while ( SynchronizedViewIt != p_m_QGoSynchronizedViews.end() )
106  {
107  // we copy the position of the moved camera into
108  // each SynchronizedView's camera
109  if ( ( ( *SynchronizedViewIt )->GetCamera() != NULL )
110  && ( ( *SynchronizedViewIt )->GetCamera() != movedCamera ) )
111  {
112  ( *SynchronizedViewIt )->GetCamera()->DeepCopy(movedCamera);
113  // we render all SynchronizedViews
114  ( *SynchronizedViewIt )->Render();
115  }
116  ++SynchronizedViewIt;
117  }
118 }
119 
120 //--------------------------------------------------------------------------
121 void
123 {
124  // create the callback object (connection event -> function )
125  m_vtkCallBackCamSync = vtkCallbackCommand::New();
129 }
130 
131 //--------------------------------------------------------------------------
132 void
134 {
135  if ( ioSynchronizedView ) // this should always be true
136  {
137  // We look for the SynchronizedView in the vector
138  // of synchronized SynchronizedViews
139  std::vector< QGoSynchronizedView2D * >::iterator
140  SynchronizedViewIt = std::find(m_openSynchronizedView.begin(),
142  ioSynchronizedView);
143 
144  if ( SynchronizedViewIt != m_openSynchronizedView.end() ) // if we found it
145  {
146  if ( ioSynchronizedView->HasViewer() )
147  {
148  // remove the callback object from each object's camera
149  ioSynchronizedView->GetCamera()
150  ->RemoveObserver(QGoSynchronizedView2DCallbacks::
152  }
153  ( *SynchronizedViewIt ) = NULL;
154  // we remove the SynchronizedView
155  m_openSynchronizedView.erase(SynchronizedViewIt);
156  }
157  }
158 }
159 
160 //--------------------------------------------------------------------------
161 void
163 {
164  if ( ioSynchronizedView ) // this should always be true
165  {
166  m_openSynchronizedView.push_back(ioSynchronizedView);
167  // if this SynchronizedView has a viewer, we add an observer
168  if ( ioSynchronizedView->HasViewer() )
169  {
170  // add the callback to the SynchronizedView's camera
171  ioSynchronizedView->GetCamera()->AddObserver(
172  vtkCommand::ModifiedEvent,
174  }
175  else
176  {
177  std::cerr << "trying to synchronize a visualization object missing a QGoImageView"
178  << std::endl;
179  }
180  }
181 }
vtkCallbackCommand * m_vtkCallBackCamSync
callback object to link callback function to SynchronizedView's event
class used to display a QWidget containing a two dimensional a vtkimagedata* or an itkimage*...
This object takes a list of QGoSynchronizedView and synchronize their cameras by setting up callbacks...
void removeSynchronizedView(QGoSynchronizedView2D *ioSynchronizedView)
remove a SynchronizedView from the vector of synchronized SynchronizedViews (this method takes care o...
static void synchronizeCameras(vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
callback function to synchornize cameras (has to be public)
void SetupCallBack()
setup the callback command object (client data, called function...etc)
QGoSynchronizedView2DCallbacks(std::vector< QGoSynchronizedView2D * > iOpenSynchronizedViews, QObject *iParent=0)
the constructor do most of the work : add observers & callbacks to QGoSynchronizedViews of the vector...
bool HasViewer(void)
true if the widget has a viewer
void Initialize(TIterator iBegin, TIterator iEnd)
vtkCamera * GetCamera(void)
get the camera of the current viewer
void addSynchronizedView(QGoSynchronizedView2D *ioSynchronizedView)
add a SynchronizedView to the vector of synchronized SynchronizedViews (this method takes care of add...
std::vector< QGoSynchronizedView2D * > m_openSynchronizedView
all open SynchronizedViews are stored in this array, to transmit it to the callback function ...