2013-02-26 11:25:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-05-02 14:15:24 +00:00
|
|
|
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-02-26 11:25:30 +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
|
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
/**
|
|
|
|
* @file dialog_set_grid.cpp
|
|
|
|
* @brief Manage user grid.
|
|
|
|
*/
|
2009-02-04 15:25:03 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <dialog_set_grid_base.h>
|
2015-04-30 08:46:06 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
#include <base_units.h>
|
|
|
|
#include <convert_to_biu.h>
|
|
|
|
#include <common.h>
|
|
|
|
|
|
|
|
#include <wxBasePcbFrame.h>
|
|
|
|
#include <class_drawpanel.h>
|
2015-04-10 23:43:15 +00:00
|
|
|
#include <class_draw_panel_gal.h>
|
2016-05-02 14:15:24 +00:00
|
|
|
|
|
|
|
#include <gal/graphics_abstraction_layer.h>
|
2015-06-30 12:08:03 +00:00
|
|
|
#include <tools/common_actions.h>
|
2016-05-02 14:15:24 +00:00
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
2008-12-05 16:03:05 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
// Max values for grid size
|
2016-05-02 14:15:24 +00:00
|
|
|
static const double MAX_GRID_SIZE = 50.0 * IU_PER_MM;
|
|
|
|
static const double MIN_GRID_SIZE = 0.001 * IU_PER_MM;
|
2015-06-28 14:11:57 +00:00
|
|
|
|
|
|
|
// Min/Max value for grid offset
|
2016-05-02 14:15:24 +00:00
|
|
|
static const double MAX_GRID_OFFSET = INT_MAX / 2.0;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2010-07-14 13:24:36 +00:00
|
|
|
class DIALOG_SET_GRID : public DIALOG_SET_GRID_BASE
|
2010-07-13 10:42:32 +00:00
|
|
|
{
|
2016-07-14 14:25:35 +00:00
|
|
|
PCB_BASE_FRAME* m_parent;
|
|
|
|
wxArrayString m_fast_grid_opts;
|
|
|
|
|
2010-07-14 13:24:36 +00:00
|
|
|
public:
|
2013-07-09 05:48:26 +00:00
|
|
|
/// This has no dependencies on calling wxFrame derivative, such as PCB_BASE_FRAME.
|
2016-05-02 14:15:24 +00:00
|
|
|
DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices );
|
|
|
|
|
|
|
|
bool TransferDataFromWindow();
|
|
|
|
bool TransferDataToWindow();
|
2010-07-14 13:24:36 +00:00
|
|
|
|
|
|
|
private:
|
2016-07-14 14:25:35 +00:00
|
|
|
void OnResetGridOrgClick( wxCommandEvent& event );
|
|
|
|
void OnInitDlg( wxInitDialogEvent& event )
|
|
|
|
{
|
|
|
|
// Call the default wxDialog handler of a wxInitDialogEvent
|
|
|
|
TransferDataToWindow();
|
2009-02-07 20:11:09 +00:00
|
|
|
|
2016-07-14 14:25:35 +00:00
|
|
|
// Now all widgets have the size fixed, call FinishDialogSettings
|
|
|
|
FinishDialogSettings();
|
|
|
|
}
|
2013-06-05 14:24:03 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void setGridUnits( EDA_UNITS_T units );
|
|
|
|
EDA_UNITS_T getGridUnits();
|
2008-12-05 16:03:05 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void setGridSize( const wxRealPoint& grid );
|
2015-06-28 14:11:57 +00:00
|
|
|
bool getGridSize( wxRealPoint& aGrisSize );
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void setGridOrigin( const wxPoint& grid );
|
2015-06-28 14:11:57 +00:00
|
|
|
bool getGridOrigin( wxPoint& aGridOrigin );
|
2011-09-09 11:02:03 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 );
|
|
|
|
void getGridForFastSwitching( int& aGrid1, int& aGrid2 );
|
2016-05-02 14:15:24 +00:00
|
|
|
|
|
|
|
void setGridStyle( KIGFX::GRID_STYLE aStyle );
|
|
|
|
KIGFX::GRID_STYLE getGridStyle() const;
|
2013-07-09 05:48:26 +00:00
|
|
|
};
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
DIALOG_SET_GRID::DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices ):
|
|
|
|
DIALOG_SET_GRID_BASE( aParent ),
|
|
|
|
m_parent( aParent ),
|
|
|
|
m_fast_grid_opts( aGridChoices )
|
2013-07-09 05:48:26 +00:00
|
|
|
{
|
2016-07-14 14:25:35 +00:00
|
|
|
m_sdbSizerOK->SetDefault(); // set OK button as default response to 'Enter' key
|
2011-09-09 11:02:03 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
m_TextPosXUnits->SetLabel( GetUnitsLabel( m_parent->m_UserGridUnit ) );
|
|
|
|
m_TextPosYUnits->SetLabel( GetUnitsLabel( m_parent->m_UserGridUnit ) );
|
2010-07-13 10:42:32 +00:00
|
|
|
|
2016-07-14 14:25:35 +00:00
|
|
|
FixOSXCancelButtonIssue();
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
bool DIALOG_SET_GRID::TransferDataFromWindow()
|
|
|
|
{
|
|
|
|
// Validate new settings
|
|
|
|
wxRealPoint gridSize;
|
|
|
|
if( !getGridSize( gridSize ) )
|
|
|
|
{
|
|
|
|
wxMessageBox( wxString::Format( _( "Incorrect grid size "
|
|
|
|
"(size must be >= %.3f mm and <= %.3f mm)" ),
|
|
|
|
MIN_GRID_SIZE/IU_PER_MM, MAX_GRID_SIZE/IU_PER_MM ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxPoint gridOrigin;
|
|
|
|
if( !getGridOrigin( gridOrigin ) )
|
|
|
|
{
|
|
|
|
wxMessageBox( wxString::Format( _( "Incorrect grid origin "
|
|
|
|
"(coordinates must be >= %.3f mm and <= %.3f mm)" ),
|
|
|
|
-MAX_GRID_OFFSET/IU_PER_MM, MAX_GRID_OFFSET/IU_PER_MM ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fastGrid1, fastGrid2;
|
|
|
|
getGridForFastSwitching( fastGrid1, fastGrid2 );
|
|
|
|
|
|
|
|
EDA_UNITS_T units = getGridUnits();
|
|
|
|
|
|
|
|
// Apply the new settings
|
|
|
|
|
|
|
|
// Because grid origin is saved in board, show as modified
|
|
|
|
m_parent->OnModify();
|
|
|
|
m_parent->SetGridOrigin( gridOrigin );
|
|
|
|
m_parent->m_UserGridUnit = units;
|
|
|
|
m_parent->m_UserGridSize = gridSize;
|
|
|
|
m_parent->m_FastGrid1 = fastGrid1;
|
|
|
|
m_parent->m_FastGrid2 = fastGrid2;
|
|
|
|
|
|
|
|
// User grid
|
|
|
|
BASE_SCREEN* screen = m_parent->GetScreen();
|
|
|
|
screen->AddGrid( gridSize, units, ID_POPUP_GRID_USER );
|
|
|
|
|
|
|
|
// If the user grid is the current option, recall SetGrid()
|
|
|
|
// to force new values put in list as current grid value
|
|
|
|
if( screen->GetGridCmdId() == ID_POPUP_GRID_USER )
|
|
|
|
screen->SetGrid( ID_POPUP_GRID_USER );
|
|
|
|
|
|
|
|
// Notify GAL
|
|
|
|
TOOL_MANAGER* mgr = m_parent->GetToolManager();
|
|
|
|
|
|
|
|
if( mgr && m_parent->IsGalCanvasActive() )
|
|
|
|
{
|
|
|
|
mgr->RunAction( "common.Control.gridPreset", true,
|
|
|
|
screen->GetGridCmdId() - ID_POPUP_GRID_LEVEL_1000 );
|
|
|
|
|
|
|
|
TOOL_EVENT gridOriginUpdate = COMMON_ACTIONS::gridSetOrigin.MakeEvent();
|
|
|
|
gridOriginUpdate.SetParameter( new VECTOR2D( gridOrigin ) );
|
|
|
|
mgr->ProcessEvent( gridOriginUpdate );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_parent->GetGalCanvas()->GetGAL()->SetGridStyle( getGridStyle() );
|
|
|
|
m_parent->GetCanvas()->Refresh();
|
|
|
|
|
|
|
|
return wxDialog::TransferDataFromWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_SET_GRID::TransferDataToWindow()
|
|
|
|
{
|
|
|
|
setGridUnits( m_parent->m_UserGridUnit );
|
|
|
|
setGridSize( m_parent->m_UserGridSize );
|
|
|
|
setGridOrigin( m_parent->GetGridOrigin() );
|
|
|
|
setGridForFastSwitching( m_fast_grid_opts, m_parent->m_FastGrid1, m_parent->m_FastGrid2 );
|
|
|
|
setGridStyle( m_parent->GetGalCanvas()->GetGAL()->GetGridStyle() );
|
|
|
|
|
|
|
|
return wxDialog::TransferDataToWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void DIALOG_SET_GRID::setGridUnits( EDA_UNITS_T aUnits )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
m_UnitGrid->SetSelection( aUnits != INCHES );
|
2013-07-09 05:48:26 +00:00
|
|
|
}
|
2010-07-13 10:42:32 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
|
|
|
|
EDA_UNITS_T DIALOG_SET_GRID::getGridUnits()
|
|
|
|
{
|
|
|
|
return m_UnitGrid->GetSelection() == 0 ? INCHES : MILLIMETRES;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void DIALOG_SET_GRID::setGridSize( const wxRealPoint& grid )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2010-07-13 10:42:32 +00:00
|
|
|
wxString msg;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2013-06-05 14:24:03 +00:00
|
|
|
msg.Printf( wxT( "%.10g" ), grid.x );
|
2010-07-13 10:42:32 +00:00
|
|
|
m_OptGridSizeX->SetValue( msg );
|
2013-06-05 14:24:03 +00:00
|
|
|
|
|
|
|
msg.Printf( wxT( "%.10g" ), grid.y );
|
2010-07-13 10:42:32 +00:00
|
|
|
m_OptGridSizeY->SetValue( msg );
|
|
|
|
}
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
bool DIALOG_SET_GRID::getGridSize( wxRealPoint& aGrisSize )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2010-07-13 10:42:32 +00:00
|
|
|
wxRealPoint grid;
|
2015-06-28 14:11:57 +00:00
|
|
|
wxString val = m_OptGridSizeX->GetValue();
|
2016-05-02 14:15:24 +00:00
|
|
|
double grid_unit_to_iu = ( getGridUnits() == INCHES ? IU_PER_MILS * 1000 : IU_PER_MM );
|
2015-06-28 14:11:57 +00:00
|
|
|
double tmp;
|
|
|
|
|
|
|
|
if( !val.ToDouble( &tmp ) ||
|
2016-05-02 14:15:24 +00:00
|
|
|
tmp * grid_unit_to_iu < MIN_GRID_SIZE || tmp * grid_unit_to_iu > MAX_GRID_SIZE )
|
2015-06-28 14:11:57 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aGrisSize.x = tmp;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
val = m_OptGridSizeY->GetValue();
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
if( !val.ToDouble( &tmp ) ||
|
|
|
|
tmp*grid_unit_to_iu < MIN_GRID_SIZE || tmp*grid_unit_to_iu > MAX_GRID_SIZE )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aGrisSize.y = tmp;
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
return true;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2015-06-28 14:11:57 +00:00
|
|
|
bool DIALOG_SET_GRID::getGridOrigin( wxPoint& aGridOrigin )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
double x, y;
|
|
|
|
|
|
|
|
const wxString& x_str = m_GridOriginXCtrl->GetValue();
|
2015-06-28 14:11:57 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
if( !x_str.ToDouble( &x ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
x = DoubleValueFromString( g_UserUnit, x_str );
|
2015-06-28 14:11:57 +00:00
|
|
|
|
|
|
|
// Some error checking here is a good thing.
|
2016-05-02 14:15:24 +00:00
|
|
|
if( x < -MAX_GRID_OFFSET || x > MAX_GRID_OFFSET )
|
2015-06-28 14:11:57 +00:00
|
|
|
return false;
|
2009-11-20 14:55:20 +00:00
|
|
|
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
const wxString& y_str = m_GridOriginYCtrl->GetValue();
|
|
|
|
|
|
|
|
if( !y_str.ToDouble( &y ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
y = DoubleValueFromString( g_UserUnit, y_str );
|
2015-06-28 14:11:57 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
if( y < -MAX_GRID_OFFSET || y > MAX_GRID_OFFSET )
|
2015-06-28 14:11:57 +00:00
|
|
|
return false;
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
|
|
|
|
aGridOrigin.x = KiROUND( x );
|
|
|
|
aGridOrigin.y = KiROUND( y );
|
2015-06-28 14:11:57 +00:00
|
|
|
|
|
|
|
return true;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 13:24:36 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void DIALOG_SET_GRID::setGridOrigin( const wxPoint& grid )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2012-04-13 18:51:24 +00:00
|
|
|
PutValueInLocalUnits( *m_GridOriginXCtrl, grid.x );
|
|
|
|
PutValueInLocalUnits( *m_GridOriginYCtrl, grid.y );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-05 14:24:03 +00:00
|
|
|
|
2015-04-10 23:43:15 +00:00
|
|
|
void DIALOG_SET_GRID::setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 )
|
2011-09-09 11:02:03 +00:00
|
|
|
{
|
2013-02-26 11:25:30 +00:00
|
|
|
m_comboBoxGrid1->Append( aGrids );
|
|
|
|
m_comboBoxGrid2->Append( aGrids );
|
2011-09-09 11:02:03 +00:00
|
|
|
|
|
|
|
m_comboBoxGrid1->SetSelection( aGrid1 );
|
|
|
|
m_comboBoxGrid2->SetSelection( aGrid2 );
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:24:03 +00:00
|
|
|
|
2013-07-09 05:48:26 +00:00
|
|
|
void DIALOG_SET_GRID::getGridForFastSwitching( int& aGrid1, int& aGrid2 )
|
2011-09-09 11:02:03 +00:00
|
|
|
{
|
|
|
|
aGrid1 = m_comboBoxGrid1->GetSelection();
|
|
|
|
aGrid2 = m_comboBoxGrid2->GetSelection();
|
|
|
|
}
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
void DIALOG_SET_GRID::setGridStyle( KIGFX::GRID_STYLE aStyle )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
m_Style->SetSelection( aStyle != KIGFX::GRID_STYLE_DOTS );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 13:24:36 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
KIGFX::GRID_STYLE DIALOG_SET_GRID::getGridStyle() const
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
return m_Style->GetSelection() == 0 ? KIGFX::GRID_STYLE_DOTS : KIGFX::GRID_STYLE_LINES;
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2010-07-14 13:24:36 +00:00
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
void DIALOG_SET_GRID::OnResetGridOrgClick( wxCommandEvent& event )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
setGridOrigin( wxPoint( 0, 0 ) );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
2013-07-09 05:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool PCB_BASE_FRAME::InvokeDialogGrid()
|
|
|
|
{
|
2016-05-02 14:15:24 +00:00
|
|
|
DIALOG_SET_GRID dlg( this, m_gridSelectBox->GetStrings() );
|
|
|
|
return dlg.ShowModal();
|
2013-07-09 05:48:26 +00:00
|
|
|
}
|