File: entity.py

package info (click to toggle)
pythoncad 0.1.35-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,536 kB
  • ctags: 4,286
  • sloc: python: 62,752; sh: 743; makefile: 39
file content (746 lines) | stat: -rw-r--r-- 21,738 bytes parent folder | download | duplicates (4)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#
# Copyright (c) 2004, 2005, 2006 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
#
#
# _The_ base class for all objects used in PythonCAD
#

import types

from PythonCAD.Generic import message
from PythonCAD.Generic import logger
from PythonCAD.Generic import util

class Entity(message.Messenger):
    """The base class for PythonCAD entities.

An entity has the following properties:

parent: The parent of an entity - another entity or None

An entity has the following methods:

{set/get}Lock(): Set/Get the locked state on an entity.
isLocked(): Returns the locked state of an entity.
isModified(): Returns the flag indicating the entity has been modified.
modified()/reset(): Set/Unset a flag indicating the entity modified state.
isVisible(): Returns the visibity of the entity.
{set/get}Visibility(): Set/Get the visibility flag on an entity.
{set/get}Parent(): Set/Get the parent entity of an entity.
{add/del}Child(): Add/Remove a child entity to/from another entity.
hasChild(): Test if one entity is a child of another.
hasChildren(): Test if an entity has any children entities.
getChildren(): Return a list of children entities for an entity.

A few short aliases exist for some of these methods:
lock(): Same as setLock(True)
unlock(): Same as setLock(False)
hide(): Same as setVisibility(True)
show()/expose(): Same as setVisibility(False)

The aliases may be removed at some point.
    """
    __messages = {
        'lock_changed' : True,
        'modified' : True,
        'reset' : True,
        # 'parent_changed' : True,
        'visibility_changed' : True,
        'refresh' : True,
        'added_child' : True,
        'removed_child' : True,
        'change_pending' : True,
        'change_complete' : True,
        }

    __idcount = 0

    def __init__(self, **kw):
        _parent = _id = None
        if 'parent' in kw:
            _parent = kw['parent']
            if _parent is not None and not isinstance(_parent, Entity):
                raise TypeError, "Unexpected parent type: " + `type(_parent)`
        if 'id' in kw:
            _id = kw['id']
            if not isinstance(_id, int):
                raise TypeError, "Unexpected ID type: " + `type(_id)`
            if _id < 0 or _id >= Entity.__idcount:
                raise ValueError, "Invalid ID: %d" % _id
        if _id is None:
            _id = Entity.__idcount
            Entity.__idcount = Entity.__idcount + 1
        super(Entity, self).__init__()
        self.__locked = False
        self.__modified = False
        self.__visible = True
        self.__parent = None
        self.__children = None
        self.__log = None
        self.__undoing = 0 # 1 => undo, -1 => redo
        self.__id = _id
        if _parent is not None:
            Entity.setParent(self, _parent)

    def finish(self):
        if self.__parent is not None:
            print "parent is not None: " + `self`
            print "parent: " + `self.__parent`
        if self.__children is not None:
            print "children is not None: " + `self`
            for _child in self.__children.values():
                print "Child: " + `_child`
        super(Entity, self).finish()

    def getValues(self):
        """Return values comprising the Entity.

getValues()

This method returns an EntityData instance.
        """
        _data = EntityData()
        _data.setValue('id', self.__id)
        _pid = None
        if self.__parent is not None:
            _pid = self.__parent.getID()
        _data.setValue('pid', _pid)
        return _data

    values = property(getValues, None, None, "Entity values")

    def getID(self):
        """Return the unique integer identifier for the entity.

getID()
        """
        return self.__id

    id = property(getID, None, None, "Entity ID")

    def isLocked(self):
        """Test if the entity has been locked.

isLocked()

This method returns a boolean.
        """
        return self.__locked

    def lock(self):
        """Set the entity so that its attributes cannot be changed.

lock()
        """
        Entity.setLock(self, True)

    def unlock(self):
        """Set the entity so that its attributes can be changed.

unlock()
        """
        Entity.setLock(self, False)

    def getLock(self):
        """Return the locked status of an entity.

getLock()        
        """
        return self.__locked
    
    def setLock(self, lock):
        """Set the locked status on an entity.

setLock(lock)

Argument 'lock' can be True or False.
        """
        util.test_boolean(lock)
        _l = self.__locked
        if _l is not lock:
            self.__locked = lock
            self.sendMessage('lock_changed', _l)
            self.modified()
        
    def isModified(self):
        """Test if the entity has been altered in some form.

isModified()

This method returns a boolean.
        """
        return self.__modified

    def startChange(self, msg):
        """Enter the modification state of an entity.

startChange(msg)

Argument 'msg' is the message the entity will send at the completion
of the modification.
        """
        if not self.sendsMessage(msg):
            raise ValueError, "Unknown message: %s" % msg
        self.sendMessage('change_pending', msg)

    def endChange(self, msg):
        """Complete the modification state of an entity.

endChange(msg)

This method is meant to complete the notification process begun
by the startChange() method.
        """
        if not self.sendsMessage(msg):
            raise ValueError, "Unknown message: %s" % msg
        self.sendMessage('change_complete', msg)
        
    def modified(self):
        """Indicate that the entity has been altered.

modified()
        """
        if not self.__modified:
            self.__modified = True
        self.sendMessage('modified')

    def reset(self):
        """Indicate that the entity is no longer altered.

reset()
        """
        if self.__modified:
            self.__modified = False
        self.sendMessage('reset')

    def isVisible(self):
        """Test to see if the entity can be seen.

isVisible()

This method returns a boolean.
        """
        return self.__visible is True

    def getVisibility(self):
        """Return the visibility flag of an entity.

getVisibility()        
        """
        return self.__visible
    
    def setVisibility(self, vis):
        """Set the visibility of the entity.

setVisibility(vis)

Argument 'vis' must be either True or False.
        """
        util.test_boolean(vis)
        _v = self.__visible
        if _v is not vis:
            self.startChange('visibility_changed')
            self.__visible = vis
            self.endChange('visibility_changed')
            self.sendMessage('visibility_changed', _v)
            self.modified()

    def hide(self):
        """Mark the entity invisible.

hide()
        """
        Entity.setVisibility(self, False)

    def show(self):
        """Mark the entity as visible.

show()
        """
        Entity.setVisibility(self, True)

    def expose(self):
        """Mark the entity as visible.

expose()

This method is identical to show().
        """
        Entity.setVisibility(self, True)

    def canParent(self, obj):
        """Test if an Entity can be the parent of another Entity.

canParent(obj)

Subclasses should override this method if the ability to be
the parent of another entity needs refinement.
        """
        return True

    def setParent(self, parent):
        """Store the parent of the entity.

setParent(parent)

Argument 'parent' must be another entity, and the parent
entity is tested to ensure that it can have children entities.
        """
        if parent is not None:
            if not isinstance(parent, Entity):
                raise TypeError, "Unexpected parent type: " + `type(parent)`
            if not parent.canParent(self):
                raise ValueError, "Invalid parent for Entity: " + `parent`
        _oldparent = self.__parent
        if _oldparent is not parent:
            if _oldparent is not None:
                Entity.__delChild(_oldparent, self)
            self.__parent = parent
            if parent is not None:
                Entity.__addChild(parent, self)
            #
            # fixme - re-examine sending (or not) the 'parent_changed'
            # and 'modified' messages when this method is invoked as this
            # method invocation is often called as part of an add/delete
            # operation that and as such can be thought of as a "side-effect"
            #
            # self.sendMessage('parent_changed', _oldparent)
            # self.modified()

    def getParent(self):
        """Return the parent entity of an entity.

getParent()

This method returns an Entity instance, or None of no
parent has been set.
        """
        return self.__parent

    parent = property(getParent, setParent, None, "Parent of an Entity.")

    def __addChild(self, child):
        """Take an entity as a child.

addChild(child)

This method is private to the Entity class.
        """
        if not isinstance(child, Entity):
            raise TypeError, "Unexpected child type: " + `type(child)`
        if child.getParent() is not self:
            raise ValueError, "Invalid parent: " + `child`
        if self.__children is None:
            self.__children = {}
        _cid = id(child)
        if _cid in self.__children:
            raise ValueError, "Child entity already stored: " + `child`
        self.__children[_cid] = child
        self.sendMessage('added_child', child)
        self.modified()

    def __delChild(self, child):
        """Remove an entity as a child.

delChild(child)

This method is private to the Entity class.
        """
        if child.getParent() is not self:
            raise ValueError, "Invalid parent for child: " + `child`
        if self.__children is None:
            raise ValueError, "Entity has no children: " + `self`
        _cid = id(child)
        if _cid not in self.__children:
            raise ValueError, "Child entity not stored: " + `child`
        del self.__children[_cid]
        if len(self.__children) == 0:
            self.__children = None
        self.sendMessage('removed_child', child)
        self.modified()

    def hasChild(self, child):
        """Test if an entity is a stored as a child.

hasChild(child)
        """
        return self.__children is not None and id(child) in self.__children

    def hasChildren(self):
        """Test if an entity has children entities.

hasChildren()

This method returns a boolean.
        """
        return self.__children is not None

    def getChildren(self):
        """Return any children entities of an entity.

getChildren()

This method returns a list of children entities.
        """
        if self.__children is not None:
            return self.__children.values()
        return []

    def sendsMessage(self, m):
        """Test if an entity can send a type of message.

sendsMessage(m)

Argument 'm' should be a string giving the message name.
This method returns a boolean.
        """
        if m in Entity.__messages:
            return True
        return super(Entity, self).sendsMessage(m)

    def clone(self):
        """Return an identical copy of an entity.

clone()
        """
        return Entity(parent=self.__parent)

    def inRegion(self, xmin, ymin, xmax, ymax, all=False):
        """Test if an entity is found in a spatial region.

inRegion(xmin, ymin, xmax, ymax[, all])

Arguments 'xmin', 'ymin', 'xmax', and 'ymax' should all be
floats. Argument 'all' is a boolean, with the default set
to False. If 'all' is true, then the entire entity must
lie within the boundary.
        """
        return False

    def getLog(self):
        """Return the history log for an entity.

getLog()

This method returns a Logger instance, or None if no log
has been assigned to the entity.
        """
        return self.__log

    def setLog(self, log):
        """Assign a Logger instance to an entity.

setLog(log)

Argument 'log' should be a Logger instance, or None.
        """
        if log is not None:
            if not isinstance(log, logger.Logger):
                raise TypeError, "Unexpected log type: " + `type(log)`
            if self.__log is not None:
                raise ValueError, "Entity already contains a log."
        self.__log = log

    def delLog(self):
        """Remove a Logger instance from an entity.

delLog()
        """
        if self.__log is not None:
            self.__log.clear()
        self.__log = None

    def undo(self):
        """Undo an action.

undo()

Using undo() requires a Logger instance to be assigned to the entity.
        """
        if self.__log is not None:
            self.__log.undo()

    def startUndo(self, mute=False):
        """Store an indication that an undo operation is commencing.

startUndo([mute])

Optional argument 'mute' must be a boolean. By default it is False, and
when True the entity will be muted during the undo operation.
        """
        _state = self.__undoing
        if _state != 0:
            if _state == -1:
                raise RuntimeError, "Object in redo state: " + `self`
            elif _state == 1:
                raise RuntimeError, "Object already in undo state: " + `self`
            else:
                raise ValueError, "Unexpected undo/redo state: %d" % _state
        if mute and False:
            self.mute()
        self.__undoing = 1

    def endUndo(self):
        """Set the entity to the state of undo operation completion.

endUndo()
        """
        _state = self.__undoing
        if _state != 1:
            if _state == -1:
                raise RuntimeError, "Object in redo state: " + `self`
            elif _state == 0:
                raise RuntimeError, "Object not in undo state: " + `self`
            else:
                raise ValueError, "Unexpected undo/redo state: %d" % _state
        self.__undoing = 0

    def inUndo(self):
        """Test if the entity is currently in an undo operation.

inUndo()

This method returns a boolean.
        """
        return self.__undoing == 1

    def redo(self):
        """Redo an action.

redo()

Using redo() requires a Logger instance to be assigned to the entity.
        """
        if self.__log is not None:
            self.__log.redo()

    def startRedo(self):
        """Store an indication that an redo operation is commencing.

startRedo()

        """
        _state = self.__undoing
        if _state != 0:
            if _state == 1:
                raise RuntimeError, "Object in undo state: " + `self`
            elif _state == -1:
                raise RuntimeError, "Object already in redo state: " + `self`
            else:
                raise ValueError, "Unexpected undo/redo state: %d" % _state
        self.__undoing = -1

    def endRedo(self):
        """Set the entity to the state of redo operation completion.

endRedo()
        """
        _state = self.__undoing
        if _state != -1:
            if _state == 1:
                raise RuntimeError, "Object in undo state: " + `self`
            elif _state == 0:
                raise RuntimeError, "Object not in redo state: " + `self`
            else:
                raise ValueError, "Unexpected undo/redo state: %d" % _state
        self.__undoing = 0

    def inRedo(self):
        """Test if the entity is currently in a redo operation.

inRedo()

This method returns a boolean.
        """
        return self.__undoing == -1

#
# Entity history class
#

class EntityLog(logger.Logger):
    def __init__(self, obj):
        if not isinstance(obj, Entity):
            raise TypeError, "Unexpected entity type: " + `type(obj)`
        super(EntityLog, self).__init__()
        self.__obj = obj
        obj.connect('visibility_changed', self.__visibilityChanged)
        obj.connect('lock_changed', self.__lockChanged)
        # obj.connect('parent_changed', self.__parentChanged)

    def detatch(self):
        self.__obj.disconnect(self)
        self.__obj = None

    def getObject(self):
        """Return the object stored in the log.

getEntity()        
        """
        return self.__obj
    
    def __visibilityChanged(self, obj, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _vis = args[0]
        util.test_boolean(_vis)
        self.saveUndoData('visibilty_changed', _vis)

    def __lockChanged(self, obj, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _lock = args[0]
        util.test_boolean(_lock)
        self.saveUndoData('lock_changed', _lock)
        
    def __parentChanged(self, obj, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _parent = args[0]
        if _parent is not None and not isinstance(_parent, Entity):
            raise TypeError, "Unexpected parent type: " + `type(_parent)`
        _pid = None
        if _parent is not None:
            _pid = _parent.getID()
        self.saveUndoData('parent_changed', _pid)

    def execute(self, undo, *args):
        # print "EntityLog::execute() ..."
        # print args
        util.test_boolean(undo)
        _alen = len(args)
        if len(args) == 0:
            raise ValueError, "No arguments to execute()"
        _obj = self.__obj
        _op = args[0]
        if _op == 'visibility_changed':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _sdata = _obj.getVisibility()
            self.ignore(_op)
            try:
                _vis = args[1]
                if undo:
                    _obj.startUndo()
                    try:
                        _obj.setVisibility(_vis)
                    finally:
                        _obj.endUndo()
                else:
                    _obj.startRedo()
                    try:
                        _obj.setVisibility(_vis)
                    finally:
                        _obj.endRedo()
            finally:
                self.receive(_op)
            self.saveData(undo, _op, _sdata)
        elif _op == 'lock_changed':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _sdata = _obj.isLocked()
            self.ignore(_op)
            try:
                _lock = args[1]
                if undo:
                    _obj.startUndo()
                    try:
                        _obj.setLock(_lock)
                    finally:
                        _obj.endUndo()
                else:
                    _obj.startRedo()
                    try:
                        _obj.setLock(_lock)
                    finally:
                        _obj.endRedo()
            finally:
                self.receive(_op)
            self.saveData(undo, _op, _sdata)
        else:
            raise ValueError, "Unexpected operation: %s" % _op

    def saveData(self, undo, op, *args):
        util.test_boolean(undo)
        if undo:
            self.saveRedoData(op, *args)
        else:
            self.saveUndoData(op, *args)

#
# property storage class
#

class EntityData(object):
    def __init__(self):
        self.__values = {}
        self.__locked = False

    def keys(self):
        return self.__values.keys()

    def values(self):
        return self.__values.values()

    def lock(self):
        self.__locked = True

    def unlock(self):
        self.__locked = False

    def isLocked(self):
        return self.__locked is True

    def setValue(self, key, value):
        if self.__locked:
            raise RuntimeError, "EntityData instance is locked: " + `self`
        if not isinstance(key, types.StringTypes):
            raise TypeError, "Invalid key type: " + `type(key)`
        if not EntityData._testValueType(self, value):
            raise TypeError, "Invalid value type: " + `type(value)`
        self.__values[key] = value

    def _testValueType(self, value):
        _pass = isinstance(value, (types.StringTypes, types.NoneType, int, float, EntityData))
        if not _pass:
            if isinstance(value, (tuple, list)):
                for _v in value:
                    _pass = EntityData._testValueType(self, _v)
                    if not _pass:
                        break
            elif isinstance(value, dict):
                for _k, _v in value.items():
                    _pass = isinstance(_k, types.StringTypes)
                    if not _pass:
                        break
                    _pass = EntityData._testValueType(self, _v)
                    if not _pass:
                        break
            else:
                _pass = False
        return _pass

    def getValue(self, key):
        return self.__values[key]

    def get(self, key):
        return self.__values.get(key)