Add import capability to SVGdraw.py
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
# -*- tab-width: 4; indent-tabs-mode: t -*-
|
||||
|
||||
import presence
|
||||
import avahi
|
||||
|
||||
ACTION_BUDDY_ADDED = "added"
|
||||
ACTION_BUDDY_REMOVED = "removed"
|
||||
|
||||
|
||||
class Buddy(object):
|
||||
def __init__(self, nick, realname, servicename, host, address, port, key=None):
|
||||
self._nick = nick
|
||||
self._realname = realname
|
||||
self._servicename = servicename
|
||||
self._key = key
|
||||
self._host = host
|
||||
self._address = str(address)
|
||||
self._port = int(port)
|
||||
self._chat = None
|
||||
|
||||
def set_chat(self, chat):
|
||||
self._chat = chat
|
||||
|
||||
def chat(self):
|
||||
return self._chat
|
||||
|
||||
def nick(self):
|
||||
return self._nick
|
||||
|
||||
def realname(self):
|
||||
return self._realname
|
||||
|
||||
def servicename(self):
|
||||
return self._servicename
|
||||
|
||||
def host(self):
|
||||
return self._host
|
||||
|
||||
def address(self):
|
||||
return self._address
|
||||
|
||||
def port(self):
|
||||
return self._port
|
||||
|
||||
def key(self):
|
||||
return self._key
|
||||
|
||||
class BuddyList(object):
|
||||
""" Manage a list of buddies """
|
||||
|
||||
def __init__(self, servicename):
|
||||
self._listeners = []
|
||||
self._buddies = {}
|
||||
self._servicename = servicename
|
||||
self._pdiscovery = presence.PresenceDiscovery()
|
||||
self._pdiscovery.add_service_listener(self._on_service_change)
|
||||
|
||||
def start(self):
|
||||
self._pdiscovery.start()
|
||||
|
||||
def add_buddy_listener(self, listener):
|
||||
self._listeners.append(listener)
|
||||
|
||||
def _add_buddy(self, host, address, port, servicename, data):
|
||||
# Ignore ourselves
|
||||
if servicename == self._servicename:
|
||||
return
|
||||
|
||||
if len(data) > 0 and 'name' in data.keys():
|
||||
buddy = self._find_buddy_by_service_name(servicename)
|
||||
if not buddy:
|
||||
buddy = Buddy(data['name'], data['realname'], servicename, host, address, port)
|
||||
self._buddies[data['name']] = buddy
|
||||
self._notify_listeners(ACTION_BUDDY_ADDED, buddy)
|
||||
|
||||
def _remove_buddy(self, buddy):
|
||||
nick = buddy.nick()
|
||||
self._notify_listeners(ACTION_BUDDY_REMOVED, buddy)
|
||||
del self._buddies[nick]
|
||||
|
||||
def _find_buddy_by_service_name(self, servicename):
|
||||
for buddy in self._buddies.values():
|
||||
if buddy.servicename() == servicename:
|
||||
return buddy
|
||||
return None
|
||||
|
||||
def find_buddy_by_address(self, address):
|
||||
for buddy_name in self._buddies.keys():
|
||||
buddy = self._buddies[buddy_name]
|
||||
if buddy.address() == address:
|
||||
return buddy
|
||||
return None
|
||||
|
||||
def _notify_listeners(self, action, buddy):
|
||||
for listener in self._listeners:
|
||||
listener(action, buddy)
|
||||
|
||||
def _on_service_change(self, action, interface, protocol, name, stype, domain, flags):
|
||||
if stype != presence.OLPC_CHAT_SERVICE:
|
||||
return
|
||||
if action == presence.ACTION_SERVICE_NEW:
|
||||
self._pdiscovery.resolve_service(interface, protocol, name, stype, domain, self._on_service_resolved)
|
||||
elif action == presence.ACTION_SERVICE_REMOVED:
|
||||
buddy = self._find_buddy_by_service_name(name)
|
||||
if buddy:
|
||||
self._remove_buddy(buddy)
|
||||
|
||||
def _pair_to_dict(self, l):
|
||||
res = {}
|
||||
for el in l:
|
||||
tmp = el.split('=', 1)
|
||||
if len(tmp) > 1:
|
||||
res[tmp[0]] = tmp[1]
|
||||
else:
|
||||
res[tmp[0]] = ''
|
||||
return res
|
||||
|
||||
def _on_service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
|
||||
data = self._pair_to_dict(avahi.txt_array_to_string_array(txt))
|
||||
self._add_buddy(host, address, port, name, data)
|
||||
|
||||
+515
-110
@@ -69,6 +69,7 @@ __version__="1.0"
|
||||
# Anyway the text based approach is about 60 times faster than using the full dom implementation.
|
||||
use_dom_implementation=0
|
||||
|
||||
from xml.parsers import expat
|
||||
|
||||
import exceptions
|
||||
if use_dom_implementation<>0:
|
||||
@@ -226,7 +227,11 @@ class Attribute:
|
||||
self.value = value
|
||||
self.nsname = nsname
|
||||
self.nsref = nsref
|
||||
|
||||
|
||||
def get_attr_value(attrs, name):
|
||||
return attrs[name]
|
||||
|
||||
|
||||
class SVGelement:
|
||||
"""SVGelement(type,attributes,elements,text,namespace,**args)
|
||||
Creates a arbitrary svg element and is intended to be subclassed not used on its own.
|
||||
@@ -266,9 +271,38 @@ class SVGelement:
|
||||
def setParent(self, parent):
|
||||
self._parent = parent
|
||||
|
||||
def addAttribute(self, attribute):
|
||||
def setAttribute(self, attribute):
|
||||
self._attributes[attribute.name] = attribute
|
||||
|
||||
def delAttribute(self, name):
|
||||
if name in self._attributes.keys():
|
||||
del self._attributes[name]
|
||||
|
||||
def hasAttribute(self, name):
|
||||
if name in self._attributes.keys():
|
||||
return True
|
||||
return False
|
||||
|
||||
def _construct(attributes):
|
||||
raise Exception("Can't construct a default object.")
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
def construct(name, attributes, text=None, cdata=None):
|
||||
try:
|
||||
eltClass = elementTable[name]
|
||||
except KeyError, e:
|
||||
print "Unknown SVG element %s." % e
|
||||
return None
|
||||
element = eltClass._construct(attributes)
|
||||
for attrname, attrvalue in attributes.items():
|
||||
element.setAttribute(Attribute(attrname, attrvalue))
|
||||
if text:
|
||||
element.text = text
|
||||
if cdata:
|
||||
element.cdata = cdata
|
||||
return element
|
||||
construct = staticmethod(construct)
|
||||
|
||||
def toXml(self,level,f):
|
||||
f.write('\t'*level)
|
||||
f.write('<'+self.type)
|
||||
@@ -323,11 +357,16 @@ class tspan(SVGelement):
|
||||
def __repr__(self):
|
||||
s="<tspan"
|
||||
for key, attr in self._attributes.items():
|
||||
s+= ' %s="%s"' % (key,attr.value)
|
||||
s+= ' %s="%s"' % (key,attr.value)
|
||||
s+='>'
|
||||
s+=self.text
|
||||
s+='</tspan>'
|
||||
return s
|
||||
|
||||
def _construct(attributes):
|
||||
return tspan()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class tref(SVGelement):
|
||||
"""tr=tref(link='',**args)
|
||||
@@ -341,14 +380,20 @@ class tref(SVGelement):
|
||||
"""
|
||||
def __init__(self,link,**args):
|
||||
SVGelement.__init__(self,'tref',**args)
|
||||
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
def __repr__(self):
|
||||
s="<tref"
|
||||
|
||||
for key, attr in self._attributes.items():
|
||||
s+= ' %s="%s"' % (key,attr.value)
|
||||
s+= ' %s="%s"' % (key,attr.value)
|
||||
s+='/>'
|
||||
return s
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
return tref(href)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
class spannedtext:
|
||||
"""st=spannedtext(textlist=[])
|
||||
@@ -400,16 +445,23 @@ class rect(SVGelement):
|
||||
raise ValueError, 'both height and width are required'
|
||||
SVGelement.__init__(self,'rect',{'width':width,'height':height},**args)
|
||||
if x<>None:
|
||||
self.addAttribute(Attribute('x', x))
|
||||
self.setAttribute(Attribute('x', x))
|
||||
if y<>None:
|
||||
self.addAttribute(Attribute('y', y))
|
||||
self.setAttribute(Attribute('y', y))
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
|
||||
def _construct(attributes):
|
||||
width = get_attr_value(attributes, 'width')
|
||||
height = get_attr_value(attributes, 'height')
|
||||
return rect(width=width, height=height)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class ellipse(SVGelement):
|
||||
"""e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)
|
||||
|
||||
@@ -425,17 +477,23 @@ class ellipse(SVGelement):
|
||||
raise ValueError, 'both rx and ry are required'
|
||||
SVGelement.__init__(self,'ellipse',{'rx':rx,'ry':ry},**args)
|
||||
if cx<>None:
|
||||
self.addAttribute(Attribute('cx', cx))
|
||||
self.setAttribute(Attribute('cx', cx))
|
||||
if cy<>None:
|
||||
self.addAttribute(Attribute('cy', cy))
|
||||
self.setAttribute(Attribute('cy', cy))
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
|
||||
|
||||
def _construct(attributes):
|
||||
rx = get_attr_value(attributes, 'rx')
|
||||
ry = get_attr_value(attributes, 'ry')
|
||||
return ellipse(rx=rx, ry=ry)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class circle(SVGelement):
|
||||
"""c=circle(x,y,radius,fill,stroke,stroke_width,**args)
|
||||
|
||||
@@ -446,15 +504,24 @@ class circle(SVGelement):
|
||||
raise ValueError, 'r is required'
|
||||
SVGelement.__init__(self,'circle',{'r':r},**args)
|
||||
if cx<>None:
|
||||
self.addAttribute(Attribute('cx', cx))
|
||||
self.setAttribute(Attribute('cx', cx))
|
||||
if cy<>None:
|
||||
self.addAttribute(Attribute('cy', cy))
|
||||
self.setAttribute(Attribute('cy', cy))
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
|
||||
def _construct(attributes):
|
||||
r = get_attr_value(attributes, 'r')
|
||||
if int(r) == 1:
|
||||
return point()
|
||||
else:
|
||||
return circle(r=r)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class point(circle):
|
||||
"""p=point(x,y,color)
|
||||
@@ -462,7 +529,7 @@ class point(circle):
|
||||
A point is defined as a circle with a size 1 radius. It may be more efficient to use a
|
||||
very small rectangle if you use many points because a circle is difficult to render.
|
||||
"""
|
||||
def __init__(self,x,y,fill='black',**args):
|
||||
def __init__(self,x=None,y=None,fill='black',**args):
|
||||
circle.__init__(self,x,y,1,fill,**args)
|
||||
|
||||
class line(SVGelement):
|
||||
@@ -473,17 +540,22 @@ class line(SVGelement):
|
||||
def __init__(self,x1=None,y1=None,x2=None,y2=None,stroke=None,stroke_width=None,**args):
|
||||
SVGelement.__init__(self,'line',**args)
|
||||
if x1<>None:
|
||||
self.addAttribute(Attribute('x1', x1))
|
||||
self.setAttribute(Attribute('x1', x1))
|
||||
if y1<>None:
|
||||
self.addAttribute(Attribute('y1', y1))
|
||||
self.setAttribute(Attribute('y1', y1))
|
||||
if x2<>None:
|
||||
self.addAttribute(Attribute('x2', x2))
|
||||
self.setAttribute(Attribute('x2', x2))
|
||||
if y2<>None:
|
||||
self.addAttribute(Attribute('y2', y2))
|
||||
self.setAttribute(Attribute('y2', y2))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
|
||||
def _construct(attributes):
|
||||
return line()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class polyline(SVGelement):
|
||||
"""pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
|
||||
@@ -493,11 +565,17 @@ class polyline(SVGelement):
|
||||
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
|
||||
SVGelement.__init__(self,'polyline',{'points':_xypointlist(points)},**args)
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
|
||||
def _construct(attributes):
|
||||
points = get_attr_value(attributes, 'points')
|
||||
return polyline(points)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class polygon(SVGelement):
|
||||
"""pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
|
||||
@@ -507,11 +585,17 @@ class polygon(SVGelement):
|
||||
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
|
||||
SVGelement.__init__(self,'polygon',{'points':_xypointlist(points)},**args)
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
|
||||
def _construct(attributes):
|
||||
points = get_attr_value(attributes, 'points')
|
||||
return polygon(points)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class path(SVGelement):
|
||||
"""p=path(path,fill,stroke,stroke_width,**args)
|
||||
@@ -521,14 +605,19 @@ class path(SVGelement):
|
||||
def __init__(self,pathdata,fill=None,stroke=None,stroke_width=None,id=None,**args):
|
||||
SVGelement.__init__(self,'path',{'d':str(pathdata)},**args)
|
||||
if stroke<>None:
|
||||
self.addAttribute(Attribute('stroke', stroke))
|
||||
self.setAttribute(Attribute('stroke', stroke))
|
||||
if fill<>None:
|
||||
self.addAttribute(Attribute('fill', fill))
|
||||
self.setAttribute(Attribute('fill', fill))
|
||||
if stroke_width<>None:
|
||||
self.addAttribute(Attribute('stroke-width', stroke_width))
|
||||
self.setAttribute(Attribute('stroke-width', stroke_width))
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
|
||||
def _construct(attributes):
|
||||
pathdata = get_attr_value(attributes, 'd')
|
||||
return path(d)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class text(SVGelement):
|
||||
"""t=text(x,y,text,font_size,font_family,**args)
|
||||
@@ -538,17 +627,21 @@ class text(SVGelement):
|
||||
def __init__(self,x=None,y=None,text=None,font_size=None,font_family=None,text_anchor=None,**args):
|
||||
SVGelement.__init__(self,'text',**args)
|
||||
if x<>None:
|
||||
self.addAttribute(Attribute('x', x))
|
||||
self.setAttribute(Attribute('x', x))
|
||||
if y<>None:
|
||||
self.addAttribute(Attribute('y', y))
|
||||
self.setAttribute(Attribute('y', y))
|
||||
if font_size<>None:
|
||||
self.addAttribute(Attribute('font-size', font_size))
|
||||
self.setAttribute(Attribute('font-size', font_size))
|
||||
if font_family<>None:
|
||||
self.addAttribute(Attribute('font-family', font_family))
|
||||
self.setAttribute(Attribute('font-family', font_family))
|
||||
if text<>None:
|
||||
self.text=text
|
||||
if text_anchor<>None:
|
||||
self.addAttribute(Attribute('text-anchor', text_anchor))
|
||||
self.setAttribute(Attribute('text-anchor', text_anchor))
|
||||
|
||||
def _construct(attributes):
|
||||
return text()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class textpath(SVGelement):
|
||||
@@ -558,10 +651,18 @@ class textpath(SVGelement):
|
||||
"""
|
||||
def __init__(self,link,text=None,**args):
|
||||
SVGelement.__init__(self,'textPath',**args)
|
||||
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
if text<>None:
|
||||
self.text=text
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
return textpath(href)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class pattern(SVGelement):
|
||||
"""p=pattern(x,y,width,height,patternUnits,**args)
|
||||
|
||||
@@ -572,15 +673,20 @@ class pattern(SVGelement):
|
||||
def __init__(self,x=None,y=None,width=None,height=None,patternUnits=None,**args):
|
||||
SVGelement.__init__(self,'pattern',**args)
|
||||
if x<>None:
|
||||
self.addAttribute(Attribute('x', x))
|
||||
self.setAttribute(Attribute('x', x))
|
||||
if y<>None:
|
||||
self.addAttribute(Attribute('y', y))
|
||||
self.setAttribute(Attribute('y', y))
|
||||
if width<>None:
|
||||
self.addAttribute(Attribute('width', width))
|
||||
self.setAttribute(Attribute('width', width))
|
||||
if height<>None:
|
||||
self.addAttribute(Attribute('height', height))
|
||||
self.setAttribute(Attribute('height', height))
|
||||
if patternUnits<>None:
|
||||
self.addAttribute(Attribute('patternUnits', patternUnits))
|
||||
self.setAttribute(Attribute('patternUnits', patternUnits))
|
||||
|
||||
def _construct(attributes):
|
||||
return pattern()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class title(SVGelement):
|
||||
"""t=title(text,**args)
|
||||
@@ -593,6 +699,11 @@ class title(SVGelement):
|
||||
if text<>None:
|
||||
self.text=text
|
||||
|
||||
def _construct(attributes):
|
||||
return title()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class description(SVGelement):
|
||||
"""d=description(text,**args)
|
||||
|
||||
@@ -604,6 +715,11 @@ class description(SVGelement):
|
||||
if text<>None:
|
||||
self.text=text
|
||||
|
||||
def _construct(attributes):
|
||||
return description()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class lineargradient(SVGelement):
|
||||
"""lg=lineargradient(x1,y1,x2,y2,id,**args)
|
||||
|
||||
@@ -613,15 +729,20 @@ class lineargradient(SVGelement):
|
||||
def __init__(self,x1=None,y1=None,x2=None,y2=None,id=None,**args):
|
||||
SVGelement.__init__(self,'linearGradient',**args)
|
||||
if x1<>None:
|
||||
self.addAttribute(Attribute('x1', x1))
|
||||
self.setAttribute(Attribute('x1', x1))
|
||||
if y1<>None:
|
||||
self.addAttribute(Attribute('y1', y1))
|
||||
self.setAttribute(Attribute('y1', y1))
|
||||
if x2<>None:
|
||||
self.addAttribute(Attribute('x2', x2))
|
||||
self.setAttribute(Attribute('x2', x2))
|
||||
if y2<>None:
|
||||
self.addAttribute(Attribute('y2', y2))
|
||||
self.setAttribute(Attribute('y2', y2))
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
|
||||
def _construct(attributes):
|
||||
return lineargradient()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class radialgradient(SVGelement):
|
||||
"""rg=radialgradient(cx,cy,r,fx,fy,id,**args)
|
||||
@@ -632,17 +753,22 @@ class radialgradient(SVGelement):
|
||||
def __init__(self,cx=None,cy=None,r=None,fx=None,fy=None,id=None,**args):
|
||||
SVGelement.__init__(self,'radialGradient',**args)
|
||||
if cx<>None:
|
||||
self.addAttribute(Attribute('cx', cx))
|
||||
self.setAttribute(Attribute('cx', cx))
|
||||
if cy<>None:
|
||||
self.addAttribute(Attribute('cy', cy))
|
||||
self.setAttribute(Attribute('cy', cy))
|
||||
if r<>None:
|
||||
self.addAttribute(Attribute('r', r))
|
||||
self.setAttribute(Attribute('r', r))
|
||||
if fx<>None:
|
||||
self.addAttribute(Attribute('fx', fx))
|
||||
self.setAttribute(Attribute('fx', fx))
|
||||
if fy<>None:
|
||||
self.addAttribute(Attribute('fy', fy))
|
||||
self.setAttribute(Attribute('fy', fy))
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
|
||||
def _construct(attributes):
|
||||
return radialgradient()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class stop(SVGelement):
|
||||
"""st=stop(offset,stop_color,**args)
|
||||
@@ -652,7 +778,12 @@ class stop(SVGelement):
|
||||
def __init__(self,offset,stop_color=None,**args):
|
||||
SVGelement.__init__(self,'stop',{'offset':offset},**args)
|
||||
if stop_color<>None:
|
||||
self.addAttribute(Attribute('stop-color', stop_color))
|
||||
self.setAttribute(Attribute('stop-color', stop_color))
|
||||
|
||||
def _construct(attributes):
|
||||
offset = get_attr_value(attributes, 'offset')
|
||||
return stop(offset)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
class style(SVGelement):
|
||||
"""st=style(type,cdata=None,**args)
|
||||
@@ -661,6 +792,11 @@ class style(SVGelement):
|
||||
"""
|
||||
def __init__(self,type,cdata=None,**args):
|
||||
SVGelement.__init__(self,'style',{'type':type},cdata=cdata, **args)
|
||||
|
||||
def _construct(attributes):
|
||||
type = get_attr_value(attributes, 'type')
|
||||
return style(type)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class image(SVGelement):
|
||||
@@ -677,12 +813,22 @@ class image(SVGelement):
|
||||
else:
|
||||
raise ValueError, 'both height and width are required'
|
||||
SVGelement.__init__(self,'image',{'width':width,'height':height},**args)
|
||||
self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
||||
if x<>None:
|
||||
self.addAttribute(Attribute('x', x))
|
||||
self.setAttribute(Attribute('x', x))
|
||||
if y<>None:
|
||||
self.addAttribute(Attribute('y', y))
|
||||
self.setAttribute(Attribute('y', y))
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
width = get_attr_value(attributes, 'width')
|
||||
height = get_attr_value(attributes, 'height')
|
||||
return image(href, width=width, height=height)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class cursor(SVGelement):
|
||||
"""c=cursor(url,**args)
|
||||
|
||||
@@ -690,7 +836,14 @@ class cursor(SVGelement):
|
||||
"""
|
||||
def __init__(self,url,**args):
|
||||
SVGelement.__init__(self,'cursor',**args)
|
||||
self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef))
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
return cursor(href)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class marker(SVGelement):
|
||||
@@ -702,17 +855,22 @@ class marker(SVGelement):
|
||||
def __init__(self,id=None,viewBox=None,refx=None,refy=None,markerWidth=None,markerHeight=None,**args):
|
||||
SVGelement.__init__(self,'marker',**args)
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
if viewBox<>None:
|
||||
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
if refx<>None:
|
||||
self.addAttribute(Attribute('refX', refx))
|
||||
self.setAttribute(Attribute('refX', refx))
|
||||
if refy<>None:
|
||||
self.addAttribute(Attribute('refY', refy))
|
||||
self.setAttribute(Attribute('refY', refy))
|
||||
if markerWidth<>None:
|
||||
self.addAttribute(Attribute('markerWidth', markerWidth))
|
||||
self.setAttribute(Attribute('markerWidth', markerWidth))
|
||||
if markerHeight<>None:
|
||||
self.addAttribute(Attribute('markerHeight', markerHeight))
|
||||
self.setAttribute(Attribute('markerHeight', markerHeight))
|
||||
|
||||
def _construct(attributes):
|
||||
return marker()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class group(SVGelement):
|
||||
"""g=group(id,**args)
|
||||
@@ -723,7 +881,11 @@ class group(SVGelement):
|
||||
def __init__(self,id=None,**args):
|
||||
SVGelement.__init__(self,'g',**args)
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
|
||||
def _construct(attributes):
|
||||
return group()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
class symbol(SVGelement):
|
||||
"""sy=symbol(id,viewbox,**args)
|
||||
@@ -737,9 +899,14 @@ class symbol(SVGelement):
|
||||
def __init__(self,id=None,viewBox=None,**args):
|
||||
SVGelement.__init__(self,'symbol',**args)
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
if viewBox<>None:
|
||||
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
|
||||
def _construct(attributes):
|
||||
return symbol()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class defs(SVGelement):
|
||||
"""d=defs(**args)
|
||||
@@ -749,6 +916,11 @@ class defs(SVGelement):
|
||||
def __init__(self,**args):
|
||||
SVGelement.__init__(self,'defs',**args)
|
||||
|
||||
def _construct(attributes):
|
||||
return defs()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class switch(SVGelement):
|
||||
"""sw=switch(**args)
|
||||
|
||||
@@ -759,7 +931,11 @@ class switch(SVGelement):
|
||||
def __init__(self,**args):
|
||||
SVGelement.__init__(self,'switch',**args)
|
||||
|
||||
|
||||
def _construct(attributes):
|
||||
return switch()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class use(SVGelement):
|
||||
"""u=use(link,x,y,width,height,**args)
|
||||
|
||||
@@ -767,18 +943,25 @@ class use(SVGelement):
|
||||
"""
|
||||
def __init__(self,link,x=None,y=None,width=None,height=None,**args):
|
||||
SVGelement.__init__(self,'use',**args)
|
||||
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
if x<>None:
|
||||
self.addAttribute(Attribute('x', x))
|
||||
self.setAttribute(Attribute('x', x))
|
||||
if y<>None:
|
||||
self.addAttribute(Attribute('y', y))
|
||||
self.setAttribute(Attribute('y', y))
|
||||
|
||||
if width<>None:
|
||||
self.addAttribute(Attribute('width', width))
|
||||
self.setAttribute(Attribute('width', width))
|
||||
if height<>None:
|
||||
self.addAttribute(Attribute('height', height))
|
||||
|
||||
|
||||
self.setAttribute(Attribute('height', height))
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
return use(href)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class link(SVGelement):
|
||||
"""a=link(url,**args)
|
||||
|
||||
@@ -787,7 +970,15 @@ class link(SVGelement):
|
||||
"""
|
||||
def __init__(self,link='',**args):
|
||||
SVGelement.__init__(self,'a',**args)
|
||||
self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef))
|
||||
|
||||
def _construct(attributes):
|
||||
href = get_attr_value(attributes, 'xlink:href')
|
||||
if href and href.startswith("xlink:"):
|
||||
href = href[len("xlink:"):]
|
||||
return link(href)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class view(SVGelement):
|
||||
"""v=view(id,**args)
|
||||
@@ -796,7 +987,12 @@ class view(SVGelement):
|
||||
def __init__(self,id=None,**args):
|
||||
SVGelement.__init__(self,'view',**args)
|
||||
if id<>None:
|
||||
self.addAttribute(Attribute('id', id))
|
||||
self.setAttribute(Attribute('id', id))
|
||||
|
||||
def _construct(attributes):
|
||||
return view()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class script(SVGelement):
|
||||
"""sc=script(type,type,cdata,**args)
|
||||
@@ -806,6 +1002,12 @@ class script(SVGelement):
|
||||
"""
|
||||
def __init__(self,type,cdata=None,**args):
|
||||
SVGelement.__init__(self,'script',{'type':type},cdata=cdata,**args)
|
||||
|
||||
def _construct(attributes):
|
||||
type = get_attr_value(attributes, 'type')
|
||||
return script(type, cdata=cdata)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class animate(SVGelement):
|
||||
"""an=animate(attribute,from,to,during,**args)
|
||||
@@ -815,23 +1017,34 @@ class animate(SVGelement):
|
||||
def __init__(self,attribute,fr=None,to=None,dur=None,**args):
|
||||
SVGelement.__init__(self,'animate',{'attributeName':attribute},**args)
|
||||
if fr<>None:
|
||||
self.addAttribute(Attribute('from', fr))
|
||||
self.setAttribute(Attribute('from', fr))
|
||||
if to<>None:
|
||||
self.addAttribute(Attribute('to', to))
|
||||
self.setAttribute(Attribute('to', to))
|
||||
if dur<>None:
|
||||
self.addAttribute(Attribute('dur', dur))
|
||||
self.setAttribute(Attribute('dur', dur))
|
||||
|
||||
def _construct(attributes):
|
||||
attribute = get_attr_value(attributes, 'attributeName')
|
||||
return animate(attribute)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class animateMotion(SVGelement):
|
||||
"""an=animateMotion(pathdata,dur,**args)
|
||||
|
||||
animates a SVGelement over the given path in dur seconds
|
||||
"""
|
||||
def __init__(self,pathdata,dur,**args):
|
||||
def __init__(self,pathdata=None,dur=None,**args):
|
||||
SVGelement.__init__(self,'animateMotion',**args)
|
||||
if pathdata<>None:
|
||||
self.addAttribute(Attribute('path', str(pathdata)))
|
||||
self.setAttribute(Attribute('path', str(pathdata)))
|
||||
if dur<>None:
|
||||
self.addAttribute(Attribute('dur', dur))
|
||||
self.setAttribute(Attribute('dur', dur))
|
||||
|
||||
def _construct(attributes):
|
||||
return animateMotion()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class animateTransform(SVGelement):
|
||||
"""antr=animateTransform(type,from,to,dur,**args)
|
||||
@@ -842,13 +1055,19 @@ class animateTransform(SVGelement):
|
||||
SVGelement.__init__(self,'animateTransform',{'attributeName':'transform'},**args)
|
||||
#As far as I know the attributeName is always transform
|
||||
if type<>None:
|
||||
self.addAttribute(Attribute('type', type))
|
||||
self.setAttribute(Attribute('type', type))
|
||||
if fr<>None:
|
||||
self.addAttribute(Attribute('from', fr))
|
||||
self.setAttribute(Attribute('from', fr))
|
||||
if to<>None:
|
||||
self.addAttribute(Attribute('to', to))
|
||||
self.setAttribute(Attribute('to', to))
|
||||
if dur<>None:
|
||||
self.addAttribute(Attribute('dur', dur))
|
||||
self.setAttribute(Attribute('dur', dur))
|
||||
|
||||
def _construct(attributes):
|
||||
return animateTransform()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class animateColor(SVGelement):
|
||||
"""ac=animateColor(attribute,type,from,to,dur,**args)
|
||||
|
||||
@@ -857,13 +1076,20 @@ class animateColor(SVGelement):
|
||||
def __init__(self,attribute,type=None,fr=None,to=None,dur=None,**args):
|
||||
SVGelement.__init__(self,'animateColor',{'attributeName':attribute},**args)
|
||||
if type<>None:
|
||||
self.addAttribute(Attribute('type', type))
|
||||
self.setAttribute(Attribute('type', type))
|
||||
if fr<>None:
|
||||
self.addAttribute(Attribute('from', fr))
|
||||
self.setAttribute(Attribute('from', fr))
|
||||
if to<>None:
|
||||
self.addAttribute(Attribute('to', to))
|
||||
self.setAttribute(Attribute('to', to))
|
||||
if dur<>None:
|
||||
self.addAttribute(Attribute('dur', dur))
|
||||
self.setAttribute(Attribute('dur', dur))
|
||||
|
||||
def _construct(attributes):
|
||||
attribute = get_attr_value(attributes, 'attributeName')
|
||||
return animateColor(attribute)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class set(SVGelement):
|
||||
"""st=set(attribute,to,during,**args)
|
||||
|
||||
@@ -872,12 +1098,16 @@ class set(SVGelement):
|
||||
def __init__(self,attribute,to=None,dur=None,**args):
|
||||
SVGelement.__init__(self,'set',{'attributeName':attribute},**args)
|
||||
if to<>None:
|
||||
self.addAttribute(Attribute('to', to))
|
||||
self.setAttribute(Attribute('to', to))
|
||||
if dur<>None:
|
||||
self.addAttribute(Attribute('dur', dur))
|
||||
self.setAttribute(Attribute('dur', dur))
|
||||
|
||||
def _construct(attributes):
|
||||
attribute = get_attr_value(attributes, 'attributeName')
|
||||
return set(attribute)
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
|
||||
class svg(SVGelement):
|
||||
"""s=svg(viewbox,width,height,**args)
|
||||
|
||||
@@ -896,12 +1126,113 @@ class svg(SVGelement):
|
||||
def __init__(self,viewBox=None, width=None, height=None,**args):
|
||||
SVGelement.__init__(self,'svg',**args)
|
||||
if viewBox<>None:
|
||||
self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox)))
|
||||
if width<>None:
|
||||
self.addAttribute(Attribute('width', width))
|
||||
self.setAttribute(Attribute('width', width))
|
||||
if height<>None:
|
||||
self.addAttribute(Attribute('height', height_))
|
||||
self.setAttribute(Attribute('height', height_))
|
||||
self.namespace="http://www.w3.org/2000/svg"
|
||||
|
||||
def _construct(attributes):
|
||||
return svg()
|
||||
_construct = staticmethod(_construct)
|
||||
|
||||
|
||||
class ElementHelper(object):
|
||||
def __init__(self, name, attrs):
|
||||
self.name = name
|
||||
self.attrs = attrs
|
||||
self.cdata = None
|
||||
self.text = None
|
||||
|
||||
class Stack(object):
|
||||
def __init__(self):
|
||||
self._stack = []
|
||||
|
||||
def push(self, obj):
|
||||
self._stack.append(obj)
|
||||
|
||||
def pop(self):
|
||||
if len(self._stack) == 0:
|
||||
return None
|
||||
obj = self._stack[-1]
|
||||
del self._stack[-1]
|
||||
return obj
|
||||
|
||||
def peek(self):
|
||||
if len(self._stack) == 0:
|
||||
return None
|
||||
return self._stack[-1]
|
||||
|
||||
def is_empty(self):
|
||||
if len(self._stack) == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def size(self):
|
||||
return len(self._stack)
|
||||
|
||||
class SVGImportParser(object):
|
||||
def __init__(self):
|
||||
self._indent = 0
|
||||
self._cdata = 0
|
||||
self._stack = Stack()
|
||||
self._base = None
|
||||
self._debug = False
|
||||
|
||||
def getBaseElement(self):
|
||||
return self._base
|
||||
|
||||
def log(self, msg):
|
||||
if self._debug:
|
||||
print " " * self._indent + msg
|
||||
|
||||
def StartElementHandler(self, name, attrs):
|
||||
self.log("<%s>" % name)
|
||||
self._indent = self._indent + 1
|
||||
for attrname, attrvalue in attrs.items():
|
||||
self.log("%s = %s" % (attrname, attrvalue))
|
||||
parent = self._stack.peek()
|
||||
elt = SVGelement.construct(name, attrs)
|
||||
if not self._base:
|
||||
self._base = elt
|
||||
if parent:
|
||||
parent.addElement(elt)
|
||||
self._stack.push(elt)
|
||||
|
||||
def StartCdataSectionHandler(self):
|
||||
self._cdata = 1
|
||||
|
||||
def CharacterDataHandler(self, content):
|
||||
if self._cdata:
|
||||
self.log("CDATA = '%s'" % content)
|
||||
elt = self._stack.peek()
|
||||
if elt:
|
||||
elt.cdata = elt.cdata + content
|
||||
else:
|
||||
if len(content) and not content.isspace():
|
||||
self.log("Text = '%s'" % content.strip())
|
||||
elt = self._stack.peek()
|
||||
if elt:
|
||||
elt.text = elt.text + content.strip()
|
||||
|
||||
def EndCdataSectionHandler(self):
|
||||
self._cdata = 0
|
||||
|
||||
def EndElementHandler(self, name):
|
||||
self._indent = self._indent - 1
|
||||
self.log("</%s>" % name)
|
||||
self._stack.pop()
|
||||
|
||||
def CommentHandler(self, comment):
|
||||
self.log("Comment = '%s'" % comment)
|
||||
|
||||
def StartNamespaceDeclHandler(self, prefix, uri):
|
||||
self.log("Namespace = '%s' -> '%s'" % (prefix, uri))
|
||||
|
||||
def EndNamespaceDeclHandler(self, prefix):
|
||||
self.log("Namespace = '%s'" % prefix)
|
||||
|
||||
|
||||
class drawing:
|
||||
"""d=drawing()
|
||||
@@ -921,6 +1252,30 @@ class drawing:
|
||||
self.svg=svg
|
||||
#Voeg een element toe aan de grafiek toe.
|
||||
|
||||
def fromXml(self, xml):
|
||||
HANDLER_NAMES = [
|
||||
'StartElementHandler', 'EndElementHandler',
|
||||
'CharacterDataHandler',
|
||||
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
|
||||
'CommentHandler',
|
||||
'StartCdataSectionHandler', 'EndCdataSectionHandler',
|
||||
]
|
||||
|
||||
# Create a parser
|
||||
parser = expat.ParserCreate()
|
||||
|
||||
handler = SVGImportParser()
|
||||
for name in HANDLER_NAMES:
|
||||
setattr(parser, name, getattr(handler, name))
|
||||
parser.returns_unicode = 0
|
||||
|
||||
# Parse the XML
|
||||
parser.Parse(xml)
|
||||
base_element = handler.getBaseElement()
|
||||
if not base_element or not isinstance(base_element, svg):
|
||||
raise Exception("Base element was not SVG.")
|
||||
self.setSVG(base_element)
|
||||
|
||||
if use_dom_implementation==0:
|
||||
def toXml(self, filename='',compress=False):
|
||||
import cStringIO
|
||||
@@ -1032,13 +1387,60 @@ class drawing:
|
||||
print "SVG well formed"
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
elementTable = {
|
||||
'tspan': tspan,
|
||||
'tref': tref,
|
||||
'rect': rect,
|
||||
'ellipse': ellipse,
|
||||
'circle': circle,
|
||||
'line': line,
|
||||
'polyline': polyline,
|
||||
'polygon': polygon,
|
||||
'path': path,
|
||||
'text': text,
|
||||
'textPath': textpath,
|
||||
'pattern': pattern,
|
||||
'title': title,
|
||||
'desc': description,
|
||||
'linearGradient': lineargradient,
|
||||
'radialGradient': radialgradient,
|
||||
'stop': stop,
|
||||
'style': style,
|
||||
'image': image,
|
||||
'cursor': cursor,
|
||||
'marker': marker,
|
||||
'g': group,
|
||||
'symbol': symbol,
|
||||
'defs': defs,
|
||||
'switch': switch,
|
||||
'use': use,
|
||||
'a': link,
|
||||
'view': view,
|
||||
'script': script,
|
||||
'animate': animate,
|
||||
'animateMotion': animateMotion,
|
||||
'animateTransform': animateTransform,
|
||||
'animateColor': animateColor,
|
||||
'set': set,
|
||||
'svg': svg
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
d=drawing()
|
||||
import sys
|
||||
f = open(sys.argv[1], "r")
|
||||
fc = f.read()
|
||||
d.fromXml(fc)
|
||||
print d.toXml()
|
||||
return
|
||||
|
||||
|
||||
s=svg((0,0,100,100))
|
||||
r=rect(-100,-100,300,300,'cyan')
|
||||
s.addElement(r)
|
||||
|
||||
text = tspan("Foobar")
|
||||
s.addElement(text)
|
||||
|
||||
t=title('SVGdraw Demo')
|
||||
s.addElement(t)
|
||||
@@ -1052,8 +1454,8 @@ if __name__=='__main__':
|
||||
pd.relsmbezier(10,5,0,10)
|
||||
pd.relsmbezier(-10,5,0,10)
|
||||
an=animateMotion(pd,10)
|
||||
an.addAttribute(Attribute('rotate', 'auto-reverse'))
|
||||
an.addAttribute(Attribute('repeatCount', "indefinite"))
|
||||
an.setAttribute(Attribute('rotate', 'auto-reverse'))
|
||||
an.setAttribute(Attribute('repeatCount', "indefinite"))
|
||||
g.addElement(an)
|
||||
s.addElement(g)
|
||||
for i in range(20,120,20):
|
||||
@@ -1067,3 +1469,6 @@ if __name__=='__main__':
|
||||
|
||||
print d.toXml()
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
|
||||
+28
-27
@@ -10,11 +10,9 @@ pygtk.require('2.0')
|
||||
import gtk, gobject
|
||||
|
||||
from sugar.shell import activity
|
||||
from sugar.p2p.Group import Group
|
||||
from sugar.p2p.Group import LocalGroup
|
||||
from sugar.p2p.Service import Service
|
||||
from sugar.p2p.StreamReader import StreamReader
|
||||
from sugar.p2p.StreamWriter import StreamWriter
|
||||
from sugar.p2p.Group import *
|
||||
from sugar.p2p.StreamReader import *
|
||||
from sugar.p2p.StreamWriter import *
|
||||
import sugar.env
|
||||
|
||||
import richtext
|
||||
@@ -73,14 +71,14 @@ class Chat(activity.Activity):
|
||||
self._hbox = gtk.HBox(False, 12)
|
||||
self._hbox.set_border_width(12)
|
||||
|
||||
[chat_vbox, buf] = self._create_chat()
|
||||
[chat_vbox, buffer] = self._create_chat()
|
||||
self._hbox.pack_start(chat_vbox)
|
||||
chat_vbox.show()
|
||||
|
||||
vbox.pack_start(self._hbox)
|
||||
self._hbox.show()
|
||||
|
||||
toolbar = self._create_toolbar(buf)
|
||||
toolbar = self._create_toolbar(buffer)
|
||||
vbox.pack_start(toolbar, False)
|
||||
toolbar.show()
|
||||
|
||||
@@ -132,37 +130,37 @@ class Chat(activity.Activity):
|
||||
button.set_menu(menu)
|
||||
|
||||
def activity_on_close_from_user(self):
|
||||
print "act %d: in activity_on_close_from_user" % self.activity_get_id()
|
||||
print "act %d: in activity_on_close_from_user"%self.activity_get_id()
|
||||
self.activity_shutdown()
|
||||
|
||||
def activity_on_lost_focus(self):
|
||||
print "act %d: in activity_on_lost_focus" % self.activity_get_id()
|
||||
print "act %d: in activity_on_lost_focus"%self.activity_get_id()
|
||||
|
||||
def activity_on_got_focus(self):
|
||||
print "act %d: in activity_on_got_focus" % self.activity_get_id()
|
||||
# FIXME self._controller.notify_activate(self)
|
||||
print "act %d: in activity_on_got_focus"%self.activity_get_id()
|
||||
self._controller.notify_activate(self)
|
||||
|
||||
def recv_message(self, buddy, msg):
|
||||
self._insert_rich_message(buddy.get_nick_name(), msg)
|
||||
self._controller.notify_new_message(self, buddy)
|
||||
|
||||
def _insert_rich_message(self, nick, msg):
|
||||
buf = self._chat_view.get_buffer()
|
||||
aniter = buf.get_end_iter()
|
||||
buf.insert(aniter, nick + ": ")
|
||||
buffer = self._chat_view.get_buffer()
|
||||
aniter = buffer.get_end_iter()
|
||||
buffer.insert(aniter, nick + ": ")
|
||||
|
||||
serializer = richtext.RichTextSerializer()
|
||||
serializer.deserialize(msg, buf)
|
||||
serializer.deserialize(msg, buffer)
|
||||
|
||||
aniter = buf.get_end_iter()
|
||||
buf.insert(aniter, "\n")
|
||||
aniter = buffer.get_end_iter()
|
||||
buffer.insert(aniter, "\n")
|
||||
|
||||
def _local_message(self, success, text):
|
||||
if not success:
|
||||
message = "Error: %s\n" % text
|
||||
buf = self._chat_view.get_buffer()
|
||||
aniter = buf.get_end_iter()
|
||||
buf.insert(aniter, message)
|
||||
buffer = self._chat_view.get_buffer()
|
||||
aniter = buffer.get_end_iter()
|
||||
buffer.insert(aniter, message)
|
||||
else:
|
||||
owner = self._controller.get_group().get_owner()
|
||||
self._insert_rich_message(owner.get_nick_name(), text)
|
||||
@@ -327,11 +325,11 @@ class GroupChat(Chat):
|
||||
if buddy.get_nick_name() == self._group.get_owner().get_nick_name():
|
||||
# Do not show ourself in the buddy list
|
||||
pass
|
||||
elif action == Group.BUDDY_JOIN:
|
||||
elif action == BUDDY_JOIN:
|
||||
aniter = self._buddy_list_model.append(None)
|
||||
self._buddy_list_model.set(aniter, self._MODEL_COL_NICK, buddy.get_nick_name(),
|
||||
self._MODEL_COL_ICON, None, self._MODEL_COL_BUDDY, buddy)
|
||||
elif action == Group.BUDDY_LEAVE:
|
||||
elif action == BUDDY_LEAVE:
|
||||
aniter = self._get_iter_for_buddy(buddy)
|
||||
if aniter:
|
||||
self._buddy_list_model.remove(aniter)
|
||||
@@ -348,7 +346,7 @@ class GroupChat(Chat):
|
||||
aniter = self._get_iter_for_buddy(buddy)
|
||||
self._buddy_list_model.set(aniter, self._MODEL_COL_ICON, self._pixbuf_new_message)
|
||||
|
||||
def notify_activate(self, chat, buddy):
|
||||
def notify_activate(self, chat):
|
||||
aniter = self._get_iter_for_buddy(buddy)
|
||||
self._buddy_list_model.set(aniter, self._MODEL_COL_ICON, self._pixbuf_active_chat)
|
||||
|
||||
@@ -389,16 +387,19 @@ class ChatShell(dbus.service.Object):
|
||||
dbus.service.Object.__init__(self, bus_name, object_path)
|
||||
|
||||
def open_group_chat(self):
|
||||
self._group_chat = GroupChat()
|
||||
self._group_chat.activity_connect_to_shell()
|
||||
group_chat = GroupChat()
|
||||
group_chat.activity_connect_to_shell()
|
||||
|
||||
@dbus.service.method('com.redhat.Sugar.ChatShell')
|
||||
def send_message(self, message):
|
||||
self._group_chat.send_message(message)
|
||||
pass
|
||||
|
||||
def main():
|
||||
ChatShell.get_instance().open_group_chat()
|
||||
gtk.main()
|
||||
try:
|
||||
gtk.main()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+22
-23
@@ -45,7 +45,7 @@ class RichTextView(gtk.TextView):
|
||||
|
||||
def __motion_notify_cb(self, widget, event):
|
||||
if event.is_hint:
|
||||
event.window.get_pointer();
|
||||
[x, y, state] = event.window.get_pointer();
|
||||
|
||||
it = self.__get_event_iter(event)
|
||||
if it:
|
||||
@@ -134,9 +134,9 @@ class RichTextBuffer(gtk.TextBuffer):
|
||||
|
||||
def __insert_text_cb(self, widget, pos, text, length):
|
||||
for tag in self.active_tags:
|
||||
pos_end = pos.copy()
|
||||
pos_end.backward_chars(length)
|
||||
self.apply_tag_by_name(tag, pos, pos_end)
|
||||
pos_end = pos.copy()
|
||||
pos_end.backward_chars(length)
|
||||
self.apply_tag_by_name(tag, pos, pos_end)
|
||||
|
||||
class RichTextToolbar(gtk.Toolbar):
|
||||
def __init__(self, buf):
|
||||
@@ -205,7 +205,6 @@ class RichTextToolbar(gtk.Toolbar):
|
||||
|
||||
class RichTextHandler(xml.sax.handler.ContentHandler):
|
||||
def __init__(self, serializer, buf):
|
||||
xml.sax.handler.ContentHandler.__init__(self)
|
||||
self.buf = buf
|
||||
self.serializer = serializer
|
||||
self.tags = []
|
||||
@@ -287,7 +286,7 @@ class RichTextSerializer:
|
||||
def serialize(self, buf):
|
||||
self.buf = buf
|
||||
|
||||
res = "<richtext>"
|
||||
xml = "<richtext>"
|
||||
|
||||
next_it = buf.get_start_iter()
|
||||
while not next_it.is_end():
|
||||
@@ -300,29 +299,29 @@ class RichTextSerializer:
|
||||
for tag in it.get_toggled_tags(False):
|
||||
while 1:
|
||||
open_tag = self._open_tags.pop()
|
||||
res += self.serialize_tag_end(tag)
|
||||
xml += self.serialize_tag_end(tag)
|
||||
if open_tag == tag:
|
||||
break
|
||||
tags_to_reopen.append(open_tag)
|
||||
|
||||
for tag in tags_to_reopen:
|
||||
self._open_tags.append(tag)
|
||||
res += self.serialize_tag_start(tag, it)
|
||||
xml += self.serialize_tag_start(tag, it)
|
||||
|
||||
for tag in it.get_toggled_tags(True):
|
||||
self._open_tags.append(tag)
|
||||
res += self.serialize_tag_start(tag, it)
|
||||
xml += self.serialize_tag_start(tag, it)
|
||||
|
||||
res += buf.get_text(it, next_it, False)
|
||||
xml += buf.get_text(it, next_it, False)
|
||||
|
||||
if next_it.is_end():
|
||||
self._open_tags.reverse()
|
||||
for tag in self._open_tags:
|
||||
res += self.serialize_tag_end(tag)
|
||||
xml += self.serialize_tag_end(tag)
|
||||
|
||||
res += "</richtext>"
|
||||
xml += "</richtext>"
|
||||
|
||||
return res
|
||||
return xml
|
||||
|
||||
def deserialize(self, xml_string, buf):
|
||||
parser = xml.sax.make_parser()
|
||||
@@ -331,11 +330,11 @@ class RichTextSerializer:
|
||||
parser.feed(xml_string)
|
||||
parser.close()
|
||||
|
||||
def test_quit(w, rb):
|
||||
print RichTextSerializer().serialize(rb)
|
||||
def test_quit(window, rich_buf):
|
||||
print RichTextSerializer().serialize(rich_buf)
|
||||
gtk.main_quit()
|
||||
|
||||
def link_clicked(v, address):
|
||||
def link_clicked(view, address):
|
||||
print "Link clicked " + address
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -351,15 +350,15 @@ if __name__ == "__main__":
|
||||
|
||||
rich_buf = view.get_buffer()
|
||||
|
||||
test_xml = "<richtext>"
|
||||
xml_string = "<richtext>"
|
||||
|
||||
test_xml += "<bold><italic>Test</italic>one</bold>\n"
|
||||
test_xml += "<bold><italic>Test two</italic></bold>"
|
||||
test_xml += "<font size=\"xx-small\">Test three</font>"
|
||||
test_xml += "<link href=\"http://www.gnome.org\">Test link</link>"
|
||||
test_xml += "</richtext>"
|
||||
xml_string += "<bold><italic>Test</italic>one</bold>\n"
|
||||
xml_string += "<bold><italic>Test two</italic></bold>"
|
||||
xml_string += "<font size=\"xx-small\">Test three</font>"
|
||||
xml_string += "<link href=\"http://www.gnome.org\">Test link</link>"
|
||||
xml_string += "</richtext>"
|
||||
|
||||
RichTextSerializer().deserialize(test_xml, rich_buf)
|
||||
RichTextSerializer().deserialize(xml_string, rich_buf)
|
||||
|
||||
toolbar = RichTextToolbar(rich_buf)
|
||||
vbox.pack_start(toolbar, False)
|
||||
|
||||
Reference in New Issue
Block a user