-
Notifications
You must be signed in to change notification settings - Fork 7
/
a_Main_Window.py
225 lines (191 loc) · 8.26 KB
/
a_Main_Window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright: (c) Oskar Petersons 2013
"""Launches the GMCR-py Decision Support System."""
from version import __version__
from tkinter import Tk, VERTICAL, HORIZONTAL, N, S, E, W
from tkinter import ttk
from tkinter import filedialog
from data_01_conflictModel import ConflictModel
from frame_01_decisionMakers import DMInpFrame
from frame_02_infeasibles import InfeasInpFrame
from frame_02a_misperceptions import MisperceptionFrame
from frame_03_irreversibles import IrrevInpFrame
from frame_04_preferencePrioritization import PreferencesFrame
from frame_05_preferenceRanking import PreferenceRankingFrame
from frame_06_equilibria import ResultFrame
from frame_07_inverseGMCR import InverseFrame
from frame_08_stabilityAnalysis import StabilityFrame
from multiprocessing import freeze_support
import os
import sys
NSEW = (N, S, E, W)
class MainAppWindow:
"""Top Level Window for the GMCR-py application."""
def __init__(self, file=None):
"""Initialize the window."""
self.file = file # file reference used for saving the conflict.
self.root = Tk()
self.root.iconbitmap('gmcr.ico')
self.root.wm_title('GMCR+ v{} | New Model'.format(__version__))
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
self.frameList = []
self.frameBtnCmds = []
self.frameBtnList = []
self.topFrame = ttk.Frame(self.root)
self.fileFrame = ttk.Frame(self.topFrame, border=3, relief='raised')
self.pageSelectFrame = ttk.Frame(self.topFrame, border=3,
relief='raised')
self.contentFrame = ttk.Frame(self.topFrame)
self.topVSep = ttk.Separator(self.topFrame, orient=HORIZONTAL)
self.bottomHSep = ttk.Separator(self.contentFrame, orient=VERTICAL)
self.topFrame.grid(column=0, row=0, sticky=NSEW)
self.topFrame.columnconfigure(2, weight=1)
self.topFrame.rowconfigure(3, weight=1)
self.fileFrame.grid(column=0, row=1, sticky=NSEW)
self.pageSelectFrame.grid(column=1, row=1, columnspan=4,
sticky=(N, S, W))
self.pageSelectFrame.rowconfigure(0, weight=1)
self.topVSep.grid(column=0, row=2, columnspan=10, sticky=NSEW)
self.bottomHSep.grid(column=1, row=0, rowspan=10, sticky=NSEW)
self.contentFrame.grid(column=0, row=3, columnspan=5, sticky=NSEW)
self.contentFrame.columnconfigure(0, weight=1)
self.contentFrame.rowconfigure(1, weight=1)
self.saveButton = ttk.Button(self.fileFrame, text='Save Conflict',
command=self.saveConflict, width=20)
self.saveAsButton = ttk.Button(self.fileFrame, text='Save Conflict As',
command=self.saveAs, width=20)
self.loadButton = ttk.Button(self.fileFrame, text='Load Conflict',
command=self.loadConflict, width=20)
self.newButton = ttk.Button(self.fileFrame, text='New Conflict',
command=self.newConflict, width=20)
self.saveButton.grid(column=0, row=0, sticky=(E, W))
self.saveAsButton.grid(column=0, row=1, sticky=(E, W))
self.loadButton.grid(column=0, row=2, sticky=(E, W))
self.newButton.grid(column=0, row=3, sticky=(E, W))
self.activeConflict = ConflictModel()
self.contentFrame.currFrame = None
self.addMod(DMInpFrame)
self.addMod(InfeasInpFrame)
self.addMod(MisperceptionFrame)
self.addMod(IrrevInpFrame)
self.addMod(PreferencesFrame)
self.addMod(PreferenceRankingFrame)
self.addMod(ResultFrame)
self.addMod(InverseFrame)
self.addMod(StabilityFrame)
self.refreshActiveFrames()
self.root.bind_all("<<checkData>>", self.checkFramesHaveData)
if self.file is not None:
self.loadConflict(self.file)
self.root.mainloop()
def addMod(self, newMod):
"""Add a new input frame and Module to the Conflict."""
newFrame = newMod(self.contentFrame, self.activeConflict)
self.frameList.append(newFrame)
newButton = newFrame.makeButton(self.pageSelectFrame, self)
self.frameBtnList.append(newButton)
newButton.grid(column=len(self.frameBtnList), row=0, sticky=NSEW)
def checkFramesHaveData(self, event=None):
"""Check if required data exists for each frame then de/activate."""
for idx, frame in enumerate(self.frameList):
if frame.hasRequiredData():
self.frameBtnList[idx].config(state="normal")
else:
frame.clearFrame()
self.frameBtnList[idx].config(state="disabled")
if frame != self.contentFrame.currFrame:
frame.built = False
def refreshActiveFrames(self, event=None):
"""Unload all frames and attempt to reload the current one."""
self.unloadAllFrames()
self.checkFramesHaveData()
try:
self.contentFrame.currFrame.built = False
except AttributeError:
pass
try:
if self.contentFrame.currFrame.hasRequiredData():
self.contentFrame.currFrame.enter()
else:
self.frameBtnCmds[0](self)
except AttributeError:
self.frameBtnCmds[0](self)
def unloadAllFrames(self, event=None):
"""Remove all frames (conflict screens) from the window."""
for idx, frame in enumerate(self.frameList):
frame.clearFrame()
self.frameBtnList[idx].config(state="disabled")
def frameLeave(self):
"""Ungrid the current frame and performs other exit tasks."""
try:
self.contentFrame.currFrame.leave()
except AttributeError:
pass
def saveConflict(self):
"""Save all information to the currently active file."""
if not self.file:
self.saveAs()
return
try:
self.contentFrame.currFrame.leave()
except AttributeError:
pass
self.contentFrame.currFrame.enter()
self.activeConflict.save_to_file(self.file)
print('Saved')
def saveAs(self):
"""Open a file dialog to save the conflict to file."""
print('running saveAs')
fileName = filedialog.asksaveasfilename(
defaultextension='.gmcr',
filetypes=(("GMCR+ Save Files", "*.gmcr"), ("All files", "*.*")),
parent=self.root
)
if fileName:
self.file = fileName
self.root.wm_title('GMCR+ v{} | {}'.format(__version__, self.file))
self.saveConflict()
def loadConflict(self, fileName=None):
"""Open a file dialog that prompts for a save file to open."""
if not fileName:
fileName = filedialog.askopenfilename(
filetypes=(("GMCR+ Save Files", "*.gmcr"),
("All files", "*.*")),
initialdir='{}/Examples'.format(os.getcwd()),
parent=self.root
)
if fileName:
self.file = fileName
self.frameBtnCmds[0](self)
self.frameLeave()
self.unloadAllFrames()
print('loading: {}'.format(fileName))
self.root.wm_title('GMCR+ v{} | {}'.format(__version__, self.file))
self.activeConflict.load_from_file(fileName)
self.refreshActiveFrames()
def newConflict(self):
"""Clear all data, creating a new conflict."""
print("Initializing new conflict...")
self.unloadAllFrames()
self.activeConflict.__init__()
self.file = None
self.refreshActiveFrames()
self.root.wm_title('GMCR+ v{} | New Model'.format(__version__))
if __name__ == '__main__':
freeze_support()
# Set working directory to program folder if started from executable.
# This allows easy access to example files.
if getattr(sys, 'frozen', False):
path = getattr(sys, '_MEIPASS', os.getcwd())
os.chdir(path)
else:
try:
os.chdir(sys.argv[0].rpartition('\\')[0])
except OSError:
pass
launchFile = None
try:
launchFile = sys.argv[1]
except IndexError:
pass
a = MainAppWindow(launchFile)