KiCad PCB EDA Suite
BITMAP_STORE Class Reference

Helper to retrieve bitmaps while handling icon themes and scaling. More...

#include <bitmap_store.h>

Public Member Functions

 BITMAP_STORE ()
 
 ~BITMAP_STORE ()=default
 
wxBitmap GetBitmap (BITMAPS aBitmapId, int aHeight=-1)
 Retrieves a bitmap from the given bitmap id. More...
 
wxBitmap GetBitmapScaled (BITMAPS aBitmapId, int aScaleFactor, int aHeight=-1)
 Retrieves a bitmap from the given bitmap id, scaled to a given factor. More...
 
void ThemeChanged ()
 Notifies the store that the icon theme has been changed by the user, so caches must be invalidated. More...
 
bool IsDarkTheme () const
 

Private Member Functions

wxImage getImage (BITMAPS aBitmapId, int aHeight=-1)
 
const wxString & bitmapName (BITMAPS aBitmapId, int aHeight=-1)
 
wxString computeBitmapName (BITMAPS aBitmapId, int aHeight=-1)
 
void buildBitmapInfoCache ()
 

Private Attributes

std::unique_ptr< ASSET_ARCHIVEm_archive
 
std::unordered_map< std::pair< BITMAPS, int >, wxString > m_bitmapNameCache
 
std::unordered_map< BITMAPS, std::vector< BITMAP_INFO > > m_bitmapInfoCache
 
wxString m_theme
 

Detailed Description

Helper to retrieve bitmaps while handling icon themes and scaling.

Definition at line 43 of file bitmap_store.h.

Constructor & Destructor Documentation

◆ BITMAP_STORE()

BITMAP_STORE::BITMAP_STORE ( )

Definition at line 104 of file bitmap_store.cpp.

105{
106 wxFileName path( PATHS::GetStockDataPath() + wxT( "/resources" ), IMAGE_ARCHIVE );
107
108 wxLogTrace( traceBitmaps, "Loading bitmaps from " + path.GetFullPath() );
109
110 m_archive = std::make_unique<ASSET_ARCHIVE>( path.GetFullPath() );
111
113
114 ThemeChanged();
115}
static const wxString traceBitmaps
static const wxString IMAGE_ARCHIVE
void buildBitmapInfoCache()
void ThemeChanged()
Notifies the store that the icon theme has been changed by the user, so caches must be invalidated.
std::unique_ptr< ASSET_ARCHIVE > m_archive
Definition: bitmap_store.h:91
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:150

References buildBitmapInfoCache(), PATHS::GetStockDataPath(), IMAGE_ARCHIVE, m_archive, path, ThemeChanged(), and traceBitmaps.

◆ ~BITMAP_STORE()

BITMAP_STORE::~BITMAP_STORE ( )
default

Member Function Documentation

◆ bitmapName()

const wxString & BITMAP_STORE::bitmapName ( BITMAPS  aBitmapId,
int  aHeight = -1 
)
private

Definition at line 196 of file bitmap_store.cpp.

197{
198 std::pair<BITMAPS, int> key = std::make_pair( aBitmapId, aHeight );
199
200 if( !m_bitmapNameCache.count( key ) )
201 m_bitmapNameCache[key] = computeBitmapName( aBitmapId, aHeight );
202
203 return m_bitmapNameCache.at( key );
204}
wxString computeBitmapName(BITMAPS aBitmapId, int aHeight=-1)
std::unordered_map< std::pair< BITMAPS, int >, wxString > m_bitmapNameCache
Definition: bitmap_store.h:93

References computeBitmapName(), and m_bitmapNameCache.

Referenced by getImage().

◆ buildBitmapInfoCache()

void BITMAP_STORE::buildBitmapInfoCache ( )
private

Definition at line 240 of file bitmap_store.cpp.

241{
243}
void BuildBitmapInfo(std::unordered_map< BITMAPS, std::vector< BITMAP_INFO > > &aBitmapInfoCache)
This file is auto-generated by CMake when MAINTAIN_PNGS is on.
Definition: bitmap_info.cpp:27
std::unordered_map< BITMAPS, std::vector< BITMAP_INFO > > m_bitmapInfoCache
Definition: bitmap_store.h:95

References BuildBitmapInfo(), and m_bitmapInfoCache.

Referenced by BITMAP_STORE().

◆ computeBitmapName()

wxString BITMAP_STORE::computeBitmapName ( BITMAPS  aBitmapId,
int  aHeight = -1 
)
private

Definition at line 207 of file bitmap_store.cpp.

208{
209 if( !m_bitmapInfoCache.count( aBitmapId ) )
210 {
211 wxLogTrace( traceBitmaps, "No bitmap info available for %d", aBitmapId );
212 return wxEmptyString;
213 }
214
215 wxString fn;
216
217 for( const BITMAP_INFO& info : m_bitmapInfoCache.at( aBitmapId ) )
218 {
219 if( info.theme != m_theme )
220 continue;
221
222 if( aHeight < 0 || info.height == aHeight )
223 {
224 fn = info.filename;
225 break;
226 }
227 }
228
229 if( fn.IsEmpty() )
230 {
231 wxLogTrace( traceBitmaps, "No bitmap found matching ID %d, height %d, theme %s",
232 aBitmapId, aHeight, m_theme );
233 return m_bitmapInfoCache.at( aBitmapId ).begin()->filename;
234 }
235
236 return fn;
237}
wxString m_theme
Definition: bitmap_store.h:97

References info, m_bitmapInfoCache, m_theme, and traceBitmaps.

Referenced by bitmapName().

◆ GetBitmap()

wxBitmap BITMAP_STORE::GetBitmap ( BITMAPS  aBitmapId,
int  aHeight = -1 
)

Retrieves a bitmap from the given bitmap id.

Parameters
aBitmapIdis from the BITMAPS enum in bitmaps_list.h
aHeightis the requested height in pixels of the source image, or -1 for any height

Definition at line 118 of file bitmap_store.cpp.

119{
120 return wxBitmap( getImage( aBitmapId, aHeight ) );
121}
wxImage getImage(BITMAPS aBitmapId, int aHeight=-1)

References getImage().

Referenced by KiBitmap().

◆ GetBitmapScaled()

wxBitmap BITMAP_STORE::GetBitmapScaled ( BITMAPS  aBitmapId,
int  aScaleFactor,
int  aHeight = -1 
)

Retrieves a bitmap from the given bitmap id, scaled to a given factor.

This factor is for legacy reasons divided by 4, so a scale factor of 4 will return the original image.

Todo:
this should be improved to take advantage of a number of different resolution PNGs stored in the asset archive, so we take the closest PNG and scale it rather than always starting with a low-resolution version.
Parameters
aBitmapIdis from the BITMAPS enum in bitmaps_list.h
aScaleFactoris used to scale the bitmap uniformly
aHeightis the requested height in pixels of the source image to scale from

Definition at line 124 of file bitmap_store.cpp.

125{
126 wxImage image = getImage( aBitmapId, aHeight );
127
128 // Bilinear seems to genuinely look better for these line-drawing icons
129 // than bicubic, despite claims in the wx documentation that bicubic is
130 // "highest quality". I don't recommend changing this. Bicubic looks
131 // blurry and makes me want an eye exam.
132 image.Rescale( aScaleFactor * image.GetWidth() / 4, aScaleFactor * image.GetHeight() / 4,
133 wxIMAGE_QUALITY_BILINEAR );
134
135 return wxBitmap( image );
136}

References getImage(), and image.

Referenced by KiScaledBitmap().

◆ getImage()

wxImage BITMAP_STORE::getImage ( BITMAPS  aBitmapId,
int  aHeight = -1 
)
private

Definition at line 139 of file bitmap_store.cpp.

140{
141 const unsigned char* data = nullptr;
142 long count;
143
144 if( aBitmapId == BITMAPS::dummy_item )
145 {
146 data = s_dummyItem;
147 count = sizeof( s_dummyItem );
148 }
149 else
150 {
151 count = m_archive->GetFilePointer( bitmapName( aBitmapId, aHeight ), &data );
152
153 if( count < 0 )
154 {
155 wxLogTrace( traceBitmaps, "Bitmap for %d, %d, %s has an info tag with file %s,"
156 "but that file could not be found in the archive!",
157 aBitmapId, aHeight, m_theme );
158 data = s_imageNotFound;
159 count = sizeof( s_imageNotFound );
160 }
161 }
162
163 wxMemoryInputStream is( data, count );
164 wxImage image( is, wxBITMAP_TYPE_PNG );
165
166 return image;
167}
static const unsigned char s_dummyItem[]
Icon used for EDA_ITEMs that don't have a custom icon configured.
static const unsigned char s_imageNotFound[]
A question-mark icon shown when we can't find a given bitmap in the archive.
const wxString & bitmapName(BITMAPS aBitmapId, int aHeight=-1)

References bitmapName(), dummy_item, image, m_archive, m_theme, s_dummyItem, s_imageNotFound, and traceBitmaps.

Referenced by GetBitmap(), and GetBitmapScaled().

◆ IsDarkTheme()

bool BITMAP_STORE::IsDarkTheme ( ) const
inline

Definition at line 79 of file bitmap_store.h.

79{ return m_theme == wxT( "dark" ); }

References m_theme.

◆ ThemeChanged()

void BITMAP_STORE::ThemeChanged ( )

Notifies the store that the icon theme has been changed by the user, so caches must be invalidated.

Definition at line 170 of file bitmap_store.cpp.

171{
172 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
173 wxString oldTheme = m_theme;
174
175 if( settings )
176 {
177 switch( settings->m_Appearance.icon_theme )
178 {
179 case ICON_THEME::LIGHT: m_theme = wxT( "light" ); break;
180 case ICON_THEME::DARK: m_theme = wxT( "dark" ); break;
181 case ICON_THEME::AUTO:
182 m_theme = KIPLATFORM::UI::IsDarkTheme() ? wxT( "dark" ) : wxT( "light" );
183 break;
184 }
185 }
186 else
187 {
188 m_theme = wxT( "light" );
189 }
190
191 if( !oldTheme.IsSameAs( m_theme ) )
192 m_bitmapNameCache.clear();
193}
APPEARANCE m_Appearance
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111

References AUTO, DARK, COMMON_SETTINGS::APPEARANCE::icon_theme, KIPLATFORM::UI::IsDarkTheme(), LIGHT, COMMON_SETTINGS::m_Appearance, m_bitmapNameCache, m_theme, and Pgm().

Referenced by BITMAP_STORE(), EDA_BASE_FRAME::CommonSettingsChanged(), EDA_BASE_FRAME::HandleSystemColorChange(), ACTION_TOOLBAR::onThemeChanged(), PANEL_KICAD_LAUNCHER::onThemeChanged(), PROJECT_TREE_PANE::onThemeChanged(), and PCB_CALCULATOR_FRAME::onThemeChanged().

Member Data Documentation

◆ m_archive

std::unique_ptr<ASSET_ARCHIVE> BITMAP_STORE::m_archive
private

Definition at line 91 of file bitmap_store.h.

Referenced by BITMAP_STORE(), and getImage().

◆ m_bitmapInfoCache

std::unordered_map<BITMAPS, std::vector<BITMAP_INFO> > BITMAP_STORE::m_bitmapInfoCache
private

Definition at line 95 of file bitmap_store.h.

Referenced by buildBitmapInfoCache(), and computeBitmapName().

◆ m_bitmapNameCache

std::unordered_map<std::pair<BITMAPS, int>, wxString> BITMAP_STORE::m_bitmapNameCache
private

Definition at line 93 of file bitmap_store.h.

Referenced by bitmapName(), and ThemeChanged().

◆ m_theme

wxString BITMAP_STORE::m_theme
private

Definition at line 97 of file bitmap_store.h.

Referenced by computeBitmapName(), getImage(), IsDarkTheme(), and ThemeChanged().


The documentation for this class was generated from the following files: