base_panel: layout simplification

This commit is contained in:
alfrix 2022-05-27 14:25:52 -03:00 committed by Alfredo Monclus
parent 05b25cda15
commit da26bf3143
8 changed files with 113 additions and 200 deletions

View File

@ -33,6 +33,7 @@ class KlippyGtk:
elif fontsize_type == "large":
self.font_size = round(self.font_size * 1.09)
self.header_size = int(round((self.width / self.width_ratio) / 1.33))
self.titlebar_height = self.font_size * 2
self.img_width = int(round(self.width / self.width_ratio))
self.img_height = int(round(self.height / self.height_ratio))
if self.screen.vertical_mode:
@ -62,11 +63,17 @@ class KlippyGtk:
return self.width - self.action_bar_width
def get_content_height(self):
return self.height - self.header_size
if self.screen.vertical_mode:
return self.height - self.titlebar_height - self.action_bar_height
else:
return self.height - self.titlebar_height
def get_font_size(self):
return self.font_size
def get_titlebar_height(self):
return self.titlebar_height
def get_header_size(self):
return self.header_size

View File

@ -25,7 +25,9 @@ class ScreenPanel:
self.layout = Gtk.Layout()
self.layout.set_size(self._screen.width, self._screen.height)
self.content = Gtk.Box(spacing=0)
self.content = Gtk.VBox(spacing=0)
self.content.set_hexpand(True)
self.content.set_vexpand(True)
def initialize(self, panel_name):
# Create gtk items here

View File

@ -16,25 +16,15 @@ class BasePanel(ScreenPanel):
self.current_panel = None
self.time_min = -1
self.time_format = self._config.get_main_config_option("24htime")
self.title_spacing = self._gtk.font_size * 2
self.time_update = None
self.titlebar_name_type = None
self.buttons_showing = {
'back': False if back else True,
'back': not(back),
'macros_shortcut': False,
'printer_select': False
}
self.layout = Gtk.Layout()
self.layout.set_size(self._screen.width, self._screen.height)
action_bar_width = self._gtk.get_action_bar_width() if action_bar is True else 0
action_bar_height = self._gtk.get_action_bar_height() if action_bar is True else 0
self.control_grid = self._gtk.HomogeneousGrid()
self.control_grid.set_size_request(action_bar_width, action_bar_height)
self.control_grid.get_style_context().add_class('action_bar')
# Action bar buttons
self.control['back'] = self._gtk.ButtonImage('back', None, None, 1)
self.control['back'].connect("clicked", self.back)
self.control['home'] = self._gtk.ButtonImage('main', None, None, 1)
@ -44,8 +34,8 @@ class BasePanel(ScreenPanel):
self.control['printer_select'] = self._gtk.ButtonImage('shuffle', None, None, 1)
self.control['printer_select'].connect("clicked", self._screen.show_printer_select)
self.control['macro_shortcut'] = self._gtk.ButtonImage('custom-script', None, None, 1)
self.control['macro_shortcut'].connect("clicked", self.menu_item_clicked, "gcode_macros", {
self.control['macros_shortcut'] = self._gtk.ButtonImage('custom-script', None, None, 1)
self.control['macros_shortcut'].connect("clicked", self.menu_item_clicked, "gcode_macros", {
"name": "Macros",
"panel": "gcode_macros"
})
@ -53,87 +43,77 @@ class BasePanel(ScreenPanel):
self.control['estop'] = self._gtk.ButtonImage('emergency', None, None, 1)
self.control['estop'].connect("clicked", self.emergency_stop)
self.locations = {
'macro_shortcut': 2,
'printer_select': 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("")
if self._screen.vertical_mode:
self.control_grid.attach(self.control['space%s' % i], i, 0, 1, 1)
else:
self.control_grid.attach(self.control['space%s' % i], 0, i, 1, 1)
# Action bar
self.action_bar = Gtk.VBox(spacing=5)
if self._screen.vertical_mode:
self.control_grid.attach(self.control['estop'], 4, 0, 1, 1)
self.action_bar.set_hexpand(True)
self.action_bar.set_vexpand(False)
self.action_bar.set_size_request(0, self._gtk.get_action_bar_height())
else:
self.control_grid.attach(self.control['estop'], 0, 4, 1, 1)
self.action_bar.set_hexpand(False)
self.action_bar.set_vexpand(True)
self.action_bar.set_size_request(self._gtk.get_action_bar_width(), 0)
try:
env = Environment(extensions=["jinja2.ext.i18n"])
env.install_gettext_translations(self.lang)
j2_temp = env.from_string(title)
title = j2_temp.render()
except Exception:
logging.debug("Error parsing jinja for title: %s" % title)
self.action_bar.get_style_context().add_class('action_bar')
self.action_bar.add(self.control['back'])
self.action_bar.add(self.control['home'])
if len(self._config.get_printers()) > 1:
self.action_bar.add(self.control['printer_select'])
self.action_bar.add(self.control['macros_shortcut'])
self.action_bar.add(self.control['estop'])
# Titlebar
# This box will be populated by show_heaters
self.control['temp_box'] = Gtk.Box(spacing=10)
self.titlelbl = Gtk.Label()
self.titlelbl.set_hexpand(True)
self.titlelbl.set_vexpand(True)
self.titlelbl.set_halign(Gtk.Align.CENTER)
self.titlelbl.set_ellipsize(Pango.EllipsizeMode.END)
self.set_title(title)
self.hmargin = 5
self.content = Gtk.VBox(spacing=0)
if self._screen.vertical_mode:
self.content.set_size_request(self._screen.width - self.hmargin * 2,
self._screen.height - self.title_spacing - action_bar_height)
else:
self.content.set_size_request(self._screen.width - action_bar_width - self.hmargin,
self._screen.height - self.title_spacing)
if action_bar is True:
if self._screen.vertical_mode:
self.layout.put(self.control_grid, 0, self._screen.height - action_bar_height)
else:
self.layout.put(self.control_grid, 0, 0)
self.control['time'] = Gtk.Label("00:00 AM")
self.control['time_box'] = Gtk.Box()
self.control['time_box'].set_halign(Gtk.Align.END)
self.control['time'] = Gtk.Label("00:00 AM")
self.control['time_box'].pack_end(self.control['time'], True, True, self.hmargin)
self.control['time_box'].pack_end(self.control['time'], True, True, 5)
self.control['temp_box'] = Gtk.Box()
self.titlebar = Gtk.Grid()
self.titlebar = Gtk.HBox(spacing=5)
self.titlebar.set_size_request(0, self._gtk.get_titlebar_height())
self.titlebar.set_valign(Gtk.Align.CENTER)
self.titlebar.set_property("column-spacing", 5)
if self._screen.vertical_mode:
self.titlebar.set_size_request(self._screen.width, self.title_spacing)
else:
self.titlebar.set_size_request(self._screen.width - action_bar_width, self.title_spacing)
self.titlebar.attach(self.control['temp_box'], 0, 0, 1, 1)
self.titlebar.attach(self.titlelbl, 1, 0, 1, 1)
self.titlebar.attach(self.control['time_box'], 2, 0, 1, 1)
self.titlebar.add(self.control['temp_box'])
self.titlebar.add(self.titlelbl)
self.titlebar.add(self.control['time_box'])
# Main layout
self.main_grid = Gtk.Grid()
# To achieve rezisability this needs to be removed
# The main issue is that currently the content doesn't expand correctly
self.main_grid.set_size_request(self._screen.width, self._screen.height)
if self._screen.vertical_mode:
self.layout.put(self.titlebar, 0, 0)
self.layout.put(self.content, self.hmargin, self.title_spacing)
self.main_grid.attach(self.titlebar, 0, 0, 1, 1)
self.main_grid.attach(self.content, 0, 1, 1, 1)
self.main_grid.attach(self.action_bar, 0, 2, 1, 1)
self.action_bar.set_orientation(orientation=Gtk.Orientation.HORIZONTAL)
else:
self.layout.put(self.titlebar, action_bar_width, 0)
self.layout.put(self.content, action_bar_width + self.hmargin, self.title_spacing)
self.main_grid.attach(self.action_bar, 0, 0, 1, 2)
self.action_bar.set_orientation(orientation=Gtk.Orientation.VERTICAL)
self.main_grid.attach(self.titlebar, 1, 0, 1, 1)
self.main_grid.attach(self.content, 1, 1, 1, 1)
# Layout is and content are on screen_panel
self.layout.add(self.main_grid)
def initialize(self, panel_name):
self.update_time()
return
def show_heaters(self, show=True):
printer_cfg = self._config.get_printer_config(self._screen.connected_printer)
self.titlebar_name_type = printer_cfg.get("titlebar_name_type", None)
logging.info("Titlebar name type: %s", self.titlebar_name_type)
for child in self.control['temp_box'].get_children():
self.control['temp_box'].remove(child)
@ -141,33 +121,36 @@ class BasePanel(ScreenPanel):
return
for device in self._screen.printer.get_temp_store_devices():
logging.info(device)
self.labels[device + '_box'] = Gtk.Box(spacing=0)
self.labels[device] = Gtk.Label(label="100º")
self.labels[device].set_ellipsize(Pango.EllipsizeMode.START)
if device.startswith("extruder"):
if self._screen.printer.extrudercount > 1:
if device == "extruder":
ext_img = self._gtk.Image("extruder-0", .5)
icon = self._gtk.Image("extruder-0", .5)
else:
ext_img = self._gtk.Image("extruder-%s" % device[8:], .5)
icon = self._gtk.Image("extruder-%s" % device[8:], .5)
else:
ext_img = self._gtk.Image("extruder", .5)
self.labels[device + '_box'].pack_start(ext_img, True, True, 3)
icon = self._gtk.Image("extruder", .5)
elif device.startswith("heater_bed"):
bed_img = self._gtk.Image("bed", .5)
self.labels[device + '_box'].pack_start(bed_img, True, True, 3)
icon = self._gtk.Image("bed", .5)
# Extra items
elif self.titlebar_name_type is not None:
# The item has a name, do not use an icon
icon = None
elif device.startswith("temperature_fan"):
fan_img = self._gtk.Image("fan", .5)
self.labels[device + '_box'].pack_start(fan_img, True, True, 3)
icon = self._gtk.Image("fan", .5)
elif device.startswith("heater_generic"):
heat_img = self._gtk.Image("heater", .5)
self.labels[device + '_box'].pack_start(heat_img, True, True, 3)
icon = self._gtk.Image("heater", .5)
else:
temp_img = self._gtk.Image("heat-up", .5)
self.labels[device + '_box'].pack_start(temp_img, True, True, 3)
self.labels[device + '_box'].pack_start(self.labels[device], True, True, 0)
icon = self._gtk.Image("heat-up", .5)
self.labels[device] = Gtk.Label(label="100º")
self.labels[device].set_ellipsize(Pango.EllipsizeMode.START)
self.labels[device + '_box'] = Gtk.Box()
if icon is not None:
self.labels[device + '_box'].pack_start(icon, False, False, 3)
self.labels[device + '_box'].pack_start(self.labels[device], False, False, 0)
# Limit the number of items according to resolution
if self._screen.width <= 480:
nlimit = 3
elif self._screen.width <= 800:
@ -178,22 +161,19 @@ class BasePanel(ScreenPanel):
n = 0
if self._screen.printer.get_tools():
self.current_extruder = self._screen.printer.get_stat("toolhead", "extruder")
self.control['temp_box'].pack_start(self.labels["%s_box" % self.current_extruder], True, True, 3)
self.control['temp_box'].add(self.labels["%s_box" % self.current_extruder])
n += 1
if self._screen.printer.has_heated_bed():
self.control['temp_box'].pack_start(self.labels['heater_bed_box'], True, True, 3)
self.control['temp_box'].add(self.labels['heater_bed_box'])
n += 1
# Options in the config have priority
printer_cfg = self._config.get_printer_config(self._screen.connected_printer)
if printer_cfg is not None:
titlebar_items = printer_cfg.get("titlebar_items", "")
if titlebar_items is not None:
titlebar_items = [str(i.strip()) for i in titlebar_items.split(',')]
logging.info("Titlebar items: %s", titlebar_items)
self.titlebar_name_type = printer_cfg.get("titlebar_name_type", None)
logging.info("Titlebar name type: %s", self.titlebar_name_type)
for device in self._screen.printer.get_temp_store_devices():
# Users can fill the bar if they want
if n >= nlimit + 1:
@ -204,7 +184,7 @@ class BasePanel(ScreenPanel):
name = device
for item in titlebar_items:
if name == item:
self.control['temp_box'].pack_start(self.labels["%s_box" % device], True, True, 3)
self.control['temp_box'].add(self.labels["%s_box" % device])
n += 1
break
@ -213,7 +193,7 @@ class BasePanel(ScreenPanel):
if n >= nlimit:
break
if device.startswith("heater_generic"):
self.control['temp_box'].pack_start(self.labels["%s_box" % device], True, True, 3)
self.control['temp_box'].add(self.labels["%s_box" % device])
n += 1
self.control['temp_box'].show_all()
@ -230,8 +210,7 @@ class BasePanel(ScreenPanel):
if self.current_panel is None:
return
if self._screen.is_keyboard_showing():
self._screen.remove_keyboard()
self._screen.remove_keyboard()
if hasattr(self.current_panel, "back"):
if not self.current_panel.back():
@ -239,9 +218,6 @@ class BasePanel(ScreenPanel):
else:
self._screen._menu_go_back()
def get(self):
return self.layout
def process_update(self, action, data):
if action != "notify_status_update" or self._screen.printer is None:
return
@ -249,14 +225,14 @@ class BasePanel(ScreenPanel):
devices = self._screen.printer.get_temp_store_devices()
if devices is not None:
for device in devices:
name = ""
if not (device.startswith("extruder") or device.startswith("heater_bed")):
if self.titlebar_name_type == "full":
name = device.split(" ")[1:][0].capitalize().replace("_", " ") + ": "
elif self.titlebar_name_type == "short":
name = device.split(" ")[1:][0][:1].upper() + ": "
temp = self._screen.printer.get_dev_stat(device, "temperature")
if temp is not None:
name = ""
if not (device.startswith("extruder") or device.startswith("heater_bed")):
if self.titlebar_name_type == "full":
name = device.split(" ")[1:][0].capitalize().replace("_", " ") + ": "
elif self.titlebar_name_type == "short":
name = device.split(" ")[1:][0][:1].upper() + ": "
self.labels[device].set_label("%s%d°" % (name, round(temp)))
if "toolhead" in data and "extruder" in data["toolhead"]:
@ -272,99 +248,39 @@ class BasePanel(ScreenPanel):
def show_back(self, show=True):
if show is True and self.buttons_showing['back'] is False:
self.control_grid.remove(self.control_grid.get_child_at(0, 0))
self.control_grid.attach(self.control['back'], 0, 0, 1, 1)
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(1, 0))
self.control_grid.attach(self.control['home'], 1, 0, 1, 1)
else:
self.control_grid.remove(self.control_grid.get_child_at(0, 1))
self.control_grid.attach(self.control['home'], 0, 1, 1, 1)
self.control['back'].set_sensitive(True)
self.control['home'].set_sensitive(True)
self.buttons_showing['back'] = True
elif show is False and self.buttons_showing['back'] is True:
for i in range(0, 2):
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(i, 0))
self.control_grid.attach(self.control['space%s' % i], i, 0, 1, 1)
else:
self.control_grid.remove(self.control_grid.get_child_at(0, i))
self.control_grid.attach(self.control['space%s' % i], 0, i, 1, 1)
self.control['back'].set_sensitive(False)
self.control['home'].set_sensitive(False)
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 is True and self.buttons_showing['macros_shortcut'] is False:
if len(self._config.get_printers()) > 1 and mod_row is True:
if self._screen.vertical_mode:
self.control_grid.insert_column(self.locations['macro_shortcut'])
else:
self.control_grid.insert_row(self.locations['macro_shortcut'])
self.action_bar.add(self.control['macros_shortcut'])
if self.buttons_showing['printer_select'] is False:
self.action_bar.reorder_child(self.control['macros_shortcut'], 2)
else:
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(self.locations['macro_shortcut'], 0))
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']])
if self._screen.vertical_mode:
self.control_grid.attach(self.control['macro_shortcut'], self.locations['macro_shortcut'], 0, 1, 1)
else:
self.control_grid.attach(self.control['macro_shortcut'], 0, self.locations['macro_shortcut'], 1, 1)
self.action_bar.reorder_child(self.control['macros_shortcut'], 3)
self.buttons_showing['macros_shortcut'] = True
elif show is not True and self.buttons_showing['macros_shortcut'] is 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 is True:
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(self.locations['macro_shortcut'], 0))
self.control_grid.remove_column(self.locations['macro_shortcut'])
else:
self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['macro_shortcut']))
self.control_grid.remove_row(self.locations['macro_shortcut'])
self.control_grid.remove(self.control['macro_shortcut'])
else:
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(self.locations['macro_shortcut'], 0))
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("")
if self._screen.vertical_mode:
self.control_grid.attach(self.control['space%s' % self.locations['macro_shortcut']],
self.locations['macro_shortcut'], 0, 1, 1)
else:
self.control_grid.attach(self.control['space%s' % self.locations['macro_shortcut']],
0, self.locations['macro_shortcut'], 1, 1)
elif show is False and self.buttons_showing['macros_shortcut'] is True:
self.action_bar.remove(self.control['macros_shortcut'])
self.buttons_showing['macros_shortcut'] = False
self._screen.show_all()
def show_printer_select(self, show=True):
if len(self._config.get_printers()) <= 1:
return
if show and self.buttons_showing['printer_select'] is False:
logging.info("Turning on printer_select button")
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(self.locations['printer_select'], 0))
self.control_grid.attach(self.control['printer_select'], self.locations['printer_select'], 0, 1, 1)
else:
self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['printer_select']))
self.control_grid.attach(self.control['printer_select'], 0, self.locations['printer_select'], 1, 1)
self.action_bar.add(self.control['printer_select'])
self.action_bar.reorder_child(self.control['printer_select'], 2)
self.buttons_showing['printer_select'] = True
elif show is False and self.buttons_showing['printer_select']:
logging.info("Turning off printer_select button")
if self._screen.vertical_mode:
self.control_grid.remove(self.control_grid.get_child_at(self.locations['printer_select'], 0))
self.control_grid.attach(self.control['space%s' % self.locations['printer_select']],
self.locations['printer_select'], 0, 1, 1)
else:
self.control_grid.remove(self.control_grid.get_child_at(0, self.locations['printer_select']))
self.control_grid.attach(self.control['space%s' % self.locations['printer_select']],
0, self.locations['printer_select'], 1, 1)
self.action_bar.remove(self.control['printer_select'])
self.buttons_showing['printer_select'] = False
self._screen.show_all()
@ -379,13 +295,6 @@ class BasePanel(ScreenPanel):
self.titlelbl.set_label("%s | %s" % (self._screen.connecting_to_printer, title))
def show_back_buttons(self):
self.control_grid.attach(self.control['back'], 0, 0, 1, 1)
if self._screen.vertical_mode:
self.control_grid.attach(self.control['home'], 1, 0, 1, 1)
else:
self.control_grid.attach(self.control['home'], 0, 1, 1, 1)
def update_time(self):
now = datetime.datetime.now()
confopt = self._config.get_main_config_option("24htime")

View File

@ -272,7 +272,6 @@ class MovePanel(ScreenPanel):
scale.set_hexpand(True)
scale.set_digits(0)
scale.connect("button-release-event", self.scale_moved, option['section'], opt_name)
scale.set_property("width-request", round(self._screen.width / 2.2))
dev.add(scale)
frame = Gtk.Frame()

View File

@ -298,7 +298,6 @@ class PrintPanel(ScreenPanel):
grid = Gtk.Grid()
grid.add(label)
grid.set_size_request(self._screen.width - 60, -1)
pixbuf = self.get_file_image(filename, 8, 3.2)
if pixbuf is not None:

View File

@ -16,7 +16,6 @@ class SettingsPanel(ScreenPanel):
_ = self.lang.gettext
self.settings = {}
self.menu = ['settings_menu']
self.labels['add_printer_button'] = self._gtk.Button(_("Add Printer"), "color1")
options = self._config.get_configurable_options().copy()
@ -119,7 +118,6 @@ class SettingsPanel(ScreenPanel):
scale.set_hexpand(True)
scale.set_digits(0)
scale.connect("button-release-event", self.scale_moved, option['section'], opt_name)
scale.set_property("width-request", round(self._screen.width / 2.2))
dev.add(scale)
elif option['type'] == "printer":
logging.debug("Option: %s" % option)

View File

@ -18,9 +18,6 @@ class SplashScreenPanel(ScreenPanel):
def __init__(self, screen, title, back=True):
super().__init__(screen, title, back)
self.layout = Gtk.Layout()
self.layout.set_size(self._screen.width, self._screen.height)
def initialize(self, panel_name):
_ = self.lang.gettext

View File

@ -125,7 +125,7 @@ class KlipperScreen(Gtk.Window):
self.keyboard_height = self.gtk.get_keyboard_height()
self.init_style()
self.base_panel = BasePanel(self, "Base Panel", False)
self.base_panel = BasePanel(self, title="Base Panel", back=False)
self.add(self.base_panel.get())
self.show_all()
self.base_panel.activate()
@ -1034,18 +1034,20 @@ class KlipperScreen(Gtk.Window):
logging.debug("PID %s" % p.pid)
keyboard = Gtk.Socket()
action_bar_width = self.gtk.get_action_bar_width()
box = Gtk.VBox()
box.set_vexpand(False)
box.set_size_request(self.width - action_bar_width, self.keyboard_height)
if self._screen.vertical_mode:
box.set_size_request(self.width, self.keyboard_height)
else:
action_bar_width = self.gtk.get_action_bar_width()
box.set_size_request(self.width - action_bar_width, self.keyboard_height)
box.add(keyboard)
self.base_panel.get_content().pack_end(box, False, 0, 0)
self.show_all()
keyboard.add_id(xid)
keyboard.show()
self.keyboard = {
"box": box,