From 5aea1a037772168994761b1697d7f487ca729c27 Mon Sep 17 00:00:00 2001 From: mistangl Date: Tue, 16 Jan 2024 16:44:27 +0100 Subject: [PATCH] =?UTF-8?q?assembly=20id=20hinzugef=C3=BCgt.=20Linke=20Fra?= =?UTF-8?q?me=20wereitert.=20to=5Fjson=20und=20from=5Fjson=20implementiert?= =?UTF-8?q?.=20load=20and=20save=20implementiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/CreoMigrateGui.py | 131 +++++++++++++++++++++++++-------------- lib/caditem.py | 82 ++++++++++++++++++++----- lib/controller.py | 1 - lib/test_gui.py | 140 +++++++++++++++++++----------------------- 4 files changed, 215 insertions(+), 139 deletions(-) diff --git a/lib/CreoMigrateGui.py b/lib/CreoMigrateGui.py index 1b874a9..c594c63 100644 --- a/lib/CreoMigrateGui.py +++ b/lib/CreoMigrateGui.py @@ -8,19 +8,21 @@ from caditem import CadItem #logger = logging.getLogger(__name__) class TreeView(): - def __init__(self, frame): + def __init__(self, frame, nrow=0): # a treeview # hier Anzahl der sichtbaren Zeilen des Baums angeben: self.tree = ttk.Treeview(frame, height=40) # TODO hier anstatt root die Statistik rein schreiben. self.tree.heading('#0', text='root', anchor=tk.W) + self.tree.grid(row=nrow, column=0, sticky=tk.NSEW) # add a scrollbar scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=self.tree.yview) self.tree.configure(yscroll=scrollbar.set) - scrollbar.grid(row=0, column=1, sticky='ns') + scrollbar.grid(row=nrow, column=1, sticky='ns') - self.tree.bind('<>', self.item_selected) + #self.tree.bind('<>', self.item_selected) # Einfachklick + self.tree.bind('', self.item_selected) # Doppelklick: def set_controller(self, controller): self.controller = controller @@ -61,9 +63,9 @@ class TreeView(): self.tree.tag_configure('good', background='white') self.tree.tag_configure('missing', background='red') - def data_from_model(self, controller): - parent_children_mapping = controller.get_parent_children() - red_ids = controller.get_red_ids() + def data_from_model(self): + parent_children_mapping = self.controller.get_parent_children() + red_ids = self.controller.get_red_ids() self.refresh(parent_children_mapping, red_ids) def item_selected(self, event): @@ -74,14 +76,13 @@ class TreeView(): self.controller.set_gui_data('teilenummer', cid) self.controller.refresh_gui() - def get_handle(self): - return self.tree -class AllRightFrames(): +class AllFrames(): """ enthält alle gleichen Methoden für die Widgets in der rechte Spalte """ def __init__(self, nrow=0): self.set_start_rwo(nrow) + self._controller = None def get_rows(self): return self.rownum @@ -91,6 +92,11 @@ class AllRightFrames(): raise Exception("this row can not be set") self.rownum = nrow + def set_controller(self, controller): + self._controller = controller + def get_controller(self): + return self._controller + def data_to_model(self, controller): data = self.get_data() controller.set_gui_dict(data) @@ -98,9 +104,38 @@ class AllRightFrames(): def data_to_model(self, controller, key, value): controller.set_gui_data(key, value) -class RightFrameLabels(AllRightFrames): +class LeftFrameAsm(AllFrames): def __init__(self, frame, nrow=0): - AllRightFrames.__init__(self, nrow) + AllFrames.__init__(self, nrow) + self.frame = frame + self.build_widgets() + + def build_widgets(self): + frame = self.frame + self.rownum = AllFrames.get_rows(self) + col_left = 0 + # Baugruppennummer + asm_label = ttk.Label(frame, text="Baugruppennummer:" ) + asm_label.grid(column=col_left, row=self.rownum, sticky=tk.W, padx=5, pady=5) + self.rownum += 1 + self.asm_number = tk.StringVar() + asm_entry = ttk.Entry(frame, textvariable=self.asm_number, width=9) + asm_entry.grid(column=col_left, row=self.rownum, sticky=tk.W, padx=5, pady=5) + self.rownum += 1 + + def get_data(self): + return { + "asm_id": self.asm_number.get(), + } + + def data_from_model(self): + data = self.get_controller().get_all_gui_data() + if 'asm_id' in data: + self.asm_number.set(data["asm_id"]) + +class RightFrameLabels(AllFrames): + def __init__(self, frame, nrow=0): + AllFrames.__init__(self, nrow) self.frame = frame self.build_widgets() @@ -131,6 +166,7 @@ class RightFrameLabels(AllRightFrames): self.teilenummer_text = tk.StringVar() teilenummer_entry = ttk.Entry(frame, textvariable=self.teilenummer_text) teilenummer_entry.grid(column=col_left+1, row=self.rownum, sticky=tk.W, padx=5, pady=5) + teilenummer_entry.bind('<>', self.combo_callback) self.rownum += 1 separator2 = ttk.Separator(frame, orient='horizontal') separator2.grid(column=col_left, columnspan=3, row=self.rownum, sticky=tk.EW, padx=5, pady=5) @@ -142,14 +178,20 @@ class RightFrameLabels(AllRightFrames): "teilennummer": self.teilenummer_text.get(), } - def data_from_model(self, controller): - data = controller.get_all_gui_data() + def data_from_model(self): + data = self.get_controller().get_all_gui_data() if 'teilenummer' in data: self.teilenummer_text.set(data["teilenummer"]) -class RightFramePDM(AllRightFrames): + def combo_callback(self, event): + """ handle the changed event of TK""" + # TODO falls nötig + a = 6 + pass + +class RightFramePDM(AllFrames): def __init__(self, frame, nrow=0): - AllRightFrames.__init__(self, nrow) + AllFrames.__init__(self, nrow) self.frame = frame self.build_widgets() @@ -164,7 +206,7 @@ class RightFramePDM(AllRightFrames): pdmkategorie_combo = ttk.Combobox(frame, textvariable=self.pdm_category) pdmkategorie_combo['values'] = ["Baugruppe", "Einzelteil", "Kaufteil", "NichtSivas", "Normteil", "Schweissbaugruppe"] pdmkategorie_combo['state'] = 'readonly' - pdmkategorie_combo.bind('<>', self.combo_callbacks) # falls nötig + pdmkategorie_combo.bind('<>', self.combo_callback) # falls nötig pdmkategorie_combo.grid(column=col_left+2, row=self.rownum, sticky=tk.W, padx=5, pady=5) self.rownum += 1 @@ -176,9 +218,9 @@ class RightFramePDM(AllRightFrames): neuaufbau_checkbutton = ttk.Checkbutton(frame, text='ja', variable=self.neuaufbau, - onvalue="True", - offvalue="False") - #command=self.checkbox_callback, + command=self.checkbox_callback, + onvalue=True, + offvalue=False) neuaufbau_checkbutton.grid(column=col_left+2, row=self.rownum, sticky=tk.W, padx=5, pady=5) self.rownum += 1 @@ -189,7 +231,7 @@ class RightFramePDM(AllRightFrames): lebenszyklusstatus_combo = ttk.Combobox(frame, textvariable=self.lebenszyklus_status_neu) lebenszyklusstatus_combo['values'] = ["Serie", "Prototyp"] lebenszyklusstatus_combo['state'] = 'readonly' - lebenszyklusstatus_combo.bind('<>', self.combo_callbacks) + lebenszyklusstatus_combo.bind('<>', self.combo_callback) lebenszyklusstatus_combo.grid(column=col_left+2, row=self.rownum, sticky=tk.W, padx=5, pady=5) self.rownum += 1 @@ -200,9 +242,9 @@ class RightFramePDM(AllRightFrames): aushalbzeug_checkbuttonr = ttk.Checkbutton(frame, text='neu', variable=self.aus_halbzeug_neu, - onvalue="True", - offvalue="False") - #command=self.checkbox_callback, + command=self.checkbox_callback, + onvalue=True, + offvalue=False) aushalbzeug_checkbuttonr.grid(column=col_left+2, row=self.rownum, sticky=tk.W, padx=5, pady=5) self.rownum += 1 @@ -223,8 +265,8 @@ class RightFramePDM(AllRightFrames): "zusatzinfos_txt_neu": self.zusatzinfos_text.get(), } - def data_from_model(self, controller): - data = controller.get_all_gui_data() + def data_from_model(self): + data = self.get_controller().get_all_gui_data() if 'pdm_kategorie_neu' in data: self.pdm_category.set(data["pdm_kategorie_neu"]) if 'neuaufbau' in data: @@ -236,15 +278,10 @@ class RightFramePDM(AllRightFrames): if 'zusatzinfos_text_neu' in data: self.zusatzinfos_text.set(data["zusatzinfos_text_neu"]) - def combo_callbacks(self, event): - """ handle the changed event of TK""" - # TODO falls nötig - a = 6 + def combo_callback(self, event): pass - def checkbox_callback(self, event): - """ handle the changed event of TK""" - a = 6 + def checkbox_callback(self): pass @@ -271,9 +308,9 @@ class App(): style = ttk.Style(self.root) style.theme_use('classic') - self.tv = TreeView(left_frame) - gridhdl = self.tv.get_handle() - gridhdl.grid(row=0, column=0, sticky=tk.NSEW) + self.lf1 = LeftFrameAsm(left_frame, 0) + nrows = self.lf1.get_rows() + self.tv = TreeView(left_frame, nrows) self.rf0 = RightFrameLabels(right_frame, 0) nrows = self.rf0.get_rows() @@ -285,15 +322,17 @@ class App(): def data_from_model(self): """ methode zum auslesen aller Daten der Oberfläche aus dem Modell """ - self.tv.data_from_model(self.controller) - self.rf0.data_from_model(self.controller) - self.rf1.data_from_model(self.controller) + self.tv.data_from_model() + self.rf0.data_from_model() + self.rf1.data_from_model() def set_controller(self, controller): - # merken des Handels auf den Controller - self.controller = controller - # der Tree braucht aufgrund des Callbacks der Selektion auch einen Verweis + # merken des Handels auf den Controller in jedem widget + self._controller = controller + self.tv.set_controller(controller) + self.rf0.set_controller(controller) + self.rf1.set_controller(controller) def mainloop(self): self.root.mainloop() @@ -309,9 +348,9 @@ if __name__ == "__main__": data model objects. The CONTROLLER receives the input, optionally validates it and then passes the input to the model. - MODEL - <-updates- ^--manipulates-- - VIEW CONTROLLER - --sees --> -- uses --> - USER + + <--fills gui -- --manipulates--> + --user input-> <--give model data-- + + """ diff --git a/lib/caditem.py b/lib/caditem.py index 7fba644..b9d31fb 100644 --- a/lib/caditem.py +++ b/lib/caditem.py @@ -1,10 +1,6 @@ import json from controller import Controller - - - - class CadItem(): """ This item contains all data for writing all necessary data to the solid edg cad file @@ -34,7 +30,8 @@ class CadItem(): "TOS_Trolley_Projekte", "TOS_Trolley_Omniflo", "TOS_Trolley_Spinnerei", "TOS_Trolley_ILS", "Änderungsbeschreibung", "Freigabevermerk", "ZusaetzlicheInformationen"] - gui_data_keys = [ "teilenummer", + gui_data_keys = [ "asm_id", + "teilenummer", "pdm_kategorie_neu", "neuaufbau", "lebenszyklus_status_neu", @@ -48,22 +45,22 @@ class CadItem(): # TODO siehe Layout Entwurf svg ] - def __init__(self, asm_id): + def __init__(self): """ is initalized with its unique id""" - self.set_asm_id(asm_id) self.red_ids = {} self.parent_children_mapping = {} self.data = {} self.gui_data = {} + self.read_only_items = {} self.controller = None def set_controller(self, controller): self.controller = controller def set_asm_id(self, asm_id): - self.asm_id = asm_id - def get_current_asm_id(self): - return self.asm_id + self.set_gui_data("asm_id", asm_id) + def get_asm_id(self): + return self.get_gui_data("asm_id") def get_current_part_id(self): return self.get_gui_data("teilenummer") @@ -84,6 +81,20 @@ class CadItem(): def get_red_ids(self): return self.red_ids + # Methoden um die Verwendbarkeit einzelner Schalter auszugrauen + def set_read_only_dict(self, data:dict): + for k,v in data: + self.set_read_only(k,v) + def set_read_only(self, key, val): + if key not in self.gui_data: + raise Exception("this data can not be set") + else: + self.read_only_items[key] = val + def get_read_only(self, key): + return self.read_only_items[key] + def get_all_read_only_data(self): + return self.read_only_items + # Daten zum Impfen merken def set_data_dict(self, data:dict): for k,v in data: @@ -101,7 +112,7 @@ class CadItem(): """ gibt alle enthaltenen Daten zurück """ return self.data - # Daten in der GUI + # Daten die in der GUI gespeichert werden sollen def set_gui_dict(self, data:dict): """ setzt die Daten der GUI """ for k,v in data.items(): @@ -118,14 +129,58 @@ class CadItem(): def get_all_gui_data(self): return self.gui_data - def load(): + def from_json(self, json_str): + jsonobj = json.loads(json_str) + + if "gui_data" in jsonobj: + gui_data = jsonobj["gui_data"] + self.set_gui_dict(gui_data) + else: + raise Exception("no gui_data given in string") + + if "data" in jsonobj: + data = jsonobj["data"] + self.set_data_dict(data) + else: + raise Exception("no data given in string") + + if "parent_children_mapping" in jsonobj: + parent_children_mapping = jsonobj["parent_children_mapping"] + self.set_parent_children(parent_children_mapping) + else: + raise Exception("no parent_children_mapping given in string") + if "red_ids" in jsonobj: + red_ids = jsonobj["red_ids"] + self.set_red_ids(red_ids) + + def to_json(self): + jsonobj = dict() + + # die Oberflächendaten + gui_data = self.get_gui_dict() + jsonobj["gui_data"] = gui_data + + # die Impfdaten + data = self.get_all_data() + jsonobj["data"] = data + + # der Baum + parent_children_mapping = self.get_parent_children() + jsonobj["parent_children_mapping"] = parent_children_mapping + + # die ids der nicht migrierten Dateien + red_ids = self.get_red_ids() + jsonobj["red_ids"] = red_ids + return json.dumps(jsonobj) + + def load(self, path): #try: # with open('data.json', 'r') as f: # self.model.todos = json.load(f) #except Exception: # pass pass - def save(): + def save(self, path): #with open('data.json', 'w') as f: # data = json.dump(self.model.data, f) pass @@ -153,7 +208,6 @@ class CadItemRepository: def update(self, old_item, new_item): self.delete(old_item.id) self.add(new_item) - raise Exception("CadItem not found") def delete(self, id): for p in self.caditems: diff --git a/lib/controller.py b/lib/controller.py index 9e052e7..9ff673f 100644 --- a/lib/controller.py +++ b/lib/controller.py @@ -9,7 +9,6 @@ class Controller(): def __init__(self, model, view): self.set_model(model) self.set_view(view) - #Observer.__init__(self) def set_model(self, model): self.model = model diff --git a/lib/test_gui.py b/lib/test_gui.py index 77df455..31f0d5c 100644 --- a/lib/test_gui.py +++ b/lib/test_gui.py @@ -3,84 +3,68 @@ from caditem import CadItem from CreoMigrateGui import App from controller import Controller - -red_ids = ( '400102274', '400102275', '400102190', '400102198', - '400102161', '400102277', '400102163', '400102278', - '400102282', '400102284', '400102195', 'MA0000008', - 'MA0000004', '929055170', '400102267', '400102235', - '400102236', 'MA0000006', '400102237', '400102238', - '400102239', '400102268', '829416159', '924031058', - '924031081', '822064091', '822066199', '822066200') - -parent_children_mapping = {'400102196': ['400102125', '400102126', '400102130', - '400102161', '400102163', '400102165', - '400102166', '400102167', '400102168', - '400102190', '400102195', '400102198', - '400102246', '400102247', '400102267', - '400102272', '400102274', '400102275', - '400102277', '400102278', '400102282', - '400102284', '400102417', '720002003', - '721001034', '821114025', '822064091', - '822066085', '822996075', '829416061', - '829516004', '911021113', '911021181', - '911021259', '911091054', '911091096', - '911091097', '911251013', '912011009', - '919050006', '924031058', - '924031081'], - '400102282': ['822066026', '822066089', '919010052'], - '400102195': ['822066075', '822066216', - '822066154', '822066101', '822064090', - '822066196', '400102129', '822066217', - '822066160', '822066215', '913041045', - '913011009', '911011057', '822064035', - '925011006', '911091041', '913051040', - '911021112', '911021166', '825006003', - '912041005', '911011014', '911011009', - '911021144', '911021001', '913011016', - '929055170', '913011010', '911021035', - '829416065', '712000151', - '712000100'], - '822066075': ['822066157', '822066162', '911261023'], - '822066162': ['822064013'], - '822066196': ['MA0000008'], - '822066160': ['MA0000004'], - '822064035': ['822066065'], - '822064093': ['822066204', '822066205', '822066206', '822066207', '822066208'], - '822066220': ['924031033'], - '822064091': ['822066212', '822064093', '822066220', - '822066200', '912011010', '911021156', - '822066201', '911071027', '913011109', - '911011070', '913011001', '710005021', - '919010055', '720000008', '720000007'], - '400102267': ['400102235', '829416159', '829416077', - '829416079', '911021260', '911021147', - '911251014'], - '400102235': ['400102236', '400102237', '400102238', - '400102239', '400102268', '911021143', - '911021054', '911021055'], - '400102236': ['MA0000006'], - '829416159': ['829416089'], - '400102130': ['400102142'], - '821116004': ['822026004'], - '400102168': ['821116001'], - '821114025': ['911081007', '821116053'], - '400102167': ['821116004']} - - - -model = CadItem("400102196") -gui_data = { - "teilenummer":"400102142", - "pdm_kategorie_neu":"Einzelteil", - "neuaufbau" :"False", - "lebenszyklus_status_neu" :"Serie", - "aus_halbzeug_neu" :"True", - "zusatzinfos_text_neu": 'Dies sind neue Zusatzinfos', - } -model.set_gui_dict(gui_data) -model.set_parent_children(parent_children_mapping) -model.set_red_ids(red_ids) - +json_str = """ +{ + "parent_children_mapping": { + "400102130": [ "400102142" ], + "400102167": [ "821116004" ], + "400102168": [ "821116001" ], + "400102195": [ "822066075", "822066216", "822066154", "822066101", + "822064090", "822066196", "400102129", "822066217", "822066160", + "822066215", "913041045", "913011009", "911011057", "822064035", + "925011006", "911091041", "913051040", "911021112", "911021166", + "825006003", "912041005", "911011014", "911011009", "911021144", + "911021001", "913011016", "929055170", "913011010", "911021035", + "829416065", "712000151", "712000100" ], + "400102196": [ "400102125", "400102126", "400102130", "400102161", + "400102163", "400102165", "400102166", "400102167", "400102168", + "400102190", "400102195", "400102198", "400102246", "400102247", + "400102267", "400102272", "400102274", "400102275", "400102277", + "400102278", "400102282", "400102284", "400102417", "720002003", + "721001034", "821114025", "822064091", "822066085", "822996075", + "829416061", "829516004", "911021113", "911021181", "911021259", + "911091054", "911091096", "911091097", "911251013", "912011009", + "919050006", "924031058", "924031081" ], + "400102235": [ "400102236", "400102237", "400102238", "400102239", + "400102268", "911021143", "911021054", "911021055" ], + "400102236": [ "MA0000006" ], + "400102267": [ "400102235", "829416159", "829416077", "829416079", + "911021260", "911021147", "911251014" ], + "400102282": [ "822066026", "822066089", "919010052" ], + "821114025": [ "911081007", "821116053" ], + "821116004": [ "822026004" ], + "822064035": [ "822066065" ], + "822064091": [ "822066212", "822064093", "822066220", "822066200", + "912011010", "911021156", "822066201", "911071027", "913011109", + "911011070", "913011001", "710005021", "919010055", "720000008", "720000007" ], + "822064093": [ "822066204", "822066205", "822066206", "822066207", "822066208" ], + "822066075": [ "822066157", "822066162", "911261023" ], + "822066160": [ "MA0000004" ], + "822066162": [ "822064013" ], + "822066196": [ "MA0000008" ], + "822066220": [ "924031033" ], + "829416159": [ "829416089" ] + }, + "red_ids": [ "400102274", "400102275", "400102190", "400102198", "400102161", + "400102277", "400102163", "400102278", "400102282", "400102284", + "400102195", "MA0000008", "MA0000004", "929055170", "400102267", + "400102235", "400102236", "MA0000006", "400102237", "400102238", + "400102239", "400102268", "829416159", "924031058", "924031081", + "822064091", "822066199", "822066200" ], + "gui_data": { + "asm_id": "400102196", + "teilenummer": "400102142", + "pdm_kategorie_neu": "Einzelteil", + "neuaufbau": "False", + "lebenszyklus_status_neu": "Serie", + "aus_halbzeug_neu": "True", + "zusatzinfos_text_neu": "Dies sind neue Zusatzinfos" + }, + "data": {} +} +""" +model = CadItem() +model.from_json(json_str) root = tk.Tk() app = App(root)