NewObjectFrame.py 5.09 KB
Newer Older
Francois Mares's avatar
Francois Mares committed
1
2
from Parameters import parameters
from Model import ModelObjects
Francois Mares's avatar
add GUI  
Francois Mares committed
3

Francois Mares's avatar
Francois Mares committed
4
from GrapheDeComposition import MGC
Francois Mares's avatar
add GUI  
Francois Mares committed
5

Francois Mares's avatar
Francois Mares committed
6
7
8
from re import split, findall
from tkinter import TOP, BOTTOM, LEFT, RIGHT, X, VERTICAL, \
                    LabelFrame, Frame, Label, Text, Scrollbar, Button
Francois Mares's avatar
add GUI  
Francois Mares committed
9

Francois Mares's avatar
Francois Mares committed
10
from tkinter.ttk import Combobox
Francois Mares's avatar
add GUI  
Francois Mares committed
11
12
13
14
15
16


class NewObjectFrame(LabelFrame):
    """
    Frame objects
    """
Francois Mares's avatar
Francois Mares committed
17
18
    _model=None

Francois Mares's avatar
add GUI  
Francois Mares committed
19
    _NOList=None
Francois Mares's avatar
Francois Mares committed
20
    _NOFrames={}
Francois Mares's avatar
add GUI  
Francois Mares committed
21
22
23
    _frame=None


Francois Mares's avatar
Francois Mares committed
24
25
    def __init__(self, parent, model, *args, **kwargs):
        assert model and parent
Francois Mares's avatar
add GUI  
Francois Mares committed
26
        LabelFrame .__init__(self, parent,
Francois Mares's avatar
Francois Mares committed
27
28
                                bd=parameters.FRAME_BD,
                                text="Nouvel Objet",
Francois Mares's avatar
add GUI  
Francois Mares committed
29
                                *args, **kwargs)
Francois Mares's avatar
Francois Mares committed
30
        self._model=model
Francois Mares's avatar
add GUI  
Francois Mares committed
31
32
33
        self._initFrame()

    def _initFrame(self):
Francois Mares's avatar
Francois Mares committed
34
35
36
        values=[""]
        for obj in ModelObjects:
            values.append(obj.value)
Francois Mares's avatar
add GUI  
Francois Mares committed
37
        self._NOList=Combobox(self,
Francois Mares's avatar
Francois Mares committed
38
                            values=values,
Francois Mares's avatar
add GUI  
Francois Mares committed
39
40
41
42
43
44
45
46
47
48
                            state='readonly')
        self._NOList.pack(side = TOP, fill=X)
        self._NOList.bind("<<ComboboxSelected>>", self.selectNOType)

        self._initNOFrames()
        self._frame=self._NOFrames[0]
        self._frame.pack(side = TOP, fill=X)

    def _initNOFrames(self):
        self._NOFrames=[Frame(self)]
Francois Mares's avatar
Francois Mares committed
49
50
        for obj in ModelObjects:
            self._NOFrames.append(BuildObjectFrame(self, self._model, obj))
Francois Mares's avatar
add GUI  
Francois Mares committed
51
52

    def selectNOType(self, event):
Francois Mares's avatar
Francois Mares committed
53
54
        self._frame.forget()
        self._frame=self._NOFrames[self._NOList.current()]
Francois Mares's avatar
add GUI  
Francois Mares committed
55
56
57
        self._frame.pack(side = TOP, fill=X)


Francois Mares's avatar
Francois Mares committed
58
class BuildObjectFrame(Frame):
Francois Mares's avatar
add GUI  
Francois Mares committed
59
60
61
    """
        Frame New "GC - Graphe de composition"
    """
Francois Mares's avatar
Francois Mares committed
62
63
64
65
66
    _model=None
    _obj=None

    _validateButton=None
    _textObjects=None
Francois Mares's avatar
add GUI  
Francois Mares committed
67
68
    _textMorphismes=None

Francois Mares's avatar
Francois Mares committed
69
70
    def __init__(self, parent, model, obj:ModelObjects, *args, **kwargs):
        assert model and parent
Francois Mares's avatar
add GUI  
Francois Mares committed
71
72
        Frame .__init__(self, parent, *args, **kwargs)

Francois Mares's avatar
Francois Mares committed
73
74
        self._model=model
        self._obj=obj
Francois Mares's avatar
add GUI  
Francois Mares committed
75
76
77
78

        self._initFrame()

    def _initFrame(self):
Francois Mares's avatar
Francois Mares committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        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")

Francois Mares's avatar
add GUI  
Francois Mares committed
105

Francois Mares's avatar
Francois Mares committed
106
class LabelText(LabelFrame):
Francois Mares's avatar
add GUI  
Francois Mares committed
107

Francois Mares's avatar
Francois Mares committed
108
109
110
    _TEXT_MAX_SIZE=100
    _text=None
    _scrollbarY=None
Francois Mares's avatar
Francois Mares committed
111
    _description=None
Francois Mares's avatar
add GUI  
Francois Mares committed
112

Francois Mares's avatar
Francois Mares committed
113
    def __init__(self, parent, name:str, description:str=None, *args, **kwargs):
Francois Mares's avatar
Francois Mares committed
114
115
116
117
        LabelFrame .__init__(self, parent,
                                bd=parameters.FRAME_BD,
                                text=name,
                                *args, **kwargs)
Francois Mares's avatar
Francois Mares committed
118
        self._description=description
Francois Mares's avatar
Francois Mares committed
119
        self._initFrame()
Francois Mares's avatar
add GUI  
Francois Mares committed
120

Francois Mares's avatar
Francois Mares committed
121
122
123
    def _initFrame(self):
        self.columnconfigure(0, weight=1, minsize=20)
        self.columnconfigure(1, minsize=16)
Francois Mares's avatar
Francois Mares committed
124
125
126
127
        self.rowconfigure(1, weight=1, minsize=20)

        if self._description:
            Label(self, text=self._description, anchor="w").grid(row=0, column=0, sticky="new")
Francois Mares's avatar
add GUI  
Francois Mares committed
128

Francois Mares's avatar
Francois Mares committed
129
        self._text=Text(self)
Francois Mares's avatar
Francois Mares committed
130
        self._text.grid(row=1, column=0, sticky="nw")
Francois Mares's avatar
add GUI  
Francois Mares committed
131

Francois Mares's avatar
Francois Mares committed
132
        self._scrollbarY=Scrollbar(self, orient =VERTICAL)
Francois Mares's avatar
Francois Mares committed
133
        self._scrollbarY.grid(row=1, column=1, sticky="nsw")
Francois Mares's avatar
add GUI  
Francois Mares committed
134

Francois Mares's avatar
Francois Mares committed
135
136
        self._text.configure(yscrollcommand = self._scrollbarY.set)
        self._scrollbarY.config(command=self._text.yview)
Francois Mares's avatar
add GUI  
Francois Mares committed
137

Francois Mares's avatar
Francois Mares committed
138
139
    def getText(self):
        return self._text.get("1.0",'end-1c')
Francois Mares's avatar
add GUI  
Francois Mares committed
140

Francois Mares's avatar
Francois Mares committed
141
class ObjectLabelText(LabelText):
Francois Mares's avatar
add GUI  
Francois Mares committed
142

Francois Mares's avatar
Francois Mares committed
143
    def __init__(self, parent, *args, **kwargs):
Francois Mares's avatar
Francois Mares committed
144
        LabelText .__init__(self, parent, name="Objets", description="ex: 1,2,3...", *args, **kwargs)
Francois Mares's avatar
add GUI  
Francois Mares committed
145

Francois Mares's avatar
Francois Mares committed
146
147
    def getObjectsList(self):
        return [x for x in split('[,\n]', self.getText()) if x]
Francois Mares's avatar
add GUI  
Francois Mares committed
148

Francois Mares's avatar
Francois Mares committed
149
class MorphismeLabelText(LabelText):
Francois Mares's avatar
add GUI  
Francois Mares committed
150

Francois Mares's avatar
Francois Mares committed
151
    def __init__(self, parent, *args, **kwargs):
Francois Mares's avatar
Francois Mares committed
152
        LabelText .__init__(self, parent, name="Morphismes", description="ex: (1,2,f)(2,3,g)...", *args, **kwargs)
Francois Mares's avatar
Francois Mares committed
153
154
155
156
157
158
159
160
161
162

    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]
Francois Mares's avatar
add GUI  
Francois Mares committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198