2017-05-10 13:55:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-07-19 23:56:05 +00:00
|
|
|
* Copyright (C) 2017-2021 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>
|
2019-12-05 13:43:55 +00:00
|
|
|
#include <memory>
|
2017-05-10 13:55:03 +00:00
|
|
|
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"
|
2020-12-16 13:31:32 +00:00
|
|
|
#include "pcb_selection_tool.h"
|
2018-03-09 06:25:06 +00:00
|
|
|
#include "edit_tool.h"
|
2020-12-16 13:31:32 +00:00
|
|
|
#include "pcb_picker_tool.h"
|
2017-05-10 13:55:03 +00:00
|
|
|
#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 <bitmaps.h>
|
|
|
|
#include <confirm.h>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <collectors.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <pad.h>
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
POSITION_RELATIVE_TOOL::POSITION_RELATIVE_TOOL() :
|
2019-05-12 11:49:58 +00:00
|
|
|
PCB_TOOL_BASE( "pcbnew.PositionRelative" ),
|
2021-07-19 23:56:05 +00:00
|
|
|
m_dialog( nullptr ),
|
|
|
|
m_selectionTool( nullptr ),
|
|
|
|
m_anchor_item( nullptr )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void POSITION_RELATIVE_TOOL::Reset( RESET_REASON aReason )
|
|
|
|
{
|
|
|
|
if( aReason != RUN )
|
2019-12-05 13:43:55 +00:00
|
|
|
m_commit = std::make_unique<BOARD_COMMIT>( this );
|
2017-05-10 13:55:03 +00: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 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-17 19:25:43 +00:00
|
|
|
// TODO: Clean up this global once TOOL_EVENT supports std::functions as parameters
|
|
|
|
BOARD_ITEM* g_PositionRelativePadAnchor;
|
|
|
|
|
|
|
|
|
2017-05-10 13:55:03 +00: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 18:03:57 +00:00
|
|
|
|
2021-01-17 19:25:43 +00:00
|
|
|
g_PositionRelativePadAnchor = nullptr;
|
|
|
|
|
2018-09-25 14:23:38 +00: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
|
|
|
{
|
2021-01-15 03:14:00 +00:00
|
|
|
std::set<BOARD_ITEM*> to_add;
|
|
|
|
|
2020-12-15 22:32:02 +00:00
|
|
|
// Iterate from the back so we don't have to worry about removals.
|
|
|
|
for( int i = aCollector.GetCount() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* item = aCollector[i];
|
|
|
|
|
|
|
|
if( item->Type() == PCB_MARKER_T )
|
|
|
|
aCollector.Remove( item );
|
2021-01-15 03:14:00 +00:00
|
|
|
|
|
|
|
/// Locked pads do not get moved independently of the footprint
|
|
|
|
if( !sTool->IsFootprintEditor() && item->Type() == PCB_PAD_T && item->IsLocked()
|
|
|
|
&& !item->GetParent()->IsLocked() )
|
|
|
|
{
|
|
|
|
if( !aCollector.HasItem( item->GetParent() ) )
|
|
|
|
to_add.insert( item->GetParent() );
|
|
|
|
|
2021-01-17 19:25:43 +00:00
|
|
|
g_PositionRelativePadAnchor = item;
|
|
|
|
|
2021-01-15 03:14:00 +00:00
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
2020-12-15 22:32:02 +00:00
|
|
|
}
|
2021-01-15 03:14:00 +00:00
|
|
|
|
|
|
|
for( BOARD_ITEM* item : to_add )
|
|
|
|
aCollector.Append( item );
|
2020-12-10 01:33:24 +00:00
|
|
|
},
|
2021-01-11 19:23:26 +00:00
|
|
|
!m_isFootprintEditor /* prompt user regarding locked items */ );
|
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
|
|
|
|
2021-01-17 19:25:43 +00:00
|
|
|
if( g_PositionRelativePadAnchor )
|
|
|
|
m_selectionAnchor = static_cast<PAD*>( g_PositionRelativePadAnchor )->GetPosition();
|
|
|
|
else
|
|
|
|
m_selectionAnchor = m_selection.GetTopLeftItem()->GetPosition();
|
|
|
|
|
2019-10-22 12:09:11 +00: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 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
|
|
|
|
2021-07-26 19:35:12 +00:00
|
|
|
int POSITION_RELATIVE_TOOL::RelativeItemSelectionMove( const wxPoint& aPosAnchor,
|
|
|
|
const wxPoint& aTranslation )
|
2020-03-02 20:55:06 +00:00
|
|
|
{
|
|
|
|
wxPoint aggregateTranslation = aPosAnchor + aTranslation - GetSelectionAnchorPosition();
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
for( auto item : m_selection )
|
|
|
|
{
|
2018-10-10 05:04:55 +00:00
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && frame()->IsType( FRAME_PCB_EDITOR ) )
|
2018-10-10 05:04:55 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
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 );
|
|
|
|
|
2019-05-08 18:56:03 +00:00
|
|
|
m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
|
2019-01-08 11:36:35 +00:00
|
|
|
|
|
|
|
canvas()->Refresh();
|
2017-05-10 13:55:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSITION_RELATIVE_TOOL::SelectPositionRelativeItem( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2020-12-16 13:31:32 +00:00
|
|
|
std::string tool = "pcbnew.PositionRelative.selectReferenceItem";
|
|
|
|
PCB_PICKER_TOOL* picker = m_toolMgr->GetTool<PCB_PICKER_TOOL>();
|
|
|
|
STATUS_TEXT_POPUP statusPopup( frame() );
|
|
|
|
bool done = false;
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2019-07-20 02:14:13 +00:00
|
|
|
Activate();
|
|
|
|
|
2020-09-24 01:05:46 +00:00
|
|
|
statusPopup.SetText( _( "Click on reference item..." ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2019-07-15 23:44:01 +00: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 13:55:03 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
if( sel.Empty() )
|
|
|
|
return true; // still looking for an item
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
m_anchor_item = sel.Front();
|
|
|
|
statusPopup.Hide();
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( sel.Front() );
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
return false; // got our item; don't need any more
|
|
|
|
} );
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
picker->SetMotionHandler(
|
|
|
|
[&] ( const VECTOR2D& aPos )
|
|
|
|
{
|
2019-07-17 20:19:23 +00:00
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
2019-07-15 23:44:01 +00:00
|
|
|
} );
|
2017-06-12 14:21:06 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
picker->SetCancelHandler(
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
statusPopup.Hide();
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
if( m_dialog )
|
|
|
|
m_dialog->UpdateAnchor( m_anchor_item );
|
|
|
|
} );
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-17 20:19:23 +00:00
|
|
|
picker->SetFinalizeHandler(
|
|
|
|
[&]( const int& aFinalState )
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
} );
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
|
|
|
|
statusPopup.Popup();
|
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
|
2018-08-22 18:05:40 +00:00
|
|
|
|
2019-07-17 20:19:23 +00: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 20:19:23 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
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,
|
2021-07-19 23:56:05 +00:00
|
|
|
PCB_ACTIONS::selectpositionRelativeItem.MakeEvent() );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|