Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import wx
from enable.savage.svg.backends.kiva import renderer
from enable.savage.svg.document import SVGDocument
from enable.api import Container, Window
from traits.api import Instance, Float
class KivaContainer(Container):
document = Instance(SVGDocument)
zoom = Float(100.0)
def draw(self, gc, view_bounds=None, mode="default"):
gc.clear()
if not self.document:
gc.show_text_at_point("No Document", 20, 20)
return
with gc:
# SVG origin is upper right with y positive is down. argh.
# Set up the transforms to fix this up.
gc.translate_ctm(0, gc.height())
# zoom percentage
scale = float(self.zoom) / 100.0
gc.scale_ctm(scale, -scale)
self.document.render(gc)
# Now scale the gc and render
gc.Scale(w_scale, h_scale)
self.toggle_document.render(gc)
# And return the scaling factor back to what it originally was
# and return to the origial location
gc.Scale(1/w_scale, 1/h_scale)
gc.Translate(-x_offset, -y_offset)
class SVGButtonEditor ( Editor ):
""" Traits UI 'display only' image editor.
"""
document = Instance(SVGDocument)
toggle_document = Instance(SVGDocument)
#---------------------------------------------------------------------------
# Editor API
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.document = SVGDocument.createFromFile(self.factory.filename, renderer=Renderer)
# load the button toggle document which will be displayed when the
# button is toggled.
if self.factory.toggle_filename:
def _load_svg_document(self, filename):
if not os.path.exists(filename):
raise ValueError
tree = etree.parse(filename)
root = tree.getroot()
return SVGDocument(root, renderer=KivaRenderer)
def openFile(self, filenameOrBuffer):
start = time.time()
tree = etree.parse(filenameOrBuffer)
try:
if self.profileLoading:
import cProfile
p = cProfile.Profile()
p.enable()
self.document = document.SVGDocument(tree.getroot())
if self.profileLoading:
import pstats
p.disable()
results = pstats.Stats(p)
self.profileResults.SetResults(results)
else:
self.profileResults.SetResults(None)
self.render.document = self.document
except:
#pdb.set_trace()
import traceback
self.render.document = None
traceback.print_exc()
amount = time.time() - start
from __future__ import with_statement
import os.path
import xml.etree.cElementTree as etree
from chaco.abstract_overlay import AbstractOverlay
from pyface.timer.timer import Timer
from traits.api import Instance, Str, Enum, Float, Int
from enable.savage.svg.document import SVGDocument
from enable.savage.svg.backends.kiva.renderer import Renderer as KivaRenderer
class StatusLayer(AbstractOverlay):
filename = Str()
document = Instance(SVGDocument)
# Default size attributes if the svg does not specify them
doc_width = 48.0
doc_height = 48.0
# The type determines if the layer is displayed as part of the component's
# overlay or underlays
type = Enum('overlay', 'underlay')
# The position of the legend with respect to its overlaid component.
#
# * c = Center
# * ur = Upper Right
# * ul = Upper Left
# * ll = Lower Left
# * lr = Lower Right
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.document = SVGDocument.createFromFile(self.factory.filename, renderer=Renderer)
# load the button toggle document which will be displayed when the
# button is toggled.
if self.factory.toggle_filename:
self.toggle_document = SVGDocument.createFromFile(self.factory.toggle_filename, renderer=Renderer)
else:
tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', 'button_toggle.svg'))
self.toggle_document = SVGDocument(tree.getroot(), renderer=Renderer)
padding = (self.factory.width_padding, self.factory.height_padding)
self.control = ButtonRenderPanel( parent, self, padding=padding )
if self.factory.tooltip != '':
self.control.SetToolTip(wx.ToolTip(self.factory.tooltip))
svg_w, svg_h = self.control.GetBestSize()
self.control.zoom_x /= float(svg_w) / self.factory.width
self.control.zoom_y /= float(svg_h) / self.factory.height
self.control.Refresh()
import os.path
import xml.etree.cElementTree as etree
from enable.api import Container, Component, ComponentEditor, BaseTool
from kiva.constants import MODERN
from kiva.fonttools import Font
from traits.api import Instance, Callable, List, Str, HasTraits, Enum
from traitsui.api import View, Item
from enable.savage.svg.document import SVGDocument
from enable.savage.svg.backends.kiva.renderer import Renderer as KivaRenderer
class CanvasButton(Component):
document = Instance(SVGDocument)
toggle_document = Instance(SVGDocument)
label = Str()
callback = Callable
callback_args = List(Str)
state = Enum('up', 'down')
bounds = [64, 64]
def __init__(self, filename, callback, callback_args, *args, **kw):
super(CanvasButton, self).__init__(*args, **kw)
self.document = self._load_svg_document(filename)
# set the toggle doc if it wasn't passed in as a keyword arg
if self.toggle_document is None:
toggle_filename = os.path.join(os.path.dirname(__file__),
'button_toggle.svg')
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.document = SVGDocument.createFromFile(self.factory.filename, renderer=Renderer)
# load the button toggle document which will be displayed when the
# button is toggled.
if self.factory.toggle_filename:
self.toggle_document = SVGDocument.createFromFile(self.factory.toggle_filename, renderer=Renderer)
else:
tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', 'button_toggle.svg'))
self.toggle_document = SVGDocument(tree.getroot(), renderer=Renderer)
padding = (self.factory.width_padding, self.factory.height_padding)
self.control = ButtonRenderPanel( parent, self, padding=padding )
if self.factory.tooltip != '':
self.control.SetToolTip(wx.ToolTip(self.factory.tooltip))
svg_w, svg_h = self.control.GetBestSize()
self.control.zoom_x /= float(svg_w) / self.factory.width
self.control.zoom_y /= float(svg_h) / self.factory.height
self.control.Refresh()
def __init__(self, component, *args, **kw):
super(StatusLayer, self).__init__(component, *args, **kw)
if self.document is None:
if self.filename == '':
self.filename = os.path.join(os.path.dirname(__file__), 'data',
'Dialog-error.svg')
tree = etree.parse(self.filename)
root = tree.getroot()
self.document = SVGDocument(root, renderer=KivaRenderer)
if hasattr(self.document, 'getSize'):
self.doc_width = self.document.getSize()[0]
self.doc_height = self.document.getSize()[1]