KiCad PCB EDA Suite
dialog_set_grid.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
29 #include <dialog_set_grid_base.h>
30 
31 #include <base_units.h>
32 #include <convert_to_biu.h>
33 #include <common.h>
34 
35 #include <wxBasePcbFrame.h>
36 #include <class_drawpanel.h>
37 #include <class_draw_panel_gal.h>
38 
40 #include <tools/pcb_actions.h>
41 #include <tool/tool_manager.h>
42 
43 #include <limits.h>
44 
45 // Max values for grid size
46 static const double MAX_GRID_SIZE = 50.0 * IU_PER_MM;
47 static const double MIN_GRID_SIZE = 0.001 * IU_PER_MM;
48 
49 // Min/Max value for grid offset
50 static const double MAX_GRID_OFFSET = INT_MAX / 2.0;
51 
53 {
55  wxArrayString m_fast_grid_opts;
56 
57 public:
59  DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices );
60 
61  bool TransferDataFromWindow() override;
62  bool TransferDataToWindow() override;
63 
64 private:
65  void OnResetGridOrgClick( wxCommandEvent& event ) override;
66  void OnInitDlg( wxInitDialogEvent& event ) override
67  {
68  // Call the default wxDialog handler of a wxInitDialogEvent
70 
71  // Now all widgets have the size fixed, call FinishDialogSettings
73  }
74 
75  void setGridUnits( EDA_UNITS_T units );
77 
78  void setGridSize( const wxRealPoint& grid );
79  bool getGridSize( wxRealPoint& aGrisSize );
80 
81  void setGridOrigin( const wxPoint& grid );
82  bool getGridOrigin( wxPoint& aGridOrigin );
83 
84  void setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 );
85  void getGridForFastSwitching( int& aGrid1, int& aGrid2 );
86 };
87 
88 
89 DIALOG_SET_GRID::DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices ):
90  DIALOG_SET_GRID_BASE( aParent ),
91  m_parent( aParent ),
92  m_fast_grid_opts( aGridChoices )
93 {
94  m_sdbSizerOK->SetDefault(); // set OK button as default response to 'Enter' key
95 
98 
99 }
100 
101 
103 {
104  // Validate new settings
105  wxRealPoint gridSize;
106  if( !getGridSize( gridSize ) )
107  {
108  wxMessageBox( wxString::Format( _( "Incorrect grid size "
109  "(size must be >= %.3f mm and <= %.3f mm)" ),
110  MIN_GRID_SIZE/IU_PER_MM, MAX_GRID_SIZE/IU_PER_MM ) );
111 
112  return false;
113  }
114 
115  wxPoint gridOrigin;
116  if( !getGridOrigin( gridOrigin ) )
117  {
118  wxMessageBox( wxString::Format( _( "Incorrect grid origin "
119  "(coordinates must be >= %.3f mm and <= %.3f mm)" ),
120  -MAX_GRID_OFFSET/IU_PER_MM, MAX_GRID_OFFSET/IU_PER_MM ) );
121 
122  return false;
123  }
124 
125  int fastGrid1, fastGrid2;
126  getGridForFastSwitching( fastGrid1, fastGrid2 );
127 
128  EDA_UNITS_T units = getGridUnits();
129 
130  // Apply the new settings
131 
132  // Because grid origin is saved in board, show as modified
133  m_parent->OnModify();
134  m_parent->SetGridOrigin( gridOrigin );
135  m_parent->m_UserGridUnit = units;
136  m_parent->m_UserGridSize = gridSize;
137  m_parent->m_FastGrid1 = fastGrid1;
138  m_parent->m_FastGrid2 = fastGrid2;
139 
140  // User grid
141  BASE_SCREEN* screen = m_parent->GetScreen();
142  screen->AddGrid( gridSize, units, ID_POPUP_GRID_USER );
143 
144  // If the user grid is the current option, recall SetGrid()
145  // to force new values put in list as current grid value
146  if( screen->GetGridCmdId() == ID_POPUP_GRID_USER )
147  screen->SetGrid( ID_POPUP_GRID_USER );
148 
149  // Notify GAL
151 
152  if( mgr && m_parent->IsGalCanvasActive() )
153  {
154  mgr->RunAction( "common.Control.gridPreset", true,
156 
157  TOOL_EVENT gridOriginUpdate = ACTIONS::gridSetOrigin.MakeEvent();
158  gridOriginUpdate.SetParameter( new VECTOR2D( gridOrigin ) );
159  mgr->ProcessEvent( gridOriginUpdate );
160  }
161 
162  return wxDialog::TransferDataFromWindow();
163 }
164 
165 
167 {
172 
173  return wxDialog::TransferDataToWindow();
174 }
175 
176 
178 {
179  m_UnitGrid->SetSelection( aUnits != INCHES );
180 }
181 
182 
184 {
185  return m_UnitGrid->GetSelection() == 0 ? INCHES : MILLIMETRES;
186 }
187 
188 
189 void DIALOG_SET_GRID::setGridSize( const wxRealPoint& grid )
190 {
191  wxString msg;
192 
193  msg.Printf( wxT( "%.10g" ), grid.x );
194  m_OptGridSizeX->SetValue( msg );
195 
196  msg.Printf( wxT( "%.10g" ), grid.y );
197  m_OptGridSizeY->SetValue( msg );
198 }
199 
200 
201 bool DIALOG_SET_GRID::getGridSize( wxRealPoint& aGrisSize )
202 {
203  wxRealPoint grid;
204  wxString val = m_OptGridSizeX->GetValue();
205  double grid_unit_to_iu = ( getGridUnits() == INCHES ? IU_PER_MILS * 1000 : IU_PER_MM );
206  double tmp;
207 
208  if( !val.ToDouble( &tmp ) ||
209  tmp * grid_unit_to_iu < MIN_GRID_SIZE || tmp * grid_unit_to_iu > MAX_GRID_SIZE )
210  {
211  return false;
212  }
213  else
214  aGrisSize.x = tmp;
215 
216  val = m_OptGridSizeY->GetValue();
217 
218  if( !val.ToDouble( &tmp ) ||
219  tmp*grid_unit_to_iu < MIN_GRID_SIZE || tmp*grid_unit_to_iu > MAX_GRID_SIZE )
220  {
221  return false;
222  }
223  else
224  aGrisSize.y = tmp;
225 
226  return true;
227 }
228 
229 
231 {
232  double x, y;
233 
234  const wxString& x_str = m_GridOriginXCtrl->GetValue();
235 
236  if( !x_str.ToDouble( &x ) )
237  return false;
238 
239  x = DoubleValueFromString( g_UserUnit, x_str );
240 
241  // Some error checking here is a good thing.
242  if( x < -MAX_GRID_OFFSET || x > MAX_GRID_OFFSET )
243  return false;
244 
245 
246  const wxString& y_str = m_GridOriginYCtrl->GetValue();
247 
248  if( !y_str.ToDouble( &y ) )
249  return false;
250 
251  y = DoubleValueFromString( g_UserUnit, y_str );
252 
253  if( y < -MAX_GRID_OFFSET || y > MAX_GRID_OFFSET )
254  return false;
255 
256 
257  aGridOrigin.x = KiROUND( x );
258  aGridOrigin.y = KiROUND( y );
259 
260  return true;
261 }
262 
263 
265 {
268 }
269 
270 
271 void DIALOG_SET_GRID::setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 )
272 {
273  m_comboBoxGrid1->Append( aGrids );
274  m_comboBoxGrid2->Append( aGrids );
275 
276  m_comboBoxGrid1->SetSelection( aGrid1 );
277  m_comboBoxGrid2->SetSelection( aGrid2 );
278 }
279 
280 
281 void DIALOG_SET_GRID::getGridForFastSwitching( int& aGrid1, int& aGrid2 )
282 {
283  aGrid1 = m_comboBoxGrid1->GetSelection();
284  aGrid2 = m_comboBoxGrid2->GetSelection();
285 }
286 
287 
288 void DIALOG_SET_GRID::OnResetGridOrgClick( wxCommandEvent& event )
289 {
290  setGridOrigin( wxPoint( 0, 0 ) );
291 }
292 
293 
295 {
296  DIALOG_SET_GRID dlg( this, m_gridSelectBox->GetStrings() );
297  return dlg.ShowModal();
298 }
wxArrayString m_fast_grid_opts
void setGridUnits(EDA_UNITS_T units)
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
void SetGridOrigin(const wxPoint &aPoint) override
bool getGridSize(wxRealPoint &aGrisSize)
Implementation of conversion functions that require both schematic and board internal units...
bool TransferDataToWindow() override
int GetGridCmdId() const
Return the command ID of the currently selected grid.
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:452
PCB_BASE_FRAME * m_parent
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
const wxPoint & GetGridOrigin() const override
Function GetGridOrigin returns the absolute coordinates of the origin of the snap grid...
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
static const double MAX_GRID_SIZE
EDA_UNITS_T m_UserGridUnit
void OnResetGridOrgClick(wxCommandEvent &event) override
bool getGridOrigin(wxPoint &aGridOrigin)
wxChoice * m_gridSelectBox
Choice box to choose the grid size.
Definition: draw_frame.h:115
DIALOG_SET_GRID(PCB_BASE_FRAME *aParent, const wxArrayString &aGridChoices)
This has no dependencies on calling wxFrame derivative, such as PCB_BASE_FRAME.
wxRealPoint m_UserGridSize
void setGridOrigin(const wxPoint &grid)
Class TOOL_MANAGER.
Definition: tool_manager.h:49
void SetParameter(T aParam)
Function SetParameter() Sets a non-standard parameter assigned to the event.
Definition: tool_event.h:385
Classes used in Pcbnew, CvPcb and GerbView.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
bool TransferDataFromWindow() override
void setGridForFastSwitching(const wxArrayString &aGrids, int aGrid1, int aGrid2)
wxStaticText * m_TextPosXUnits
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
wxStaticText * m_TextPosYUnits
Definition: common.h:145
Class TOOL_EVENT.
Definition: tool_event.h:162
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:795
void setGridSize(const wxRealPoint &grid)
ID_POPUP_GRID_USER
Definition: draw_frame.cpp:105
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
EDA_UNITS_T getGridUnits()
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:265
virtual void OnModify()
Function OnModify Virtual Must be called after a change in order to set the "modify" flag of the curr...
TOOL_MANAGER * GetToolManager() const
Function GetToolManager returns the tool manager instance, if any.
Definition: draw_frame.h:810
void AddGrid(const GRID_TYPE &grid)
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
void OnInitDlg(wxInitDialogEvent &event) override
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static const double MAX_GRID_OFFSET
The common library.
PCB_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Class DIALOG_SET_GRID_BASE.
void getGridForFastSwitching(int &aGrid1, int &aGrid2)
void ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
static TOOL_ACTION gridSetOrigin
Definition: actions.h:64
int SetGrid(const wxRealPoint &size)
set the current grid size m_Grid.
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
EDA_UNITS_T
Definition: common.h:144
static const double MIN_GRID_SIZE