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 100 of file bitmap_store.cpp.

101 {
102  wxFileName path( PATHS::GetStockDataPath() + wxT( "/resources" ), IMAGE_ARCHIVE );
103 
104  wxLogTrace( traceBitmaps, "Loading bitmaps from " + path.GetFullPath() );
105 
106  m_archive = std::make_unique<ASSET_ARCHIVE>( path.GetFullPath() );
107 
109 
110  ThemeChanged();
111 }
void buildBitmapInfoCache()
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
void ThemeChanged()
Notifies the store that the icon theme has been changed by the user, so caches must be invalidated.
static const wxString traceBitmaps
std::unique_ptr< ASSET_ARCHIVE > m_archive
Definition: bitmap_store.h:91
static const wxString IMAGE_ARCHIVE

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 193 of file bitmap_store.cpp.

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

References computeBitmapName(), and m_bitmapNameCache.

Referenced by getImage().

◆ buildBitmapInfoCache()

void BITMAP_STORE::buildBitmapInfoCache ( )
private

Definition at line 237 of file bitmap_store.cpp.

238 {
239  for( const BITMAP_INFO& entry : g_BitmapInfo )
240  m_bitmapInfoCache[entry.id].emplace_back( entry );
241 }
const std::vector< BITMAP_INFO > g_BitmapInfo
Definition: bitmap_info.h:42
std::unordered_map< BITMAPS, std::vector< BITMAP_INFO > > m_bitmapInfoCache
Definition: bitmap_store.h:95
This file is auto-generated by CMake when MAINTAIN_PNGS is on.
Definition: bitmap_info.h:33

References g_BitmapInfo, and m_bitmapInfoCache.

Referenced by BITMAP_STORE().

◆ computeBitmapName()

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

Definition at line 204 of file bitmap_store.cpp.

205 {
206  if( !m_bitmapInfoCache.count( aBitmapId ) )
207  {
208  wxLogTrace( traceBitmaps, "No bitmap info available for %d", aBitmapId );
209  return wxEmptyString;
210  }
211 
212  wxString fn;
213 
214  for( const BITMAP_INFO& info : m_bitmapInfoCache.at( aBitmapId ) )
215  {
216  if( info.theme != m_theme )
217  continue;
218 
219  if( aHeight < 0 || info.height == aHeight )
220  {
221  fn = info.filename;
222  break;
223  }
224  }
225 
226  if( fn.IsEmpty() )
227  {
228  wxLogTrace( traceBitmaps, "No bitmap found matching ID %d, height %d, theme %s",
229  aBitmapId, aHeight, m_theme );
230  return m_bitmapInfoCache.at( aBitmapId ).begin()->filename;
231  }
232 
233  return fn;
234 }
wxString m_theme
Definition: bitmap_store.h:97
std::unordered_map< BITMAPS, std::vector< BITMAP_INFO > > m_bitmapInfoCache
Definition: bitmap_store.h:95
This file is auto-generated by CMake when MAINTAIN_PNGS is on.
Definition: bitmap_info.h:33
static const wxString traceBitmaps

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 114 of file bitmap_store.cpp.

115 {
116  return wxBitmap( getImage( aBitmapId, aHeight ) );
117 }
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 120 of file bitmap_store.cpp.

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

References getImage(), and image.

Referenced by KiScaledBitmap().

◆ getImage()

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

Definition at line 135 of file bitmap_store.cpp.

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

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 167 of file bitmap_store.cpp.

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

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(), PANEL_KICAD_LAUNCHER::onThemeChanged(), PCB_CALCULATOR_FRAME::onThemeChanged(), PROJECT_TREE_PANE::onThemeChanged(), and ACTION_TOOLBAR::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: