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, see <https://www.gnu.org/licenses/>.
18 */
19
21
25#include <pcb_edit_frame.h>
26#include <grid_tricks.h>
27#include <eda_text.h>
28
29#include <kidialog.h>
30
31
32// Columns of layer classes grid
33enum
34{
41};
42
43enum
44{
51
53};
54
55
57 BOARD_DESIGN_SETTINGS* aBrdSettings) :
59 m_Frame( aFrame ),
60 m_BrdSettings( aBrdSettings )
61{
62 m_grid->SetUnitsProvider( m_Frame );
63 m_grid->SetAutoEvalCols( { COL_LINE_THICKNESS,
67 m_grid->SetUseNativeColLabels();
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 // Gives a suitable size to m_grid columns
75 // The default wxWidget col size is not very good
76 // Calculate a min best size to handle longest usual numeric values:
77 int min_best_width = m_grid->GetTextExtent( wxT( "555,555555 mils" ) ).x;
78
79 for( int i = 0; i < m_grid->GetNumberCols(); ++i )
80 {
81 // We calculate the column min size only from texts sizes, not using the initial col width
82 // as this initial width is sometimes strange depending on the language (wxGrid bug?)
83 int min_width = m_grid->GetVisibleWidth( i );
84
85 m_grid->SetColMinimalWidth( i, min_width );
86
87 // We use a "best size" >= min_best_width
88 m_grid->SetColSize( i, std::max( min_width, min_best_width ) );
89 }
90
91 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
92
93 m_mainSizer->Fit( this );
94
95 m_Frame->Bind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
96}
97
98
100{
101 // destroy GRID_TRICKS before m_grid.
102 m_grid->PopEventHandler( true );
103
104 m_Frame->Unbind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
105}
106
107
109{
110 BOARD_DESIGN_SETTINGS tempBDS( nullptr, "dummy" );
112
113 m_BrdSettings = &tempBDS; // No, address of stack var does not escape function
114
117
118 m_BrdSettings = saveBDS;
119
120 aEvent.Skip();
121}
122
123
125{
126 wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
127
128#define SET_MILS_CELL( row, col, val ) \
129 m_grid->SetCellValue( row, col, m_Frame->StringFromValue( val, true ) )
130
131#define DISABLE_CELL( row, col ) \
132 m_grid->SetReadOnly( row, col ); m_grid->SetCellBackgroundColour( row, col, disabledColour );
133
134 for( int i = 0; i < ROW_COUNT; ++i )
135 {
136 SET_MILS_CELL( i, COL_LINE_THICKNESS, m_BrdSettings->m_LineThickness[ i ] );
137
138 if( i == ROW_EDGES || i == ROW_COURTYARD )
139 {
145 }
146 else
147 {
148 SET_MILS_CELL( i, COL_TEXT_WIDTH, m_BrdSettings->m_TextSize[ i ].x );
149 SET_MILS_CELL( i, COL_TEXT_HEIGHT, m_BrdSettings->m_TextSize[ i ].y );
150 SET_MILS_CELL( i, COL_TEXT_THICKNESS, m_BrdSettings->m_TextThickness[ i ] );
151 m_grid->SetCellValue( i, COL_TEXT_ITALIC, m_BrdSettings->m_TextItalic[ i ] ? wxT( "1" )
152 : wxT( "" ) );
153 m_grid->SetCellValue( i, COL_TEXT_UPRIGHT, m_BrdSettings->m_TextUpright[ i ] ? wxT( "1" )
154 : wxT( "" ) );
155
156 auto attr = new wxGridCellAttr;
157 attr->SetRenderer( new wxGridCellBoolRenderer() );
158 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
159 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
160 m_grid->SetAttr( i, COL_TEXT_ITALIC, attr );
161
162 attr = new wxGridCellAttr;
163 attr->SetRenderer( new wxGridCellBoolRenderer() );
164 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
165 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
166 m_grid->SetAttr( i, COL_TEXT_UPRIGHT, attr );
167 }
168 }
169
170 // Work around an issue where wxWidgets doesn't calculate the row width on its own
171 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
172 m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col ) );
173
174 m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
175
176 return true;
177}
178
179
181{
182 if( !m_grid->CommitPendingChanges() )
183 return false;
184
185 // A minimal value for sizes and thickness
186 const int minWidth = pcbIUScale.mmToIU( MINIMUM_LINE_WIDTH_MM );
187 const int maxWidth = pcbIUScale.mmToIU( MAXIMUM_LINE_WIDTH_MM );
188 const int minSize = pcbIUScale.mmToIU( TEXT_MIN_SIZE_MM );
189 const int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
190 wxString errorsMsg;
191 UNITS_PROVIDER* unitProvider = m_Frame;
192
193 for( int i = 0; i < ROW_COUNT; ++i )
194 {
195 bool badParam = false;
196
197 int lineWidth = m_grid->GetUnitValue( i, COL_LINE_THICKNESS );
198
199 if( lineWidth < minWidth || lineWidth > maxWidth )
200 {
201 if( !errorsMsg.IsEmpty() )
202 errorsMsg += wxT( "\n\n" );
203
204 errorsMsg += wxString::Format( _( "%s: Incorrect line width.\n"
205 "It must be between %s and %s" ),
206 m_grid->GetRowLabelValue( i ),
207 unitProvider->StringFromValue( minWidth , true),
208 unitProvider->StringFromValue( maxWidth , true) );
209 badParam = true;
210 }
211
212 if( !badParam )
213 m_BrdSettings->m_LineThickness[ i ] = lineWidth;
214
215 if( i == ROW_EDGES || i == ROW_COURTYARD )
216 continue;
217
218 badParam = false;
219 int textWidth = m_grid->GetUnitValue( i, COL_TEXT_WIDTH );
220 int textHeight = m_grid->GetUnitValue( i, COL_TEXT_HEIGHT );
221 int textThickness = m_grid->GetUnitValue( i, COL_TEXT_THICKNESS );
222
223 if( textWidth < minSize || textHeight < minSize || textWidth > maxSize || textHeight > maxSize )
224 {
225 if( !errorsMsg.IsEmpty() )
226 errorsMsg += wxT( "\n\n" );
227
228 errorsMsg += wxString::Format( _( "%s: Text size is incorrect.\n"
229 "Size must be between %s and %s" ),
230 m_grid->GetRowLabelValue( i ),
231 unitProvider->StringFromValue( minSize , true),
232 unitProvider->StringFromValue( maxSize , true) );
233 badParam = true;
234 }
235
236 // Text thickness cannot be > text size /4 to be readable
237 int textMaxThickness = std::min( maxWidth, std::min( textWidth, textHeight ) /4);
238
239 if( !badParam && ( textThickness < minWidth || textThickness > textMaxThickness ) )
240 {
241 if( !errorsMsg.IsEmpty() )
242 errorsMsg += wxT( "\n\n" );
243
244 if( textThickness > textMaxThickness )
245 {
246 errorsMsg += wxString::Format( _( "%s: Text thickness is too large.\n"
247 "It will be truncated to %s" ),
248 m_grid->GetRowLabelValue( i ),
249 unitProvider->StringFromValue( textMaxThickness , true) );
250 }
251 else if( textThickness < minWidth )
252 {
253 errorsMsg += wxString::Format( _( "%s: Text thickness is too small.\n"
254 "It will be truncated to %s" ),
255 m_grid->GetRowLabelValue( i ),
256 unitProvider->StringFromValue( minWidth , true ) );
257 }
258
259 textThickness = std::min( textThickness, textMaxThickness );
260 textThickness = std::max( textThickness, minWidth );
261 SET_MILS_CELL( i, COL_TEXT_THICKNESS, textThickness );
262 }
263
264 if( !badParam )
265 {
266 m_BrdSettings->m_TextSize[ i ] = VECTOR2I( textWidth, textHeight );
267 m_BrdSettings->m_TextThickness[ i ] = textThickness;
268 }
269
270 m_BrdSettings->m_TextItalic[ i ] =
271 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) );
272 m_BrdSettings->m_TextUpright[ i ] =
273 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_UPRIGHT ) );
274 }
275
276 if( errorsMsg.IsEmpty() )
277 return true;
278
279 KIDIALOG dlg( wxGetTopLevelParent( m_grid ), errorsMsg, KIDIALOG::KD_ERROR, _( "Parameter error" ) );
280 dlg.ShowModal();
281
282 return false;
283}
284
285
287{
288 return m_grid->CommitPendingChanges();
289}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
#define MINIMUM_LINE_WIDTH_MM
#define MAXIMUM_LINE_WIDTH_MM
Container for design settings for a BOARD object.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:38
@ KD_ERROR
Definition kidialog.h:43
int ShowModal() override
Definition kidialog.cpp:89
PANEL_SETUP_TEXT_AND_GRAPHICS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
PANEL_SETUP_TEXT_AND_GRAPHICS(wxWindow *aParentWindow, PCB_EDIT_FRAME *aFrame, BOARD_DESIGN_SETTINGS *aBrdSettings)
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.
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:61
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:62
#define SET_MILS_CELL(row, col, val)
#define DISABLE_CELL(row, col)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708