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 <pcb_base_frame.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 = 1000.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 setGridSize( const wxPoint& grid );
76  bool getGridSize( wxPoint& aGrisSize );
77 
78  void setGridOrigin( const wxPoint& grid );
79  bool getGridOrigin( wxPoint& aGridOrigin );
80 
81  void setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 );
82  void getGridForFastSwitching( int& aGrid1, int& aGrid2 );
83 };
84 
85 
86 DIALOG_SET_GRID::DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices ):
87  DIALOG_SET_GRID_BASE( aParent ),
88  m_parent( aParent ),
89  m_fast_grid_opts( aGridChoices )
90 {
91  m_sdbSizerOK->SetDefault(); // set OK button as default response to 'Enter' key
92 
95 
98 }
99 
100 
102 {
103  // Validate new settings
104  wxPoint gridOrigin;
105 
106  if( !getGridOrigin( gridOrigin ) )
107  {
108  wxMessageBox( wxString::Format( _( "Incorrect grid origin "
109  "(coordinates must be >= %.3f mm and <= %.3f mm)" ),
110  -MAX_GRID_OFFSET/IU_PER_MM, MAX_GRID_OFFSET/IU_PER_MM ) );
111 
112  return false;
113  }
114 
115  wxPoint gridSize;
116 
117  if( !getGridSize( gridSize ) )
118  {
119  wxMessageBox( wxString::Format( _( "Incorrect grid size "
120  "(size must be >= %.3f mm and <= %.3f mm)" ),
121  MIN_GRID_SIZE/IU_PER_MM, MAX_GRID_SIZE/IU_PER_MM ) );
122 
123  return false;
124  }
125 
126  int fastGrid1, fastGrid2;
127  getGridForFastSwitching( fastGrid1, fastGrid2 );
128 
129  // Apply the new settings
130 
131  // Because grid origin is saved in board, show as modified
132  m_parent->OnModify();
133  m_parent->SetGridOrigin( gridOrigin );
134  m_parent->m_UserGridSize = gridSize;
135  m_parent->m_FastGrid1 = fastGrid1;
136  m_parent->m_FastGrid2 = fastGrid2;
137 
138  // User grid
139  BASE_SCREEN* screen = m_parent->GetScreen();
141 
142  // If the user grid is the current option, recall SetGrid()
143  // to force new values put in list as current grid value
144  if( screen->GetGridCmdId() == ID_POPUP_GRID_USER )
145  screen->SetGrid( ID_POPUP_GRID_USER );
146 
147  // Notify GAL
149 
150  if( mgr && m_parent->IsGalCanvasActive() )
151  {
152  mgr->RunAction( "common.Control.gridPreset", true,
154 
155  TOOL_EVENT gridOriginUpdate = ACTIONS::gridSetOrigin.MakeEvent();
156  gridOriginUpdate.SetParameter( new VECTOR2D( gridOrigin ) );
157  mgr->ProcessEvent( gridOriginUpdate );
158  }
159 
160  return wxDialog::TransferDataFromWindow();
161 }
162 
163 
165 {
169 
170  return wxDialog::TransferDataToWindow();
171 }
172 
173 
175 {
176  wxString msg;
177 
178  msg.Printf( wxT( "%.10g" ), To_User_Unit( g_UserUnit, grid.x ) );
179  m_OptGridSizeX->SetValue( msg );
180 
181  msg.Printf( wxT( "%.10g" ), To_User_Unit( g_UserUnit, grid.y ) );
182  m_OptGridSizeY->SetValue( msg );
183 }
184 
185 
187 {
188  double x, y;
189 
190  const wxString& x_str = m_OptGridSizeX->GetValue();
191 
192  if( !x_str.ToDouble( &x ) )
193  return false;
194 
195  x = DoubleValueFromString( g_UserUnit, x_str );
196 
197  // Some error checking here is a good thing.
198  if( x < MIN_GRID_SIZE || x > MAX_GRID_SIZE )
199  return false;
200 
201  const wxString& y_str = m_OptGridSizeY->GetValue();
202 
203  if( !y_str.ToDouble( &y ) )
204  return false;
205 
206  y = DoubleValueFromString( g_UserUnit, y_str );
207 
208  // Some error checking here is a good thing.
209  if( y < MIN_GRID_SIZE || y > MAX_GRID_SIZE )
210  return false;
211 
212  aGridSize.x = KiROUND( x );
213  aGridSize.y = KiROUND( y );
214 
215  return true;
216 }
217 
218 
220 {
221  double x, y;
222 
223  const wxString& x_str = m_GridOriginXCtrl->GetValue();
224 
225  if( !x_str.ToDouble( &x ) )
226  return false;
227 
228  x = DoubleValueFromString( g_UserUnit, x_str );
229 
230  // Some error checking here is a good thing.
231  if( x < -MAX_GRID_OFFSET || x > MAX_GRID_OFFSET )
232  return false;
233 
234 
235  const wxString& y_str = m_GridOriginYCtrl->GetValue();
236 
237  if( !y_str.ToDouble( &y ) )
238  return false;
239 
240  y = DoubleValueFromString( g_UserUnit, y_str );
241 
242  if( y < -MAX_GRID_OFFSET || y > MAX_GRID_OFFSET )
243  return false;
244 
245 
246  aGridOrigin.x = KiROUND( x );
247  aGridOrigin.y = KiROUND( y );
248 
249  return true;
250 }
251 
252 
254 {
257 }
258 
259 
260 void DIALOG_SET_GRID::setGridForFastSwitching( const wxArrayString& aGrids, int aGrid1, int aGrid2 )
261 {
262  m_comboBoxGrid1->Append( aGrids );
263  m_comboBoxGrid2->Append( aGrids );
264 
265  m_comboBoxGrid1->SetSelection( aGrid1 );
266  m_comboBoxGrid2->SetSelection( aGrid2 );
267 }
268 
269 
270 void DIALOG_SET_GRID::getGridForFastSwitching( int& aGrid1, int& aGrid2 )
271 {
272  aGrid1 = m_comboBoxGrid1->GetSelection();
273  aGrid2 = m_comboBoxGrid2->GetSelection();
274 }
275 
276 
277 void DIALOG_SET_GRID::OnResetGridOrgClick( wxCommandEvent& event )
278 {
279  setGridOrigin( wxPoint( 0, 0 ) );
280 }
281 
282 
284 {
285  DIALOG_SET_GRID dlg( this, m_gridSelectBox->GetStrings() );
286  return dlg.ShowModal();
287 }
wxArrayString m_fast_grid_opts
wxStaticText * m_TextSizeXUnits
void setGridSize(const wxPoint &grid)
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
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.
Definition: base_screen.h:403
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
void OnResetGridOrgClick(wxCommandEvent &event) override
bool getGridOrigin(wxPoint &aGridOrigin)
DIALOG_SET_GRID(PCB_BASE_FRAME *aParent, const wxArrayString &aGridChoices)
This has no dependencies on calling wxFrame derivative, such as PCB_BASE_FRAME.
Classes used in Pcbnew, CvPcb and GerbView.
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:390
wxStaticText * m_TextSizeYUnits
VECTOR2< double > VECTOR2D
Definition: vector2d.h:588
bool TransferDataFromWindow() override
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
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 ...)
Definition: base_screen.h:76
wxStaticText * m_TextPosYUnits
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:485
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:887
ID_POPUP_GRID_USER
Definition: draw_frame.cpp:110
wxPoint m_UserGridSize
bool getGridSize(wxPoint &aGrisSize)
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:267
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:902
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.
double To_User_Unit(EDA_UNITS_T aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91
Class DIALOG_SET_GRID_BASE.
void getGridForFastSwitching(int &aGrid1, int &aGrid2)
static TOOL_ACTION gridSetOrigin
Definition: actions.h:65
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:303
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
static const double MIN_GRID_SIZE