Do some refactoring in prep for sharing some sch tools with libedit.

This commit is contained in:
Jeff Young 2019-05-04 14:02:05 +01:00
parent ed0e6af66d
commit 32765ebffd
17 changed files with 856 additions and 771 deletions

View File

@ -242,6 +242,7 @@ set( EESCHEMA_SRCS
tools/sch_editor_control.cpp
tools/sch_inspection_tool.cpp
tools/sch_line_drawing_tool.cpp
tools/sch_move_tool.cpp
tools/sch_picker_tool.cpp
tools/sch_selection_tool.cpp
tools/selection.cpp

View File

@ -33,7 +33,6 @@
#include <general.h>
#include <class_library.h>
#include <lib_pin.h>
#include <list_operations.h>
#include <sch_bus_entry.h>
#include <sch_marker.h>
#include <sch_junction.h>
@ -43,7 +42,6 @@
#include <sch_component.h>
#include <sch_sheet.h>
#include <sch_sheet_path.h>
#include <list_operations.h>
#include <preview_items/selection_area.h>
#include <sch_view.h>

View File

@ -48,7 +48,6 @@
#include <widgets/symbol_tree_pane.h>
#include <widgets/lib_tree.h>
#include <symbol_lib_table.h>
#include <list_operations.h>
#include <kicad_device_context.h>
#include <hotkeys.h>
#include <eeschema_config.h>

View File

@ -1,49 +0,0 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 KiCad Developers, see CHANGELOG.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
*/
#ifndef LIST_OPERATIONS_H
#define LIST_OPERATIONS_H
class wxPoint;
class wxDC;
class SCH_ITEM;
class SCH_SCREEN;
class PICKED_ITEMS_LIST;
void SetSchItemParent( SCH_ITEM* Struct, SCH_SCREEN* Screen );
/**
* Routine to create a new copy of given struct.
* The new object is not put in draw list (not linked)
*
* @param aItem = the SCH_ITEM to duplicate
* @param doClone (default = false)
* if true duplicate also some parameters that must be unique
* (timestamp and sheet name)
* aClone must be false. use true only is undo/redo duplications
*/
SCH_ITEM* DuplicateItem( SCH_ITEM* aItem, bool doClone = false );
#endif /* LIST_OPERATIONS_H */

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors.
* Copyright (C) 2014-2019 KiCad Developers, see CHANGELOG.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
@ -22,55 +22,16 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file operations_on_items_lists.cpp
* @brief Functions used in block commands, or undo/redo, to move, mirror, delete, copy ...
* lists of schematic items.
*/
#include <fctsys.h>
#include <pgm_base.h>
#include <sch_draw_panel.h>
#include <sch_edit_frame.h>
#include <general.h>
#include <list_operations.h>
#include <sch_bus_entry.h>
#include <sch_marker.h>
#include <sch_line.h>
#include <sch_no_connect.h>
#include <sch_sheet.h>
#include <sch_component.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <tool/selection.h>
void SetSchItemParent( SCH_ITEM* Struct, SCH_SCREEN* Screen )
{
switch( Struct->Type() )
{
case SCH_JUNCTION_T:
case SCH_TEXT_T:
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIER_LABEL_T:
case SCH_COMPONENT_T:
case SCH_LINE_T:
case SCH_BUS_BUS_ENTRY_T:
case SCH_BUS_WIRE_ENTRY_T:
case SCH_SHEET_T:
case SCH_MARKER_T:
case SCH_NO_CONNECT_T:
Struct->SetParent( Screen );
break;
case SCH_SHEET_PIN_T:
break;
default:
break;
}
}
void SCH_EDIT_FRAME::CheckConnections( SELECTION& aSelection, bool aUndoAppend )
{
@ -128,80 +89,3 @@ void SCH_EDIT_FRAME::CheckConnections( SELECTION& aSelection, bool aUndoAppend )
}
void SCH_EDIT_FRAME::DeleteItem( SCH_ITEM* aItem, bool aAppend )
{
wxCHECK_RET( aItem != NULL, wxT( "Cannot delete invalid item." ) );
wxCHECK_RET( !( aItem->GetEditFlags() & STRUCT_DELETED ),
wxT( "Cannot delete item that is already deleted." ) );
// Here, aItem is not null.
SCH_SCREEN* screen = GetScreen();
if( aItem->Type() == SCH_SHEET_PIN_T )
{
// This item is attached to its parent hierarchical sheet,
// and is not accessible by the global list directly and cannot be removed from this list.
SCH_SHEET* sheet = (SCH_SHEET*) aItem->GetParent();
wxCHECK_RET( (sheet != NULL) && (sheet->Type() == SCH_SHEET_T),
wxT( "Sheet label has invalid parent item." ) );
SaveCopyInUndoList( (SCH_ITEM*) sheet, UR_CHANGED, aAppend );
sheet->RemovePin( (SCH_SHEET_PIN*) aItem );
RefreshItem( sheet );
}
else if( aItem->Type() == SCH_JUNCTION_T )
{
DeleteJunction( aItem, aAppend );
}
else
{
aItem->SetFlags( STRUCT_DELETED );
SaveCopyInUndoList( aItem, UR_DELETED, aAppend );
RemoveFromScreen( aItem );
std::vector< wxPoint > pts;
aItem->GetConnectionPoints( pts );
for( auto point : pts )
{
SCH_ITEM* junction = screen->GetItem( point, 0, SCH_JUNCTION_T );
if( junction && !screen->IsJunctionNeeded( point ) )
DeleteJunction( junction, true );
}
}
}
SCH_ITEM* DuplicateItem( SCH_ITEM* aItem, bool doClone )
{
wxCHECK_MSG( aItem != NULL, NULL, "Cannot duplicate NULL schematic item! Bad programmer." );
SCH_ITEM* newItem = (SCH_ITEM*) aItem->Clone();
if( doClone )
newItem->SetTimeStamp( aItem->GetTimeStamp() );
newItem->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
if( newItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
for( SCH_PIN& pin : component->GetPins() )
pin.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
std::vector<SCH_FIELD*> fields;
component->GetFields( fields, false );
for( SCH_FIELD* field : fields )
field->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
}
if( newItem->Type() == SCH_SHEET_T )
{
SCH_SHEET* sheet = (SCH_SHEET*) newItem;
for( SCH_SHEET_PIN& pin : sheet->GetPins() )
pin.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
}
return newItem;
}

View File

@ -63,6 +63,7 @@
#include <tools/sch_picker_tool.h>
#include <tools/sch_drawing_tool.h>
#include <tools/sch_line_drawing_tool.h>
#include <tools/sch_move_tool.h>
#include <tools/sch_edit_tool.h>
#include <tools/sch_inspection_tool.h>
#include <tools/sch_editor_control.h>
@ -430,6 +431,7 @@ void SCH_EDIT_FRAME::setupTools()
m_toolManager->RegisterTool( new SCH_PICKER_TOOL );
m_toolManager->RegisterTool( new SCH_DRAWING_TOOL );
m_toolManager->RegisterTool( new SCH_LINE_DRAWING_TOOL );
m_toolManager->RegisterTool( new SCH_MOVE_TOOL );
m_toolManager->RegisterTool( new SCH_EDIT_TOOL );
m_toolManager->RegisterTool( new SCH_INSPECTION_TOOL );
m_toolManager->RegisterTool( new SCH_EDITOR_CONTROL );

View File

@ -961,14 +961,6 @@ public:
*/
SCH_SHEET_PIN* ImportSheetPin( SCH_SHEET* aSheet );
/**
* Remove \a aItem from the current screen and saves it in the undo list.
*
* @param aItem The item to remove from the current screen.
* @param aAppend True if we are updating a previous Undo state
*/
void DeleteItem( SCH_ITEM* aItem, bool aAppend = false );
/**
* Removes a given junction and heals any wire segments under the junction
*

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-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
@ -22,10 +22,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file sch_item_struct.cpp
*/
#include <fctsys.h>
#include <common.h>
#include <gr_basic.h>
@ -36,7 +32,9 @@
#include <sch_sheet_path.h>
#include <sch_draw_panel.h>
#include <sch_edit_frame.h>
#include <sch_component.h>
#include <sch_sheet.h>
#include <sch_pin.h>
#include <general.h>
@ -74,6 +72,41 @@ SCH_ITEM::~SCH_ITEM()
}
SCH_ITEM* SCH_ITEM::Duplicate( bool doClone )
{
SCH_ITEM* newItem = (SCH_ITEM*) Clone();
if( doClone )
newItem->SetTimeStamp( GetTimeStamp() );
newItem->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
if( newItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
for( SCH_PIN& pin : component->GetPins() )
pin.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
std::vector<SCH_FIELD*> fields;
component->GetFields( fields, false );
for( SCH_FIELD* field : fields )
field->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
}
if( newItem->Type() == SCH_SHEET_T )
{
SCH_SHEET* sheet = (SCH_SHEET*) newItem;
for( SCH_SHEET_PIN& pin : sheet->GetPins() )
pin.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
}
return newItem;
}
void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
{
// Basic fallback

View File

@ -159,6 +159,15 @@ public:
SCH_ITEM* Next() const { return static_cast<SCH_ITEM*>( Pnext ); }
SCH_ITEM* Back() const { return static_cast<SCH_ITEM*>( Pback ); }
/**
* Routine to create a new copy of given item.
* The new object is not put in draw list (not linked).
*
* @param doClone (default = false) indicates unique values (such as timestamp and
* sheet name) should be duplicated. Use only for undo/redo operations.
*/
SCH_ITEM* Duplicate( bool doClone = false );
/**
* Virtual function IsMovableFromAnchorPoint
* @return true for items which are moved with the anchor point at mouse cursor

View File

@ -35,7 +35,6 @@
#include <base_units.h>
#include <eeschema_config.h>
#include <general.h>
#include <list_operations.h>
#include <sch_line.h>
#include <sch_edit_frame.h>
#include <netlist_object.h>

View File

@ -40,7 +40,6 @@
#include <gal/stroke_font.h>
#include <bitmaps.h>
#include <list_operations.h>
#include <sch_text.h>
#include <netlist_object.h>
#include <trace_helpers.h>

View File

@ -27,7 +27,6 @@
#include <sch_edit_frame.h>
#include <tool/tool_manager.h>
#include <general.h>
#include <list_operations.h>
#include <sch_bus_entry.h>
#include <sch_marker.h>
#include <sch_junction.h>
@ -129,7 +128,7 @@ void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_ITEM* aItem,
switch( aCommandType )
{
case UR_CHANGED: /* Create a copy of item */
itemWrapper.SetLink( DuplicateItem( aItem, true ) );
itemWrapper.SetLink( aItem->Duplicate( true ) );
commandToUndo->PushItem( itemWrapper );
break;
@ -218,7 +217,7 @@ void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
* If this link is not null, the copy is already done
*/
if( commandToUndo->GetPickedItemLink( ii ) == NULL )
commandToUndo->SetPickedItemLink( DuplicateItem( item, true ), ii );
commandToUndo->SetPickedItemLink( item->Duplicate( true ), ii );
wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
break;

View File

@ -26,6 +26,7 @@
#include <tools/sch_selection_tool.h>
#include <tools/sch_line_drawing_tool.h>
#include <tools/sch_picker_tool.h>
#include <tools/sch_move_tool.h>
#include <sch_actions.h>
#include <hotkeys.h>
#include <bitmaps.h>
@ -42,21 +43,11 @@
#include <sch_item_struct.h>
#include <sch_bus_entry.h>
#include <sch_edit_frame.h>
#include <list_operations.h>
#include <eeschema_id.h>
#include <status_popup.h>
#include <wx/gdicmn.h>
#include "sch_drawing_tool.h"
TOOL_ACTION SCH_ACTIONS::move( "eeschema.InteractiveEdit.move",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_MOVE ),
_( "Move" ), _( "Moves the selected item(s)" ),
move_xpm, AF_ACTIVATE );
TOOL_ACTION SCH_ACTIONS::drag( "eeschema.InteractiveEdit.drag",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_DRAG ),
_( "Drag" ), _( "Drags the selected item(s)" ),
move_xpm, AF_ACTIVATE );
TOOL_ACTION SCH_ACTIONS::duplicate( "eeschema.InteractiveEdit.duplicate",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_DUPLICATE ),
@ -174,10 +165,6 @@ TOOL_ACTION SCH_ACTIONS::breakBus( "eeschema.InteractiveEdit.breakBus",
break_line_xpm );
// For adding to or removing from selections
#define QUIET_MODE true
class SYMBOL_UNIT_MENU : public CONTEXT_MENU
{
public:
@ -240,12 +227,8 @@ private:
SCH_EDIT_TOOL::SCH_EDIT_TOOL() :
TOOL_INTERACTIVE( "eeschema.InteractiveEdit" ),
m_selectionTool( nullptr ),
m_view( nullptr ),
m_controls( nullptr ),
m_frame( nullptr ),
m_menu( *this ),
m_moveInProgress( false ),
m_moveOffset( 0, 0 )
m_menu( *this )
{
}
@ -260,14 +243,11 @@ bool SCH_EDIT_TOOL::Init()
m_frame = getEditFrame<SCH_EDIT_FRAME>();
m_selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
SCH_DRAWING_TOOL* drawingTool = m_toolMgr->GetTool<SCH_DRAWING_TOOL>();
SCH_MOVE_TOOL* moveTool = m_toolMgr->GetTool<SCH_MOVE_TOOL>();
wxASSERT_MSG( m_selectionTool, "eeshema.InteractiveSelection tool is not available" );
wxASSERT_MSG( drawingTool, "eeshema.InteractiveDrawing tool is not available" );
auto activeTool = [ this ] ( const SELECTION& aSel ) {
return ( m_frame->GetToolId() != ID_NO_TOOL_SELECTED );
};
auto sheetTool = [ this ] ( const SELECTION& aSel ) {
return ( m_frame->GetToolId() == ID_SHEET_SYMBOL_BUTT );
};
@ -279,10 +259,7 @@ bool SCH_EDIT_TOOL::Init()
|| m_frame->GetToolId() == ID_TEXT_COMMENT_BUTT );
};
auto moveCondition = [] ( const SELECTION& aSel ) {
if( aSel.Empty() )
return false;
auto duplicateCondition = [] ( const SELECTION& aSel ) {
if( SCH_LINE_DRAWING_TOOL::IsDrawingLineWireOrBus( aSel ) )
return false;
@ -370,81 +347,77 @@ bool SCH_EDIT_TOOL::Init()
&& SCH_CONDITIONS::OnlyType( SCH_SHEET_T );
//
// Build the edit tool menu (shown when moving or dragging)
// Add edit actions to the move tool menu
//
CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
if( moveTool )
{
CONDITIONAL_MENU& moveMenu = moveTool->GetToolMenu().GetMenu();
ctxMenu.AddItem( ACTIONS::cancelInteractive, activeTool, 1 );
moveMenu.AddSeparator( SELECTION_CONDITIONS::NotEmpty );
moveMenu.AddItem( SCH_ACTIONS::rotateCCW, orientCondition );
moveMenu.AddItem( SCH_ACTIONS::rotateCW, orientCondition );
moveMenu.AddItem( SCH_ACTIONS::mirrorX, orientCondition );
moveMenu.AddItem( SCH_ACTIONS::mirrorY, orientCondition );
moveMenu.AddItem( SCH_ACTIONS::duplicate, duplicateCondition );
moveMenu.AddItem( SCH_ACTIONS::doDelete, SCH_CONDITIONS::NotEmpty );
ctxMenu.AddSeparator( SELECTION_CONDITIONS::NotEmpty );
ctxMenu.AddItem( SCH_ACTIONS::rotateCCW, orientCondition );
ctxMenu.AddItem( SCH_ACTIONS::rotateCW, orientCondition );
ctxMenu.AddItem( SCH_ACTIONS::mirrorX, orientCondition );
ctxMenu.AddItem( SCH_ACTIONS::mirrorY, orientCondition );
ctxMenu.AddItem( SCH_ACTIONS::duplicate, moveCondition );
ctxMenu.AddItem( SCH_ACTIONS::doDelete, SCH_CONDITIONS::NotEmpty );
moveMenu.AddItem( SCH_ACTIONS::properties, propertiesCondition );
moveMenu.AddItem( SCH_ACTIONS::editReference, singleComponentCondition );
moveMenu.AddItem( SCH_ACTIONS::editValue, singleComponentCondition );
moveMenu.AddItem( SCH_ACTIONS::editFootprint, singleComponentCondition );
moveMenu.AddItem( SCH_ACTIONS::convertDeMorgan, SCH_CONDITIONS::SingleDeMorganSymbol );
ctxMenu.AddItem( SCH_ACTIONS::properties, propertiesCondition );
ctxMenu.AddItem( SCH_ACTIONS::editReference, singleComponentCondition );
ctxMenu.AddItem( SCH_ACTIONS::editValue, singleComponentCondition );
ctxMenu.AddItem( SCH_ACTIONS::editFootprint, singleComponentCondition );
ctxMenu.AddItem( SCH_ACTIONS::convertDeMorgan, SCH_CONDITIONS::SingleDeMorganSymbol );
std::shared_ptr<SYMBOL_UNIT_MENU> symUnitMenu = std::make_shared<SYMBOL_UNIT_MENU>();
symUnitMenu->SetTool( this );
m_menu.AddSubMenu( symUnitMenu );
moveMenu.AddMenu( symUnitMenu.get(), false, SCH_CONDITIONS::SingleMultiUnitSymbol, 1 );
std::shared_ptr<SYMBOL_UNIT_MENU> symUnitMenu = std::make_shared<SYMBOL_UNIT_MENU>();
symUnitMenu->SetTool( this );
m_menu.AddSubMenu( symUnitMenu );
ctxMenu.AddMenu( symUnitMenu.get(), false, SCH_CONDITIONS::SingleMultiUnitSymbol, 1 );
ctxMenu.AddSeparator( SCH_CONDITIONS::IdleSelection );
ctxMenu.AddItem( SCH_ACTIONS::cut, SCH_CONDITIONS::IdleSelection );
ctxMenu.AddItem( SCH_ACTIONS::copy, SCH_CONDITIONS::IdleSelection );
ctxMenu.AddSeparator( SELECTION_CONDITIONS::NotEmpty, 1000 );
m_menu.AddStandardSubMenus( m_frame );
moveMenu.AddSeparator( SCH_CONDITIONS::IdleSelection );
moveMenu.AddItem( SCH_ACTIONS::cut, SCH_CONDITIONS::IdleSelection );
moveMenu.AddItem( SCH_ACTIONS::copy, SCH_CONDITIONS::IdleSelection );
}
//
// Add editing actions to the drawing tool menu
//
CONDITIONAL_MENU& drawingMenu = drawingTool->GetToolMenu().GetMenu();
CONDITIONAL_MENU& drawMenu = drawingTool->GetToolMenu().GetMenu();
drawingMenu.AddSeparator( SCH_CONDITIONS::NotEmpty, 200 );
drawingMenu.AddItem( SCH_ACTIONS::rotateCCW, orientCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::rotateCW, orientCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::mirrorX, orientCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::mirrorY, orientCondition, 200 );
drawMenu.AddSeparator( SCH_CONDITIONS::NotEmpty, 200 );
drawMenu.AddItem( SCH_ACTIONS::rotateCCW, orientCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::rotateCW, orientCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::mirrorX, orientCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::mirrorY, orientCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::properties, propertiesCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::editReference, singleComponentCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::editValue, singleComponentCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::editFootprint, singleComponentCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::convertDeMorgan, SCH_CONDITIONS::SingleDeMorganSymbol, 200 );
drawMenu.AddItem( SCH_ACTIONS::properties, propertiesCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::editReference, singleComponentCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::editValue, singleComponentCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::editFootprint, singleComponentCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::convertDeMorgan, SCH_CONDITIONS::SingleDeMorganSymbol, 200 );
std::shared_ptr<SYMBOL_UNIT_MENU> symUnitMenu2 = std::make_shared<SYMBOL_UNIT_MENU>();
symUnitMenu2->SetTool( drawingTool );
drawingTool->GetToolMenu().AddSubMenu( symUnitMenu2 );
drawingMenu.AddMenu( symUnitMenu2.get(), false, SCH_CONDITIONS::SingleMultiUnitSymbol, 1 );
drawMenu.AddMenu( symUnitMenu2.get(), false, SCH_CONDITIONS::SingleMultiUnitSymbol, 1 );
drawingMenu.AddItem( SCH_ACTIONS::toShapeSlash, entryCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::toShapeBackslash, entryCondition, 200 );
drawingMenu.AddItem( SCH_ACTIONS::toLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawingMenu.AddItem( SCH_ACTIONS::toHLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawingMenu.AddItem( SCH_ACTIONS::toGLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawingMenu.AddItem( SCH_ACTIONS::toText, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawingMenu.AddItem( SCH_ACTIONS::cleanupSheetPins, sheetTool && SCH_CONDITIONS::Idle, 200 );
drawingMenu.AddItem( SCH_ACTIONS::resizeSheet, sheetTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::toShapeSlash, entryCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::toShapeBackslash, entryCondition, 200 );
drawMenu.AddItem( SCH_ACTIONS::toLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::toHLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::toGLabel, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::toText, anyTextTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::cleanupSheetPins, sheetTool && SCH_CONDITIONS::Idle, 200 );
drawMenu.AddItem( SCH_ACTIONS::resizeSheet, sheetTool && SCH_CONDITIONS::Idle, 200 );
//
// Add editing actions to the selection tool menu
//
CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
selToolMenu.AddItem( SCH_ACTIONS::move, moveCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::drag, moveCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::rotateCCW, orientCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::rotateCW, orientCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::mirrorX, orientCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::mirrorY, orientCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::duplicate, moveCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::duplicate, duplicateCondition, 200 );
selToolMenu.AddItem( SCH_ACTIONS::doDelete, SCH_CONDITIONS::NotEmpty, 200 );
selToolMenu.AddItem( SCH_ACTIONS::properties, propertiesCondition, 200 );
@ -469,9 +442,9 @@ bool SCH_EDIT_TOOL::Init()
selToolMenu.AddItem( SCH_ACTIONS::resizeSheet, singleSheetCondition, 200 );
selToolMenu.AddSeparator( SCH_CONDITIONS::Idle, 200 );
selToolMenu.AddItem( SCH_ACTIONS::cut, SCH_CONDITIONS::IdleSelection, 200 );
selToolMenu.AddItem( SCH_ACTIONS::copy, SCH_CONDITIONS::IdleSelection, 200 );
selToolMenu.AddItem( SCH_ACTIONS::paste, SCH_CONDITIONS::Idle, 200 );
selToolMenu.AddItem( SCH_ACTIONS::cut, SCH_CONDITIONS::IdleSelection, 200 );
selToolMenu.AddItem( SCH_ACTIONS::copy, SCH_CONDITIONS::IdleSelection, 200 );
selToolMenu.AddItem( SCH_ACTIONS::paste, SCH_CONDITIONS::Idle, 200 );
return true;
}
@ -481,459 +454,12 @@ void SCH_EDIT_TOOL::Reset( RESET_REASON aReason )
{
if( aReason == MODEL_RELOAD )
{
m_moveInProgress = false;
m_moveOffset = { 0, 0 };
// Init variables used by every drawing tool
m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
m_controls = getViewControls();
m_frame = getEditFrame<SCH_EDIT_FRAME>();
}
}
int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
{
const KICAD_T movableItems[] =
{
SCH_MARKER_T,
SCH_JUNCTION_T,
SCH_NO_CONNECT_T,
SCH_BUS_BUS_ENTRY_T,
SCH_BUS_WIRE_ENTRY_T,
SCH_LINE_T,
SCH_BITMAP_T,
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,
EOT
};
KIGFX::VIEW_CONTROLS* controls = getViewControls();
controls->SetSnapping( true );
VECTOR2I originalCursorPos = controls->GetCursorPosition();
// Be sure that there is at least one item that we can move. If there's no selection try
// looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection).
SELECTION& selection = m_selectionTool->RequestSelection( movableItems );
EDA_ITEMS dragAdditions;
bool unselect = selection.IsHover();
if( selection.Empty() )
return 0;
if( aEvent.IsAction( &SCH_ACTIONS::move ) )
m_frame->SetToolID( ID_SCH_MOVE, wxCURSOR_DEFAULT, _( "Move Items" ) );
else
m_frame->SetToolID( ID_SCH_DRAG, wxCURSOR_DEFAULT, _( "Drag Items" ) );
Activate();
controls->ShowCursor( true );
controls->SetAutoPan( true );
bool restore_state = false;
bool chain_commands = false;
OPT_TOOL_EVENT evt = aEvent;
VECTOR2I prevPos;
if( m_moveInProgress )
{
// User must have switched from move to drag or vice-versa. Reset the moved items
// so we can start again with the current m_isDragOperation and m_moveOffset.
m_frame->RollbackSchematicFromUndo();
m_selectionTool->RemoveItemsFromSel( &dragAdditions, QUIET_MODE );
m_moveInProgress = false;
// And give it a kick so it doesn't have to wait for the first mouse movement to
// refresh.
m_toolMgr->RunAction( SCH_ACTIONS::refreshPreview );
return 0;
}
// Main loop: keep receiving events
do
{
controls->SetSnapping( !evt->Modifier( MD_ALT ) );
if( evt->IsAction( &SCH_ACTIONS::move ) || evt->IsAction( &SCH_ACTIONS::drag )
|| evt->IsMotion() || evt->IsDrag( BUT_LEFT )
|| evt->IsAction( &SCH_ACTIONS::refreshPreview ) )
{
if( !m_moveInProgress ) // Prepare to start moving/dragging
{
//------------------------------------------------------------------------
// Setup a drag or a move
//
for( SCH_ITEM* it = m_frame->GetScreen()->GetDrawItems(); it; it = it->Next() )
{
if( it->IsSelected() )
it->SetFlags( STARTPOINT | ENDPOINT | SELECTEDNODE );
else
it->ClearFlags( STARTPOINT | ENDPOINT | SELECTEDNODE );
}
// Add connections to the selection for a drag.
//
if( m_frame->GetToolId() == ID_SCH_DRAG )
{
for( EDA_ITEM* item : selection )
{
if( static_cast<SCH_ITEM*>( item )->IsConnectable() )
{
std::vector<wxPoint> connections;
static_cast<SCH_ITEM*>( item )->GetConnectionPoints( connections );
for( wxPoint point : connections )
getConnectedDragItems( (SCH_ITEM*) item, point, dragAdditions );
}
}
m_selectionTool->AddItemsToSel( &dragAdditions, QUIET_MODE );
for( EDA_ITEM* item : dragAdditions )
saveCopyInUndoList( (SCH_ITEM*) item, UR_CHANGED, true );
}
// Mark the edges of the block with dangling flags for a move.
//
if( m_frame->GetToolId() == ID_SCH_MOVE )
{
std::vector<DANGLING_END_ITEM> internalPoints;
for( EDA_ITEM* item : selection )
static_cast<SCH_ITEM*>( item )->GetEndPoints( internalPoints );
for( EDA_ITEM* item : selection )
static_cast<SCH_ITEM*>( item )->UpdateDanglingState( internalPoints );
}
// Generic setup
//
bool first = true;
for( EDA_ITEM* item : selection )
{
if( item->IsNew() || ( item->GetParent() && item->GetParent()->IsSelected() ) )
{
// already saved to undo
}
else
{
saveCopyInUndoList( (SCH_ITEM*) item, UR_CHANGED, !first );
first = false;
}
// Apply any initial offset in case we're coming from a previous command.
//
moveItem( (SCH_ITEM*) item, m_moveOffset, m_frame->GetToolId() == ID_SCH_DRAG );
}
// Set up the starting position and move/drag offset
//
m_cursor = controls->GetCursorPosition();
if( selection.HasReferencePoint() )
{
VECTOR2I delta = m_cursor - selection.GetReferencePoint();
// Drag items to the current cursor position
for( int i = 0; i < selection.GetSize(); ++i )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
// Don't double move pins, fields, etc.
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
moveItem( item, delta, m_frame->GetToolId() == ID_SCH_DRAG );
updateView( item );
}
selection.SetReferencePoint( m_cursor );
}
else if( selection.Size() == 1 )
{
// Set the current cursor position to the first dragged item origin,
// so the movement vector can be computed later
updateModificationPoint( selection );
m_cursor = originalCursorPos;
}
else
{
updateModificationPoint( selection );
}
controls->SetCursorPosition( m_cursor, false );
prevPos = m_cursor;
controls->SetAutoPan( true );
m_moveInProgress = true;
}
//------------------------------------------------------------------------
// Follow the mouse
//
m_cursor = controls->GetCursorPosition();
VECTOR2I delta( m_cursor - prevPos );
selection.SetReferencePoint( m_cursor );
m_moveOffset += delta;
prevPos = m_cursor;
for( EDA_ITEM* item : selection )
{
// Don't double move pins, fields, etc.
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
moveItem( (SCH_ITEM*) item, delta, m_frame->GetToolId() == ID_SCH_DRAG );
updateView( item );
}
m_frame->UpdateMsgPanel();
}
//------------------------------------------------------------------------
// Handle cancel
//
else if( TOOL_EVT_UTILS::IsCancelInteractive( evt.get() ) )
{
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
if( m_moveInProgress )
restore_state = true;
break;
}
//------------------------------------------------------------------------
// Handle TOOL_ACTION special cases
//
else if( evt->Action() == TA_UNDO_REDO_PRE )
{
unselect = true;
break;
}
else if( evt->Category() == TC_COMMAND )
{
if( evt->IsAction( &SCH_ACTIONS::doDelete ) )
{
// Exit on a remove operation; there is no further processing for removed items.
break;
}
else if( evt->IsAction( &SCH_ACTIONS::duplicate ) )
{
if( selection.Front()->IsNew() )
{
// This doesn't really make sense; we'll just end up dragging a stack of
// objects so Duplicate() is going to ignore this and we'll just carry on.
continue;
}
// Move original back and exit. The duplicate will run in its own loop.
restore_state = true;
unselect = false;
chain_commands = true;
break;
}
else if( evt->Action() == TA_CONTEXT_MENU_CHOICE )
{
if( evt->GetCommandId().get() >= ID_POPUP_SCH_SELECT_UNIT_CMP
&& evt->GetCommandId().get() <= ID_POPUP_SCH_SELECT_UNIT_CMP_MAX )
{
SCH_COMPONENT* component = dynamic_cast<SCH_COMPONENT*>( selection.Front() );
int unit = evt->GetCommandId().get() - ID_POPUP_SCH_SELECT_UNIT_CMP;
if( component )
{
m_frame->SelectUnit( component, unit );
m_toolMgr->RunAction( SCH_ACTIONS::refreshPreview );
}
}
}
}
//------------------------------------------------------------------------
// Handle context menu
//
else if( evt->IsClick( BUT_RIGHT ) )
{
m_menu.ShowContextMenu( selection );
}
//------------------------------------------------------------------------
// Handle drop
//
else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
{
break; // Finish
}
} while( ( evt = Wait() ) ); //Should be assignment not equality test
controls->ForceCursorPosition( false );
controls->ShowCursor( false );
controls->SetSnapping( false );
controls->SetAutoPan( false );
if( !chain_commands )
m_moveOffset = { 0, 0 };
m_moveInProgress = false;
m_frame->SetNoToolSelected();
selection.ClearReferencePoint();
for( auto item : selection )
item->ClearFlags( IS_MOVED );
if( unselect )
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
else
m_selectionTool->RemoveItemsFromSel( &dragAdditions, QUIET_MODE );
if( restore_state )
{
m_frame->RollbackSchematicFromUndo();
}
else
{
m_frame->CheckConnections( selection, true );
m_frame->SchematicCleanUp( true );
m_frame->TestDanglingEnds();
m_frame->OnModify();
}
return 0;
}
void SCH_EDIT_TOOL::getConnectedDragItems( SCH_ITEM* aItem, wxPoint aPoint, EDA_ITEMS& aList )
{
for( SCH_ITEM* test = m_frame->GetScreen()->GetDrawItems(); test; test = test->Next() )
{
if( test->IsSelected() || !test->IsConnectable() || !test->CanConnect( aItem ) )
continue;
switch( test->Type() )
{
default:
case SCH_LINE_T:
{
// Select wires/busses that are connected at one end and/or the other. Any
// unconnected ends must be flagged (STARTPOINT or ENDPOINT).
SCH_LINE* line = (SCH_LINE*) test;
if( line->GetStartPoint() == aPoint )
{
if( !( line->GetFlags() & SELECTEDNODE ) )
aList.push_back( line );
line->SetFlags( STARTPOINT | SELECTEDNODE );
}
else if( line->GetEndPoint() == aPoint )
{
if( !( line->GetFlags() & SELECTEDNODE ) )
aList.push_back( line );
line->SetFlags( ENDPOINT | SELECTEDNODE );
}
break;
}
case SCH_SHEET_T:
// Dragging a sheet just because it's connected to something else feels a bit like
// the tail wagging the dog, but this could be moved down to the next case.
break;
case SCH_COMPONENT_T:
case SCH_NO_CONNECT_T:
case SCH_JUNCTION_T:
// Select connected items that have no wire between them.
if( aItem->Type() != SCH_LINE_T && test->IsConnected( aPoint ) )
aList.push_back( test );
break;
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIER_LABEL_T:
case SCH_BUS_WIRE_ENTRY_T:
case SCH_BUS_BUS_ENTRY_T:
// Select labels and bus entries that are connected to a wire being moved.
if( aItem->Type() == SCH_LINE_T )
{
std::vector<wxPoint> connections;
test->GetConnectionPoints( connections );
for( wxPoint& point : connections )
{
if( aItem->HitTest( point ) )
aList.push_back( test );
}
}
break;
}
}
}
void SCH_EDIT_TOOL::moveItem( SCH_ITEM* aItem, VECTOR2I aDelta, bool isDrag )
{
switch( aItem->Type() )
{
case SCH_LINE_T:
if( aItem->GetFlags() & STARTPOINT )
static_cast<SCH_LINE*>( aItem )->MoveStart( (wxPoint) aDelta );
if( aItem->GetFlags() & ENDPOINT )
static_cast<SCH_LINE*>( aItem )->MoveEnd( (wxPoint) aDelta );
break;
case SCH_PIN_T:
case SCH_FIELD_T:
aItem->Move( wxPoint( aDelta.x, -aDelta.y ) );
break;
default:
aItem->Move( (wxPoint) aDelta );
break;
}
aItem->SetFlags( IS_MOVED );
}
bool SCH_EDIT_TOOL::updateModificationPoint( SELECTION& aSelection )
{
if( m_moveInProgress && aSelection.HasReferencePoint() )
return false;
// When there is only one item selected, the reference point is its position...
if( aSelection.Size() == 1 )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( aSelection.Front() );
// For some items, moving the cursor to anchor is not good (for instance large
// hierarchical sheets or components can have the anchor outside the view)
if( item->IsMovableFromAnchorPoint() )
{
wxPoint pos = item->GetPosition();
aSelection.SetReferencePoint( pos );
return true;
}
}
// ...otherwise modify items with regard to the grid-snapped cursor position
m_cursor = getViewControls()->GetCursorPosition( true );
aSelection.SetReferencePoint( m_cursor );
return true;
}
int SCH_EDIT_TOOL::Rotate( const TOOL_EVENT& aEvent )
{
SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::RotatableItems );
@ -1249,7 +775,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
for( unsigned ii = 0; ii < selection.GetSize(); ++ii )
{
SCH_ITEM* oldItem = static_cast<SCH_ITEM*>( selection.GetItem( ii ) );
SCH_ITEM* newItem = DuplicateItem( oldItem );
SCH_ITEM* newItem = oldItem->Duplicate();
newItem->SetFlags( IS_NEW );
newItems.push_back( newItem );
saveCopyInUndoList( newItem, UR_NEW, ii > 0 );
@ -1314,9 +840,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
m_toolMgr->RunAction( SCH_ACTIONS::addItemsToSel, true, &newItems );
TOOL_EVENT evt = SCH_ACTIONS::move.MakeEvent();
Main( evt );
m_toolMgr->RunAction( SCH_ACTIONS::move );
return 0;
}
@ -1339,7 +863,8 @@ int SCH_EDIT_TOOL::RepeatDrawItem( const TOOL_EVENT& aEvent )
{
( (SCH_COMPONENT*) newItem )->SetTimeStamp( GetNewTimeStamp() );
newItem->Move( (wxPoint)m_controls->GetCursorPosition( true ) - newItem->GetPosition() );
wxPoint cursorPos = (wxPoint) getViewControls()->GetCursorPosition( true );
newItem->Move( cursorPos - newItem->GetPosition() );
performDrag = true;
}
else
@ -1357,10 +882,7 @@ int SCH_EDIT_TOOL::RepeatDrawItem( const TOOL_EVENT& aEvent )
m_selectionTool->AddItemToSel( newItem );
if( performDrag )
{
TOOL_EVENT evt = SCH_ACTIONS::move.MakeEvent();
Main( evt );
}
m_toolMgr->RunAction( SCH_ACTIONS::move, true );
newItem->ClearFlags();
@ -1376,26 +898,56 @@ int SCH_EDIT_TOOL::RepeatDrawItem( const TOOL_EVENT& aEvent )
int SCH_EDIT_TOOL::DoDelete( const TOOL_EVENT& aEvent )
{
std::vector<SCH_ITEM*> items;
// get a copy instead of reference (we're going to clear the selection before removing items)
SELECTION selectionCopy = m_selectionTool->RequestSelection();
SCH_SCREEN* screen = m_frame->GetScreen();
auto items = m_selectionTool->RequestSelection().GetItems();
bool appendToUndo = false;
if( selectionCopy.Empty() )
if( items.empty() )
return 0;
// As we are about to remove items, they have to be removed from the selection first
// Don't leave a freed pointer in the selection
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
for( unsigned ii = 0; ii < selectionCopy.GetSize(); ii++ )
for( EDA_ITEM* item : items )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selectionCopy.GetItem( ii ) );
// Junctions, in particular, may have already been deleted if deleting wires made
// them redundant
if( item->GetEditFlags() & STRUCT_DELETED )
continue;
m_frame->DeleteItem( item, ii > 0 );
if( item->Type() == SCH_JUNCTION_T )
{
m_frame->DeleteJunction( (SCH_ITEM*) item, appendToUndo );
appendToUndo = true;
}
else
{
item->SetFlags( STRUCT_DELETED );
saveCopyInUndoList( item, UR_DELETED, appendToUndo );
appendToUndo = true;
if( item->Type() == SCH_SHEET_PIN_T )
static_cast<SCH_SHEET*>( item->GetParent() )->RemovePin( (SCH_SHEET_PIN*) item );
else
m_frame->RemoveFromScreen( (SCH_ITEM*) item );
SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item );
if( sch_item && sch_item->IsConnectable() )
{
std::vector< wxPoint > pts;
sch_item->GetConnectionPoints( pts );
for( auto point : pts )
{
SCH_ITEM* junction = screen->GetItem( point, 0, SCH_JUNCTION_T );
if( junction && !screen->IsJunctionNeeded( point ) )
m_frame->DeleteJunction( junction, appendToUndo );
}
}
updateView( (SCH_ITEM*) item );
}
}
m_frame->SetRepeatItem( nullptr );
@ -1687,7 +1239,7 @@ int SCH_EDIT_TOOL::ChangeTextType( const TOOL_EVENT& aEvent )
int SCH_EDIT_TOOL::BreakWire( const TOOL_EVENT& aEvent )
{
VECTOR2I cursorPos = m_controls->GetCursorPosition( !aEvent.Modifier( MD_ALT ) );
VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !aEvent.Modifier( MD_ALT ) );
if( m_frame->BreakSegments( (wxPoint) cursorPos ) )
{
@ -1740,21 +1292,19 @@ void SCH_EDIT_TOOL::updateView( EDA_ITEM* aItem )
}
void SCH_EDIT_TOOL::saveCopyInUndoList( SCH_ITEM* aItem, UNDO_REDO_T aType, bool aAppend )
void SCH_EDIT_TOOL::saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO_T aType, bool aAppend )
{
KICAD_T itemType = aItem->Type();
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
m_frame->SaveCopyInUndoList( (SCH_ITEM*)aItem->GetParent(), aType, aAppend );
m_frame->SaveCopyInUndoList( (SCH_ITEM*) aItem->GetParent(), UR_CHANGED, aAppend );
else
m_frame->SaveCopyInUndoList( aItem, aType, aAppend );
m_frame->SaveCopyInUndoList( (SCH_ITEM*) aItem, aType, aAppend );
}
void SCH_EDIT_TOOL::setTransitions()
{
Go( &SCH_EDIT_TOOL::Main, SCH_ACTIONS::move.MakeEvent() );
Go( &SCH_EDIT_TOOL::Main, SCH_ACTIONS::drag.MakeEvent() );
Go( &SCH_EDIT_TOOL::Duplicate, SCH_ACTIONS::duplicate.MakeEvent() );
Go( &SCH_EDIT_TOOL::RepeatDrawItem, SCH_ACTIONS::repeatDrawItem.MakeEvent() );
Go( &SCH_EDIT_TOOL::Rotate, SCH_ACTIONS::rotateCW.MakeEvent() );

View File

@ -48,13 +48,6 @@ public:
///> Get the SCH_DRAWING_TOOL top-level context menu
inline TOOL_MENU& GetToolMenu() { return m_menu; }
/**
* Function Main()
*
* Runs an interactive move of the selected items, or the item under the cursor.
*/
int Main( const TOOL_EVENT& aEvent );
int Rotate( const TOOL_EVENT& aEvent );
int Mirror( const TOOL_EVENT& aEvent );
@ -84,45 +77,23 @@ public:
int DeleteItemCursor( const TOOL_EVENT& aEvent );
private:
void moveItem( SCH_ITEM* aItem, VECTOR2I aDelta, bool isDrag );
///> Finds additional items for a drag operation.
///> Connected items with no wire are included (as there is no wire to adjust for the drag).
///> Connected wires are included with any un-connected ends flagged (STARTPOINT or ENDPOINT).
void getConnectedDragItems( SCH_ITEM* aItem, wxPoint aPoint, EDA_ITEMS& aList );
///> Returns the right modification point (e.g. for rotation), depending on the number of
///> selected items.
bool updateModificationPoint( SELECTION& aSelection );
///> Similar to getView()->Update(), but handles items that are redrawn by their parents.
void updateView( EDA_ITEM* );
///> Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their
///> parents.
void saveCopyInUndoList( SCH_ITEM*, UNDO_REDO_T aType, bool aAppend = false );
void saveCopyInUndoList( EDA_ITEM*, UNDO_REDO_T aType, bool aAppend = false );
///> Sets up handlers for various events.
void setTransitions() override;
private:
SCH_SELECTION_TOOL* m_selectionTool;
KIGFX::SCH_VIEW* m_view;
KIGFX::VIEW_CONTROLS* m_controls;
SCH_EDIT_FRAME* m_frame;
/// Menu model displayed by the tool.
TOOL_MENU m_menu;
///> Flag determining if anything is being dragged right now
bool m_moveInProgress;
///> Used for chaining commands
VECTOR2I m_moveOffset;
///> Last cursor position (needed for getModificationPoint() to avoid changes
///> of edit reference point).
VECTOR2I m_cursor;
};
#endif //KICAD_SCH_EDIT_TOOL_H

View File

@ -0,0 +1,599 @@
/*
* 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 <tool/tool_manager.h>
#include <tools/sch_edit_tool.h>
#include <tools/sch_selection_tool.h>
#include <tools/sch_drawing_tool.h>
#include <tools/sch_line_drawing_tool.h>
#include <tools/sch_picker_tool.h>
#include <sch_actions.h>
#include <hotkeys.h>
#include <bitmaps.h>
#include <base_struct.h>
#include <sch_item_struct.h>
#include <sch_component.h>
#include <sch_sheet.h>
#include <sch_view.h>
#include <sch_line.h>
#include <sch_edit_frame.h>
#include <eeschema_id.h>
#include "sch_move_tool.h"
TOOL_ACTION SCH_ACTIONS::move( "eeschema.InteractiveEdit.move",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_MOVE ),
_( "Move" ), _( "Moves the selected item(s)" ), move_xpm, AF_ACTIVATE );
TOOL_ACTION SCH_ACTIONS::drag( "eeschema.InteractiveEdit.drag",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_DRAG ),
_( "Drag" ), _( "Drags the selected item(s)" ), move_xpm, AF_ACTIVATE );
// For adding to or removing from selections
#define QUIET_MODE true
SCH_MOVE_TOOL::SCH_MOVE_TOOL() :
TOOL_INTERACTIVE( "eeschema.InteractiveMove" ),
m_selectionTool( nullptr ),
m_controls( nullptr ),
m_frame( nullptr ),
m_menu( *this ),
m_moveInProgress( false ),
m_moveOffset( 0, 0 )
{
}
SCH_MOVE_TOOL::~SCH_MOVE_TOOL()
{
}
bool SCH_MOVE_TOOL::Init()
{
m_frame = getEditFrame<SCH_EDIT_FRAME>();
m_selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
wxASSERT_MSG( m_selectionTool, "eeshema.InteractiveSelection tool is not available" );
auto activeTool = [ this ] ( const SELECTION& aSel ) {
return ( m_frame->GetToolId() != ID_NO_TOOL_SELECTED );
};
auto moveCondition = [] ( const SELECTION& aSel ) {
if( aSel.Empty() )
return false;
if( SCH_LINE_DRAWING_TOOL::IsDrawingLineWireOrBus( aSel ) )
return false;
return true;
};
//
// Build the tool menu
//
CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
ctxMenu.AddItem( ACTIONS::cancelInteractive, activeTool, 1 );
ctxMenu.AddSeparator( SELECTION_CONDITIONS::NotEmpty, 1000 );
m_menu.AddStandardSubMenus( m_frame );
//
// Add move actions to the selection tool menu
//
CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
selToolMenu.AddItem( SCH_ACTIONS::move, moveCondition, 150 );
selToolMenu.AddItem( SCH_ACTIONS::drag, moveCondition, 150 );
return true;
}
void SCH_MOVE_TOOL::Reset( RESET_REASON aReason )
{
if( aReason == MODEL_RELOAD )
{
m_moveInProgress = false;
m_moveOffset = { 0, 0 };
// Init variables used by every drawing tool
m_controls = getViewControls();
m_frame = getEditFrame<SCH_EDIT_FRAME>();
}
}
int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
{
const KICAD_T movableItems[] =
{
SCH_MARKER_T,
SCH_JUNCTION_T,
SCH_NO_CONNECT_T,
SCH_BUS_BUS_ENTRY_T,
SCH_BUS_WIRE_ENTRY_T,
SCH_LINE_T,
SCH_BITMAP_T,
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,
EOT
};
KIGFX::VIEW_CONTROLS* controls = getViewControls();
controls->SetSnapping( true );
VECTOR2I originalCursorPos = controls->GetCursorPosition();
// Be sure that there is at least one item that we can move. If there's no selection try
// looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection).
SELECTION& selection = m_selectionTool->RequestSelection( movableItems );
EDA_ITEMS dragAdditions;
bool unselect = selection.IsHover();
if( selection.Empty() )
return 0;
if( aEvent.IsAction( &SCH_ACTIONS::move ) )
m_frame->SetToolID( ID_SCH_MOVE, wxCURSOR_DEFAULT, _( "Move Items" ) );
else
m_frame->SetToolID( ID_SCH_DRAG, wxCURSOR_DEFAULT, _( "Drag Items" ) );
Activate();
controls->ShowCursor( true );
controls->SetAutoPan( true );
bool restore_state = false;
bool chain_commands = false;
OPT_TOOL_EVENT evt = aEvent;
VECTOR2I prevPos;
if( m_moveInProgress )
{
// User must have switched from move to drag or vice-versa. Reset the moved items
// so we can start again with the current m_isDragOperation and m_moveOffset.
m_frame->RollbackSchematicFromUndo();
m_selectionTool->RemoveItemsFromSel( &dragAdditions, QUIET_MODE );
m_moveInProgress = false;
// And give it a kick so it doesn't have to wait for the first mouse movement to
// refresh.
m_toolMgr->RunAction( SCH_ACTIONS::refreshPreview );
return 0;
}
// Main loop: keep receiving events
do
{
controls->SetSnapping( !evt->Modifier( MD_ALT ) );
if( evt->IsAction( &SCH_ACTIONS::move ) || evt->IsAction( &SCH_ACTIONS::drag )
|| evt->IsMotion() || evt->IsDrag( BUT_LEFT )
|| evt->IsAction( &SCH_ACTIONS::refreshPreview ) )
{
if( !m_moveInProgress ) // Prepare to start moving/dragging
{
//------------------------------------------------------------------------
// Setup a drag or a move
//
for( SCH_ITEM* it = m_frame->GetScreen()->GetDrawItems(); it; it = it->Next() )
{
if( it->IsSelected() )
it->SetFlags( STARTPOINT | ENDPOINT | SELECTEDNODE );
else
it->ClearFlags( STARTPOINT | ENDPOINT | SELECTEDNODE );
}
// Add connections to the selection for a drag.
//
if( m_frame->GetToolId() == ID_SCH_DRAG )
{
for( EDA_ITEM* item : selection )
{
if( static_cast<SCH_ITEM*>( item )->IsConnectable() )
{
std::vector<wxPoint> connections;
static_cast<SCH_ITEM*>( item )->GetConnectionPoints( connections );
for( wxPoint point : connections )
getConnectedDragItems( (SCH_ITEM*) item, point, dragAdditions );
}
}
m_selectionTool->AddItemsToSel( &dragAdditions, QUIET_MODE );
for( EDA_ITEM* item : dragAdditions )
saveCopyInUndoList( (SCH_ITEM*) item, UR_CHANGED, true );
}
// Mark the edges of the block with dangling flags for a move.
//
if( m_frame->GetToolId() == ID_SCH_MOVE )
{
std::vector<DANGLING_END_ITEM> internalPoints;
for( EDA_ITEM* item : selection )
static_cast<SCH_ITEM*>( item )->GetEndPoints( internalPoints );
for( EDA_ITEM* item : selection )
static_cast<SCH_ITEM*>( item )->UpdateDanglingState( internalPoints );
}
// Generic setup
//
bool first = true;
for( EDA_ITEM* item : selection )
{
if( item->IsNew() || ( item->GetParent() && item->GetParent()->IsSelected() ) )
{
// already saved to undo
}
else
{
saveCopyInUndoList( (SCH_ITEM*) item, UR_CHANGED, !first );
first = false;
}
// Apply any initial offset in case we're coming from a previous command.
//
moveItem( (SCH_ITEM*) item, m_moveOffset, m_frame->GetToolId() == ID_SCH_DRAG );
}
// Set up the starting position and move/drag offset
//
m_cursor = controls->GetCursorPosition();
if( selection.HasReferencePoint() )
{
VECTOR2I delta = m_cursor - selection.GetReferencePoint();
// Drag items to the current cursor position
for( int i = 0; i < selection.GetSize(); ++i )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
// Don't double move pins, fields, etc.
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
moveItem( item, delta, m_frame->GetToolId() == ID_SCH_DRAG );
updateView( item );
}
selection.SetReferencePoint( m_cursor );
}
else if( selection.Size() == 1 )
{
// Set the current cursor position to the first dragged item origin,
// so the movement vector can be computed later
updateModificationPoint( selection );
m_cursor = originalCursorPos;
}
else
{
updateModificationPoint( selection );
}
controls->SetCursorPosition( m_cursor, false );
prevPos = m_cursor;
controls->SetAutoPan( true );
m_moveInProgress = true;
}
//------------------------------------------------------------------------
// Follow the mouse
//
m_cursor = controls->GetCursorPosition();
VECTOR2I delta( m_cursor - prevPos );
selection.SetReferencePoint( m_cursor );
m_moveOffset += delta;
prevPos = m_cursor;
for( EDA_ITEM* item : selection )
{
// Don't double move pins, fields, etc.
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
moveItem( (SCH_ITEM*) item, delta, m_frame->GetToolId() == ID_SCH_DRAG );
updateView( item );
}
m_frame->UpdateMsgPanel();
}
//------------------------------------------------------------------------
// Handle cancel
//
else if( TOOL_EVT_UTILS::IsCancelInteractive( evt.get() ) )
{
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
if( m_moveInProgress )
restore_state = true;
break;
}
//------------------------------------------------------------------------
// Handle TOOL_ACTION special cases
//
else if( evt->Action() == TA_UNDO_REDO_PRE )
{
unselect = true;
break;
}
else if( evt->Category() == TC_COMMAND )
{
if( evt->IsAction( &SCH_ACTIONS::doDelete ) )
{
// Exit on a remove operation; there is no further processing for removed items.
break;
}
else if( evt->IsAction( &SCH_ACTIONS::duplicate ) )
{
if( selection.Front()->IsNew() )
{
// This doesn't really make sense; we'll just end up dragging a stack of
// objects so Duplicate() is going to ignore this and we'll just carry on.
continue;
}
// Move original back and exit. The duplicate will run in its own loop.
restore_state = true;
unselect = false;
chain_commands = true;
break;
}
else if( evt->Action() == TA_CONTEXT_MENU_CHOICE )
{
if( evt->GetCommandId().get() >= ID_POPUP_SCH_SELECT_UNIT_CMP
&& evt->GetCommandId().get() <= ID_POPUP_SCH_SELECT_UNIT_CMP_MAX )
{
SCH_COMPONENT* component = dynamic_cast<SCH_COMPONENT*>( selection.Front() );
int unit = evt->GetCommandId().get() - ID_POPUP_SCH_SELECT_UNIT_CMP;
if( component )
{
m_frame->SelectUnit( component, unit );
m_toolMgr->RunAction( SCH_ACTIONS::refreshPreview );
}
}
}
}
//------------------------------------------------------------------------
// Handle context menu
//
else if( evt->IsClick( BUT_RIGHT ) )
{
m_menu.ShowContextMenu( selection );
}
//------------------------------------------------------------------------
// Handle drop
//
else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
{
break; // Finish
}
} while( ( evt = Wait() ) ); //Should be assignment not equality test
controls->ForceCursorPosition( false );
controls->ShowCursor( false );
controls->SetSnapping( false );
controls->SetAutoPan( false );
if( !chain_commands )
m_moveOffset = { 0, 0 };
m_moveInProgress = false;
m_frame->SetNoToolSelected();
selection.ClearReferencePoint();
for( auto item : selection )
item->ClearFlags( IS_MOVED );
if( unselect )
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
else
m_selectionTool->RemoveItemsFromSel( &dragAdditions, QUIET_MODE );
if( restore_state )
{
m_frame->RollbackSchematicFromUndo();
}
else
{
m_frame->CheckConnections( selection, true );
m_frame->SchematicCleanUp( true );
m_frame->TestDanglingEnds();
m_frame->OnModify();
}
return 0;
}
void SCH_MOVE_TOOL::getConnectedDragItems( SCH_ITEM* aItem, wxPoint aPoint, EDA_ITEMS& aList )
{
for( SCH_ITEM* test = m_frame->GetScreen()->GetDrawItems(); test; test = test->Next() )
{
if( test->IsSelected() || !test->IsConnectable() || !test->CanConnect( aItem ) )
continue;
switch( test->Type() )
{
default:
case SCH_LINE_T:
{
// Select wires/busses that are connected at one end and/or the other. Any
// unconnected ends must be flagged (STARTPOINT or ENDPOINT).
SCH_LINE* line = (SCH_LINE*) test;
if( line->GetStartPoint() == aPoint )
{
if( !( line->GetFlags() & SELECTEDNODE ) )
aList.push_back( line );
line->SetFlags( STARTPOINT | SELECTEDNODE );
}
else if( line->GetEndPoint() == aPoint )
{
if( !( line->GetFlags() & SELECTEDNODE ) )
aList.push_back( line );
line->SetFlags( ENDPOINT | SELECTEDNODE );
}
break;
}
case SCH_SHEET_T:
// Dragging a sheet just because it's connected to something else feels a bit like
// the tail wagging the dog, but this could be moved down to the next case.
break;
case SCH_COMPONENT_T:
case SCH_NO_CONNECT_T:
case SCH_JUNCTION_T:
// Select connected items that have no wire between them.
if( aItem->Type() != SCH_LINE_T && test->IsConnected( aPoint ) )
aList.push_back( test );
break;
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIER_LABEL_T:
case SCH_BUS_WIRE_ENTRY_T:
case SCH_BUS_BUS_ENTRY_T:
// Select labels and bus entries that are connected to a wire being moved.
if( aItem->Type() == SCH_LINE_T )
{
std::vector<wxPoint> connections;
test->GetConnectionPoints( connections );
for( wxPoint& point : connections )
{
if( aItem->HitTest( point ) )
aList.push_back( test );
}
}
break;
}
}
}
void SCH_MOVE_TOOL::moveItem( SCH_ITEM* aItem, VECTOR2I aDelta, bool isDrag )
{
switch( aItem->Type() )
{
case SCH_LINE_T:
if( aItem->GetFlags() & STARTPOINT )
static_cast<SCH_LINE*>( aItem )->MoveStart( (wxPoint) aDelta );
if( aItem->GetFlags() & ENDPOINT )
static_cast<SCH_LINE*>( aItem )->MoveEnd( (wxPoint) aDelta );
break;
case SCH_PIN_T:
case SCH_FIELD_T:
aItem->Move( wxPoint( aDelta.x, -aDelta.y ) );
break;
default:
aItem->Move( (wxPoint) aDelta );
break;
}
aItem->SetFlags( IS_MOVED );
}
bool SCH_MOVE_TOOL::updateModificationPoint( SELECTION& aSelection )
{
if( m_moveInProgress && aSelection.HasReferencePoint() )
return false;
// When there is only one item selected, the reference point is its position...
if( aSelection.Size() == 1 )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( aSelection.Front() );
// For some items, moving the cursor to anchor is not good (for instance large
// hierarchical sheets or components can have the anchor outside the view)
if( item->IsMovableFromAnchorPoint() )
{
wxPoint pos = item->GetPosition();
aSelection.SetReferencePoint( pos );
return true;
}
}
// ...otherwise modify items with regard to the grid-snapped cursor position
m_cursor = getViewControls()->GetCursorPosition( true );
aSelection.SetReferencePoint( m_cursor );
return true;
}
void SCH_MOVE_TOOL::updateView( EDA_ITEM* aItem )
{
KICAD_T itemType = aItem->Type();
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
getView()->Update( aItem->GetParent() );
getView()->Update( aItem );
}
void SCH_MOVE_TOOL::saveCopyInUndoList( SCH_ITEM* aItem, UNDO_REDO_T aType, bool aAppend )
{
KICAD_T itemType = aItem->Type();
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
m_frame->SaveCopyInUndoList( (SCH_ITEM*)aItem->GetParent(), UR_CHANGED, aAppend );
else
m_frame->SaveCopyInUndoList( aItem, aType, aAppend );
}
void SCH_MOVE_TOOL::setTransitions()
{
Go( &SCH_MOVE_TOOL::Main, SCH_ACTIONS::move.MakeEvent() );
Go( &SCH_MOVE_TOOL::Main, SCH_ACTIONS::drag.MakeEvent() );
}

View File

@ -0,0 +1,99 @@
/*
* 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
*/
#ifndef KICAD_SCH_MOVE_TOOL_H
#define KICAD_SCH_MOVE_TOOL_H
#include <tool/tool_interactive.h>
#include <tool/tool_menu.h>
#include <sch_base_frame.h>
class SCH_EDIT_FRAME;
class SCH_SELECTION_TOOL;
class SCH_MOVE_TOOL : public TOOL_INTERACTIVE
{
public:
SCH_MOVE_TOOL();
~SCH_MOVE_TOOL();
/// @copydoc TOOL_INTERACTIVE::Init()
bool Init() override;
/// @copydoc TOOL_INTERACTIVE::Reset()
void Reset( RESET_REASON aReason ) override;
///> Get the SCH_DRAWING_TOOL top-level context menu
inline TOOL_MENU& GetToolMenu() { return m_menu; }
/**
* Function Main()
*
* Runs an interactive move of the selected items, or the item under the cursor.
*/
int Main( const TOOL_EVENT& aEvent );
private:
void moveItem( SCH_ITEM* aItem, VECTOR2I aDelta, bool isDrag );
///> Finds additional items for a drag operation.
///> Connected items with no wire are included (as there is no wire to adjust for the drag).
///> Connected wires are included with any un-connected ends flagged (STARTPOINT or ENDPOINT).
void getConnectedDragItems( SCH_ITEM* aItem, wxPoint aPoint, EDA_ITEMS& aList );
///> Returns the right modification point (e.g. for rotation), depending on the number of
///> selected items.
bool updateModificationPoint( SELECTION& aSelection );
///> Similar to getView()->Update(), but handles items that are redrawn by their parents.
void updateView( EDA_ITEM* );
///> Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their
///> parents.
void saveCopyInUndoList( SCH_ITEM*, UNDO_REDO_T aType, bool aAppend = false );
///> Sets up handlers for various events.
void setTransitions() override;
private:
SCH_SELECTION_TOOL* m_selectionTool;
KIGFX::VIEW_CONTROLS* m_controls;
SCH_EDIT_FRAME* m_frame;
/// Menu model displayed by the tool.
TOOL_MENU m_menu;
///> Flag determining if anything is being dragged right now
bool m_moveInProgress;
///> Used for chaining commands
VECTOR2I m_moveOffset;
///> Last cursor position (needed for getModificationPoint() to avoid changes
///> of edit reference point).
VECTOR2I m_cursor;
};
#endif //KICAD_SCH_MOVE_TOOL_H

View File

@ -116,8 +116,8 @@ typedef const INSPECTOR_FUNC& INSPECTOR; /// std::function passed to nested u
#define IS_DRAGGED (1 << 6) ///< Item being dragged
#define IS_DELETED (1 << 7)
#define IS_WIRE_IMAGE (1 << 8) ///< Item to be drawn as wireframe while editing
#define STARTPOINT (1 << 9)
#define ENDPOINT (1 << 10)
#define STARTPOINT (1 << 9) ///< When a line is selected, these flags indicate which
#define ENDPOINT (1 << 10) ///< ends. (Used to support dragging.)
#define SELECTED (1 << 11)
#define SELECTEDNODE (1 << 12) ///< flag indicating that the structure has already selected
#define STRUCT_DELETED (1 << 13) ///< flag indication structures to be erased