-
Notifications
You must be signed in to change notification settings - Fork 2
/
lathehandler.py
218 lines (186 loc) · 7.48 KB
/
lathehandler.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
#!/usr/bin/env python3
# vim: sts=4 sw=4 et
# This is a component of EMC
# savestate.py copyright 2013 Andy Pugh
# based on code from
# probe.py Copyright 2010 Michael Haberler
#
#
# 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 2 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA''''''
import os,sys
from gladevcp.persistence import IniFile,widget_defaults,set_debug,select_widgets
import hal
import hal_glib
import glib
import linuxcnc
import cairo
import signal
import re
import gi
gi.require_version('Rsvg', '2.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import Pango
from gi.repository import Rsvg
from gi.repository import GdkPixbuf
debug = 0
notouch = 0
norun = 0
svgfile = os.path.join(os.path.dirname(__file__), "LatheMacro.svg")
class HandlerClass:
active = False
tab_num = 0
def on_expose(self,nb,data=None):
tab_num = nb.get_current_page()
tab = nb.get_nth_page(tab_num)
alloc = tab.get_allocation()
x, y, w, h = (alloc.x, alloc.y, alloc.width, alloc.height)
pixbuf = self.svg.get_pixbuf_sub(f'#layer{tab_num}').scale_simple(w-10, h-10, GdkPixbuf.InterpType.BILINEAR)
im = self.builder.get_object(f'Image{tab_num}')
im.set_from_pixbuf(pixbuf)
for c in im.get_parent().get_children():
if c.get_has_tooltip():
m = re.findall(r'<!--(\d+),(\d+)-->', c.get_tooltip_markup())
if len(m) > 0:
x1 = int(m[0][0]); y1 = int(m[0][1])
c.set_margin_left(max(0, w * x1/1500))
c.set_margin_top(max(0, h * y1/1000))
# decide if our window is active to mask the cycle-start hardware button
# FIXME: This is probably not as reliable as one might wish.
def event(self,w,event):
if w.is_active():
if w.has_toplevel_focus() :
self.active = True
else:
self.active = False
# Capture notify events
def on_map_event(self, widget, data=None):
top = widget.get_toplevel()
top.connect('notify', self.event)
def on_destroy(self,obj,data=None):
self.ini.save_state(self)
def on_restore_defaults(self,button,data=None):
'''
example callback for 'Reset to defaults' button
currently unused
'''
self.ini.create_default_ini()
self.ini.restore_state(self)
def __init__(self, halcomp,builder,useropts):
self.halcomp = halcomp
self.builder = builder
self.ini_filename = 'savestate.sav'
self.defaults = { IniFile.vars: dict(),
IniFile.widgets : widget_defaults(select_widgets(self.builder.get_objects(),
hal_only=False,output_only = True))
}
self.ini = IniFile(self.ini_filename,self.defaults,self.builder)
self.ini.restore_state(self)
# A pin to use a physical switch to start the cycle
self.cycle_start = hal_glib.GPin(halcomp.newpin('cycle-start', hal.HAL_BIT, hal.HAL_IN))
self.cycle_start.connect('value-changed', self.cycle_pin)
# This catches the signal from Touchy to say that the tab is exposed
t = self.builder.get_object('macrobox')
t.connect('map-event',self.on_map_event)
t.add_events(Gdk.EventMask.STRUCTURE_MASK)
self.cmd = linuxcnc.command()
# This connects the expose event to re-draw and scale the SVG frames
t = self.builder.get_object('tabs1')
t.connect_after("draw", self.on_expose)
t.connect("destroy", Gtk.main_quit)
t.add_events(Gdk.EventMask.STRUCTURE_MASK)
self.svg = Rsvg.Handle().new_from_file(svgfile)
self.active = True
# handle Useropts
if norun:
for c in range(0,7):
self.builder.get_object(f'tab{c}.action').set_visible(False)
def show_keyb(self, obj, data=None):
if notouch: return False
self.active_ctrl = obj
self.keyb = self.builder.get_object('keyboard')
self.entry = self.builder.get_object('entry1')
self.entry.modify_font(Pango.FontDescription("courier 42"))
self.entry.set_text("")
resp = self.keyb.run()
return True
def keyb_prev_click(self, obj, data=None):
self.entry.set_text(self.active_ctrl.get_text())
def keyb_number_click(self, obj, data=None):
data = self.entry.get_text()
data = data + obj.get_label()
if any( x in data for x in [ '/2', '/4', '/8', '/16', '/32', '/64', '/128']):
v = [0] + [float(x) for x in data.replace('/','.').split('.')]
data = f'{v[-3] + v[-2]/v[-1]:6.7}'
self.entry.set_text(data)
def keyb_pm_click(self, obj, data=None):
data = self.entry.get_text()
if not data:
data = "-"
elif data[0] == '-':
data = data[1:]
else:
data = '-' + data
self.entry.set_text(data)
def keyb_convert_click(self, obj, data=None):
v = float(self.entry.get_text())
op = obj.get_label()
if op == 'in->mm':
self.entry.set_text(f'{v * 25.4:6.4}')
elif op == 'mm->in':
self.entry.set_text(f'{v / 25.4:6.4}')
elif op == 'tpi->pitch':
self.entry.set_text(f'{25.4 / v:6.4}')
elif op == 'pitch->tpi':
self.entry.set_text(f'{25.4 / v:6.4}')
def keyb_del_click(self, obj, data=None):
data = self.entry.get_text()
data = data[:-1]
self.entry.set_text(data)
def keyb_clear_click(self, obj, data=None):
self.entry.set_text('')
def keyb_cancel_click(self, obj, data=None):
self.keyb.hide()
def keyb_ok_click(self, obj, data=None):
if self.entry.get_text() != '':
self.active_ctrl.set_value(float(self.entry.get_text()))
self.keyb.hide()
def set_alpha(self, obj, data = None):
cr = obj.get_property('window').cairo_create()
cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
def cycle_pin(self, pin, data = None):
if pin.get() == 0:
return
if self.active:
nb = self.builder.get_object('tabs1')
print('current tab', nb.get_current_page())
c = self.builder.get_object(f"tab{nb.get_current_page()}.action")
if c is not None:
self.cmd.abort()
self.cmd.mode(linuxcnc.MODE_MDI)
self.cmd.wait_complete()
c.emit('clicked')
print(c.get_name(), "clicked")
def testing(self, obj, data = None):
print('event', data)
def get_handlers(halcomp,builder,useropts):
global debug
for cmd in useropts:
print(cmd)
exec(cmd, globals())
set_debug(debug)
return [HandlerClass(halcomp,builder,useropts)]