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 [=]( const wxAny&& aValue, EDA_ITEM* ) -> VALIDATOR_RESULT
90 {
91 int value;
92
93 if( !aValue.GetAs( &value ) )
94 return std::make_unique<VALIDATION_ERROR_MSG>( _( "Enter an integer value." ) );
95
97
98 if( value < aMin )
99 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( value, aMin, type );
100
101 if( value > aMax )
102 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( value, aMax, type );
103
104 return std::nullopt;
105 };
106}
107
108
110{
112 {
116
118 anchors.Map( LT_CORNER, _HKI( "Top left" ) )
119 .Map( RT_CORNER, _HKI( "Top right" ) )
120 .Map( LB_CORNER, _HKI( "Bottom left" ) )
121 .Map( RB_CORNER, _HKI( "Bottom right" ) );
122
124 pages.Map( ALL_PAGES, _HKI( "All pages" ) )
125 .Map( FIRST_PAGE_ONLY, _HKI( "First page only" ) )
126 .Map( SUBSEQUENT_PAGES, _HKI( "Subsequent pages" ) );
127
128 // EDA_TEXT also registers these enums; initialization order is unspecified.
130
131 if( hAlign.Choices().GetCount() == 0 )
132 {
133 hAlign.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
134 .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
135 .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
136 }
137
139
140 if( vAlign.Choices().GetCount() == 0 )
141 {
142 vAlign.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
143 .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
144 .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
145 }
146
147 auto isText = []( INSPECTABLE* aItem )
148 {
149 return static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType() == DS_DATA_ITEM::DS_TEXT;
150 };
151
152 auto hasEnd = []( INSPECTABLE* aItem )
153 {
154 auto type = static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType();
155 return type == DS_DATA_ITEM::DS_SEGMENT || type == DS_DATA_ITEM::DS_RECT;
156 };
157
158 auto hasRotation = []( INSPECTABLE* aItem )
159 {
160 auto type = static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType();
161 return type == DS_DATA_ITEM::DS_TEXT || type == DS_DATA_ITEM::DS_POLYPOLYGON;
162 };
163
164 auto isBitmap = []( INSPECTABLE* aItem )
165 {
166 return static_cast<PL_ITEM_PROPERTIES*>( aItem )->GetItem()->GetType() == DS_DATA_ITEM::DS_BITMAP;
167 };
168
171
174
177 _HKI( "Position" ) );
178
181 _HKI( "Position" ) );
182
186 _HKI( "Position" ) );
187
190 _HKI( "End Position" ) )
191 .SetAvailableFunc( hasEnd );
192
195 _HKI( "End Position" ) )
196 .SetAvailableFunc( hasEnd );
197
201 _HKI( "End Position" ) )
202 .SetAvailableFunc( hasEnd );
203
207 [isBitmap]( INSPECTABLE* aItem )
208 {
209 return !isBitmap( aItem );
210 } )
211 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 10 ), true ) )
212 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
213
216 .SetAvailableFunc( hasRotation )
218 []( const wxAny&& aValue, EDA_ITEM* ) -> VALIDATOR_RESULT
219 {
220 double value;
221
222 if( !aValue.GetAs( &value ) || !std::isfinite( value ) )
223 return std::make_unique<VALIDATION_ERROR_MSG>( _( "Enter a finite angle." ) );
224
225 return std::nullopt;
226 } );
227
230 .SetAvailableFunc( isBitmap )
231 .SetValidator( rangeValidator( 1, INT_MAX, false ) );
232
235 _HKI( "Text Properties" ) )
236 .SetAvailableFunc( isText )
238 _HKI( "Text supports ${keyword} variables. Click the button to edit multiple lines." ) );
239
242 _HKI( "Text Properties" ) )
243 .SetAvailableFunc( isText )
245 []( INSPECTABLE* )
246 {
247 wxPGChoices fonts;
248 std::vector<std::string> names;
249 Fontconfig()->ListFonts( names, std::string( Pgm().GetLanguageTag().utf8_str() ) );
250 fonts.Add( KICAD_FONT_NAME );
251
252 for( const std::string& name : names )
253 fonts.Add( wxString( name ) );
254
255 return fonts;
256 } );
257
260 _HKI( "Text Properties" ) )
261 .SetAvailableFunc( isText )
262 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 100 ), true ) )
263 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
264
265 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Text Height" ),
268 _HKI( "Text Properties" ) )
269 .SetAvailableFunc( isText )
270 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 100 ), true ) )
271 .SetHelpString( _HKI( "Set to 0 to use the drawing sheet default." ) );
272
275 _HKI( "Text Properties" ) )
276 .SetAvailableFunc( isText );
277
280 _HKI( "Text Properties" ) )
281 .SetAvailableFunc( isText );
282
286 _HKI( "Text Properties" ) )
287 .SetAvailableFunc( isText );
288
292 _HKI( "Text Properties" ) )
293 .SetAvailableFunc( isText );
294
298 _HKI( "Text Properties" ) )
299 .SetAvailableFunc( isText );
300
301 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Maximum Text Width" ),
304 _HKI( "Text Properties" ) )
305 .SetAvailableFunc( isText )
306 .SetValidator( rangeValidator( 0, INT_MAX, true ) );
307
308 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Maximum Text Height" ),
311 _HKI( "Text Properties" ) )
312 .SetAvailableFunc( isText )
313 .SetValidator( rangeValidator( 0, INT_MAX, true ) );
314
315 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Repeat Count" ),
318 _HKI( "Repeat" ) )
320
323 _HKI( "Repeat" ) );
324
327 _HKI( "Repeat" ) );
328
329 mgr.AddProperty( new PROPERTY<PL_ITEM_PROPERTIES, int>( _HKI( "Label Increment" ),
332 _HKI( "Repeat" ) )
333 .SetAvailableFunc( isText );
334
335 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Width" ),
338 _HKI( "Defaults" ) )
340 rangeValidator( drawSheetIUScale.mmToIU( 0.01 ), drawSheetIUScale.mmToIU( 100 ), true ) );
341
342 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Height" ),
345 _HKI( "Defaults" ) )
347 rangeValidator( drawSheetIUScale.mmToIU( 0.01 ), drawSheetIUScale.mmToIU( 100 ), true ) );
348
349 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Line Width" ),
352 _HKI( "Defaults" ) )
353 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 10 ), true ) );
354
355 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Default Text Thickness" ),
358 _HKI( "Defaults" ) )
359 .SetValidator( rangeValidator( 0, drawSheetIUScale.mmToIU( 5 ), true ) );
360
361 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Left Margin" ),
364 _HKI( "Margins" ) );
365
366 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Right Margin" ),
369 _HKI( "Margins" ) );
370
373 _HKI( "Margins" ) );
374
375 mgr.AddProperty( new PROPERTY<PL_PAGE_PROPERTIES, int>( _HKI( "Bottom Margin" ),
378 _HKI( "Margins" ) );
379 }
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:798
static ENUM_MAP< T > & Instance()
Definition property.h:792
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:380
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:860
#define ENUM_TO_WXANY(type)
Definition property.h:899
@ 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