reportlab/graphics/widgetbase.py
author andy_robinson
Wed, 24 Jul 2002 19:56:39 +0000
changeset 1683 7fa753e4420a
parent 1677 1450177dd19e
child 1687 3a572e14ec3e
permissions -rw-r--r--
Removed all trailing whitespace
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
817
8c3a399effda License text changes
rgbecker
parents: 724
diff changeset
     1
#copyright ReportLab Inc. 2000-2001
8c3a399effda License text changes
rgbecker
parents: 724
diff changeset
     2
#see license.txt for license details
8c3a399effda License text changes
rgbecker
parents: 724
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/graphics/widgetbase.py?cvsroot=reportlab
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/graphics/widgetbase.py,v 1.32 2002/07/24 19:56:36 andy_robinson Exp $
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
     5
__version__=''' $Id: widgetbase.py,v 1.32 2002/07/24 19:56:36 andy_robinson Exp $ '''
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     6
import string
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     7
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     8
from reportlab.graphics import shapes
724
0b10945bd324 Renamed config.py to rl_config.py
rgbecker
parents: 621
diff changeset
     9
from reportlab import rl_config
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    10
from reportlab.lib import colors
927
ad7eb78babb4 Switched to using lib.validators module.
dinu_gherman
parents: 925
diff changeset
    11
from reportlab.lib.validators import *
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 940
diff changeset
    12
from reportlab.lib.attrmap import *
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    13
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
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():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    31
                if key[0] <> '_':
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)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    33
                    assert self._attrMap.has_key(key), msg
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():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    68
            if name[0:1] <> '_':
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
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   138
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   139
        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
   140
        raise shapes.NotImplementedError, msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   141
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   142
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   143
        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
   144
        raise shapes.NotImplementedError, msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   145
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   146
    def provideNode(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   147
        return self.draw()
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   148
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   149
971
9ca160e4f41a Fixed _ItemWrapper name and made it really cache
rgbecker
parents: 970
diff changeset
   150
_ItemWrapper={}
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   151
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   152
class TypedPropertyCollection(PropHolder):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   153
    """A container with properties for objects of the same kind.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   154
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   155
    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
   156
    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
   157
    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
   158
    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
   159
    a new child object to hold that data.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   160
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   161
    So:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   162
        wedges = TypedPropertyCollection(WedgeProperties)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   163
        wedges.strokeWidth = 2                # applies to all
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   164
        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
   165
        wedges[3].strokeColor = colors.blue   # only to one
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   166
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   167
    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
   168
    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
   169
    there are only two data points.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   170
    """
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   171
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   172
    def __init__(self, exampleClass):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   173
        #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
   174
        self.__dict__['_value'] = exampleClass()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   175
        self.__dict__['_children'] = {}
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   176
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   177
    def __getitem__(self, index):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   178
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   179
            return self._children[index]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   180
        except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   181
            Klass = self._value.__class__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   182
            if _ItemWrapper.has_key(Klass):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   183
                WKlass = _ItemWrapper[Klass]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   184
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   185
                class WKlass(Klass):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   186
                    def __getattr__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   187
                        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   188
                            return self.__class__.__bases__[0].__getattr__(self,name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   189
                        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   190
                            if self._index and self._parent._children.has_key(self._index):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   191
                                if self._parent._children[self._index].__dict__.has_key(name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   192
                                    return getattr(self._parent._children[self._index],name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   193
                            return getattr(self._parent,name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   194
                _ItemWrapper[Klass] = WKlass
970
87d965592699 Improved element wrapping seems to work
rgbecker
parents: 968
diff changeset
   195
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   196
            child = WKlass()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   197
            child._parent = self
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   198
            if type(index) in (type(()),type([])) and len(index)>1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   199
                child._index = index[:-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   200
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   201
                child._index = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   202
            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
   203
                del child.__dict__[i]
970
87d965592699 Improved element wrapping seems to work
rgbecker
parents: 968
diff changeset
   204
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   205
            self._children[index] = child
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   206
            return child
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   207
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   208
    def __setitem__(self, key, value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   209
        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
   210
        assert isinstance(value, self._value.__class__), msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   211
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   212
    def __len__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   213
        return len(self._children.keys())
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   214
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   215
    def getProperties(self,recur=1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   216
        # 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
   217
        # differs from the parent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   218
        props = {}
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   219
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   220
        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
   221
            props['%s' % key] = value
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   222
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   223
        for idx in self._children.keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   224
            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
   225
            for (key, value) in childProps.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   226
                if not hasattr(self,key) or getattr(self, key)<>value:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   227
                    newKey = '[%s].%s' % (idx, key)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   228
                    props[newKey] = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   229
        return props
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   230
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   231
    def setVector(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   232
        for name, value in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   233
            for i in xrange(len(value)):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   234
                setattr(self[i],name,value[i])
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   235
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   236
    def __getattr__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   237
        return getattr(self._value,name)
997
e0888bb81f13 Allow TypedPropertyCollection exemplar class to have getattr etc
rgbecker
parents: 996
diff changeset
   238
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   239
    def __setattr__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   240
        return setattr(self._value,name,value)
997
e0888bb81f13 Allow TypedPropertyCollection exemplar class to have getattr etc
rgbecker
parents: 996
diff changeset
   241
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   242
## No longer needed!
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   243
class StyleProperties(PropHolder):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   244
    """A container class for attributes used in charts and legends.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   245
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   246
    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
   247
    (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
   248
    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
   249
    and/or the individual appearance of data series in charts.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   250
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   251
    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
   252
    properties, where the 'desc' attribute contains a descriptive
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   253
    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
   254
    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
   255
    the legend would be its own business, though.
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   256
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   257
    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
   258
    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
   259
    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
   260
    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
   261
    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
   262
    ones like strokeWidth.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   263
    """
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   264
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   265
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   266
        strokeWidth = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   267
        strokeLineCap = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   268
        strokeLineJoin = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   269
        strokeMiterLimit = AttrMapValue(None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   270
        strokeDashArray = AttrMapValue(isListOfNumbersOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   271
        strokeOpacity = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   272
        strokeColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   273
        fillColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   274
        desc = AttrMapValue(isString),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   275
        )
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   276
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   277
    def __init__(self, **kwargs):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   278
        "Initialize with attributes if any."
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   279
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   280
        for k, v in kwargs.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   281
            setattr(self, k, v)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   282
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   283
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   284
    def __setattr__(self, name, value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   285
        "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
   286
        validateSetattr(self,name,value)
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   287
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   288
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   289
class TwoCircles(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   290
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   291
        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
   292
        self.rightCircle = shapes.Circle(300,100,20, fillColor=colors.red)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   293
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   294
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   295
        return shapes.Group(self.leftCircle, self.rightCircle)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   296
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   297
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   298
class Face(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   299
    """This draws a face with two eyes.
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   300
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   301
    It exposes a couple of properties
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   302
    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
   303
    """
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   304
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   305
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   306
        x = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   307
        y = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   308
        size = AttrMapValue(isNumber),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   309
        skinColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   310
        eyeColor = AttrMapValue(isColorOrNone),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   311
        mood = AttrMapValue(OneOf('happy','sad','ok')),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   312
        )
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   313
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   314
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   315
        self.x = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   316
        self.y = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   317
        self.size = 80
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   318
        self.skinColor = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   319
        self.eyeColor = colors.blue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   320
        self.mood = 'happy'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   321
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   322
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   323
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   324
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   325
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   326
        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
   327
        g = shapes.Group()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   328
        g.transform = [1,0,0,1,self.x, self.y]
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   329
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   330
        # background
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   331
        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
   332
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   333
        # left eye
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   334
        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
   335
        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
   336
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   337
        # right eye
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   338
        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
   339
        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
   340
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   341
        # nose
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   342
        g.add(shapes.Polygon(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   343
            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
   344
            fillColor=None))
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   345
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   346
        # mouth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   347
        if self.mood == 'happy':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   348
            offset = -0.05
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   349
        elif self.mood == 'sad':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   350
            offset = +0.05
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   351
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   352
            offset = 0
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   353
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   354
        g.add(shapes.Polygon(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   355
            points = [
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   356
                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
   357
                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
   358
                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
   359
                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
   360
                ],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   361
            fillColor = colors.pink,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   362
            strokeColor = colors.red,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   363
            strokeWidth = s * 0.03
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   364
            ))
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   365
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   366
        return g
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   367
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   368
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   369
class TwoFaces(Widget):
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.faceOne = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   372
        self.faceOne.mood = "happy"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   373
        self.faceTwo = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   374
        self.faceTwo.x = 100
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   375
        self.faceTwo.mood = "sad"
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   376
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   377
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   378
        """Just return a group"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   379
        return shapes.Group(self.faceOne, self.faceTwo)
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   380
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   381
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   382
        """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
   383
        no implementation needed here"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   384
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   385
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   386
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   387
def test():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   388
    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
   389
    wedges = TypedPropertyCollection(WedgeProperties)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   390
    wedges.fillColor = colors.red
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   391
    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
   392
    print len(_ItemWrapper)
951
d810699237a0 Added TypedPropertyCollection.setVector
rgbecker
parents: 948
diff changeset
   393
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   394
    d = shapes.Drawing(400, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   395
    tc = TwoCircles()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   396
    d.add(tc)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   397
    import renderPDF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   398
    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
   399
    print 'saved sample_widget.pdf'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   400
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   401
    d = shapes.Drawing(400, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   402
    f = Face()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   403
    f.skinColor = colors.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   404
    f.mood = "sad"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   405
    d.add(f, name='theFace')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   406
    print 'drawing 1 properties:'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   407
    d.dumpProperties()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   408
    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
   409
    print 'saved face.pdf'
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   410
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   411
    d2 = d.expandUserNodes()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   412
    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
   413
    print 'saved face_copy.pdf'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   414
    print 'drawing 2 properties:'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   415
    d2.dumpProperties()
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   416
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   417
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   418
if __name__=='__main__':
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   419
    test()