231 lines
9.5 KiB
Python
231 lines
9.5 KiB
Python
# SPDX-License-Identifier: WTFPL
|
|
|
|
import os
|
|
from gi.repository import Gtk, Gdk
|
|
|
|
from .view import View
|
|
from yms import gtkutils, combobox, treeview
|
|
from yms.lookup import Dropdown
|
|
from yms.model import cable
|
|
|
|
class CablesView(View):
|
|
|
|
def __init__(self, context):
|
|
super().__init__(context, _("Cables"), "cables.ui")
|
|
|
|
self.cable = cable.Cable(context.db) # current record
|
|
self.newrecord = False
|
|
|
|
self.lst_cables = Gtk.ListStore(int, str, str, str)
|
|
self.load_list()
|
|
|
|
treeview.add_column(self.tree['cables'], _('ID'), 0)
|
|
treeview.add_column(self.tree['cables'], _('Name'), 1)
|
|
treeview.add_column(self.tree['cables'], _('Condition'), 2)
|
|
treeview.add_column(self.tree['cables'], _('Length'), 3)
|
|
self.tree['cables'].set_model(self.lst_cables)
|
|
|
|
# Cable type
|
|
self.dd_type = self.context.lookup.dropdown(Dropdown.CABLE_TYPE)
|
|
combobox.load_from_dict(self.combo['type'], {-1: _("─── none ───")} | self.dd_type)
|
|
# Cable tech type
|
|
self.dd_tech = self.context.lookup.dropdown(Dropdown.CABLE_TECHNAME)
|
|
combobox.load_from_dict(self.combo['techname'], {-1: _("─── none ───")} | self.dd_tech)
|
|
# Supplier
|
|
self.dd_supplier = self.context.lookup.company(1)
|
|
combobox.load_from_dict(self.combo['supplier'], {-1: _("─── none ───")} | self.dd_supplier)
|
|
# Manufacturer
|
|
self.dd_manufacturer = self.context.lookup.company(2)
|
|
combobox.load_from_dict(self.combo['manufacturer'], {-1: _("─── none ───")} | self.dd_manufacturer)
|
|
# Condition
|
|
self.dd_condition = self.context.lookup.enum('cable.cablecond')
|
|
combobox.load_from_dict(self.combo['condition'], self.dd_condition, 'unknown')
|
|
|
|
def load_list(self):
|
|
selected_id = treeview.get_value(self.tree['cables'])
|
|
self.lst_cables.clear()
|
|
sql = (
|
|
"SELECT cableid, cablename, cablecond, length "
|
|
"FROM cable "
|
|
"WHERE vid=%s "
|
|
"ORDER BY cableid"
|
|
)
|
|
self.db.dcur.execute(sql, (self.context.vessel.vid,))
|
|
selection = self.tree['cables'].get_selection()
|
|
for row in self.db.dcur.fetchall():
|
|
data = (
|
|
row['cableid'],
|
|
row['cablename'],
|
|
row['cablecond'],
|
|
self.format_float(row['length'], suffix='m')
|
|
)
|
|
treeiter = self.lst_cables.append(data)
|
|
if row['cableid'] == selected_id:
|
|
# restore previous selected row
|
|
selection.select_iter(treeiter)
|
|
|
|
def display_record(self):
|
|
self.label['name'].set_text(self.cable.cablename or '')
|
|
if self.cable.cabletype:
|
|
self.label['type'].set_text(self.dd_type[self.cable.cabletype])
|
|
else:
|
|
self.label['type'].set_text('')
|
|
if self.cable.techname:
|
|
self.label['techname'].set_text(self.dd_tech[self.cable.techname])
|
|
else:
|
|
self.label['techname'].set_text('')
|
|
self.label['length'].set_text(self.format_int(self.cable.length))
|
|
self.label['conductors'].set_text(self.format_int(self.cable.conductors))
|
|
self.label['diameter'].set_text(self.format_float(self.cable.diameter, 1))
|
|
self.label['xsection'].set_text(self.format_float(self.cable.xsection, 1))
|
|
self.label['weight'].set_text(self.format_float(self.cable.weight))
|
|
if self.cable.color:
|
|
self.label['color'].set_markup(f'#{self.cable.color} <span foreground="#{self.cable.color}">●</span>')
|
|
else:
|
|
self.label['color'].set_text('')
|
|
self.label['supplier'].set_text(str(self.cable.supplier))
|
|
self.label['manufacturer'].set_text(str(self.cable.manufacturer))
|
|
self.label['condition'].set_text(self.cable.cablecond)
|
|
self.label['remarks'].set_text(self.cable.remarks or '')
|
|
|
|
def edit_record(self):
|
|
# fill UI fields from database record
|
|
self.entry['name'].set_text(self.cable.cablename or '')
|
|
combobox.activate_row(self.combo['type'], self.cable.cabletype or -1)
|
|
combobox.activate_row(self.combo['techname'], self.cable.techname or -1)
|
|
self.spin['length'].set_value(self.cable.length or 0)
|
|
self.spin['conductors'].set_value(self.cable.conductors or 0)
|
|
self.spin['diameter'].set_value(self.cable.diameter or 0)
|
|
self.spin['xsection'].set_value(self.cable.xsection or 0)
|
|
self.spin['weight'].set_value(self.cable.weight or 0)
|
|
self.entry['color'].set_text(self.format_color(self.cable.color))
|
|
if self.cable.color is None:
|
|
rgba = Gdk.RGBA(0, 0, 0, 0)
|
|
else:
|
|
rgba = Gdk.RGBA()
|
|
rgba.parse('#' + self.cable.color)
|
|
self.button['color'].set_rgba(rgba)
|
|
combobox.activate_row(self.combo['supplier'], self.cable.supplier or -1)
|
|
combobox.activate_row(self.combo['manufacturer'], self.cable.manufacturer or -1)
|
|
combobox.activate_row(self.combo['condition'], self.cable.cablecond or 'unknown')
|
|
self.text['remarks'].get_buffer().set_text(self.cable.remarks or '')
|
|
self.entry['name'].grab_focus()
|
|
|
|
def save_record(self):
|
|
# fields must have been validated beforehand
|
|
self.cable.vid = self.context.vessel.vid
|
|
self.cable.cablename = self.entry['name'].get_text().strip()
|
|
self.cable.cabletype = combobox.get_list_value(self.combo['type'])
|
|
if self.cable.cabletype == -1:
|
|
self.cable.cabletype = None
|
|
self.cable.length = int(self.spin['length'].get_value())
|
|
self.cable.conductors = int(self.spin['conductors'].get_value())
|
|
self.cable.diameter = self.spin['diameter'].get_value()
|
|
self.cable.xsection = self.spin['xsection'].get_value()
|
|
self.cable.weight = int(self.spin['weight'].get_value())
|
|
if self.button['color'].get_rgba().alpha > 0:
|
|
self.cable.color = self.entry['color'].get_text().lstrip('#')
|
|
else:
|
|
self.cable.color = None
|
|
self.cable.supplier = combobox.get_list_value(self.combo['supplier'])
|
|
if self.cable.supplier == -1:
|
|
self.cable.supplier = None
|
|
self.cable.manufacturer = combobox.get_list_value(self.combo['manufacturer'])
|
|
if self.cable.manufacturer == -1:
|
|
self.cable.manufacturer = None
|
|
self.cable.cablecond = combobox.get_list_value(self.combo['condition'])
|
|
self.cable.remarks = gtkutils.get_text_contents(self.text['remarks'])
|
|
|
|
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)
|
|
a = round(rgba.alpha * 255)
|
|
if a > 0:
|
|
self.entry['color'].set_text(f"#{r:02X}{g:02X}{b:02X}")
|
|
else:
|
|
self.entry['color'].set_text('')
|
|
|
|
def on_ent_color_activate(self, entry):
|
|
self.set_color_from_entry(entry, self.button['color'])
|
|
|
|
def on_ent_color_focus_out_event(self, entry, event):
|
|
self.set_color_from_entry(entry, self.button['color'])
|
|
return False
|
|
|
|
# buttons on list page
|
|
def on_but_new_clicked(self, widget):
|
|
self.newrecord = True
|
|
self.cable.clear()
|
|
self.edit_record()
|
|
self.topwidget.set_visible_child(self.box['cable_edit'])
|
|
|
|
def on_but_edit_clicked(self, widget):
|
|
cableid = treeview.get_value(self.tree['cables'], 0)
|
|
if not cableid:
|
|
return
|
|
self.newrecord = False
|
|
self.label['title_edit'].set_text(_("Edit Cable"))
|
|
if not self.record_lock('cable', cableid):
|
|
return
|
|
self.cable.load(cableid)
|
|
self.edit_record()
|
|
self.topwidget.set_visible_child(self.box['cable_edit'])
|
|
|
|
def on_but_detail_clicked(self, widget):
|
|
cableid = treeview.get_value(self.tree['cables'], 0)
|
|
if not cableid:
|
|
return
|
|
self.cable.load(cableid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['cable_detail'])
|
|
self.button['close'].grab_focus()
|
|
|
|
def on_tvw_cables_row_activated(self, widget, path, column):
|
|
cableid = treeview.get_value(widget, 0)
|
|
self.cable.load(cableid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['cable_detail'])
|
|
|
|
# buttons on detail page
|
|
# edit2 uses edit callback
|
|
def on_but_close_clicked(self, widget):
|
|
self.topwidget.set_visible_child(self.box['cables'])
|
|
|
|
def on_but_delete_clicked(self, widget):
|
|
answer = gtkutils.questionbox(
|
|
_("Delete cable!\n"
|
|
"Are you sure?"), title="YMS"
|
|
)
|
|
if answer == False:
|
|
return
|
|
self.topwidget.set_visible_child(self.box['cables'])
|
|
|
|
# buttons on edit page
|
|
def on_but_save_clicked(self, widget):
|
|
# check mandatory fields:
|
|
# name, type, length, conductors, xsection
|
|
cname = self.entry['name'].get_text().strip()
|
|
if not cname:
|
|
# TODO error marker
|
|
return
|
|
self.save_record()
|
|
print(self.cable)
|
|
if not self.cable.save():
|
|
print("ERROR saving cable")
|
|
# TODO error in infobox
|
|
print(self.context.db.last_error())
|
|
return
|
|
self.record_unlock()
|
|
if self.newrecord:
|
|
self.load_list()
|
|
self.newrecord = False
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['cable_detail'])
|
|
|
|
def on_but_cancel_clicked(self, widget):
|
|
self.record_unlock()
|
|
self.topwidget.set_visible_child(self.box['cables'])
|
|
|