2019-05-08 18:56:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-08-14 08:28:07 +00:00
|
|
|
* Copyright (C) 2019 CERN
|
2021-01-08 23:24:12 +00:00
|
|
|
* Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2019-05-08 18:56:03 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2019-05-10 17:19:48 +00:00
|
|
|
#include <tools/ee_selection_tool.h>
|
|
|
|
#include <ee_actions.h>
|
2019-05-08 18:56:03 +00:00
|
|
|
#include <bitmaps.h>
|
2020-10-14 01:06:53 +00:00
|
|
|
#include <eda_item.h>
|
2020-12-12 03:13:52 +00:00
|
|
|
#include "symbol_editor_move_tool.h"
|
|
|
|
#include "symbol_editor_pin_tool.h"
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
SYMBOL_EDITOR_MOVE_TOOL::SYMBOL_EDITOR_MOVE_TOOL() :
|
2019-06-09 22:21:53 +00:00
|
|
|
EE_TOOL_BASE( "eeschema.SymbolMoveTool" ),
|
2019-05-08 18:56:03 +00:00
|
|
|
m_moveInProgress( false ),
|
|
|
|
m_moveOffset( 0, 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
bool SYMBOL_EDITOR_MOVE_TOOL::Init()
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2019-05-12 17:19:20 +00:00
|
|
|
EE_TOOL_BASE::Init();
|
2019-08-15 18:32:19 +00:00
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
//
|
|
|
|
// Add move actions to the selection tool menu
|
|
|
|
//
|
|
|
|
CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
|
|
|
|
|
2021-02-10 22:56:26 +00:00
|
|
|
auto canMove =
|
2021-01-08 23:24:12 +00:00
|
|
|
[&]( const SELECTION& sel )
|
|
|
|
{
|
|
|
|
SYMBOL_EDIT_FRAME* editor = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
|
|
|
|
wxCHECK( editor, false );
|
|
|
|
|
2021-02-10 22:56:26 +00:00
|
|
|
if( !editor->IsSymbolEditable() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( editor->IsSymbolAlias() )
|
|
|
|
{
|
|
|
|
for( EDA_ITEM* item : sel )
|
|
|
|
{
|
|
|
|
if( item->Type() != LIB_FIELD_T )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-01-08 23:24:12 +00:00
|
|
|
};
|
|
|
|
|
2021-02-10 22:56:26 +00:00
|
|
|
selToolMenu.AddItem( EE_ACTIONS::move, canMove && EE_CONDITIONS::IdleSelection, 150 );
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
void SYMBOL_EDITOR_MOVE_TOOL::Reset( RESET_REASON aReason )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2019-05-12 11:49:58 +00:00
|
|
|
EE_TOOL_BASE::Reset( aReason );
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
if( aReason == MODEL_RELOAD )
|
|
|
|
{
|
|
|
|
m_moveInProgress = false;
|
|
|
|
m_moveOffset = { 0, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
int SYMBOL_EDITOR_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2021-02-10 22:56:26 +00:00
|
|
|
static KICAD_T fieldsOnly[] = { LIB_FIELD_T, EOT };
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
KIGFX::VIEW_CONTROLS* controls = getViewControls();
|
2019-08-09 22:45:44 +00:00
|
|
|
|
|
|
|
m_anchorPos = { 0, 0 };
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
// 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).
|
2021-02-10 22:56:26 +00:00
|
|
|
EE_SELECTION& selection = m_frame->IsSymbolAlias()
|
|
|
|
? m_selectionTool->RequestSelection( fieldsOnly )
|
|
|
|
: m_selectionTool->RequestSelection();
|
2019-06-08 21:48:22 +00:00
|
|
|
bool unselect = selection.IsHover();
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2021-01-08 23:24:12 +00:00
|
|
|
if( !m_frame->IsSymbolEditable() || selection.Empty() || m_moveInProgress )
|
2019-05-08 18:56:03 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-07-15 12:15:58 +00:00
|
|
|
std::string tool = aEvent.GetCommandStr().get();
|
|
|
|
m_frame->PushTool( tool );
|
2019-05-08 18:56:03 +00:00
|
|
|
Activate();
|
2019-06-18 17:56:40 +00:00
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
controls->ShowCursor( true );
|
|
|
|
controls->SetAutoPan( true );
|
|
|
|
|
2019-06-17 13:43:22 +00:00
|
|
|
bool restore_state = false;
|
|
|
|
bool chain_commands = false;
|
|
|
|
TOOL_EVENT* evt = const_cast<TOOL_EVENT*>( &aEvent );
|
|
|
|
VECTOR2I prevPos;
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
if( !selection.Front()->IsNew() )
|
2020-08-26 18:04:32 +00:00
|
|
|
saveCopyInUndoList( m_frame->GetCurPart(), UNDO_REDO::LIBEDIT );
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2019-08-09 22:45:44 +00:00
|
|
|
m_cursor = controls->GetCursorPosition();
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
// Main loop: keep receiving events
|
|
|
|
do
|
|
|
|
{
|
2020-10-08 00:50:28 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::MOVING );
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2020-12-14 22:03:17 +00:00
|
|
|
if( evt->IsAction( &EE_ACTIONS::move )
|
|
|
|
|| evt->IsMotion()
|
|
|
|
|| evt->IsDrag( BUT_LEFT )
|
2020-04-23 22:57:34 +00:00
|
|
|
|| evt->IsAction( &ACTIONS::refreshPreview )
|
|
|
|
|| evt->IsAction( &EE_ACTIONS::symbolMoveActivate ) )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
|
|
|
if( !m_moveInProgress ) // Prepare to start moving/dragging
|
|
|
|
{
|
2020-12-18 23:26:37 +00:00
|
|
|
LIB_ITEM* lib_item = static_cast<LIB_ITEM*>( selection.Front() );
|
2019-08-09 22:45:44 +00:00
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
// Pick up any synchronized pins
|
|
|
|
//
|
2019-06-26 23:24:09 +00:00
|
|
|
// Careful when pasting. The pasted pin will be at the same location as it
|
|
|
|
// was copied from, leading us to believe it's a synchronized pin. It's not.
|
2020-12-18 23:26:37 +00:00
|
|
|
if( m_frame->SynchronizePins()
|
2019-08-09 22:45:44 +00:00
|
|
|
&& ( lib_item->GetEditFlags() & IS_PASTED ) == 0 )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2020-12-18 23:26:37 +00:00
|
|
|
std::set<LIB_PIN*> sync_pins;
|
2020-12-18 23:16:30 +00:00
|
|
|
|
2020-12-18 23:26:37 +00:00
|
|
|
for( EDA_ITEM* sel_item : selection )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2020-12-18 23:26:37 +00:00
|
|
|
lib_item = static_cast<LIB_ITEM*>( sel_item );
|
|
|
|
|
|
|
|
if( lib_item->Type() == LIB_PIN_T )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2020-12-18 23:26:37 +00:00
|
|
|
LIB_PIN* cur_pin = static_cast<LIB_PIN*>( lib_item );
|
|
|
|
LIB_PART* part = m_frame->GetCurPart();
|
|
|
|
std::vector<bool> got_unit( part->GetUnitCount() );
|
|
|
|
|
|
|
|
got_unit[cur_pin->GetUnit()] = true;
|
|
|
|
|
|
|
|
for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
|
|
|
|
{
|
|
|
|
if( !got_unit[pin->GetUnit()]
|
|
|
|
&& pin->GetPosition() == cur_pin->GetPosition()
|
|
|
|
&& pin->GetOrientation() == cur_pin->GetOrientation()
|
|
|
|
&& pin->GetConvert() == cur_pin->GetConvert()
|
|
|
|
&& pin->GetType() == cur_pin->GetType()
|
|
|
|
&& pin->GetName() == cur_pin->GetName() )
|
|
|
|
{
|
|
|
|
if( sync_pins.insert( pin ).second )
|
|
|
|
got_unit[pin->GetUnit()] = true;
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-18 23:26:37 +00:00
|
|
|
|
|
|
|
for( LIB_PIN* pin : sync_pins )
|
|
|
|
m_selectionTool->AddItemToSel( pin, true /*quiet mode*/ );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply any initial offset in case we're coming from a previous command.
|
|
|
|
//
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
moveItem( item, m_moveOffset );
|
|
|
|
|
|
|
|
// Set up the starting position and move/drag offset
|
|
|
|
//
|
|
|
|
m_cursor = controls->GetCursorPosition();
|
|
|
|
|
2019-08-27 12:12:34 +00:00
|
|
|
if( lib_item->IsNew() )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2019-08-09 22:45:44 +00:00
|
|
|
m_anchorPos = selection.GetReferencePoint();
|
|
|
|
VECTOR2I delta = m_cursor - mapCoords( m_anchorPos );
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
// Drag items to the current cursor position
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
{
|
|
|
|
moveItem( item, delta );
|
2020-10-30 15:15:20 +00:00
|
|
|
updateItem( item, false );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 22:45:44 +00:00
|
|
|
m_anchorPos = m_cursor;
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
2019-08-09 22:45:44 +00:00
|
|
|
else if( selection.Size() == 1 && m_frame->GetMoveWarpsCursor() )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2019-08-15 18:32:19 +00:00
|
|
|
wxPoint itemPos = lib_item->GetPosition();
|
|
|
|
m_anchorPos = wxPoint( itemPos.x, -itemPos.y );
|
|
|
|
|
2019-08-09 22:45:44 +00:00
|
|
|
getViewControls()->WarpCursor( m_anchorPos, true, true );
|
|
|
|
m_cursor = m_anchorPos;
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-09 22:45:44 +00:00
|
|
|
m_cursor = getViewControls()->GetCursorPosition( true );
|
|
|
|
m_anchorPos = m_cursor;
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
controls->SetCursorPosition( m_cursor, false );
|
2019-08-09 22:45:44 +00:00
|
|
|
m_toolMgr->PostEvent( EVENTS::SelectedItemsModified );
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
prevPos = m_cursor;
|
|
|
|
controls->SetAutoPan( true );
|
|
|
|
m_moveInProgress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Follow the mouse
|
|
|
|
//
|
|
|
|
m_cursor = controls->GetCursorPosition();
|
|
|
|
VECTOR2I delta( m_cursor - prevPos );
|
2019-08-09 22:45:44 +00:00
|
|
|
m_anchorPos = m_cursor;
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
m_moveOffset += delta;
|
|
|
|
prevPos = m_cursor;
|
|
|
|
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
{
|
|
|
|
moveItem( item, delta );
|
2020-10-30 15:15:20 +00:00
|
|
|
updateItem( item, false );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 18:40:49 +00:00
|
|
|
m_toolMgr->PostEvent( EVENTS::SelectedItemsMoved );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Handle cancel
|
|
|
|
//
|
2019-07-01 21:01:33 +00:00
|
|
|
else if( evt->IsCancelInteractive() || evt->IsActivate() )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
|
|
|
if( m_moveInProgress )
|
2020-08-25 11:53:39 +00:00
|
|
|
{
|
|
|
|
evt->SetPassEvent( false );
|
2019-05-08 18:56:03 +00:00
|
|
|
restore_state = true;
|
2020-08-25 11:53:39 +00:00
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Handle TOOL_ACTION special cases
|
|
|
|
//
|
|
|
|
else if( evt->Action() == TA_UNDO_REDO_PRE )
|
|
|
|
{
|
|
|
|
unselect = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( evt->Category() == TC_COMMAND )
|
|
|
|
{
|
2019-07-31 12:48:00 +00:00
|
|
|
if( evt->IsAction( &ACTIONS::doDelete ) )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
|
|
|
// Exit on a remove operation; there is no further processing for removed items.
|
|
|
|
break;
|
|
|
|
}
|
2019-05-27 10:40:52 +00:00
|
|
|
else if( evt->IsAction( &ACTIONS::duplicate ) )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-10-15 17:42:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
evt->SetPassEvent();
|
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Handle context menu
|
|
|
|
//
|
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
|
|
|
{
|
2019-05-19 21:04:04 +00:00
|
|
|
m_menu.ShowContextMenu( m_selectionTool->GetSelection() );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Handle drop
|
|
|
|
//
|
2020-12-14 22:03:17 +00:00
|
|
|
else if( evt->IsMouseUp( BUT_LEFT )
|
|
|
|
|| evt->IsClick( BUT_LEFT )
|
|
|
|
|| evt->IsDblClick( BUT_LEFT ) )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
|
|
|
if( selection.GetSize() == 1 && selection.Front()->Type() == LIB_PIN_T )
|
|
|
|
{
|
2020-12-12 03:13:52 +00:00
|
|
|
SYMBOL_EDITOR_PIN_TOOL* pinTool = m_toolMgr->GetTool<SYMBOL_EDITOR_PIN_TOOL>();
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if( !pinTool->PlacePin( (LIB_PIN*) selection.Front() ) )
|
|
|
|
restore_state = true;
|
|
|
|
}
|
|
|
|
catch( const boost::bad_pointer& e )
|
|
|
|
{
|
2019-05-08 18:56:03 +00:00
|
|
|
restore_state = true;
|
2021-01-13 16:23:09 +00:00
|
|
|
wxLogError( "Boost pointer exception occurred: \"%s\"", e.what() );
|
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break; // Finish
|
|
|
|
}
|
2019-07-26 18:16:44 +00:00
|
|
|
else
|
2020-10-15 17:42:51 +00:00
|
|
|
{
|
2019-07-26 18:16:44 +00:00
|
|
|
evt->SetPassEvent();
|
2020-10-15 17:42:51 +00:00
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2019-06-17 13:43:22 +00:00
|
|
|
} while( ( evt = Wait() ) ); // Assignment intentional; not equality test
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
controls->ForceCursorPosition( false );
|
|
|
|
controls->ShowCursor( false );
|
|
|
|
controls->SetAutoPan( false );
|
|
|
|
|
|
|
|
if( !chain_commands )
|
|
|
|
m_moveOffset = { 0, 0 };
|
|
|
|
|
2019-08-09 22:45:44 +00:00
|
|
|
m_anchorPos = { 0, 0 };
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
for( auto item : selection )
|
2019-05-11 10:06:28 +00:00
|
|
|
item->ClearEditFlags();
|
2019-05-08 18:56:03 +00:00
|
|
|
|
|
|
|
if( restore_state )
|
2019-08-09 22:45:44 +00:00
|
|
|
{
|
2020-11-15 16:08:31 +00:00
|
|
|
m_frame->RollbackSymbolFromUndo();
|
2019-08-09 22:45:44 +00:00
|
|
|
|
|
|
|
if( unselect )
|
|
|
|
m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
|
|
|
|
else
|
|
|
|
m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
|
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
else
|
2019-08-09 22:45:44 +00:00
|
|
|
{
|
|
|
|
if( unselect )
|
|
|
|
m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
m_frame->OnModify();
|
2019-08-09 22:45:44 +00:00
|
|
|
}
|
2019-05-08 18:56:03 +00:00
|
|
|
|
2019-06-15 00:29:42 +00:00
|
|
|
m_moveInProgress = false;
|
2019-07-15 12:15:58 +00:00
|
|
|
m_frame->PopTool( tool );
|
2019-05-08 18:56:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
void SYMBOL_EDITOR_MOVE_TOOL::moveItem( EDA_ITEM* aItem, VECTOR2I aDelta )
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2020-11-24 22:16:41 +00:00
|
|
|
static_cast<LIB_ITEM*>( aItem )->Offset( mapCoords( aDelta ) );
|
2019-05-08 18:56:03 +00:00
|
|
|
aItem->SetFlags( IS_MOVED );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 03:13:52 +00:00
|
|
|
void SYMBOL_EDITOR_MOVE_TOOL::setTransitions()
|
2019-05-08 18:56:03 +00:00
|
|
|
{
|
2020-12-12 03:13:52 +00:00
|
|
|
Go( &SYMBOL_EDITOR_MOVE_TOOL::Main, EE_ACTIONS::move.MakeEvent() );
|
|
|
|
Go( &SYMBOL_EDITOR_MOVE_TOOL::Main, EE_ACTIONS::symbolMoveActivate.MakeEvent() );
|
2019-05-08 18:56:03 +00:00
|
|
|
}
|