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