from Parameters import parameters from Model import ModelObjects from GrapheDeComposition import MGC from re import split, findall from tkinter import TOP, BOTTOM, LEFT, RIGHT, X, VERTICAL, \ LabelFrame, Frame, Label, Text, Scrollbar, Button from tkinter.ttk import Combobox class NewObjectFrame(LabelFrame): """ Frame objects """ _model=None _NOList=None _NOFrames={} _frame=None def __init__(self, parent, model, *args, **kwargs): assert model and parent LabelFrame .__init__(self, parent, bd=parameters.FRAME_BD, text="Nouvel Objet", *args, **kwargs) self._model=model self._initFrame() def _initFrame(self): values=[""] for obj in ModelObjects: values.append(obj.value) self._NOList=Combobox(self, values=values, state='readonly') self._NOList.pack(side = TOP, fill=X) self._NOList.bind("<>", self.selectNOType) self._initNOFrames() self._frame=self._NOFrames[0] self._frame.pack(side = TOP, fill=X) def _initNOFrames(self): self._NOFrames=[Frame(self)] for obj in ModelObjects: self._NOFrames.append(BuildObjectFrame(self, self._model, obj)) def selectNOType(self, event): self._frame.forget() self._frame=self._NOFrames[self._NOList.current()] self._frame.pack(side = TOP, fill=X) class BuildObjectFrame(Frame): """ Frame New "GC - Graphe de composition" """ _model=None _obj=None _validateButton=None _textObjects=None _textMorphismes=None def __init__(self, parent, model, obj:ModelObjects, *args, **kwargs): assert model and parent Frame .__init__(self, parent, *args, **kwargs) self._model=model self._obj=obj self._initFrame() def _initFrame(self): if self._obj==ModelObjects.GC: self._initGrapheCompositionFrame() #elif self._obj==ModelObjects.CI: def _initGrapheCompositionFrame(self): self.columnconfigure(0, weight=1, minsize =30) self.rowconfigure(0, weight=1, minsize=20) self.rowconfigure(1, weight=1, minsize=20) self.rowconfigure(2, weight=1, minsize=20) # Objects self._textObjects=ObjectLabelText(self) self._textObjects.grid(row=1, column=0) # Morphismes self._textMorphismes=MorphismeLabelText(self) self._textMorphismes.grid(row=2, column=0) # Button def generate(): self._model.createGrapheDeComposition(objets=self._textObjects.getObjectsList(), morphismes=self._textMorphismes.getMorphismesList(), nom = None) self._validateButton=Button(self, text="Générer", command=generate) self._validateButton.grid(row=0, column=0, sticky="e") class LabelText(LabelFrame): _TEXT_MAX_SIZE=100 _text=None _scrollbarY=None _description=None def __init__(self, parent, name:str, description:str=None, *args, **kwargs): LabelFrame .__init__(self, parent, bd=parameters.FRAME_BD, text=name, *args, **kwargs) self._description=description self._initFrame() def _initFrame(self): self.columnconfigure(0, weight=1, minsize=20) self.columnconfigure(1, minsize=16) self.rowconfigure(1, weight=1, minsize=20) if self._description: Label(self, text=self._description, anchor="w").grid(row=0, column=0, sticky="new") self._text=Text(self) self._text.grid(row=1, column=0, sticky="nw") self._scrollbarY=Scrollbar(self, orient =VERTICAL) self._scrollbarY.grid(row=1, column=1, sticky="nsw") self._text.configure(yscrollcommand = self._scrollbarY.set) self._scrollbarY.config(command=self._text.yview) def getText(self): return self._text.get("1.0",'end-1c') class ObjectLabelText(LabelText): def __init__(self, parent, *args, **kwargs): LabelText .__init__(self, parent, name="Objets", description="ex: 1,2,3...", *args, **kwargs) def getObjectsList(self): return [x for x in split('[,\n]', self.getText()) if x] class MorphismeLabelText(LabelText): def __init__(self, parent, *args, **kwargs): LabelText .__init__(self, parent, name="Morphismes", description="ex: (1,2,f)(2,3,g)...", *args, **kwargs) def getMorphismesList(self): # (...) tuples=[x[1:-1] for x in findall("\([^\(\)]+\)", self.getText()) if x] # ,\n tuplesSplited=[[x for x in split('[,\n]', t) if x] for t in tuples] # 3 chaînes morphismesDescription=[x for x in tuplesSplited if len(x)==3] # Création des morphismes return [MGC(x[0],x[1],x[2]) for x in morphismesDescription]