-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggeocomp.py
executable file
·258 lines (204 loc) · 7.38 KB
/
ggeocomp.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TODO:
# -> fazer sair c/ C-q !!!
import os
import string
from gtk import *
from gnome.ui import *
import geocomp
from geocomp import config
from geocomp.gui import gnome
import sys
import gtk
from gnomecanvas import Canvas
class App:
def __init__ (self):
self.panel = {}
self.step_completed = 0
self.labels = []
self.ppu = 1.0
self.idle_id = None
self.window = gtk.Window ()
self.window.connect ('destroy', mainquit)
self.window.set_title ('Geocomp')
self.window.set_border_width (10)
self.out_vbox = gtk.VBox ()
self.window.add (self.out_vbox)
self.extra_label = gtk.Label ('----------')
self.out_vbox.pack_end (self.extra_label, expand = FALSE)
self.main_hbox = gtk.HBox ()
self.out_vbox.pack_start (self.main_hbox)
self.canvas_vbox = gtk.VBox ()
self.main_hbox.pack_end (self.canvas_vbox)
ha = gtk.Adjustment (0, 0, 1000, 40, 100, 100)
va = gtk.Adjustment (0, 0, 1000, 40, 100, 100)
self.scroll = gtk.ScrolledWindow (ha, va)
self.canvas_vbox.pack_start (self.scroll)
#self.canvas = gnome.Canvas (aa=TRUE)
self.canvas = Canvas (aa=FALSE)
self.canvas.set_usize (config.WIDTH, config.HEIGHT)
self.canvas.set_scroll_region(0,0, config.WIDTH, config.HEIGHT)
self.scroll.add (self.canvas)
self.controls_box = gtk.HBox ()
self.canvas_vbox.pack_end (self.controls_box, expand=FALSE)
self.dyn_controls_box = gtk.HBox ()
self.controls_box.pack_start (self.dyn_controls_box)
self.delay = gtk.SpinButton (gtk.Adjustment (config.DELAY, 0, config.MAX_DELAY, 10, config.MAX_DELAY/10, config.MAX_DELAY/10), digits=0, climb_rate=10)
self.delay.set_numeric (TRUE)
self.dyn_controls_box.pack_start (self.delay)#, expand=TRUE, fill=FALSE)
self.step = gtk.CheckButton ('passo a passo')
self.step.set_state (TRUE)
self.step.connect_after ('clicked', self.step_clicked)
self.dyn_controls_box.pack_start (self.step, fill=FALSE)
self.zoom_in = gtk.Button ('+')
self.zoom_out = gtk.Button ('-')
#self.zoom_box = gtk.HBox ()
#self.canvas_vbox.pack_start (self.zoom_box)
self.dyn_controls_box.pack_start (self.zoom_in)
self.dyn_controls_box.pack_start (self.zoom_out)
self.zoom_in.connect ('clicked', self.zoom, 1.5)
self.zoom_out.connect ('clicked', self.zoom, 2.0/3.0)
self.hide = gtk.CheckButton ('esconder')
self.hide.set_state (TRUE)
self.controls_box.pack_start (self.hide, fill=FALSE)
geocomp.init_display (gnome, self)
self.left_box = gtk.VBox ()
self.left_box.set_border_width (5)
self.main_hbox.pack_start (self.left_box, expand=FALSE)
self.file_box = gtk.VBox ()
self.left_box.pack_start (self.file_box)
self.files_combo = gtk.Combo ()
self.handler_id = self.files_combo.entry.connect ("changed", self.open_file)
#self.handler_id = self.files_combo.list.connect ("selection_changed", self.open_file)
self.files_combo.set_value_in_list (val = TRUE, ok_if_empty = FALSE)
self.files_combo.disable_activate ()
self.files_combo.entry.set_editable (FALSE)
self.file_box.pack_start (self.files_combo, expand = FALSE)
self.create_buttons (None, (geocomp, None))
self.window.show_all ()
style = self.canvas.get_style ()
style.bg[STATE_NORMAL] = style.black
self.update_files (config.DATADIR, 1)
self.files_combo.entry.emit ("changed")
self.window.add_events (gtk.gdk.KEY_RELEASE)
# while events_pending (): mainiteration ()
def zoom (self, clicked, inout):
self.ppu = self.ppu * inout
if self.ppu < 1.0: self.ppu = 1.0
self.canvas.set_pixels_per_unit (self.ppu)
self.canvas.grab_focus ()
def update_files (self, directory, init=0):
"Atualiza a lista de arquivos disponiveis"
if self.idle_id is not None:
idle_remove (self.idle_id)
self.idle_id = None
files = os.listdir (directory)
files = filter (lambda x: x[0] != '.', files)
files.sort ()
#files.insert (0, '..')
files.append ('..')
for i in range (len(files)):
if os.path.isdir (os.path.join (directory, files[i])):
files[i] = files[i] + '/'
self.datadir = directory
self.files_combo.entry.handler_block (self.handler_id)
self.files_combo.set_popdown_strings (files)
self.files_combo.entry.handler_unblock (self.handler_id)
if not init:
self.files_combo.entry.handler_block (self.handler_id)
self.files_combo.entry.set_text (self.cur_file)
self.files_combo.entry.handler_unblock (self.handler_id)
def open_file (self, entry):
selection = os.path.join (self.datadir, entry.get_text ())
if os.path.isdir (selection):
if self.idle_id == None:
self.idle_id = idle_add (self.update_files,
selection)
else:
self.cur_file = os.path.basename (selection)
self.points = geocomp.open_file (selection)
geocomp.config_canvas (self.points)
for l in self.labels:
l.set_text ('------')
self.extra_label.set_text ('----------')
def create_buttons (self, clicked=None, list=None):
problem = list[0]
parent = list[1]
if self.panel.has_key (problem):
self.buttons.hide ()
self.buttons = self.panel[problem]
self.buttons.show_all ()
self.buttons.focus.grab_focus ()
return
if hasattr (self, 'buttons'):
self.buttons.hide ()
children = getattr (problem, 'children')
buttons = gtk.Table (len (children), 3, FALSE)
self.left_box.pack_end (buttons, expand = FALSE)
row = 0
first = 1
for a in children:
b = gtk.Button (a[-1])
if a[1] == None: # sub-modulo
buttons.attach (b, 0, 3, row, row+1, yoptions = 0, ypadding = 1)
sub_prob = getattr (problem, a[0])
b.connect ('clicked', self.create_buttons, (sub_prob, problem))
else:
buttons.attach (b, 0, 2, row, row+1, yoptions = 0, ypadding = 1)
alg = getattr (problem, a[0])
func = getattr (alg, a[1])
l = gtk.Label ('------')
buttons.attach (l, 2, 3, row, row+1, yoptions = 0, ypadding = 1)
b.connect ('clicked', self.run_algorithm, (func, l))
self.labels.append (l)
row = row + 1
if first:
b.grab_focus ()
first = 0
buttons.focus = b
if parent != None:
b = gtk.Button ('Voltar')
b.connect ('clicked', self.create_buttons, (parent, parent))
buttons.attach (b, 0, 3, row, row+1, yoptions = 0, ypadding = 3)
else:
b = gtk.Button ('Sair')
b.connect ('clicked', mainquit)
buttons.attach (b, 0, 3, row, row+1, yoptions = 0, ypadding = 3)
self.panel[problem] = buttons
self.buttons = buttons
self.buttons.show_all ()
return
def step_clicked (self, widget):
self.canvas.grab_focus ()
self.window.grab_focus ()
self.step_completed = 1
def key_release (self, widget, event):
#print event
#if event.keyval == gtk.gdk.space:
self.step_completed = 1
def run_algorithm (self, clicked, arg):
alg = arg[0]
label = arg[1]
# desativando os controles que nao fazem sentido durante
# o algoritmo
self.left_box.set_sensitive (FALSE)
if self.hide.get_active ():
self.dyn_controls_box.set_sensitive (FALSE)
self.hide.set_sensitive (FALSE)
self.extra_label.set_text ('----------')
self.canvas.grab_focus ()
id = self.window.connect ('key_release_event', self.key_release)
while events_pending (): mainiteration ()
cont, extra_info = geocomp.run_algorithm (alg, self.points)
self.window.disconnect (id)
if self.hide.get_active ():
self.dyn_controls_box.set_sensitive (TRUE)
self.hide.set_sensitive (TRUE)
self.left_box.set_sensitive (TRUE)
label.set_text ("%6d"%cont)
if extra_info is not None:
self.extra_label.set_text (extra_info)
if __name__ == '__main__':
app = App ()
mainloop ()