From 1e9da4684aeeaf7beb7dcb05e6da5090820b0f34 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 6 Aug 2025 17:11:30 +0100 Subject: [PATCH] Move properties inspector font-listing to SetChoicesFunc(). --- common/eda_text.cpp | 73 ++++++++++++----------- eeschema/sch_item.cpp | 11 ++++ eeschema/sch_item.h | 2 + eeschema/widgets/sch_properties_panel.cpp | 41 ------------- eeschema/widgets/sch_properties_panel.h | 3 - include/board_item.h | 2 + include/eda_item.h | 1 + include/eda_text.h | 4 +- pcbnew/board_item.cpp | 9 +++ pcbnew/widgets/pcb_properties_panel.cpp | 13 ---- 10 files changed, 66 insertions(+), 93 deletions(-) diff --git a/common/eda_text.cpp b/common/eda_text.cpp index 4d75bfffeb..559b90b7f0 100644 --- a/common/eda_text.cpp +++ b/common/eda_text.cpp @@ -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 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( 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( 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 fontNames; - Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) ); - - if( aIdx >= 0 && aIdx < static_cast( 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( _HKI( "Font" ), - &EDA_TEXT::SetFontIndex, &EDA_TEXT::GetFontIndex ), + propMgr.AddProperty( new PROPERTY( _HKI( "Font" ), + &EDA_TEXT::SetFontProp, &EDA_TEXT::GetFontProp ), textProps ) - .SetIsHiddenFromRulesEditor(); + .SetIsHiddenFromRulesEditor() + .SetChoicesFunc( []( INSPECTABLE* aItem ) + { + EDA_ITEM* eda_item = static_cast( aItem ); + wxPGChoices fonts; + std::vector 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( _HKI( "Auto Thickness" ), &EDA_TEXT::SetAutoThickness, &EDA_TEXT::GetAutoThickness ), diff --git a/eeschema/sch_item.cpp b/eeschema/sch_item.cpp index 2681cc8eb0..a6b378dc60 100644 --- a/eeschema/sch_item.cpp +++ b/eeschema/sch_item.cpp @@ -677,6 +677,17 @@ void SCH_ITEM::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector* 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() diff --git a/eeschema/sch_item.h b/eeschema/sch_item.h index c0e4a990f5..21d970a992 100644 --- a/eeschema/sch_item.h +++ b/eeschema/sch_item.h @@ -652,6 +652,8 @@ public: return m_rule_areas_cache; } + const std::vector* GetEmbeddedFonts() override; + /** * The list of flags used by the #compare function. * diff --git a/eeschema/widgets/sch_properties_panel.cpp b/eeschema/widgets/sch_properties_panel.cpp index 8a4440cc5e..f61a1982b9 100644 --- a/eeschema/widgets/sch_properties_panel.cpp +++ b/eeschema/widgets/sch_properties_panel.cpp @@ -22,7 +22,6 @@ #include "sch_properties_panel.h" #include -#include #include #include #include @@ -89,8 +88,6 @@ SCH_PROPERTIES_PANEL::SCH_PROPERTIES_PANEL( wxWindow* aParent, SCH_BASE_FRAME* a { m_colorEditorInstance = static_cast( 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* 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( m_frame ); - - if( symbolFrame->GetCurSymbol() ) - fontFiles = symbolFrame->GetCurSymbol()->GetEmbeddedFiles()->UpdateFontFiles(); - } - else if( m_frame->GetFrameType() == FRAME_SCH_VIEWER ) - { - SYMBOL_VIEWER_FRAME* symbolFrame = static_cast( m_frame ); - - if( symbolFrame->GetSelectedSymbol() ) - fontFiles = symbolFrame->GetSelectedSymbol()->GetEmbeddedFiles()->UpdateFontFiles(); - } - - // Regnerate font names - std::vector 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 ); -} diff --git a/eeschema/widgets/sch_properties_panel.h b/eeschema/widgets/sch_properties_panel.h index 0599e4e1e9..0ab2834a12 100644 --- a/eeschema/widgets/sch_properties_panel.h +++ b/eeschema/widgets/sch_properties_panel.h @@ -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; diff --git a/include/board_item.h b/include/board_item.h index 5529215dc3..4faa928365 100644 --- a/include/board_item.h +++ b/include/board_item.h @@ -431,6 +431,8 @@ public: TransformShapeToPolygon( aBuffer, aLayer, aClearance, aError, aErrorLoc ); } + const std::vector* GetEmbeddedFonts() override; + enum COMPARE_FLAGS : int { DRC = 0x01, diff --git a/include/eda_item.h b/include/eda_item.h index 86e0dc4a9d..3105573c22 100644 --- a/include/eda_item.h +++ b/include/eda_item.h @@ -466,6 +466,7 @@ public: virtual std::vector ViewGetLayers() const override; virtual EMBEDDED_FILES* GetEmbeddedFiles() { return nullptr; } + virtual const std::vector* GetEmbeddedFonts() { return nullptr; } #if defined(DEBUG) diff --git a/include/eda_text.h b/include/eda_text.h index 0f0b8c31b0..16d6e0f698 100644 --- a/include/eda_text.h +++ b/include/eda_text.h @@ -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; } diff --git a/pcbnew/board_item.cpp b/pcbnew/board_item.cpp index 786ddaa319..134f0f43ae 100644 --- a/pcbnew/board_item.cpp +++ b/pcbnew/board_item.cpp @@ -398,6 +398,15 @@ wxString BOARD_ITEM::GetParentAsString() const } +const std::vector* BOARD_ITEM::GetEmbeddedFonts() +{ + if( BOARD* board = GetBoard() ) + return board->GetFontFiles(); + + return nullptr; +} + + static struct BOARD_ITEM_DESC { BOARD_ITEM_DESC() diff --git a/pcbnew/widgets/pcb_properties_panel.cpp b/pcbnew/widgets/pcb_properties_panel.cpp index fd1c2ec2aa..9602f53367 100644 --- a/pcbnew/widgets/pcb_properties_panel.cpp +++ b/pcbnew/widgets/pcb_properties_panel.cpp @@ -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 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 ); }