# SPDX-License-Identifier: WTFPL import os from gi.repository import GLib, Gtk, Gdk from .view import View from yms import gtkutils, combobox, treeview from yms.lookup import Dropdown from yms.model import box class BoxesView(View): def __init__(self, context): super().__init__(context, _("Boxes"), "boxes.ui") self.context = context # box already used for controls so other name here self.sbox = box.Box(context.db) # current record self.newrecord = False self.lst_box = Gtk.ListStore(int, str, str, str) self.load_list() treeview.add_column(self.tree['box'], _('ID'), 0) treeview.add_column(self.tree['box'], _('Label'), 1) treeview.add_column(self.tree['box'], _('Content'), 2) treeview.add_column(self.tree['box'], _('Remarks'), 3) self.tree['box'].set_model(self.lst_box) self.dd_storage = self.context.lookup.storage(context.vessel.vid) combobox.load_from_dict(self.combo['storage'], {-1: _("─── none ───")} | self.dd_storage) self.dd_boxtype = self.context.lookup.dropdown(Dropdown.BOX_TYPE) combobox.load_from_dict(self.combo['type'], {-1: _("─── none ───")} | self.dd_boxtype) def load_list(self): selected_id = treeview.get_value(self.tree['box']) self.lst_box.clear() sql = ( "SELECT b.boxid, b.label, b.content, b.remarks " "FROM box AS b JOIN storage AS s USING (sid) " "WHERE s.vid=%s " "ORDER BY b.label" ) self.db.cur.execute(sql, (self.context.vessel.vid,)) selection = self.tree['box'].get_selection() for row in self.db.cur.fetchall(): treeiter = self.lst_box.append(row) if row[0] == selected_id: # restore previous selected row selection.select_iter(treeiter) def clear_record(self): self.entry['label'].set_text('') combobox.activate_row(self.combo['storage'], 0) combobox.activate_row(self.combo['type'], -1) self.entry['content'].set_text('') self.spin['wx'].set_value(0) self.spin['wy'].set_value(0) self.spin['wz'].set_value(0) self.entry['color'].set_text('#000000') rgba = Gdk.RGBA() rgba.parse('#000000') self.button['color'].set_rgba(rgba) self.spin['price'].set_value(0) self.spin['weight'].set_value(0) self.spin['weight_net'].set_value(0) self.text['remarks'].get_buffer().set_text('') self.entry['label'].grab_focus() def display_record(self): self.label['label'].set_text(self.sbox.label) # storage sid self.label['content'].set_text(self.sbox.content or '') if self.sbox.boxtype > 0: self.label['type'].set_text(self.dd_boxtype[self.sbox.boxtype]) else: self.label['type'].set_text('') self.label['color'].set_text(self.sbox.color or '') if self.sbox.color: self.label['color'].set_markup(f'#{self.sbox.color} ●') else: self.label['color'].set_text('') self.label['weight'].set_text(self.format_float(self.sbox.weight, 2, suffix='kg')) self.label['weight_net'].set_text(self.format_float(self.sbox.weight_net, 2, suffix='kg')) self.label['price'].set_text(self.format_float(self.sbox.price, 2, suffix='€')) self.label['remarks'].set_text(self.sbox.remarks or '') def edit_record(self): self.entry['label'].set_text(self.sbox.label) combobox.activate_row(self.combo['storage'], self.sbox.sid) combobox.activate_row(self.combo['type'], self.sbox.boxtype) self.entry['content'].set_text(self.sbox.content or '') self.spin['wx'].set_value(self.sbox.wx or 0) self.spin['wy'].set_value(self.sbox.wy or 0) self.spin['wz'].set_value(self.sbox.wz or 0) self.entry['color'].set_text('#' + self.sbox.color) rgba = Gdk.RGBA() rgba.parse('#' + self.sbox.color) self.button['color'].set_rgba(rgba) self.spin['price'].set_value(self.sbox.price or 0) self.spin['weight'].set_value(self.sbox.weight or 0) self.spin['weight_net'].set_value(self.sbox.weight_net or 0) self.text['remarks'].get_buffer().set_text(self.sbox.remarks or '') self.entry['label'].grab_focus() def save_record(self): # fields must have been validated beforehand self.sbox.label = self.entry['label'].get_text() self.sbox.sid = combobox.get_list_value(self.combo['storage']) self.sbox.boxtype = combobox.get_list_value(self.combo['type']) self.sbox.content = self.entry['content'].get_text() self.sbox.wx = int(self.spin['wx'].get_value()) self.sbox.wy = int(self.spin['wy'].get_value()) self.sbox.wz = int(self.spin['wz'].get_value()) self.sbox.color = self.entry['color'].get_text().lstrip('#') self.sbox.price = self.spin['price'].get_value() self.sbox.weight = self.spin['weight'].get_value() self.sbox.weight_net = self.spin['weight_net'].get_value() self.sbox.remarks = gtkutils.get_text_contents(self.text['remarks']) # buttons on list page def on_but_new_clicked(self, widget): self.newrecord = True self.label['title_edit'].set_text(_("Add new Box")) self.clear_record() self.topwidget.set_visible_child(self.box['box_edit']) def on_but_edit_clicked(self, widget): self.newrecord = False self.label['title_edit'].set_text(_("Edit Box")) boxid = treeview.get_value(self.tree['box'], 0) if not boxid: return self.sbox.load(boxid) self.edit_record() self.topwidget.set_visible_child(self.box['box_edit']) def on_but_detail_clicked(self, widget): boxid = treeview.get_value(self.tree['box'], 0) if not boxid: return self.sbox.load(boxid) self.display_record() self.topwidget.set_visible_child(self.box['box_detail']) self.button['close'].grab_focus() def on_tvw_box_row_activated(self, widget, path, column): boxid = treeview.get_value(widget, 0) self.sbox.load(boxid) self.display_record() self.topwidget.set_visible_child(self.box['box_detail']) self.button['close'].grab_focus() # buttons on detail page # edit2 uses edit callback def on_but_close_clicked(self, widget): self.topwidget.set_visible_child(self.box['box']) def on_but_delete_clicked(self, widget): answer = gtkutils.questionbox( _("Delete box!\n" "Are you sure?"), title="YMS" ) if answer == False: return self.topwidget.set_visible_child(self.box['box']) self.newrecord = False # buttons on edit page def on_but_save_clicked(self, widget): # check mandatory fields if not self.entry['label'].get_text().strip(): # TODO error marker return self.save_record() print(self.sbox) self.sbox.save() if self.newrecord: self.load_list() self.newrecord = False self.display_record() self.topwidget.set_visible_child(self.box['box_detail']) def on_but_color_set(self, button): rgba = button.get_rgba() r = round(rgba.red * 255) g = round(rgba.green * 255) b = round(rgba.blue * 255) #self.last_valid_rgb = f"{r:02X}{g:02X}{b:02X}" self.entry['color'].set_text(f"#{r:02X}{g:02X}{b:02X}") def on_ent_color_activate(self, entry): self.set_color_from_entry(entry) def on_ent_color_focus_out_event(self, entry, event): self.set_color_from_entry(entry) return False def on_but_cancel_clicked(self, widget): self.topwidget.set_visible_child(self.box['box']) self.newrecord = False