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