-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_canvas.py
More file actions
2522 lines (1956 loc) · 80.3 KB
/
plot_canvas.py
File metadata and controls
2522 lines (1956 loc) · 80.3 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
# -------------------------------------------------------------------------
# Copyright (C) 2005-2013 Martin Strohalm <www.mmass.org>
# This program 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 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Complete text of GNU GPL can be found in the file LICENSE.TXT in the
# main directory of the program.
# -------------------------------------------------------------------------
# load libs
import wx
import numpy
# MAIN PLOT CANVAS OBJECT
# -----------------------
class canvas(wx.Window):
"""Plot canvas"""
def __init__(self, parent, id=-1, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, **attr):
wx.Window.__init__(self, parent, id, size=size, style=style)
self.parent = parent
self.SetBackgroundColour('white')
# set default canvas params
self.properties = {
'isotopeDistance': 1.00287,
'xLabel': '',
'yLabel': '',
'showGrid': True,
'showMinorTicks': True,
'showZero': False,
'showLegend': False,
'showXPosBar': False,
'showYPosBar': False,
'showGel': False,
'showCurXPos': True,
'showCurYPos': True,
'showCurDistance': True,
'showCurCharge': True,
'showCurImage': False,
'autoScaleY': True,
'ySymmetry': False,
'overlapLabels': True,
'posBarSize': 6,
'gelHeight': 13,
'xPosDigits': 2,
'yPosDigits': 0,
'distanceDigits': 2,
'xScrollFactor': 0.1,
'xMoveFactor': 0.1,
'xScaleFactor': 0.1,
'yScaleFactor': 0.1,
'maxZoom': 0.001,
'zoomAxis': 'xy',
'checkLimits': True,
'reverseScrolling': False,
'reverseDrawing': False,
'canvasColour': (255, 255, 255),
'plotColour': (255, 255, 255),
'axisColour': (0, 0, 0),
'gridColour': (235, 235, 235),
'highlightColour': (255, 0, 0),
'axisFont': wx.Font(10, wx.SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0),
}
# get new attributes
for name, value in attr.items():
self.properties[name] = value
# set default canvas params
self.mouseFn = None
self.mouseFnLMB = None
self.mouseFnRMB = 'zoom'
self.mouseTracker = False
self.currentObject = None
self.currentCharge = 1
self.currentIsotopes = []
self.currentIsotopeLines = 0
self.gelsCount = 0
self.printerScale = {'drawings':1, 'fonts':1}
self.viewMemory = [[],[]]
self.cursorPosition = [0, 0, 0, 0]
self.cursorImage = wx.StockCursor(wx.CURSOR_ARROW)
self.draggingStart = False
self.mouseEvent = False
self.lastDraw = None
self.pointScale = 1
self.pointShift = 0
# set events
self.Bind(wx.EVT_PAINT, self.onPaint)
self.Bind(wx.EVT_SIZE, self.onSize)
self.Bind(wx.EVT_LEAVE_WINDOW, self.onLeave)
self.Bind(wx.EVT_LEFT_DOWN, self.onLMD)
self.Bind(wx.EVT_LEFT_UP, self.onLMU)
self.Bind(wx.EVT_LEFT_DCLICK, self.onLMDC)
self.Bind(wx.EVT_RIGHT_DOWN, self.onRMD)
self.Bind(wx.EVT_RIGHT_UP, self.onRMU)
self.Bind(wx.EVT_RIGHT_DCLICK, self.onRMDC)
self.Bind(wx.EVT_MIDDLE_DOWN, self.onRMD)
self.Bind(wx.EVT_MIDDLE_UP, self.onRMU)
self.Bind(wx.EVT_MOTION, self.onMMotion)
self.Bind(wx.EVT_MOUSEWHEEL, self.onMScroll)
self.Bind(wx.EVT_KEY_DOWN, self.onChar)
# initialize bitmap buffer and set initial size based on client size
self.onSize(0)
# ----
def onPaint(self, evt):
"""Repaint plot."""
# clear cursor tracker
if self.mouseTracker:
self.drawMouseTracker()
# draw buffer to screen
dc = wx.BufferedPaintDC(self, self.plotBuffer)
# ----
def onSize(self, evt):
"""Repaint plot when size changed."""
# get size
width, height = self.GetClientSize()
width = max(1, width)
height = max(1, height)
# make new offscreen bitmap
self.plotBuffer = wx.EmptyBitmap(width, height)
self.setSize()
# redraw plot or clear area
if self.lastDraw:
# get current axis and x range
minX, maxX = self.getCurrentXRange()
minY, maxY = self.getCurrentYRange()
rangeXmin, rangeXmax = self.getMaxXRange()
# block oversizing
if minX < rangeXmin:
minX = rangeXmin
if maxX > rangeXmax:
maxX = rangeXmax
if minX > maxX:
minX = rangeXmin
maxX = rangeXmax
# draw plot
self.draw(self.lastDraw[0], (minX, maxX), (minY, maxY))
else:
self.clear()
# ----
def onLeave(self, evt):
"""Escape mouse events when cursor leave out of the canvas."""
# clear cursor tracker
if self.mouseTracker:
self.drawMouseTracker()
# escape mouse events
self.escMouseEvents()
# set mouse cursor
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
# ----
def onLMD(self, evt):
"""Change cursor style according to position and current mouse function."""
# get focus
if not self.FindFocus() == self:
self.SetFocus()
try: wx.Yield()
except: pass
# get cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# escape if any mouse event set
if self.mouseEvent:
return
# clear cursor tracker
if self.mouseTracker:
self.drawMouseTracker()
# get starting coords for movement
self.draggingStart = self.cursorPosition[:]
location = self.getCursorLocation()
# set zooming with Control (for one-button mouse)
if location == 'plot' and evt.ControlDown():
self.mouseEvent = 'zoom'
self.drawZoomBox()
# draw point tracker
elif location == 'plot' and self.mouseFnLMB == 'point':
self.mouseEvent = 'point'
self.drawPointTracker()
# draw isotope ruler
elif location == 'plot' and self.mouseFnLMB == 'isotopes':
self.mouseEvent = 'isotopes'
self.drawIsotopeRuler()
# draw selection rectangle
elif location == 'plot' and self.mouseFnLMB == 'rectangle':
self.mouseEvent = 'rectangle'
self.drawSelectionRect()
# draw selection range
elif location == 'plot' and self.mouseFnLMB == 'range':
self.mouseEvent = 'range'
self.drawSelectionRange()
# draw distance arrow
elif location == 'plot' and self.mouseFnLMB in ('xDistance', 'yDistance'):
self.mouseEvent = 'distance'
self.drawDistanceTracker()
# set axis dragging
elif location == 'xAxis':
self.mouseEvent = 'xShift'
elif location == 'yAxis':
self.mouseEvent = 'yShift'
# not in area
elif location == 'blank':
self.mouseEvent = 'LOut'
# ----
def onLMU(self, evt):
"""Clear cursor."""
# get focus
if not self.FindFocus() == self:
self.SetFocus()
return
# zoom plot
if self.mouseEvent == 'zoom':
# clear zoombox
self.drawZoomBox()
# get zoom
minX = min(self.draggingStart[0], self.cursorPosition[0])
maxX = max(self.draggingStart[0], self.cursorPosition[0])
minY = min(self.draggingStart[1], self.cursorPosition[1])
maxY = max(self.draggingStart[1], self.cursorPosition[1])
# apply zoom
if self.properties['zoomAxis'] == 'xy' and minX != maxX and minY != maxY:
self.zoom(xAxis=(minX, maxX), yAxis=(minY, maxY))
elif self.properties['zoomAxis'] == 'x' and minX != maxX:
self.zoom(xAxis=(minX, maxX))
elif self.properties['zoomAxis'] == 'y' and minY != maxY:
self.zoom(yAxis=(minY, maxY))
# clear point selection
elif self.mouseEvent == 'point':
self.drawPointTracker()
# clear isotope ruler
elif self.mouseEvent == 'isotopes':
self.drawIsotopeRuler()
# clear selection rectangle
elif self.mouseEvent == 'rectangle':
self.drawSelectionRect()
# clear selection range
elif self.mouseEvent == 'range':
self.drawSelectionRange()
# clear distance arrow
elif self.mouseEvent == 'distance':
self.drawDistanceTracker()
# remember zoom
elif self.mouseEvent in ('xShift', 'yShift'):
self.rememberView()
# set cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# reset mouse event flag
if self.mouseEvent in ('LOut', 'zoom', 'point', 'isotopes', 'rectangle', 'range', 'distance', 'xShift', 'yShift'):
self.mouseEvent = False
# show point tracker
if self.getCursorLocation() == 'plot':
self.drawMouseTracker()
# ----
def onLMDC(self, evt):
"""Show full-axis plot on left-mouse double-click."""
# set axis ranges according to cursor location
location = self.getCursorLocation()
if location == 'plot':
minX, maxX = self.getMaxXRange()
minY, maxY = self.getMaxYRange()
elif location == 'xAxis':
minX, maxX = self.getMaxXRange()
if self.properties['autoScaleY']:
minY, maxY = self.getMaxYRange(minX, maxX)
else:
minY, maxY = self.getCurrentYRange()
elif location == 'yAxis':
minX, maxX = self.getCurrentXRange()
minY, maxY = self.getMaxYRange()
else:
return
# redraw plot
self.draw(self.lastDraw[0], (minX, maxX), (minY, maxY))
# remember new zoom
self.rememberView((minX, maxX), (minY, maxY))
# reset cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# show point tracker
if self.getCursorLocation() == 'plot':
self.drawMouseTracker()
# ----
def onRMD(self, evt):
"""Change cursor style according to position and current mouse function."""
# get focus
if not self.FindFocus() == self:
self.SetFocus()
# get cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# escape if any mouse event set
if self.mouseEvent:
return
# get starting coords for movement
self.draggingStart = self.cursorPosition[:]
location = self.getCursorLocation()
# draw zoom box and set event
if location == 'plot' and self.mouseFnRMB == 'zoom':
self.mouseEvent = 'zoom'
self.drawZoomBox()
# set event
elif location == 'xAxis':
self.mouseEvent = 'xScale'
elif location == 'yAxis':
self.mouseEvent = 'yScale'
elif location == 'blank':
self.mouseEvent = 'ROut'
# clear cursor tracker
if self.mouseTracker:
self.drawMouseTracker()
# ----
def onRMU(self, evt):
"""Set new zoom and clear cursor."""
# get focus
if not self.FindFocus() == self:
self.SetFocus()
return
# zoom dragging
if self.mouseEvent == 'zoom':
# clear zoombox
self.drawZoomBox()
# get zoom
minX = min(self.draggingStart[0], self.cursorPosition[0])
maxX = max(self.draggingStart[0], self.cursorPosition[0])
minY = min(self.draggingStart[1], self.cursorPosition[1])
maxY = max(self.draggingStart[1], self.cursorPosition[1])
# apply zoom
if self.properties['zoomAxis'] == 'xy' and minX != maxX and minY != maxY:
self.zoom(xAxis=(minX, maxX), yAxis=(minY, maxY))
elif self.properties['zoomAxis'] == 'x' and minX != maxX:
self.zoom(xAxis=(minX, maxX))
elif self.properties['zoomAxis'] == 'y' and minY != maxY:
self.zoom(yAxis=(minY, maxY))
# axis scaling
elif self.mouseEvent in ('xScale', 'yScale'):
self.rememberView()
# set new cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# reset mouse event flag
if self.mouseEvent in ('ROut', 'zoom', 'xScale', 'yScale'):
self.mouseEvent = False
# show point tracker
if self.getCursorLocation() == 'plot':
self.drawMouseTracker()
# ----
def onRMDC(self, evt):
"""Show full plot on right-mouse double-click."""
# set axis ranges according to cursor location
if self.getCursorLocation() == 'plot':
minX, maxX = self.getMaxXRange()
minY, maxY = self.getMaxYRange()
else:
return
# redraw plot
self.draw(self.lastDraw[0], (minX, maxX), (minY, maxY))
# remember new zoom
self.rememberView((minX, maxX), (minY, maxY))
# reset cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# show point tracker
if self.getCursorLocation() == 'plot':
self.drawMouseTracker()
# ----
def onMMotion(self, evt):
"""Draw cursor on mouse motion."""
# clear cursor tracker
if not self.mouseEvent and self.mouseTracker:
self.drawMouseTracker()
# clear zoombox
elif self.mouseEvent == 'zoom':
self.drawZoomBox()
# clear point tracker
elif self.mouseEvent == 'point':
self.drawPointTracker()
# clear isotope ruler
elif self.mouseEvent == 'isotopes':
self.drawIsotopeRuler()
# clear selection rectangle
elif self.mouseEvent == 'rectangle':
self.drawSelectionRect()
# clear selection range
elif self.mouseEvent == 'range':
self.drawSelectionRange()
# clear distance arrow
elif self.mouseEvent == 'distance':
self.drawDistanceTracker()
# store cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# draw cursor tracker if no event
if not self.mouseEvent:
self.setCursorByLocation()
if self.getCursorLocation() == 'plot':
self.drawMouseTracker()
# draw zoombox
elif self.mouseEvent == 'zoom':
self.drawZoomBox()
# draw point tracker
elif self.mouseEvent == 'point':
self.drawPointTracker()
# draw isotope ruler
elif self.mouseEvent == 'isotopes':
self.drawIsotopeRuler()
# draw selection rectangle
elif self.mouseEvent == 'rectangle':
self.drawSelectionRect()
# draw selection range
elif self.mouseEvent == 'range':
self.drawSelectionRange()
# draw distance arrow
elif self.mouseEvent == 'distance':
self.drawDistanceTracker()
# move x axis
elif self.mouseEvent == 'xShift':
self.shiftAxis('x')
# move y axis
elif self.mouseEvent == 'yShift':
self.shiftAxis('y')
# scale x axis
elif self.mouseEvent == 'xScale':
self.scaleAxis('x')
# scale y axis
elif self.mouseEvent == 'yScale':
self.scaleAxis('y')
# ----
def onMScroll(self, evt):
"""Process mouse scroll."""
# escape if any mouse event set
if self.mouseEvent:
return
# clear cursor tracker
if self.mouseTracker:
self.drawMouseTracker()
# get scroll direction
direction = 1
if evt.GetWheelRotation() < 0:
direction = -1
if self.properties['reverseScrolling']:
direction *= -1
# set new charge and count for isotope ruler
if self.mouseFn == 'isotoperuler' and evt.ShiftDown():
if evt.AltDown() or evt.ControlDown():
self.currentIsotopeLines = min(50, self.currentIsotopeLines + direction)
else:
self.currentCharge = max(1, self.currentCharge + direction)
self.currentCharge = min(50, self.currentCharge)
self.drawMouseTracker()
return
# store cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# get current axis and x range
minX, maxX = self.getCurrentXRange()
minY, maxY = self.getCurrentYRange()
rangeXmin, rangeXmax = self.getMaxXRange()
# scale x axis and center to current cursor position
if evt.AltDown() or evt.ControlDown():
cursorPos = self.getCursorPosition()
if cursorPos:
currX = cursorPos[0]
minX -= (currX - minX) * self.properties['xScaleFactor'] * direction
maxX += (maxX - currX) * self.properties['xScaleFactor'] * direction
else:
return
# check limits
if self.properties['checkLimits']:
if minX < rangeXmin:
minX = rangeXmin
if maxX > rangeXmax:
maxX = rangeXmax
# check max zoom
if (maxX - minX) < self.properties['maxZoom']:
return
# autoscale y axis
if self.properties['autoScaleY']:
minY, maxY = self.getMaxYRange(minX, maxX)
# scale y axis
elif evt.ShiftDown() or self.getCursorLocation() == 'yAxis':
maxY += (minY - maxY) * self.properties['yScaleFactor'] * direction
# check y symmetry
if self.properties['ySymmetry']:
minY = -maxY
# shift x axis
else:
shift = (minX - maxX) * self.properties['xScrollFactor'] * direction
# check limits
if self.properties['checkLimits']:
if minX + shift < rangeXmin:
shift = rangeXmin - minX
elif maxX + shift > rangeXmax:
shift = rangeXmax - maxX
minX += shift
maxX += shift
# autoscale y axis
if self.properties['autoScaleY']:
minY, maxY = self.getMaxYRange(minX, maxX)
# redraw plot
self.draw(self.lastDraw[0], (minX, maxX), (minY, maxY))
# remember new zoom
self.rememberView((minX, maxX), (minY, maxY))
# store cursor positions
self.cursorPosition[0], self.cursorPosition[1] = self.getXY(evt)
self.cursorPosition[2], self.cursorPosition[3] = evt.GetPosition()
# ----
def onChar(self, evt):
"""Set zoom or position according to pressed key."""
# get key
key = evt.GetKeyCode()
# escape current mouse events
if key == wx.WXK_ESCAPE:
self.escMouseEvents()
return
# stop if any mouse event set
elif self.mouseEvent:
return
# get direction
direction = 1
if key in (wx.WXK_RIGHT, wx.WXK_UP, wx.WXK_PAGEDOWN, wx.WXK_NEXT):
direction = -1
# get current axis and x range
minX, maxX = self.getCurrentXRange()
minY, maxY = self.getCurrentYRange()
rangeXmin, rangeXmax = self.getMaxXRange()
rangeYmin, rangeYmax = self.getMaxYRange()
# move/scale x axis by factor
if key in (wx.WXK_LEFT, wx.WXK_RIGHT):
# scale x axis
if evt.AltDown():
scale = (maxX - minX) * self.properties['xScaleFactor'] * direction
minX -= scale
maxX += scale
# check limits
if self.properties['checkLimits']:
if minX < rangeXmin:
minX = rangeXmin
if maxX > rangeXmax:
maxX = rangeXmax
# move x axis
else:
shift = (minX - maxX) * self.properties['xMoveFactor'] * direction
# check limits
if self.properties['checkLimits']:
if minX + shift < rangeXmin:
shift = rangeXmin - minX
elif maxX + shift > rangeXmax:
shift = rangeXmax - maxX
minX += shift
maxX += shift
# check max zoom
if (maxX - minX) < self.properties['maxZoom']:
return
# move x axis by page
elif key in (wx.WXK_PAGEUP, wx.WXK_PAGEDOWN, wx.WXK_PRIOR, wx.WXK_NEXT):
shift = (minX - maxX) * 1 * direction
# check limits
if self.properties['checkLimits']:
if minX + shift < rangeXmin:
shift = rangeXmin - minX
elif maxX + shift > rangeXmax:
shift = rangeXmax - maxX
minX += shift
maxX += shift
# check max zoom
if (maxX - minX) < self.properties['maxZoom']:
return
# scale y axis
elif key == wx.WXK_UP or key == wx.WXK_DOWN:
maxY += (maxY - minY) * self.properties['yScaleFactor'] * direction
# fullsize
elif key == wx.WXK_HOME and evt.ControlDown():
minX = rangeXmin
maxX = rangeXmax
minY = rangeYmin
maxY = rangeYmax
# go to plot start
elif key == wx.WXK_HOME:
diff = maxX - minX
minX = rangeXmin
maxX = rangeXmin + diff
# go to plot end
elif key == wx.WXK_END:
diff = maxX - minX
minX = rangeXmax - diff
maxX = rangeXmax
# go forth in zoom memory
elif key == wx.WXK_BACK and evt.AltDown():
if len(self.viewMemory[1]) > 0:
self.viewMemory[0].append(self.viewMemory[1][-1])
minX = self.viewMemory[1][-1][0][0]
maxX = self.viewMemory[1][-1][0][1]
minY = self.viewMemory[1][-1][1][0]
maxY = self.viewMemory[1][-1][1][1]
del self.viewMemory[1][-1]
else:
return
# go back in zoom memory
elif key == wx.WXK_BACK:
if len(self.viewMemory[0]) > 1:
self.viewMemory[1].append(self.viewMemory[0][-1])
del self.viewMemory[0][-1]
minX = self.viewMemory[0][-1][0][0]
maxX = self.viewMemory[0][-1][0][1]
minY = self.viewMemory[0][-1][1][0]
maxY = self.viewMemory[0][-1][1][1]
else:
return
else:
evt.Skip()
return
# autoscale y axis
if self.properties['autoScaleY'] and key in (wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_END, wx.WXK_HOME, wx.WXK_PAGEUP, wx.WXK_PAGEDOWN, wx.WXK_PRIOR, wx.WXK_NEXT):
minY, maxY = self.getMaxYRange(minX, maxX)
# redraw plot
self.draw(self.lastDraw[0], (minX, maxX), (minY, maxY))
# remember new zoom
if key != wx.WXK_BACK:
self.rememberView((minX, maxX), (minY, maxY))
# ----
def getPrintout(self, filterSize, title):
"""Get printout of current plot."""
return printout(self, filterSize, title)
# ----
def getBitmap(self, width=None, height=None, printerScale=None):
"""Get current plot bitmap with selected size."""
# get width/height if not set
if not width or not height:
width, height = self.GetClientSize()
# create empty bitmap
tmpBitmap = wx.EmptyBitmap(width, height)
tmpDC = wx.MemoryDC()
tmpDC.SelectObject(tmpBitmap)
tmpDC.Clear()
# rescale plot
self.setSize(width, height)
# thicken up pens and fonts
if not printerScale:
ratioW = float(width) / 750
ratioH = float(height) / 750
scale = max(min(ratioW, ratioH), 1)
self.printerScale['drawings'] = scale
self.printerScale['fonts'] = scale
else:
self.printerScale['drawings'] = max(printerScale['drawings'], 1)
self.printerScale['fonts'] = max(printerScale['fonts'], 1)
# set filter size
filterSize = max(self.printerScale['drawings']*0.5, 0.5)
# draw plot
self.drawOutside(tmpDC, filterSize)
tmpDC.SelectObject(wx.NullBitmap)
# rescale back to original
self.setSize()
self.printerScale['drawings'] = 1
self.printerScale['fonts'] = 1
self.refresh()
return tmpBitmap
# ----
def getXY(self, evt):
"""Get XY position in user values."""
x, y = self.positionScreenToUser(evt.GetPosition())
return x, y
# ----
def getCurrentXRange(self):
"""Get current X-axis range."""
return self.lastDraw[1]
# ----
def getCurrentYRange(self):
"""Get current Y-axis range."""
return self.lastDraw[2]
# ----
def getMaxXRange(self, absolute=False):
"""Get maximal X-axis range."""
graphics = self.lastDraw[0]
p1, p2 = graphics.getBoundingBox(absolute=absolute)
return (p1[0], p2[0])
# ----
def getMaxYRange(self, minX=None, maxX=None, absolute=False):
"""Get maximal Y-axis range."""
# get bounding box
graphics = self.lastDraw[0]
p1, p2 = graphics.getBoundingBox(minX, maxX, absolute)
# check y symmetry
if self.properties['ySymmetry']:
maxY = max(abs(p1[1]), abs(p2[1]))
return (-maxY, maxY)
else:
return (p1[1], p2[1])
# ----
def getCursorLocation(self):
"""Locate cursor within the plot area."""
# get plot width/height
minX = self.plotCoords[0]
minY = self.plotCoords[1]
maxX = self.plotCoords[2]
maxY = self.plotCoords[3]
# get current position
x = self.cursorPosition[2]
y = self.cursorPosition[3]
# locate cursor
if minX < x < maxX and minY < y < maxY:
return 'plot'
elif minX < x < maxX and y > minY:
return 'xAxis'
elif minY < y < maxY and x < minX:
return 'yAxis'
else:
return 'blank'
# ----
def getCursorPosition(self):
"""Get cursor position in user coordinations."""
# check position
if self.getCursorLocation() != 'plot':
return False
# return current position
return self.cursorPosition[0], self.cursorPosition[1]
# ----
def getDistance(self):
"""Get current cursor distance."""
# check event
if self.mouseEvent != 'distance':
return False
# get distance coord in user units
x1 = self.draggingStart[0]
y1 = self.draggingStart[1]
x2 = self.cursorPosition[0]
y2 = self.cursorPosition[1]
# return distance
return [x2-x1, y2-y1]
# ----
def getCharge(self):
"""Get current charge."""
return self.currentCharge
# ----
def getIsotopes(self):
"""Get current isotopes."""
# check event
if self.mouseEvent != 'isotopes':
return False
# check position
if self.getCursorLocation() != 'plot':
return False
# return current isotopes
return self.currentIsotopes[:]
# ----
def getPoint(self, xPos=None, coord='screen'):
"""Get corresponding data point from current object and xPos."""
# check current object
if self.currentObject == None:
return None
# get corresponding point
graphics = self.lastDraw[0]
point = graphics.getPoint(self.currentObject, xPos, coord)
return point
# ----
def getSelectionBox(self):
"""Get selection rectangle coordinations."""
# check position
if not self.mouseEvent in ('rectangle', 'range'):
return False
# get coordinations
x1 = min(self.draggingStart[0], self.cursorPosition[0])
y1 = min(self.draggingStart[1], self.cursorPosition[1])
x2 = max(self.draggingStart[0], self.cursorPosition[0])
y2 = max(self.draggingStart[1], self.cursorPosition[1])
return x1, y1, x2, y2
# ----
def setSize(self, width=None, height=None):
"""Set DC width and height."""
# get size
if width == None:
(width, height) = self.GetClientSize()