src/reportlab/graphics/widgetbase.py
author rptlab
Thu, 21 Oct 2010 10:34:13 +0000
branchrtl-support
changeset 3467 4f25b3a34c5f
parent 3032 22224b1b4d24
permissions -rw-r--r--
rtl-support: merged trunk changes r3591:3803
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2200
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
817
8c3a399effda License text changes
rgbecker
parents: 724
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2200
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/widgetbase.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2200
diff changeset
     4
__version__=''' $Id$ '''
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     5
__doc__='''Base class for user-defined graphical widgets'''
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     6
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     7
import string
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     8
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     9
from reportlab.graphics import shapes
724
0b10945bd324 Renamed config.py to rl_config.py
rgbecker
parents: 621
diff changeset
    10
from reportlab import rl_config
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    11
from reportlab.lib import colors
927
ad7eb78babb4 Switched to using lib.validators module.
dinu_gherman
parents: 925
diff changeset
    12
from reportlab.lib.validators import *
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 940
diff changeset
    13
from reportlab.lib.attrmap import *
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    14
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    15
class PropHolder:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    16
    '''Base for property holders'''
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    17
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    18
    _attrMap = None
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    19
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    20
    def verify(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    21
        """If the _attrMap attribute is not None, this
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    22
        checks all expected attributes are present; no
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    23
        unwanted attributes are present; and (if a
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    24
        checking function is found) checks each
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    25
        attribute has a valid value.  Either succeeds
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    26
        or raises an informative exception.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    27
        """
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 940
diff changeset
    28
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    29
        if self._attrMap is not None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    30
            for key in self.__dict__.keys():
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
    31
                if key[0] != '_':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    32
                    msg = "Unexpected attribute %s found in %s" % (key, self)
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
    33
                    assert key in self._attrMap, msg
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    34
            for (attr, metavalue) in self._attrMap.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    35
                msg = "Missing attribute %s from %s" % (attr, self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    36
                assert hasattr(self, attr), msg
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    37
                value = getattr(self, attr)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    38
                args = (value, attr, self.__class__.__name__)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    39
                assert metavalue.validate(value), "Invalid value %s for attribute %s in class %s" % args
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    40
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    41
    if rl_config.shapeChecking:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    42
        """This adds the ability to check every attribute assignment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    43
        as it is made. It slows down shapes but is a big help when
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    44
        developing. It does not get defined if rl_config.shapeChecking = 0.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    45
        """
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    46
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    47
        def __setattr__(self, name, value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    48
            """By default we verify.  This could be off
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    49
            in some parallel base classes."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    50
            validateSetattr(self,name,value)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    51
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    52
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    53
    def getProperties(self,recur=1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    54
        """Returns a list of all properties which can be edited and
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    55
        which are not marked as private. This may include 'child
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    56
        widgets' or 'primitive shapes'.  You are free to override
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    57
        this and provide alternative implementations; the default
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    58
        one simply returns everything without a leading underscore.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    59
        """
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    60
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    61
        from reportlab.lib.validators import isValidChild
931
07db840b3db6 Changed validator functions into classes.
dinu_gherman
parents: 927
diff changeset
    62
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    63
        # TODO when we need it, but not before -
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    64
        # expose sequence contents?
931
07db840b3db6 Changed validator functions into classes.
dinu_gherman
parents: 927
diff changeset
    65
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    66
        props = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    67
        for name in self.__dict__.keys():
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
    68
            if name[0:1] != '_':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    69
                component = getattr(self, name)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    70
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    71
                if recur and isValidChild(component):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    72
                    # child object, get its properties too
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    73
                    childProps = component.getProperties(recur=recur)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    74
                    for (childKey, childValue) in childProps.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    75
                        #key might be something indexed like '[2].fillColor'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    76
                        #or simple like 'fillColor'; in the former case we
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    77
                        #don't need a '.' between me and my child.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    78
                        if childKey[0] == '[':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    79
                            props['%s%s' % (name, childKey)] = childValue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    80
                        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    81
                            props['%s.%s' % (name, childKey)] = childValue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    82
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    83
                    props[name] = component
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    84
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    85
        return props
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    86
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    87
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    88
    def setProperties(self, propDict):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    89
        """Permits bulk setting of properties.  These may include
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    90
        child objects e.g. "chart.legend.width = 200".
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    91
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    92
        All assignments will be validated by the object as if they
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    93
        were set individually in python code.
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    94
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    95
        All properties of a top-level object are guaranteed to be
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    96
        set before any of the children, which may be helpful to
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    97
        widget designers.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    98
        """
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    99
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   100
        childPropDicts = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   101
        for (name, value) in propDict.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   102
            parts = string.split(name, '.', 1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   103
            if len(parts) == 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   104
                #simple attribute, set it now
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   105
                setattr(self, name, value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   106
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   107
                (childName, remains) = parts
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   108
                try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   109
                    childPropDicts[childName][remains] = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   110
                except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   111
                    childPropDicts[childName] = {remains: value}
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   112
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   113
        # now assign to children
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   114
        for (childName, childPropDict) in childPropDicts.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   115
            child = getattr(self, childName)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   116
            child.setProperties(childPropDict)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   117
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   118
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   119
    def dumpProperties(self, prefix=""):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   120
        """Convenience. Lists them on standard output.  You
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   121
        may provide a prefix - mostly helps to generate code
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   122
        samples for documentation.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   123
        """
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   124
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   125
        propList = self.getProperties().items()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   126
        propList.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   127
        if prefix:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   128
            prefix = prefix + '.'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   129
        for (name, value) in propList:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   130
            print '%s%s = %s' % (prefix, name, value)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   131
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   132
927
ad7eb78babb4 Switched to using lib.validators module.
dinu_gherman
parents: 925
diff changeset
   133
class Widget(PropHolder, shapes.UserNode):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   134
    """Base for all user-defined widgets.  Keep as simple as possible. Does
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   135
    not inherit from Shape so that we can rewrite shapes without breaking
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   136
    widgets and vice versa."""
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   137
2363
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2332
diff changeset
   138
    def _setKeywords(self,**kw):
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2332
diff changeset
   139
        for k,v in kw.items():
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   140
            if k not in self.__dict__:
2363
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2332
diff changeset
   141
                setattr(self,k,v)
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2332
diff changeset
   142
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   143
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   144
        msg = "draw() must be implemented for each Widget!"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   145
        raise shapes.NotImplementedError, msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   146
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   147
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   148
        msg = "demo() must be implemented for each Widget!"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   149
        raise shapes.NotImplementedError, msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   150
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   151
    def provideNode(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   152
        return self.draw()
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   153
2004
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   154
    def getBounds(self):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   155
        "Return outer boundary as x1,y1,x2,y2.  Can be overridden for efficiency"
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   156
        return self.draw().getBounds()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   157
2776
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   158
class ScaleWidget(Widget):
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   159
    '''Contents with a scale and offset''' 
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   160
    _attrMap = AttrMap(
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   161
        x = AttrMapValue(isNumber,desc="x offset"),
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   162
        y = AttrMapValue(isNumber,desc="y offset"),
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   163
        scale = AttrMapValue(isNumber,desc="scale"),
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   164
        contents = AttrMapValue(None,desc="Contained drawable elements"),
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   165
        )
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   166
    def __init__(self,x=0,y=0,scale=1.0,contents=None):
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   167
        self.x = x
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   168
        self.y = y
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   169
        if not contents: contents=[]
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   170
        elif not isinstance(contents,(tuple,list)):
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   171
            contents = (contents,)
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   172
        self.contents = list(contents)
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   173
        self.scale = scale
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   174
    
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   175
    def draw(self):
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   176
        return shapes.Group(transform=(self.scale,0,0,self.scale,self.x,self.y),*self.contents)
6e7adab745fe widgetbase.py: add ScaleWidget
rgbecker
parents: 2519
diff changeset
   177
971
9ca160e4f41a Fixed _ItemWrapper name and made it really cache
rgbecker
parents: 970
diff changeset
   178
_ItemWrapper={}
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   179
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   180
class CloneMixin:
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   181
    def clone(self,**kwds):
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   182
        n = self.__class__()
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   183
        n.__dict__.clear()
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   184
        n.__dict__.update(self.__dict__)
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   185
        if kwds: n.__dict__.update(kwds)
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   186
        return n
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   187
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   188
class TypedPropertyCollection(PropHolder):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   189
    """A container with properties for objects of the same kind.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   190
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   191
    This makes it easy to create lists of objects. You initialize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   192
    it with a class of what it is to contain, and that is all you
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   193
    can add to it.  You can assign properties to the collection
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   194
    as a whole, or to a numeric index within it; if so it creates
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   195
    a new child object to hold that data.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   196
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   197
    So:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   198
        wedges = TypedPropertyCollection(WedgeProperties)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   199
        wedges.strokeWidth = 2                # applies to all
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   200
        wedges.strokeColor = colors.red       # applies to all
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   201
        wedges[3].strokeColor = colors.blue   # only to one
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   202
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   203
    The last line should be taken as a prescription of how to
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   204
    create wedge no. 3 if one is needed; no error is raised if
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   205
    there are only two data points.
2519
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   206
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   207
    We try and make sensible use of tuple indeces.
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   208
        line[(3,x)] is backed by line[(3,)], line[3] & line
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   209
    """
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   210
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   211
    def __init__(self, exampleClass):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   212
        #give it same validation rules as what it holds
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   213
        self.__dict__['_value'] = exampleClass()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   214
        self.__dict__['_children'] = {}
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   215
2420
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   216
    def wKlassFactory(self,Klass):
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   217
        class WKlass(Klass,CloneMixin):
2420
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   218
            def __getattr__(self,name):
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   219
                try:
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   220
                    return self.__class__.__bases__[0].__getattr__(self,name)
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   221
                except:
2519
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   222
                    i = self._index
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   223
                    if i:
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   224
                        c = self._parent._children
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   225
                        if i in c and name in c[i].__dict__:
2519
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   226
                            return getattr(c[i],name)
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   227
                        elif len(i)==1:
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   228
                            i = i[0]
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   229
                            if i in c and name in c[i].__dict__:
2519
2b98e73f9718 merge utf8 2666:2667 widgetbase: make line[3] back up for line[(3,x)]
rgbecker
parents: 2420
diff changeset
   230
                                return getattr(c[i],name)
2420
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   231
                    return getattr(self._parent,name)
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   232
        return WKlass
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   233
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   234
    def __getitem__(self, index):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   235
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   236
            return self._children[index]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   237
        except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   238
            Klass = self._value.__class__
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   239
            if Klass in _ItemWrapper:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   240
                WKlass = _ItemWrapper[Klass]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   241
            else:
2420
8c40b77b4862 graphics: changes for SmartLegend
rgbecker
parents: 2363
diff changeset
   242
                _ItemWrapper[Klass] = WKlass = self.wKlassFactory(Klass)
970
87d965592699 Improved element wrapping seems to work
rgbecker
parents: 968
diff changeset
   243
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   244
            child = WKlass()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   245
            child._parent = self
1687
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   246
            if type(index) in (type(()),type([])):
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   247
                index = tuple(index)
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   248
                if len(index)>1:
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   249
                    child._index = tuple(index[:-1])
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   250
                else:
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   251
                    child._index = None
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   252
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   253
                child._index = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   254
            for i in filter(lambda x,K=child.__dict__.keys(): x in K,child._attrMap.keys()):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   255
                del child.__dict__[i]
970
87d965592699 Improved element wrapping seems to work
rgbecker
parents: 968
diff changeset
   256
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   257
            self._children[index] = child
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   258
            return child
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   259
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   260
    def __contains__(self,key):
1687
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   261
        if type(key) in (type(()),type([])): key = tuple(key)
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   262
        return key in self._children
1687
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   263
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   264
    def __setitem__(self, key, value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   265
        msg = "This collection can only hold objects of type %s" % self._value.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   266
        assert isinstance(value, self._value.__class__), msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   267
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   268
    def __len__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   269
        return len(self._children.keys())
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   270
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   271
    def getProperties(self,recur=1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   272
        # return any children which are defined and whatever
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   273
        # differs from the parent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   274
        props = {}
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   275
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   276
        for (key, value) in self._value.getProperties(recur=recur).items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   277
            props['%s' % key] = value
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   278
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   279
        for idx in self._children.keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   280
            childProps = self._children[idx].getProperties(recur=recur)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   281
            for (key, value) in childProps.items():
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   282
                if not hasattr(self,key) or getattr(self, key)!=value:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   283
                    newKey = '[%s].%s' % (idx, key)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   284
                    props[newKey] = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   285
        return props
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   286
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   287
    def setVector(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   288
        for name, value in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   289
            for i in xrange(len(value)):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   290
                setattr(self[i],name,value[i])
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   291
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   292
    def __getattr__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   293
        return getattr(self._value,name)
997
e0888bb81f13 Allow TypedPropertyCollection exemplar class to have getattr etc
rgbecker
parents: 996
diff changeset
   294
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   295
    def __setattr__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   296
        return setattr(self._value,name,value)
997
e0888bb81f13 Allow TypedPropertyCollection exemplar class to have getattr etc
rgbecker
parents: 996
diff changeset
   297
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   298
## No longer needed!
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   299
class StyleProperties(PropHolder):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   300
    """A container class for attributes used in charts and legends.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   301
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   302
    Attributes contained can be those for any graphical element
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   303
    (shape?) in the ReportLab graphics package. The idea for this
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   304
    container class is to be useful in combination with legends
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   305
    and/or the individual appearance of data series in charts.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   306
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   307
    A legend could be as simple as a wrapper around a list of style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   308
    properties, where the 'desc' attribute contains a descriptive
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   309
    string and the rest could be used by the legend e.g. to draw
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   310
    something like a color swatch. The graphical presentation of
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   311
    the legend would be its own business, though.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   312
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   313
    A chart could be inspecting a legend or, more directly, a list
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   314
    of style properties to pick individual attributes that it knows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   315
    about in order to render a particular row of the data. A bar
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   316
    chart e.g. could simply use 'strokeColor' and 'fillColor' for
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   317
    drawing the bars while a line chart could also use additional
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   318
    ones like strokeWidth.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   319
    """
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   320
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   321
    _attrMap = AttrMap(
3467
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   322
        strokeWidth = AttrMapValue(isNumber,desc='width of the stroke line'),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   323
        strokeLineCap = AttrMapValue(isNumber,desc='Line cap 0=butt, 1=round & 2=square',advancedUsage=1),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   324
        strokeLineJoin = AttrMapValue(isNumber,desc='Line join 0=miter, 1=round & 2=bevel',advancedUsage=1),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   325
        strokeMiterLimit = AttrMapValue(None,desc='miter limit control miter line joins',advancedUsage=1),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   326
        strokeDashArray = AttrMapValue(isListOfNumbersOrNone,desc='dashing patterns e.g. (1,3)'),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   327
        strokeOpacity = AttrMapValue(isNumber,desc='level of transparency (alpha) accepts values between 0..1',advancedUsage=1),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   328
        strokeColor = AttrMapValue(isColorOrNone,desc='the color of the stroke'),
4f25b3a34c5f rtl-support: merged trunk changes r3591:3803
rptlab
parents: 3032
diff changeset
   329
        fillColor = AttrMapValue(isColorOrNone,desc='the filling color'),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   330
        desc = AttrMapValue(isString),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   331
        )
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   332
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   333
    def __init__(self, **kwargs):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   334
        "Initialize with attributes if any."
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   335
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   336
        for k, v in kwargs.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   337
            setattr(self, k, v)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   338
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   339
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   340
    def __setattr__(self, name, value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   341
        "Verify attribute name and value, before setting it."
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   342
        validateSetattr(self,name,value)
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   343
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   344
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   345
class TwoCircles(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   346
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   347
        self.leftCircle = shapes.Circle(100,100,20, fillColor=colors.red)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   348
        self.rightCircle = shapes.Circle(300,100,20, fillColor=colors.red)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   349
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   350
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   351
        return shapes.Group(self.leftCircle, self.rightCircle)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   352
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   353
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   354
class Face(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   355
    """This draws a face with two eyes.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   356
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   357
    It exposes a couple of properties
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   358
    to configure itself and hides all other details.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   359
    """
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   360
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   361
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   362
        x = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   363
        y = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   364
        size = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   365
        skinColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   366
        eyeColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   367
        mood = AttrMapValue(OneOf('happy','sad','ok')),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   368
        )
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   369
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   370
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   371
        self.x = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   372
        self.y = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   373
        self.size = 80
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   374
        self.skinColor = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   375
        self.eyeColor = colors.blue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   376
        self.mood = 'happy'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   377
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   378
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   379
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   380
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   381
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   382
        s = self.size  # abbreviate as we will use this a lot
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   383
        g = shapes.Group()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   384
        g.transform = [1,0,0,1,self.x, self.y]
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   385
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   386
        # background
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   387
        g.add(shapes.Circle(s * 0.5, s * 0.5, s * 0.5, fillColor=self.skinColor))
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   388
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   389
        # left eye
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   390
        g.add(shapes.Circle(s * 0.35, s * 0.65, s * 0.1, fillColor=colors.white))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   391
        g.add(shapes.Circle(s * 0.35, s * 0.65, s * 0.05, fillColor=self.eyeColor))
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   392
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   393
        # right eye
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   394
        g.add(shapes.Circle(s * 0.65, s * 0.65, s * 0.1, fillColor=colors.white))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   395
        g.add(shapes.Circle(s * 0.65, s * 0.65, s * 0.05, fillColor=self.eyeColor))
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   396
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   397
        # nose
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   398
        g.add(shapes.Polygon(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   399
            points=[s * 0.5, s * 0.6, s * 0.4, s * 0.3, s * 0.6, s * 0.3],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   400
            fillColor=None))
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   401
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   402
        # mouth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   403
        if self.mood == 'happy':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   404
            offset = -0.05
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   405
        elif self.mood == 'sad':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   406
            offset = +0.05
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   407
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   408
            offset = 0
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   409
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   410
        g.add(shapes.Polygon(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   411
            points = [
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   412
                s * 0.3, s * 0.2, #left of mouth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   413
                s * 0.7, s * 0.2, #right of mouth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   414
                s * 0.6, s * (0.2 + offset), # the bit going up or down
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   415
                s * 0.4, s * (0.2 + offset) # the bit going up or down
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   416
                ],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   417
            fillColor = colors.pink,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   418
            strokeColor = colors.red,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   419
            strokeWidth = s * 0.03
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   420
            ))
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   421
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   422
        return g
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   423
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   424
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   425
class TwoFaces(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   426
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   427
        self.faceOne = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   428
        self.faceOne.mood = "happy"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   429
        self.faceTwo = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   430
        self.faceTwo.x = 100
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   431
        self.faceTwo.mood = "sad"
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   432
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   433
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   434
        """Just return a group"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   435
        return shapes.Group(self.faceOne, self.faceTwo)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   436
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   437
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   438
        """The default case already looks good enough,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   439
        no implementation needed here"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   440
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   441
2004
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   442
class Sizer(Widget):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   443
    "Container to show size of all enclosed objects"
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2004
diff changeset
   444
2004
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   445
    _attrMap = AttrMap(BASE=shapes.SolidShape,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   446
        contents = AttrMapValue(isListOfShapes,desc="Contained drawable elements"),
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   447
        )
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   448
    def __init__(self, *elements):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   449
        self.contents = []
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   450
        self.fillColor = colors.cyan
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   451
        self.strokeColor = colors.magenta
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2004
diff changeset
   452
2004
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   453
        for elem in elements:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   454
            self.add(elem)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   455
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   456
    def _addNamedNode(self,name,node):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   457
        'if name is not None add an attribute pointing to node and add to the attrMap'
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   458
        if name:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   459
            if name not in self._attrMap.keys():
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   460
                self._attrMap[name] = AttrMapValue(isValidChild)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   461
            setattr(self, name, node)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   462
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   463
    def add(self, node, name=None):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   464
        """Appends non-None child node to the 'contents' attribute. In addition,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   465
        if a name is provided, it is subsequently accessible by name
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   466
        """
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   467
        # propagates properties down
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   468
        if node is not None:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   469
            assert isValidChild(node), "Can only add Shape or UserNode objects to a Group"
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   470
            self.contents.append(node)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   471
            self._addNamedNode(name,node)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   472
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   473
    def getBounds(self):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   474
        # get bounds of each object
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   475
        if self.contents:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   476
            b = []
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   477
            for elem in self.contents:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   478
                b.append(elem.getBounds())
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   479
            return shapes.getRectsBounds(b)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   480
        else:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   481
            return (0,0,0,0)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2004
diff changeset
   482
2004
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   483
    def draw(self):
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   484
        g = shapes.Group()
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   485
        (x1, y1, x2, y2) = self.getBounds()
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   486
        r = shapes.Rect(
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   487
            x = x1,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   488
            y = y1,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   489
            width = x2-x1,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   490
            height = y2-y1,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   491
            fillColor = self.fillColor,
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   492
            strokeColor = self.strokeColor
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   493
            )
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   494
        g.add(r)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   495
        for elem in self.contents:
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   496
            g.add(elem)
14a180d2691f Added getBounds() logic throughout graphics hierarchy
andy_robinson
parents: 1687
diff changeset
   497
        return g
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   498
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   499
def test():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   500
    from reportlab.graphics.charts.piecharts import WedgeProperties
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   501
    wedges = TypedPropertyCollection(WedgeProperties)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   502
    wedges.fillColor = colors.red
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   503
    wedges.setVector(fillColor=(colors.blue,colors.green,colors.white))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   504
    print len(_ItemWrapper)
951
d810699237a0 Added TypedPropertyCollection.setVector
rgbecker
parents: 948
diff changeset
   505
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   506
    d = shapes.Drawing(400, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   507
    tc = TwoCircles()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   508
    d.add(tc)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   509
    import renderPDF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   510
    renderPDF.drawToFile(d, 'sample_widget.pdf', 'A Sample Widget')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   511
    print 'saved sample_widget.pdf'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   512
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   513
    d = shapes.Drawing(400, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   514
    f = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   515
    f.skinColor = colors.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   516
    f.mood = "sad"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   517
    d.add(f, name='theFace')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   518
    print 'drawing 1 properties:'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   519
    d.dumpProperties()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   520
    renderPDF.drawToFile(d, 'face.pdf', 'A Sample Widget')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   521
    print 'saved face.pdf'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   522
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   523
    d2 = d.expandUserNodes()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   524
    renderPDF.drawToFile(d2, 'face_copy.pdf', 'An expanded drawing')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   525
    print 'saved face_copy.pdf'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   526
    print 'drawing 2 properties:'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   527
    d2.dumpProperties()
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   528
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   529
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   530
if __name__=='__main__':
1687
3a572e14ec3e Allow for multicolour single data barcharts
rgbecker
parents: 1683
diff changeset
   531
    test()