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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCheckerboardWidget.cxx
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 "vtkCheckerboardWidget.h"
#include "vtkCheckerboardRepresentation.h"
#include "vtkSliderRepresentation3D.h"
#include "vtkCommand.h"
#include "vtkCallbackCommand.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkSliderWidget.h"
vtkStandardNewMacro(vtkCheckerboardWidget);
// The checkerboard simply observes the behavior of four vtkSliderWidgets.
// Here we create the command/observer classes to respond to the
// slider widgets.
class vtkCWCallback : public vtkCommand
{
public:
static vtkCWCallback *New()
{ return new vtkCWCallback; }
void Execute(vtkObject *, unsigned long eventId, void*) VTK_OVERRIDE
{
switch (eventId)
{
case vtkCommand::StartInteractionEvent:
this->CheckerboardWidget->StartCheckerboardInteraction();
break;
case vtkCommand::InteractionEvent:
this->CheckerboardWidget->CheckerboardInteraction(this->SliderNumber);
break;
case vtkCommand::EndInteractionEvent:
this->CheckerboardWidget->EndCheckerboardInteraction();
break;
}
}
vtkCWCallback():SliderNumber(0),CheckerboardWidget(0) {}
int SliderNumber; //the number of the currently active slider
vtkCheckerboardWidget *CheckerboardWidget;
};
//----------------------------------------------------------------------
vtkCheckerboardWidget::vtkCheckerboardWidget()
{
this->TopSlider = vtkSliderWidget::New();
this->TopSlider->KeyPressActivationOff();
this->RightSlider = vtkSliderWidget::New();
this->RightSlider->KeyPressActivationOff();
this->BottomSlider = vtkSliderWidget::New();
this->BottomSlider->KeyPressActivationOff();
this->LeftSlider = vtkSliderWidget::New();
this->LeftSlider->KeyPressActivationOff();
// Set up the callbacks on the sliders
vtkCWCallback *cwCallback0 = vtkCWCallback::New();
cwCallback0->CheckerboardWidget = this;
cwCallback0->SliderNumber = vtkCheckerboardRepresentation::TopSlider;
this->TopSlider->AddObserver(vtkCommand::StartInteractionEvent, cwCallback0, this->Priority);
this->TopSlider->AddObserver(vtkCommand::InteractionEvent, cwCallback0, this->Priority);
this->TopSlider->AddObserver(vtkCommand::EndInteractionEvent, cwCallback0, this->Priority);
cwCallback0->Delete(); //okay reference counting
vtkCWCallback *cwCallback1 = vtkCWCallback::New();
cwCallback1->CheckerboardWidget = this;
cwCallback1->SliderNumber = vtkCheckerboardRepresentation::RightSlider;
this->RightSlider->AddObserver(vtkCommand::StartInteractionEvent, cwCallback1, this->Priority);
this->RightSlider->AddObserver(vtkCommand::InteractionEvent, cwCallback1, this->Priority);
this->RightSlider->AddObserver(vtkCommand::EndInteractionEvent, cwCallback1, this->Priority);
cwCallback1->Delete(); //okay reference counting
vtkCWCallback *cwCallback2 = vtkCWCallback::New();
cwCallback2->CheckerboardWidget = this;
cwCallback2->SliderNumber = vtkCheckerboardRepresentation::BottomSlider;
this->BottomSlider->AddObserver(vtkCommand::StartInteractionEvent, cwCallback2, this->Priority);
this->BottomSlider->AddObserver(vtkCommand::InteractionEvent, cwCallback2, this->Priority);
this->BottomSlider->AddObserver(vtkCommand::EndInteractionEvent, cwCallback2, this->Priority);
cwCallback2->Delete(); //okay reference counting
vtkCWCallback *cwCallback3 = vtkCWCallback::New();
cwCallback3->CheckerboardWidget = this;
cwCallback3->SliderNumber = vtkCheckerboardRepresentation::LeftSlider;
this->LeftSlider->AddObserver(vtkCommand::StartInteractionEvent, cwCallback3, this->Priority);
this->LeftSlider->AddObserver(vtkCommand::InteractionEvent, cwCallback3, this->Priority);
this->LeftSlider->AddObserver(vtkCommand::EndInteractionEvent, cwCallback3, this->Priority);
cwCallback3->Delete(); //okay reference counting
}
//----------------------------------------------------------------------
vtkCheckerboardWidget::~vtkCheckerboardWidget()
{
this->TopSlider->Delete();
this->RightSlider->Delete();
this->BottomSlider->Delete();
this->LeftSlider->Delete();
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::CreateDefaultRepresentation()
{
if ( ! this->WidgetRep )
{
this->WidgetRep = vtkCheckerboardRepresentation::New();
}
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::SetEnabled(int enabling)
{
if ( ! this->Interactor )
{
vtkErrorMacro(<<"The interactor must be set prior to enabling/disabling widget");
return;
}
if ( enabling ) //----------------
{
vtkDebugMacro(<<"Enabling checkerboard widget");
if ( this->Enabled ) //already enabled, just return
{
return;
}
if ( ! this->CurrentRenderer )
{
this->SetCurrentRenderer(
this->Interactor->FindPokedRenderer(
this->Interactor->GetLastEventPosition()[0],
this->Interactor->GetLastEventPosition()[1]));
if (this->CurrentRenderer == NULL)
{
return;
}
}
// Everything is ok, enable the representation
this->Enabled = 1;
this->CreateDefaultRepresentation();
this->WidgetRep->SetRenderer(this->CurrentRenderer);
// Configure this slider widgets
this->TopSlider->SetInteractor(this->Interactor);
this->RightSlider->SetInteractor(this->Interactor);
this->BottomSlider->SetInteractor(this->Interactor);
this->LeftSlider->SetInteractor(this->Interactor);
// Make sure there is a representation
this->WidgetRep->BuildRepresentation();
this->TopSlider->SetRepresentation(reinterpret_cast<vtkCheckerboardRepresentation*>
(this->WidgetRep)->GetTopRepresentation());
this->RightSlider->SetRepresentation(reinterpret_cast<vtkCheckerboardRepresentation*>
(this->WidgetRep)->GetRightRepresentation());
this->BottomSlider->SetRepresentation(reinterpret_cast<vtkCheckerboardRepresentation*>
(this->WidgetRep)->GetBottomRepresentation());
this->LeftSlider->SetRepresentation(reinterpret_cast<vtkCheckerboardRepresentation*>
(this->WidgetRep)->GetLeftRepresentation());
// We temporarily disable the sliders to avoid multiple renders
this->Interactor->Disable();
this->TopSlider->SetEnabled(1);
this->RightSlider->SetEnabled(1);
this->BottomSlider->SetEnabled(1);
this->LeftSlider->SetEnabled(1);
this->Interactor->Enable();
// Add the actors
this->InvokeEvent(vtkCommand::EnableEvent,NULL);
}
else //disabling------------------
{
vtkDebugMacro(<<"Disabling checkerboard widget");
if ( ! this->Enabled ) //already disabled, just return
{
return;
}
this->Enabled = 0;
// Turn off the slider widgets. Temporariliy disable
this->Interactor->Disable();
this->TopSlider->SetEnabled(0);
this->RightSlider->SetEnabled(0);
this->BottomSlider->SetEnabled(0);
this->LeftSlider->SetEnabled(0);
this->Interactor->Enable();
this->InvokeEvent(vtkCommand::DisableEvent,NULL);
this->SetCurrentRenderer(NULL);
}
this->Render();
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::StartCheckerboardInteraction()
{
this->Superclass::StartInteraction();
this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::CheckerboardInteraction(int sliderNum)
{
reinterpret_cast<vtkCheckerboardRepresentation*>(this->WidgetRep)->
SliderValueChanged(sliderNum);
this->InvokeEvent(vtkCommand::InteractionEvent,NULL);
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::EndCheckerboardInteraction()
{
this->Superclass::EndInteraction();
this->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
}
//----------------------------------------------------------------------
void vtkCheckerboardWidget::PrintSelf(ostream& os, vtkIndent indent)
{
//Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
this->Superclass::PrintSelf(os,indent);
if ( this->TopSlider )
{
os << indent << "Top Slider: " << this->TopSlider << "\n";
}
else
{
os << indent << "Top Slider: (none)\n";
}
if ( this->BottomSlider )
{
os << indent << "Bottom Slider: " << this->BottomSlider << "\n";
}
else
{
os << indent << "Bottom Slider: (none)\n";
}
if ( this->BottomSlider )
{
os << indent << "Bottom Slider: " << this->BottomSlider << "\n";
}
else
{
os << indent << "Bottom Slider: (none)\n";
}
if ( this->LeftSlider )
{
os << indent << "Left Slider: " << this->LeftSlider << "\n";
}
else
{
os << indent << "Left Slider: (none)\n";
}
}
|