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( std::make_unique<PANEL_SETUP_DIMENSIONS>( this, *aFrame, *m_BrdSettings ) )
65{
71 m_grid->SetUseNativeColLabels();
72
73 // Work around a bug in wxWidgets where it fails to recalculate the grid height
74 // after changing the default row size
75 m_grid->AppendRows( 1 );
76 m_grid->DeleteRows( m_grid->GetNumberRows() - 1, 1 );
77
78 // Gives a suitable size to m_grid columns
79 // The default wxWidget col size is not very good
80 // Calculate a min best size to handle longest usual numeric values:
81 int min_best_width = m_grid->GetTextExtent( wxT( "555,555555 mils" ) ).x;
82
83 for( int i = 0; i < m_grid->GetNumberCols(); ++i )
84 {
85 // We calculate the column min size only from texts sizes, not using the initial col width
86 // as this initial width is sometimes strange depending on the language (wxGrid bug?)
87 int min_width = m_grid->GetVisibleWidth( i );
88
89 m_grid->SetColMinimalWidth( i, min_width );
90
91 // We use a "best size" >= min_best_width
92 m_grid->SetColSize( i, std::max( min_width, min_best_width ) );
93 }
94
95 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
96
97 GetSizer()->Add( m_dimensionsPanel.get(), 0, wxEXPAND, 5 );
98
99 Layout();
100 m_mainSizer->Fit( this );
101
102 m_Frame->Bind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
103}
104
105
107{
108 // destroy GRID_TRICKS before m_grid.
109 m_grid->PopEventHandler( true );
110
111 m_Frame->Unbind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_TEXT_AND_GRAPHICS::onUnitsChanged, this );
112}
113
114
116{
117 BOARD_DESIGN_SETTINGS tempBDS( nullptr, "dummy" );
119
120 m_BrdSettings = &tempBDS; // No, address of stack var does not escape function
121
124
125 m_BrdSettings = saveBDS;
126
127 aEvent.Skip();
128}
129
130
132{
133 wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
134
135#define SET_MILS_CELL( row, col, val ) \
136 m_grid->SetCellValue( row, col, m_Frame->StringFromValue( val, true ) )
137
138#define DISABLE_CELL( row, col ) \
139 m_grid->SetReadOnly( row, col ); m_grid->SetCellBackgroundColour( row, col, disabledColour );
140
141 for( int i = 0; i < ROW_COUNT; ++i )
142 {
144
145 if( i == ROW_EDGES || i == ROW_COURTYARD )
146 {
152 }
153 else
154 {
158 m_grid->SetCellValue( i, COL_TEXT_ITALIC, m_BrdSettings->m_TextItalic[ i ] ? wxT( "1" )
159 : wxT( "" ) );
160 m_grid->SetCellValue( i, COL_TEXT_UPRIGHT, m_BrdSettings->m_TextUpright[ i ] ? wxT( "1" )
161 : wxT( "" ) );
162
163 auto attr = new wxGridCellAttr;
164 attr->SetRenderer( new wxGridCellBoolRenderer() );
165 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
166 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
167 m_grid->SetAttr( i, COL_TEXT_ITALIC, attr );
168
169 attr = new wxGridCellAttr;
170 attr->SetRenderer( new wxGridCellBoolRenderer() );
171 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
172 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
173 m_grid->SetAttr( i, COL_TEXT_UPRIGHT, attr );
174 }
175 }
176
177 // Work around an issue where wxWidgets doesn't calculate the row width on its own
178 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
179 m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col ) );
180
181 m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
182
183 Layout();
184
185 m_dimensionsPanel->TransferDataToWindow();
186
187 return true;
188}
189
190
192{
194 return false;
195
196 // A minimal value for sizes and thickness
197 const int minWidth = pcbIUScale.mmToIU( MINIMUM_LINE_WIDTH_MM );
198 const int maxWidth = pcbIUScale.mmToIU( MAXIMUM_LINE_WIDTH_MM );
200 const int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
201 wxString errorsMsg;
202 UNITS_PROVIDER* unitProvider = m_Frame;
203
204 for( int i = 0; i < ROW_COUNT; ++i )
205 {
206 bool badParam = false;
207
208 int lineWidth = m_grid->GetUnitValue( i, COL_LINE_THICKNESS );
209
210 if( lineWidth < minWidth || lineWidth > maxWidth )
211 {
212 if( !errorsMsg.IsEmpty() )
213 errorsMsg += wxT( "\n\n" );
214
215 errorsMsg += wxString::Format( _( "%s: Incorrect line width.\n"
216 "It must be between %s and %s" ),
217 m_grid->GetRowLabelValue( i ),
218 unitProvider->StringFromValue( minWidth , true),
219 unitProvider->StringFromValue( maxWidth , true) );
220 badParam = true;
221 }
222
223 if( !badParam )
224 m_BrdSettings->m_LineThickness[ i ] = lineWidth;
225
226 if( i == ROW_EDGES || i == ROW_COURTYARD )
227 continue;
228
229 badParam = false;
230 int textWidth = m_grid->GetUnitValue( i, COL_TEXT_WIDTH );
231 int textHeight = m_grid->GetUnitValue( i, COL_TEXT_HEIGHT );
232 int textThickness = m_grid->GetUnitValue( i, COL_TEXT_THICKNESS );
233
234 if( textWidth < minSize || textHeight < minSize || textWidth > maxSize || textHeight > maxSize )
235 {
236 if( !errorsMsg.IsEmpty() )
237 errorsMsg += wxT( "\n\n" );
238
239 errorsMsg += wxString::Format( _( "%s: Text size is incorrect.\n"
240 "Size must be between %s and %s" ),
241 m_grid->GetRowLabelValue( i ),
242 unitProvider->StringFromValue( minSize , true),
243 unitProvider->StringFromValue( maxSize , true) );
244 badParam = true;
245 }
246
247 // Text thickness cannot be > text size /4 to be readable
248 int textMaxThickness = std::min( maxWidth, std::min( textWidth, textHeight ) /4);
249
250 if( !badParam && ( textThickness < minWidth || textThickness > textMaxThickness ) )
251 {
252 if( !errorsMsg.IsEmpty() )
253 errorsMsg += wxT( "\n\n" );
254
255 if( textThickness > textMaxThickness )
256 {
257 errorsMsg += wxString::Format( _( "%s: Text thickness is too large.\n"
258 "It will be truncated to %s" ),
259 m_grid->GetRowLabelValue( i ),
260 unitProvider->StringFromValue( textMaxThickness , true) );
261 }
262 else if( textThickness < minWidth )
263 {
264 errorsMsg += wxString::Format( _( "%s: Text thickness is too small.\n"
265 "It will be truncated to %s" ),
266 m_grid->GetRowLabelValue( i ),
267 unitProvider->StringFromValue( minWidth , true ) );
268 }
269
270 textThickness = std::min( textThickness, textMaxThickness );
271 textThickness = std::max( textThickness, minWidth );
272 SET_MILS_CELL( i, COL_TEXT_THICKNESS, textThickness );
273 }
274
275 if( !badParam )
276 {
277 m_BrdSettings->m_TextSize[ i ] = VECTOR2I( textWidth, textHeight );
278 m_BrdSettings->m_TextThickness[ i ] = textThickness;
279 }
280
282 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) );
284 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_UPRIGHT ) );
285 }
286
287 m_dimensionsPanel->TransferDataFromWindow();
288
289 if( errorsMsg.IsEmpty() )
290 return true;
291
292 KIDIALOG dlg( wxGetTopLevelParent( this ), errorsMsg, KIDIALOG::KD_ERROR, _( "Parameter error" ) );
293 dlg.ShowModal();
294
295 return false;
296}
297
298
300{
302 return;
303
304 BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
305
306 m_BrdSettings = &aBoard->GetDesignSettings();
308
309 m_BrdSettings = savedSettings;
310}
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.
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:317
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1024
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:50
@ KD_ERROR
Definition: kidialog.h:53
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:916
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:854
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition: wx_grid.h:145
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:832
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
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:46
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition: eda_text.h:47
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:92
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695