From a83bbed85a70d9c0ec9db0c72457da4f6740498c Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Tue, 4 Oct 2022 10:52:44 -0300 Subject: [PATCH] exclude: graph (#743) Also support name wrapping and remove the object from the list if it was excluded (easier to see) --- ks_includes/widgets/objectmap.py | 150 +++++++++++++++++++++++++++++++ panels/exclude.py | 87 +++++++++++++----- screen.py | 5 +- 3 files changed, 217 insertions(+), 25 deletions(-) create mode 100644 ks_includes/widgets/objectmap.py diff --git a/ks_includes/widgets/objectmap.py b/ks_includes/widgets/objectmap.py new file mode 100644 index 00000000..b9ad10de --- /dev/null +++ b/ks_includes/widgets/objectmap.py @@ -0,0 +1,150 @@ +import logging + +import gi + +gi.require_version("Gtk", "3.0") +from gi.repository import Gdk, Gtk + + +class ObjectMap(Gtk.DrawingArea): + def __init__(self, screen, printer, font_size): + super().__init__() + self._screen = screen + self.set_hexpand(True) + self.set_vexpand(True) + # self.get_style_context().add_class('objectmap') + self.printer = printer + self.max_length = 0 + self.connect('draw', self.draw_graph) + self.add_events(Gdk.EventMask.TOUCH_MASK) + self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) + self.connect('button_press_event', self.event_cb) + self.font_size = round(font_size * 0.75) + self.font_spacing = round(self.font_size * 1.5) + self.margin_left = round(self.font_size * 2.75) + self.margin_right = 15 + self.margin_top = 10 + self.margin_bottom = self.font_size * 2 + self.objects = self.printer.get_stat("exclude_object", "objects") + self.current_object = self.printer.get_stat("current_object", "current_object") + self.excluded_objects = self.printer.get_stat("exclude_object", "excluded_objects") + self.min_x = self.min_y = 99999999 + self.max_x = self.max_y = 0 + + def x_graph_to_bed(self, width, gx): + return (((gx - self.margin_left) * (self.max_x - self.min_x)) + / (width - self.margin_left - self.margin_right)) + self.min_x + + def y_graph_to_bed(self, height, gy): + return ((1 - ((gy - self.margin_top) / (height - self.margin_top - self.margin_bottom))) + * (self.max_y - self.min_y)) + self.min_y + + def event_cb(self, da, ev): + # Convert coordinates from screen-graph to bed + x = self.x_graph_to_bed(da.get_allocated_width(), ev.x) + y = self.y_graph_to_bed(da.get_allocated_height(), ev.y) + logging.info(f"Touched GRAPH {ev.x:.0f},{ev.y:.0f} BED: {x:.0f},{y:.0f}") + + for obj in self.objects: + obj_min_x = obj_max_x = obj["polygon"][0][0] + obj_min_y = obj_max_y = obj["polygon"][0][1] + for point in obj["polygon"]: + obj_min_x = min(obj_min_x, point[0]) + obj_min_y = min(obj_min_y, point[1]) + obj_max_x = max(obj_max_x, point[0]) + obj_max_y = max(obj_max_y, point[1]) + if obj_min_x < x < obj_max_x and obj_min_y < y < obj_max_y: + logging.info(f"TOUCHED object it's: {obj['name']}") + if obj['name'] not in self.excluded_objects: + self.exclude_object(obj['name']) + break + + def exclude_object(self, name): + script = {"script": f"EXCLUDE_OBJECT NAME={name}"} + self._screen._confirm_send_action( + None, + _("Are you sure do you want to exclude the object?") + f"\n\n{name}", + "printer.gcode.script", + script + ) + + def draw_graph(self, da, ctx): + right = da.get_allocated_width() - self.margin_right + bottom = da.get_allocated_height() - self.margin_bottom + self.objects = self.printer.get_stat("exclude_object", "objects") + + for obj in self.objects: + for point in obj["polygon"]: + # Find min coords + self.min_x = min(self.min_x, point[0]) + self.min_y = min(self.min_y, point[1]) + # Find max coords + self.max_x = max(self.max_x, point[0]) + self.max_y = max(self.max_y, point[1]) + + # Styling + ctx.set_source_rgb(.5, .5, .5) # Grey + ctx.set_line_width(1) + ctx.set_font_size(self.font_size) + + # Borders + ctx.move_to(self.margin_left, self.margin_top) + # logging.info(f"l:{self.margin_left:.0f} t:{self.margin_top:.0f} r:{right:.0f} b:{bottom:.0f}") + ctx.line_to(right, self.margin_top) + ctx.line_to(right, bottom) + ctx.line_to(self.margin_left, bottom) + ctx.line_to(self.margin_left, self.margin_top) + ctx.stroke() + + # Axis labels + ctx.move_to(0, bottom + self.font_spacing) + ctx.show_text(f"{self.min_x:.0f},{self.min_y:.0f}") + ctx.stroke() + ctx.move_to(right - self.font_spacing * 2, bottom + self.font_spacing) + ctx.show_text(f"{self.max_x:.0f},{self.min_y:.0f}") + ctx.stroke() + ctx.move_to(0, self.font_spacing / 2) + ctx.show_text(f"{self.min_x:.0f},{self.max_y:.0f}") + ctx.stroke() + + # middle markers + midx = (right - self.margin_left) / 2 + self.margin_left + ctx.set_dash([1, 1]) + ctx.move_to(midx, self.margin_top) + ctx.line_to(midx, bottom) + ctx.stroke() + midy = (self.margin_top - bottom) / 2 + bottom + ctx.move_to(self.margin_left, midy) + ctx.line_to(right, midy) + ctx.stroke() + ctx.set_dash([1, 0]) + + # Draw objects + for obj in self.objects: + # change the color depending on the status + if obj['name'] == self.printer.get_stat("exclude_object", "current_object"): + ctx.set_source_rgb(1, 0, 0) # Red + elif obj['name'] in self.printer.get_stat("exclude_object", "excluded_objects"): + ctx.set_source_rgb(0, 0, 0) # Black + else: + ctx.set_source_rgb(.5, .5, .5) # Grey + for i, point in enumerate(obj["polygon"]): + # Convert coordinates from bed to screen-graph + x = self.x_bed_to_graph(da.get_allocated_width(), point[0]) + y = self.y_bed_to_graph(da.get_allocated_height(), point[1]) + if i == 0: + ctx.move_to(x, y) + continue + ctx.line_to(x, y) + # logging.info(f"obj graph: {x=:.0f},{y=:.0f} bed: {point[0]:.0f},{point[1]:.0f}") + ctx.close_path() + ctx.fill() + ctx.stroke() + + def x_bed_to_graph(self, width, bx): + return (((bx - self.min_x) * (width - self.margin_left - self.margin_right)) + / (self.max_x - self.min_x)) + self.margin_left + + def y_bed_to_graph(self, height, by): + return ((1 - ((by - self.min_y) / (self.max_y - self.min_y))) + * (height - self.margin_top - self.margin_bottom)) + self.margin_top diff --git a/panels/exclude.py b/panels/exclude.py index 04207c07..19fd4062 100644 --- a/panels/exclude.py +++ b/panels/exclude.py @@ -1,11 +1,13 @@ -import gi import contextlib import logging +import gi + gi.require_version("Gtk", "3.0") -from gi.repository import Gtk +from gi.repository import Gtk, Pango from ks_includes.screen_panel import ScreenPanel +from ks_includes.widgets.objectmap import ObjectMap def create_panel(*args): @@ -15,48 +17,65 @@ def create_panel(*args): class ExcludeObjectPanel(ScreenPanel): def __init__(self, screen, title, back=True): super().__init__(screen, title, back) + self._screen = screen self.object_list = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0) self.object_list.set_valign(Gtk.Align.CENTER) - self.object_list.set_halign(Gtk.Align.CENTER) + self.object_list.set_halign(Gtk.Align.START) self.buttons = {} - self.current_object = self._gtk.ButtonImage("extrude", "", scale=.66, - position=Gtk.PositionType.LEFT, lines=1) + self.current_object = self._gtk.ButtonImage("extrude", "", scale=.66, position=Gtk.PositionType.LEFT, lines=1) self.current_object.connect("clicked", self.exclude_current) - self.current_object.set_hexpand(True) self.current_object.set_vexpand(False) self.excluded_objects = self._printer.get_stat("exclude_object", "excluded_objects") logging.info(f'Excluded: {self.excluded_objects}') + self.objects = self._printer.get_stat("exclude_object", "objects") + self.labels['map'] = None def initialize(self, panel_name): - objects = self._printer.get_stat("exclude_object", "objects") - for obj in objects: + for obj in self.objects: logging.info(f"Adding {obj['name']}") self.add_object(obj["name"]) scroll = self._gtk.ScrolledWindow() scroll.add(self.object_list) scroll.set_halign(Gtk.Align.CENTER) - scroll.set_size_request(self._gtk.get_content_width(), 0) + scroll.set_size_request(self._gtk.get_content_width() // 2, 0) grid = Gtk.Grid() - grid.attach(self.current_object, 0, 0, 1, 1) - grid.attach(Gtk.Separator(), 0, 1, 1, 1) - grid.attach(scroll, 0, 2, 1, 1) + grid.attach(self.current_object, 0, 0, 2, 1) + grid.attach(Gtk.Separator(), 0, 1, 2, 1) + + if self.objects and "polygon" in self.objects[0]: + self.labels['map'] = ObjectMap(self._screen, self._printer, self._gtk.get_font_size()) + self.labels['map'].set_size_request(self._gtk.get_content_width() // 2, 0) + grid.attach(self.labels['map'], 0, 2, 1, 1) + grid.attach(scroll, 1, 2, 1, 1) + else: + grid.attach(scroll, 0, 2, 2, 1) + self.content.add(grid) self.content.show_all() def add_object(self, name): - if name not in self.buttons: - self.buttons[name] = self._gtk.ButtonImage("cancel", name, scale=.66, position=Gtk.PositionType.LEFT, - lines=1) + if name not in self.buttons and name not in self.excluded_objects: + self.buttons[name] = self._gtk.Button(name.replace("_", " ")) + self.buttons[name].get_children()[0].set_line_wrap_mode(Pango.WrapMode.CHAR) + self.buttons[name].get_children()[0].set_line_wrap(True) self.buttons[name].connect("clicked", self.exclude_object, name) self.buttons[name].set_hexpand(True) - if name in self.excluded_objects: - self.buttons[name].set_sensitive(False) - self.buttons[name].get_style_context().add_class("frame-item") - self.object_list.add(self.buttons[name]) + self.buttons[name].get_style_context().add_class("frame-item") + self.object_list.add(self.buttons[name]) def exclude_object(self, widget, name): + if len(self.buttons) == 1: + # Do not exclude the last object, this is a workaround for a bug of klipper that starts + # to move the toolhead really fast skipping gcode until the file ends + # Remove this if they fix it. + self._screen._confirm_send_action( + widget, + _("Are you sure you wish to cancel this print?"), + "printer.print.cancel", + ) + return script = {"script": f"EXCLUDE_OBJECT NAME={name}"} self._screen._confirm_send_action( widget, @@ -71,13 +90,35 @@ class ExcludeObjectPanel(ScreenPanel): def process_update(self, action, data): if action == "notify_status_update": with contextlib.suppress(KeyError): - self.current_object.set_label(f'{data["exclude_object"]["current_object"]}') + # Update objects + self.objects = data["exclude_object"]["objects"] + logging.info(f'Objects: {data["exclude_object"]["objects"]}') + for obj in self.buttons: + self.object_list.remove(self.buttons[obj]) + self.buttons = {} + for obj in self.objects: + logging.info(f"Adding {obj['name']}") + self.add_object(obj["name"]) with contextlib.suppress(KeyError): + # Update current objects + if data["exclude_object"]["current_object"]: + self.current_object.set_label(f'{data["exclude_object"]["current_object"].replace("_", " ")}') + self.update_graph() + with contextlib.suppress(KeyError): + # Update excluded objects logging.info(f'Excluded objects: {data["exclude_object"]["excluded_objects"]}') self.excluded_objects = data["exclude_object"]["excluded_objects"] for name in self.excluded_objects: - self.buttons[name].set_sensitive(False) - with contextlib.suppress(KeyError): - logging.info(f'Objects: {data["exclude_object"]["objects"]}') + if name in self.buttons: + self.object_list.remove(self.buttons[name]) + self.update_graph() elif action == "notify_gcode_response" and "Excluding object" in data: self._screen.show_popup_message(data, level=1) + self.update_graph() + + def activate(self): + self.update_graph() + + def update_graph(self): + if self.labels['map']: + self.labels['map'].queue_draw() diff --git a/screen.py b/screen.py index 8e21ad0f..7bd58e42 100644 --- a/screen.py +++ b/screen.py @@ -46,7 +46,9 @@ PRINTER_BASE_STATUS_OBJECTS = [ 'toolhead', 'virtual_sdcard', 'webhooks', - 'motion_report' + 'motion_report', + 'firmware_retraction', + 'exclude_object', ] klipperscreendir = pathlib.Path(__file__).parent.resolve() @@ -405,7 +407,6 @@ class KlipperScreen(Gtk.Window): self.base_panel.get().remove(self.popup_message) self.popup_message = None - self.show_all() def show_error_modal(self, err, e=""): logging.exception(f"Showing error modal: {err}")