Repository navigation
Expand file tree
/
Copy pathFCObjects.py
More file actions
2910 lines (2364 loc) · 102 KB
/
Copy pathFCObjects.py
File metadata and controls
2910 lines (2364 loc) · 102 KB
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
#----------------------------------------------------------------------------
# Name: FCObjects.py
# Purpose: Contains all the drawing objects FloatCanvas supports
#
# Author:
#
# Created:
# Version:
# Date:
# Licence:
# Tags: phoenix-port, unittest, documented, py3-port
#----------------------------------------------------------------------------
"""
This is where FloatCanvas defines its drawing objects.
"""
import sys
import wx
import numpy as np
from .Utilities import BBox
from wx.lib.floatcanvas.Utilities import Colors
mac = sys.platform.startswith("darwin")
## A global variable to hold the Pixels per inch that wxWindows thinks is in use
## This is used for scaling fonts.
## This can't be computed on module __init__, because a wx.App might not have initialized yet.
global FontScale
def ComputeFontScale():
"""
Compute the font scale.
A global variable to hold the scaling from pixel size to point size.
"""
global FontScale
dc = wx.ScreenDC()
dc.SetFont(wx.Font(16, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
E = dc.GetTextExtent("X")
FontScale = 16/E[1]
del dc
# why do we do this here, causes a Sphinx build crash
#ComputeFontScale()
## fixme: This should probably be re-factored into a class
_testBitmap = None
def _cycleidxs(indexcount, maxvalue, step):
"""
Utility function used by _colorGenerator
"""
def colormatch(color):
"""Return True if the color comes back from the bitmap identically."""
if len(color) < 3:
return True
global _testBitmap
dc = wx.MemoryDC()
if not _testBitmap:
_testBitmap = wx.Bitmap(1, 1)
dc.SelectObject(_testBitmap)
dc.SetBackground(wx.BLACK_BRUSH)
dc.Clear()
dc.SetPen(wx.Pen(wx.Colour(*color), 4))
dc.DrawPoint(0,0)
if mac: # NOTE: can the Mac not just use the DC?
del dc # Mac can't work with bitmap when selected into a DC.
pdata = wx.AlphaPixelData(_testBitmap)
pacc = pdata.GetPixels()
pacc.MoveTo(pdata, 0, 0)
outcolor = pacc.Get()[:3]
else:
outcolor = dc.GetPixel(0,0)
return outcolor == color
if indexcount == 0:
yield ()
else:
for idx in range(0, maxvalue, step):
for tail in _cycleidxs(indexcount - 1, maxvalue, step):
color = (idx, ) + tail
if not colormatch(color):
continue
yield color
def _colorGenerator():
"""
Generates a series of unique colors used to do hit-tests with the Hit
Test bitmap
"""
return _cycleidxs(indexcount=3, maxvalue=256, step=1)
class DrawObject:
"""
This is the base class for all the objects that can be drawn.
One must subclass from this (and an assortment of Mixins) to create
a new DrawObject, see for example :class:`~lib.floatcanvas.FloatCanvas.Circle`.
"""
#This class contains a series of static dictionaries:
#* BrushList
#* PenList
#* FillStyleList
#* LineStyleList
def __init__(self, InForeground = False, IsVisible = True):
"""
Default class constructor.
:param boolean `InForeground`: Define if object should be in foreground
or not
:param boolean `IsVisible`: Define if object should be visible
"""
self.InForeground = InForeground
self._Canvas = None
self.HitColor = None
self.CallBackFuncs = {}
## these are the defaults
self.HitAble = False
self.HitLine = True
self.HitFill = True
self.MinHitLineWidth = 3
self.HitLineWidth = 3 ## this gets re-set by the subclasses if necessary
self.Brush = None
self.Pen = None
self.FillStyle = "Solid"
self.Visible = IsVisible
# I pre-define all these as class variables to provide an easier
# interface, and perhaps speed things up by caching all the Pens
# and Brushes, although that may not help, as I think wx now
# does that on it's own. Send me a note if you know!
BrushList = {
( None, "Transparent") : wx.TRANSPARENT_BRUSH,
("Blue", "Solid") : wx.BLUE_BRUSH,
("Green", "Solid") : wx.GREEN_BRUSH,
("White", "Solid") : wx.WHITE_BRUSH,
("Black", "Solid") : wx.BLACK_BRUSH,
("Grey", "Solid") : wx.GREY_BRUSH,
("MediumGrey", "Solid") : wx.MEDIUM_GREY_BRUSH,
("LightGrey", "Solid") : wx.LIGHT_GREY_BRUSH,
("Cyan", "Solid") : wx.CYAN_BRUSH,
("Red", "Solid") : wx.RED_BRUSH
}
PenList = {
(None, "Transparent", 1) : wx.TRANSPARENT_PEN,
("Green", "Solid", 1) : wx.GREEN_PEN,
("White", "Solid", 1) : wx.WHITE_PEN,
("Black", "Solid", 1) : wx.BLACK_PEN,
("Grey", "Solid", 1) : wx.GREY_PEN,
("MediumGrey", "Solid", 1) : wx.MEDIUM_GREY_PEN,
("LightGrey", "Solid", 1) : wx.LIGHT_GREY_PEN,
("Cyan", "Solid", 1) : wx.CYAN_PEN,
("Red", "Solid", 1) : wx.RED_PEN
}
FillStyleList = {
"Transparent" : wx.BRUSHSTYLE_TRANSPARENT,
"Solid" : wx.BRUSHSTYLE_SOLID,
"BiDiagonalHatch": wx.BRUSHSTYLE_BDIAGONAL_HATCH,
"CrossDiagHatch" : wx.BRUSHSTYLE_CROSSDIAG_HATCH,
"FDiagonal_Hatch": wx.BRUSHSTYLE_FDIAGONAL_HATCH,
"CrossHatch" : wx.BRUSHSTYLE_CROSS_HATCH,
"HorizontalHatch": wx.BRUSHSTYLE_HORIZONTAL_HATCH,
"VerticalHatch" : wx.BRUSHSTYLE_VERTICAL_HATCH
}
LineStyleList = {
"Solid" : wx.PENSTYLE_SOLID,
"Transparent": wx.PENSTYLE_TRANSPARENT,
"Dot" : wx.PENSTYLE_DOT,
"LongDash" : wx.PENSTYLE_LONG_DASH,
"ShortDash" : wx.PENSTYLE_SHORT_DASH,
"DotDash" : wx.PENSTYLE_DOT_DASH,
}
def Bind(self, Event, CallBackFun):
"""
Bind an event to the DrawObject
:param `Event`: see below for supported event types
- EVT_FC_LEFT_DOWN
- EVT_FC_LEFT_UP
- EVT_FC_LEFT_DCLICK
- EVT_FC_MIDDLE_DOWN
- EVT_FC_MIDDLE_UP
- EVT_FC_MIDDLE_DCLICK
- EVT_FC_RIGHT_DOWN
- EVT_FC_RIGHT_UP
- EVT_FC_RIGHT_DCLICK
- EVT_FC_ENTER_OBJECT
- EVT_FC_LEAVE_OBJECT
:param `CallBackFun`: the call back function for the event
"""
##fixme: Way too much Canvas Manipulation here!
self.CallBackFuncs[Event] = CallBackFun
self.HitAble = True
self._Canvas.UseHitTest = True
if self.InForeground and self._Canvas._ForegroundHTBitmap is None:
self._Canvas.MakeNewForegroundHTBitmap()
elif self._Canvas._HTBitmap is None:
self._Canvas.MakeNewHTBitmap()
if not self.HitColor:
if not self._Canvas.HitColorGenerator:
# first call to prevent the background color from being used.
self._Canvas.HitColorGenerator = _colorGenerator()
next(self._Canvas.HitColorGenerator)
self.HitColor = next(self._Canvas.HitColorGenerator)
self.SetHitPen(self.HitColor,self.HitLineWidth)
self.SetHitBrush(self.HitColor)
# put the object in the hit dict, indexed by it's color
if not self._Canvas.HitDict:
self._Canvas.MakeHitDict()
self._Canvas.HitDict[Event][self.HitColor] = (self) # put the object in the hit dict, indexed by its color
def UnBindAll(self):
"""
Unbind all events
.. note:: Currently only removes one from each list
"""
## fixme: this only removes one from each list, there could be more.
## + patch by Tim Ansel
if self._Canvas.HitDict:
for Event in self._Canvas.HitDict.itervalues():
try:
del Event[self.HitColor]
except KeyError:
pass
self.HitAble = False
def SetBrush(self, FillColor, FillStyle):
"""
Set the brush for this DrawObject
:param `FillColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid entries
:param `FillStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetFillStyle`
for valid entries
"""
if FillColor is None or FillStyle is None:
self.Brush = wx.TRANSPARENT_BRUSH
##fixme: should I really re-set the style?
self.FillStyle = "Transparent"
else:
self.Brush = self.BrushList.setdefault(
(FillColor, FillStyle),
wx.Brush(FillColor, self.FillStyleList[FillStyle]))
#print("Setting Brush, BrushList length:", len(self.BrushList))
def SetPen(self, LineColor, LineStyle, LineWidth):
"""
Set the Pen for this DrawObject
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid entries
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
for valid entries
:param integer `LineWidth`: the width in pixels
"""
if (LineColor is None) or (LineStyle is None):
self.Pen = wx.TRANSPARENT_PEN
self.LineStyle = 'Transparent'
else:
self.Pen = self.PenList.setdefault(
(LineColor, LineStyle, LineWidth),
wx.Pen(LineColor, int(LineWidth), self.LineStyleList[LineStyle]))
def SetHitBrush(self, HitColor):
"""
Set the brush used for hit test, do not call directly.
:param `HitColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
"""
if not self.HitFill:
self.HitBrush = wx.TRANSPARENT_BRUSH
else:
self.HitBrush = self.BrushList.setdefault(
(HitColor,"solid"),
wx.Brush(HitColor, self.FillStyleList["Solid"]))
def SetHitPen(self, HitColor, LineWidth):
"""
Set the pen used for hit test, do not call directly.
:param `HitColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param integer `LineWidth`: the line width in pixels
"""
if not self.HitLine:
self.HitPen = wx.TRANSPARENT_PEN
else:
self.HitPen = self.PenList.setdefault( (HitColor, "solid", self.HitLineWidth), wx.Pen(HitColor, self.HitLineWidth, self.LineStyleList["Solid"]) )
## Just to make sure that they will always be there
## the appropriate ones should be overridden in the subclasses
def SetColor(self, Color):
"""
Set the Color - this method is overridden in the subclasses
:param `Color`: use one of the following values any valid entry from
:class:`wx.ColourDatabase`
- ``Green``
- ``White``
- ``Black``
- ``Grey``
- ``MediumGrey``
- ``LightGrey``
- ``Cyan``
- ``Red``
"""
pass
def SetLineColor(self, LineColor):
"""
Set the LineColor - this method is overridden in the subclasses
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid values
"""
pass
def SetLineStyle(self, LineStyle):
"""
Set the LineStyle - this method is overridden in the subclasses
:param `LineStyle`: Use one of the following values or ``None`` :
===================== =============================
Style Description
===================== =============================
``Solid`` Solid line
``Transparent`` A transparent line
``Dot`` Dotted line
``LongDash`` Dashed line (long)
``ShortDash`` Dashed line (short)
``DotDash`` Dash-dot-dash line
===================== =============================
"""
pass
def SetLineWidth(self, LineWidth):
"""
Set the LineWidth
:param integer `LineWidth`: sets the line width in pixel
"""
pass
def SetFillColor(self, FillColor):
"""
Set the FillColor - this method is overridden in the subclasses
:param `FillColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid values
"""
pass
def SetFillStyle(self, FillStyle):
"""
Set the FillStyle - this method is overridden in the subclasses
:param string `FillStyle`: following is a list of valid values:
===================== =========================================
Style Description
===================== =========================================
``Transparent`` Transparent fill
``Solid`` Solid fill
``BiDiagonalHatch`` Bi Diagonal hatch fill
``CrossDiagHatch`` Cross Diagonal hatch fill
``FDiagonal_Hatch`` F Diagonal hatch fill
``CrossHatch`` Cross hatch fill
``HorizontalHatch`` Horizontal hatch fill
``VerticalHatch`` Vertical hatch fill
===================== =========================================
"""
pass
def PutInBackground(self):
"""Put the object in the background."""
if self._Canvas and self.InForeground:
self._Canvas._ForeDrawList.remove(self)
self._Canvas._DrawList.append(self)
self._Canvas._BackgroundDirty = True
self.InForeground = False
def PutInForeground(self):
"""Put the object in the foreground."""
if self._Canvas and (not self.InForeground):
self._Canvas._ForeDrawList.append(self)
self._Canvas._DrawList.remove(self)
self._Canvas._BackgroundDirty = True
self.InForeground = True
def Hide(self):
"""Hide the object."""
self.Visible = False
def Show(self):
"""Show the object."""
self.Visible = True
class ColorOnlyMixin:
"""
Mixin class for objects that have just one color, rather than a fill
color and line color
"""
def SetColor(self, Color):
"""
Set the Color
:param `Color`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid values
"""
self.SetPen(Color,"Solid",1)
self.SetBrush(Color,"Solid")
SetFillColor = SetColor # Just to provide a consistent interface
class LineOnlyMixin:
"""
Mixin class for objects that have just a line, rather than a fill
color and line color
"""
def SetLineColor(self, LineColor):
"""
Set the LineColor
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid values
"""
self.LineColor = LineColor
self.SetPen(LineColor,self.LineStyle,self.LineWidth)
SetColor = SetLineColor# so that it will do something reasonable
def SetLineStyle(self, LineStyle):
"""
Set the LineStyle
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
for valid values
"""
self.LineStyle = LineStyle
self.SetPen(self.LineColor,LineStyle,self.LineWidth)
def SetLineWidth(self, LineWidth):
"""
Set the LineWidth
:param integer `LineWidth`: line width in pixels
"""
self.LineWidth = LineWidth
self.SetPen(self.LineColor,self.LineStyle,LineWidth)
class LineAndFillMixin(LineOnlyMixin):
"""
Mixin class for objects that have both a line and a fill color and
style.
"""
def SetFillColor(self, FillColor):
"""
Set the FillColor
:param `FillColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
for valid values
"""
self.FillColor = FillColor
self.SetBrush(FillColor, self.FillStyle)
def SetFillStyle(self, FillStyle):
"""
Set the FillStyle
:param `FillStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetFillStyle`
for valid values
"""
self.FillStyle = FillStyle
self.SetBrush(self.FillColor,FillStyle)
def SetUpDraw(self, dc, WorldToPixel, ScaleWorldToPixel, HTdc):
"""
Setup for draw
:param `dc`: the dc to draw ???
:param `WorldToPixel`: ???
:param `ScaleWorldToPixel`: ???
:param `HTdc`: ???
"""
dc.SetPen(self.Pen)
dc.SetBrush(self.Brush)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.SetBrush(self.HitBrush)
return ( WorldToPixel(self.XY),
ScaleWorldToPixel(self.WH) )
class XYObjectMixin:
"""
This is a mixin class that provides some methods suitable for use
with objects that have a single (x,y) coordinate pair.
"""
def Move(self, Delta):
"""
Moves the object by delta, where delta is a (dx, dy) pair.
:param `Delta`: is a (dx, dy) pair ideally a `NumPy <http://www.numpy.org/>`_
array of shape (2, )
"""
Delta = np.asarray(Delta, float)
self.XY += Delta
self.BoundingBox += Delta
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
def CalcBoundingBox(self):
"""Calculate the bounding box."""
## This may get overwritten in some subclasses
self.BoundingBox = BBox.asBBox((self.XY, self.XY))
def SetPoint(self, xy):
xy = np.array(xy, float)
xy = xy.reshape((2,))
self.XY = xy
self.CalcBoundingBox()
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
class PointsObjectMixin:
"""
A mixin class that provides some methods suitable for use
with objects that have a set of (x, y) coordinate pairs.
"""
def Move(self, Delta):
"""
Moves the object by delta, where delta is a (dx, dy) pair.
:param `Delta`: is a (dx, dy) pair ideally a `NumPy <http://www.numpy.org/>`_
array of shape (2, )
"""
Delta = np.asarray(Delta, float)
Delta = Delta.reshape((2,))
self.Points += Delta
self.BoundingBox += Delta
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
def CalcBoundingBox(self):
"""Calculate the bounding box."""
self.BoundingBox = BBox.fromPoints(self.Points)
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
def SetPoints(self, Points, copy=True):
"""
Sets the coordinates of the points of the object to Points (NX2 array).
:param `Points`: takes a 2-tuple, or a (2,)
`NumPy <http://www.numpy.org/>`_ array of point coordinates
:param boolean `copy`: By default, a copy is made, if copy is set to
``False``, a reference is used, if Points is a NumPy array of Floats.
This allows you to change some or all of the points without making
any copies.
For example::
Points = Object.Points
# shifts the points 5 in the x dir, and 10 in the y dir.
Points += (5, 10)
# Sets the points to the same array as it was
Object.SetPoints(Points, False)
"""
if copy:
self.Points = np.array(Points, float)
self.Points = self.Points.reshape((-1, 2)) # Make sure it is a NX2 array, even if there is only one point
else:
self.Points = np.asarray(Points, float)
self.CalcBoundingBox()
class Polygon(PointsObjectMixin, LineAndFillMixin, DrawObject):
"""
Draws a polygon
Points is a list of 2-tuples, or a NX2 NumPy array of
point coordinates. so that Points[N][0] is the x-coordinate of
point N and Points[N][1] is the y-coordinate or Points[N,0] is the
x-coordinate of point N and Points[N,1] is the y-coordinate for
arrays.
"""
def __init__(self,
Points,
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 1,
FillColor = None,
FillStyle = "Solid",
InForeground = False):
"""
Default class constructor.
:param `Points`: start point, takes a 2-tuple, or a (2,)
`NumPy <http://www.numpy.org/>`_ array of point coordinates
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
:param `LineWidth`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineWidth`
:param `FillColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param `FillStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetFillStyle`
:param boolean `InForeground`: should object be in foreground
"""
DrawObject.__init__(self, InForeground)
self.Points = np.array(Points ,float) # this DOES need to make a copy
self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
self.LineWidth = LineWidth
self.FillColor = FillColor
self.FillStyle = FillStyle
self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)
self.SetPen(LineColor,LineStyle,LineWidth)
self.SetBrush(FillColor,FillStyle)
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel = None, HTdc=None):
Points = WorldToPixel(self.Points)
dc.SetPen(self.Pen)
dc.SetBrush(self.Brush)
dc.DrawPolygon(Points)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.SetBrush(self.HitBrush)
HTdc.DrawPolygon(Points)
class Line(PointsObjectMixin, LineOnlyMixin, DrawObject):
"""
Draws a line
It will draw a straight line if there are two points, and a polyline
if there are more than two.
"""
def __init__(self, Points,
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 1,
InForeground = False):
"""
Default class constructor.
:param `Points`: takes a 2-tuple, or a (2,)
`NumPy <http://www.numpy.org/>`_ array of point coordinates
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
:param `LineWidth`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineWidth`
:param boolean `InForeground`: should object be in foreground
"""
DrawObject.__init__(self, InForeground)
self.Points = np.array(Points,float)
self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
self.LineWidth = LineWidth
self.SetPen(LineColor,LineStyle,LineWidth)
self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
Points = WorldToPixel(self.Points)
dc.SetPen(self.Pen)
dc.DrawLines(Points)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.DrawLines(Points)
class Spline(Line):
"""Draws a spline"""
def __init__(self, *args, **kwargs):
"""
Default class constructor.
see :class:`~lib.floatcanvas.FloatCanvas.Line`
"""
Line.__init__(self, *args, **kwargs)
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
Points = WorldToPixel(self.Points)
dc.SetPen(self.Pen)
dc.DrawSpline(Points)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.DrawSpline(Points)
class Arrow(XYObjectMixin, LineOnlyMixin, DrawObject):
"""
Draws an arrow
It will draw an arrow , starting at the point ``XY`` points at an angle
defined by ``Direction``.
"""
def __init__(self,
XY,
Length,
Direction,
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 2,
ArrowHeadSize = 8,
ArrowHeadAngle = 30,
InForeground = False):
"""
Default class constructor.
:param `XY`: the (x, y) coordinate of the starting point, or a 2-tuple,
or a (2,) `NumPy <http://www.numpy.org/>`_ array
:param integer `Length`: length of arrow in pixels
:param integer `Direction`: angle of arrow in degrees, zero is straight
up `+` angle is to the right
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
:param `LineWidth`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineWidth`
:param `ArrowHeadSize`: size of arrow head in pixels
:param `ArrowHeadAngle`: angle of arrow head in degrees
:param boolean `InForeground`: should object be in foreground
"""
DrawObject.__init__(self, InForeground)
self.XY = np.array(XY, float)
self.XY = self.XY.reshape((2,)) # Make sure it is a length 2 vector
self.Length = Length
self.Direction = float(Direction)
self.ArrowHeadSize = ArrowHeadSize
self.ArrowHeadAngle = float(ArrowHeadAngle)
self.CalcArrowPoints()
self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
self.LineWidth = LineWidth
self.SetPen(LineColor,LineStyle,LineWidth)
##fixme: How should the HitTest be drawn?
self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)
def SetDirection(self, Direction):
"""
Set the direction
:param integer `Direction`: angle of arrow in degrees, zero is straight
up `+` angle is to the right
"""
self.Direction = float(Direction)
self.CalcArrowPoints()
def SetLength(self, Length):
"""
Set the length
:param integer `Length`: length of arrow in pixels
"""
self.Length = Length
self.CalcArrowPoints()
def SetLengthDirection(self, Length, Direction):
"""
Set the length and direction
:param integer `Length`: length of arrow in pixels
:param integer `Direction`: angle of arrow in degrees, zero is straight
up `+` angle is to the right
"""
self.Direction = float(Direction)
self.Length = Length
self.CalcArrowPoints()
## def CalcArrowPoints(self):
## L = self.Length
## S = self.ArrowHeadSize
## phi = self.ArrowHeadAngle * N.pi / 360
## theta = (self.Direction-90.0) * N.pi / 180
## ArrowPoints = N.array( ( (0, L, L - S*N.cos(phi),L, L - S*N.cos(phi) ),
## (0, 0, S*N.sin(phi), 0, -S*N.sin(phi) ) ),
## float )
## RotationMatrix = N.array( ( ( N.cos(theta), -N.sin(theta) ),
## ( N.sin(theta), N.cos(theta) ) ),
## float
## )
## ArrowPoints = N.matrixmultiply(RotationMatrix, ArrowPoints)
## self.ArrowPoints = N.transpose(ArrowPoints)
def CalcArrowPoints(self):
"""Calculate the arrow points."""
L = self.Length
S = self.ArrowHeadSize
phi = self.ArrowHeadAngle * np.pi / 360
theta = (270 - self.Direction) * np.pi / 180
AP = np.array( ( (0,0),
(0,0),
(np.cos(theta - phi), -np.sin(theta - phi) ),
(0,0),
(np.cos(theta + phi), -np.sin(theta + phi) ),
), float )
AP *= S
shift = (-L*np.cos(theta), L*np.sin(theta) )
AP[1:,:] += shift
self.ArrowPoints = AP
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
dc.SetPen(self.Pen)
xy = WorldToPixel(self.XY)
ArrowPoints = (xy + self.ArrowPoints).astype(int)
dc.DrawLines(ArrowPoints)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.DrawLines(ArrowPoints)
class ArrowLine(PointsObjectMixin, LineOnlyMixin, DrawObject):
"""
Draws an arrow line.
It will draw a set of arrows from point to point.
It takes a list of 2-tuples, or a NX2 NumPy Float array of point coordinates.
"""
def __init__(self,
Points,
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 1,
ArrowHeadSize = 8,
ArrowHeadAngle = 30,
InForeground = False):
"""
Default class constructor.
:param `Points`: takes a 2-tuple, or a (2,)
`NumPy <http://www.numpy.org/>`_ array of point coordinates
:param `LineColor`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetColor`
:param `LineStyle`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineStyle`
:param `LineWidth`: see :meth:`~lib.floatcanvas.FloatCanvas.DrawObject.SetLineWidth`
:param `ArrowHeadSize`: size of arrow head in pixels
:param `ArrowHeadAngle`: angle of arrow head in degrees
:param boolean `InForeground`: should object be in foreground
"""
DrawObject.__init__(self, InForeground)
self.Points = np.asarray(Points,float)
self.Points = self.Points.reshape((-1,2)) # Make sure it is a NX2 array, even if there is only one point
self.ArrowHeadSize = ArrowHeadSize
self.ArrowHeadAngle = float(ArrowHeadAngle)
self.CalcArrowPoints()
self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
self.LineWidth = LineWidth
self.SetPen(LineColor,LineStyle,LineWidth)
self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)
def CalcArrowPoints(self):
"""Calculate the arrow points."""
S = self.ArrowHeadSize
phi = self.ArrowHeadAngle * np.pi / 360
Points = self.Points
n = Points.shape[0]
self.ArrowPoints = np.zeros((n-1, 3, 2), float)
for i in range(n-1):
dx, dy = self.Points[i] - self.Points[i+1]
theta = np.arctan2(dy, dx)
AP = np.array( (
(np.cos(theta - phi), -np.sin(theta-phi)),
(0,0),
(np.cos(theta + phi), -np.sin(theta + phi))
),
float )
self.ArrowPoints[i,:,:] = AP
self.ArrowPoints *= S
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
Points = WorldToPixel(self.Points)
ArrowPoints = (Points[1:,np.newaxis,:] + self.ArrowPoints).astype(int)
dc.SetPen(self.Pen)
dc.DrawLines(Points)
for arrow in ArrowPoints:
dc.DrawLines(arrow)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
HTdc.DrawLines(Points)
for arrow in ArrowPoints:
HTdc.DrawLines(arrow)
class PointSet(PointsObjectMixin, ColorOnlyMixin, DrawObject):
"""
Draws a set of points
If Points is a sequence of tuples: Points[N][0] is the x-coordinate of
point N and Points[N][1] is the y-coordinate.
If Points is a NumPy array: Points[N,0] is the x-coordinate of point
N and Points[N,1] is the y-coordinate for arrays.
Each point will be drawn the same color and Diameter. The Diameter
is in screen pixels, not world coordinates.
The hit-test code does not distinguish between the points, you will
only know that one of the points got hit, not which one. You can use
PointSet.FindClosestPoint(WorldPoint) to find out which one
In the case of points, the HitLineWidth is used as diameter.
"""
def __init__(self, Points, Color="Black", Diameter=1, InForeground=False):
"""