KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pl_properties.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
20#include "pl_properties.h"
21
22#include <font/font.h>
23#include <font/fontconfig.h>
25#include <i18n_utility.h>
26#include <pgm_base.h>
27#include <properties/property.h>
30
31#include <cmath>
32#include <climits>
33
34// EDA_TEXT owns the alignment conversions. Declare its specializations here instead of
35// instantiating wxAny's generic enum storage a second time in this module.
40
41
43{
44 if( m_item->GetType() == DS_DATA_ITEM::DS_TEXT )
45 return text()->m_Orient;
46
47 return static_cast<DS_DATA_ITEM_POLYGONS*>( m_item )->m_Orient.AsDegrees();
48}
49
50
52{
53 if( m_item->GetType() == DS_DATA_ITEM::DS_TEXT )
54 text()->m_Orient = aValue;
55 else
56 static_cast<DS_DATA_ITEM_POLYGONS*>( m_item )->m_Orient = EDA_ANGLE( aValue, DEGREES_T );
57}
58
59
61{
62 return text()->m_Font ? text()->m_Font->GetName() : wxString( KICAD_FONT_NAME );
63}
64
65
66void PL_ITEM_PROPERTIES::SetFont( wxString aValue )
67{
68 text()->m_Font = aValue == KICAD_FONT_NAME ? nullptr : KIFONT::FONT::GetFont( aValue, GetBold(), GetItalic() );
69}
70
71
72void PL_ITEM_PROPERTIES::SetBold( bool aValue )
73{
74 text()->m_Bold = aValue;
75 SetFont( GetFont() );
76}
77
78
80{
81 text()->m_Italic = aValue;
82 SetFont( GetFont() );
83}
84
85
86// wxPropertyGrid supplies integer values as longs; accept conversion before checking the range.
87static PROPERTY_VALIDATOR_FN rangeValidator( int aMin, int aMax, bool aDistance = true )
88{
89 return
90 [=]( const wxAny&& aValue, EDA_ITEM* ) -> VALIDATOR_RESULT
91 {
92 int value;
93
94 if( !aValue.GetAs( &value ) )
95 return std::make_unique<VALIDATION_ERROR_MSG>( _( "Enter an integer value." ) );
96
98
99 if( value < aMin )
100 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( value, aMin, type );
101
102 if( value > aMax )
103 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( value, aMax, type );
104
105 return std::nullopt;
106 };
107}
108
109
111{
113 {
117
119 anchors.Map( LT_CORNER, _HKI( "Top left" ) )
120 .Map( RT_CORNER, _HKI( "Top right" ) )
121 .Map( LB_CORNER, _HKI( "Bottom left" ) )
122 .Map( RB_CORNER, _HKI( "Bottom right" ) );
123
125 pages.Map( ALL_PAGES, _HKI( "All pages" ) )
126 .Map( FIRST_PAGE_ONLY, _HKI( "First page only" ) )
127 .Map( SUBSEQUENT_PAGES, _HKI( "Subsequent pages" ) );
128
129 // EDA_TEXT also registers these enums; initialization order is unspecified.
131
132 if( hAlign.Choices().GetCount() == 0 )
133 {
134 hAlign.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
135 .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
136 .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
137 }
138
140
141 if( vAlign.Choices().GetCount() == 0 )
142 {
143 vAlign.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
144 .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
145 .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
146 }
147
148 auto isText =
149 []( INSPECTABLE* aItem )
150 {
151 return static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType() == DS_DATA_ITEM::DS_TEXT;
152 };
153
154 auto hasEnd =
155 []( INSPECTABLE* aItem )
156 {
157 auto type = static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType();
158 return type == DS_DATA_ITEM::DS_SEGMENT || type == DS_DATA_ITEM::DS_RECT;
159 };
160
161 auto hasRotation =
162 []( INSPECTABLE* aItem )
163 {
164 auto type = static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType();
165 return type == DS_DATA_ITEM::DS_TEXT || type == DS_DATA_ITEM::DS_POLYPOLYGON;
166 };
167
168 auto isBitmap =
169 []( INSPECTABLE* aItem )
170 {
171 return static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType() == DS_DATA_ITEM::DS_BITMAP;
172 };
173
176
179
182 _HKI( "Position" ) );
183
186 _HKI( "Position" ) );
187
191 _HKI( "Position" ) );
192
195 _HKI( "End Position" ) )
196 .SetAvailableFunc( hasEnd );
197
200 _HKI( "End Position" ) )
201 .SetAvailableFunc( hasEnd );
202
206 _HKI( "End Position" ) )
207 .SetAvailableFunc( hasEnd );
208
212 [isBitmap]( INSPECTABLE* aItem )
213 {
214 return !isBitmap( aItem );
215 } )
216 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 10 ), true ) )
217 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
218
221 .SetAvailableFunc( hasRotation )
223 []( const wxAny&& aValue, EDA_ITEM* ) -> VALIDATOR_RESULT
224 {
225 double value;
226
227 if( !aValue.GetAs( &value ) || !std::isfinite( value ) )
228 return std::make_unique<VALIDATION_ERROR_MSG>( _( "Enter a finite angle." ) );
229
230 return std::nullopt;
231 } );
232
235 .SetAvailableFunc( isBitmap )
236 .SetValidator( rangeValidator( 1, INT_MAX, false ) );
237
240 _HKI( "Text Properties" ) )
241 .SetAvailableFunc( isText )
243 _HKI( "Text supports ${keyword} variables. Click the button to edit multiple lines." ) );
244
247 _HKI( "Text Properties" ) )
248 .SetAvailableFunc( isText )
250 []( INSPECTABLE* )
251 {
252 wxPGChoices fonts;
253 std::vector<std::string> names;
254 Fontconfig()->ListFonts( names, std::string( Pgm().GetLanguageTag().utf8_str() ) );
255 fonts.Add( KICAD_FONT_NAME );
256
257 for( const std::string& name : names )
258 fonts.Add( wxString( name ) );
259
260 return fonts;
261 } );
262
265 _HKI( "Text Properties" ) )
266 .SetAvailableFunc( isText )
267 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 100 ), true ) )
268 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
269
270 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Text Height" ),
273 _HKI( "Text Properties" ) )
274 .SetAvailableFunc( isText )
275 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 100 ), true ) )
276 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
277
280 _HKI( "Text Properties" ) )
281 .SetAvailableFunc( isText );
282
285 _HKI( "Text Properties" ) )
286 .SetAvailableFunc( isText );
287
291 _HKI( "Text Properties" ) )
292 .SetAvailableFunc( isText );
293
297 _HKI( "Text Properties" ) )
298 .SetAvailableFunc( isText );
299
303 _HKI( "Text Properties" ) )
304 .SetAvailableFunc( isText );
305
306 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Maximum Text Width" ),
309 _HKI( "Text Properties" ) )
310 .SetAvailableFunc( isText )
311 .SetValidator( rangeValidator( 0, INT_MAX, true ) );
312
313 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Maximum Text Height" ),
316 _HKI( "Text Properties" ) )
317 .SetAvailableFunc( isText )
318 .SetValidator( rangeValidator( 0, INT_MAX, true ) );
319
320 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Repeat Count" ),
323 _HKI( "Repeat" ) )
325
328 _HKI( "Repeat" ) );
329
332 _HKI( "Repeat" ) );
333
334 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Label Increment" ),
337 _HKI( "Repeat" ) )
338 .SetAvailableFunc( isText );
339
340 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Width" ),
343 _HKI( "Defaults" ) )
345 rangeValidator( drawSheetIUScale.mmToIU( 0.01 ), drawSheetIUScale.mmToIU( 100 ), true ) );
346
347 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Height" ),
350 _HKI( "Defaults" ) )
352 rangeValidator( drawSheetIUScale.mmToIU( 0.01 ), drawSheetIUScale.mmToIU( 100 ), true ) );
353
354 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Line Width" ),
357 _HKI( "Defaults" ) )
358 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 10 ), true ) );
359
360 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Thickness" ),
363 _HKI( "Defaults" ) )
364 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 5 ), true ) );
365
366 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Left Margin" ),
369 _HKI( "Margins" ) );
370
371 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Right Margin" ),
374 _HKI( "Margins" ) );
375
378 _HKI( "Margins" ) );
379
380 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Bottom Margin" ),
383 _HKI( "Margins" ) );
384 }
const char * name
constexpr EDA_IU_SCALE drawSheetIUScale
Definition base_units.h:129
KIFONT::FONT * m_Font
double AsDegrees() const
Definition eda_angle.h:115
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition property.h:817
static ENUM_MAP< T > & Instance()
Definition property.h:811
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:144
const wxString & GetName() const
Definition font.h:112
Exposes a drawing-sheet definition to the shared properties panel.
void SetTextHeight(int aValue)
int GetStepX() const
void SetStartAnchor(CORNER_ANCHOR aValue)
void SetStartX(int aValue)
KIGFX::COLOR4D GetTextColor() const
void SetRotation(double aValue)
void SetLineWidth(int aValue)
void SetConstraintHeight(int aValue)
int GetEndY() const
void SetLabelIncrement(int aValue)
GR_TEXT_H_ALIGN_T GetHAlign() const
void SetEndX(int aValue)
int GetTextWidth() const
void SetFont(wxString aValue)
int GetTextHeight() const
int GetLineWidth() const
int GetEndX() const
void SetComment(wxString aValue)
PAGE_OPTION GetPageOption() const
void SetStepY(int aValue)
int GetStepY() const
int GetConstraintWidth() const
PL_ITEM_PROPERTIES(DS_DATA_ITEM *aItem)
int GetStartY() const
void SetItalic(bool aValue)
void SetText(wxString aValue)
int GetConstraintHeight() const
void SetPageOption(PAGE_OPTION aValue)
CORNER_ANCHOR GetStartAnchor() const
void SetConstraintWidth(int aValue)
void SetStartY(int aValue)
GR_TEXT_V_ALIGN_T GetVAlign() const
DS_DATA_ITEM_TEXT * text() const
int GetRepeatCount() const
void SetEndAnchor(CORNER_ANCHOR aValue)
void SetEndY(int aValue)
wxString GetFont() const
wxString GetText() const
CORNER_ANCHOR GetEndAnchor() const
int GetLabelIncrement() const
void SetHAlign(GR_TEXT_H_ALIGN_T aValue)
void SetRepeatCount(int aValue)
void SetTextWidth(int aValue)
bool GetItalic() const
void SetVAlign(GR_TEXT_V_ALIGN_T aValue)
int GetStartX() const
bool GetBold() const
void SetStepX(int aValue)
void SetPPI(int aValue)
void SetTextColor(KIGFX::COLOR4D aValue)
DS_DATA_ITEM * m_item
double GetRotation() const
wxString GetComment() const
void SetBold(bool aValue)
Drawing-sheet defaults and margins, shown when no item is selected.
int GetDefaultTextWidth() const
int GetTopMargin() const
void SetRightMargin(int aValue)
void SetBottomMargin(int aValue)
int GetBottomMargin() const
int GetLeftMargin() const
void SetLeftMargin(int aValue)
int GetDefaultLineWidth() const
int GetDefaultTextHeight() const
void SetTopMargin(int aValue)
void SetDefaultLineWidth(int aValue)
void SetDefaultTextWidth(int aValue)
int GetDefaultTextThickness() const
void SetDefaultTextThickness(int aValue)
void SetDefaultTextHeight(int aValue)
int GetRightMargin() const
PROPERTY_BASE & SetChoicesFunc(std::function< wxPGChoices(INSPECTABLE *)> aFunc)
Definition property.h:289
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:275
PROPERTY_BASE & SetHelpString(const wxString &aHelpString)
Set untranslated help text, marked with _HKI for translation extraction.
Definition property.h:229
PROPERTY_BASE & SetValidator(PROPERTY_VALIDATOR_FN &&aValidator)
Definition property.h:397
Provide class metadata.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
PAGE_OPTION
@ FIRST_PAGE_ONLY
@ SUBSEQUENT_PAGES
@ ALL_PAGES
CORNER_ANCHOR
A coordinate is relative to a page corner.
@ RB_CORNER
@ RT_CORNER
@ LT_CORNER
@ LB_CORNER
constexpr int DS_MAX_REPEAT_COUNT
Upper bound on DS_DATA_ITEM::m_RepeatCount.
#define _(s)
@ DEGREES_T
Definition eda_angle.h:30
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
FONTCONFIG * Fontconfig()
Some functions to handle hotkeys in KiCad.
#define KICAD_FONT_NAME
#define _HKI(x)
Definition page_info.cpp:40
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
static struct PL_PROPERTIES_DESC s_propertiesDesc
static PROPERTY_VALIDATOR_FN rangeValidator(int aMin, int aMax, bool aDistance=true)
#define DECLARE_ENUM_TO_WXANY(type)
Definition property.h:879
#define ENUM_TO_WXANY(type)
Definition property.h:918
@ PT_DEGREE
Angle expressed in degrees.
Definition property.h:66
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
std::function< VALIDATOR_RESULT(const wxAny &&, EDA_ITEM *aItem)> PROPERTY_VALIDATOR_FN
A property validator function takes in the data type of the owning property, and returns a VALIDATOR_...
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP