Files
CreoMigrate/lib/CreoMigrateGui.py
T

297 lines
12 KiB
Python

import tkinter as tk
from tkinter import ttk
#import logging
#logger = logging.getLogger(__name__)
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']}
class TreeView():
def __init__(self, frame):
# a treeview
self.tree = ttk.Treeview(frame)
# TODO hier anstatt root die Statistik rein schreiben.
self.tree.heading('#0', text='root', anchor=tk.W)
def clear(self):
self.tree.clipboard_clear()
def create(self, parent_children_mapping, red_ids):
# adding data of first column after root
pool = set()
for parent,children in parent_children_mapping.items():
if parent not in pool:
self.tree.insert('', tk.END, text=parent, iid=parent, open=True)
pool.add(parent)
cn = children
# sortiere die Zahlen im Baum immer aufsteigend
cn.sort(reverse=True)
for child_id in cn:
if child_id not in pool:
pool.add(child_id)
if child_id not in red_ids: rowtags = ('good')
else: rowtags = ('missing')
self.tree.insert('', tk.END, text=child_id, iid=child_id, tags=rowtags, open=True)
#self.tree.move(child_id, parent, 0)
# einmal über alles gehen und alle keys an die richtige Stelle schieben
for parent,children in parent_children_mapping.items():
for child_id in children:
self.tree.move(child_id, parent, 0)
# https://www.askpython.com/python-modules/tkinter/tkinter-colors
#self.tree.tag_configure('good', background='LawnGreen')
self.tree.tag_configure('good', background='white')
self.tree.tag_configure('missing', background='red')
def get_handle(self):
return self.tree
class App():
def __init__(self, root, controller):
self.root = root
self.controller = controller
# configure the grid
root.columnconfigure(0, weight=2)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
root.columnconfigure(3, weight=1)
root.geometry('800x700')
root.resizable(0, 0)
style = ttk.Style(self.root)
style.theme_use('classic')
# style.configure('Error.TLabel', foreground='white')
# style.configure('Error.TLabel', background='red')
# style.configure('Error.TLabel', font=('Helvetica', 12))
# style.configure('Error.TLabel', padding=(10, 10))
self.tv = TreeView(self.root)
self.tv.create(parent_children_mapping, red_ids)
gridhdl = self.tv.get_handle()
gridhdl.grid(row=0, column=0, rowspan=2, sticky=tk.NSEW)
# message = 'This is an error message!'
# label = ttk.Label(right_list, text=message, style='Error.TLabel')
self.create_widgets()
def create_widgets(self):
# username
username_label = ttk.Label(self.root, text="Username:")
username_label.grid(column=1, row=0, sticky=tk.W, padx=5, pady=5, bg="green")
username_entry = ttk.Entry(self.root)
username_entry.grid(column=2, row=0, sticky=tk.E, padx=5, pady=5)
# password
password_label = ttk.Label(self.root, text="Password:")
password_label.grid(column=1, row=1, sticky=tk.W, padx=5, pady=5)
password_entry = ttk.Entry(self.root, show="*")
password_entry.grid(column=2, row=1, sticky=tk.E, padx=5, pady=5)
# login button
login_button = ttk.Button(self.root, text="Login")
login_button.grid(column=2, row=2, sticky=tk.E, padx=5, pady=5)
def mainloop(self):
self.root.mainloop()
# class Navbar(tk.Frame): ...
# class Toolbar(tk.Frame): ...
# class Statusbar(tk.Frame): ...
# class Main(tk.Frame): ...
# class MainApp(tk.Frame):
# def __init__(self, parent, *args, **kwargs):
# tk.Frame.__init__(self, parent, *args, **kwargs)
# self.statusbar = Statusbar(self, ...)
# self.toolbar = Toolbar(self, ...)
# self.navbar = Navbar(self, ...)
# self.main = Main(self, ...)
# self.statusbar.pack(side="bottom", fill="x")
# self.toolbar.pack(side="top", fill="x")
# self.navbar.pack(side="left", fill="y")
# self.main.pack(side="right", fill="both", expand=True)
if __name__ == "__main__":
""" using the Model-View-Controller (MVC) Pattern
- The MODEL is responsible for managing the data of the application. It
receives user input from the CONTROLLER
- The VIEW renders presentation of the model in a particular format.
- The CONTROLLER responds to the user input and performs interactions on the
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
"""
class CadItem():
"""
This item contains all data for writing all necessary data to the solid edg cad file
"""
def __init__(self, uid):
""" is initalized with its unique id"""
self.id = uid
self.red_ids = {}
self.parent_children_mapping = {}
def set_parent_children(self, parent_children_mapping):
""" contains all id relations below, data of subassemblies etc. in
a dict of the kind parent-children
"""
self.parent_children_mapping = parent_children_mapping
def get_parent_children(self):
return self.parent_children_mapping
def set_red_ids(self, red_ids):
""" these ids are not yet known by the PDM"""
self.red_ids = red_ids
def get_red_ids(self):
return self.red_ids
def load():
pass
def save():
pass
class CadItemRepository:
"""
container class for all CadItems in the local folder
also just managed by the controller
"""
def __init__(self):
self.caditems = []
def add(self, caditem):
self.caditems.append(caditem)
def get(self, id):
for caditem in self.caditems:
if caditem.id == id:
return caditem
return None
def get_all(self):
return self.caditems
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:
if p.id == id:
self.caditems.remove(p)
return
raise Exception("CadItem not found")
class Controller():
"""
Class for manipulating the model and returning data for the view
"""
def __init__(self, model):
self.model = model
def set_red_ids(self, red_ids):
self.red_ids = red_ids
def get_red_ids(self):
return self.red_ids
def set_parent_children(self, parent_children_mapping):
self.parent_children_mapping = parent_children_mapping
def get_parent_children(self):
return self.parent_children_mapping
#logging.basicConfig(level=logging.DEBUG)
model = CadItem("400102196")
controller = Controller(model)
controller.set_parent_children(parent_children_mapping)
controller.set_red_ids(red_ids)
root = tk.Tk()
app = App(root, controller)
app.mainloop()