Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
vtkQtConnection.cxx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // original copyright below
18 
19 /*=========================================================================
20 
21  Copyright 2004 Sandia Corporation.
22  Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
23  license for use of this work by or on behalf of the
24  U.S. Government. Redistribution and use in source and binary forms, with
25  or without modification, are permitted provided that this Notice and any
26  statement of authorship are reproduced on all copies.
27 
28 =========================================================================*/
29 
30 /*========================================================================
31  For general information about using VTK and Qt, see:
32  http://www.trolltech.com/products/3rdparty/vtksupport.html
33 =========================================================================*/
34 
35 /*========================================================================
36  !!! WARNING for those who want to contribute code to this file.
37  !!! If you use a commercial edition of Qt, you can modify this code.
38  !!! If you use an open source version of Qt, you are free to modify
39  !!! and use this code within the guidelines of the GPL license.
40  !!! Unfortunately, you cannot contribute the changes back into this
41  !!! file. Doing so creates a conflict between the GPL and BSD-like VTK
42  !!! license.
43 =========================================================================*/
44 
45 #include "vtkQtConnection.h"
46 #include "vtkEventQtSlotConnect.h"
47 #include "vtkCallbackCommand.h"
48 
49 #include <qobject.h>
50 #include <qmetaobject.h>
51 
52 // constructor
53 vtkQtConnection::vtkQtConnection(vtkEventQtSlotConnect* owner)
54  : Owner(owner)
55 {
57  this->Callback->SetCallback(vtkQtConnection::DoCallback);
58  this->Callback->SetClientData(this);
59  this->VTKObject = 0;
60  this->QtObject = 0;
61  this->ClientData = 0;
62  this->VTKEvent = vtkCommand::NoEvent;
63 }
64 
65 // destructor, disconnect if necessary
67 {
68  if(this->VTKObject)
69  {
70  this->VTKObject->RemoveObserver(this->Callback);
71  //Qt takes care of disconnecting slots
72  }
73  this->Callback->Delete();
74 }
75 
76 void vtkQtConnection::DoCallback(vtkObject* vtk_obj, unsigned long event,
77  void* client_data, void* call_data)
78 {
79  vtkQtConnection* conn = static_cast<vtkQtConnection*>(client_data);
80  conn->Execute(vtk_obj, event, call_data);
81 }
82 
83 
84 // callback from VTK to emit signal
85 void vtkQtConnection::Execute(vtkObject* caller, unsigned long e, void* call_data)
86 {
87  if(e != vtkCommand::DeleteEvent ||
88  (e == vtkCommand::DeleteEvent && this->VTKEvent == vtkCommand::DeleteEvent))
89  {
90  emit EmitExecute(caller, e, ClientData, call_data, this->Callback);
91  }
92 
93  if(e == vtkCommand::DeleteEvent)
94  {
95  this->Owner->Disconnect(this->VTKObject, this->VTKEvent, this->QtObject,
96  this->QtSlot.toUtf8().data(),
97  this->ClientData);
98  }
99 }
100 
101 bool vtkQtConnection::IsConnection(vtkObject* vtk_obj, unsigned long e,
102  const QObject* qt_obj, const char* slot, void* client_data)
103 {
104  if(this->VTKObject != vtk_obj)
105  return false;
106 
107  if(e != vtkCommand::NoEvent && e != this->VTKEvent)
108  return false;
109 
110  if(qt_obj && qt_obj != this->QtObject)
111  return false;
112 
113  if(slot && this->QtSlot != slot)
114  return false;
115 
116  if(client_data && this->ClientData != client_data)
117  return false;
118 
119  return true;
120 }
121 
122 // set the connection
124  vtkObject* vtk_obj, unsigned long e,
125  const QObject* qt_obj, const char* slot,
126  void* client_data, float priority
127  , Qt::ConnectionType type)
128 {
129  // keep track of what we connected
130  this->VTKObject = vtk_obj;
131  this->QtObject = qt_obj;
132  this->VTKEvent = e;
133  this->ClientData = client_data;
134  this->QtSlot = slot;
135 
136  // make a connection between this and the vtk object
137  vtk_obj->AddObserver(e, this->Callback, priority);
138 
139  if(e != vtkCommand::DeleteEvent)
140  {
141  vtk_obj->AddObserver(vtkCommand::DeleteEvent, this->Callback);
142  }
143 
144  // make a connection between this and the Qt object
145  qt_obj->connect(
146  this, SIGNAL(EmitExecute(vtkObject*,unsigned long,void*,void*,vtkCommand*)),
147  slot
148  ,type);
149  QObject::connect(qt_obj, SIGNAL(destroyed(QObject*)), this,
150  SLOT(deleteConnection()));
151 }
152 
154 {
155  this->Owner->RemoveConnection(this);
156 }
157 
158 void vtkQtConnection::PrintSelf(ostream& os, vtkIndent indent)
159 {
160  if(this->VTKObject && this->QtObject)
161  {
162  os << indent <<
163  this->VTKObject->GetClassName() << ":" <<
164  vtkCommand::GetStringFromEventId(this->VTKEvent) << " <----> " <<
165  this->QtObject->metaObject()->className() << "::" <<
166  this->QtSlot.toUtf8().data() << "\n";
167  }
168 }
vtkCallbackCommand * Callback
static void DoCallback(vtkObject *vtk_obj, unsigned long event, void *client_data, void *call_data)
vtkQtConnection(vtkEventQtSlotConnect *owner)
void SetConnection(vtkObject *vtk_obj, unsigned long event, const QObject *qt_obj, const char *slot, void *client_data, float priority=0.0, Qt::ConnectionType type=Qt::AutoConnection)
void PrintSelf(ostream &os, vtkIndent indent)
unsigned long VTKEvent
const QObject * QtObject
void EmitExecute(vtkObject *, unsigned long, void *client_data, void *call_data, vtkCommand *)
vtkEventQtSlotConnect * Owner
void Execute(vtkObject *caller, unsigned long event, void *client_data)
bool IsConnection(vtkObject *vtk_obj, unsigned long event, const QObject *qt_obj, const char *slot, void *client_data)
vtkObject * VTKObject
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.