Dialog set grid: add error checking in dialog (min and max grid size and max offset)

This commit is contained in:
jean-pierre charras 2015-06-28 16:11:57 +02:00
parent f556161a21
commit 3076876ef2
1 changed files with 68 additions and 19 deletions

View File

@ -29,6 +29,7 @@
#include <macros.h> #include <macros.h>
#include <common.h> #include <common.h>
#include <base_units.h> #include <base_units.h>
#include <macros.h>
#include <pcbnew.h> #include <pcbnew.h>
#include <pcbnew_id.h> #include <pcbnew_id.h>
@ -39,6 +40,12 @@
#include <class_draw_panel_gal.h> #include <class_draw_panel_gal.h>
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
// Max values for grid size
#define MAX_GRID_SIZE ( 50.0 * IU_PER_MM )
#define MIN_GRID_SIZE ( 0.001 * IU_PER_MM )
// Min/Max value for grid offset
#define MAX_GRID_OFFSET (INT_MAX/2)
class DIALOG_SET_GRID : public DIALOG_SET_GRID_BASE class DIALOG_SET_GRID : public DIALOG_SET_GRID_BASE
{ {
@ -65,10 +72,10 @@ private:
EDA_UNITS_T getGridUnits(); EDA_UNITS_T getGridUnits();
void setGridSize( const wxRealPoint& grid ); void setGridSize( const wxRealPoint& grid );
wxRealPoint getGridSize(); bool getGridSize( wxRealPoint& aGrisSize );
void setGridOrigin( const wxPoint& grid ); void setGridOrigin( const wxPoint& grid );
wxPoint getGridOrigin(); bool getGridOrigin( wxPoint& aGridOrigin );
void setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 ); void setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 );
void getGridForFastSwitching( int& aGrid1, int& aGrid2 ); void getGridForFastSwitching( int& aGrid1, int& aGrid2 );
@ -125,30 +132,58 @@ void DIALOG_SET_GRID::setGridSize( const wxRealPoint& grid )
} }
wxRealPoint DIALOG_SET_GRID::getGridSize() bool DIALOG_SET_GRID::getGridSize( wxRealPoint& aGrisSize )
{ {
wxRealPoint grid; wxRealPoint grid;
m_callers_grid_units = getGridUnits();
double grid_unit_to_iu = m_callers_grid_units == INCHES ? IU_PER_MILS*1000 : IU_PER_MM;
// @todo: Some error checking here would be a good thing. wxString val = m_OptGridSizeX->GetValue();
wxString x = m_OptGridSizeX->GetValue();
wxString y = m_OptGridSizeY->GetValue();
x.ToDouble( &grid.x ); double tmp;
y.ToDouble( &grid.y );
return grid; if( !val.ToDouble( &tmp ) ||
tmp*grid_unit_to_iu < MIN_GRID_SIZE || tmp*grid_unit_to_iu > MAX_GRID_SIZE )
{
return false;
}
else
aGrisSize.x = tmp;
val = m_OptGridSizeY->GetValue();
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;
return true;
} }
wxPoint DIALOG_SET_GRID::getGridOrigin() bool DIALOG_SET_GRID::getGridOrigin( wxPoint& aGridOrigin )
{ {
wxPoint grid; double tmp;
// @todo Some error checking here would be a good thing. tmp = DoubleValueFromString( g_UserUnit, m_GridOriginXCtrl->GetValue() );
grid.x = ValueFromTextCtrl( *m_GridOriginXCtrl );
grid.y = ValueFromTextCtrl( *m_GridOriginYCtrl );
return grid; // Some error checking here is a good thing.
if( tmp < -MAX_GRID_OFFSET || tmp > MAX_GRID_OFFSET )
return false;
aGridOrigin.x = KiROUND( tmp );
tmp = DoubleValueFromString( g_UserUnit, m_GridOriginYCtrl->GetValue() );
if( tmp < -MAX_GRID_OFFSET || tmp > MAX_GRID_OFFSET )
return false;
aGridOrigin.y = KiROUND( tmp );
return true;
} }
@ -190,9 +225,23 @@ void DIALOG_SET_GRID::OnCancelClick( wxCommandEvent& event )
void DIALOG_SET_GRID::OnOkClick( wxCommandEvent& event ) void DIALOG_SET_GRID::OnOkClick( wxCommandEvent& event )
{ {
m_callers_grid_units = getGridUnits(); bool success = getGridSize( m_callers_user_size );
m_callers_user_size = getGridSize();
m_callers_origin = getGridOrigin(); if( !success )
{
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;
}
success = getGridOrigin( m_callers_origin );
if( !success )
{
wxMessageBox( wxString::Format( _( "Incorrect grid origin (size must be >= %.3f and <= %.f mm)" ),
-MAX_GRID_OFFSET/IU_PER_MM, MAX_GRID_OFFSET/IU_PER_MM ) );
return;
}
getGridForFastSwitching( m_callers_fast_grid1, m_callers_fast_grid2 ); getGridForFastSwitching( m_callers_fast_grid1, m_callers_fast_grid2 );
@ -234,7 +283,7 @@ bool PCB_BASE_FRAME::InvokeDialogGrid()
if( mgr && IsGalCanvasActive() ) if( mgr && IsGalCanvasActive() )
mgr->RunAction( "common.Control.gridPreset", true, mgr->RunAction( "common.Control.gridPreset", true,
ID_POPUP_GRID_USER - ID_POPUP_GRID_LEVEL_1000 ); screen->GetGridCmdId() - ID_POPUP_GRID_LEVEL_1000 );
m_canvas->Refresh(); m_canvas->Refresh();