Move properties inspector font-listing to SetChoicesFunc().

This commit is contained in:
Jeff Young 2025-08-06 17:11:30 +01:00
parent 4f8a4e4e2b
commit 1e9da4684a
10 changed files with 66 additions and 93 deletions

View File

@ -993,46 +993,33 @@ wxString EDA_TEXT::GetFontName() const
}
int EDA_TEXT::GetFontIndex() const
wxString EDA_TEXT::GetFontProp() const
{
if( !GetFont() )
return -1;
if( KIFONT::FONT* font = GetFont() )
return font->GetName();
if( GetFont()->GetName() == KICAD_FONT_NAME )
return -2;
std::vector<std::string> fontNames;
Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
for( int ii = 0; ii < (int) fontNames.size(); ++ii )
{
if( fontNames[ii] == GetFont()->GetName() )
return ii;
}
return 0;
if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
return _( "Default Font" );
else
return KICAD_FONT_NAME;
}
void EDA_TEXT::SetFontIndex( int aIdx )
void EDA_TEXT::SetFontProp( const wxString& aFontName )
{
if( aIdx == -1 )
if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
{
SetFont( nullptr );
}
else if( aIdx == -2 )
{
SetFont( KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() ) );
if( aFontName == _( "Default Font" ) )
SetFont( nullptr );
else
SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
}
else
{
std::vector<std::string> fontNames;
Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
if( aIdx >= 0 && aIdx < static_cast<int>( fontNames.size() ) )
SetFont( KIFONT::FONT::GetFont( fontNames[ aIdx ], IsBold(), IsItalic() ) );
else
if( aFontName == KICAD_FONT_NAME )
SetFont( nullptr );
else
SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
}
}
@ -1351,12 +1338,30 @@ static struct EDA_TEXT_DESC
&EDA_TEXT::SetText, &EDA_TEXT::GetText ),
textProps );
// This must be a PROPERTY_ENUM to get a choice list.
// SCH_ and PCB_PROPERTIES_PANEL::updateFontList() fill in the enum values.
propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, int>( _HKI( "Font" ),
&EDA_TEXT::SetFontIndex, &EDA_TEXT::GetFontIndex ),
propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Font" ),
&EDA_TEXT::SetFontProp, &EDA_TEXT::GetFontProp ),
textProps )
.SetIsHiddenFromRulesEditor();
.SetIsHiddenFromRulesEditor()
.SetChoicesFunc( []( INSPECTABLE* aItem )
{
EDA_ITEM* eda_item = static_cast<EDA_ITEM*>( aItem );
wxPGChoices fonts;
std::vector<std::string> fontNames;
Fontconfig()->ListFonts( fontNames,
std::string( Pgm().GetLanguageTag().utf8_str() ),
eda_item->GetEmbeddedFonts() );
if( IsEeschemaType( eda_item->Type() ) )
fonts.Add( _( "Default Font" ) );
fonts.Add( KICAD_FONT_NAME );
for( const std::string& fontName : fontNames )
fonts.Add( wxString( fontName ) );
return fonts;
} );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Auto Thickness" ),
&EDA_TEXT::SetAutoThickness, &EDA_TEXT::GetAutoThickness ),

View File

@ -677,6 +677,17 @@ void SCH_ITEM::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_IT
}
const std::vector<wxString>* SCH_ITEM::GetEmbeddedFonts()
{
if( SCHEMATIC* schematic = Schematic() )
return schematic->GetEmbeddedFiles()->GetFontFiles();
else if( SYMBOL* symbol = GetParentSymbol() )
return symbol->GetEmbeddedFiles()->UpdateFontFiles();
return nullptr;
}
static struct SCH_ITEM_DESC
{
SCH_ITEM_DESC()

View File

@ -652,6 +652,8 @@ public:
return m_rule_areas_cache;
}
const std::vector<wxString>* GetEmbeddedFonts() override;
/**
* The list of flags used by the #compare function.
*

View File

@ -22,7 +22,6 @@
#include "sch_properties_panel.h"
#include <font/fontconfig.h>
#include <font/kicad_font_name.h>
#include <pgm_base.h>
#include <connection_graph.h>
#include <properties/pg_editors.h>
@ -89,8 +88,6 @@ SCH_PROPERTIES_PANEL::SCH_PROPERTIES_PANEL( wxWindow* aParent, SCH_BASE_FRAME* a
{
m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( it->second );
}
updateFontList();
}
@ -229,46 +226,8 @@ void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
void SCH_PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
{
PROPERTIES_PANEL::OnLanguageChanged( aEvent );
updateFontList();
aEvent.Skip();
}
void SCH_PROPERTIES_PANEL::updateFontList()
{
wxPGChoices fonts;
const std::vector<wxString>* fontFiles = nullptr;
if( m_frame->GetFrameType() == FRAME_SCH && m_frame->GetScreen() && m_frame->GetScreen()->Schematic() )
{
fontFiles = m_frame->GetScreen()->Schematic()->GetEmbeddedFiles()->GetFontFiles();
}
else if( m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR )
{
SYMBOL_EDIT_FRAME* symbolFrame = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
if( symbolFrame->GetCurSymbol() )
fontFiles = symbolFrame->GetCurSymbol()->GetEmbeddedFiles()->UpdateFontFiles();
}
else if( m_frame->GetFrameType() == FRAME_SCH_VIEWER )
{
SYMBOL_VIEWER_FRAME* symbolFrame = static_cast<SYMBOL_VIEWER_FRAME*>( m_frame );
if( symbolFrame->GetSelectedSymbol() )
fontFiles = symbolFrame->GetSelectedSymbol()->GetEmbeddedFiles()->UpdateFontFiles();
}
// Regnerate font names
std::vector<std::string> fontNames;
Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ), fontFiles );
fonts.Add( _( "Default Font" ), -1 );
fonts.Add( KICAD_FONT_NAME, -2 );
for( int ii = 0; ii < (int) fontNames.size(); ++ii )
fonts.Add( wxString( fontNames[ii] ), ii );
auto fontProperty = m_propMgr.GetProperty( TYPE_HASH( EDA_TEXT ), _HKI( "Font" ) );
fontProperty->SetChoices( fonts );
}

View File

@ -53,9 +53,6 @@ protected:
void OnLanguageChanged( wxCommandEvent& aEvent ) override;
///< Regenerates caches of font list property
void updateFontList();
SCH_BASE_FRAME* m_frame;
PROPERTY_MANAGER& m_propMgr;
PG_UNIT_EDITOR* m_unitEditorInstance;

View File

@ -431,6 +431,8 @@ public:
TransformShapeToPolygon( aBuffer, aLayer, aClearance, aError, aErrorLoc );
}
const std::vector<wxString>* GetEmbeddedFonts() override;
enum COMPARE_FLAGS : int
{
DRC = 0x01,

View File

@ -466,6 +466,7 @@ public:
virtual std::vector<int> ViewGetLayers() const override;
virtual EMBEDDED_FILES* GetEmbeddedFiles() { return nullptr; }
virtual const std::vector<wxString>* GetEmbeddedFonts() { return nullptr; }
#if defined(DEBUG)

View File

@ -248,8 +248,8 @@ public:
wxString GetFontName() const;
void SetFontIndex( int aIdx );
int GetFontIndex() const;
void SetFontProp( const wxString& aFontName );
wxString GetFontProp() const;
void SetLineSpacing( double aLineSpacing );
double GetLineSpacing() const { return m_attributes.m_LineSpacing; }

View File

@ -398,6 +398,15 @@ wxString BOARD_ITEM::GetParentAsString() const
}
const std::vector<wxString>* BOARD_ITEM::GetEmbeddedFonts()
{
if( BOARD* board = GetBoard() )
return board->GetFontFiles();
return nullptr;
}
static struct BOARD_ITEM_DESC
{
BOARD_ITEM_DESC()

View File

@ -293,17 +293,4 @@ void PCB_PROPERTIES_PANEL::updateLists( const BOARD* aBoard )
auto netProperty = m_propMgr.GetProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Net" ) );
netProperty->SetChoices( nets );
// Regenerate font names
std::vector<std::string> fontNames;
Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
aBoard->GetFontFiles() );
fonts.Add( KICAD_FONT_NAME, -1 );
for( int ii = 0; ii < (int) fontNames.size(); ++ii )
fonts.Add( wxString( fontNames[ii] ), ii );
auto fontProperty = m_propMgr.GetProperty( TYPE_HASH( EDA_TEXT ), _HKI( "Font" ) );
fontProperty->SetChoices( fonts );
}