2017-05-10 13:55:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-06-12 07:36:35 +00:00
|
|
|
* Copyright (C) 2017-2018 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>
|
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
#include <widgets/tab_traversal.h>
|
|
|
|
#include <pcb_edit_frame.h>
|
2017-05-10 13:55:03 +00:00
|
|
|
|
|
|
|
// initialise statics
|
|
|
|
DIALOG_POSITION_RELATIVE::POSITION_RELATIVE_OPTIONS DIALOG_POSITION_RELATIVE::m_options;
|
|
|
|
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
DIALOG_POSITION_RELATIVE::DIALOG_POSITION_RELATIVE( PCB_BASE_FRAME* aParent, wxPoint& translation,
|
|
|
|
wxPoint& anchor ) :
|
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() ),
|
2017-05-10 13:55:03 +00:00
|
|
|
m_translation( translation ),
|
2018-08-22 18:05:40 +00:00
|
|
|
m_anchor_position( anchor ),
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset( aParent, m_xLabel, m_xEntry, m_xUnit ),
|
|
|
|
m_yOffset( aParent, m_yLabel, m_yEntry, m_yUnit )
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetValue( m_options.entry1 );
|
|
|
|
m_yOffset.SetValue( m_options.entry2 );
|
2018-05-18 11:24:02 +00:00
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
m_stdButtonsOK->SetDefault();
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
FinishDialogSettings();
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::ToPolarDeg( double x, double y, double& r, double& q )
|
|
|
|
{
|
|
|
|
// convert to polar coordinates
|
|
|
|
r = hypot( x, y );
|
|
|
|
|
|
|
|
q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
bool DIALOG_POSITION_RELATIVE::GetTranslationInIU ( wxPoint& val, bool polar )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
|
|
|
if( polar )
|
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
const int r = m_xOffset.GetValue();
|
|
|
|
const double q = m_yOffset.GetValue();
|
2017-05-10 13:55:03 +00:00
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
val.x = r * cos( DEG2RAD( q / 10.0 ) );
|
|
|
|
val.y = r * sin( DEG2RAD( q / 10.0 ) );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// direct read
|
2018-06-12 07:36:35 +00:00
|
|
|
val.x = m_xOffset.GetValue();
|
|
|
|
val.y = m_yOffset.GetValue();
|
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();
|
|
|
|
wxPoint val;
|
|
|
|
|
|
|
|
// get the value as previously stored
|
|
|
|
GetTranslationInIU( val, !newPolar );
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
// now switch the controls to the new representations
|
|
|
|
updateDialogControls( newPolar );
|
|
|
|
|
2017-05-10 13:55:03 +00:00
|
|
|
if( newPolar )
|
|
|
|
{
|
|
|
|
// convert to polar coordinates
|
|
|
|
double r, q;
|
|
|
|
ToPolarDeg( val.x, val.y, r, q );
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetValue( KiROUND( r / 10.0) * 10 );
|
|
|
|
m_yOffset.SetValue( q * 10 );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// vector is already in Cartesian, so just render out
|
|
|
|
// note - round off the last decimal place (10nm) to prevent
|
|
|
|
// (some) rounding causing errors when round-tripping
|
|
|
|
// you can never eliminate entirely, however
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetValue( KiROUND( val.x / 10.0 ) * 10 );
|
|
|
|
m_yOffset.SetValue( KiROUND( val.y / 10.0 ) * 10 );
|
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
|
|
|
|
m_yOffset.SetUnits( DEGREES );
|
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() );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnClear( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxObject* obj = event.GetEventObject();
|
|
|
|
|
|
|
|
if( obj == m_clearX )
|
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
m_xOffset.SetValue( 0 );
|
2017-05-10 13:55:03 +00:00
|
|
|
}
|
|
|
|
else if( obj == m_clearY )
|
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
m_yOffset.SetValue( 0 );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 23:08:47 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::OnUseGridOriginClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
BOARD* board = (BOARD*) m_toolMgr->GetModel();
|
|
|
|
|
|
|
|
m_anchor_position = board->GetGridOrigin();
|
|
|
|
m_referenceInfo->SetLabel( _( "Reference location: grid origin" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_POSITION_RELATIVE::OnUseUserOriginClick( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
|
|
|
|
|
2019-06-13 13:34:38 +00:00
|
|
|
m_anchor_position = (wxPoint) frame->GetScreen()->m_LocalOrigin;
|
2018-11-12 23:08:47 +00:00
|
|
|
m_referenceInfo->SetLabel( _( "Reference location: local coordinates origin" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
void DIALOG_POSITION_RELATIVE::UpdateAnchor( EDA_ITEM* aItem )
|
2017-05-10 13:55:03 +00:00
|
|
|
{
|
2018-06-12 07:36:35 +00:00
|
|
|
wxString reference = _( "<none selected>" );
|
2019-01-03 16:49:25 +00:00
|
|
|
BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
|
2018-06-12 07:36:35 +00:00
|
|
|
|
2019-01-03 16:49:25 +00:00
|
|
|
if( item )
|
2018-06-12 07:36:35 +00:00
|
|
|
{
|
2019-01-03 16:49:25 +00:00
|
|
|
m_anchor_position = item->GetPosition();
|
|
|
|
reference = item->GetSelectMenuText( GetUserUnits() );
|
2018-06-12 07:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 08:43:40 +00:00
|
|
|
m_referenceInfo->SetLabel( wxString::Format( "Reference item: %s", reference ) );
|
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
|
|
|
|
bool ok = GetTranslationInIU( m_translation, m_polarCoords->IsChecked() );
|
|
|
|
|
|
|
|
if( ok )
|
|
|
|
{
|
|
|
|
// save the settings
|
|
|
|
m_options.polarCoords = m_polarCoords->GetValue();
|
2018-06-12 07:36:35 +00:00
|
|
|
m_options.entry1 = m_xOffset.GetValue();
|
|
|
|
m_options.entry2 = m_yOffset.GetValue();
|
2017-05-10 13:55:03 +00:00
|
|
|
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
|
|
|
|
2018-09-23 23:42:22 +00:00
|
|
|
posrelTool->RelativeItemSelectionMove( m_anchor_position, m_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();
|
|
|
|
}
|