2017-05-10 13:55:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-06-12 14:21:06 +00:00
|
|
|
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-05-10 13:55: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 <functional>
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2017-06-12 14:58:47 +00:00
|
|
|
#include "position_relative_tool.h"
|
2017-05-10 13:55:03 +00:00
|
|
|
#include "pcb_actions.h"
|
|
|
|
#include "selection_tool.h"
|
2018-03-09 06:25:06 +00:00
|
|
|
#include "edit_tool.h"
|
2017-05-10 13:55:03 +00:00
|
|
|
#include "picker_tool.h"
|
|
|
|
|
|
|
|
#include <dialogs/dialog_position_relative.h>
|
2018-08-22 18:05:40 +00:00
|
|
|
#include <status_popup.h>
|
2017-05-10 13:55:03 +00:00
|
|
|
#include <board_commit.h>
|
2017-06-12 14:58:47 +00:00
|
|
|
#include <hotkeys.h>
|
|
|
|
#include <bitmaps.h>
|
|
|
|
#include <confirm.h>
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Position relative tool actions
|
|
|
|
|
|
|
|
TOOL_ACTION PCB_ACTIONS::positionRelative( "pcbnew.PositionRelative.positionRelative",
|
|
|
|
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_POSITION_RELATIVE ),
|
2018-05-18 11:24:02 +00:00
|
|
|
_( "Position Relative To..." ),
|
|
|
|
_( "Positions the selected item(s) by an exact amount relative to another" ),
|
2017-07-04 15:17:55 +00:00
|
|
|
move_relative_xpm );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
TOOL_ACTION PCB_ACTIONS::selectpositionRelativeItem(
|
|
|
|
"pcbnew.PositionRelative.selectpositionRelativeItem",
|
2017-10-23 07:20:10 +00:00
|
|
|
AS_GLOBAL, 0, "", "", nullptr );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2017-06-12 14:21:06 +00:00
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
POSITION_RELATIVE_TOOL::POSITION_RELATIVE_TOOL() :
|
2018-06-12 07:36:35 +00:00
|
|
|
PCB_TOOL( "pcbnew.PositionRelative" ),
|
2018-08-22 18:05:40 +00:00
|
|
|
m_dialog( NULL ),
|
2018-06-12 07:36:35 +00:00
|
|
|
m_selectionTool( NULL ),
|
|
|
|
m_anchor_item( NULL )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void POSITION_RELATIVE_TOOL::Reset( RESET_REASON aReason )
|
|
|
|
{
|
|
|
|
if( aReason != RUN )
|
|
|
|
m_commit.reset( new BOARD_COMMIT( this ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSITION_RELATIVE_TOOL::Init()
|
|
|
|
{
|
|
|
|
// Find the selection tool, so they can cooperate
|
2018-08-22 18:05:40 +00:00
|
|
|
m_selectionTool = m_toolMgr->GetTool<SELECTION_TOOL>();
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
return m_selectionTool != nullptr;
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSITION_RELATIVE_TOOL::PositionRelative( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2018-06-26 18:03:57 +00:00
|
|
|
PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
|
|
|
|
|
2018-09-25 14:23:38 +00:00
|
|
|
const auto& selection = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS ); } );
|
2018-06-26 18:03:57 +00:00
|
|
|
|
2018-09-25 14:23:38 +00:00
|
|
|
if( selection.Empty() )
|
2017-05-10 13:55:03 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
m_selection = selection;
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
if( !m_dialog )
|
|
|
|
m_dialog = new DIALOG_POSITION_RELATIVE( editFrame, m_translation, m_anchor );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
m_dialog->Show( true );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
int POSITION_RELATIVE_TOOL::RelativeItemSelectionMove( wxPoint aPosAnchor, wxPoint aTranslation )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
2018-09-23 23:42:22 +00:00
|
|
|
wxPoint aSelAnchor( INT_MAX, INT_MAX );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
// Find top-left item anchor in selection
|
2018-08-22 18:05:40 +00:00
|
|
|
for( auto item : m_selection )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
2018-09-23 23:42:22 +00:00
|
|
|
wxPoint itemAnchor = static_cast<BOARD_ITEM*>( item )->GetPosition();
|
|
|
|
|
|
|
|
if( EuclideanNorm( itemAnchor ) < EuclideanNorm( aSelAnchor ) )
|
|
|
|
aSelAnchor = itemAnchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxPoint aggregateTranslation = aPosAnchor + aTranslation - aSelAnchor;
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
for( auto item : m_selection )
|
|
|
|
{
|
|
|
|
m_commit->Modify( item );
|
|
|
|
static_cast<BOARD_ITEM*>( item )->Move( aggregateTranslation );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 06:14:57 +00:00
|
|
|
m_commit->Push( _( "Position Relative" ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
if( m_selection.IsHover() )
|
2017-05-10 13:55:03 +00:00
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectionModified, true );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSITION_RELATIVE_TOOL::SelectPositionRelativeItem( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
Activate();
|
|
|
|
|
|
|
|
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
|
2018-08-22 18:05:40 +00:00
|
|
|
STATUS_TEXT_POPUP statusPopup( frame() );
|
|
|
|
bool picking = true;
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
statusPopup.SetText( _( "Select reference item..." ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
picker->SetSnapping( false );
|
|
|
|
picker->Activate();
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
picker->SetClickHandler( [&]( const VECTOR2D& aPoint ) -> bool
|
|
|
|
{
|
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
|
2018-09-25 14:23:38 +00:00
|
|
|
const SELECTION& sel = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_TRANSIENTS ); } );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
if( sel.Empty() )
|
|
|
|
return true; // still looking for an item
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
m_anchor_item = sel.Front();
|
|
|
|
statusPopup.Hide();
|
|
|
|
|
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( sel.Front() );
|
|
|
|
|
|
|
|
picking = false;
|
|
|
|
return false; // got our item; don't need any more
|
|
|
|
} );
|
|
|
|
|
|
|
|
picker->SetCancelHandler( [&]()
|
|
|
|
{
|
|
|
|
statusPopup.Hide();
|
2017-06-12 14:21:06 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( m_anchor_item );
|
|
|
|
|
|
|
|
picking = false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
|
|
|
statusPopup.Popup();
|
|
|
|
|
|
|
|
while( picking )
|
|
|
|
{
|
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-06-12 14:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-31 12:30:51 +00:00
|
|
|
void POSITION_RELATIVE_TOOL::setTransitions()
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
Go( &POSITION_RELATIVE_TOOL::PositionRelative, PCB_ACTIONS::positionRelative.MakeEvent() );
|
|
|
|
Go( &POSITION_RELATIVE_TOOL::SelectPositionRelativeItem,
|
|
|
|
PCB_ACTIONS::selectpositionRelativeItem.MakeEvent() );
|
|
|
|
}
|