From 9e464f9f5b4895a03bbab4fcc1ef91edf7550a02 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 17 Jun 2019 22:11:41 +0100 Subject: [PATCH] Implement rollover highlighting for Eeschema delete tool. --- eeschema/ee_collectors.cpp | 18 ---- eeschema/ee_collectors.h | 1 - eeschema/sch_painter.cpp | 2 +- eeschema/tools/ee_picker_tool.cpp | 24 +++-- eeschema/tools/ee_picker_tool.h | 14 ++- eeschema/tools/sch_edit_tool.cpp | 156 +++++++++++++++++++++--------- eeschema/tools/sch_edit_tool.h | 5 +- 7 files changed, 146 insertions(+), 74 deletions(-) diff --git a/eeschema/ee_collectors.cpp b/eeschema/ee_collectors.cpp index b366ebab31..b1af5aa737 100644 --- a/eeschema/ee_collectors.cpp +++ b/eeschema/ee_collectors.cpp @@ -54,24 +54,6 @@ const KICAD_T EE_COLLECTOR::EditableItems[] = { }; -const KICAD_T EE_COLLECTOR::RotatableItems[] = { - SCH_TEXT_T, - SCH_LABEL_T, - SCH_GLOBAL_LABEL_T, - SCH_HIER_LABEL_T, - SCH_FIELD_T, - SCH_COMPONENT_T, - SCH_SHEET_PIN_T, - SCH_SHEET_T, - SCH_BITMAP_T, - SCH_BUS_BUS_ENTRY_T, - SCH_BUS_WIRE_ENTRY_T, - SCH_LINE_T, - SCH_JUNCTION_T, - EOT -}; - - const KICAD_T EE_COLLECTOR::ComponentsOnly[] = { SCH_COMPONENT_T, EOT diff --git a/eeschema/ee_collectors.h b/eeschema/ee_collectors.h index 8b6f4e1434..6d84e6db1f 100644 --- a/eeschema/ee_collectors.h +++ b/eeschema/ee_collectors.h @@ -40,7 +40,6 @@ class EE_COLLECTOR : public COLLECTOR public: static const KICAD_T AllItems[]; static const KICAD_T EditableItems[]; - static const KICAD_T RotatableItems[]; static const KICAD_T ComponentsOnly[]; static const KICAD_T SheetsOnly[]; diff --git a/eeschema/sch_painter.cpp b/eeschema/sch_painter.cpp index 4f13f5d78b..c3ccea822a 100644 --- a/eeschema/sch_painter.cpp +++ b/eeschema/sch_painter.cpp @@ -227,7 +227,7 @@ COLOR4D SCH_PAINTER::getRenderColor( const EDA_ITEM* aItem, int aLayer, bool aOn { COLOR4D color = m_schSettings.GetLayerColor( aLayer ); - if( aItem->IsBrightened() ) + if( aItem->IsBrightened() && !aOnBackgroundLayer ) color = m_schSettings.GetLayerColor( LAYER_BRIGHTENED ); else if( aItem->Type() == SCH_LINE_T ) color = static_cast( aItem )->GetLineColor(); diff --git a/eeschema/tools/ee_picker_tool.cpp b/eeschema/tools/ee_picker_tool.cpp index 8e35c671ca..fb155b5d67 100644 --- a/eeschema/tools/ee_picker_tool.cpp +++ b/eeschema/tools/ee_picker_tool.cpp @@ -38,25 +38,23 @@ EE_PICKER_TOOL::EE_PICKER_TOOL() int EE_PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) { KIGFX::VIEW_CONTROLS* controls = getViewControls(); - int finalize_state = WAIT_CANCEL; + int finalize_state = WAIT_CANCEL; setControls(); while( TOOL_EVENT* evt = Wait() ) { - VECTOR2I cursorPos = controls->GetCursorPosition( !evt->Modifier( MD_ALT ) ); + VECTOR2D cursorPos = controls->GetCursorPosition( !evt->Modifier( MD_ALT ) ); if( evt->IsClick( BUT_LEFT ) ) { bool getNext = false; - m_picked = cursorPos; - if( m_clickHandler ) { try { - getNext = (*m_clickHandler)( *m_picked ); + getNext = (*m_clickHandler)( cursorPos ); } catch( std::exception& e ) { @@ -75,6 +73,21 @@ int EE_PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) setControls(); } + else if( evt->IsMotion() ) + { + if( m_motionHandler ) + { + try + { + (*m_motionHandler)( cursorPos ); + } + catch( std::exception& e ) + { + std::cerr << "EE_PICKER_TOOL motion handler error: " << e.what() << std::endl; + } + } + } + else if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) ) { if( m_cancelHandler ) @@ -135,7 +148,6 @@ void EE_PICKER_TOOL::resetPicker() m_cursorCapture = false; m_autoPanning = false; - m_picked = NULLOPT; m_clickHandler = NULLOPT; m_cancelHandler = NULLOPT; m_finalizeHandler = NULLOPT; diff --git a/eeschema/tools/ee_picker_tool.h b/eeschema/tools/ee_picker_tool.h index 032da997ab..00c064a022 100644 --- a/eeschema/tools/ee_picker_tool.h +++ b/eeschema/tools/ee_picker_tool.h @@ -39,6 +39,7 @@ public: ///> Event handler types. typedef std::function CLICK_HANDLER; + typedef std::function MOTION_HANDLER; typedef std::function CANCEL_HANDLER; typedef std::function FINALIZE_HANDLER; @@ -77,6 +78,16 @@ public: m_clickHandler = aHandler; } + /** + * Function SetMotionHandler() + * Sets a handler for mouse motion. Used for roll-over highlighting. + */ + inline void SetMotionHandler( MOTION_HANDLER aHandler ) + { + assert( !m_motionHandler ); + m_motionHandler = aHandler; + } + /** * Function SetCancelHandler() * Sets a handler for cancel events (ESC or context-menu Cancel). @@ -112,10 +123,9 @@ private: bool m_autoPanning; OPT m_clickHandler; + OPT m_motionHandler; OPT m_cancelHandler; OPT m_finalizeHandler; - - OPT m_picked; }; #endif /* EE_PICKER_TOOL_H */ diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index 0e5b716b76..4c5f8f0701 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -317,9 +317,27 @@ bool SCH_EDIT_TOOL::Init() } +const KICAD_T rotatableItems[] = { + SCH_TEXT_T, + SCH_LABEL_T, + SCH_GLOBAL_LABEL_T, + SCH_HIER_LABEL_T, + SCH_FIELD_T, + SCH_COMPONENT_T, + SCH_SHEET_PIN_T, + SCH_SHEET_T, + SCH_BITMAP_T, + SCH_BUS_BUS_ENTRY_T, + SCH_BUS_WIRE_ENTRY_T, + SCH_LINE_T, + SCH_JUNCTION_T, + EOT +}; + + int SCH_EDIT_TOOL::Rotate( const TOOL_EVENT& aEvent ) { - EE_SELECTION& selection = m_selectionTool->RequestSelection( EE_COLLECTOR::RotatableItems ); + EE_SELECTION& selection = m_selectionTool->RequestSelection( rotatableItems ); if( selection.GetSize() == 0 ) return 0; @@ -489,7 +507,7 @@ int SCH_EDIT_TOOL::Rotate( const TOOL_EVENT& aEvent ) int SCH_EDIT_TOOL::Mirror( const TOOL_EVENT& aEvent ) { - EE_SELECTION& selection = m_selectionTool->RequestSelection( EE_COLLECTOR::RotatableItems ); + EE_SELECTION& selection = m_selectionTool->RequestSelection( rotatableItems ); if( selection.GetSize() == 0 ) return 0; @@ -669,24 +687,25 @@ int SCH_EDIT_TOOL::Mirror( const TOOL_EVENT& aEvent ) } +static KICAD_T duplicatableItems[] = +{ + SCH_JUNCTION_T, + SCH_LINE_T, + SCH_BUS_BUS_ENTRY_T, + SCH_BUS_WIRE_ENTRY_T, + SCH_TEXT_T, + SCH_LABEL_T, + SCH_GLOBAL_LABEL_T, + SCH_HIER_LABEL_T, + SCH_NO_CONNECT_T, + SCH_SHEET_T, + SCH_COMPONENT_T, + EOT +}; + + int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent ) { - static KICAD_T duplicatableItems[] = - { - SCH_JUNCTION_T, - SCH_LINE_T, - SCH_BUS_BUS_ENTRY_T, - SCH_BUS_WIRE_ENTRY_T, - SCH_TEXT_T, - SCH_LABEL_T, - SCH_GLOBAL_LABEL_T, - SCH_HIER_LABEL_T, - SCH_NO_CONNECT_T, - SCH_SHEET_T, - SCH_COMPONENT_T, - EOT - }; - EE_SELECTION& selection = m_selectionTool->RequestSelection( duplicatableItems ); if( selection.GetSize() == 0 ) @@ -827,10 +846,29 @@ int SCH_EDIT_TOOL::RepeatDrawItem( const TOOL_EVENT& aEvent ) } +static KICAD_T deletableItems[] = +{ + SCH_MARKER_T, + SCH_JUNCTION_T, + SCH_LINE_T, + SCH_BUS_BUS_ENTRY_T, + SCH_BUS_WIRE_ENTRY_T, + SCH_TEXT_T, + SCH_LABEL_T, + SCH_GLOBAL_LABEL_T, + SCH_HIER_LABEL_T, + SCH_NO_CONNECT_T, + SCH_SHEET_T, + SCH_SHEET_PIN_T, + SCH_COMPONENT_T, + EOT +}; + + int SCH_EDIT_TOOL::DoDelete( const TOOL_EVENT& aEvent ) { SCH_SCREEN* screen = m_frame->GetScreen(); - auto items = m_selectionTool->RequestSelection().GetItems(); + auto items = m_selectionTool->RequestSelection( deletableItems ).GetItems(); bool appendToUndo = false; if( items.empty() ) @@ -890,45 +928,73 @@ int SCH_EDIT_TOOL::DoDelete( const TOOL_EVENT& aEvent ) } -static bool deleteItem( SCH_BASE_FRAME* aFrame, const VECTOR2D& aPosition ) -{ - EE_SELECTION_TOOL* selectionTool = aFrame->GetToolManager()->GetTool(); - wxCHECK( selectionTool, false ); - - aFrame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true ); - - EDA_ITEM* item = selectionTool->SelectPoint( aPosition ); - SCH_ITEM* sch_item = dynamic_cast( item ); - - if( sch_item && sch_item->IsLocked() ) - { - STATUS_TEXT_POPUP statusPopup( aFrame ); - statusPopup.SetText( _( "Item locked." ) ); - statusPopup.PopupFor( 2000 ); - statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) ); - return true; - } - - if( item ) - aFrame->GetToolManager()->RunAction( EE_ACTIONS::doDelete, true ); - - return true; -} +#define HITTEST_THRESHOLD_PIXELS 5 int SCH_EDIT_TOOL::DeleteItemCursor( const TOOL_EVENT& aEvent ) { + m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true ); + m_frame->SetTool( aEvent.GetCommandStr().get() ); Activate(); EE_PICKER_TOOL* picker = m_toolMgr->GetTool(); wxCHECK( picker, 0 ); + m_pickerItem = nullptr; + + picker->SetClickHandler( [this] ( const VECTOR2D& aPosition ) -> bool { + if( m_pickerItem ) + { + SCH_ITEM* sch_item = dynamic_cast( m_pickerItem ); + + if( sch_item && sch_item->IsLocked() ) + { + STATUS_TEXT_POPUP statusPopup( m_frame ); + statusPopup.SetText( _( "Item locked." ) ); + statusPopup.PopupFor( 2000 ); + statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) ); + return true; + } + + EE_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool(); + selectionTool->AddItemToSel( m_pickerItem, true ); + m_toolMgr->RunAction( EE_ACTIONS::doDelete, true ); + m_pickerItem = nullptr; + } + + return true; + } ); + + picker->SetMotionHandler( [this] ( const VECTOR2D& aPos ) { + EE_COLLECTOR collector; + collector.m_Threshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) ); + collector.Collect( m_frame->GetScreen()->GetDrawItems(), deletableItems, (wxPoint) aPos ); + EDA_ITEM* item = collector.GetCount() == 1 ? collector[ 0 ] : nullptr; + + if( m_pickerItem != item ) + { + EE_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool(); + + if( m_pickerItem ) + selectionTool->UnbrightenItem( m_pickerItem ); + + m_pickerItem = item; + + if( m_pickerItem ) + selectionTool->BrightenItem( m_pickerItem ); + } + } ); + + picker->SetCancelHandler( [this] () { + m_frame->ClearToolStack(); + } ); - picker->SetClickHandler( std::bind( deleteItem, m_frame, std::placeholders::_1 ) ); - picker->SetCancelHandler( [this]() { m_frame->ClearToolStack(); } ); picker->Activate(); Wait(); + if( m_pickerItem ) + m_toolMgr->GetTool()->UnbrightenItem( m_pickerItem ); + return 0; } diff --git a/eeschema/tools/sch_edit_tool.h b/eeschema/tools/sch_edit_tool.h index 61503bf33a..235287e289 100644 --- a/eeschema/tools/sch_edit_tool.h +++ b/eeschema/tools/sch_edit_tool.h @@ -36,7 +36,7 @@ class SCH_EDIT_TOOL : public EE_TOOL_BASE { public: SCH_EDIT_TOOL(); - ~SCH_EDIT_TOOL() { } + ~SCH_EDIT_TOOL() override { } /// @copydoc TOOL_INTERACTIVE::Init() bool Init() override; @@ -69,6 +69,9 @@ public: private: ///> Sets up handlers for various events. void setTransitions() override; + +private: + EDA_ITEM* m_pickerItem; }; #endif //KICAD_SCH_EDIT_TOOL_H