From 48c276d22fd65be43c7e05c16f93aef61037b9a8 Mon Sep 17 00:00:00 2001 From: Jordan Ruthe Date: Sat, 28 Nov 2020 13:57:51 -0500 Subject: [PATCH] menu: add ability for enable to hide certain menus --- docs/Configuration.md | 7 +++++ docs/changelog.md | 9 ++++-- panels/menu.py | 38 ++++++++++++++++++++++++++ scripts/KlipperScreen-requirements.txt | 1 + 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 1975c7a4..0e196df9 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -32,9 +32,16 @@ panel: preheat method: printer.gcode.script # Parameters that would be passed with the method above params: {"script":"G28 X"} +# Enable allows hiding of a menu if the condition is false. This statement is evaluated in Jinja2 +# Available variables are listed below. +enable: {{ printer.power_devices.count > 0 }} ``` Available panels are listed here: [docs/panels.md](panels.md) +Certain variables are available for conditional testing of the enable statement: +``` +printer.power_devices.count # Number of power devices configured in Moonraker +``` A sample configuration of a main menu would be as follows: diff --git a/docs/changelog.md b/docs/changelog.md index 66e074e1..5926c82c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,9 +1,12 @@ ## Changelog +#### 2020 11 28 +* Add option for enable in menu for configuration. This can hide certain options + #### 2020 11 18 -* Changed configuration file format. -* Moved default configuration file to an include folder. -* Added ability to do a confirm dialog from a menu item when running a script +* Changed configuration file format. +* Moved default configuration file to an include folder. +* Added ability to do a confirm dialog from a menu item when running a script * Added "Save Config" button to default configuration's Configuration menu. #### 2020 11 14 diff --git a/panels/menu.py b/panels/menu.py index 93189f18..8c20c985 100644 --- a/panels/menu.py +++ b/panels/menu.py @@ -7,6 +7,8 @@ from gi.repository import Gtk, Gdk, GLib from ks_includes.KlippyGtk import KlippyGtk from ks_includes.screen_panel import ScreenPanel +from jinja2 import Template + logger = logging.getLogger("KlipperScreen.MenuPanel") def create_panel(*args): @@ -16,6 +18,7 @@ class MenuPanel(ScreenPanel): def initialize(self, panel_name, items): _ = self.lang.gettext + self.activate() grid = self.arrangeMenuItems(items, 4) b = KlippyGtk.ButtonImage('back', _('Back')) @@ -24,10 +27,26 @@ class MenuPanel(ScreenPanel): self.panel = grid + def activate(self): + self.j2_data = { + "printer": { + "power_devices": { + "count": len(self._screen.printer.get_power_devices()) + } + } + } + logger.debug("j2_data: %s" % self.j2_data) + def arrangeMenuItems (self, items, columns, expandLast=False): grid = Gtk.Grid() grid.set_row_homogeneous(True) grid.set_column_homogeneous(True) + logger.debug(items) + for item in items: + key = list(item)[0] + logger.debug(item) + if not self.evaluate_enable(item[key]['enable']): + items.remove(item) l = len(items) i = 0 @@ -60,3 +79,22 @@ class MenuPanel(ScreenPanel): i += 1 return grid + + def evaluate_enable(self, enable): + if enable == True: + return True + if enable == False: + return False + + try: + logger.debug("Template: '%s'" % enable) + logger.debug("Date: %s" % self.j2_data) + j2_temp = Template(enable) + result = j2_temp.render(self.j2_data) + logger.debug("Result: %s" % result) + if result == 'True': + return True + return False + except: + logger.debug("Error evaluating enable statement: %s", enable) + return False diff --git a/scripts/KlipperScreen-requirements.txt b/scripts/KlipperScreen-requirements.txt index e6398117..ddfe8085 100644 --- a/scripts/KlipperScreen-requirements.txt +++ b/scripts/KlipperScreen-requirements.txt @@ -3,3 +3,4 @@ requests==2.25.0 vext==0.7.4 websocket-client==0.57.0 humanize==3.1.0 +jinja2==2.11.2