Create keypad widget and include set temp on the main menu

This commit is contained in:
Jordan 2021-12-04 15:13:31 -05:00 committed by jordanruthe
parent e26e4006d6
commit dd56d192cc
4 changed files with 181 additions and 109 deletions

View File

@ -0,0 +1,79 @@
import datetime
import gi
import logging
import math
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Pango
class Keypad(Gtk.Box):
def __init__(self, screen, change_temp, close_function):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
_ = screen.lang.gettext
self.labels = {}
self.change_temp = change_temp
self.screen = screen
self._gtk = screen.gtk
numpad = self._gtk.HomogeneousGrid()
numpad.set_direction(Gtk.TextDirection.LTR)
keys = [
['1', 'numpad_tleft'],
['2', 'numpad_top'],
['3', 'numpad_tright'],
['4', 'numpad_left'],
['5', 'numpad_button'],
['6', 'numpad_right'],
['7', 'numpad_left'],
['8', 'numpad_button'],
['9', 'numpad_right'],
['B', 'numpad_bleft'],
['0', 'numpad_bottom'],
['E', 'numpad_bright']
]
for i in range(len(keys)):
id = 'button_' + str(keys[i][0])
if keys[i][0] == "B":
self.labels[id] = self._gtk.ButtonImage("backspace", None, None, 1, 1)
elif keys[i][0] == "E":
self.labels[id] = self._gtk.ButtonImage("complete", None, None, 1, 1)
else:
self.labels[id] = Gtk.Button(keys[i][0])
self.labels[id].connect('clicked', self.update_entry, keys[i][0])
self.labels[id].get_style_context().add_class(keys[i][1])
numpad.attach(self.labels[id], i % 3, i/3, 1, 1)
self.labels["keypad"] = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.labels['entry'] = Gtk.Entry()
self.labels['entry'].props.xalign = 0.5
ctx = self.labels['entry'].get_style_context()
b = self._gtk.ButtonImage('cancel', _('Close'), None, 1, 1)
b.connect("clicked", close_function)
self.add(self.labels['entry'])
self.add(numpad)
self.add(b)
self.labels["keypad"] = numpad
def clear(self):
self.labels['entry'].set_text("")
def update_entry(self, widget, digit):
text = self.labels['entry'].get_text()
if digit == 'B':
if len(text) < 1:
return
self.labels['entry'].set_text(text[0:-1])
elif digit == 'E':
temp = int(text)
self.change_temp(temp)
self.labels['entry'].set_text("")
else:
if len(text) >= 3:
return
self.labels['entry'].set_text(text + digit)

View File

@ -6,7 +6,10 @@ import logging
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Pango
from panels.menu import MenuPanel
from ks_includes.KlippyGcodes import KlippyGcodes
from ks_includes.widgets.graph import HeaterGraph
from ks_includes.widgets.keypad import Keypad
def create_panel(*args):
return MainPanel(*args)
@ -16,6 +19,7 @@ class MainPanel(MenuPanel):
super().__init__(screen, title, False)
self.devices = {}
self.graph_update = None
self.active_heater = None
def initialize(self, panel_name, items, extrudercount):
print("### Making MainMenu")
@ -33,7 +37,8 @@ class MainPanel(MenuPanel):
leftpanel = self.create_left_panel()
grid.attach(leftpanel, 0, 0, 1, 1)
grid.attach(self.arrangeMenuItems(items, 2, True), 1, 0, 1, 1)
self.labels['menu'] = self.arrangeMenuItems(items, 2, True)
grid.attach(self.labels['menu'], 1, 0, 1, 1)
self.grid = grid
@ -125,43 +130,22 @@ class MainPanel(MenuPanel):
pos = devices.index(device) + 1
self.labels['devices'].insert_row(pos)
# self.labels['devices'].attach(name['b'], 0, pos, 1, 1)
self.labels['devices'].attach(name, 0, pos, 1, 1)
self.labels['devices'].attach(temp, 1, pos, 1, 1)
if can_target:
self.labels['devices'].attach(target, 2, pos, 1, 1)
self.labels['devices'].show_all()
def on_popover_clicked(self, widget, device):
self.popover_device = device
po = self.labels['popover']
po.set_relative_to(widget)
self.popover_populate_menu()
po.show_all()
def popover_populate_menu(self):
pobox = self.labels['popover_vbox']
for child in pobox.get_children():
pobox.remove(child)
if self.labels['da'].is_showing(self.popover_device):
pobox.pack_start(self.labels['graph_hide'], True, True, 5)
def change_target_temp(self, temp):
if self.active_heater.startswith('heater_generic '):
self._screen._ws.klippy.set_heater_temp(" ".join(self.active_heater.split(" ")[1:]), temp)
elif self.active_heater == "heater_bed":
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_BED_TEMP else temp
self._screen._ws.klippy.set_bed_temp(temp)
else:
pobox.pack_start(self.labels['graph_show'], True, True, 5)
def graph_show_device(self, widget, show=True):
logging.info("Graph show: %s %s" % (self.popover_device, show))
self.labels['da'].set_showing(self.popover_device, show)
if show:
self.devices[self.popover_device]['name'].get_style_context().remove_class("graph_label_hidden")
self.devices[self.popover_device]['name'].get_style_context().add_class(
self.devices[self.popover_device]['class'])
else:
self.devices[self.popover_device]['name'].get_style_context().remove_class(
self.devices[self.popover_device]['class'])
self.devices[self.popover_device]['name'].get_style_context().add_class("graph_label_hidden")
self.labels['da'].queue_draw()
self.popover_populate_menu()
self.labels['popover'].show_all()
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_EXT_TEMP else temp
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(self.active_heater), temp)
self._printer.set_dev_stat(self.active_heater, "target", temp)
def create_left_panel(self):
_ = self.lang.gettext
@ -189,9 +173,11 @@ class MainPanel(MenuPanel):
box.add(da)
self.labels['graph_hide'] = self._gtk.Button(label="Hide")
self.labels['graph_settemp'] = self._gtk.Button(label=_("Set Temp"))
self.labels['graph_settemp'].connect("clicked", self.show_numpad)
self.labels['graph_hide'] = self._gtk.Button(label=_("Hide"))
self.labels['graph_hide'].connect("clicked", self.graph_show_device, False)
self.labels['graph_show'] = self._gtk.Button(label="Show")
self.labels['graph_show'] = self._gtk.Button(label=_("Show"))
self.labels['graph_show'].connect("clicked", self.graph_show_device)
popover = Gtk.Popover()
@ -207,6 +193,48 @@ class MainPanel(MenuPanel):
return box
def graph_show_device(self, widget, show=True):
logging.info("Graph show: %s %s" % (self.popover_device, show))
self.labels['da'].set_showing(self.popover_device, show)
if show:
self.devices[self.popover_device]['name'].get_style_context().remove_class("graph_label_hidden")
self.devices[self.popover_device]['name'].get_style_context().add_class(
self.devices[self.popover_device]['class'])
else:
self.devices[self.popover_device]['name'].get_style_context().remove_class(
self.devices[self.popover_device]['class'])
self.devices[self.popover_device]['name'].get_style_context().add_class("graph_label_hidden")
self.labels['da'].queue_draw()
self.popover_populate_menu()
self.labels['popover'].show_all()
def hide_numpad(self, widget):
self.devices[self.active_heater]['name'].get_style_context().remove_class("active_device")
self.active_heater = None
self.grid.remove_column(1)
self.grid.attach(self.labels['menu'], 1, 0, 1, 1)
self.grid.show_all()
def on_popover_clicked(self, widget, device):
self.popover_device = device
po = self.labels['popover']
po.set_relative_to(widget)
self.popover_populate_menu()
po.show_all()
def popover_populate_menu(self):
pobox = self.labels['popover_vbox']
for child in pobox.get_children():
pobox.remove(child)
if self.labels['da'].is_showing(self.popover_device):
pobox.pack_start(self.labels['graph_hide'], True, True, 5)
pobox.pack_start(self.labels['graph_settemp'], True, True, 5)
else:
pobox.pack_start(self.labels['graph_show'], True, True, 5)
pobox.pack_start(self.labels['graph_settemp'], True, True, 5)
def process_update(self, action, data):
if action != "notify_status_update":
return
@ -225,6 +253,24 @@ class MainPanel(MenuPanel):
)
return
def show_numpad(self, widget):
_ = self.lang.gettext
if self.active_heater is not None:
self.devices[self.active_heater]['name'].get_style_context().remove_class("active_device")
self.active_heater = self.popover_device
self.devices[self.active_heater]['name'].get_style_context().add_class("active_device")
if "keypad" not in self.labels:
self.labels["keypad"] = Keypad(self._screen, self.change_target_temp, self.hide_numpad)
self.labels["keypad"].clear()
self.grid.remove_column(1)
self.grid.attach(self.labels["keypad"], 1, 0, 1, 1)
self.grid.show_all()
self.labels['popover'].popdown()
def update_graph(self):
self.labels['da'].queue_draw()
alloc = self.labels['devices'].get_allocation()

View File

@ -7,6 +7,7 @@ from gi.repository import Gtk, Gdk, GLib
from ks_includes.KlippyGcodes import KlippyGcodes
from ks_includes.screen_panel import ScreenPanel
from ks_includes.widgets.keypad import Keypad
def create_panel(*args):
return TemperaturePanel(*args)
@ -58,9 +59,9 @@ class TemperaturePanel(ScreenPanel):
self.labels["control_grid"] = self._gtk.HomogeneousGrid()
self.labels["increase"] = self._gtk.ButtonImage("increase", _("Increase"), "color1")
self.labels["increase"].connect("clicked", self.change_target_temp, "+")
self.labels["increase"].connect("clicked", self.change_target_temp_incremental, "+")
self.labels["decrease"] = self._gtk.ButtonImage("decrease", _("Decrease"), "color3")
self.labels["decrease"].connect("clicked", self.change_target_temp, "-")
self.labels["decrease"].connect("clicked", self.change_target_temp_incremental, "-")
self.labels["npad"] = self._gtk.ButtonImage("hashtag", _("Number Pad"), "color2")
self.labels["npad"].connect("clicked", self.show_numpad)
@ -119,53 +120,12 @@ class TemperaturePanel(ScreenPanel):
def show_numpad(self, widget):
_ = self.lang.gettext
numpad = self._gtk.HomogeneousGrid()
numpad.set_direction(Gtk.TextDirection.LTR)
keys = [
['1', 'numpad_tleft'],
['2', 'numpad_top'],
['3', 'numpad_tright'],
['4', 'numpad_left'],
['5', 'numpad_button'],
['6', 'numpad_right'],
['7', 'numpad_left'],
['8', 'numpad_button'],
['9', 'numpad_right'],
['B', 'numpad_bleft'],
['0', 'numpad_bottom'],
['E', 'numpad_bright']
]
for i in range(len(keys)):
id = 'button_' + str(keys[i][0])
if keys[i][0] == "B":
self.labels[id] = self._gtk.ButtonImage("backspace", None, None, 1, 1)
elif keys[i][0] == "E":
self.labels[id] = self._gtk.ButtonImage("complete", None, None, 1, 1)
else:
self.labels[id] = Gtk.Button(keys[i][0])
self.labels[id].connect('clicked', self.update_entry, keys[i][0])
ctx = self.labels[id].get_style_context()
ctx.add_class(keys[i][1])
numpad.attach(self.labels[id], i % 3, i/3, 1, 1)
self.labels["keypad"] = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.labels['entry'] = Gtk.Entry()
self.labels['entry'].props.xalign = 0.5
ctx = self.labels['entry'].get_style_context()
b = self._gtk.ButtonImage('cancel', _('Close'), None, 1, 1)
b.connect("clicked", self.hide_numpad)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box.add(self.labels['entry'])
box.add(numpad)
box.add(b)
self.labels["keypad"] = numpad
if "keypad" not in self.labels:
self.labels["keypad"] = Keypad(self._screen, self.change_target_temp, self.hide_numpad)
self.labels["keypad"].clear()
self.grid.remove_column(1)
self.grid.attach(box, 1, 0, 1, 1)
self.grid.attach(self.labels["keypad"], 1, 0, 1, 1)
self.grid.show_all()
def hide_numpad(self, widget):
@ -206,7 +166,18 @@ class TemperaturePanel(ScreenPanel):
)
return
def change_target_temp(self, widget, dir):
def change_target_temp(self, temp):
if self.active_heater.startswith('heater_generic '):
self._screen._ws.klippy.set_heater_temp(" ".join(self.active_heater.split(" ")[1:]), temp)
elif self.active_heater == "heater_bed":
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_BED_TEMP else temp
self._screen._ws.klippy.set_bed_temp(temp)
else:
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_EXT_TEMP else temp
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(self.active_heater), temp)
self._printer.set_dev_stat(self.active_heater, "target", temp)
def change_target_temp_incremental(self, widget, dir):
logging.debug("Dev stats %s: %s" % (self.active_heater, self._printer.get_dev_stats(self.active_heater)))
target = self._printer.get_dev_stat(self.active_heater, "target")
if dir == "+":
@ -228,31 +199,3 @@ class TemperaturePanel(ScreenPanel):
self._screen._ws.klippy.set_temp_fan_temp(" ".join(self.active_heater.split(" ")[1:]), target)
else:
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(self.active_heater), target)
def update_entry(self, widget, digit):
text = self.labels['entry'].get_text()
if digit == 'B':
if len(text) < 1:
return
self.labels['entry'].set_text(text[0:-1])
elif digit == 'E':
if self.active_heater.startswith('heater_generic '):
temp = int(text)
self._screen._ws.klippy.set_heater_temp(" ".join(self.active_heater.split(" ")[1:]), temp)
elif self.active_heater == "heater_bed":
temp = int(text)
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_BED_TEMP else temp
self._screen._ws.klippy.set_bed_temp(temp)
elif self.active_heater.startswith("temperature_fan "):
temp = int(text)
self._screen._ws.klippy.set_temp_fan_temp(" ".join(self.active_heater.split(" ")[1:]), temp)
else:
temp = int(text)
temp = 0 if temp < 0 or temp > KlippyGcodes.MAX_EXT_TEMP else temp
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(self.active_heater), temp)
self._printer.set_dev_stat(self.active_heater, "target", temp)
self.labels['entry'].set_text("")
else:
if len(text) >= 3:
return
self.labels['entry'].set_text(text + digit)

View File

@ -172,6 +172,10 @@ trough {
/*border-right: 2px solid #444;*/
}
.active_device {
background-color: #20303D;
}
.dialog {
border: .1em solid black;
padding: 2.5em;