SphereWidgetEvents
vtk-examples/Cxx/Widgets/SphereWidgetEvents
Description¶
This example shows how to subclass a widget so that events can be further customized.
Contributed by: Alex Malyushytskyy.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
SphereWidgetEvents.cxx
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereWidget.h>
#include <iostream>
#include <string>
namespace {
class MySphereWidget : public vtkSphereWidget
{
public:
static MySphereWidget* New();
vtkTypeMacro(MySphereWidget, vtkSphereWidget);
// Handles the events
static void ProcessEvents(vtkObject* object, unsigned long event,
void* clientdata, void* calldata);
protected:
MySphereWidget();
// Create a new callback command rather than using the one defined in
// vtkSphereWidget. This prevents problems with unexpected behavior due
// to the AbortFlag being manipulated.
vtkSmartPointer<vtkCallbackCommand> MyEventCallbackCommand;
};
MySphereWidget::MySphereWidget()
{
this->MyEventCallbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
// Connect our own callback command to our own ProcessEvents function.
// This way we do not have to deal with side effects of
// SphereWidget::ProcessEvents.
this->MyEventCallbackCommand->SetCallback(MySphereWidget::ProcessEvents);
// Connect our callback function to a few events.
this->AddObserver(vtkCommand::StartInteractionEvent,
this->MyEventCallbackCommand);
this->AddObserver(vtkCommand::InteractionEvent, this->MyEventCallbackCommand);
this->AddObserver(vtkCommand::EndInteractionEvent,
this->MyEventCallbackCommand);
}
void MySphereWidget::ProcessEvents(vtkObject* vtkNotUsed(object),
unsigned long event,
void* vtkNotUsed(clientdata),
void* vtkNotUsed(calldata))
{
switch (event)
{
case vtkCommand::StartInteractionEvent:
std::cout << "StartInteractionEvent" << std::endl;
break;
case vtkCommand::EndInteractionEvent:
std::cout << "EndInteractionEvent" << std::endl;
break;
case vtkCommand::InteractionEvent:
std::cout << "InteractionEvent" << std::endl;
break;
}
}
vtkStandardNewMacro(MySphereWidget);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create a renderer and render window.
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("SphereWidgetEvents");
// Create an interactor.
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<MySphereWidget> sphereWidget;
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToSurface();
sphereWidget->HandleVisibilityOn();
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindow->Render();
sphereWidget->On();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(SphereWidgetEvents)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "SphereWidgetEvents: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(SphereWidgetEvents MACOSX_BUNDLE SphereWidgetEvents.cxx )
target_link_libraries(SphereWidgetEvents PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS SphereWidgetEvents
MODULES ${VTK_LIBRARIES}
)
Download and Build SphereWidgetEvents¶
Click here to download SphereWidgetEvents and its CMakeLists.txt file. Once the tarball SphereWidgetEvents.tar has been downloaded and extracted,
cd SphereWidgetEvents/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./SphereWidgetEvents
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.