-
Notifications
You must be signed in to change notification settings - Fork 0
/
g.py
1504 lines (1111 loc) · 54.8 KB
/
g.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
# <html><body><pre>
# Source Code for g, a lowlevel, deviceindependent, graphics language
# for Python. See http://rlai.cs.ualberta.ca/RLAI/RLtoolkit/g.html for
# <a href="http://rlai.cs.ualberta.ca/RLAI/RLtoolkit/g.html">documentation</a>.
# This package is based on Tkinter (python's version of tk)
import tkinter
import tkinter.colorchooser
import tkinter.filedialog
import sys
# The color chooser currently starts up its own python application and will not appear until
# you click on the bouncing python icon in your dock.
# Last update: Dec 7, 2004
#### OBJECTS
class CSinfo:
"""Coordinate system info for windows, views and devices"""
def __init__(self, width=1, height=1):
"Initialize coordinate system values"
self.csLeft = 0. # Normalized coordinate values
self.csBottom = 0.
self.csRight = 1.
self.csTop = 1.
self.offsetx = self.offsety = 0
self.scalex = self.scaley = 1.0
self.x = self.y = 0 # Device coordinate values
self.wwidth = width
self.wheight = height
class Gview(CSinfo, tkinter.Canvas): # <a name="Gview"></a>[<a href="g.html#Gview">Doc</a>]
"Main graphical view class"
parent = None
childviews = []
def __init__(self, parent, vwidth=10, vheight=10,
**kwargs): # <a name="Gview...)"</a>[<a href="g.html#Gview...)">Doc</a>]
"Initialization for Gview"
global GDEVICE
tkinter.Canvas.__init__(self, parent, borderwidth=0, highlightthickness=0, \
width=vwidth, height=vheight)
CSinfo.__init__(self, vwidth, vheight)
self.color = None
self.backcolor = None
self.gSetParent(parent)
self.place(in_=self.parent, anchor=tkinter.NW)
gUpdateNormalization(self)
# Now set up to handle events
self.bind("<Button-1>", self.viewClickEventHandler)
self.bind("<ButtonRelease-1>", self.windowMouseUpEventHandler)
self.bind("<Key>", self.viewKeyEventHandler)
self.bind("<Control-Key>", self.viewControlKeyEventHandler)
self.bind("<Motion>", self.viewMotionEventHandler)
def gGetChildren(self): # <a name="gGetChildren"</a>[<a href="g.html#gGetChildren">Doc</a>]
"""get the child views of this view"""
return self.childviews
def gGetParent(self):
"""get the parent of this view"""
return self.parent
def gSetParent(self, newparent):
"""Set the parent for this view. Things are slightly different if the parent is
a Gwindow"""
parent = self.gGetParent()
if parent != None and parent != newparent and not isinstance(newparent, tkinter.Toplevel):
self.parent.childviews.remove(self)
self.parent = newparent
if newparent != GDEVICE and not isinstance(self, Gwindow) and not isinstance(newparent, tkinter.Toplevel):
newparent.childviews = newparent.childviews + [self]
def setViewContainer(self):
"""check that parent is big enough to hold all its childviews; if not, resize it"""
curxmax = self.wwidth
curymax = self.wheight
for c in self.gGetChildren():
x, y, xmax, ymax = gdGetViewport(c)
if xmax > curxmax:
curxmax = xmax
if ymax > curymax:
curymax = ymax
if curymax > self.wheight or curxmax > self.wwidth:
self.setViewSize(curxmax, curymax)
def setViewSize(self, h, v=None, changegeom=True):
"""update size and normalization attributes for view. changegeom flag is used for the
Gwindow method only but is included here so that the methods have the same calling
sequence"""
if isinstance(h, (list, tuple)):
v = h[1]
h = h[0]
self.wwidth = h
self.wheight = v
self.config(width=h, height=v)
self.gAcceptNewViewportSize()
gUpdateNormalization(self)
def setViewPosition(self, xpos, ypos=None):
"""Update attributes for new position"""
if isinstance(xpos, (list, tuple)):
ypos = xpos[1]
xpos = xpos[0]
if xpos != None and ypos != None:
self.place(x=xpos, y=ypos)
self.x = xpos
self.y = ypos
self.gAcceptNewViewportPosition()
def gAcceptNewViewportSize(
self): # <a name="gAcceptNewViewportSize"</a>[<a href="g.html#gAcceptNewViewportSize">Doc</a>]
"""Notice and handle the new viewport size. If there are subviews, check to make
sure they all fit"""
if not isinstance(self, Gwindow) and self.parent != GDEVICE:
self.parent.setViewContainer()
def gAcceptNewViewportPosition(self):
"""Notice and handle new viewport position"""
pass
### EVENTS
"""
def viewDrawContents(self):
"Draw the contents of this view"
self.gDrawView()
"""
def gDrawView(self): # <a name="gDrawView"</a>[<a href="g.html#gDrawView">Doc</a>]
"""This is the default for drawing a view. If there are subviews, draw them all"""
for child in self.gGetChildren():
child.gDrawView()
# To respond to a Mouse click on a view you specialize a method
# gdClickEventHandler(self, dx, dy)
# or gClickEventHandler(self, x, y)
# which will be called each time there is a Mouse click in that view.
def viewClickEventHandler(self, event):
"Determines the coordinates of the click event and calls the appropriate routines"
self.focus_set()
dx = self.canvasx(event.x)
dy = self.canvasy(event.y)
self.gdClickEventHandler(dx, dy)
self.gClickEventHandler(gCoordx(self, dx), gCoordy(self, dy))
def gdClickEventHandler(self, dx,
dy): # <a name="gdClickEventHandler"</a>[<a href="g.html#gdClickEventHandler">Doc</a>]
"Specialize this method (or gClickEventHandler) to handle mouse clicks for your application"
pass
def gClickEventHandler(self, x, y): # <a name="gClickEventHandler"</a>[<a href="g.html#gClickEventHandler">Doc</a>]
"Specialize this method (or gdClickEventHandler) to handle mouse clicks for your application"
pass
# To respond to mouse movement on a view you specialize a method
# gdMotionEventHandler(self, dx, dy)
# or gMotionEventHandler(self, x, y)
# which will be called at intervals along the mouse path
def viewMotionEventHandler(self, event):
"Determines the coordinates of the motion and calls the appropriate routines"
self.focus_set()
dx = self.canvasx(event.x)
dy = self.canvasy(event.y)
self.gdMotionEventHandler(dx, dy)
self.gMotionEventHandler(gCoordx(self, dx), gCoordy(self, dy))
def gdMotionEventHandler(self, dx,
dy): # <a name="gdClickEventHandler"</a>[<a href="g.html#gdClickEventHandler">Doc</a>]
"Specialize this routine (or gMotionEventHandler) to handle mouse motion for your application"
pass
def gMotionEventHandler(self, x,
y): # <a name="gClickEventHandler"</a>[<a href="g.html#gClickEventHandler">Doc</a>]
"Specialize this routine (or gdMotionEventHandler) to handle mouse motion for your application"
pass
# To respond to a mouse button being released on a view you specialize a method
# gdMouseUpEventHandler(self, dx, dy)
# or gMouseUpEventHandler(self, x, y)
# which will be called when the mouse button is released.
def windowMouseUpEventHandler(self, event):
"Determines the coordinates of the mouse up event and calls the appropriate routines"
self.focus_set()
dx = self.canvasx(event.x)
dy = self.canvasy(event.y)
self.gdMouseUpEventHandler(dx, dy)
self.gMouseUpEventHandler(gCoordx(self, dx), gCoordy(self, dy))
def gdMouseUpEventHandler(self, dx, dy):
"Specialize this routine (or gMouseUpEventHandler) to handle mouse button releases for your application"
pass
def gMouseUpEventHandler(self, x, y):
"Specialize this routine (or gdMouseUpEventHandler) to handle mouse button releases for your application"
pass
# To respond to a mouse button being released on a view you specialize a method
# gKeyEventHandler(self, key)
# which will be called whenever a key is pressed.
def viewKeyEventHandler(self, event):
"Determines what key was pressed and calls the key event handler"
self.focus_set()
self.gKeyEventHandler(event.keysym)
def viewControlKeyEventHandler(self, event):
"Determines what key was pressed and calls the key event handler"
self.focus_set()
self.gKeyEventHandler(event.keysym)
def gKeyEventHandler(self, key):
"Specialize this routine to handle key presses for your application"
# Note- not handling alt or ctl key combos
pass
def gCursor(self, cursorname): # <a name="gCursor"</a>[<a href="g.html#gCursor">Doc</a>]
"Sets the cursor for the view"
self.config(cursor=cursorname)
def gCloseView(self):
"Close the Window and let the parent know this view is no longer in use"
self.parent.childviews.remove(self)
self.destroy()
gViewport = 'gViewport'
gdViewport = 'gdViewport'
gViewportR = 'gViewportR'
gdViewportR = 'gdViewportR'
windowTitle = 'windowTitle'
resizable = 'resizable'
class Gwindow(Gview):
"""Window for g drawing. Because we can't have an object that is both a tk canvas and
a tk toplevel at the same time, we let the window be the canvas object so that it can
be drawn on, and have it point to a parent which is a toplevel object so that we get a window"""
def __init__(self, **kwargs): # <a name="Gwindow...)"</a>[<a href="g.html#Gwindow...)">Doc</a>]
"Initialization for Gwindow"
global GDEVICE
new = tkinter.Toplevel(GDEVICE) # make a Toplevel window, and put a canvas(Gview) under it
self.screenx = 25 # default placement for Toplevel window on screen device
self.screeny = 65
self.menu = None
GDEVICE.childwindows.append(self) # keep track of this guy in main device
new.lift() # try to make this window come out on top of others
new.tkraise()
new.config(takefocus=True)
new.config(width=200, height=200) # tk default for new windows
Gview.__init__(self, new, 200, 200, **kwargs)
self.title = kwargs.get(windowTitle) # Check for window attributes
if self.title == None: # window title
self.title = 'Gwindow'
if self.title != None: # make the title show up on the Toplevel
new.title(self.title)
resize = kwargs.get(resizable) # resizable attribute
if resize == False:
new.resizable(0, 0)
gvp = kwargs.get(gViewport) # Now check for viewports
if gvp != None:
gSetViewport(self, gvp[0], gvp[1], gvp[2], gvp[3])
gdvp = kwargs.get(gdViewport)
if gdvp != None:
gdSetViewport(self, gdvp[0], gdvp[1], gdvp[2], gdvp[3])
gvpr = kwargs.get(gViewportR)
if gvpr != None:
gSetViewportR(self, gvpr[0], gvpr[1], gvpr[2], gvpr[3])
gdvpr = kwargs.get(gdViewportR)
if gdvpr != None:
gdSetViewportR(self, gdvpr[0], gdvpr[1], gdvpr[2], gdvpr[3])
self.place(in_=self.parent, anchor=tkinter.NW, relheight=1, relwidth=1, relx=0, rely=0)
gUpdateNormalization(self)
self.bind("<Configure>", self.gResize) # set up to catch certain events
self.bind("<Destroy>", self.gDestroy)
def setViewSize(self, h, v=None, changegeom=True):
"Change the size of the window to new values"
if v == None:
v = h
Gview.setViewSize(self, h, v, changegeom)
if changegeom: # window needs to be resized, not just new values saved
parent = self.parent
parent.config(width=h, height=v)
geom = str(self.wwidth) + 'x' + str(self.wheight) + '+' + str(self.screenx) + '+' + str(self.screeny)
parent.geometry(geom)
def setViewPosition(self, x, y):
"Change the position of the window"
self.screenx = x
self.screeny = y
self.x = x
self.y = y
self.gAcceptNewViewportPosition()
def gGetParent(self):
global GDEVICE
return GDEVICE
def gAcceptNewViewportSize(
self): # <a name="gAcceptNewViewportSize"</a>[<a href="g.html#gAcceptNewViewportSize">Doc</a>]
"Handle window changing size"
pass
def gAcceptNewViewportPosition(
self): # <a name="gAcceptNewViewportPosition"</a>[<a href="g.html#gAcceptNewViewportPosition">Doc</a>]
"Handle window moving"
pass
def gCloseView(self): # <a name="gCloseView"</a>[<a href="g.html#gCloseView">Doc</a>]
"Close the window"
self.gDestroy(None)
def gDestroy(self, event):
"""Called when window closed by any means, as well as by gCloseView. Removes the
window from the screen and from the list of current windows"""
global GDEVICE
if self in GDEVICE.childwindows: # remove this window from the window list
GDEVICE.childwindows.remove(self)
self.parent.destroy() # destroy the toplevel object
# if this is the only window, should we Quit? Let applications decide this.
def gResize(self, event):
"This routine automatically resizes windows as long as you have an appropriate gDrawView routine"
width = event.width
height = event.height
if width != self.wwidth or height != self.wheight:
self.setViewSize(width, height, False)
self.gDrawView() # redraw window contents
class Gdevice(CSinfo, tkinter.Tk): # <a name="Gdevice"</a>[<a href="g.html#Gdevice">Doc</a>]
"""Object for a device, such as a screen or printer"""
def __init__(self):
"Initialization for Gdevice"
tkinter.Tk.__init__(self)
CSinfo.__init__(self, self.winfo_screenwidth(), self.winfo_screenheight())
self.lower() # don't want it visible
self.withdraw()
self.childwindows = []
gUpdateNormalization(self)
### Viewports
def viewSize(view):
"returns the size of the view, a tuple of width and height"
return view.wwidth, view.wheight
def viewPosition(view):
"returns the position of the view, a tuple of x and y values"
return view.x, view.y
def gdGetViewport(view): # <a name="gdGetViewport"</a>[<a href="g.html#gdGetViewport">Doc</a>]
"returns the current device viewport values for view as a tuple (x1, y1, x2, y2)"
x1, y1 = viewPosition(view)
w, h = viewSize(view)
return x1, y1, x1 + w - 1, y1 + h - 1
def gGetViewport(view): # <a name="gGetViewport"</a>[<a href="g.html#gGetViewport">Doc</a>]
"returns the normalized values for the viewport for the view as a tuple (x1, y1, x2, y2)"
parent = view.gGetParent()
dx1, dy1, dx2, dy2 = gdGetViewport(view)
x1, y1 = gCoords(parent, dx1, dy1)
x2, y2 = gCoords(parent, dx2, dy2)
return min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
def gdGetViewportR(view): # <a name="gdGetViewportR"</a>[<a href="g.html#gdGetViewportR">Doc</a>]
"Returns the device viewport values for the view as a tuple (x, y, width, height)"
x1, y1 = viewPosition(view)
w, h = viewSize(view)
return x1, y1, w, h
def gGetViewportR(view): # <a name="gGetViewportR"</a>[<a href="g.html#gGetViewportR">Doc</a>]
"Returns the normalized viewport values for the view as a tuple (x, y, width, ehgith)"
parent = view.gGetParent()
dx1, dy1, w, h = gdGetViewportR(view)
x1, y1 = gCoords(parent, dx1, dy1)
w2, h2 = gOffset(parent, w, h)
return x1, y1, abs(w2), abs(h2)
def gdSetViewport(view, dx1, dy1, dx2, dy2): # <a name="gdSetViewport"</a>[<a href="g.html#gdSetViewport">Doc</a>]
"Sets the device viewport values for a view to the given values"
posx = min(dx1, dx2)
posy = min(dy1, dy2)
width = 1 + abs(dx1 - dx2)
height = 1 + abs(dy1 - dy2)
view.setViewPosition(posx, posy)
if view.gGetChildren() != []: # check to see if it can handle its children
view.setViewContainer()
width = max(width, view.wwidth)
height = max(height, view.wheight)
view.setViewSize(width, height)
def gSetViewport(view, vpx1, vpy1, vpx2, vpy2): # <a name="gSetViewport"</a>[<a href="g.html#gSetViewport">Doc</a>]
"Sets the normal viewport values for a view to the given values"
if isinstance(view, Gwindow):
parent = GDEVICE
else:
parent = view.gGetParent()
gdSetViewport(view, \
gdCoordx(parent, vpx1), \
gdCoordy(parent, vpy1), \
vpx2 and gdCoordx(parent, vpx2), \
vpy2 and gdCoordy(parent, vpy2))
# <a name="gdSetViewportR"</a>[<a href="g.html#gdSetViewportR">Doc</a>]
def gdSetViewportR(view, dx, dy, deltax=None, deltay=None):
"Sets the device viewport values for a view to the given values"
if dx == None:
dx = gdGetViewport(view)[0]
if dy == None:
dy = gdGetViewport(view)[1]
if deltax == None:
deltax = gdGetViewportR(view)[2]
if deltay == None:
deltay = gdGetViewportR(view)[3]
gdSetViewport(view, dx, dy, dx + deltax - 1, dy + deltay - 1)
def gSetViewportR(view, x, y, deltax=None,
deltay=None): # <a name="gSetViewportR"</a>[<a href="g.html#gSetViewportR">Doc</a>]
"Sets the normal viewport values for a view to the given values"
oldv = gGetViewport(view)
if x == None:
x = oldv[0]
if y == None:
y = oldv[1]
if deltax == None:
deltax = oldv[2]
if deltay == None:
deltay = oldv[3]
if isinstance(view, Gwindow):
parent = GDEVICE
else:
parent = view.gGetParent()
gdSetViewportR(view, \
gdCoordx(parent, x), \
gdCoordy(parent, y), \
gdOffsetx(parent, deltax), \
gdOffsety(parent, deltay))
# gSetViewport(view, x, y, x + deltax, y + deltay)
### Coordinate Systems
def gGetCoordinateSystem(view): # <a name="gGetCoordinateSystem"</a>[<a href="g.html#gGetCoordinateSystem">Doc</a>]
"""Returns the normal values associated with this view as a tuple
(x1, y1, x2, y2, corner)
where x1 and x2 are the left and right values
and y1 and y2 are the top and bottom values
and corner specifies which corner values are relative to"""
if view.csLeft <= view.csRight:
if view.csBottom <= view.csTop:
corner = 'lowerLeft'
else:
corner = 'upperLeft'
elif view.csBottom <= view.csTop:
corner = 'lowerRight'
else:
corner = 'upperRight'
return min(view.csLeft, view.csRight), min(view.csTop, view.csBottom), \
max(view.csRight, view.csLeft), max(view.csTop, view.csBottom), \
corner
gGetCS = gGetCoordinateSystem
def gdGetCoordinateSystem(
view): # <a name="gdGetCoordinateSystem"</a>[<a href="g.html#gdGetCoordinateSystem">Doc</a>] #
"""Returns the device values associated with this view as a tuple
(left, top, right, bottom, 'upperleft') - the corner is always upperleft for device coords"""
x1, y1 = 0, 0 # viewPosition(view)
w, h = viewSize(view)
if w == 1 or h == 1:
return x1, y1, x1 + w, y1 + h, 'upperLeft'
else:
return x1, y1, x1 + w - 1, y1 + h - 1, 'upperLeft'
gdGetCS = gdGetCoordinateSystem
# <a name="gGetCoordinateSystemR"</a>[<a href="g.html#gGetCoordinateSystemR">Doc</a>]
def gGetCoordinateSystemR(view):
"""Returns the relative normal values associated with this view as a tuple
(x, y, width, height, corner)
where x and y are the coordinates of the corner
and width and height are the relative width and height of the view
and corner specifies which corner values are relative to"""
x1, y1, x2, y2, corner = gGetCoordinateSystem(view)
return x1, y1, x2 - x1, y2 - y1, corner
gGetCSR = gGetCoordinateSystemR
# <a name="gdGetCoordinateSystemR"</a>[<a href="g.html#gdGetCoordinateSystemR">Doc</a>]
def gdGetCoordinateSystemR(view):
"""Returns the relative device values associated with this view as a tuple
(left, top, width, height, 'upperleft') - the corner is always upperleft for device coords"""
x1, y1, x2, y2, corner = gdGetCoordinateSystem(view)
return x1, y1, x2 - x1 + 1, y2 - y1 + 1, corner
gdGetCSR = gdGetCoordinateSystemR
# <a name="gGetCoordinateSystemScale"</a>[<a href="g.html#gGetCoordinateSystemScale">Doc</a>]
def gGetCoordinateSystemScale(view):
"Returns the first coordinates (corner), the scaling values, and the corner location"
x1, y1, x2, y2, corner = gGetCoordinateSystem(view) # return offset??
return x1, y1, view.scalex, view.scaley, corner
# <a name="gGetCSScale"</a>[<a href="g.html#gGetCSScale">Doc</a>]
gGetCSScale = gGetCoordinateSystemScale
# <a name="gSetCoordinateSystem"</a>[<a href="g.html#gSetCoordinateSystem">Doc</a>]
def gSetCoordinateSystem(view, x1, y1, x2, y2, corner='lowerLeft'):
"""Sets the coordinate system for a view to the one specified"""
x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
if x1 == x2:
print("Attempt to Set left and right of g Coordinate System to same values.")
x2 = x1 + 1
elif y1 == y2:
print("Attempt to Set top and bottom of g Coordinate System to same values.")
y2 = y1 + 1
else:
if corner in ['lowerLeft', 'upperLeft']:
view.csLeft = x1
view.csRight = x2
else:
view.csLeft = x2
view.csRight = x1
if corner in ['lowerLeft', 'lowerRight']:
view.csBottom = y1
view.csTop = y2
else:
view.csBottom = y2
view.csTop = y1
gUpdateNormalization(view)
gSetCS = gSetCoordinateSystem
# <a name="gSetCoordinateSystemR"</a>[<a href="g.html#gSetCoordinateSystemR">Doc</a>]
def gSetCoordinateSystemR(view, x, y, deltax, deltay, corner='lowerLeft'):
"""Sets the coordinate system for a view using relative values"""
gSetCoordinateSystem(view, x, y, x + deltax, y + deltay, corner)
gSetCSR = gSetCoordinateSystemR
Scales = {'inches': 72, 'centimeters': 28.35, 'pixels': 1, 'points': 1}
# <a name="gSetCoordinateSystemScale"</a>[<a href="g.html#gSetCoordinateSystemScale">Doc</a>]
def gSetCoordinateSystemScale(view, x, y, xScale, yScale=None, corner='lowerLeft'):
"""Sets the coordinate system scale for a view to the given values"""
if yScale == None:
yScale = xScale
if not isinstance(xScale, (int, float)):
xScale = _Scales.get(xScale, 1)
if not isinstance(yScale, (int, float)):
yScale = _Scales.get(yScale, xScale)
dx1, dy1, dx2, dy2 = gdGetViewport(view)
x2 = x + (abs(dx1 - dx2) / float(xScale))
y2 = y + (abs(dy1 - dy2) / float(yScale))
gSetCoordinateSystem(view, x, y, x2, y2, corner)
# <a name="gSetCSScale"</a>[<a href="g.html#gSetCSScale">Doc</a>]
gSetCSScale = gSetCoordinateSystemScale
def gUpdateNormalization(view):
"Updates state variables of Normalized Coordinate System"
x1, y1, x2, y2, corner = gdGetCoordinateSystem(view)
if view.csRight == view.csLeft:
print("Error - Attempt to establish invalid (zero area) g Coordinate System")
else:
view.scalex = float(x2 - x1) / float(view.csRight - view.csLeft)
view.offsetx = x1 - (view.csLeft * view.scalex)
if view.csBottom == view.csTop:
print("Error - Attempt to establish invalid (zero area) g Coordinate System")
else:
view.scaley = float(y2 - y1) / float(view.csBottom - view.csTop)
view.offsety = y1 - (view.csTop * view.scaley)
### Converting from normal to device coordinates
def gdCoordx(view, x): # <a name="gdCoordx"</a>[<a href="g.html#gdCoordx">Doc</a>]
return int(round(view.offsetx + (x * view.scalex)))
def gdCoordy(view, y): # <a name="gdCoordy"</a>[<a href="g.html#gdCoordy">Doc</a>]
return int(round(view.offsety + (y * view.scaley)))
def gdCoords(view, x, y):
return gdCoordx(view, x), gdCoordy(view, y)
# <a name="gdOffsetx"</a>[<a href="g.html#gdOffsetx">Doc</a>]
def gdOffsetx(view, xoffset):
"Returns the length in device coords (pixels) of the xdistance in Normal coords"
return int(round(xoffset * view.scalex))
# <a name="gdOffsety"</a>[<a href="g.html#gdOffsety">Doc</a>]
def gdOffsety(view, yoffset):
"Returns the length in device coords (pixels) of the ydistance in Normal coords"
return int(round(yoffset * view.scaley))
def gdOffset(view, xoffset, yoffset):
return gdOffsetx(view, xoffset), gdOffsety(view, yoffset)
### Converting from device to Normal Coordinates
def gCoordx(view, dx): # <a name="gCoordx"</a>[<a href="g.html#gCoordx">Doc</a>]
return float(dx - view.offsetx) / view.scalex
def gCoordy(view, dy): # <a name="gCoordy"</a>[<a href="g.html#gCoordy">Doc</a>]
return float(dy - view.offsety) / view.scaley
def gCoords(view, dx, dy):
return gCoordx(view, dx), gCoordy(view, dy)
def gOffsetx(view, dxoffset): # <a name="gOffsetx"</a>[<a href="g.html#gOffsetx">Doc</a>]
return float(dxoffset) / view.scalex
def gOffsety(view, dyoffset): # <a name="gOffsety"</a>[<a href="g.html#gOffsety">Doc</a>]
return float(dyoffset) / view.scaley
def gOffset(view, dxoffset, dyoffset):
return gOffsetx(view, dxoffset), gOffsety(view, dyoffset)
### Converting Coordinates between Views:
# <a name="gdConvertx"</a>[<a href="g.html#gdConvertx">Doc</a>]
def gdConvertx(fromView, toView, dx):
# convert to parent window view, then to other view - views must be in same window
return dx
# <a name="gdConverty"</a>[<a href="g.html#gdConverty">Doc</a>]
def gdConverty(fromView, toView, dy):
return dy
# <a name="gConvertx"</a>[<a href="g.html#gConvertx">Doc</a>]
def gConvertx(fromView, toView, x):
n = gCoordx(toView, gdConvertx(fromView, toView, gdCoordx(fromView, x)))
return n
# <a name="gConverty"</a>[<a href="g.html#gConverty">Doc</a>]
def gConverty(fromView, toView, y):
n = gCoordy(toView, gdConverty(fromView, toView, gdCoordy(fromView, y)))
return n
## Color Routines
# <a name="gColorPen"</a>[<a href="g.html#gColorPen">Doc</a>]
def gColorPen(view, color='black', pattern=None, mode=None, xsize=None, ysize=None):
"""Returns a new color With specified pen characteristics
pattern may be 'gray12', 'gray25', 'gray50', 'gray75', or '' although it doesn't seem
to be used on the mac
mode currently is ignored"""
if ysize == None:
ysize = xsize
usecolor, pat, md, (xs, ys) = useColor(color)
if pattern != None: # update if necessary
pat = pattern
if mode != None:
md = mode
if xsize != None:
xs = xsize
if ysize != None:
ys = ysize
return usecolor, pat, md, (xs, ys)
def gFont(fontname="Times", fontsize=12, fontface='normal'):
""" Returns a font specification to use with the draw text routines
fontname must be a string, fontsize and integer
fontface is also a string normal, bold or italic; for bold and italic,
make fontface "bold italic"
"""
return fontname, fontsize, fontface
def getColor(view, colorcode=None):
"gets color to use for current drawing"
if colorcode == None: # use view default if there is one, or regular default
if view.color == None:
return useColor()
else:
return useColor(view.color)
else: # use new colorcode
return useColor(colorcode)
def useColor(colorcode='black', pattern="", mode='copy', xsize=1, ysize=None):
"separates color into color, pattern, mode, and sizes - pattern and mode ignored currently"
if isinstance(colorcode, (tuple, list)): # pen list
color = colorcode[0]
pattern = colorcode[1]
mode = colorcode[2]
xsize, ysize = colorcode[3]
else: # integer or string color
color = colorcode
if ysize == None:
ysize = xsize
return color, pattern, mode, (xsize, ysize)
def gColorSize(view, color, xsize, ysize=None):
"sets the color and size components of the pen for drawing"
return gColorPen(view, color, None, None, (xsize, ysize))
def gColorPenInvisible(view):
"returns the invisible pen"
return gColorInvisible(view)
def gColorInvisible(view):
"returns the off color to blend in with the default background"
if view.backcolor != None:
return view.backcolor
else:
return gColorOff(view)
def gColorPenFlip(view):
"returns the inverted color pen for the view on color"
return gColorFlip(view)
def gColorFlip(view):
"""returns the inverted color for the view on color. Previous versions used a mode of XOR to do this
This version returns the off color"""
return gColorOff(view)
def gColorRGB(view, red, green, blue): # <a name="gColorRGB"</a>[<a href="g.html#gColorRGB">Doc</a>]
"Returns a color - red, green and blue span from 0.0 to 1.0"
red = min(1.0, max(0.0, red)) # bound the values between 0.0 and 1.0
green = min(1.0, max(0.0, green))
blue = min(1.0, max(0.0, blue))
red = int(255 * red) # Now get the corresponding 256 color number
green = int(255 * green)
blue = int(255 * blue)
return "#%02x%02x%02x" % (red, green, blue) # format colors into something tk likes
def gColorRGB255(view, red, green, blue): # <a name="gColorRGB255"</a>[<a href="g.html#gColorRGB255">Doc</a>]
"Returns a color - red, green and blue go from 0 to 255"
red = int(min(255, max(0, red))) # bound the values between 0 and 255, make sure integer
green = int(min(255, max(0, green)))
blue = int(min(255, max(0, blue)))
return "#%02x%02x%02x" % (red, green, blue) # format colors into something tk likes
# <a name="Selecting Colors by Name"</a>[<a href="g.html#Selecting Colors by Name">Doc</a>]
def gColorBlack(view): return 'black'
def gColorWhite(view): return 'white'
def gColorPink(view): return 'pink'
def gColorRed(view): return 'red'
def gColorOrange(view): return 'orange'
def gColorYellow(view): return 'yellow'
def gColorGreen(view): return 'green'
def gColorDarkGreen(view): return 'dark green'
def gColorLightBlue(view): return 'light blue'
def gColorBlue(view): return 'blue'
def gColorPurple(view): return 'purple'
def gColorBrown(view): return 'brown'
def gColorTan(view): return 'tan'
def gColorLightGray(view): return 'light gray'
def gColorGray(view): return 'gray'
def gColorDarkGray(view): return 'dark gray'
def gColorCyan(view): return 'cyan'
def gColorMagenta(view): return 'magenta'
def gColorOn(view):
if view not in [None, True, False] and view.color != None:
return view.color
else:
return 'black'
def gColorOff(view):
if view not in [None, True, False] and view.color != None:
return view.color
else:
if sys.platform in ['mac', 'darwin']:
return 'white'
else:
return gColorRGB255(True, 204, 204, 204) # light grey
gBlack = gColorBlack(None)
gWhite = gColorWhite(None)
gPink = gColorPink(None)
gRed = gColorRed(None)
gOrange = gColorOrange(None)
gYellow = gColorYellow(None)
gGreen = gColorGreen(None)
gDarkGreen = gColorDarkGreen(None)
gLightBlue = gColorLightBlue(None)
gBlue = gColorBlue(None)
gPurple = gColorPurple(None)
gBrown = gColorBrown(None)
gTan = gColorTan(None)
gLightGray = gColorLightGray(None)
gGray = gColorGray(None)
gDarkGray = gColorDarkGray(None)
gCyan = gColorCyan(None)
gMagenta = gColorMagenta(None)
gOn = gColorOn(None) # default on color
gOff = gColorOff(None) # default off color
def gColorBW(view, intensity): # <a name="gColorBW"</a>[<a href="g.html#gColorBW">Doc</a>]
"returns a shade of grey corresponding to intensity"
intensity = (1 - intensity) # white is most intense for tk, but not for people
return gColorRGB(view, intensity, intensity, intensity)
def gColorUserPick(view, **args): # <a name="gColorUserPick"</a>[<a href="g.html#gColorUserPick">Doc</a>]
"call up tk's color picker - currently doesn't come out until you click the bouncing python icon in the dock"
# return tkColorChooser.askcolor(parent=view, **args)[1]
b = tkinter.colorchooser.Chooser(view)
return b.show()[1]
def gSetColor(view, color): # <a name="gSetColor"</a>[<a href="g.html#gSetColor">Doc</a>]
"set on color for a view"
if color != None:
view.color = color
# File opening and saving using tk open and save dialogs
def gOpenFileUserPick(view, **args): # <a name="gOpenFileUserPick"</a>[<a href="g.html#gOpenFileUserPick">Doc</a>]
"call up tk's file open dialog - args are title=, initialdir=, and initialfile="
global GDEVICE
if view == None:
view = GDEVICE
return tkinter.filedialog.Open(view).show(**args)
def gSaveFileUserPick(view, **args): # <a name="gSaveFileUserPick"</a>[<a href="g.html#gSaveFileUserPick">Doc</a>]
"call up tk's file save dialog - args are title=, initialdir=, and initialfile="
global GDEVICE
if view == None:
view = GDEVICE
return tkinter.filedialog.SaveAs(view).show(**args)
### gdGRAPHICS
def canvasPoint(view, x1, y1):
return x1, y1
def gdDrawPoint(view, dx, dy, colorcode=None): # <a name="gdDrawPoint"</a>[<a href="g.html#gdDrawPoint">Doc</a>]
"Draws a point on view, using device coordinates"
dx, dy = canvasPoint(view, dx, dy)
color, pattern, mode, (xsize, ysize) = getColor(view, colorcode)
# return view.create_line(dx, dy, dx+1, dy, fill=color, width=xsize)
return view.create_oval(dx, dy, dx + xsize, dy + ysize, width=0, outline=color, fill=color)
# <a name="gdDrawLine"</a>[<a href="g.html#gdDrawLine">Doc</a>]
def gdDrawLine(view, dx1, dy1, dx2, dy2, colorcode=None):
"Draws a line on view, using device coordinates"
dx1, dy1 = canvasPoint(view, dx1, dy1)
dx2, dy2 = canvasPoint(view, dx2, dy2)
color, pattern, mode, (xsize, ysize) = getColor(view, colorcode)
return view.create_line(dx1, dy1, dx2, dy2, fill=color, width=xsize)
# <a name="gdDrawLineR"</a>[<a href="g.html#gdDrawLineR">Doc</a>]
def gdDrawLineR(view, dx, dy, deltax, deltay, colorcode=None):
"""Draws a line on view, using device coordinates for the first point and
the relative distance to determine the second point"""
return gdDrawLine(view, dx, dy, dx + deltax, dy + deltay, colorcode)
# <a name="gdOutlineRect"</a>[<a href="g.html#gdOutlineRect">Doc</a>]
def gdOutlineRect(view, dx1, dy1, dx2, dy2, colorcode=None):
"Draws a rectangle outline on view, using device coordinates"
return gdOutlineRectR(view, dx1, dy1, dx2 - dx1, dy2 - dy1, colorcode)
# <a name="gdOutlineRectR"</a>[<a href="g.html#gdOutlineRectR">Doc</a>]
def gdOutlineRectR(view, dx, dy, deltax, deltay, colorcode=None):
"""Draws a rectangle outline on view, using device coordinates for the first corner and
the relative distance to determine the second corner"""
deltax += dx
deltay += dy
dx, dy = canvasPoint(view, dx, dy)
deltax, deltay = canvasPoint(view, deltax, deltay)
color, pattern, mode, (xsize, ysize) = getColor(view, colorcode)
return view.create_rectangle(dx, dy, deltax + 1, deltay + 1, outline=color, width=xsize)
# <a name="gdFillRect"</a>[<a href="g.html#gdFillRect">Doc</a>]
def gdFillRect(view, dx1, dy1, dx2, dy2, colorcode=None):
"Draws a solid rectangle on view, using device coordinates"
return gdFillRectR(view, dx1, dy1, dx2 - dx1, dy2 - dy1, colorcode)
# <a name="gdFillRectR"</a>[<a href="g.html#gdFillRectR">Doc</a>]
def gdFillRectR(view, dx, dy, deltax, deltay, colorcode=None):
"""Draws a solid rectangle on view, using device coordinates for the first corner and