[reportlab-users] Re: [reportlab-team] widgets/markers.py

Dinu Gherman reportlab-users@reportlab.com
Mon, 5 Aug 2002 13:04:12 +0200


--Apple-Mail-2-846128980
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

I wrote:
>
> Not quite true, this can be automatic! I'm attaching a Dinu-style
> sample version with only a Smiley marker to illustrate my point.

Oops, no file attached...

Dinu

--Apple-Mail-2-846128980
Content-Disposition: attachment;
	filename=markersDCG.py
Content-Transfer-Encoding: 7bit
Content-Type: application/text;
	x-unix-mode=0644;
	x-mac-type=54455854;
	name="markersDCG.py"

"""
This modules defines a collection of markers used in charts.
"""

from types import FunctionType, ClassType
from reportlab.graphics.shapes import Rect, Line, Circle, Polygon, Drawing, Group
from reportlab.graphics.widgets.signsandsymbols import SmileyFace
from reportlab.graphics.widgetbase import Widget
from reportlab.lib.validators import isNumber, isColorOrNone, OneOf, Validator
from reportlab.lib.attrmap import AttrMap, AttrMapValue
from reportlab.lib import colors
from reportlab.graphics.widgets.flags import Flag
from math import sin, cos, pi
import copy, new, sys

_toradians = pi/180.0




class Marker(Widget):
    
    _attrMap = AttrMap(BASE=Widget,
        size = AttrMapValue(isNumber,desc='marker size'),
        x = AttrMapValue(isNumber,desc='marker x coordinate'),
        y = AttrMapValue(isNumber,desc='marker y coordinate'),
        dx = AttrMapValue(isNumber,desc='marker x coordinate adjustment'),
        dy = AttrMapValue(isNumber,desc='marker y coordinate adjustment'),
        angle = AttrMapValue(isNumber,desc='marker rotation'),
        fillColor = AttrMapValue(isColorOrNone, desc='marker fill colour'),
        strokeColor = AttrMapValue(isColorOrNone, desc='marker stroke colour'),
        strokeWidth = AttrMapValue(isNumber, desc='marker stroke width'),
        )
    
    # '_used' contains sort-of "shadowed" clones (adding 'self' 
    # is tricky because of reference couting...)
    _used = []

    def __init__(self, *args, **kw):
        self.strokeColor = colors.black
        self.strokeWidth = 0.1
        self.fillColor = None
        self.size = 5
        self.x = self.y = self.dx = self.dy = self.angle = 0
        self.setProperties(kw)
        self._memo()

    def _memo(self):
        klass = self.__class__
        props = self.getProperties()
        klass._used.append((klass, props, id(self)))

    def __del__(self):
        klass = self.__class__
        props = self.getProperties()
        klass._used.remove((klass, props, id(self)))

    def clone(self):
        return new.instance(self.__class__, self.__dict__.copy())

    def draw(self):
        return Group()




class Smiley(Marker):
    
    _attrMap = AttrMap(BASE=Marker)

    def draw(self):
        x, y = self.x+self.dx, self.y+self.dy
        d = self.size/2.0
        s = SmileyFace()
        s.fillColor = self.fillColor
        s.strokeWidth = self.strokeWidth
        s.strokeColor = self.strokeColor
        s.x = x-d
        s.y = y-d
        s.size = d*2
        
        return s


def show():
    "Print the current number of alive instances."
    print "Marker instances:", len(Marker._used)


if __name__=='__main__':
    show()
    t = Marker()
    show()
    del t
    show()
    tt = Marker()
    show()
    d = Drawing(50, 50)
    d.add(Smiley(x=25, y=25, fillColor=colors.chartreuse, size=20))
    show()
    d.save(fnRoot='markersDCG', formats=['pdf'], outDir='.')

--Apple-Mail-2-846128980
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed



--Apple-Mail-2-846128980--