reportlab/graphics/widgetbase.py
author dinu_gherman
Wed, 09 May 2001 15:20:58 +0000
changeset 925 0436c4cb1d71
parent 914 d4a08fcf249a
child 927 ad7eb78babb4
permissions -rw-r--r--
Code and docstring enhancements.
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
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/graphics/widgetbase.py,v 1.11 2001/05/09 15:20:58 dinu_gherman Exp $
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     5
import string
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     6
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     7
from reportlab.graphics import shapes
724
0b10945bd324 Renamed config.py to rl_config.py
rgbecker
parents: 621
diff changeset
     8
from reportlab import rl_config
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
     9
from reportlab.lib import colors
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    10
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    11
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    12
class PropHolder:
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    13
    '''Base for property holders'''
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    14
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    15
    _attrMap = None
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    16
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    17
    def verify(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    18
        """If the _attrMap attribute is not None, this
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    19
        checks all expected attributes are present; no
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    20
        unwanted attributes are present; and (if a
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    21
        checking function is found) checks each
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    22
        attribute has a valid value.  Either succeeds
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    23
        or raises an informative exception.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    24
        """
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    25
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    26
        if self._attrMap is not None:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    27
            for key in self.__dict__.keys():
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    28
                if key[0] <> '_':
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    29
                    msg = "Unexpected attribute %s found in %s" % (key, self)
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    30
                    assert self._attrMap.has_key(key), msg
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    31
            for (attr, checkerFunc) in self._attrMap.items():
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    32
                msg = "Missing attribute %s from %s" % (key, self)
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    33
                assert hasattr(self, attr), msg
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    34
                if checkerFunc:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    35
                    value = getattr(self, attr)
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    36
                    args = (value, attr, self.__class__.__name__)
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    37
                    msg = "Invalid value %s for attribute %s in class %s" % args
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    38
                    assert checkerFunc(value), msg
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    39
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    40
724
0b10945bd324 Renamed config.py to rl_config.py
rgbecker
parents: 621
diff changeset
    41
    if rl_config.shapeChecking:
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    42
        """This adds the ability to check every attribute assignment
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    43
        as it is made. It slows down shapes but is a big help when
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    44
        developing. It does not get defined if rl_config.shapeChecking = 0.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    45
        """
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    46
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    47
        def __setattr__(self, attr, value):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    48
            """By default we verify.  This could be off
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    49
            in some parallel base classes."""
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    50
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    51
            if self._attrMap is not None:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    52
                if attr[0:1] <> '_':
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    53
                    try:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    54
                        checker = self._attrMap[attr]
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    55
                        if checker and not checker(value):
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    56
                            args = (value, attr, self.__class__.__name__)
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    57
                            msg = "Illegal assignment of '%s' to '%s' in class %s" % args
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    58
                            raise AttributeError, msg
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    59
                    except KeyError:
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    60
                        args = (attr, self.__class__.__name__)
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    61
                        msg = "Illegal attribute '%s' in class %s" % args
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    62
                        raise AttributeError, msg
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    63
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    64
            #if we are still here, set it.
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    65
            self.__dict__[attr] = value
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    66
            #print 'set %s.%s = %s' % (self.__class__.__name__, attr, value)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    67
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    68
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    69
    def getProperties(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    70
        """Returns a list of all properties which can be edited and
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    71
        which are not marked as private. This may include 'child
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    72
        widgets' or 'primitive shapes'.  You are free to override
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    73
        this and provide alternative implementations; the default
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    74
        one simply returns everything without a leading underscore.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    75
        """
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
    76
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    77
        # TODO when we need it, but not before -
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    78
        # expose sequence contents?
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    79
        props = {}
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    80
        for name in self.__dict__.keys():
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    81
            if name[0:1] <> '_':
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    82
                component = getattr(self, name)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    83
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    84
                if shapes.isValidChild(component):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    85
                    # child object, get its properties too
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    86
                    childProps = component.getProperties()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    87
                    for (childKey, childValue) in childProps.items():
574
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    88
                        #key might be something indexed like '[2].fillColor'
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    89
                        #or simple like 'fillColor'; in the former case we
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    90
                        #don't need a '.' between me and my child.
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    91
                        if childKey[0] == '[':
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    92
                            props['%s%s' % (name, childKey)] = childValue
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    93
                        else:
27d5643a5851 Collection based piechart amd support for it.
andy_robinson
parents: 568
diff changeset
    94
                            props['%s.%s' % (name, childKey)] = childValue
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    95
                else:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    96
                    props[name] = component
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
    97
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    98
        return props
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
    99
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   100
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   101
    def setProperties(self, propDict):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   102
        """Permits bulk setting of properties.  These may include
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   103
        child objects e.g. "chart.legend.width = 200".
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   104
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   105
        All assignments will be validated by the object as if they
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   106
        were set individually in python code.
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   107
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   108
        All properties of a top-level object are guaranteed to be
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   109
        set before any of the children, which may be helpful to
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   110
        widget designers.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   111
        """
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   112
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   113
        childPropDicts = {}
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   114
        for (name, value) in propDict.items():
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   115
            parts = string.split(name, '.', 1)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   116
            if len(parts) == 1:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   117
                #simple attribute, set it now
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   118
                setattr(self, name, value)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   119
            else:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   120
                (childName, remains) = parts
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   121
                try:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   122
                    childPropDicts[childName][remains] = value
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   123
                except KeyError:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   124
                    childPropDicts[childName] = {remains: value}
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   125
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   126
        # now assign to children
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   127
        for (childName, childPropDict) in childPropDicts.items():
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   128
            child = getattr(self, childName)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   129
            child.setProperties(childPropDict)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   130
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   131
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   132
    def dumpProperties(self, prefix=""):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   133
        """Convenience. Lists them on standard output.  You
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   134
        may provide a prefix - mostly helps to generate code
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   135
        samples for documentation.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   136
        """
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   137
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   138
        propList = self.getProperties().items()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   139
        propList.sort()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   140
        if prefix:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   141
            prefix = prefix + '.'
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   142
        for (name, value) in propList:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   143
            print '%s%s = %s' % (prefix, name, value)
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   144
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   145
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   146
class Widget(PropHolder,shapes.UserNode):
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   147
    """Base for all user-defined widgets.  Keep as simple as possible. Does
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   148
    not inherit from Shape so that we can rewrite shapes without breaking
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   149
    widgets and vice versa."""
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   150
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   151
    def draw(self):
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   152
        msg = "draw() must be implemented for each Widget!"
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   153
        raise shapes.NotImplementedError, msg
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   154
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   155
    def demo(self):
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   156
        msg = "demo() must be implemented for each Widget!"
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   157
        raise shapes.NotImplementedError, msg
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   158
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   159
    def provideNode(self):
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   160
        return self.draw()
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   161
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   162
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   163
class TypedPropertyCollection(PropHolder):
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   164
    """A container with properties for objects of the same kind.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   165
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   166
    This makes it easy to create lists of objects. You initialize
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   167
    it with a class of what it is to contain, and that is all you
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   168
    can add to it.  You can assign properties to the collection
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   169
    as a whole, or to a numeric index within it; if so it creates
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   170
    a new child object to hold that data.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   171
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   172
    So:
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   173
        wedges = TypesPropertyCollection0(WedgeProperties)
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   174
        wedges.strokeWidth = 2                # applies to all
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   175
        wedges.strokeColor = colors.red       # applies to all
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   176
        wedges[3].strokeColor = colors.blue   # only to one
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   177
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   178
    The last line should be taken as a prescription of how to
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   179
    create wedge no. 3 if one is needed; no error is raised if
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   180
    there are only two data points.
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   181
    """
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   182
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   183
    def __init__(self, exampleClass):
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   184
        #give it same validation rules as what it holds
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   185
        self._prototype = exampleClass
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   186
        example = exampleClass()
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   187
        self._attrMap = example._attrMap.copy()
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   188
        #give it same default values as whhat it holds
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   189
        self.setProperties(example.getProperties())
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   190
        self._children = {}
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   191
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   192
    def __getitem__(self, index):
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   193
        try:
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   194
            return self._children[index]
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   195
        except KeyError:
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   196
            child = self._prototype()
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   197
            #should we copy down?  how to keep in synch?
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   198
            child.setProperties(Widget.getProperties(self))
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   199
            self._children[index] = child
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   200
            return child
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   201
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   202
    def __setitem__(self, key, value):
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   203
        msg = "This collection can only hold objects of type %s" % self._prototype.__name__
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   204
        assert isinstance(value, self._prototype), msg
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   205
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   206
    def __len__(self):
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   207
        return len(self._children.keys())
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   208
        
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   209
    def getProperties(self):
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   210
        # return any children which are defined and whatever
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   211
        # differs from the parent
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   212
        props = {}
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   213
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   214
        for (key, value) in Widget.getProperties(self).items():
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   215
            props['%s' % key] = value
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   216
584
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   217
        for idx in self._children.keys():
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   218
            childProps = self._children[idx].getProperties()
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   219
            for (key, value) in childProps.items():
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   220
                parentValue = getattr(self, key)
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   221
                if parentValue <> value:
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   222
                    newKey = '[%s].%s' % (idx, key)
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   223
                    props[newKey] = value
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   224
94214696e4b9 Added barchart compatibility
andy_robinson
parents: 574
diff changeset
   225
        return props
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   226
914
d4a08fcf249a Added a __len__ method to TypedPropertyCOllection.
dinu_gherman
parents: 848
diff changeset
   227
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   228
## No longer needed!
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   229
    
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   230
class StyleProperties(PropHolder):
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   231
	"""A container class for attributes used in charts and legends.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   232
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   233
	Attributes contained can be those for any graphical element
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   234
	(shape?) in the ReportLab graphics package. The idea for this
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   235
	container class is to be useful in combination with legends
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   236
	and/or the individual appearance of data series in charts.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   237
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   238
	A legend could be as simple as a wrapper around a list of style
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   239
	properties, where the 'desc' attribute contains a descriptive
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   240
	string and the rest could be used by the legend e.g. to draw 
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   241
	something like a color swatch. The graphical presentation of
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   242
	the legend would be its own business, though.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   243
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   244
	A chart could be inspecting a legend or, more directly, a list
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   245
	of style properties to pick individual attributes that it knows
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   246
	about in order to render a particular row of the data. A bar
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   247
	chart e.g. could simply use 'strokeColor' and 'fillColor' for
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   248
	drawing the bars while a line chart could also use additional
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   249
	ones like strokeWidth.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   250
	"""
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   251
	
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   252
	_attrMap = {
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   253
		'strokeWidth': shapes.isNumber,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   254
		'strokeLineCap': shapes.isNumber,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   255
		'strokeLineJoin': shapes.isNumber,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   256
		'strokeMiterLimit': None,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   257
		'strokeDashArray': shapes.isListOfNumbersOrNone,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   258
		'strokeOpacity': shapes.isNumber,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   259
		'strokeColor': shapes.isColorOrNone,
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   260
		'fillColor': shapes.isColorOrNone,
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   261
		'desc':shapes.isString
848
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   262
		}
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   263
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   264
	def __init__(self, **kwargs):
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   265
		"Initialize with attributes if any."
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   266
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   267
		for k, v in kwargs.items():
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   268
			setattr(self, k, v)
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   269
			
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   270
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   271
	def __setattr__(self, name, value):
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   272
		"Verify attribute name and value, before setting it."
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   273
		
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   274
		# Make sure attribute is allowed.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   275
		if self._attrMap != None and name[0] != '_':
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   276
			className = self.__class__.__name__
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   277
			try:
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   278
				checker = self._attrMap[name]
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   279
				if checker and not checker(value):
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   280
					msg = "Illegal assignment of '%s' to '%s' in class %s" \
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   281
						  % (value, name, className)
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   282
					raise AttributeError, msg
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   283
			except KeyError:
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   284
				msg = "Illegal attribute '%s' in class %s" \
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   285
					  % (name, className)
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   286
				raise AttributeError, msg
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   287
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   288
		# Now set it.
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   289
		self.__dict__[name] = value
0b9f07f0a2ed Added StyleProeprties class
rgbecker
parents: 847
diff changeset
   290
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   291
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   292
class TwoCircles(Widget):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   293
    def __init__(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   294
        self.leftCircle = shapes.Circle(100,100,20, fillColor=colors.red)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   295
        self.rightCircle = shapes.Circle(300,100,20, fillColor=colors.red)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   296
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   297
    def draw(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   298
        return shapes.Group(self.leftCircle, self.rightCircle)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   299
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   300
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   301
class Face(Widget):
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   302
    """This draws a face with two eyes.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   303
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   304
    It exposes a couple of properties
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   305
    to configure itself and hides all other details.
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   306
    """
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   307
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   308
    def checkMood(moodName):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   309
        return (moodName in ('happy','sad','ok'))
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   310
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   311
    _attrMap = {
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   312
        'x': shapes.isNumber,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   313
        'y': shapes.isNumber,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   314
        'size': shapes.isNumber,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   315
        'skinColor':shapes.isColorOrNone,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   316
        'eyeColor': shapes.isColorOrNone,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   317
        'mood': checkMood 
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   318
        }
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   319
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   320
    def __init__(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   321
        self.x = 10
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   322
        self.y = 10
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   323
        self.size = 80
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   324
        self.skinColor = None
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   325
        self.eyeColor = colors.blue
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   326
        self.mood = 'happy'
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   327
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   328
    def demo(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   329
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   330
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   331
    def draw(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   332
        s = self.size  # abbreviate as we will use this a lot
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   333
        g = shapes.Group()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   334
        g.transform = [1,0,0,1,self.x, self.y]
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   335
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   336
        # background
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   337
        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
   338
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   339
        # left eye
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   340
        g.add(shapes.Circle(s * 0.35, s * 0.65, s * 0.1, fillColor=colors.white))
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   341
        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
   342
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   343
        # right eye
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   344
        g.add(shapes.Circle(s * 0.65, s * 0.65, s * 0.1, fillColor=colors.white))
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   345
        g.add(shapes.Circle(s * 0.65, s * 0.65, s * 0.05, fillColor=self.eyeColor))
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   346
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   347
        # nose
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   348
        g.add(shapes.Polygon(
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   349
            points=[s * 0.5, s * 0.6, s * 0.4, s * 0.3, s * 0.6, s * 0.3],
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   350
            fillColor=None))
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   351
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   352
        # mouth
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   353
        if self.mood == 'happy':
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   354
            offset = -0.05
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   355
        elif self.mood == 'sad':
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   356
            offset = +0.05
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   357
        else:
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   358
            offset = 0
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   359
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   360
        g.add(shapes.Polygon(
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   361
            points = [
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   362
                s * 0.3, s * 0.2, #left of mouth
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   363
                s * 0.7, s * 0.2, #right of mouth
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   364
                s * 0.6, s * (0.2 + offset), # the bit going up or down
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   365
                s * 0.4, s * (0.2 + offset) # the bit going up or down
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   366
                ],
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   367
            fillColor = colors.pink,
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   368
            strokeColor = colors.red,
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   369
            strokeWidth = s * 0.03
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   370
            ))
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   371
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   372
        return g
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   373
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   374
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   375
class TwoFaces(Widget):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   376
    def __init__(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   377
        self.faceOne = Face()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   378
        self.faceOne.mood = "happy"
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   379
        self.faceTwo = Face()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   380
        self.faceTwo.x = 100
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   381
        self.faceTwo.mood = "sad"
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   382
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   383
    def draw(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   384
        """Just return a group"""
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   385
        return shapes.Group(self.faceOne, self.faceTwo)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   386
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   387
    def demo(self):
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   388
        """The default case already looks good enough,
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   389
        no implementation needed here"""
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   390
        pass
847
c1b686db033c Robin's refactoring to separate propholder and Widget
andy_robinson
parents: 817
diff changeset
   391
925
0436c4cb1d71 Code and docstring enhancements.
dinu_gherman
parents: 914
diff changeset
   392
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   393
def test():
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   394
    d = shapes.Drawing(400, 200)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   395
    tc = TwoCircles()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   396
    d.add(tc)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   397
    import renderPDF
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   398
    renderPDF.drawToFile(d, 'sample_widget.pdf', 'A Sample Widget')
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   399
    print 'saved sample_widget.pdf'
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   400
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   401
    d = shapes.Drawing(400, 200)
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   402
    f = Face()
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   403
    f.skinColor = colors.yellow
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   404
    f.mood = "sad"
592
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   405
    d.add(f, name='theFace')
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   406
    print 'drawing 1 properties:'
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   407
    d.dumpProperties()
568
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   408
    renderPDF.drawToFile(d, 'face.pdf', 'A Sample Widget')
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   409
    print 'saved face.pdf'
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   410
592
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   411
    d2 = d.expandUserNodes0()
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   412
    renderPDF.drawToFile(d2, 'face_copy.pdf', 'An expanded drawing')
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   413
    print 'saved face_copy.pdf'
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
diff changeset
   414
    print 'drawing 2 properties:'
35f70c45f74c Fixed user node rendering bug, added barchart1
andy_robinson
parents: 584
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__':
9cadc5ef53db Added graphics module
andy_robinson
parents:
diff changeset
   419
    test()