/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, you may find one here: * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * or you may search the http://www.gnu.org website for the version 2 license, * or you may write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pl_selection_tool.h" /** * The maximum number of items in the clarify selection context menu. The current * setting of 40 is arbitrary. */ #define MAX_SELECT_ITEM_IDS 40 // Selection tool actions TOOL_ACTION PL_ACTIONS::selectionActivate( "plEditor.InteractiveSelection", AS_GLOBAL, 0, "", "", // No description, not shown anywhere nullptr, AF_ACTIVATE ); TOOL_ACTION PL_ACTIONS::selectionMenu( "plEditor.InteractiveSelection.SelectionMenu", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere TOOL_ACTION PL_ACTIONS::addItemToSel( "plEditor.InteractiveSelection.AddItemToSel", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere TOOL_ACTION PL_ACTIONS::addItemsToSel( "plEditor.InteractiveSelection.AddItemsToSel", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere TOOL_ACTION PL_ACTIONS::removeItemFromSel( "plEditor.InteractiveSelection.RemoveItemFromSel", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere TOOL_ACTION PL_ACTIONS::removeItemsFromSel( "plEditor.InteractiveSelection.RemoveItemsFromSel", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere TOOL_ACTION PL_ACTIONS::clearSelection( "plEditor.InteractiveSelection.ClearSelection", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere SELECTION_CONDITION PL_CONDITIONS::Idle = [] (const SELECTION& aSelection ) { return ( !aSelection.Front() || aSelection.Front()->GetEditFlags() == 0 ); }; #define HITTEST_THRESHOLD_PIXELS 5 PL_SELECTION_TOOL::PL_SELECTION_TOOL() : TOOL_INTERACTIVE( "plEditor.InteractiveSelection" ), m_frame( nullptr ), m_additive( false ), m_subtractive( false ), m_multiple( false ), m_skip_heuristics( false ), m_menu( *this ) { } bool PL_SELECTION_TOOL::Init() { m_frame = getEditFrame(); auto& menu = m_menu.GetMenu(); menu.AddSeparator( PL_CONDITIONS::Idle, 200 ); menu.AddItem( PL_ACTIONS::addLine, PL_CONDITIONS::Idle, 250 ); menu.AddItem( PL_ACTIONS::addRectangle, PL_CONDITIONS::Idle, 250 ); menu.AddItem( PL_ACTIONS::addText, PL_CONDITIONS::Idle, 250 ); menu.AddItem( PL_ACTIONS::addImage, PL_CONDITIONS::Idle, 250 ); menu.AddItem( PL_ACTIONS::appendImportedWorksheet, PL_CONDITIONS::Idle, 250 ); menu.AddSeparator( SELECTION_CONDITIONS::ShowAlways, 1000 ); m_menu.AddStandardSubMenus( m_frame ); return true; } void PL_SELECTION_TOOL::Reset( RESET_REASON aReason ) { if( aReason == MODEL_RELOAD ) m_frame = getEditFrame(); } int PL_SELECTION_TOOL::UpdateMenu( const TOOL_EVENT& aEvent ) { ACTION_MENU* actionMenu = aEvent.Parameter(); CONDITIONAL_MENU* conditionalMenu = dynamic_cast( actionMenu ); if( conditionalMenu ) conditionalMenu->Evaluate( m_selection ); if( actionMenu ) actionMenu->UpdateAll(); return 0; } int PL_SELECTION_TOOL::Main( const TOOL_EVENT& aEvent ) { // Main loop: keep receiving events while( OPT_TOOL_EVENT evt = Wait() ) { // Should selected items be added to the current selection or // become the new selection (discarding previously selected items) m_additive = evt->Modifier( MD_SHIFT ); // Should selected items be REMOVED from the current selection? // This will be ignored if the SHIFT modifier is pressed m_subtractive = !m_additive && evt->Modifier( MD_CTRL ); // Is the user requesting that the selection list include all possible // items without removing less likely selection candidates m_skip_heuristics = !!evt->Modifier( MD_ALT ); // Single click? Select single object if( evt->IsClick( BUT_LEFT ) ) { // If no modifier keys are pressed, clear the selection if( !m_additive ) ClearSelection(); SelectPoint( evt->Position()); } // right click? if there is any object - show the context menu else if( evt->IsClick( BUT_RIGHT ) ) { bool selectionCancelled = false; if( m_selection.Empty() ) { SelectPoint( evt->Position(), &selectionCancelled ); m_selection.SetIsHover( true ); } if( !selectionCancelled ) m_menu.ShowContextMenu( m_selection ); } // double click? Display the properties window else if( evt->IsDblClick( BUT_LEFT ) ) { // No double-click actions currently defined } // drag with LMB? Select multiple objects (or at least draw a selection box) or drag them else if( evt->IsDrag( BUT_LEFT ) ) { if( m_additive || m_subtractive || m_selection.Empty() ) { selectMultiple(); } else { // Check if dragging has started within any of selected items bounding box if( selectionContains( evt->Position() ) ) { // Yes -> run the move tool and wait till it finishes m_toolMgr->InvokeTool( "plEditor.InteractiveEdit" ); } else { // No -> clear the selection list ClearSelection(); } } } else if( evt->IsAction( &ACTIONS::cancelInteractive ) || evt->IsCancel() ) { ClearSelection(); } else if( evt->Action() == TA_UNDO_REDO_PRE ) { ClearSelection(); } else if( evt->Action() == TA_CONTEXT_MENU_CLOSED ) { m_menu.CloseContextMenu( evt ); } else m_toolMgr->PassEvent(); } // This tool is supposed to be active forever assert( false ); return 0; } SELECTION& PL_SELECTION_TOOL::GetSelection() { return m_selection; } EDA_ITEM* PL_SELECTION_TOOL::SelectPoint( const VECTOR2I& aWhere, bool* aSelectionCancelledFlag ) { int threshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) ); // locate items. COLLECTOR collector; for( WS_DATA_ITEM* dataItem : WS_DATA_MODEL::GetTheInstance().GetItems() ) { for( WS_DRAW_ITEM_BASE* drawItem : dataItem->GetDrawItems() ) { if( drawItem->HitTest( (wxPoint) aWhere, threshold ) ) collector.Append( drawItem ); } } bool anyCollected = collector.GetCount() != 0; m_selection.ClearReferencePoint(); // Apply some ugly heuristics to avoid disambiguation menus whenever possible if( collector.GetCount() > 1 && !m_skip_heuristics ) { guessSelectionCandidates( collector, aWhere ); } // If still more than one item we're going to have to ask the user. if( collector.GetCount() > 1 ) { collector.m_MenuTitle = _( "Clarify Selection" ); // Must call selectionMenu via RunAction() to avoid event-loop contention m_toolMgr->RunAction( PL_ACTIONS::selectionMenu, true, &collector ); if( collector.m_MenuCancelled ) { if( aSelectionCancelledFlag ) *aSelectionCancelledFlag = true; return nullptr; } } if( collector.GetCount() == 1 ) { EDA_ITEM* item = collector[ 0 ]; toggleSelection( item ); return item; } if( !m_additive && anyCollected ) ClearSelection(); return nullptr; } void PL_SELECTION_TOOL::guessSelectionCandidates( COLLECTOR& collector, const VECTOR2I& aPos ) { // There are certain conditions that can be handled automatically. // Prefer an exact hit to a sloppy one for( int i = 0; collector.GetCount() == 2 && i < 2; ++i ) { EDA_ITEM* item = collector[ i ]; EDA_ITEM* other = collector[ ( i + 1 ) % 2 ]; if( item->HitTest( (wxPoint) aPos, 0 ) && !other->HitTest( (wxPoint) aPos, 0 ) ) collector.Remove( other ); } } SELECTION& PL_SELECTION_TOOL::RequestSelection() { // If nothing is selected do a hover selection if( m_selection.Empty() ) { VECTOR2D cursorPos = getViewControls()->GetCursorPosition( true ); ClearSelection(); SelectPoint( cursorPos ); m_selection.SetIsHover( true ); } return m_selection; } bool PL_SELECTION_TOOL::selectMultiple() { bool cancelled = false; // Was the tool cancelled while it was running? m_multiple = true; // Multiple selection mode is active KIGFX::VIEW* view = getView(); KIGFX::PREVIEW::SELECTION_AREA area; view->Add( &area ); while( OPT_TOOL_EVENT evt = Wait() ) { if( evt->IsAction( &ACTIONS::cancelInteractive ) || evt->IsActivate() || evt->IsCancel() ) { cancelled = true; break; } if( evt->IsDrag( BUT_LEFT ) ) { // Start drawing a selection box area.SetOrigin( evt->DragOrigin() ); area.SetEnd( evt->Position() ); area.SetAdditive( m_additive ); area.SetSubtractive( m_subtractive ); view->SetVisible( &area, true ); view->Update( &area ); getViewControls()->SetAutoPan( true ); } if( evt->IsMouseUp( BUT_LEFT ) ) { getViewControls()->SetAutoPan( false ); // End drawing the selection box view->SetVisible( &area, false ); int width = area.GetEnd().x - area.GetOrigin().x; int height = area.GetEnd().y - area.GetOrigin().y; /* Selection mode depends on direction of drag-selection: * Left > Right : Select objects that are fully enclosed by selection * Right > Left : Select objects that are crossed by selection */ bool windowSelection = width >= 0 ? true : false; // Construct an EDA_RECT to determine EDA_ITEM selection EDA_RECT selectionRect( (wxPoint)area.GetOrigin(), wxSize( width, height ) ); selectionRect.Normalize(); for( WS_DATA_ITEM* dataItem : WS_DATA_MODEL::GetTheInstance().GetItems() ) { for( WS_DRAW_ITEM_BASE* item : dataItem->GetDrawItems() ) { if( item->HitTest( selectionRect, windowSelection ) ) { if( m_subtractive ) unselect( item ); else select( item ); } } } // Inform other potentially interested tools if( !m_selection.Empty() ) m_toolMgr->ProcessEvent( EVENTS::SelectedEvent ); break; // Stop waiting for events } } getViewControls()->SetAutoPan( false ); // Stop drawing the selection box view->Remove( &area ); m_multiple = false; // Multiple selection mode is inactive if( !cancelled ) m_selection.ClearReferencePoint(); return cancelled; } int PL_SELECTION_TOOL::AddItemToSel( const TOOL_EVENT& aEvent ) { AddItemToSel( aEvent.Parameter() ); return 0; } void PL_SELECTION_TOOL::AddItemToSel( EDA_ITEM* aItem, bool aQuietMode ) { if( aItem ) { select( aItem ); // Inform other potentially interested tools if( !aQuietMode ) m_toolMgr->ProcessEvent( EVENTS::SelectedEvent ); } } int PL_SELECTION_TOOL::AddItemsToSel( const TOOL_EVENT& aEvent ) { AddItemsToSel( aEvent.Parameter(), false ); return 0; } void PL_SELECTION_TOOL::AddItemsToSel( EDA_ITEMS* aList, bool aQuietMode ) { if( aList ) { for( EDA_ITEM* item : *aList ) select( item ); // Inform other potentially interested tools if( !aQuietMode ) m_toolMgr->ProcessEvent( EVENTS::SelectedEvent ); } } int PL_SELECTION_TOOL::RemoveItemFromSel( const TOOL_EVENT& aEvent ) { RemoveItemFromSel( aEvent.Parameter() ); return 0; } void PL_SELECTION_TOOL::RemoveItemFromSel( EDA_ITEM* aItem, bool aQuietMode ) { if( aItem ) { unselect( aItem ); // Inform other potentially interested tools if( !aQuietMode ) m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent ); } } int PL_SELECTION_TOOL::RemoveItemsFromSel( const TOOL_EVENT& aEvent ) { RemoveItemsFromSel( aEvent.Parameter(), false ); return 0; } void PL_SELECTION_TOOL::RemoveItemsFromSel( EDA_ITEMS* aList, bool aQuietMode ) { if( aList ) { for( EDA_ITEM* item : *aList ) unselect( item ); // Inform other potentially interested tools if( !aQuietMode ) m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent ); } } int PL_SELECTION_TOOL::ClearSelection( const TOOL_EVENT& aEvent ) { ClearSelection(); return 0; } void PL_SELECTION_TOOL::RebuildSelection() { m_selection.Clear(); for( WS_DATA_ITEM* dataItem : WS_DATA_MODEL::GetTheInstance().GetItems() ) { for( WS_DRAW_ITEM_BASE* item : dataItem->GetDrawItems() ) { if( item->IsSelected() ) select( item ); } } } int PL_SELECTION_TOOL::SelectionMenu( const TOOL_EVENT& aEvent ) { COLLECTOR* collector = aEvent.Parameter(); if( !doSelectionMenu( collector ) ) collector->m_MenuCancelled = true; return 0; } bool PL_SELECTION_TOOL::doSelectionMenu( COLLECTOR* aCollector ) { EDA_ITEM* current = nullptr; ACTION_MENU menu; int limit = std::min( MAX_SELECT_ITEM_IDS, aCollector->GetCount() ); for( int i = 0; i < limit; ++i ) { wxString text; EDA_ITEM* item = ( *aCollector )[i]; text = item->GetSelectMenuText( m_frame->GetUserUnits() ); wxString menuText = wxString::Format("&%d. %s", i + 1, text ); menu.Add( menuText, i + 1, item->GetMenuImage() ); } if( aCollector->m_MenuTitle.Length() ) menu.SetTitle( aCollector->m_MenuTitle ); menu.SetIcon( info_xpm ); menu.DisplayTitle( true ); SetContextMenu( &menu, CMENU_NOW ); while( OPT_TOOL_EVENT evt = Wait() ) { if( evt->Action() == TA_CONTEXT_MENU_UPDATE ) { if( current ) unhighlight( current, BRIGHTENED ); int id = *evt->GetCommandId(); // User has pointed an item, so show it in a different way if( id > 0 && id <= limit ) { current = ( *aCollector )[id - 1]; highlight( current, BRIGHTENED ); } else { current = NULL; } } else if( evt->Action() == TA_CONTEXT_MENU_CHOICE ) { if( current ) unhighlight( current, BRIGHTENED ); OPT id = evt->GetCommandId(); // User has selected an item, so this one will be returned if( id && ( *id > 0 ) ) current = ( *aCollector )[*id - 1]; else current = NULL; break; } getView()->UpdateItems(); m_frame->GetGalCanvas()->Refresh(); } if( current ) { unhighlight( current, BRIGHTENED ); getView()->UpdateItems(); m_frame->GetGalCanvas()->Refresh(); aCollector->Empty(); aCollector->Append( current ); return true; } return false; } void PL_SELECTION_TOOL::ClearSelection() { if( m_selection.Empty() ) return; while( m_selection.GetSize() ) unhighlight( (EDA_ITEM*) m_selection.Front(), SELECTED, &m_selection ); getView()->Update( &m_selection ); m_selection.SetIsHover( false ); m_selection.ClearReferencePoint(); // Inform other potentially interested tools m_toolMgr->ProcessEvent( EVENTS::ClearedEvent ); } void PL_SELECTION_TOOL::toggleSelection( EDA_ITEM* aItem ) { if( aItem->IsSelected() ) { unselect( aItem ); // Inform other potentially interested tools m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent ); } else { if( !m_additive ) ClearSelection(); select( aItem ); // Inform other potentially interested tools m_toolMgr->ProcessEvent( EVENTS::SelectedEvent ); } if( m_frame ) m_frame->GetGalCanvas()->ForceRefresh(); } void PL_SELECTION_TOOL::select( EDA_ITEM* aItem ) { highlight( aItem, SELECTED, &m_selection ); } void PL_SELECTION_TOOL::unselect( EDA_ITEM* aItem ) { unhighlight( aItem, SELECTED, &m_selection ); } void PL_SELECTION_TOOL::highlight( EDA_ITEM* aItem, int aMode, SELECTION* aGroup ) { if( aMode == SELECTED ) aItem->SetSelected(); else if( aMode == BRIGHTENED ) aItem->SetBrightened(); if( aGroup ) aGroup->Add( aItem ); getView()->Update( aItem ); } void PL_SELECTION_TOOL::unhighlight( EDA_ITEM* aItem, int aMode, SELECTION* aGroup ) { if( aMode == SELECTED ) aItem->ClearSelected(); else if( aMode == BRIGHTENED ) aItem->ClearBrightened(); if( aGroup ) aGroup->Remove( aItem ); getView()->Update( aItem ); } bool PL_SELECTION_TOOL::selectionContains( const VECTOR2I& aPoint ) const { const unsigned GRIP_MARGIN = 20; VECTOR2I margin = getView()->ToWorld( VECTOR2I( GRIP_MARGIN, GRIP_MARGIN ), false ); // Check if the point is located within any of the currently selected items bounding boxes for( auto item : m_selection ) { BOX2I itemBox = item->ViewBBox(); itemBox.Inflate( margin.x, margin.y ); // Give some margin for gripping an item if( itemBox.Contains( aPoint ) ) return true; } return false; } void PL_SELECTION_TOOL::setTransitions() { Go( &PL_SELECTION_TOOL::UpdateMenu, ACTIONS::updateMenu.MakeEvent() ); Go( &PL_SELECTION_TOOL::Main, PL_ACTIONS::selectionActivate.MakeEvent() ); Go( &PL_SELECTION_TOOL::ClearSelection, PL_ACTIONS::clearSelection.MakeEvent() ); Go( &PL_SELECTION_TOOL::AddItemToSel, PL_ACTIONS::addItemToSel.MakeEvent() ); Go( &PL_SELECTION_TOOL::AddItemsToSel, PL_ACTIONS::addItemsToSel.MakeEvent() ); Go( &PL_SELECTION_TOOL::RemoveItemFromSel, PL_ACTIONS::removeItemFromSel.MakeEvent() ); Go( &PL_SELECTION_TOOL::RemoveItemsFromSel, PL_ACTIONS::removeItemsFromSel.MakeEvent() ); Go( &PL_SELECTION_TOOL::SelectionMenu, PL_ACTIONS::selectionMenu.MakeEvent() ); }