From abf0a46dceba75b2b77785c9e932ce76745ad461 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sun, 31 Jan 2021 09:50:19 -0500 Subject: [PATCH] Refactor PICKER_TOOL and push up snapping disable functionality Fixes https://gitlab.com/kicad/code/kicad/-/issues/7348 --- common/tool/picker_tool.cpp | 39 +++++----- eeschema/tools/sch_edit_tool.cpp | 1 + eeschema/tools/sch_editor_control.cpp | 3 + include/tool/picker_tool.h | 59 +++++++++------ pcbnew/dialogs/dialog_group_properties.cpp | 2 + pcbnew/tools/pcb_picker_tool.cpp | 19 ++--- pcbnew/tools/pcb_picker_tool.h | 83 +++------------------- 7 files changed, 82 insertions(+), 124 deletions(-) diff --git a/common/tool/picker_tool.cpp b/common/tool/picker_tool.cpp index bdeec844eb..295b316ab4 100644 --- a/common/tool/picker_tool.cpp +++ b/common/tool/picker_tool.cpp @@ -28,11 +28,30 @@ #include +void PICKER_TOOL_BASE::reset() +{ + m_cursor = KICURSOR::ARROW; + m_snap = true; + + m_picked = NULLOPT; + m_clickHandler = NULLOPT; + m_motionHandler = NULLOPT; + m_cancelHandler = NULLOPT; + m_finalizeHandler = NULLOPT; +} + + +PICKER_TOOL::PICKER_TOOL( const std::string& aName ) : + TOOL_INTERACTIVE( aName ), + PICKER_TOOL_BASE() +{ +} + + PICKER_TOOL::PICKER_TOOL() : TOOL_INTERACTIVE( "common.InteractivePicker" ), - m_frame( nullptr ) + PICKER_TOOL_BASE() { - resetPicker(); } @@ -76,7 +95,7 @@ int PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) while( TOOL_EVENT* evt = Wait() ) { setCursor(); - VECTOR2D cursorPos = controls->GetCursorPosition( m_frame->IsGridVisible() ); + VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() ); if( evt->IsCancelInteractive() || evt->IsActivate() ) { @@ -173,7 +192,7 @@ int PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) } } - resetPicker(); + reset(); controls->ForceCursorPosition( false ); m_frame->PopTool( tool ); return 0; @@ -186,18 +205,6 @@ void PICKER_TOOL::setTransitions() } -void PICKER_TOOL::resetPicker() -{ - m_cursor = KICURSOR::ARROW; - - m_picked = NULLOPT; - m_clickHandler = NULLOPT; - m_motionHandler = NULLOPT; - m_cancelHandler = NULLOPT; - m_finalizeHandler = NULLOPT; -} - - void PICKER_TOOL::setControls() { KIGFX::VIEW_CONTROLS* controls = getViewControls(); diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index ea4613950b..d4360690ef 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -1088,6 +1088,7 @@ int SCH_EDIT_TOOL::DeleteItemCursor( const TOOL_EVENT& aEvent ) Activate(); picker->SetCursor( KICURSOR::REMOVE ); + picker->SetSnapping( false ); picker->SetClickHandler( [this]( const VECTOR2D& aPosition ) -> bool diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index 56fc6e8ed8..e7bfe7cf8a 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -617,6 +617,7 @@ int SCH_EDITOR_CONTROL::SimProbe( const TOOL_EVENT& aEvent ) Activate(); picker->SetCursor( KICURSOR::VOLTAGE_PROBE ); + picker->SetSnapping( false ); picker->SetClickHandler( [this, simFrame]( const VECTOR2D& aPosition ) @@ -737,6 +738,7 @@ int SCH_EDITOR_CONTROL::SimTune( const TOOL_EVENT& aEvent ) Activate(); picker->SetCursor( KICURSOR::TUNE ); + picker->SetSnapping( false ); picker->SetClickHandler( [this]( const VECTOR2D& aPosition ) @@ -1131,6 +1133,7 @@ int SCH_EDITOR_CONTROL::HighlightNetCursor( const TOOL_EVENT& aEvent ) Activate(); picker->SetCursor( KICURSOR::BULLSEYE ); + picker->SetSnapping( false ); picker->SetClickHandler( [this] ( const VECTOR2D& aPos ) diff --git a/include/tool/picker_tool.h b/include/tool/picker_tool.h index bb1465744f..d2863783e6 100644 --- a/include/tool/picker_tool.h +++ b/include/tool/picker_tool.h @@ -33,18 +33,9 @@ class EDA_DRAW_FRAME; -class PICKER_TOOL : public TOOL_INTERACTIVE +class PICKER_TOOL_BASE { public: - PICKER_TOOL(); - ~PICKER_TOOL() {} - - /// @copydoc TOOL_INTERACTIVE::Init() - bool Init() override; - - /// @copydoc TOOL_INTERACTIVE::Reset() - void Reset( RESET_REASON aReason ) override { } - ///< Event handler types. typedef std::function CLICK_HANDLER; typedef std::function MOTION_HANDLER; @@ -60,11 +51,16 @@ public: EXCEPTION_CANCEL }; - ///< Main event loop. - int Main( const TOOL_EVENT& aEvent ); + PICKER_TOOL_BASE() : + m_frame( nullptr ) + { + reset(); + } inline void SetCursor( KICURSOR aCursor ) { m_cursor = aCursor; } + inline void SetSnapping( bool aSnap ) { m_snap = aSnap; } + /** * Set a handler for mouse click event. * @@ -107,19 +103,13 @@ public: m_finalizeHandler = aHandler; } -private: +protected: ///< Reinitializes tool to its initial state. - void resetPicker(); + virtual void reset(); - ///< Applies the requested VIEW_CONTROLS settings. - void setControls(); - - ///< @copydoc TOOL_INTERACTIVE::setTransitions(); - void setTransitions() override; - -private: EDA_DRAW_FRAME* m_frame; KICURSOR m_cursor; + bool m_snap; OPT m_clickHandler; OPT m_motionHandler; @@ -129,4 +119,31 @@ private: OPT m_picked; }; + +class PICKER_TOOL : public TOOL_INTERACTIVE, public PICKER_TOOL_BASE +{ +public: + PICKER_TOOL(); + + PICKER_TOOL( const std::string& aName ); + + virtual ~PICKER_TOOL() = default; + + /// @copydoc TOOL_INTERACTIVE::Init() + bool Init() override; + + /// @copydoc TOOL_INTERACTIVE::Reset() + void Reset( RESET_REASON aReason ) override { } + + ///< Main event loop. + int Main( const TOOL_EVENT& aEvent ); + +protected: + ///< Applies the requested VIEW_CONTROLS settings. + void setControls(); + + ///< @copydoc TOOL_INTERACTIVE::setTransitions(); + void setTransitions() override; +}; + #endif /* PICKER_TOOL_H */ diff --git a/pcbnew/dialogs/dialog_group_properties.cpp b/pcbnew/dialogs/dialog_group_properties.cpp index 4feb3d4dd4..923a6f32e8 100644 --- a/pcbnew/dialogs/dialog_group_properties.cpp +++ b/pcbnew/dialogs/dialog_group_properties.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/pcbnew/tools/pcb_picker_tool.cpp b/pcbnew/tools/pcb_picker_tool.cpp index ffea0aaeba..536882a2ae 100644 --- a/pcbnew/tools/pcb_picker_tool.cpp +++ b/pcbnew/tools/pcb_picker_tool.cpp @@ -31,18 +31,18 @@ #include "pcb_selection_tool.h" -PCB_PICKER_TOOL::PCB_PICKER_TOOL() - : PCB_TOOL_BASE( "pcbnew.InteractivePicker" ) +PCB_PICKER_TOOL::PCB_PICKER_TOOL() : + PCB_TOOL_BASE( "pcbnew.InteractivePicker" ), + PICKER_TOOL_BASE() { - reset(); } int PCB_PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) { KIGFX::VIEW_CONTROLS* controls = getViewControls(); - PCB_BASE_FRAME* frame = getEditFrame(); - PCB_GRID_HELPER grid( m_toolMgr, frame->GetMagneticItemsSettings() ); + PCB_BASE_FRAME* frame = getEditFrame(); + PCB_GRID_HELPER grid( m_toolMgr, frame->GetMagneticItemsSettings() ); int finalize_state = WAIT_CANCEL; std::string tool = *aEvent.Parameter(); @@ -185,14 +185,7 @@ void PCB_PICKER_TOOL::setTransitions() void PCB_PICKER_TOOL::reset() { m_layerMask = LSET::AllLayersMask(); - m_cursor = KICURSOR::ARROW; - m_snap = true; - - m_picked = NULLOPT; - m_clickHandler = NULLOPT; - m_motionHandler = NULLOPT; - m_cancelHandler = NULLOPT; - m_finalizeHandler = NULLOPT; + PICKER_TOOL_BASE::reset(); } diff --git a/pcbnew/tools/pcb_picker_tool.h b/pcbnew/tools/pcb_picker_tool.h index 23abc914b8..06eae8c4c0 100644 --- a/pcbnew/tools/pcb_picker_tool.h +++ b/pcbnew/tools/pcb_picker_tool.h @@ -27,35 +27,18 @@ #ifndef PCB_PICKER_TOOL_H #define PCB_PICKER_TOOL_H -#include +#include +#include #include /** *Generic tool for picking an item. */ -class PCB_PICKER_TOOL : public PCB_TOOL_BASE +class PCB_PICKER_TOOL : public PCB_TOOL_BASE, public PICKER_TOOL_BASE { public: PCB_PICKER_TOOL(); - ~PCB_PICKER_TOOL() override { } - - ///< Event handler types. - typedef std::function CLICK_HANDLER; - typedef std::function MOTION_HANDLER; - typedef std::function CANCEL_HANDLER; - typedef std::function FINALIZE_HANDLER; - - enum pickerEndState - { - WAIT_CANCEL, - CLICK_CANCEL, - END_ACTIVATE, - EVT_CANCEL, - EXCEPTION_CANCEL - }; - - ///< @copydoc TOOL_INTERACTIVE::Reset() - void Reset( RESET_REASON aReason ) override {} + virtual ~PCB_PICKER_TOOL() = default; ///< Main event loop. int Main( const TOOL_EVENT& aEvent ); @@ -65,67 +48,19 @@ public: */ inline void SetLayerSet( LSET aLayerSet ) { m_layerMask = aLayerSet; } - inline void SetCursor( KICURSOR aCursor ) { m_cursor = aCursor; } - - inline void SetSnapping( bool aSnap ) { m_snap = aSnap; } - /** - * Set a handler for mouse click event. Handler may decide to receive further click by - * returning true. - */ - inline void SetClickHandler( CLICK_HANDLER aHandler ) - { - wxASSERT( !m_clickHandler ); - m_clickHandler = aHandler; - } - - /** - * Set a handler for mouse motion. Used for roll-over highlighting. - */ - inline void SetMotionHandler( MOTION_HANDLER aHandler ) - { - wxASSERT( !m_motionHandler ); - m_motionHandler = aHandler; - } - - /** - * Set a handler for cancel events (ESC or context-menu Cancel). - */ - inline void SetCancelHandler( CANCEL_HANDLER aHandler ) - { - wxASSERT( !m_cancelHandler ); - m_cancelHandler = aHandler; - } - - /** - * Set a handler for the finalize event. Takes the state of the exit from the Main loop. - */ - inline void SetFinalizeHandler( FINALIZE_HANDLER aHandler ) - { - wxASSERT( !m_finalizeHandler ); - m_finalizeHandler = aHandler; - } - +protected: ///< @copydoc TOOL_INTERACTIVE::setTransitions(); void setTransitions() override; - ///< Reinitialize tool to its initial state. - void reset(); - - ///< Apply the requested VIEW_CONTROLS settings. + ///< Applies the requested VIEW_CONTROLS settings. void setControls(); + ///< Reinitialize tool to its initial state. + void reset() override; + private: ///< The layer set to use for optional snapping. LSET m_layerMask; - KICURSOR m_cursor; - bool m_snap; - - OPT m_clickHandler; - OPT m_motionHandler; - OPT m_cancelHandler; - OPT m_finalizeHandler; - - OPT m_picked; }; #endif /* PCB_PICKER_TOOL_H */