forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkAbstractWidget.cxx
360 lines (309 loc) · 10.1 KB
/
vtkAbstractWidget.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAbstractWidget.cxx,v
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkAbstractWidget.h"
#include "vtkWidgetRepresentation.h"
#include "vtkCommand.h"
#include "vtkCallbackCommand.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkWidgetEventTranslator.h"
#include "vtkWidgetCallbackMapper.h"
#include "vtkRenderer.h"
#include "vtkEvent.h"
#include "vtkWidgetEvent.h"
//----------------------------------------------------------------------
vtkAbstractWidget::vtkAbstractWidget()
{
// Setup event processing
this->EventCallbackCommand->SetCallback(
vtkAbstractWidget::ProcessEventsHandler);
// There is no parent to this widget currently
this->Parent = nullptr;
// Set up the geometry
this->WidgetRep = nullptr;
// Set priority higher than interactor styles
this->Priority = 0.5;
// Does this widget manage a cursor?
this->ManagesCursor = 1;
// Does this widget respond to interaction?
this->ProcessEvents = 1;
// Okay, set up the event translations for the subclasses.
this->EventTranslator = vtkWidgetEventTranslator::New();
this->CallbackMapper = vtkWidgetCallbackMapper::New();
this->CallbackMapper->SetEventTranslator(this->EventTranslator);
}
//----------------------------------------------------------------------
vtkAbstractWidget::~vtkAbstractWidget()
{
if ( this->WidgetRep )
{
// Remove the representation from the renderer.
vtkRenderer* ren = this->WidgetRep->GetRenderer();
if (ren)
{
ren->RemoveViewProp(this->WidgetRep);
}
this->WidgetRep->Delete();
this->WidgetRep = nullptr;
}
this->EventTranslator->Delete();
this->CallbackMapper->Delete();
this->SetEnabled(0);
}
//----------------------------------------------------------------------
void vtkAbstractWidget::SetWidgetRepresentation(vtkWidgetRepresentation *r)
{
if ( r != this->WidgetRep )
{
int enabled=0;
if ( this->Enabled )
{
enabled = 1;
this->SetEnabled(0);
}
if ( this->WidgetRep )
{
this->WidgetRep->Delete();
}
this->WidgetRep = r;
if ( this->WidgetRep )
{
this->WidgetRep->Register(this);
}
this->Modified();
if ( enabled )
{
this->SetEnabled(1);
}
}
}
//----------------------------------------------------------------------
void vtkAbstractWidget::SetEnabled(int enabling)
{
if ( enabling ) //----------------
{
vtkDebugMacro(<<"Enabling widget");
if ( this->Enabled ) //already enabled, just return
{
return;
}
if ( ! this->Interactor )
{
vtkErrorMacro(<<"The interactor must be set prior to enabling the widget");
return;
}
int X=this->Interactor->GetEventPosition()[0];
int Y=this->Interactor->GetEventPosition()[1];
if ( ! this->CurrentRenderer )
{
this->SetCurrentRenderer(this->Interactor->FindPokedRenderer(X,Y));
if (this->CurrentRenderer == nullptr)
{
return;
}
}
// We're ready to enable
this->Enabled = 1;
if (! this->WidgetRep )
{
this->CreateDefaultRepresentation();
}
this->WidgetRep->SetRenderer(this->CurrentRenderer);
this->WidgetRep->RegisterPickers();
// listen for the events found in the EventTranslator
if ( ! this->Parent )
{
this->EventTranslator->AddEventsToInteractor(this->Interactor,
this->EventCallbackCommand,this->Priority);
}
else
{
this->EventTranslator->AddEventsToParent(this->Parent,
this->EventCallbackCommand,this->Priority);
}
if ( this->ManagesCursor )
{
this->WidgetRep->ComputeInteractionState(X, Y);
this->SetCursor(this->WidgetRep->GetInteractionState());
}
this->WidgetRep->BuildRepresentation();
this->CurrentRenderer->AddViewProp(this->WidgetRep);
this->InvokeEvent(vtkCommand::EnableEvent,nullptr);
}
else //disabling------------------
{
vtkDebugMacro(<<"Disabling widget");
if ( ! this->Enabled ) //already disabled, just return
{
return;
}
this->Enabled = 0;
// don't listen for events any more
if ( ! this->Parent )
{
this->Interactor->RemoveObserver(this->EventCallbackCommand);
}
else
{
this->Parent->RemoveObserver(this->EventCallbackCommand);
}
if (this->CurrentRenderer)
{
this->CurrentRenderer->RemoveViewProp(this->WidgetRep);
}
this->InvokeEvent(vtkCommand::DisableEvent,nullptr);
this->SetCurrentRenderer(nullptr);
if (this->WidgetRep)
{
this->WidgetRep->UnRegisterPickers();
}
}
// We no longer call render when enabled state changes. It's the applications
// resposibility to explicitly call render after changing enable state.
//// Should only render if there is no parent
//if ( this->Interactor && !this->Parent )
// {
// this->Interactor->Render();
// }
}
//-------------------------------------------------------------------------
void vtkAbstractWidget::ProcessEventsHandler(vtkObject* vtkNotUsed(object),
unsigned long vtkEvent,
void* clientdata,
void* calldata)
{
vtkAbstractWidget* self =
reinterpret_cast<vtkAbstractWidget *>( clientdata );
// if ProcessEvents is Off, we ignore all interaction events.
if (!self->GetProcessEvents())
{
return;
}
// if the event has data then get the translation using the
// event data
unsigned long widgetEvent = vtkWidgetEvent::NoEvent;
if (calldata && vtkCommand::EventHasData(vtkEvent))
{
widgetEvent = self->EventTranslator->GetTranslation(vtkEvent,
static_cast<vtkEventData *>(calldata));
}
else
{
int modifier = vtkEvent::GetModifier(self->Interactor);
// If neither the ctrl nor the shift keys are pressed, give
// NoModifier a preference over AnyModifer.
if (modifier == vtkEvent::AnyModifier)
{
widgetEvent = self->EventTranslator->GetTranslation(vtkEvent,
vtkEvent::NoModifier,
self->Interactor->GetKeyCode(),
self->Interactor->GetRepeatCount(),
self->Interactor->GetKeySym());
}
if ( widgetEvent == vtkWidgetEvent::NoEvent)
{
widgetEvent = self->EventTranslator->GetTranslation(vtkEvent,
modifier,
self->Interactor->GetKeyCode(),
self->Interactor->GetRepeatCount(),
self->Interactor->GetKeySym());
}
}
// Save the call data for widgets if needed
self->CallData = calldata;
// Invoke the widget callback
if ( widgetEvent != vtkWidgetEvent::NoEvent)
{
self->CallbackMapper->InvokeCallback(widgetEvent);
}
}
//----------------------------------------------------------------------
void vtkAbstractWidget::Render()
{
if ( ! this->Parent && this->Interactor )
{
this->Interactor->Render();
}
}
//----------------------------------------------------------------------
void vtkAbstractWidget::SetPriority( float f )
{
if (f != this->Priority)
{
this->Superclass::SetPriority(f);
// We are going to re-add all the events to the interactor. The
// interactor observer maintains a sorted list of command-observers, sorted
// by prioirty. The sorting happens only during insertion of a command-
// observer into the list. Yeah.. Look at the documentation of SetPriority
// in vtkInteractorObserver. That documentation recommends setting the
// interactor to nullptr and back again. We won't do that because it will
// cause two unnecessary re-renders, (cause we'd have had to enable and
// disable the widgets).
if (this->Enabled)
{
if (this->Interactor)
{
this->Interactor->RemoveObserver(this->CharObserverTag);
this->Interactor->RemoveObserver(this->DeleteObserverTag);
this->CharObserverTag = this->Interactor->AddObserver(
vtkCommand::CharEvent,
this->KeyPressCallbackCommand,
this->Priority);
this->DeleteObserverTag = this->Interactor->AddObserver(
vtkCommand::DeleteEvent,
this->KeyPressCallbackCommand,
this->Priority);
}
if ( !this->Parent )
{
if(this->Interactor)
{
this->Interactor->RemoveObserver(this->EventCallbackCommand);
}
}
else
{
this->Parent->RemoveObserver(this->EventCallbackCommand);
}
if ( ! this->Parent )
{
if(this->Interactor)
{
this->EventTranslator->AddEventsToInteractor(this->Interactor,
this->EventCallbackCommand,this->Priority);
}
}
else
{
this->EventTranslator->AddEventsToParent(this->Parent,
this->EventCallbackCommand,this->Priority);
}
}
}
}
//----------------------------------------------------------------------
void vtkAbstractWidget::PrintSelf(ostream& os, vtkIndent indent)
{
//Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
this->Superclass::PrintSelf(os,indent);
os << indent << "ProcessEvents: "
<< (this->ProcessEvents? "On" : "Off") << "\n";
if ( this->WidgetRep )
{
os << indent << "Widget Representation: " << this->WidgetRep << "\n";
}
else
{
os << indent << "Widget Representation: (none)\n";
}
os << indent << "Manages Cursor: "
<< (this->ManagesCursor? "On" : "Off") << "\n";
os << indent << "Parent: " << this->Parent << "\n";
}