From a0de521631885e0dde474d48ff9cd7564fec9173 Mon Sep 17 00:00:00 2001 From: Jordan Date: Tue, 18 May 2021 22:24:28 -0400 Subject: [PATCH] base_panel: Include time --- ks_includes/config.py | 3 ++- panels/base_panel.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ks_includes/config.py b/ks_includes/config.py index e2e84ec7..26f22810 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -53,7 +53,8 @@ class KlipperScreenConfig: "value": "z-bolt","options":[ {"name": _("Z-bolt (default)"), "value": "z-bolt"}, {"name": _("Colorized"), "value": "colorized"} - ]}} + ]}}, + {"24htime": {"section": "main", "name": _("24 Hour Time"), "type": "binary", "value": "True"}}, #{"": {"section": "main", "name": _(""), "type": ""}} ] diff --git a/panels/base_panel.py b/panels/base_panel.py index 3ac51675..bc96054d 100644 --- a/panels/base_panel.py +++ b/panels/base_panel.py @@ -1,3 +1,4 @@ +import datetime import gi import logging @@ -13,6 +14,8 @@ class BasePanel(ScreenPanel): def __init__(self, screen, title, back=True, action_bar=True, printer_name=True): super().__init__(screen, title, back, action_bar, printer_name) self.current_panel = None + self.time_min = -1 + self.time_format = self._config.get_main_config_option("24htime") self.buttons_showing = { 'back': False if back else True @@ -75,6 +78,17 @@ class BasePanel(ScreenPanel): if action_bar == True: self.layout.put(self.control_grid, 0, 0) + + self.control['time_box'] = Gtk.Box() + self.control['time_box'].set_halign(Gtk.Align.END) + self.control['time_box'].set_size_request(0, self.title_spacing) + self.control['time'] = Gtk.Label("00:00 AM") + self.control['time'].set_size_request(0, self.title_spacing) + self.control['time'].set_halign(Gtk.Align.END) + self.control['time'].set_valign(Gtk.Align.CENTER) + self.control['time_box'].pack_end(self.control['time'], True, 0, 0) + + self.layout.put(self.control['time_box'], action_bar_width, 0) self.layout.put(self.titlelbl, action_bar_width, 0) self.layout.put(self.content, action_bar_width, self.title_spacing) @@ -82,6 +96,25 @@ class BasePanel(ScreenPanel): # Create gtk items here return + def activate(self): + size = self.control['time_box'].get_allocation().width + self.layout.remove(self.control['time_box']) + self.control['time_box'].set_size_request(size, self.title_spacing) + self.layout.put(self.control['time_box'], self._screen.width - size - 5, 0) + + GLib.timeout_add_seconds(1, self.update_time) + self.update_time() + + def update_time(self): + now = datetime.datetime.now() + confopt = self._config.get_main_config_option("24htime") + if now.minute != self.time_min or self.time_format != confopt: + if confopt == "True": + self.control['time'].set_text(now.strftime("%H:%M")) + else: + self.control['time'].set_text(now.strftime("%I:%M %p")) + return True + def add_content(self, panel): self.current_panel = panel self.set_title(panel.get_title())