KiCad PCB EDA Suite
panel_setup_text_and_graphics.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) 2018 KiCad Developers, see change_log.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 
24 #include <base_units.h>
25 #include <pcb_edit_frame.h>
26 #include <board_design_settings.h>
27 #include <widgets/wx_grid.h>
28 #include <grid_tricks.h>
30 
31 
32 // Columns of layer classes grid
33 enum
34 {
41 };
42 
43 enum
44 {
45  ROW_SILK = 0,
51 
53 };
54 
55 
57  PCB_EDIT_FRAME* aFrame ) :
58  PANEL_SETUP_TEXT_AND_GRAPHICS_BASE( aParent->GetTreebook() ),
59  m_arrowLength( aFrame, m_lblArrowLength, m_dimensionArrowLength, m_arrowLengthUnits ),
60  m_extensionOffset( aFrame, m_lblExtensionOffset, m_dimensionExtensionOffset,
61  m_dimensionExtensionOffsetUnits )
62 {
63  m_Parent = aParent;
64  m_Frame = aFrame;
66 
67  m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
68 
69  // Work around a bug in wxWidgets where it fails to recalculate the grid height
70  // after changing the default row size
71  m_grid->AppendRows( 1 );
72  m_grid->DeleteRows( m_grid->GetNumberRows() - 1, 1 );
73 
74  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
75 }
76 
77 
79 {
80  // destroy GRID_TRICKS before m_grid.
81  m_grid->PopEventHandler( true );
82 }
83 
84 
86 {
87  wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
88 
89 #define SET_MILS_CELL( row, col, val ) \
90  m_grid->SetCellValue( row, col, StringFromValue( m_Frame->GetUserUnits(), val, true ) )
91 
92 #define DISABLE_CELL( row, col ) \
93  m_grid->SetReadOnly( row, col ); m_grid->SetCellBackgroundColour( row, col, disabledColour );
94 
95  for( int i = 0; i < ROW_COUNT; ++i )
96  {
98 
99  if( i == ROW_EDGES || i == ROW_COURTYARD )
100  {
106  }
107  else
108  {
112  m_grid->SetCellValue( i, COL_TEXT_ITALIC, m_BrdSettings->m_TextItalic[ i ] ? "1" : "" );
113  m_grid->SetCellValue( i, COL_TEXT_UPRIGHT, m_BrdSettings->m_TextUpright[ i ] ? "1" : "" );
114 
115  auto attr = new wxGridCellAttr;
116  attr->SetRenderer( new wxGridCellBoolRenderer() );
117  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
118  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
119  m_grid->SetAttr( i, COL_TEXT_ITALIC, attr );
120 
121  attr = new wxGridCellAttr;
122  attr->SetRenderer( new wxGridCellBoolRenderer() );
123  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
124  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
125  m_grid->SetAttr( i, COL_TEXT_UPRIGHT, attr );
126  }
127  }
128 
129  // Work around an issue where wxWidgets doesn't calculate the row width on its own
130  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
131  m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col, true, true, false ) );
132 
133  m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
134 
135  Layout();
136 
137  wxASSERT_MSG( m_BrdSettings->m_DimensionPrecision <= 4, "Unhandled dimension precision!" );
138 
139  int mode = static_cast<int>( m_BrdSettings->m_DimensionUnitsMode );
140  m_dimensionUnits->SetSelection( mode );
141 
142  int format = static_cast<int>( m_BrdSettings->m_DimensionUnitsFormat );
143  m_dimensionUnitsFormat->SetSelection( format );
144 
147 
148  int position = static_cast<int>( m_BrdSettings->m_DimensionTextPosition );
149  m_dimensionTextPositionMode->SetSelection( position );
150 
154 
155  return true;
156 }
157 
158 
160 {
161  return ValueFromString( m_Frame->GetUserUnits(), m_grid->GetCellValue( aRow, aCol ) );
162 }
163 
164 
166 {
167  if( !m_grid->CommitPendingChanges() )
168  return false;
169 
170  // Test text parameters.
171  for( int row : { ROW_SILK, ROW_COPPER, ROW_FAB, ROW_OTHERS } )
172  {
173  int textSize = std::min( getGridValue( row, COL_TEXT_WIDTH ),
174  getGridValue( row, COL_TEXT_HEIGHT ) );
175 
176  if( getGridValue( row, COL_TEXT_THICKNESS ) > textSize / 4 )
177  {
178  wxString msg = _( "Text will not be readable with a thickness greater than\n"
179  "1/4 its width or height." );
180  m_Parent->SetError( msg, this, m_grid, row, COL_TEXT_THICKNESS );
181  return false;
182  }
183  }
184 
185  return true;
186 }
187 
188 
190 {
191  if( !validateData() )
192  return false;
193 
194  for( int i = 0; i < ROW_COUNT; ++i )
195  {
197 
198  if( i == ROW_EDGES || i == ROW_COURTYARD )
199  continue;
200 
201  m_BrdSettings->m_TextSize[ i ] =
205  wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) );
207  wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_UPRIGHT ) );
208  }
209 
210  // These are all stored in project file, not board, so no need for OnModify()
211 
212  int mode = m_dimensionUnits->GetSelection();
213  m_BrdSettings->m_DimensionUnitsMode = static_cast<DIM_UNITS_MODE>( mode );
214  int format = m_dimensionUnitsFormat->GetSelection();
215  m_BrdSettings->m_DimensionUnitsFormat = static_cast<DIM_UNITS_FORMAT>( format );
218  int position = m_dimensionTextPositionMode->GetSelection();
219  m_BrdSettings->m_DimensionTextPosition = static_cast<DIM_TEXT_POSITION>( position );
223 
224  return true;
225 }
226 
227 
229 {
230  if( !m_grid->CommitPendingChanges() )
231  return;
232 
233  BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
234 
235  m_BrdSettings = &aBoard->GetDesignSettings();
237 
238  m_BrdSettings = savedSettings;
239 }
Implementation of conversion functions that require both schematic and board internal units.
Class PANEL_SETUP_TEXT_AND_GRAPHICS_BASE.
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:52
int m_DimensionPrecision
Number of digits after the decimal.
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: class_board.h:558
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
DIM_TEXT_POSITION m_DimensionTextPosition
DIM_UNITS_MODE m_DimensionUnitsMode
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:236
wxSize m_TextSize[LAYER_CLASS_COUNT]
int m_TextThickness[LAYER_CLASS_COUNT]
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:444
#define DISABLE_CELL(row, col)
bool m_TextItalic[LAYER_CLASS_COUNT]
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:180
PANEL_SETUP_TEXT_AND_GRAPHICS(PAGED_DIALOG *aParent, PCB_EDIT_FRAME *aFrame)
int m_LineThickness[LAYER_CLASS_COUNT]
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
#define _(s)
Definition: 3d_actions.cpp:33
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
PCB_EDIT_FRAME is the main frame for Pcbnew.
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
DIM_UNITS_FORMAT m_DimensionUnitsFormat
#define SET_MILS_CELL(row, col, val)
BOARD * GetBoard() const
bool m_TextUpright[LAYER_CLASS_COUNT]
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.