1 /*========================================================================= 2 3 Program: Visualization Toolkit 4 Module: vtkDistanceWidget.h,v 5 6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 7 All rights reserved. 8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 9 10 This software is distributed WITHOUT ANY WARRANTY; without even 11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 PURPOSE. See the above copyright notice for more information. 13 14 =========================================================================*/ 15 /** 16 * @class vtkDistanceWidget 17 * @brief measure the distance between two points 18 * 19 * The vtkDistanceWidget is used to measure the distance between two points. 20 * The two end points can be positioned independently, and when they are 21 * released, a special PlacePointEvent is invoked so that special operations 22 * may be take to reposition the point (snap to grid, etc.) The widget has 23 * two different modes of interaction: when initially defined (i.e., placing 24 * the two points) and then a manipulate mode (adjusting the position of 25 * the two points). 26 * 27 * To use this widget, specify an instance of vtkDistanceWidget and a 28 * representation (a subclass of vtkDistanceRepresentation). The widget is 29 * implemented using two instances of vtkHandleWidget which are used to 30 * position the end points of the line. The representations for these two 31 * handle widgets are provided by the vtkDistanceRepresentation. 32 * 33 * @par Event Bindings: 34 * By default, the widget responds to the following VTK events (i.e., it 35 * watches the vtkRenderWindowInteractor for these events): 36 * <pre> 37 * LeftButtonPressEvent - add a point or select a handle 38 * MouseMoveEvent - position the second point or move a handle 39 * LeftButtonReleaseEvent - release the handle 40 * </pre> 41 * 42 * @par Event Bindings: 43 * Note that the event bindings described above can be changed using this 44 * class's vtkWidgetEventTranslator. This class translates VTK events 45 * into the vtkDistanceWidget's widget events: 46 * <pre> 47 * vtkWidgetEvent::AddPoint -- add one point; depending on the state 48 * it may the first or second point added. Or, 49 * if near a handle, select the handle. 50 * vtkWidgetEvent::Move -- move the second point or handle depending on the state. 51 * vtkWidgetEvent::EndSelect -- the handle manipulation process has completed. 52 * </pre> 53 * 54 * @par Event Bindings: 55 * This widget invokes the following VTK events on itself (which observers 56 * can listen for): 57 * <pre> 58 * vtkCommand::StartInteractionEvent (beginning to interact) 59 * vtkCommand::EndInteractionEvent (completing interaction) 60 * vtkCommand::InteractionEvent (moving after selecting something) 61 * vtkCommand::PlacePointEvent (after point is positioned; 62 * call data includes handle id (0,1)) 63 * </pre> 64 * 65 * @sa 66 * vtkHandleWidget 67 */ 68 69 #ifndef vtkDistanceWidget_h 70 #define vtkDistanceWidget_h 71 72 #include "vtkAbstractWidget.h" 73 #include "vtkInteractionWidgetsModule.h" // For export macro 74 75 class vtkDistanceRepresentation; 76 class vtkHandleWidget; 77 class vtkDistanceWidgetCallback; 78 79 class VTKINTERACTIONWIDGETS_EXPORT vtkDistanceWidget : public vtkAbstractWidget 80 { 81 public: 82 /** 83 * Instantiate this class. 84 */ 85 static vtkDistanceWidget* New(); 86 87 ///@{ 88 /** 89 * Standard methods for a VTK class. 90 */ 91 vtkTypeMacro(vtkDistanceWidget, vtkAbstractWidget); 92 void PrintSelf(ostream& os, vtkIndent indent) override; 93 ///@} 94 95 /** 96 * The method for activating and deactivating this widget. This method 97 * must be overridden because it is a composite widget and does more than 98 * its superclasses' vtkAbstractWidget::SetEnabled() method. 99 */ 100 void SetEnabled(int) override; 101 102 /** 103 * Specify an instance of vtkWidgetRepresentation used to represent this 104 * widget in the scene. Note that the representation is a subclass of vtkProp 105 * so it can be added to the renderer independent of the widget. 106 */ SetRepresentation(vtkDistanceRepresentation * r)107 void SetRepresentation(vtkDistanceRepresentation* r) 108 { 109 this->Superclass::SetWidgetRepresentation(reinterpret_cast<vtkWidgetRepresentation*>(r)); 110 } 111 112 /** 113 * Return the representation as a vtkDistanceRepresentation. 114 */ GetDistanceRepresentation()115 vtkDistanceRepresentation* GetDistanceRepresentation() 116 { 117 return reinterpret_cast<vtkDistanceRepresentation*>(this->WidgetRep); 118 } 119 120 /** 121 * Create the default widget representation if one is not set. 122 */ 123 void CreateDefaultRepresentation() override; 124 125 /** 126 * Methods to change the whether the widget responds to interaction. 127 * Overridden to pass the state to component widgets. 128 */ 129 void SetProcessEvents(vtkTypeBool) override; 130 131 /** 132 * Description: 133 * Enum defining the state of the widget. By default the widget is in Start mode, 134 * and expects to be interactively placed. While placing the points the widget 135 * transitions to Define state. Once placed, the widget enters the Manipulate state. 136 */ 137 138 enum 139 { 140 Start = 0, 141 Define, 142 Manipulate 143 }; 144 145 ///@{ 146 /** 147 * Set the state of the widget. If the state is set to "Manipulate" then it 148 * is assumed that the widget and its representation will be initialized 149 * programmatically and is not interactively placed. Initially the widget 150 * state is set to "Start" which means nothing will appear and the user 151 * must interactively place the widget with repeated mouse selections. Set 152 * the state to "Start" if you want interactive placement. Generally state 153 * changes must be followed by a Render() for things to visually take 154 * effect. 155 */ 156 virtual void SetWidgetStateToStart(); 157 virtual void SetWidgetStateToManipulate(); 158 ///@} 159 160 /** 161 * Return the current widget state. 162 */ GetWidgetState()163 virtual int GetWidgetState() { return this->WidgetState; } 164 165 protected: 166 vtkDistanceWidget(); 167 ~vtkDistanceWidget() override; 168 169 // The state of the widget 170 int WidgetState; 171 int CurrentHandle; 172 173 // Callback interface to capture events when 174 // placing the widget. 175 static void AddPointAction(vtkAbstractWidget*); 176 static void MoveAction(vtkAbstractWidget*); 177 static void EndSelectAction(vtkAbstractWidget*); 178 static void AddPointAction3D(vtkAbstractWidget*); 179 static void MoveAction3D(vtkAbstractWidget*); 180 static void EndSelectAction3D(vtkAbstractWidget*); 181 182 // The positioning handle widgets 183 vtkHandleWidget* Point1Widget; 184 vtkHandleWidget* Point2Widget; 185 vtkDistanceWidgetCallback* DistanceWidgetCallback1; 186 vtkDistanceWidgetCallback* DistanceWidgetCallback2; 187 188 // Methods invoked when the handles at the 189 // end points of the widget are manipulated 190 void StartDistanceInteraction(int handleNum); 191 void DistanceInteraction(int handleNum); 192 void EndDistanceInteraction(int handleNum); 193 194 friend class vtkDistanceWidgetCallback; 195 196 private: 197 vtkDistanceWidget(const vtkDistanceWidget&) = delete; 198 void operator=(const vtkDistanceWidget&) = delete; 199 }; 200 201 #endif 202