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