File: style.py

package info (click to toggle)
pythoncad 0.1.23-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,044 kB
  • ctags: 3,568
  • sloc: python: 54,867; sh: 100; makefile: 39
file content (210 lines) | stat: -rw-r--r-- 6,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#
# Copyright (c) 2002, 2003, 2004, 2005 Art Haas
#
# This file is part of PythonCAD.
# 
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# classes for styles
#

import types

from PythonCAD.Generic import color
from PythonCAD.Generic import linetype
from PythonCAD.Generic import util

class Style(object):
    """A class storing a particular of Linetype, Color, and Thickness.

A Style consists of four attributes:

name: The Style name
linetype: A Linetype object
color: A Color object
thickness: A positive float value giving the line thickness

A Style has the following methods:

getName(): Get the Style name.
getColor(): Get the Style color.
getLinetype(): Get the Style Linetype.
getThickness(): Get the Style line thickness.
clone(): Return an identical copy of the Style.

Once a Style is created, the values in that object cannot
be changed.
    """

    __defcolor = color.Color(0xffffff)
    
    def __init__(self, name, lt=None, col=None, t=None):
        """Instatiate a Style object.

Style(name [, lt, col, t])

name: A string giving the style a name

Option arguments:
lt: A Linetype object - defaults to a solid line Linetype
col: A Color object - defaults to the default Color object
t: A positive float value - defaults to 1.0
        """
        if not isinstance(name, types.StringTypes):
            raise TypeError, "Invalid Style name: " + `name`
        _n = name
        if not isinstance(_n, unicode):
            _n = unicode(name)
        _lt = lt
        if _lt is None:
            _lt = linetype.Linetype('Default_Solid', None)
        if not isinstance(_lt, linetype.Linetype):
            raise TypeError, "Invalid linetype: " + `_lt`
        _c = col
        if _c is None:
            _c = Style.__defcolor
        if not isinstance(_c, color.Color):
            _c = color.Color(color)
        _t = t
        if _t is None:
            _t = 1.0
        _t = util.get_float(_t)
        if _t < 0.0:
            raise ValueError, "Invalid line thickness: %g" % _t
        self.__name = _n
        self.__linetype = _lt
        self.__color = _c
        self.__thickness = _t

    def __eq__(self, obj):
        """Compare a Style object to another Style for equality.

Comparing two styles is really comparing that the linetypes
are the same, then the colors are the same, and that the
line thickness are the same (within a tiny tolerance). If all
three are the same, the comparison returns True. Otherwise, the
comparison returns False.
        """
        if not isinstance(obj, Style):
            raise TypeError, "Invalid object for Style comparison: " + str(obj)
        _val = False        
        if (self.__name == obj.getName() and
            self.__linetype == obj.getLinetype() and
            self.__color == obj.getColor() and
            abs(self.__thickness - obj.getThickness()) < 1e-10):
            _val = True
        return _val

    def __ne__(self, obj):
        """Compare a Style object to another Style for non-equality.

Comparing two styles is really comparing that the linetypes
are the same, then the colors are the same, and that the
line thickness are the same (within a tiny tolerance). If all
three are the same, the comparison returns False. Otherwise, the
comparison returns True.
        """
        if not isinstance(obj, Style):
            raise TypeError, "Invalid object for Style comparison: " + str(obj)
        _val = True        
        if (self.__name == obj.getName() and
            self.__linetype == obj.getLinetype() and
            self.__color == obj.getColor() and
            abs(self.__thickness - obj.getThickness()) < 1e-10):
            _val = False
        return _val

    def __hash__(self):
        """Return a hash value for the Style.

Defining this method allows Styles to be stored in dictionaries.
        """
        _val = hash(self.__color)
        _val = _val ^ hash(self.__linetype)
        _val = _val ^ hash(long(self.__thickness * 1e10))
        return _val

    def getName(self):
        """Return the name of the Style.

getName()        
        """
        return self.__name

    name = property(getName, None, None, "Style name.")
    
    def getLinetype(self):
        """Return the Linetype used by this Style.

getLinetype()        
        """
        return self.__linetype

    linetype = property(getLinetype, None, None, "Style Linetype")

    def getColor(self):
        """Return the Color used by this Style.

getColor()
        """
        return self.__color

    color = property(getColor, None, None, "Style Color")
    
    def getThickness(self):
        """Return the line thickness used by this style.

getThickness()        
        """
        return self.__thickness

    thickness = property(getThickness, None, None, "Style Thickness.")

    def getStyleValues(self):
        _n = self.__name
        _l = self.__linetype.getName(), self.__linetype.getList()
        _c = self.__color.getColors()
        _t = self.thickness
        return _n, _l, _c, _t
    
    def clone(self):
        """Return an identical copy of a Style.

clone()
        """
        _name = self.__name[:]
        _linetype = self.__linetype.clone()
        _color = self.__color.clone()
        _thickness = self.__thickness
        return Style(_name, _linetype, _color, _thickness)
#
# StyleDict Class
#
# The StyleDict is built from the dict object. Using instances
# of this class will guarantee than only Style objects will be
# stored in the instance
#

class StyleDict(dict):
    def __init__(self):
        super(StyleDict, self).__init__()
        
    def __setitem__(self, key, value):
        if not isinstance(key, Style):
            raise TypeError, "StyleDict keys must be Style objects: " + `key`
        if not isinstance(value, Style):
            raise TypeError, "StyleDict values must be Style objects: " + `value`
        super(StyleDict, self).__setitem__(key, value)