GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoSynchronizedView3DCallbacks.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 "QGoImageView3D.h"
37 #include "QGoSynchronizedView3D.h"
38 #include "vtkCommand.h"
39 #include "vtkCallbackCommand.h"
40 #include "vtkCamera.h"
41 #include "vtkRenderer.h"
42 #include "vtkSmartPointer.h"
43 #include <algorithm>
44 
45 //--------------------------------------------------------------------------
47  ioOpenSynchronizedViews,
48  QObject *iParent) :
49  QObject (iParent)
50 {
51  m_vtkCallBackCamSync.resize(4);
52 
53  // create the callback object
54  SetupCallBack();
55 
56  // for every opened SynchronizedView :
57  std::vector< QGoSynchronizedView3D * >::iterator
58  SynchronizedViewIt = ioOpenSynchronizedViews.begin();
59 
60  while ( SynchronizedViewIt != ioOpenSynchronizedViews.end() )
61  {
62  m_openSynchronizedView.push_back(*SynchronizedViewIt);
63 
64  if ( ( *SynchronizedViewIt )->HasViewer() )
65  // add the callback to the SynchronizedView's camera
66  {
67  for ( int i = 0; i < 4; i++ )
68  {
69  ( *SynchronizedViewIt )->GetCamera(i)
70  ->AddObserver(vtkCommand::ModifiedEvent,
73  }
74 
75  // we connect the sliders to the synchronizer (monitor slider changes) :
76  QObject::connect( ( *SynchronizedViewIt )->GetImageView(),
77  SIGNAL( SliceViewXYChanged(int) ),
78  this,
79  SIGNAL( SliceViewXYChanged(int) ) );
80 
81  QObject::connect( ( *SynchronizedViewIt )->GetImageView(),
82  SIGNAL( SliceViewXZChanged(int) ),
83  this,
84  SIGNAL( SliceViewXZChanged(int) ) );
85 
86  QObject::connect( ( *SynchronizedViewIt )->GetImageView(),
87  SIGNAL( SliceViewYZChanged(int) ),
88  this,
89  SIGNAL( SliceViewYZChanged(int) ) );
90 
91  // we connect the synchronizer to the SynchronizedView
92  QObject::connect( this,
93  SIGNAL( SliceViewXYChanged(int) ),
94  ( *SynchronizedViewIt )->GetImageView(),
95  SLOT( SetSliceViewXY(int) ) );
96 
97  QObject::connect( this,
98  SIGNAL( SliceViewXZChanged(int) ),
99  ( *SynchronizedViewIt )->GetImageView(),
100  SLOT( SetSliceViewXZ(int) ) );
101 
102  QObject::connect( this,
103  SIGNAL( SliceViewYZChanged(int) ),
104  ( *SynchronizedViewIt )->GetImageView(),
105  SLOT( SetSliceViewYZ(int) ) );
106  }
107  ++SynchronizedViewIt;
108  }
109 }
110 
111 //--------------------------------------------------------------------------
112 // the destructor is very important here, we want to leave clean
113 // SynchronizedViews behind
116 {
117  std::vector< QGoSynchronizedView3D * >::iterator SynchronizedViewIt;
118 
119  // we remove the open synchronized SynchronizedViews
120  while ( !m_openSynchronizedView.empty() )
121  {
122  // remove (AND NOT DELETE, this is the Orchestra's business)
123  // all pointers in the vector
124 
125  // We remove the observer if any
126  if ( m_openSynchronizedView.back()->HasViewer() )
127  {
128  // remove the callback object from each object's camera
129  for ( int i = 0; i < 4; i++ )
130  {
131  m_openSynchronizedView.back()->GetCamera(i)
132  ->RemoveObserver(QGoSynchronizedView3DCallbacks::
134  }
135  }
136  m_openSynchronizedView.pop_back();
137  }
138 
139  // we can now delete the callbacks !
140  for ( int i = 0; i < 4; i++ )
141  {
142  m_vtkCallBackCamSync[i]->Delete();
143  }
144 }
145 
146 //--------------------------------------------------------------------------
147 // this is the callback function : do deep copies to keep track of
148 // the moving camera's position
149 void
151  long unsigned int eventId,
152  void *clientData,
153  void *callData)
154 {
155  synchronizeCamera(0, caller, eventId, clientData, callData);
156 }
157 
158 //--------------------------------------------------------------------------
159 // this is the callback function : do deep copies to keep track of
160 // master's camera position
161 //
162 void
164  long unsigned int eventId,
165  void *clientData,
166  void *callData)
167 {
168  synchronizeCamera(1, caller, eventId, clientData, callData);
169 }
170 
171 //--------------------------------------------------------------------------
172 // this is the callback function : do deep copies to keep track of
173 // master's camera position
174 void
176  long unsigned int eventId,
177  void *clientData,
178  void *callData)
179 {
180  synchronizeCamera(2, caller, eventId, clientData, callData);
181 }
182 
183 //--------------------------------------------------------------------------
184 // this is the callback function : do deep copies to keep track of
185 // master's camera position
186 void
188  long unsigned int eventId,
189  void *clientData,
190  void *callData)
191 {
192  synchronizeCamera(3, caller, eventId, clientData, callData);
193 }
194 
195 //--------------------------------------------------------------------------
196 void
198  vtkObject *caller,
199  long unsigned int eventId,
200  void *clientData,
201  void *callData)
202 {
203  (void)eventId;
204  (void)callData;
205 
206  // client data is a pointer to std::vector<QGoImageView2D*>
207  // so client data is a std::vector<QGoSynchronizedView3D*>*
208  // we get the p_m_QGoSynchronizedView3D array by the following cast :
209  std::vector< QGoSynchronizedView3D * > p_m_QGoSynchronizedView3Ds =
210  *static_cast< std::vector< QGoSynchronizedView3D * > * >( clientData );
211  // the observer are set on cameras, so that the caller is a vtk camera*
212  vtkCamera *movedCamera =
213  static_cast< vtkCamera * >( caller );
214  // for every opened SynchronizedView :
215  std::vector< QGoSynchronizedView3D * >::iterator SynchronizedViewIt =
216  p_m_QGoSynchronizedView3Ds.begin();
217 
218  while ( SynchronizedViewIt != p_m_QGoSynchronizedView3Ds.end() )
219  {
220  /*
221  * To inspect : if we don't move even the non visible views,
222  * the positionning is not correct when going back to quadview
223  * solution => a reset camera taking reference at the last fullscreenview ?
224  */
225  /*
226  // if the SynchronizedView is visible and the modified camera is rendrered
227  if ((*SynchronizedViewIt)->isVisible()
228  && (((*SynchronizedViewIt)->GetFullScreenView() == 0)
229  || ((*SynchronizedViewIt)->GetFullScreenView() == 1)))
230  */
231  {
232  // we copy the position of the moved camera
233  // into each SynchronizedView's camera
234  if ( ( ( *SynchronizedViewIt )->GetCamera(iCamera) )
235  && ( ( *SynchronizedViewIt )->GetCamera(iCamera) != movedCamera ) )
236  {
237  ( *SynchronizedViewIt )->GetCamera(iCamera)->DeepCopy(movedCamera);
238  // we render all SynchronizedViews
239  ( *SynchronizedViewIt )->Render(iCamera);
240  }
241  }
242  ++SynchronizedViewIt;
243  }
244 }
245 
246 //--------------------------------------------------------------------------
247 void
249 {
250  // create the callback object (connection event -> function )
251  for ( int i = 0; i < 4; i++ )
252  {
253  m_vtkCallBackCamSync[i] = vtkCallbackCommand::New();
254  m_vtkCallBackCamSync[i]->SetClientData(&m_openSynchronizedView);
255  }
256 
265 }
266 
267 //--------------------------------------------------------------------------
268 void
270 {
271  if ( ioSynchronizedView ) // this should always be true
272  {
273  // We look for the SynchronizedView
274  // in the vector of synchronized SynchronizedViews
275  std::vector< QGoSynchronizedView3D * >::iterator
276  SynchronizedViewIt = std::find(m_openSynchronizedView.begin(),
278  ioSynchronizedView);
279  if ( SynchronizedViewIt != m_openSynchronizedView.end() ) // if we found it
280  {
281  // remove the callback object from each object's camera
282  for ( int i = 0; i < 4; i++ )
283  {
284  ioSynchronizedView->GetCamera(i)
285  ->RemoveObserver(QGoSynchronizedView3DCallbacks::
287  }
288 
289  // we remove the SynchronizedView
290  m_openSynchronizedView.erase(SynchronizedViewIt);
291  }
292  }
293 }
294 
295 //--------------------------------------------------------------------------
296 void
298 {
299  if ( ioSynchronizedView ) // this should always be true
300  {
301  if ( ioSynchronizedView->HasViewer() )
302  // add the callback to the SynchronizedView's camera
303  {
304  m_openSynchronizedView.push_back(ioSynchronizedView);
305 
306  for ( int i = 0; i < 4; i++ )
307  {
308  ioSynchronizedView->GetCamera(i)
309  ->AddObserver(vtkCommand::ModifiedEvent,
312  }
313 
314  // we connect the sliders to the synchronizer (monitor slider changes) :
315  QObject::connect( ioSynchronizedView->GetImageView(),
316  SIGNAL( SliceViewXYChanged(int) ),
317  this,
318  SIGNAL( SliceViewXYChanged(int) ) );
319  QObject::connect( ioSynchronizedView->GetImageView(),
320  SIGNAL( SliceViewXZChanged(int) ),
321  this,
322  SIGNAL( SliceViewXZChanged(int) ) );
323  QObject::connect( ioSynchronizedView->GetImageView(),
324  SIGNAL( SliceViewYZChanged(int) ),
325  this,
326  SIGNAL( SliceViewYZChanged(int) ) );
327 
328  // we connect the synchronizer to the SynchronizedView
329  QObject::connect( this, SIGNAL( SliceViewXYChanged(int) ),
330  ioSynchronizedView->GetImageView(),
331  SLOT( SetSliceViewXY(int) ) );
332  QObject::connect( this, SIGNAL( SliceViewXZChanged(int) ),
333  ioSynchronizedView->GetImageView(),
334  SLOT( SetSliceViewXZ(int) ) );
335  QObject::connect( this, SIGNAL( SliceViewYZChanged(int) ),
336  ioSynchronizedView->GetImageView(),
337  SLOT( SetSliceViewYZ(int) ) );
338  }
339  else
340  {
341  std::cerr << "trying to synchronize a visualization object missing a QGoImageView"
342  << std::endl;
343  }
344  }
345 }
bool HasViewer(void)
true if the widget has a viewer
void SetupCallBack()
setup the callback command object (client data, called function...etc)
QGoImageView3D * GetImageView()
static void synchronizeCamera(int iCamera, vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
void removeSynchronizedView(QGoSynchronizedView3D *ioSynchronizedView)
remove a QGoSynchronizedView3D from the vector of synchronized Managers (this method takes care of re...
static void synchronizeCameras3(vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
callback function to synchornize cameras (has to be public)
QGoSynchronizedView3DCallbacks(std::vector< QGoSynchronizedView3D * > ioOpenSynchronizedViews, QObject *iParent=0)
the constructor do most of the work : add observers &amp; callbacks to QGoSynchronizedViews of the vector...
std::vector< QGoSynchronizedView3D * > m_openSynchronizedView
class used to display a QWidget containing a two dimensional vtkimagedata* or itkimage*. QGoSynchronizedView3D provide the interface to synchronize cameras among several GoSynchronizedView3D.
static void synchronizeCameras0(vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
callback function to synchornize cameras (has to be public)
static void synchronizeCameras1(vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
callback function to synchornize cameras (has to be public)
This object takes a list of QGoSynchronizedView and synchronize their cameras by setting up callbacks...
void SliceViewXZChanged(int oSlice)
signals for synchronizing sliders (gives current XZ slice of any changing QGoSynchronizedView) ...
std::vector< vtkCallbackCommand * > m_vtkCallBackCamSync
void addSynchronizedView(QGoSynchronizedView3D *ioSynchronizedView)
add a QGoSynchronizedView3D to the vector of synchronized Managers (this method takes care of adding ...
void SliceViewYZChanged(int oSlice)
signals for synchronizing sliders (gives current YZ slice of any changing QGoSynchronizedView) ...
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void SliceViewXYChanged(int oSlice)
signals for synchronizing sliders (gives current XYslice of any changing QGoSynchronizedView) ...
vtkCamera * GetCamera(void)
get the camera of the current fullscreen view
static void synchronizeCameras2(vtkObject *caller, long unsigned int eventId, void *clientData, void *callData)
callback function to synchornize cameras (has to be public)