1
+ from kivy .utils import platform
2
+
3
+ #avoid conflict between mouse provider and touch (very important with touch device)
4
+ #no need for android platform
5
+ if platform != 'android' :
6
+ from kivy .config import Config
7
+ Config .set ('input' , 'mouse' , 'mouse,disable_on_activity' )
8
+
9
+ from kivy .lang import Builder
10
+ from kivy .app import App
11
+ from graph_generator import GraphGenerator ,f
12
+ import numpy as np
13
+ from kivy .clock import Clock
14
+ from functools import partial
15
+ import kivy_matplotlib_widget #register all widgets to kivy register
16
+ from kivy .uix .slider import Slider
17
+
18
+ KV = '''
19
+
20
+ Screen
21
+ figure_wgt:figure_wgt
22
+ slider_amp:slider_amp
23
+ slider_freq:slider_freq
24
+ BoxLayout:
25
+ orientation:'vertical'
26
+ BoxLayout:
27
+ size_hint_y:0.2
28
+ Button:
29
+ text:"home"
30
+ on_release:app.home()
31
+ ToggleButton:
32
+ group:'touch_mode'
33
+ state:'down'
34
+ text:"pan"
35
+ on_release:
36
+ app.set_touch_mode('pan')
37
+ self.state='down'
38
+ ToggleButton:
39
+ group:'touch_mode'
40
+ text:"zoom box"
41
+ on_release:
42
+ app.set_touch_mode('zoombox')
43
+ self.state='down'
44
+ BoxLayout:
45
+ BoxLayout:
46
+ size_hint_x:0.2
47
+ orientation:'vertical'
48
+ canvas.before:
49
+ Color:
50
+ rgba:1,1,1,1
51
+ Rectangle:
52
+ pos:self.pos
53
+ size:self.size
54
+ BoxLayout:
55
+ size_hint_y:0.06
56
+ Label:
57
+ text: "Amplitude"
58
+ color:0,0,0,1
59
+ BoxLayout:
60
+ MySlider:
61
+ id: slider_amp
62
+ padding:0
63
+ orientation:'vertical'
64
+ min: 0.1
65
+ max: 10
66
+ step: 0.1
67
+ value_track:True
68
+ value_track_color:[0, 0, 1, 0.5]
69
+ value:5
70
+
71
+ BoxLayout:
72
+ size_hint_y:0.25
73
+ Label:
74
+ text: "{:.1f}".format(slider_amp.value)
75
+ color:0,0,0,1
76
+
77
+ BoxLayout:
78
+ padding:-dp(20),0,0,0
79
+ MatplotFigure:
80
+ id:figure_wgt
81
+ BoxLayout:
82
+ size_hint_y:0.2
83
+ canvas.before:
84
+ Color:
85
+ rgba:1,1,1,1
86
+ Rectangle:
87
+ pos:self.pos
88
+ size:self.size
89
+
90
+ BoxLayout:
91
+ size_hint_x:0.28
92
+ Label:
93
+ text: "Frequency [Hz]"
94
+ color:0,0,0,1
95
+ BoxLayout:
96
+ MySlider:
97
+ id: slider_freq
98
+ padding:0
99
+ min: 0.1
100
+ max: 30
101
+ step: 0.1
102
+ value_track:True
103
+ value_track_color:[0, 0, 1, 0.5]
104
+ value:3
105
+
106
+ BoxLayout:
107
+ size_hint_x:0.08
108
+ Label:
109
+ text: "{:.1f}".format(slider_freq.value)
110
+ color:0,0,0,1
111
+
112
+ <MySlider@Slider>:
113
+ canvas.before:
114
+ Color:
115
+ rgb: 0, 0, 1
116
+ BorderImage:
117
+ border: self.border_horizontal if self.orientation == 'horizontal' else self.border_vertical
118
+ pos: (self.x + self.padding, self.center_y - self.background_width / 2) if self.orientation == 'horizontal' else (self.center_x - self.background_width / 2, self.y + self.padding)
119
+ size: (self.width - self.padding * 2, self.background_width) if self.orientation == 'horizontal' else (self.background_width, self.height - self.padding * 2)
120
+ source: (self.background_disabled_horizontal if self.orientation == 'horizontal' else self.background_disabled_vertical) if self.disabled else (self.background_horizontal if self.orientation == 'horizontal' else self.background_vertical)
121
+ '''
122
+
123
+
124
+ class Test (App ):
125
+ lines = []
126
+ current_idx = 0
127
+
128
+ def build (self ):
129
+ self .screen = Builder .load_string (KV )
130
+ return self .screen
131
+
132
+ def on_start (self , * args ):
133
+ mygraph = GraphGenerator ()
134
+
135
+ self .screen .figure_wgt .figure = mygraph .fig
136
+ self .screen .figure_wgt .axes = mygraph .ax1
137
+ self .screen .figure_wgt .xmin = mygraph .xmin
138
+ self .screen .figure_wgt .xmax = mygraph .xmax
139
+ self .screen .figure_wgt .ymin = mygraph .ymin
140
+ self .screen .figure_wgt .ymax = mygraph .ymax
141
+ self .screen .figure_wgt .fast_draw = False #update axis during pan/zoom
142
+
143
+ #register lines instance if need to be update
144
+ self .lines .append (mygraph .line1 )
145
+
146
+ self .screen .slider_freq .bind (value = self .update_freq )
147
+ self .screen .slider_amp .bind (value = self .update_amp )
148
+
149
+
150
+
151
+ def set_touch_mode (self ,mode ):
152
+ self .screen .figure_wgt .touch_mode = mode
153
+
154
+ def update_freq (self ,instance ,val ):
155
+ self .lines [0 ].set_ydata (f (self .lines [0 ].get_xdata (), self .screen .slider_amp .value , val ))
156
+ self .screen .figure_wgt .axes .figure .canvas .draw_idle ()
157
+ self .screen .figure_wgt .axes .figure .canvas .flush_events ()
158
+
159
+ def update_amp (self ,instance ,val ):
160
+ self .lines [0 ].set_ydata (f (self .lines [0 ].get_xdata (), val , self .screen .slider_freq .value ))
161
+ self .screen .figure_wgt .axes .figure .canvas .draw_idle ()
162
+ self .screen .figure_wgt .axes .figure .canvas .flush_events ()
163
+
164
+ def home (self ):
165
+ self .screen .figure_wgt .home ()
166
+
167
+ Test ().run ()
0 commit comments