1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import sys
import os
import glob
import wx
from wx.svg import SVGimage
#----------------------------------------------------------------------
class SVGBitmapDisplay(wx.Panel):
"""
A simple panel containing a static box and a rasterized SVG image
"""
def __init__(self, parent, bmp_size, *args, **kw):
wx.Panel.__init__(self, parent, *args, **kw)
self.bmp_size = wx.Size(*bmp_size)
self.statbmp = wx.StaticBitmap(self, bitmap=wx.Bitmap(*self.bmp_size))
label='{}x{}'.format(self.bmp_size.width, self.bmp_size.height)
sbox = wx.StaticBoxSizer(wx.VERTICAL, self, label)
sbox.Add(self.statbmp)
self.SetSizer(sbox)
if not self.IsDoubleBuffered():
self.SetDoubleBuffered(True) # Reduce flicker on size event.
def UpdateSVG(self, svg_filename):
img = SVGimage.CreateFromFile(svg_filename)
bmp = img.ConvertToScaledBitmap(self.bmp_size, self)
self.statbmp.SetBitmap(bmp)
#print(bmp.GetSize())
#----------------------------------------------------------------------
ADD_NEW = '[Double-click to Add New File]'
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
self.listbox = wx.ListBox(self, style=wx.LB_SINGLE, size=(250, -1))
self.listbox.Append(ADD_NEW)
self.listbox.Append(glob.glob(os.path.join('data', '*.svg')))
self.updateables = []
rightSizer = wx.BoxSizer(wx.VERTICAL)
topRowSizer = wx.BoxSizer(wx.HORIZONTAL)
# Add a few smallish bitmaps in a row
for d in [32, 64, 128]:
sbd = SVGBitmapDisplay(self, (d,d))
self.updateables.append(sbd)
topRowSizer.Add(sbd, wx.SizerFlags().Border().Top())
rightSizer.Add(topRowSizer)
# and add another, larger one below that row
sbd = SVGBitmapDisplay(self, (256,256))
self.updateables.append(sbd)
rightSizer.Add(sbd)
self.Sizer = wx.BoxSizer(wx.HORIZONTAL)
self.Sizer.Add(self.listbox, wx.SizerFlags(1).Border(wx.ALL, 10).Expand())
self.Sizer.Add(rightSizer, wx.SizerFlags(2).Border(wx.RIGHT|wx.BOTTOM|wx.TOP, 10).Expand())
self.Bind(wx.EVT_LISTBOX, self.OnSelectItem)
self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnDClickItem)
self.listbox.SetSelection(1)
# Load the first SVG in the list into the static bitmaps
self.UpdateAll(self.listbox.GetString(1))
def OnSelectItem(self, evt):
filename = self.listbox.GetStringSelection()
if filename != ADD_NEW:
self.UpdateAll(filename)
def OnDClickItem(self, evt):
if self.listbox.GetSelection() == 0:
with wx.FileDialog(self, "Select SVG file", "data",
wildcard="SVG files (*.svg)|*.svg",
style=wx.FD_OPEN) as dlg:
if dlg.ShowModal() == wx.ID_OK:
self.listbox.Insert(dlg.GetPath(), 1)
self.listbox.SetSelection(1)
self.UpdateAll(self.listbox.GetString(1))
def UpdateAll(self, svg_filename):
for item in self.updateables:
item.UpdateSVG(svg_filename)
self.Layout()
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#----------------------------------------------------------------------
overview = """<html><body>
<h2><center>SVGImage</center></h2>
The wx.svg.SVGimage class provides the ability to load, parse and render
Scalable Vector Graphics (SVG) files. The advantage of SVG files is that
they can be used to create bitmaps of any size without loss of quality.
<p>
This sample demonstrates rasterizing an SVG image to wx.Bitmaps of various
sizes.
</body></html>
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|