KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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
25
28#include <pcb_edit_frame.h>
29#include <grid_tricks.h>
30#include <eda_text.h>
31
32#include <kidialog.h>
33
34
35// Columns of layer classes grid
36enum
37{
44};
45
46enum
47{
54
56};
57
58
60 PCB_EDIT_FRAME* aFrame ) :
62 m_Frame( aFrame ),
63 m_BrdSettings( &m_Frame->GetBoard()->GetDesignSettings() ),
64 m_dimensionsPanel(
65 std::make_unique<PANEL_SETUP_DIMENSIONS>( this, *aFrame, *m_BrdSettings ) )
66{
72 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + FromDIP( 4 ) );
73 m_grid->SetUseNativeColLabels();
74
75 // Work around a bug in wxWidgets where it fails to recalculate the grid height
76 // after changing the default row size
77 m_grid->AppendRows( 1 );
78 m_grid->DeleteRows( m_grid->GetNumberRows() - 1, 1 );
79
80 // Gives a suitable size to m_grid columns
81 // The default wxWidget col size is not very good
82 // Calculate a min best size to handle longest usual numeric values:
83 int min_best_width = m_grid->GetTextExtent( wxT( "555,555555 mils" ) ).x;
84
85 for( int i = 0; i < m_grid->GetNumberCols(); ++i )
86 {
87 // We calculate the column min size only from texts sizes, not using the initial col width
88 // as this initial width is sometimes strange depending on the language (wxGrid bug?)
89 int min_width = m_grid->GetVisibleWidth( i );
90
91 m_grid->SetColMinimalWidth( i, min_width );
92
93 // We use a "best size" >= min_best_width
94 m_grid->SetColSize( i, std::max( min_width, min_best_width ) );
95 }
96
97 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
98
99 GetSizer()->Add( m_dimensionsPanel.get(), 0, wxEXPAND, 5 );
100
101 Layout();
102 m_mainSizer->Fit( this );
103
104 m_Frame->Bind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
105}
106
107
109{
110 // destroy GRID_TRICKS before m_grid.
111 m_grid->PopEventHandler( true );
112
113 m_Frame->Unbind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
114}
115
116
118{
119 BOARD_DESIGN_SETTINGS tempBDS( nullptr, "dummy" );
121
122 m_BrdSettings = &tempBDS; // No, address of stack var does not escape function
123
126
127 m_BrdSettings = saveBDS;
128
129 aEvent.Skip();
130}
131
132
134{
135 wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
136
137#define SET_MILS_CELL( row, col, val ) \
138 m_grid->SetCellValue( row, col, m_Frame->StringFromValue( val, true ) )
139
140#define DISABLE_CELL( row, col ) \
141 m_grid->SetReadOnly( row, col ); m_grid->SetCellBackgroundColour( row, col, disabledColour );
142
143 for( int i = 0; i < ROW_COUNT; ++i )
144 {
146
147 if( i == ROW_EDGES || i == ROW_COURTYARD )
148 {
154 }
155 else
156 {
160 m_grid->SetCellValue( i, COL_TEXT_ITALIC,
161 m_BrdSettings->m_TextItalic[ i ] ? wxT( "1" ) : wxT( "" ) );
162 m_grid->SetCellValue( i, COL_TEXT_UPRIGHT,
163 m_BrdSettings->m_TextUpright[ i ] ? wxT( "1" ) : wxT( "" ) );
164
165 auto attr = new wxGridCellAttr;
166 attr->SetRenderer( new wxGridCellBoolRenderer() );
167 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
168 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
169 m_grid->SetAttr( i, COL_TEXT_ITALIC, attr );
170
171 attr = new wxGridCellAttr;
172 attr->SetRenderer( new wxGridCellBoolRenderer() );
173 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
174 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
175 m_grid->SetAttr( i, COL_TEXT_UPRIGHT, attr );
176 }
177 }
178
179 // Work around an issue where wxWidgets doesn't calculate the row width on its own
180 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
181 m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col ) );
182
183 m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
184
185 Layout();
186
187 m_dimensionsPanel->TransferDataToWindow();
188
189 return true;
190}
191
192
194{
196 return false;
197
198 // A minimal value for sizes and thickness
199 const int minWidth = pcbIUScale.mmToIU( MINIMUM_LINE_WIDTH_MM );
200 const int maxWidth = pcbIUScale.mmToIU( MAXIMUM_LINE_WIDTH_MM );
202 const int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
203 wxString errorsMsg;
204 UNITS_PROVIDER* unitProvider = m_Frame;
205
206 for( int i = 0; i < ROW_COUNT; ++i )
207 {
208 bool badParam = false;
209
210 int lineWidth = m_grid->GetUnitValue( i, COL_LINE_THICKNESS );
211
212 if( lineWidth < minWidth || lineWidth > maxWidth )
213 {
214 if( !errorsMsg.IsEmpty() )
215 errorsMsg += wxT( "\n\n" );
216
217 errorsMsg += wxString::Format( _( "%s: Incorrect line width.\n"
218 "It must be between %s and %s" ),
219 m_grid->GetRowLabelValue( i ),
220 unitProvider->StringFromValue( minWidth , true),
221 unitProvider->StringFromValue( maxWidth , true) );
222 badParam = true;
223 }
224
225 if( !badParam )
226 m_BrdSettings->m_LineThickness[ i ] = lineWidth;
227
228 if( i == ROW_EDGES || i == ROW_COURTYARD )
229 continue;
230
231 badParam = false;
232 int textWidth = m_grid->GetUnitValue( i, COL_TEXT_WIDTH );
233 int textHeight = m_grid->GetUnitValue( i, COL_TEXT_HEIGHT );
234 int textThickness = m_grid->GetUnitValue( i, COL_TEXT_THICKNESS );
235
236 if( textWidth < minSize || textHeight < minSize
237 || textWidth > maxSize || textHeight > maxSize )
238 {
239 if( !errorsMsg.IsEmpty() )
240 errorsMsg += wxT( "\n\n" );
241
242 errorsMsg += wxString::Format( _( "%s: Text size is incorrect.\n"
243 "Size must be between %s and %s" ),
244 m_grid->GetRowLabelValue( i ),
245 unitProvider->StringFromValue( minSize , true),
246 unitProvider->StringFromValue( maxSize , true) );
247 badParam = true;
248 }
249
250 // Text thickness cannot be > text size /4 to be readable
251 int textMaxThickness = std::min( maxWidth, std::min( textWidth, textHeight ) /4);
252
253 if( !badParam && ( textThickness < minWidth || textThickness > textMaxThickness ) )
254 {
255 if( !errorsMsg.IsEmpty() )
256 errorsMsg += wxT( "\n\n" );
257
258
259 if( textThickness > textMaxThickness )
260 errorsMsg += wxString::Format( _( "%s: Text thickness is too large.\n"
261 "It will be truncated to %s" ),
262 m_grid->GetRowLabelValue( i ),
263 unitProvider->StringFromValue( textMaxThickness , true) );
264
265 else if( textThickness < minWidth )
266 errorsMsg += wxString::Format( _( "%s: Text thickness is too small.\n"
267 "It will be truncated to %s" ),
268 m_grid->GetRowLabelValue( i ),
269 unitProvider->StringFromValue( minWidth , true ) );
270
271 textThickness = std::min( textThickness, textMaxThickness );
272 textThickness = std::max( textThickness, minWidth );
273 SET_MILS_CELL( i, COL_TEXT_THICKNESS, textThickness );
274 }
275
276 if( !badParam )
277 {
278 m_BrdSettings->m_TextSize[ i ] = VECTOR2I( textWidth, textHeight );
279 m_BrdSettings->m_TextThickness[ i ] = textThickness;
280 }
281
283 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) );
285 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_UPRIGHT ) );
286 }
287
288 m_dimensionsPanel->TransferDataFromWindow();
289
290 if( errorsMsg.IsEmpty() )
291 return true;
292
293 KIDIALOG dlg( wxGetTopLevelParent( this ), errorsMsg, KIDIALOG::KD_ERROR,
294 _( "Parameter error" ) );
295 dlg.ShowModal();
296
297 return false;
298}
299
300
302{
304 return;
305
306 BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
307
308 m_BrdSettings = &aBoard->GetDesignSettings();
310
311 m_BrdSettings = savedSettings;
312}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
#define MINIMUM_LINE_WIDTH_MM
#define MAXIMUM_LINE_WIDTH_MM
Container for design settings for a BOARD object.
bool m_TextUpright[LAYER_CLASS_COUNT]
int m_TextThickness[LAYER_CLASS_COUNT]
int m_LineThickness[LAYER_CLASS_COUNT]
VECTOR2I m_TextSize[LAYER_CLASS_COUNT]
bool m_TextItalic[LAYER_CLASS_COUNT]
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:937
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
@ KD_ERROR
Definition: kidialog.h:46
int ShowModal() override
Definition: kidialog.cpp:95
Class PANEL_SETUP_TEXT_AND_GRAPHICS_BASE.
std::unique_ptr< PANEL_SETUP_DIMENSIONS > m_dimensionsPanel
PANEL_SETUP_TEXT_AND_GRAPHICS(wxWindow *aParentWindow, PCB_EDIT_FRAME *aFrame)
void onUnitsChanged(wxCommandEvent &aEvent)
The main frame for Pcbnew.
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:778
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:707
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition: wx_grid.h:128
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a EUNITS_PROVIDER to enable use of unit- and eval-based Getters.
Definition: wx_grid.cpp:698
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
#define TEXT_MIN_SIZE_MM
Minimum text size (1 micron).
Definition: eda_text.h:47
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition: eda_text.h:48
This file is part of the common library.
STL namespace.
#define SET_MILS_CELL(row, col, val)
#define DISABLE_CELL(row, col)
BOARD * GetBoard()
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695