screen_panel: integrate add_option to deduplicate code
This commit is contained in:
parent
198cdfcc11
commit
6117b6e10d
@ -3,7 +3,7 @@ import datetime
|
|||||||
import gi
|
import gi
|
||||||
|
|
||||||
gi.require_version("Gtk", "3.0")
|
gi.require_version("Gtk", "3.0")
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk, Pango
|
||||||
|
|
||||||
|
|
||||||
class ScreenPanel:
|
class ScreenPanel:
|
||||||
@ -195,3 +195,77 @@ class ScreenPanel:
|
|||||||
self.labels[dev].get_style_context().remove_class("heater-grid-temp-power")
|
self.labels[dev].get_style_context().remove_class("heater-grid-temp-power")
|
||||||
elif dev in self.devices:
|
elif dev in self.devices:
|
||||||
self.devices[dev]["temp"].get_child().set_label(new_label_text)
|
self.devices[dev]["temp"].get_child().set_label(new_label_text)
|
||||||
|
|
||||||
|
def add_option(self, boxname, opt_array, opt_name, option):
|
||||||
|
if option['type'] is None:
|
||||||
|
return
|
||||||
|
name = Gtk.Label(
|
||||||
|
hexpand=True, vexpand=True, halign=Gtk.Align.START, valign=Gtk.Align.CENTER,
|
||||||
|
wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR)
|
||||||
|
name.set_markup(f"<big><b>{option['name']}</b></big>")
|
||||||
|
|
||||||
|
labels = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||||
|
labels.add(name)
|
||||||
|
|
||||||
|
dev = Gtk.Box(spacing=5,
|
||||||
|
valign=Gtk.Align.CENTER, hexpand=True, vexpand=False)
|
||||||
|
dev.get_style_context().add_class("frame-item")
|
||||||
|
dev.add(labels)
|
||||||
|
setting = {}
|
||||||
|
if option['type'] == "binary":
|
||||||
|
switch = Gtk.Switch(active=self._config.get_config().getboolean(option['section'], opt_name, fallback=True))
|
||||||
|
switch.connect("notify::active", self.switch_config_option, option['section'], opt_name,
|
||||||
|
option['callback'] if "callback" in option else None)
|
||||||
|
dev.add(switch)
|
||||||
|
setting = {opt_name: switch}
|
||||||
|
elif option['type'] == "dropdown":
|
||||||
|
dropdown = Gtk.ComboBoxText()
|
||||||
|
for i, opt in enumerate(option['options']):
|
||||||
|
dropdown.append(opt['value'], opt['name'])
|
||||||
|
if opt['value'] == self._config.get_config()[option['section']].get(opt_name, option['value']):
|
||||||
|
dropdown.set_active(i)
|
||||||
|
dropdown.connect("changed", self.on_dropdown_change, option['section'], opt_name,
|
||||||
|
option['callback'] if "callback" in option else None)
|
||||||
|
dropdown.set_entry_text_column(0)
|
||||||
|
dev.add(dropdown)
|
||||||
|
setting = {opt_name: dropdown}
|
||||||
|
elif option['type'] == "scale":
|
||||||
|
dev.set_orientation(Gtk.Orientation.VERTICAL)
|
||||||
|
scale = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL,
|
||||||
|
min=option['range'][0], max=option['range'][1], step=option['step'])
|
||||||
|
scale.set_hexpand(True)
|
||||||
|
scale.set_value(int(self._config.get_config().get(option['section'], opt_name, fallback=option['value'])))
|
||||||
|
scale.set_digits(0)
|
||||||
|
scale.connect("button-release-event", self.scale_moved, option['section'], opt_name)
|
||||||
|
dev.add(scale)
|
||||||
|
setting = {opt_name: scale}
|
||||||
|
elif option['type'] == "printer":
|
||||||
|
box = Gtk.Box(vexpand=False)
|
||||||
|
label = Gtk.Label(f"{option['moonraker_host']}:{option['moonraker_port']}")
|
||||||
|
box.add(label)
|
||||||
|
dev.add(box)
|
||||||
|
elif option['type'] == "menu":
|
||||||
|
open_menu = self._gtk.Button("settings", style="color3")
|
||||||
|
open_menu.connect("clicked", self.load_menu, option['menu'], option['name'])
|
||||||
|
open_menu.set_hexpand(False)
|
||||||
|
open_menu.set_halign(Gtk.Align.END)
|
||||||
|
dev.add(open_menu)
|
||||||
|
elif option['type'] == "lang":
|
||||||
|
select = self._gtk.Button("load", style="color3")
|
||||||
|
select.connect("clicked", self._screen.change_language, option['name'])
|
||||||
|
select.set_hexpand(False)
|
||||||
|
select.set_halign(Gtk.Align.END)
|
||||||
|
dev.add(select)
|
||||||
|
|
||||||
|
opt_array[opt_name] = {
|
||||||
|
"name": option['name'],
|
||||||
|
"row": dev
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = sorted(list(opt_array), key=lambda x: opt_array[x]['name'].casefold())
|
||||||
|
pos = opts.index(opt_name)
|
||||||
|
|
||||||
|
self.labels[boxname].insert_row(pos)
|
||||||
|
self.labels[boxname].attach(opt_array[opt_name]['row'], 0, pos, 1, 1)
|
||||||
|
self.labels[boxname].show_all()
|
||||||
|
return setting
|
||||||
|
@ -128,6 +128,7 @@ class Panel(ScreenPanel):
|
|||||||
self.options[macro] = {
|
self.options[macro] = {
|
||||||
"name": macro,
|
"name": macro,
|
||||||
"section": f"displayed_macros {self._screen.connected_printer}",
|
"section": f"displayed_macros {self._screen.connected_printer}",
|
||||||
|
"type": "binary"
|
||||||
}
|
}
|
||||||
show = self._config.get_config().getboolean(self.options[macro]["section"], macro.lower(), fallback=True)
|
show = self._config.get_config().getboolean(self.options[macro]["section"], macro.lower(), fallback=True)
|
||||||
if macro not in self.macros and show:
|
if macro not in self.macros and show:
|
||||||
@ -142,36 +143,6 @@ class Panel(ScreenPanel):
|
|||||||
self.labels['macros'].attach(self.macros[macro]['row'], 0, pos, 1, 1)
|
self.labels['macros'].attach(self.macros[macro]['row'], 0, pos, 1, 1)
|
||||||
self.labels['macros'].show_all()
|
self.labels['macros'].show_all()
|
||||||
|
|
||||||
def add_option(self, boxname, opt_array, opt_name, option):
|
|
||||||
name = Gtk.Label(hexpand=True, vexpand=True, halign=Gtk.Align.START, valign=Gtk.Align.CENTER,
|
|
||||||
wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR)
|
|
||||||
name.set_markup(f"<big><b>{option['name']}</b></big>")
|
|
||||||
|
|
||||||
box = Gtk.Box(vexpand=False)
|
|
||||||
switch = Gtk.Switch(hexpand=False, vexpand=False,
|
|
||||||
width_request=round(self._gtk.font_size * 7),
|
|
||||||
height_request=round(self._gtk.font_size * 3.5),
|
|
||||||
active=self._config.get_config().getboolean(option['section'], opt_name, fallback=True))
|
|
||||||
switch.connect("notify::active", self.switch_config_option, option['section'], opt_name)
|
|
||||||
box.add(switch)
|
|
||||||
|
|
||||||
dev = Gtk.Box(hexpand=True, vexpand=False, valign=Gtk.Align.CENTER)
|
|
||||||
dev.get_style_context().add_class("frame-item")
|
|
||||||
dev.add(name)
|
|
||||||
dev.add(box)
|
|
||||||
|
|
||||||
opt_array[opt_name] = {
|
|
||||||
"name": option['name'],
|
|
||||||
"row": dev
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = sorted(self.options, key=str.casefold)
|
|
||||||
pos = opts.index(opt_name)
|
|
||||||
|
|
||||||
self.labels[boxname].insert_row(pos)
|
|
||||||
self.labels[boxname].attach(opt_array[opt_name]['row'], 0, pos, 1, 1)
|
|
||||||
self.labels[boxname].show_all()
|
|
||||||
|
|
||||||
def back(self):
|
def back(self):
|
||||||
if len(self.menu) > 1:
|
if len(self.menu) > 1:
|
||||||
self.unload_menu()
|
self.unload_menu()
|
||||||
|
@ -177,50 +177,6 @@ class Panel(ScreenPanel):
|
|||||||
if self._printer.get_stat("gcode_move", "absolute_coordinates"):
|
if self._printer.get_stat("gcode_move", "absolute_coordinates"):
|
||||||
self._screen._ws.klippy.gcode_script("G90")
|
self._screen._ws.klippy.gcode_script("G90")
|
||||||
|
|
||||||
def add_option(self, boxname, opt_array, opt_name, option):
|
|
||||||
name = Gtk.Label(hexpand=True, vexpand=True, halign=Gtk.Align.START, valign=Gtk.Align.CENTER, wrap=True)
|
|
||||||
name.set_markup(f"<big><b>{option['name']}</b></big>")
|
|
||||||
name.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
|
|
||||||
|
|
||||||
dev = Gtk.Box(spacing=5,
|
|
||||||
hexpand=True, vexpand=False, valign=Gtk.Align.CENTER)
|
|
||||||
dev.get_style_context().add_class("frame-item")
|
|
||||||
dev.add(name)
|
|
||||||
setting = {}
|
|
||||||
if option['type'] == "binary":
|
|
||||||
box = Gtk.Box(hexpand=False)
|
|
||||||
switch = Gtk.Switch(hexpand=False, vexpand=False,
|
|
||||||
width_request=round(self._gtk.font_size * 7),
|
|
||||||
height_request=round(self._gtk.font_size * 3.5),
|
|
||||||
active=self._config.get_config().getboolean(option['section'], opt_name))
|
|
||||||
switch.connect("notify::active", self.switch_config_option, option['section'], opt_name)
|
|
||||||
setting = {opt_name: switch}
|
|
||||||
box.add(switch)
|
|
||||||
dev.add(box)
|
|
||||||
elif option['type'] == "scale":
|
|
||||||
dev.set_orientation(Gtk.Orientation.VERTICAL)
|
|
||||||
scale = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL,
|
|
||||||
min=option['range'][0], max=option['range'][1], step=option['step'])
|
|
||||||
scale.set_hexpand(True)
|
|
||||||
scale.set_value(int(self._config.get_config().get(option['section'], opt_name, fallback=option['value'])))
|
|
||||||
scale.set_digits(0)
|
|
||||||
scale.connect("button-release-event", self.scale_moved, option['section'], opt_name)
|
|
||||||
setting = {opt_name: scale}
|
|
||||||
dev.add(scale)
|
|
||||||
|
|
||||||
opt_array[opt_name] = {
|
|
||||||
"name": option['name'],
|
|
||||||
"row": dev
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = sorted(list(opt_array), key=lambda x: opt_array[x]['name'])
|
|
||||||
pos = opts.index(opt_name)
|
|
||||||
|
|
||||||
self.labels[boxname].insert_row(pos)
|
|
||||||
self.labels[boxname].attach(opt_array[opt_name]['row'], 0, pos, 1, 1)
|
|
||||||
self.labels[boxname].show_all()
|
|
||||||
return setting
|
|
||||||
|
|
||||||
def back(self):
|
def back(self):
|
||||||
if len(self.menu) > 1:
|
if len(self.menu) > 1:
|
||||||
self.unload_menu()
|
self.unload_menu()
|
||||||
|
@ -64,73 +64,3 @@ class Panel(ScreenPanel):
|
|||||||
self.unload_menu()
|
self.unload_menu()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def add_option(self, boxname, opt_array, opt_name, option):
|
|
||||||
if option['type'] is None:
|
|
||||||
return
|
|
||||||
name = Gtk.Label(
|
|
||||||
hexpand=True, vexpand=True, halign=Gtk.Align.START, valign=Gtk.Align.CENTER,
|
|
||||||
wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR)
|
|
||||||
name.set_markup(f"<big><b>{option['name']}</b></big>")
|
|
||||||
|
|
||||||
labels = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
||||||
labels.add(name)
|
|
||||||
|
|
||||||
dev = Gtk.Box(spacing=5,
|
|
||||||
valign=Gtk.Align.CENTER, hexpand=True, vexpand=False)
|
|
||||||
dev.get_style_context().add_class("frame-item")
|
|
||||||
|
|
||||||
dev.add(labels)
|
|
||||||
if option['type'] == "binary":
|
|
||||||
switch = Gtk.Switch(active=self._config.get_config().getboolean(option['section'], opt_name))
|
|
||||||
switch.connect("notify::active", self.switch_config_option, option['section'], opt_name,
|
|
||||||
option['callback'] if "callback" in option else None)
|
|
||||||
dev.add(switch)
|
|
||||||
elif option['type'] == "dropdown":
|
|
||||||
dropdown = Gtk.ComboBoxText()
|
|
||||||
for i, opt in enumerate(option['options']):
|
|
||||||
dropdown.append(opt['value'], opt['name'])
|
|
||||||
if opt['value'] == self._config.get_config()[option['section']].get(opt_name, option['value']):
|
|
||||||
dropdown.set_active(i)
|
|
||||||
dropdown.connect("changed", self.on_dropdown_change, option['section'], opt_name,
|
|
||||||
option['callback'] if "callback" in option else None)
|
|
||||||
dropdown.set_entry_text_column(0)
|
|
||||||
dev.add(dropdown)
|
|
||||||
elif option['type'] == "scale":
|
|
||||||
dev.set_orientation(Gtk.Orientation.VERTICAL)
|
|
||||||
scale = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL,
|
|
||||||
min=option['range'][0], max=option['range'][1], step=option['step'])
|
|
||||||
scale.set_hexpand(True)
|
|
||||||
scale.set_value(int(self._config.get_config().get(option['section'], opt_name, fallback=option['value'])))
|
|
||||||
scale.set_digits(0)
|
|
||||||
scale.connect("button-release-event", self.scale_moved, option['section'], opt_name)
|
|
||||||
dev.add(scale)
|
|
||||||
elif option['type'] == "printer":
|
|
||||||
box = Gtk.Box(vexpand=False)
|
|
||||||
label = Gtk.Label(f"{option['moonraker_host']}:{option['moonraker_port']}")
|
|
||||||
box.add(label)
|
|
||||||
dev.add(box)
|
|
||||||
elif option['type'] == "menu":
|
|
||||||
open_menu = self._gtk.Button("settings", style="color3")
|
|
||||||
open_menu.connect("clicked", self.load_menu, option['menu'], option['name'])
|
|
||||||
open_menu.set_hexpand(False)
|
|
||||||
open_menu.set_halign(Gtk.Align.END)
|
|
||||||
dev.add(open_menu)
|
|
||||||
elif option['type'] == "lang":
|
|
||||||
select = self._gtk.Button("load", style="color3")
|
|
||||||
select.connect("clicked", self._screen.change_language, option['name'])
|
|
||||||
select.set_hexpand(False)
|
|
||||||
select.set_halign(Gtk.Align.END)
|
|
||||||
dev.add(select)
|
|
||||||
|
|
||||||
opt_array[opt_name] = {
|
|
||||||
"name": option['name'],
|
|
||||||
"row": dev
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = sorted(list(opt_array), key=lambda x: opt_array[x]['name'])
|
|
||||||
pos = opts.index(opt_name)
|
|
||||||
|
|
||||||
self.labels[boxname].insert_row(pos)
|
|
||||||
self.labels[boxname].attach(opt_array[opt_name]['row'], 0, pos, 1, 1)
|
|
||||||
self.labels[boxname].show_all()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user