2017-05-10 13:55:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-11-17 00:49:26 +00:00
|
|
|
* Copyright (C) 2017-2022 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
|
|
|
|
*/
|
|
|
|
|
2019-02-23 13:58:57 +00:00
|
|
|
#include <dialogs/dialog_position_relative.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2019-02-23 13:58:57 +00:00
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
#include <widgets/tab_traversal.h>
|
|
|
|
#include <pcb_edit_frame.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <board_design_settings.h>
|
2020-10-14 03:37:48 +00:00
|
|
|
#include <trigo.h>
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
// initialise statics
|
|
|
|
DIALOG_POSITION_RELATIVE::POSITION_RELATIVE_OPTIONS DIALOG_POSITION_RELATIVE::m_options;
|
|
|
|
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
DIALOG_POSITION_RELATIVE::DIALOG_POSITION_RELATIVE( PCB_BASE_FRAME* aParent ) :
|
2017-05-10 13:55:03 +00:00
|
|
|
DIALOG_POSITION_RELATIVE_BASE( aParent ),
|
2018-08-22 18:05:40 +00:00
|
|
|
m_toolMgr( aParent->GetToolManager() ),
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset( aParent, m_xLabel, m_xEntry, m_xUnit ),
|
2020-02-29 02:21:18 +00:00
|
|
|
m_yOffset( aParent, m_yLabel, m_yEntry, m_yUnit ),
|
|
|
|
m_stateX( 0.0 ),
|
|
|
|
m_stateY( 0.0 ),
|
2022-01-18 23:20:48 +00:00
|
|
|
m_stateRadius( 0.0 )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
2019-08-26 17:36:35 +00:00
|
|
|
// We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
|
|
|
|
// implementation on MSW
|
|
|
|
m_tabOrder = {
|
|
|
|
m_xEntry,
|
|
|
|
m_yEntry,
|
|
|
|
m_stdButtonsOK,
|
|
|
|
m_stdButtonsCancel
|
|
|
|
};
|
2019-02-23 13:58:57 +00:00
|
|
|
|
2020-07-06 14:41:29 +00:00
|
|
|
// Configure display origin transforms
|
|
|
|
m_xOffset.SetCoordType( ORIGIN_TRANSFORMS::REL_X_COORD );
|
|
|
|
m_yOffset.SetCoordType( ORIGIN_TRANSFORMS::REL_Y_COORD );
|
|
|
|
|
2019-02-23 13:58:57 +00:00
|
|
|
SetInitialFocus( m_xEntry );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
// and set up the entries according to the saved options
|
|
|
|
m_polarCoords->SetValue( m_options.polarCoords );
|
2018-06-12 07:36:35 +00:00
|
|
|
updateDialogControls( m_polarCoords->IsChecked() );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
updateAnchorInfo( nullptr );
|
|
|
|
|
2020-02-29 02:21:18 +00:00
|
|
|
m_xOffset.SetDoubleValue( m_options.entry1 );
|
|
|
|
m_yOffset.SetDoubleValue( m_options.entry2 );
|
2018-05-18 11:24:02 +00:00
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons();
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
finishDialogSettings();
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-18 23:20:48 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::ToPolarDeg( double x, double y, double& r, EDA_ANGLE& q )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
// convert to polar coordinates
|
|
|
|
r = hypot( x, y );
|
|
|
|
|
2022-01-18 23:20:48 +00:00
|
|
|
q = ( r != 0) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
bool DIALOG_POSITION_RELATIVE::getTranslationInIU( VECTOR2I& val, bool polar )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
if( polar )
|
|
|
|
{
|
2022-01-18 23:20:48 +00:00
|
|
|
const double r = m_xOffset.GetDoubleValue();
|
|
|
|
const EDA_ANGLE q = m_yOffset.GetAngleValue();
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
val.x = KiROUND( r * q.Cos() );
|
|
|
|
val.y = KiROUND( r * q.Sin() );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// direct read
|
2022-11-17 00:49:26 +00:00
|
|
|
val.x = KiROUND( m_xOffset.GetDoubleValue() );
|
|
|
|
val.y = KiROUND( m_yOffset.GetDoubleValue() );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no validation to do here, but in future, you could return false here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnPolarChanged( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
bool newPolar = m_polarCoords->IsChecked();
|
2020-02-29 02:21:18 +00:00
|
|
|
double xOffset = m_xOffset.GetDoubleValue();
|
|
|
|
double yOffset = m_yOffset.GetDoubleValue();
|
2018-06-12 07:36:35 +00:00
|
|
|
updateDialogControls( newPolar );
|
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
if( newPolar )
|
|
|
|
{
|
2020-02-29 02:21:18 +00:00
|
|
|
if( xOffset != m_stateX || yOffset != m_stateY )
|
|
|
|
{
|
|
|
|
m_stateX = xOffset;
|
|
|
|
m_stateY = yOffset;
|
|
|
|
ToPolarDeg( m_stateX, m_stateY, m_stateRadius, m_stateTheta );
|
|
|
|
|
|
|
|
m_xOffset.SetDoubleValue( m_stateRadius );
|
|
|
|
m_stateRadius = m_xOffset.GetDoubleValue();
|
2022-01-18 23:20:48 +00:00
|
|
|
m_yOffset.SetAngleValue( m_stateTheta );
|
|
|
|
m_stateTheta = m_yOffset.GetAngleValue();
|
2020-02-29 02:21:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_xOffset.SetDoubleValue( m_stateRadius );
|
2022-01-18 23:20:48 +00:00
|
|
|
m_yOffset.SetAngleValue( m_stateTheta );
|
2020-02-29 02:21:18 +00:00
|
|
|
}
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-18 23:20:48 +00:00
|
|
|
if( xOffset != m_stateRadius || yOffset != m_stateTheta.AsDegrees() )
|
2020-02-29 02:21:18 +00:00
|
|
|
{
|
|
|
|
m_stateRadius = xOffset;
|
2022-01-18 23:20:48 +00:00
|
|
|
m_stateTheta = EDA_ANGLE( yOffset, DEGREES_T );
|
|
|
|
m_stateX = m_stateRadius * m_stateTheta.Cos();
|
|
|
|
m_stateY = m_stateRadius * m_stateTheta.Sin();
|
2020-02-29 02:21:18 +00:00
|
|
|
|
|
|
|
m_xOffset.SetDoubleValue( m_stateX );
|
|
|
|
m_stateX = m_xOffset.GetDoubleValue();
|
|
|
|
m_yOffset.SetDoubleValue( m_stateY );
|
|
|
|
m_stateY = m_yOffset.GetDoubleValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_xOffset.SetDoubleValue( m_stateX );
|
|
|
|
m_yOffset.SetDoubleValue( m_stateY );
|
|
|
|
}
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::updateDialogControls( bool aPolar )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
if( aPolar )
|
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetLabel( _( "Distance:" ) ); // Polar radius
|
|
|
|
m_yOffset.SetLabel( _( "Angle:" ) ); // Polar theta or angle
|
2019-12-20 14:11:39 +00:00
|
|
|
m_yOffset.SetUnits( EDA_UNITS::DEGREES );
|
2020-03-02 20:55:06 +00:00
|
|
|
m_clearX->SetToolTip( _( "Reset to the current distance from the reference position." ) );
|
|
|
|
m_clearY->SetToolTip( _( "Reset to the current angle from the reference position." ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetLabel( _( "Offset X:" ) );
|
|
|
|
m_yOffset.SetLabel( _( "Offset Y:" ) );
|
|
|
|
m_yOffset.SetUnits( GetUserUnits() );
|
2020-03-02 20:55:06 +00:00
|
|
|
m_clearX->SetToolTip( _( "Reset to the current X offset from the reference position." ) );
|
|
|
|
m_clearY->SetToolTip( _( "Reset to the current Y offset from the reference position." ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnClear( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxObject* obj = event.GetEventObject();
|
2020-03-02 20:55:06 +00:00
|
|
|
POSITION_RELATIVE_TOOL* posrelTool = m_toolMgr->GetTool<POSITION_RELATIVE_TOOL>();
|
|
|
|
wxASSERT( posrelTool );
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
VECTOR2I offset = posrelTool->GetSelectionAnchorPosition() - getAnchorPos();
|
2022-01-18 23:20:48 +00:00
|
|
|
double r;
|
|
|
|
EDA_ANGLE q;
|
2020-03-02 20:55:06 +00:00
|
|
|
ToPolarDeg( offset.x, offset.y, r, q );
|
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
if( obj == m_clearX )
|
|
|
|
{
|
2020-03-02 20:55:06 +00:00
|
|
|
m_stateX = offset.x;
|
|
|
|
m_xOffset.SetDoubleValue( r );
|
|
|
|
m_stateRadius = m_xOffset.GetDoubleValue();
|
|
|
|
|
|
|
|
if( m_polarCoords->IsChecked() )
|
|
|
|
m_xOffset.SetDoubleValue( m_stateRadius );
|
|
|
|
else
|
|
|
|
m_xOffset.SetValue( m_stateX );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else if( obj == m_clearY )
|
|
|
|
{
|
2020-03-02 20:55:06 +00:00
|
|
|
m_stateY = offset.y;
|
2022-01-18 23:20:48 +00:00
|
|
|
m_yOffset.SetAngleValue( q );
|
|
|
|
m_stateTheta = m_yOffset.GetAngleValue();
|
2020-03-02 20:55:06 +00:00
|
|
|
|
|
|
|
if( m_polarCoords->IsChecked() )
|
2022-01-18 23:20:48 +00:00
|
|
|
m_yOffset.SetAngleValue( m_stateTheta );
|
2020-03-02 20:55:06 +00:00
|
|
|
else
|
|
|
|
m_yOffset.SetValue( m_stateY );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnSelectItemClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
|
|
|
|
POSITION_RELATIVE_TOOL* posrelTool = m_toolMgr->GetTool<POSITION_RELATIVE_TOOL>();
|
2017-06-15 11:13:06 +00:00
|
|
|
wxASSERT( posrelTool );
|
2017-05-10 13:55:03 +00:00
|
|
|
m_toolMgr->RunAction( PCB_ACTIONS::selectpositionRelativeItem, true );
|
2018-08-22 18:05:40 +00:00
|
|
|
|
|
|
|
Hide();
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::updateAnchorInfo( BOARD_ITEM* aItem )
|
2018-11-12 23:08:47 +00:00
|
|
|
{
|
2022-11-17 00:49:26 +00:00
|
|
|
switch( m_options.anchorType )
|
|
|
|
{
|
|
|
|
case ANCHOR_GRID_ORIGIN:
|
|
|
|
m_referenceInfo->SetLabel( _( "Reference location: grid origin" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANCHOR_USER_ORIGIN:
|
|
|
|
m_referenceInfo->SetLabel( _( "Reference location: local coordinates origin" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANCHOR_ITEM:
|
|
|
|
{
|
|
|
|
UNITS_PROVIDER unitsProvider( pcbIUScale, GetUserUnits() );
|
|
|
|
wxString msg = _( "<none selected>" );
|
|
|
|
|
|
|
|
if( aItem )
|
2023-01-12 03:27:44 +00:00
|
|
|
msg = aItem->GetItemDescription( &unitsProvider );
|
2018-11-12 23:08:47 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
m_referenceInfo->SetLabel( wxString::Format( _( "Reference item: %s" ), msg ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 23:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
VECTOR2I DIALOG_POSITION_RELATIVE::getAnchorPos()
|
2018-11-12 23:08:47 +00:00
|
|
|
{
|
2022-11-17 00:49:26 +00:00
|
|
|
switch( m_options.anchorType )
|
|
|
|
{
|
|
|
|
case ANCHOR_GRID_ORIGIN:
|
|
|
|
return static_cast<BOARD*>( m_toolMgr->GetModel() )->GetDesignSettings().GetGridOrigin();
|
2018-11-12 23:08:47 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
case ANCHOR_USER_ORIGIN:
|
|
|
|
return static_cast<PCB_BASE_FRAME*>( m_toolMgr->GetToolHolder() )->GetScreen()->m_LocalOrigin;
|
|
|
|
|
|
|
|
case ANCHOR_ITEM:
|
|
|
|
return m_anchorItemPosition;
|
|
|
|
}
|
2022-11-17 07:39:42 +00:00
|
|
|
|
|
|
|
// Needed by some compilers to avoid a fatal compil error (no return value).
|
|
|
|
return m_anchorItemPosition;
|
2022-11-17 00:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnUseGridOriginClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
m_options.anchorType = ANCHOR_GRID_ORIGIN;
|
|
|
|
updateAnchorInfo( nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnUseUserOriginClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
m_options.anchorType = ANCHOR_USER_ORIGIN;
|
|
|
|
updateAnchorInfo( nullptr );
|
2018-11-12 23:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::UpdateAnchor( EDA_ITEM* aItem )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
2022-11-17 00:49:26 +00:00
|
|
|
BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
|
2018-06-12 07:36:35 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
m_options.anchorType = ANCHOR_ITEM;
|
|
|
|
updateAnchorInfo( item );
|
2018-06-12 07:36:35 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
if( item )
|
|
|
|
m_anchorItemPosition = item->GetPosition();
|
2018-06-12 07:36:35 +00:00
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
Show( true );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnOkClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
// for the output, we only deliver a Cartesian vector
|
2022-11-17 00:49:26 +00:00
|
|
|
VECTOR2I translation;
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
if( getTranslationInIU( translation, m_polarCoords->IsChecked() ) )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
// save the settings
|
|
|
|
m_options.polarCoords = m_polarCoords->GetValue();
|
2020-02-29 02:21:18 +00:00
|
|
|
m_options.entry1 = m_xOffset.GetDoubleValue();
|
|
|
|
m_options.entry2 = m_yOffset.GetDoubleValue();
|
2022-11-17 00:49:26 +00:00
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
POSITION_RELATIVE_TOOL* posrelTool = m_toolMgr->GetTool<POSITION_RELATIVE_TOOL>();
|
|
|
|
|
2022-11-17 00:49:26 +00:00
|
|
|
posrelTool->RelativeItemSelectionMove( getAnchorPos(), translation );
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnTextFocusLost( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
|
|
|
|
|
|
|
|
if( obj->GetValue().IsEmpty() )
|
|
|
|
obj->SetValue( "0" );
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|