2017-05-10 23:55:03 +10:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-11-14 23:39:08 +00:00
|
|
|
* Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-05-10 23:55:03 +10: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>
|
2019-12-05 05:43:55 -08:00
|
|
|
#include <memory>
|
2017-05-10 23:55:03 +10:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2022-08-22 22:32:42 +01:00
|
|
|
#include <tools/position_relative_tool.h>
|
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
#include <tools/pcb_selection_tool.h>
|
|
|
|
#include <tools/pcb_picker_tool.h>
|
2017-05-10 23:55:03 +10:00
|
|
|
#include <dialogs/dialog_position_relative.h>
|
2018-08-22 19:05:40 +01:00
|
|
|
#include <status_popup.h>
|
2017-05-10 23:55:03 +10:00
|
|
|
#include <board_commit.h>
|
2017-06-12 16:58:47 +02:00
|
|
|
#include <confirm.h>
|
2021-06-03 19:05:43 +01:00
|
|
|
#include <collectors.h>
|
2021-06-06 15:03:10 -04:00
|
|
|
#include <pad.h>
|
2022-08-22 22:32:42 +01:00
|
|
|
#include <footprint.h>
|
|
|
|
#include <pcb_group.h>
|
2017-05-10 23:55:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
POSITION_RELATIVE_TOOL::POSITION_RELATIVE_TOOL() :
|
2019-05-12 12:49:58 +01:00
|
|
|
PCB_TOOL_BASE( "pcbnew.PositionRelative" ),
|
2021-07-19 19:56:05 -04:00
|
|
|
m_dialog( nullptr ),
|
|
|
|
m_selectionTool( nullptr ),
|
|
|
|
m_anchor_item( nullptr )
|
2017-05-10 23:55:03 +10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void POSITION_RELATIVE_TOOL::Reset( RESET_REASON aReason )
|
|
|
|
{
|
|
|
|
if( aReason != RUN )
|
2019-12-05 05:43:55 -08:00
|
|
|
m_commit = std::make_unique<BOARD_COMMIT>( this );
|
2017-05-10 23:55:03 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSITION_RELATIVE_TOOL::Init()
|
|
|
|
{
|
|
|
|
// Find the selection tool, so they can cooperate
|
2020-12-16 13:31:32 +00:00
|
|
|
m_selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
return m_selectionTool != nullptr;
|
2017-05-10 23:55:03 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSITION_RELATIVE_TOOL::PositionRelative( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2020-12-10 01:33:24 +00:00
|
|
|
PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
|
2018-06-26 19:03:57 +01:00
|
|
|
|
2018-09-25 15:23:38 +01:00
|
|
|
const auto& selection = m_selectionTool->RequestSelection(
|
2020-12-16 13:31:32 +00:00
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
|
2020-12-10 01:33:24 +00:00
|
|
|
{
|
2022-08-22 22:32:42 +01:00
|
|
|
sTool->FilterCollectorForHierarchy( aCollector, true );
|
|
|
|
sTool->FilterCollectorForMarkers( aCollector );
|
2020-12-10 01:33:24 +00:00
|
|
|
},
|
2021-01-11 11:23:26 -08:00
|
|
|
!m_isFootprintEditor /* prompt user regarding locked items */ );
|
2018-06-26 19:03:57 +01:00
|
|
|
|
2018-09-25 15:23:38 +01:00
|
|
|
if( selection.Empty() )
|
2017-05-10 23:55:03 +10:00
|
|
|
return 0;
|
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
m_selection = selection;
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2022-10-24 18:46:15 -04:00
|
|
|
// We prefer footprints, then pads, then anything else here.
|
|
|
|
EDA_ITEM* preferredItem = m_selection.GetTopLeftItem( true );
|
2022-08-22 22:32:42 +01:00
|
|
|
|
2022-10-24 18:46:15 -04:00
|
|
|
if( !preferredItem && m_selection.HasType( PCB_PAD_T ) )
|
2022-08-22 22:32:42 +01:00
|
|
|
{
|
2022-10-24 18:46:15 -04:00
|
|
|
PCB_SELECTION padsOnly = m_selection;
|
|
|
|
std::deque<EDA_ITEM*>& items = padsOnly.Items();
|
|
|
|
items.erase( std::remove_if( items.begin(), items.end(),
|
|
|
|
[]( const EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return aItem->Type() != PCB_PAD_T;
|
|
|
|
} ), items.end() );
|
|
|
|
|
|
|
|
preferredItem = padsOnly.GetTopLeftItem();
|
2022-08-22 22:32:42 +01:00
|
|
|
}
|
|
|
|
|
2022-10-24 18:46:15 -04:00
|
|
|
if( preferredItem )
|
|
|
|
m_selectionAnchor = preferredItem->GetPosition();
|
2021-01-17 14:25:43 -05:00
|
|
|
else
|
|
|
|
m_selectionAnchor = m_selection.GetTopLeftItem()->GetPosition();
|
|
|
|
|
2019-10-22 14:09:11 +02:00
|
|
|
// The dialog is not modal and not deleted between calls.
|
|
|
|
// It means some options can have changed since the last call.
|
|
|
|
// Therefore we need to rebuild it in case UI units have changed since the last call.
|
|
|
|
if( m_dialog && m_dialog->GetUserUnits() != editFrame->GetUserUnits() )
|
|
|
|
{
|
|
|
|
m_dialog->Destroy();
|
|
|
|
m_dialog = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
if( !m_dialog )
|
2022-11-17 00:49:26 +00:00
|
|
|
m_dialog = new DIALOG_POSITION_RELATIVE( editFrame );
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
m_dialog->Show( true );
|
2017-05-10 23:55:03 +10:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:42:22 +01:00
|
|
|
|
2021-12-29 16:30:11 -05:00
|
|
|
int POSITION_RELATIVE_TOOL::RelativeItemSelectionMove( const VECTOR2I& aPosAnchor,
|
|
|
|
const VECTOR2I& aTranslation )
|
2020-03-02 20:55:06 +00:00
|
|
|
{
|
2021-12-29 16:30:11 -05:00
|
|
|
VECTOR2I aggregateTranslation = aPosAnchor + aTranslation - GetSelectionAnchorPosition();
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2022-08-22 22:32:42 +01:00
|
|
|
for( EDA_ITEM* item : m_selection )
|
2018-09-24 00:42:22 +01:00
|
|
|
{
|
2018-10-09 22:04:55 -07:00
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2022-10-24 18:46:15 -04:00
|
|
|
if( item->Type() == PCB_PAD_T
|
|
|
|
&& !frame()->GetPcbNewSettings()->m_AllowFreePads
|
|
|
|
&& frame()->IsType( FRAME_PCB_EDITOR ) )
|
|
|
|
{
|
2018-10-09 22:04:55 -07:00
|
|
|
item = item->GetParent();
|
2022-10-24 18:46:15 -04:00
|
|
|
}
|
2018-10-09 22:04:55 -07:00
|
|
|
|
2018-09-24 00:42:22 +01:00
|
|
|
m_commit->Modify( item );
|
2022-08-22 22:32:42 +01:00
|
|
|
|
|
|
|
// If moving a group, record position of all the descendants for undo
|
|
|
|
if( item->Type() == PCB_GROUP_T )
|
|
|
|
{
|
|
|
|
PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
|
|
|
|
group->RunOnDescendants( [&]( BOARD_ITEM* bItem )
|
|
|
|
{
|
|
|
|
m_commit->Modify( bItem );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:42:22 +01:00
|
|
|
static_cast<BOARD_ITEM*>( item )->Move( aggregateTranslation );
|
2017-05-10 23:55:03 +10:00
|
|
|
}
|
|
|
|
|
2017-06-14 08:14:57 +02:00
|
|
|
m_commit->Push( _( "Position Relative" ) );
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
if( m_selection.IsHover() )
|
2017-05-10 23:55:03 +10:00
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
2019-05-08 19:56:03 +01:00
|
|
|
m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
|
2019-01-08 03:36:35 -08:00
|
|
|
|
|
|
|
canvas()->Refresh();
|
2017-05-10 23:55:03 +10:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSITION_RELATIVE_TOOL::SelectPositionRelativeItem( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2020-12-16 13:31:32 +00:00
|
|
|
PCB_PICKER_TOOL* picker = m_toolMgr->GetTool<PCB_PICKER_TOOL>();
|
|
|
|
STATUS_TEXT_POPUP statusPopup( frame() );
|
|
|
|
bool done = false;
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2019-07-19 20:14:13 -06:00
|
|
|
Activate();
|
|
|
|
|
2020-09-24 02:05:46 +01:00
|
|
|
statusPopup.SetText( _( "Click on reference item..." ) );
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
picker->SetClickHandler(
|
|
|
|
[&]( const VECTOR2D& aPoint ) -> bool
|
|
|
|
{
|
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
|
2020-12-16 13:31:32 +00:00
|
|
|
const PCB_SELECTION& sel = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector,
|
|
|
|
PCB_SELECTION_TOOL* sTool )
|
2020-11-14 19:16:42 +00:00
|
|
|
{
|
2020-03-02 20:55:06 +00:00
|
|
|
} );
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
if( sel.Empty() )
|
|
|
|
return true; // still looking for an item
|
2017-05-10 23:55:03 +10:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
m_anchor_item = sel.Front();
|
|
|
|
statusPopup.Hide();
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( sel.Front() );
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
return false; // got our item; don't need any more
|
|
|
|
} );
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
picker->SetMotionHandler(
|
|
|
|
[&] ( const VECTOR2D& aPos )
|
|
|
|
{
|
2019-07-17 21:19:23 +01:00
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
2019-07-16 00:44:01 +01:00
|
|
|
} );
|
2017-06-12 16:21:06 +02:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
picker->SetCancelHandler(
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
statusPopup.Hide();
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( m_anchor_item );
|
|
|
|
} );
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-17 21:19:23 +01:00
|
|
|
picker->SetFinalizeHandler(
|
|
|
|
[&]( const int& aFinalState )
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
} );
|
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
|
|
|
statusPopup.Popup();
|
2022-09-04 21:04:17 +01:00
|
|
|
canvas()->SetStatusPopup( statusPopup.GetPanel() );
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2023-05-01 11:01:48 -04:00
|
|
|
m_toolMgr->RunAction( ACTIONS::pickerTool, true, (void*) &aEvent );
|
2018-08-22 19:05:40 +01:00
|
|
|
|
2019-07-17 21:19:23 +01:00
|
|
|
while( !done )
|
2020-10-27 00:32:41 +00:00
|
|
|
{
|
|
|
|
// Pass events unless we receive a null event, then we must shut down
|
|
|
|
if( TOOL_EVENT* evt = Wait() )
|
|
|
|
evt->SetPassEvent();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2019-07-17 21:19:23 +01:00
|
|
|
|
2022-09-04 21:04:17 +01:00
|
|
|
canvas()->SetStatusPopup( nullptr );
|
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
return 0;
|
2017-06-12 16:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-31 14:30:51 +02:00
|
|
|
void POSITION_RELATIVE_TOOL::setTransitions()
|
2017-05-10 23:55:03 +10:00
|
|
|
{
|
|
|
|
Go( &POSITION_RELATIVE_TOOL::PositionRelative, PCB_ACTIONS::positionRelative.MakeEvent() );
|
|
|
|
Go( &POSITION_RELATIVE_TOOL::SelectPositionRelativeItem,
|
2021-07-19 19:56:05 -04:00
|
|
|
PCB_ACTIONS::selectpositionRelativeItem.MakeEvent() );
|
2017-05-10 23:55:03 +10:00
|
|
|
}
|