-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuiMethods.py
148 lines (114 loc) · 4.68 KB
/
GuiMethods.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
from Files import get_data, save_data
import wx, os
from FieldSolver import force_field
from PlotOnFig import plot_on_fig
from Parser import parse_dsl
from threading import Thread
from GuiConstants import GUI_CONSTANTS
def display_help_msg_callback(self):
''' Displays the help message in the output box'''
self.set_console_msg(self.helpstr)
def set_console_msg(self,msg):
'''Sets the output box text'''
self.output_text_ctrl.ChangeValue(msg)
def update_fig(self):
'''Sends the figure to some math and plotting, comes back with a new image'''
self.start_fig_update()
def update_fig_from_button(self, e):
'''Spawns threads for math and progress bar so that the gui doesn't die'''
self.figure.clf()
t = Thread(target = self.start_fig_update)
t.run()
def start_fig_update(self):
'''Mutates the matplotlib figure to show data. Takes data from
various input fields.'''
self.status_text.SetLabel('Computing Field...')
domain_x = (float(self.x1_input_field.GetValue()),float(self.x2_input_field.GetValue()))
range_y = (float(self.y1_input_field.GetValue()),float(self.y2_input_field.GetValue()))
plot_type= self.plot_type_selector.GetValue()
vector_type = self.vector_drawing_selector.GetValue()
field_type = self.field_type_selector.GetValue()
xs, ys, cs = parse_dsl(self.input_text_ctrl.GetValue(),self.display_help_msg_callback)
if xs == []:
self.set_console_msg('Input cannot be empty!\
Update with "help" for commands')
elif xs == None:
pass
elif GUI_CONSTANTS['Cache']==[domain_x,range_y,plot_type, field_type, self.input_text_ctrl.GetValue(),vector_type]:
return
elif GUI_CONSTANTS['Cache'][0:5]==[domain_x,range_y,plot_type, field_type, self.input_text_ctrl.GetValue()] and GUI_CONSTANTS['Cache'][5]!= vector_type:
tmp = GUI_CONSTANTS['Field_Cache'][0:9]
fig, X, Y, Xq, Yq, F, Fq, res, direc=\
plot_on_fig(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5],tmp[6],tmp[7],direc=vector_type)
GUI_CONSTANTS['Cache'] = [domain_x,range_y,plot_type, field_type, self.input_text_ctrl.GetValue(), vector_type]
GUI_CONSTANTS['Field_Cache'] = [fig, X, Y, Xq, Yq, F, Fq, res, direc]
self.canvas.draw()
return
else:
fig, X, Y, Xq, Yq, F, Fq, res, direc =\
force_field(self.figure, plot_type, vector_type,
domain_x, range_y,xs,ys,cs,field_type, res = 100)
self.canvas.draw()
GUI_CONSTANTS['Cache'] = [domain_x,range_y,plot_type, field_type, self.input_text_ctrl.GetValue(), vector_type]
GUI_CONSTANTS['Field_Cache'] = [fig, X, Y, Xq, Yq, F, Fq, res, direc]
self.status_text.SetLabel('Render Finished')
def threaded_progress_bar_update(self):
''' Currently not in use'''
while 1:
percent = self.progress_q.get()
self.progress_gauge.SetValue(percent)
if percent >= 100:
break
def load_charge_field(self,e):
''' Load button functionality.'''
wildcard = "Files (*.figsave)|*.figsave"
open_dlg = wx.FileDialog(
self, message = 'Choose file',
defaultDir=os.getcwd()+"/Saves",
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.CHANGE_DIR
)
if open_dlg.ShowModal() == wx.ID_OK:
path = open_dlg.GetPaths()[0]
xs,ys,cs = get_data(path)
## Build a command for the Gui and pass it along
f=zip(xs,ys,cs)
m=''
for i in range(len(f)):
s='point('
for j in range(3):
s= s+f[i][j]+','
m= m+s[0:-1]+")\n"
self.input_text_ctrl.ChangeValue(m)
self.start_fig_update()
open_dlg.Destroy()
def save_charge_field(self,e):
''' Saves what's currently in the input box'''
wildcard = "Files (*.figsave)|*.figsave"
save_dlg = wx.FileDialog(
self, message = 'Choose file',
defaultDir=os.getcwd()+"/Saves",
defaultFile="",
wildcard=wildcard,
style=wx.SAVE | wx.CHANGE_DIR
)
if save_dlg.ShowModal() == wx.ID_OK:
path = save_dlg.GetPaths()[0]
save_dlg.Destroy()
xs, ys, cs = parse_dsl(self.input_text_ctrl.GetValue(),
self.display_help_msg_callback)
save_data(path,xs,ys,cs)
def save_figure(self,e):
wildcard = "PNG Image (*.png)|*.png"
save_dlg = wx.FileDialog(
self, message = 'Choose file',
defaultDir=os.getcwd()+"/Saves",
defaultFile="",
wildcard=wildcard,
style=wx.SAVE | wx.CHANGE_DIR
)
if save_dlg.ShowModal() == wx.ID_OK:
filename = save_dlg.GetPaths()[0]
save_dlg.Destroy()
self.figure.savefig(filename)