Base class for custom implementations of wx.BitmapBundle.
This class shouldn’t be used directly in the application code, but may be derived from to implement custom bitmap bundles.
Example of use:
class MyCustomBitmapBundleImpl(wx.BitmapBundleImpl):
def __init__(self):
super().__init__()
self.img = wx.Image(pngFile)
self.size = self.img.GetSize()
def GetDefaultSize(self):
# Report the best or default size for the bitmap
return self.size
def GetPreferredBitmapSizeAtScale(self, scale):
# Return the size of the bitmap at the given display scale. It
# doesn't need to be size*scale if there are unscaled bitmaps
# near that size.
return self.size * scale
def GetBitmap(self, size):
# Return the version of the bitmap at the desired size
img = self.img.Scale(size.width, size.height)
return wx.Bitmap(img)
toolBar.AddTool(wx.ID_OPEN, wx.BitmapBundle.FromImpl(MyCustomBitmapBundleImpl())
Full (but still very simple) example of using it can be found in the toolbar sample code.
New in version 4.1/wxWidgets-3.1.6.
Helper for implementing |
|
Retrieve the bitmap of exactly the given size. |
|
Return the size of the bitmaps represented by this bundle in the default |
|
Return the index of the available scale most suitable to be upscaled to the given size. |
|
Return information about the available bitmaps. |
|
Return the preferred size that should be used at the given scale. |
|
|
See |
wx.
BitmapBundleImpl
(RefCounter)¶Base class for custom implementations of BitmapBundle.
DoGetPreferredSize
(self, scale)¶Helper for implementing GetPreferredBitmapSizeAtScale
in the derived classes.
This function implements the standard algorithm used inside wxWidgets itself and tries to find the scale closest to the given one, while also trying to choose one of the available scales, to avoid actually rescaling the bitmaps.
It relies on GetNextAvailableScale
to get information about the available bitmaps, so that function must be overridden if this one is used.
Typically this function is used in the derived classes implementation to forward GetPreferredBitmapSizeAtScale
to it and when this is done, GetBitmap
may also use GetIndexToUpscale
to choose the bitmap to upscale if necessary:
class MyCustomBitmapBundleImpl(wx.BitmapBundleImpl):
def GetDefaultSize():
return wx.Size(32, 32)
def GetPreferredBitmapSizeAtScale(self, scale):
return self.DoGetPreferredSize(scale)
def GetBitmap(self, size):
# For consistency with GetNextAvailableScale(), we must have
# bitmap variants for 32, 48 and 64px sizes.
availableSizes = [32, 48, 64]
if (size.y <= 64)
... get the bitmap from somewhere ...
else:
n = self.GetIndexToUpscale(size)
bitmap = ... get bitmap for availableSizes[n] ...
wx.Bitmap.Rescale(bitmap, size)
return bitmap
def GetNextAvailableScale(self, idx):
# The zero marks the end of available scales, and it means this
# method won't be called again after the zero is returned.
availableScales = [1.0, 1.5, 2.0, 0]
scale = availableScales[idx]
idx += 1
return (scale, idx)
scale (float) – The required scale, typically the same one as passed to GetPreferredBitmapSizeAtScale
.
New in version 4.1/wxWidgets-3.1.7.
GetBitmap
(self, size)¶Retrieve the bitmap of exactly the given size.
Note that this function is non-const because it may generate the bitmap on demand and cache it.
GetDefaultSize
(self)¶Return the size of the bitmaps represented by this bundle in the default DPI
.
Must always return a valid size.
GetIndexToUpscale
(self, size)¶Return the index of the available scale most suitable to be upscaled to the given size.
See DoGetPreferredSize
for an example of using this function.
size (wx.Size) – The required size, typically the same one as passed to GetBitmap
int
New in version 4.1/wxWidgets-3.1.7.
GetNextAvailableScale
(self, idx)¶Return information about the available bitmaps.
Overriding this function is optional and only needs to be done if either DoGetPreferredSize
or GetIndexToUpscale
are called. If you do override it, this function must return the next available scale or 0.0 if there are no more.
The returned scales must be in ascending order and the first returned scale, for the initial i value of 0, should be 1. The function must change i, but the values of this index don’t have to be consecutive and it’s only used by this function itself, the caller only initializes it to 0 before the first call.
See DoGetPreferredSize
for an example of implementing this function.
idx (int) –
tuple
( double, idx )
New in version 4.1/wxWidgets-3.1.7.
GetPreferredBitmapSizeAtScale
(self, scale)¶Return the preferred size that should be used at the given scale.
Must always return a valid size.
scale (float) –
~wxBitmapBundleImpl(self)
DefaultSize
¶See GetDefaultSize