diff --git a/ks_includes/config.py b/ks_includes/config.py index c7f66fba..34c59fcc 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -132,7 +132,8 @@ class KlipperScreenConfig: {"name": _("Colorized"), "value": "colorized"} ]}}, {"24htime": {"section": "main", "name": _("24 Hour Time"), "type": "binary", "value": "True"}}, - {"side_macro_shortcut": {"section": "main", "name": _("Macro shortcut on sidebar"), "type": "binary", "value": "True"}}, + {"side_macro_shortcut": {"section": "main", "name": _("Macro shortcut on sidebar"), "type": "binary", + "value": "True", "callback": screen.toggle_macro_shortcut}}, #{"": {"section": "main", "name": _(""), "type": ""}} ] diff --git a/panels/base_panel.py b/panels/base_panel.py index 77cc78cd..d7766931 100644 --- a/panels/base_panel.py +++ b/panels/base_panel.py @@ -19,7 +19,8 @@ class BasePanel(ScreenPanel): self.title_spacing = self._screen.font_size * 2 self.buttons_showing = { - 'back': False if back else True + 'back': False if back else True, + 'macros_shortcut': False } self.layout = Gtk.Layout() @@ -39,33 +40,43 @@ class BasePanel(ScreenPanel): self.control['home'] = self._gtk.ButtonImage('main', None, None, button_scale[0], button_scale[1]) self.control['home'].connect("clicked", self.menu_return, True) - #if back == True: - # self.control_grid.attach(self.control['back'], 0, 0, 1, 1) - # self.control_grid.attach(self.control['home'], 0, 1, 1, 1) - #else: - for i in range(2): - self.control['space%s' % i] = Gtk.Label("") - self.control_grid.attach(self.control['space%s' % i], 0, i, 1, 1) - if len(self._config.get_printers()) > 1: self.control['printer_select'] = self._gtk.ButtonImage( 'shuffle', None, None, button_scale[0], button_scale[1]) self.control['printer_select'].connect("clicked", self._screen.show_printer_select) - elif self._config.get_main_config_option('side_macro_shortcut') == "True": - self.control['printer_select'] = self._gtk.ButtonImage( - 'custom-script', None, None, button_scale[0], button_scale[1]) - self.control['printer_select'].connect("clicked", self.menu_item_clicked, "gcode_macros", { + + self.control['macro_shortcut'] = self._gtk.ButtonImage( + 'custom-script', None, None, button_scale[0], button_scale[1]) + self.control['macro_shortcut'].connect("clicked", self.menu_item_clicked, "gcode_macros", { "name": "Macros", "panel": "gcode_macros" - }) - else: - self.control['printer_select'] = Gtk.Label("") - - self.control_grid.attach(self.control['printer_select'], 0, 2, 1, 1) + }) self.control['estop'] = self._gtk.ButtonImage('emergency', None, None, button_scale[0], button_scale[1]) self.control['estop'].connect("clicked", self.emergency_stop) - self.control_grid.attach(self.control['estop'], 0, 3, 1, 1) + + self.locations = { + 'macro_shortcut': 2 + } + button_range = 3 + if len(self._config.get_printers()) > 1: + self.locations['macro_shortcut'] = 3 + if self._config.get_main_config_option('side_macro_shortcut') == "True": + button_range = 4 + + for i in range(button_range): + self.control['space%s' % i] = Gtk.Label("") + self.control_grid.attach(self.control['space%s' % i], 0, i, 1, 1) + + if len(self._config.get_printers()) > 1: + self.control_grid.remove(self.control_grid.get_child_at(0,2)) + self.control_grid.attach(self.control['printer_select'], 0, 2, 1, 1) + # If there's only one printer, show the macros shortcut if enabled. Otherwise, wait until the printer has + # been selected to show + elif self._config.get_main_config_option('side_macro_shortcut') == "True": + self.control_grid.remove(self.control_grid.get_child_at(0,self.locations['macro_shortcut'])) + self.control_grid.attach(self.control['macro_shortcut'], 0, self.locations['macro_shortcut'], 1, 1) + self.control_grid.attach(self.control['estop'], 0, 4, 1, 1) try: env = Environment(extensions=["jinja2.ext.i18n"]) @@ -201,6 +212,36 @@ class BasePanel(ScreenPanel): self.buttons_showing['back'] = False self.control_grid.show() + def show_macro_shortcut(self, show=True, mod_row=False): + if show == "True": + show = True + + if show == True and self.buttons_showing['macros_shortcut'] == False: + if len(self._config.get_printers()) > 1 and mod_row == True: + self.control_grid.insert_row(self.locations['macro_shortcut']) + else: + self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['macro_shortcut'])) + if 'space%s' % self.locations['macro_shortcut'] in self.control: + self.control_grid.remove(self.control['space%s' % self.locations['macro_shortcut']]) + self.control_grid.attach(self.control['macro_shortcut'], 0, self.locations['macro_shortcut'], 1, 1) + self.buttons_showing['macros_shortcut'] = True + self._screen.show_all() + elif show != True and self.buttons_showing['macros_shortcut'] == True: + if ('space%s' % self.locations['macro_shortcut']) not in self.control: + self.control['space%s' % self.locations['macro_shortcut']] = Gtk.Label("") + if len(self._config.get_printers()) > 1 and mod_row == True: + self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['macro_shortcut'])) + self.control_grid.remove(self.control['macro_shortcut']) + self.control_grid.remove_row(self.locations['macro_shortcut']) + else: + self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['macro_shortcut'])) + if ('space%s' % self.locations['macro_shortcut']) not in self.control: + self.control['space%s' % self.locations['macro_shortcut']] = Gtk.Label("") + self.control_grid.attach(self.control['space%s' % self.locations['macro_shortcut']], + 0, self.locations['macro_shortcut'], 1, 1) + self.buttons_showing['macros_shortcut'] = False + self._screen.show_all() + def set_title(self, title): try: env = Environment(extensions=["jinja2.ext.i18n"]) diff --git a/panels/settings.py b/panels/settings.py index 6cef758a..2310b7fd 100644 --- a/panels/settings.py +++ b/panels/settings.py @@ -138,7 +138,8 @@ class SettingsPanel(ScreenPanel): switch.set_active(self._config.get_config().getboolean(option['section'], opt_name, fallback=True)) else: switch.set_active(self._config.get_config().getboolean(option['section'], opt_name)) - switch.connect("notify::active", self.switch_config_option, option['section'], opt_name) + switch.connect("notify::active", self.switch_config_option, option['section'], opt_name, + option['callback'] if "callback" in option else None) switch.set_property("width-request", round(self._gtk.get_image_width()*2.5)) switch.set_property("height-request", round(self._gtk.get_image_height()*1.25)) box.add(switch) @@ -237,12 +238,14 @@ class SettingsPanel(ScreenPanel): self._config.set(section, option, str(int(widget.get_value()))) self._config.save_user_config_options() - def switch_config_option(self, switch, gparam, section, option): + def switch_config_option(self, switch, gparam, section, option, callback=None): logging.debug("[%s] %s toggled %s" % (section, option, switch.get_active())) if section not in self._config.get_config().sections(): self._config.get_config().add_section(section) self._config.set(section, option, "True" if switch.get_active() else "False") self._config.save_user_config_options() + if callback is not None: + callback(switch.get_active()) def add_gcode_option(self): macros = self._screen.printer.get_gcode_macros() diff --git a/screen.py b/screen.py index ab6e4530..c972462a 100644 --- a/screen.py +++ b/screen.py @@ -161,6 +161,7 @@ class KlipperScreen(Gtk.Window): while len(self.printer_select_callbacks) > 0: i = self.printer_select_callbacks.pop(0) i() + self.base_panel.show_macro_shortcut(self._config.get_main_config_option('side_macro_shortcut')) return self.printer_select_callbacks = [] @@ -573,6 +574,7 @@ class KlipperScreen(Gtk.Window): logging.debug("Saving panel: %s" % self._cur_panels[0]) self.printer_select_prepanel = self._cur_panels[0] self.show_panel("printer_select","printer_select","Printer Select", 2) + self.base_panel.show_macro_shortcut(False) def state_execute(self, callback, prev_state): if self.is_updating(): @@ -642,6 +644,7 @@ class KlipperScreen(Gtk.Window): if "job_status" in self._cur_panels or "main_menu" in self._cur_panels: return + self.base_panel.show_macro_shortcut(self._config.get_main_config_option('side_macro_shortcut')) if prev_state not in ['paused','printing']: self.init_printer() self.base_panel._printer = self.printer @@ -663,8 +666,15 @@ class KlipperScreen(Gtk.Window): return _ = self.lang.gettext + self.base_panel.show_macro_shortcut(False) self.printer_initializing(_("Klipper has shutdown")) + def toggle_macro_shortcut(self, value): + if value == True: + self.base_panel.show_macro_shortcut(True, True) + else: + self.base_panel.show_macro_shortcut(False, True) + def _websocket_callback(self, action, data): _ = self.lang.gettext