File size: 6,849 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# *   Copyright (c) 2020 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

import Path

__title__ = "Property type abstraction for editing purposes"
__author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecad.org"
__doc__ = "Prototype objects to allow extraction of setup sheet values and editing."


Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
# Path.Log.trackModule(Path.Log.thisModule())


class Property(object):
    """Base class for all prototype properties"""

    def __init__(self, name, propType, category, info):
        self.name = name
        self.propType = propType
        self.category = category
        self.info = info
        self.editorMode = 0
        self.value = None

    def setValue(self, value):
        self.value = value

    def getValue(self):
        return self.value

    def setEditorMode(self, mode):
        self.editorMode = mode

    def displayString(self):
        if self.value is None:
            t = self.typeString()
            p = "an" if t[0] in ["A", "E", "I", "O", "U"] else "a"
            return "%s %s" % (p, t)
        return self.value

    def typeString(self):
        return "Property"

    def setupProperty(self, obj, name, category, value):
        created = False
        if not hasattr(obj, name):
            obj.addProperty(self.propType, name, category, self.info)
            self.initProperty(obj, name)
            created = True
        setattr(obj, name, value)
        return created

    def initProperty(self, obj, name):
        pass

    def setValueFromString(self, string):
        self.setValue(self.valueFromString(string))

    def valueFromString(self, string):
        return string


class PropertyEnumeration(Property):
    def typeString(self):
        return "Enumeration"

    def setValue(self, value):
        if list == type(value):
            self.enums = value
        else:
            super(PropertyEnumeration, self).setValue(value)

    def getEnumValues(self):
        return self.enums

    def initProperty(self, obj, name):
        setattr(obj, name, self.enums)


class PropertyQuantity(Property):
    def displayString(self):
        if self.value is None:
            return Property.displayString(self)
        return self.value.getUserPreferred()[0]


class PropertyAngle(PropertyQuantity):
    def typeString(self):
        return "Angle"


class PropertyDistance(PropertyQuantity):
    def typeString(self):
        return "Distance"


class PropertyLength(PropertyQuantity):
    def typeString(self):
        return "Length"


class PropertyPercent(Property):
    def typeString(self):
        return "Percent"


class PropertyFloat(Property):
    def typeString(self):
        return "Float"

    def valueFromString(self, string):
        return float(string)


class PropertyInteger(Property):
    def typeString(self):
        return "Integer"

    def valueFromString(self, string):
        return int(string)


class PropertyBool(Property):
    def typeString(self):
        return "Bool"

    def valueFromString(self, string):
        return bool(string)


class PropertyString(Property):
    def typeString(self):
        return "String"


class PropertyMap(Property):
    def typeString(self):
        return "Map"

    def displayString(self, value):
        return str(value)


class OpPrototype(object):

    PropertyType = {
        "App::PropertyAngle": PropertyAngle,
        "App::PropertyBool": PropertyBool,
        "App::PropertyDistance": PropertyDistance,
        "App::PropertyEnumeration": PropertyEnumeration,
        "App::PropertyFile": PropertyString,
        "App::PropertyFloat": PropertyFloat,
        "App::PropertyFloatConstraint": Property,
        "App::PropertyFloatList": Property,
        "App::PropertyInteger": PropertyInteger,
        "App::PropertyIntegerList": PropertyInteger,
        "App::PropertyLength": PropertyLength,
        "App::PropertyLink": Property,
        "App::PropertyLinkList": Property,
        "App::PropertyLinkSubListGlobal": Property,
        "App::PropertyMap": PropertyMap,
        "App::PropertyPercent": PropertyPercent,
        "App::PropertyString": PropertyString,
        "App::PropertyStringList": Property,
        "App::PropertyVectorDistance": Property,
        "App::PropertyVectorList": Property,
        "Part::PropertyPartShape": Property,
    }

    def __init__(self, name):
        self.Label = name
        self.properties = {}
        self.DoNotSetDefaultValues = True
        self.Proxy = None

    def __setattr__(self, name, val):
        if name in ["Label", "DoNotSetDefaultValues", "properties", "Proxy"]:
            if name == "Proxy":
                val = None  # make sure the proxy is never set
            return super(OpPrototype, self).__setattr__(name, val)
        self.properties[name].setValue(val)

    def addProperty(self, typeString, name, category, info=None):
        prop = self.PropertyType[typeString](name, typeString, category, info)
        self.properties[name] = prop
        return self

    def setEditorMode(self, name, mode):
        self.properties[name].setEditorMode(mode)

    def getProperty(self, name):
        return self.properties[name]

    def setupProperties(self, setup):
        return [p for p in self.properties if p.name in setup]