-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgl.py
3052 lines (2528 loc) · 85.2 KB
/
pgl.py
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: pgl.py
"""
The pgl module implements the Portable Graphics Library (pgl) on top of
Tkinter, which is the most common graphics package for use with Python.
"""
PGL_VERSION = 0.80
PGL_DATE = "14-Oct-2018"
import atexit
import inspect
import math
import sys
import time
try:
import tkinter
try:
import tkinter.font as tkFont
except Exception:
import tkFont
except Exception as e:
print('Could not load tkinter: ' + str(e))
try:
from PIL import ImageTk, Image
_imageModel = "PIL"
except Exception:
_imageModel = "PhotoImage"
# Class GWindow
class GWindow(object):
"""
This class represents a graphics window that can contain graphical
objects.
"""
# Public constants
DEFAULT_WIDTH = 500
DEFAULT_HEIGHT = 300
# Constructor: GWindow
def __init__(self, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT):
"""
The constructor takes either of the following forms:
<pre>
GWindow()
GWindow(width, height)
</pre>
If the dimensions are missing, the constructor creates a
<code>GWindow</code> with a default size.
"""
global _rootWindow
tk = tkinter._default_root # For compatibility with filechooser.py
if tk is None:
tk = tkinter.Tk()
else:
tk.deiconify()
self.windowWidth = width
self.windowHeight = height
self.tk = tk
self.tk.protocol("WM_DELETE_WINDOW", sys.exit)
self.canvas = tkinter.Canvas(tk, width=width, height=height)
self.canvas.pack()
self.canvas.update()
self.images = { }
self.base = GCompound()
self.base.gw = self
self.eventManager = _EventManager(self)
self.setWindowTitle(getProgramName())
_rootWindow = tk
def eventLoop():
tk.mainloop()
atexit.register(eventLoop)
def __eq__(self, other):
if other is None: return False
return self.canvas is other.canvas
def __ne__(self, other):
return not(self == other)
# Public method: close
def close(self):
"""
Deletes the window from the screen.
"""
global _rootWindow
_rootWindow.destroy()
# Public method: requestFocus
def requestFocus(self):
"""
Asks the system to assign the keyboard focus to the window, which
brings it to the top and ensures that key events are delivered to
the window. Clicking in the window automatically requests the focus.
"""
raise Exception("Not yet implemented")
# Public method: clear
def clear(self):
"""
Clears the contents of the window.
"""
self.base.removeAll()
# Public method: getWidth
def getWidth(self):
"""
Returns the width of the graphics window in pixels.
"""
return self.windowWidth
# Public method: getHeight
def getHeight(self):
"""
Returns the height of the graphics window in pixels.
"""
return self.windowHeight
# Public method: addEventListener
def addEventListener(self, type, fn):
"""
Adds an event listener of the specified type to the window.
"""
self.eventManager.addEventListener(type, fn)
# Public method: repaint
def repaint(self):
"""
Schedule a repaint on this window.
"""
pass
# Public method: setWindowTitle
def setWindowTitle(self, title):
"""
Sets the title of the graphics window.
"""
self.windowTitle = title
self.tk.title(title)
# Public method: getWindowTitle
def getWindowTitle(self):
"""
Returns the title of the graphics window.
"""
return self.windowTitle
# Public method: add
def add(self, gobj, x=None, y=None):
"""
Adds the <code>GObject</code> to the window. The first parameter
is the object to be added. The <code>x</code> and <code>y</code>
parameters are optional. If they are supplied, the location of
the object is set to (<code>x</code>, <code>y</code>).
"""
self.base.add(gobj, x, y)
# Public method: remove
def remove(self, gobj):
"""
Removes the object from the window.
"""
self.base.remove(gobj)
# Public method: getElementAt
def getElementAt(self, x, y):
"""
Returns the topmost <code>GObject</code> containing the
point (x, y), or <code>None</code> if no such object exists.
"""
return self.base.getElementAt(x, y)
# Public method: createTimer
def createTimer(self, fn, delay):
"""
Creates a new timer object that calls fn after the specified
delay, which is measured in milliseconds. The timer must be
started by calling the <code>start</code> method.
"""
return GTimer(self, fn, delay)
# Public method: setTimeout
def setTimeout(self, fn, delay):
"""
Creates and starts a one-shot timer that calls fn after the
specified delay, which is measured in milliseconds. The
setTimeout method returns the <code>GTimer</code> object.
"""
timer = GTimer(self, fn, delay)
timer.start()
return timer
# Private method: _rebuild
def _rebuild(self):
"""
Rebuilds the tkinter data structure for the window. This
operation is triggered if a global update is necessary.
"""
self.canvas.delete("all")
self.base._install(self, _SimpleTransform())
# Class: GObject
class GObject(object):
"""
This class is the common superclass of all graphical objects that can
be displayed on a graphical window. For examples illustrating the use
of the <code>GObject</code> class, see the descriptions of the
individual subclasses.
"""
# Constructor: GObject
def __init__(self):
"""
Creates a new <code>GObject</code>. The constructor is called
only by subclasses.
"""
self.x = 0.0
self.y = 0.0
self.color = "Black"
self.lineWidth = 1.0
self.visible = True
self.parent = None
self.tkid = None
self.gw = None
# Public method: getX
def getX(self):
"""
Returns the x-coordinate of the object.
"""
return self.x
# Public method: getY
def getY(self):
"""
Returns the y-coordinate of the object.
"""
return self.y
# Public method: getLocation
def getLocation(self):
"""
Returns the location of this object as a <code>GPoint</code>.
"""
return GPoint(self.x, self.y)
# Public method: setLocation
def setLocation(self, x, y):
"""
Sets the location of this object to the specified point.
"""
if type(x) is GPoint:
x, y = x.getX(), x.getY()
elif type(x) is dict:
x, y = x.x, x.y
self.x = x
self.y = y
self._updateLocation()
# Public method: move
def move(self, dx, dy):
"""
Moves the object on the screen using the displacements
<code>dx</code> and <code>dy</code>.
"""
self.setLocation(self.x + dx, self.y + dy)
# Public method: movePolar
def movePolar(self, r, theta):
"""
Moves the object on the screen the distance <i>r</i> in the
direction <i>theta</i>.
"""
dx = r * math.cos(math.radians(theta))
dy = -r * math.sin(math.radians(theta))
self.move(dx, dy)
# Public method: getWidth
def getWidth(self):
"""
Returns the width of this object, which is defined to be the width of
the bounding box.
"""
return self.getBounds().getWidth()
# Public method: getHeight
def getHeight(self):
"""
Returns the height of this object, which is defined to be the height
of the bounding box.
"""
return self.getBounds().getHeight()
# Public method: getSize
def getSize(self):
"""
Returns the size of the object as a <code>GDimension</code>.
"""
bounds = self.getBounds()
return GDimension(bounds.getWidth(), bounds.getHeight())
# Public method: setLineWidth
def setLineWidth(self, lineWidth):
"""
Sets the width of the line used to draw this object.
"""
self.lineWidth = lineWidth
self.updateProperties(width=lineWidth)
# Public method: getLineWidth
def getLineWidth(self):
"""
Returns the width of the line used to draw this object.
"""
return self.lineWidth
# Public method: setColour
def setColour(self, color):
"""
Alternate spelling for <code>setColor</code>.
"""
self.setColor(color)
# Public method: setColor
def setColor(self, color):
"""
Sets the color used to display this object. The color parameter is
usually one of the CSS color names. The color can also be specified
as a string in the form <code>"#rrggbb"</code> where <code>rr</code>,
<code>gg</code>, and <code>bb</code> are pairs of hexadecimal digits
indicating the red, green, and blue components of the color.
"""
rgb = convertColorToRGB(color)
self.color = convertRGBToColor(rgb)
self.updateColor()
# Public method: getColour
def getColour(self):
"""
Alternate spelling for <code>getColor</code>.
"""
return self.getColor()
# Public method: getColor
def getColor(self):
"""
Returns the current color as a string in the form
<code>"#rrggbb"</code>. In this string, the values <code>rr</code>,
<code>gg</code>, and <code>bb</code> are two-digit hexadecimal
values representing the red, green, and blue components.
"""
return self.color
# Public method: scale
def scale(self, sf):
"""
Scales the object by the specified scale factor.
"""
raise Exception("Not yet implemented")
# Public method: rotate
def rotate(self, theta):
"""
Transforms the object by rotating it theta degrees counterclockwise
around its origin.
"""
raise Exception("Not yet implemented")
# Public method: setVisible
def setVisible(self, flag):
"""
Sets whether this object is visible.
"""
self.visible = flag
raise Exception("Not yet implemented")
# Public method: isVisible
def isVisible(self):
"""
Returns true if this object is visible.
"""
return self.visible
# Public method: sendForward
def sendForward(self):
"""
Moves this object one step toward the front in the z dimension.
If it was already at the front of the stack, nothing happens.
"""
parent = self.getParent()
if parent is not None: parent._sendForward(self)
# Public method: sendToFront
def sendToFront(self):
"""
Moves this object to the front of the display in the z dimension.
By moving it to the front, this object will appear to be on top of the
other graphical objects on the display and may hide any objects that
are further back.
"""
parent = self.getParent()
if parent is not None: parent._sendToFront(self)
# Public method: sendBackward
def sendBackward(self):
"""
Moves this object one step toward the back in the z dimension.
If it was already at the back of the stack, nothing happens.
"""
parent = self.getParent()
if parent is not None: parent._sendBackward(self)
# Public method: sendToBack
def sendToBack(self):
"""
Moves this object to the back of the display in the z dimension.
By moving it to the back, this object will appear to be behind
the other graphical objects on the display and may be obscured
by other objects in front.
"""
parent = self.getParent()
if parent is not None: parent._sendToBack(self)
# Public method: contains
def contains(self, x, y):
"""
Returns true if the specified point is inside the object.
"""
if type(x) is GPoint:
x, y = x.getX(), x.getY()
elif type(x) is dict:
x, y = x.x, x.y
bounds = self.getBounds()
if bounds is None: return False
return bounds.contains(x, y)
# Public method: getParent
def getParent(self):
"""
Returns a pointer to the <code>GCompound</code> that contains this
object. Every <code>GWindow</code> is initialized to contain a
single <code>GCompound</code> that is aligned with the window.
Adding objects to the window adds them to that <code>GCompound</code>,
which means that every object you add to the window has a parent.
Calling <code>getParent</code> on the top-level <code>GCompound</code>
returns <code>None</code>.
"""
return self.parent
# Abstract method: getType
def getType(self):
"""
Returns the concrete type of the object as a string, as in
"GOval" or "GRect".
"""
raise Exception("getType is not defined in the GObject class")
# Abstract method: getBounds
def getBounds(self):
"""
Returns the bounding box of this object, which is defined to be the
smallest rectangle that covers everything drawn by the figure. The
coordinates of this rectangle do not necessarily match the location
returned by <code>getLocation</code>. Given a <code>GLabel</code>
object, for example, <code>getLocation</code> returns the
coordinates of the point on the baseline at which the string begins.
The <code>getBounds</code> method, by contrast, returns a rectangle
that covers the entire window area occupied by the string.
"""
raise Exception("getBounds is not defined in the GObject class")
# Private method: updateProperties
def updateProperties(self, **options):
"""
Updates the specified properties of the object, if it is installed
in a window.
"""
gw = self.getWindow()
if gw is None: return
tkc = gw.canvas
tkc.itemconfig(self.tkid, **options)
# Private method: _updateLocation
def _updateLocation(self):
"""
Updates the location for this object from the stored x and y
values. Some subclasses need to override this method.
"""
gw = self.getWindow()
if gw is None: return
tkc = gw.canvas
coords = tkc.coords(self.tkid)
dx = self.x - coords[0]
dy = self.y - coords[1]
tkc.move(self.tkid, dx, dy)
# Private method: updateColor
def updateColor(self):
"""
Updates the color properties. Some subclasses need to override
this method.
"""
self.updateProperties(fill=self.color)
# Private method: getWindow
def getWindow(self):
"""
Returns the <code>GWindow</code> in which this <code>GObject</code>
is installed. If the object is not installed in a window, this
method returns <code>None</code>.
"""
gobj = self
while (gobj.parent is not None):
gobj = gobj.parent
return gobj.gw
# Private abstract method: _install
def _install(self, target, ctm):
"""
Installs the object in the target, creating any tkinter objects
that are necessary.
"""
raise Exception("_install is not defined in the GObject class")
# Class: GFillableObject
class GFillableObject(GObject):
"""
This abstract class is the superclass of all objects that are fillable.
"""
# Constructor: GFillableObject
def __init__(self):
"""
Initializes a <code>GFillableObject</code>. Because this is an
abstract class, clients should not call this constructor explicitly.
"""
GObject.__init__(self)
self.fillFlag = False
self.fillColor = ""
# Public method: setFilled
def setFilled(self, flag):
"""
Sets the fill status for the object, where <code>False</code>
is outlined and <code>True</code> is filled.
"""
self.fillFlag = flag
self.updateColor()
# Public method: isFilled
def isFilled(self):
"""
Returns <code>True</code> if the object is filled.
"""
return self.fillFlag
# Public method: setFillColor
def setFillColor(self, color):
"""
Sets the color used to display the filled region of the object.
"""
rgb = convertColorToRGB(color)
self.fillColor = convertRGBToColor(rgb)
self.updateColor()
# Public method: getFillColor
def getFillColor(self):
"""
Returns the color used to display the filled region of this
object. If no fill color has been set, <code>getFillColor</code>
returns the empty string.
"""
return self.fillColor
# Override method: updateColor
def updateColor(self):
"""
Updates the color properties for a <code>GFillableObject</code>.
"""
outline = self.color
if self.fillFlag:
fill = self.fillColor
if fill is None or fill == "":
fill = outline
else:
fill = ""
self.updateProperties(outline=outline, fill=fill)
# Class: GRect
class GRect(GFillableObject):
"""
This class represents a graphical object whose appearance consists of
a rectangular box.
"""
# Constructor: GRect
def __init__(self, a1, a2, a3=None, a4=None):
"""
The <code>GRect</code> constructor takes either of the following
forms:
<pre>
GRect(width, height)
GRect(x, y, width, height)
</pre>
If the <code>x</code> and <code>y</code> parameters are missing,
the origin is set to (0, 0).
"""
GFillableObject.__init__(self)
if a3 is None:
x = 0
y = 0
width = a1
height = a2
else:
x = a1
y = a2
width = a3
height = a4
self.width = width
self.height = height
self.setLocation(x, y)
# Public method: setSize
def setSize(self, width, height=None):
"""
Changes the size of this rectangle as specified.
"""
if type(width) is GDimension:
width, height = width.getWidth(), width.getHeight()
self.width = width
self.height = height
gw = self.getWindow()
if gw is None: return
tkc = gw.canvas
coords = tkc.coords(self.tkid)
tkc.coords(self.tkid, coords[0], coords[1],
coords[0] + width, coords[1] + height)
# Public method: setBounds
def setBounds(self, x, y=None, width=None, height=None):
"""
Changes the bounds of this rectangle to the specified values.
"""
if type(x) is GRectangle:
width, height = x.getWidth(), x.getHeight()
x, y = x.getX(), x.getY()
self.setLocation(x, y)
self.setSize(width, height)
# Override method: getBounds
def getBounds(self):
"""
Returns the bounds of this <code>GRect</code>.
"""
return GRectangle(self.x, self.y, self.width, self.height)
# Override method: getType
def getType(self):
"""
Returns the type of this object.
"""
return "GRect"
# Override method: _install
def _install(self, target, ctm):
"""
Installs the <code>GRect</code> in the canvas.
"""
gw = target
tkc = gw.canvas
pt = ctm.transform(GPoint(self.getX(), self.getY()))
x = pt.getX()
y = pt.getY()
self.tkid = tkc.create_rectangle(x, y, x + self.width, y + self.height)
self.updateColor()
# Override method: __str__
def __str__(self):
return ("GRect(" + str(self.x) + ", " + str(self.y) + ", " +
str(self.width) + ", " + str(self.height) + ")")
# Class: GOval
class GOval(GFillableObject):
"""
This graphical object subclass represents an oval inscribed in
a rectangular box.
"""
# Constructor: GOval
def __init__(self, a1, a2, a3=None, a4=None):
"""
The <code>GOval</code> constructor takes either of the following
forms:
<pre>
GOval(width, height)
GOval(x, y, width, height)
</pre>
If the <code>x</code> and <code>y</code> parameters are missing,
the origin is set to (0, 0).
"""
GFillableObject.__init__(self)
if a3 is None:
x = 0
y = 0
width = a1
height = a2
else:
x = a1
y = a2
width = a3
height = a4
self.width = width
self.height = height
self.setLocation(x, y)
# Public method: setSize
def setSize(self, width, height=None):
"""
Changes the size of this oval as specified.
"""
if type(width) is GDimension:
width, height = width.getWidth(), width.getHeight()
self.width = width
self.height = height
gw = self.getWindow()
if gw is None: return
tkc = gw.canvas
coords = tkc.coords(self.tkid)
tkc.coords(self.tkid, coords[0], coords[1],
coords[0] + width, coords[1] + height)
# Public method: setBounds
def setBounds(self, x, y=None, width=None, height=None):
"""
Changes the bounds of this rectangle to the specified values.
"""
if type(x) is GRectangle:
width, height = x.getWidth(), x.getHeight()
x, y = x.getX(), x.getY()
self.setLocation(x, y)
self.setSize(width, height)
# Override method: getBounds
def getBounds(self):
"""
Returns the bounds of this <code>GOval</code>.
"""
return GRectangle(self.x, self.y, self.width, self.height)
# Override method: contains
def contains(self, x, y):
rx = self.width / 2
ry = self.height / 2
tx = x - (self.x + rx)
ty = y - (self.y + ry)
return (tx * tx) / (rx * rx) + (ty * ty) / (ry * ry) <= 1.0
# Override method: getType
def getType(self):
"""
Returns the type of this object.
"""
return "GOval"
# Override method: _install
def _install(self, target, ctm):
"""
Installs the <code>GOval</code> in the canvas.
"""
gw = target
tkc = gw.canvas
pt = ctm.transform(GPoint(self.x, self.y))
x = pt.getX()
y = pt.getY()
self.tkid = tkc.create_oval(x, y, x + self.width, y + self.height)
self.updateColor()
# Override method: __str__
def __str__(self):
return ("GOval(" + str(self.x) + ", " + str(self.y) + ", " +
str(self.width) + ", " + str(self.height) + ")")
# Class: GCompound
class GCompound(GObject):
"""
This graphical object subclass consists of a collection of other
graphical objects. Once assembled, the internal objects can be
manipulated as a unit. The <code>GCompound</code> keeps track
of its own position, and all items within it are drawn relative
to that location.
"""
# Constructor: GCompound
def __init__(self):
"""
Creates a <code>GCompound</code> with no internal components.
"""
GObject.__init__(self)
self.contents = [ ]
# Public method: add
def add(self, gobj, x=None, y=None):
"""
Adds a new graphical object to the <code>GCompound</code>. The
first parameter is the object to be added. The <code>x</code>
and <code>y</code> parameters are optional. If they are supplied,
the location of the object is set to (<code>x</code>, <code>y</code>).
"""
if x is not None:
gobj.setLocation(x, y)
self.contents.append(gobj)
gobj.parent = self
if self.gw is None:
gw = self.getWindow()
if gw is not None:
gw._rebuild()
else:
gobj._install(self.gw, _SimpleTransform())
# Public method: remove
def remove(self, gobj):
"""
Removes the specified object from the <code>GCompound</code>.
"""
index = self.findGObject(gobj)
if index != -1: self.removeAt(index)
gw = self.getWindow()
if gw is not None:
gw._rebuild()
# Public method: removeAll
def removeAll(self):
"""
Removes all graphical objects from the <code>GCompound</code>.
"""
while (len(self.contents) > 0):
self.removeAt(0)
gw = self.getWindow()
if gw is not None:
gw._rebuild()
# Public method: getElementAt
def getElementAt(self, x, y):
"""
Returns the topmost <code>GObject</code> containing the
point (x, y), or <code>None</code> if no such object exists.
Coordinates are interpreted relative to the reference point.
"""
for gobj in reversed(self.contents):
if gobj.contains(x, y):
return gobj
return None
# Public method: getElementCount
def getElementCount(self):
"""
Returns the number of graphical objects stored in the
<code>GCompound</code>.
"""
return len(self.contents)
# Public method: getElement
def getElement(self, index):
"""
Returns the graphical object at the specified index, numbering
from back to front in the the <i>z</i> dimension.
"""
return self.contents[index]
# Override method: getBounds
def getBounds(self):
"""
Returns a bounding rectangle for this compound.
"""
xMin = sys.float_info.max
yMin = sys.float_info.max
xMax = sys.float_info.min
yMax = sys.float_info.min
x0 = self.getX()
y0 = self.getY()
for gobj in self.contents:
bounds = gobj.getBounds()
xMin = min(xMin, x0 + bounds.getX())
yMin = min(yMin, y0 + bounds.getY())
xMax = max(xMax, x0 + bounds.getX())
yMax = max(yMax, y0 + bounds.getY())
xMin = min(xMin, x0 + bounds.getX() + bounds.getWidth())
yMin = min(yMin, y0 + bounds.getY() + bounds.getHeight())
xMax = max(xMax, x0 + bounds.getX() + bounds.getWidth())
yMax = max(yMax, y0 + bounds.getY() + bounds.getHeight())
return GRectangle(xMin, yMin, xMax - xMin, yMax - yMin)
# Public method: contains
def contains(self, x, y):
"""
Returns true if the specified point is inside the object.
"""
refpt = self.getLocation()
tx = x - refpt.getX()
ty = y - refpt.getY()
for gobj in self.contents:
if gobj.contains(tx, ty): return True
return False