-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocaliza.rb
executable file
·309 lines (251 loc) · 6.67 KB
/
localiza.rb
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/ruby -w
# -*- coding: utf-8 -*-
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
=begin
* Name: rplac main file
* Description: programa principal
* Author: Joel Uchoa, Murilo de Lima
* Date: 2009-11-06
* License:
rplac 0.1, Copyright (C) 2009 Joel Uchoa, Murilo de Lima
rplac comes with ABSOLUTELY NO WARRANTY; for detais see `gpl-2.0.txt'.
This is free software, and you are welcome to redistribute it
under certain conditions; see `gpl-2.0.txt' for details.
=end
require 'libglade2'
require 'gnomecanvas2'
require 'thread'
require 'about'
require 'file_diag'
require 'lib/structures'
require 'lib/painter'
require 'lib/reader'
require 'lib/simple_algorithm'
require 'lib/rand_algorithm'
class LocalizaGlade
include GetText
attr :glade
attr_accessor :canvas
def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
bindtextdomain(domain, localedir, nil, 'UTF-8')
@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
w = @glade['main_window']
w.signal_connect('destroy') { |w| gtk_main_quit(w) }
w.show_all
end
def gtk_main_quit(widget)
Gtk.main_quit
end
def change_drawing_widget
parent = @glade['draw_box']
width, height = 800, 600
@canvas = Gnome::Canvas.new(true)
@canvas.set_size_request(width,height)
@canvas.set_scroll_region(0,0,width,height)
Gnome::CanvasRect.new(@canvas.root,
:x1 => 0, :y1 => 0,
:x2 => width, :y2 => height,
:fill_color => BLACK,
:width_units => 1.0)
parent.add(@canvas)
bar = @glade['statusbar_alg']
parent.add(bar)
parent.show_all
end
end
####### methods for GTK stuff #########
def open_diag_about
AboutDialog.new.show
end
def open_diag_file
OpenFileDialog.new.show do |fname|
begin
$map, $queries = Reader.read(fname)
$painter = Painter.new($map.points + $queries, $prog.canvas)
$map.paint($painter)
$map.build_structure
$file_ok.val = true
write_status_bar(:open_file_end)
rescue Exception => error
puts error
puts error.backtrace
dialog = Gtk::MessageDialog.new($prog.glade['main_window'],
Gtk::Dialog::DESTROY_WITH_PARENT,
Gtk::MessageDialog::ERROR,
Gtk::MessageDialog::BUTTONS_CLOSE,
"Erro lendo arquivo '%s'" % fname)
dialog.run
dialog.destroy
write_status_bar(:open_file_err)
end
end
end
def change_algorithm
$row_alg.val = $prog.glade['combobox_alg'].active
end
def toggle_step
$pap.val = !$pap.val
end
def change_speed
update_delay
end
def play_pause
bt_active = $prog.glade['bt_play_pause'].active?
if $started.val
if bt_active # continuing
update_delay
$thread.wakeup
else # pausing
$delay = nil
end
else
if bt_active # this is necessary because finish fires play_pause
$started.val = true
update_delay
start_it_all
end
end
end
def stop
finish
Thread.kill($thread)
end
def step
$mutex_pap.synchronize do
$cv_pap.signal
end
end
def next
$last_part.val = true
$mutex_jump.synchronize do
$jump = true
end
$prog.glade['bt_play_pause'].active = false # pauses if necessary; TODO só funciona se já tiver iniciado
#unlocks the last step, if necessary
$mutex_pap.synchronize do
$cv_pap.signal
end
unless $started.val
$started.val = true
$delay = nil # say pause again; TODO muito feio!
start_it_all
end
end
####### algorithm thread control #########
def update_delay
# We have a linear funcion for the FPS speed
# with points (0, min) - 3 seconds of delay -
# and (100, max) - as fast as a cartoon
# the delay is the inverse of that
min = 1/3.0; max = 30.0
speed = $prog.glade['speed_bar'].value
$delay = 1.0/(speed*(max-min)/100.0 + min)
end
def run_alg
alg_class = nil
case $row_alg.val
when 0
alg_class = Simple
when 1
alg_class = Randomized
end
alg = alg_class.new($map, $painter, $prog.glade['statusbar_alg'], $prog.glade['main_statusbar'], $prog.glade['main_window'])
alg.build_struct do
alg.delay = $delay
yield nil
end
yield 1
$queries.each do |p|
alg.query(p) do
alg.delay = $delay
yield nil
end
end
rescue Exception => error
puts error
puts error.backtrace
end
def start_it_all
$thread = Thread.new do
run_alg do |stage|
unless stage.nil? # second stage starts now
$last_part.val = true
$mutex_jump.synchronize do # stop jumping
$jump = false
end
end
unless $jump
if $pap.val
$mutex_pap.synchronize do
$cv_pap.wait($mutex_pap)
end
else
if $delay.nil?
sleep
else
sleep $delay
end
end
end
end
finish
end
end
def finish
$started.val = false
$last_part.val = false
$jump = false # just for certifying
$prog.glade['bt_play_pause'].active = false
$prog.glade['main_statusbar'].push(0, "Pronto")
end
####### methods for our control #########
# flags controlling buttons
class BtFlag
# note that the value can have any type (we use boolean or integer)
attr_reader :val
def initialize(new_val)
@val = new_val
end
def val=(new_val)
@val = new_val
update_bts
end
end
$file_ok = BtFlag.new(false)
$started = BtFlag.new(false)
$row_alg = BtFlag.new(-1)
$pap = BtFlag.new(false)
$last_part = BtFlag.new(false)
def update_bts
$prog.glade['label_combo'].sensitive = $prog.glade['combobox_alg'].sensitive = !$started.val
$prog.glade['check_step'].sensitive = !$started.val
$prog.glade['label_speed'].sensitive = $prog.glade['speed_bar'].sensitive = !$pap.val
$prog.glade['bt_play_pause'].sensitive = $file_ok.val && ($row_alg.val != -1) && !($started.val && $pap.val)
$prog.glade['bt_stop'].sensitive = $started.val
$prog.glade['bt_step'].sensitive = $started.val && $pap.val
$prog.glade['bt_ff'].sensitive = $file_ok.val && ($row_alg.val != -1) && !$last_part.val
end
# controlling status bar
$status_msg = {
:open_file_end => "Arquivo lido com sucesso",
:open_file_err => "Erro lendo arquivo"
}
def write_status_bar(msg_id)
sbar = $prog.glade['main_statusbar']
sbar.push(0, $status_msg[msg_id])
end
# Main program
if __FILE__ == $0
PROG_PATH = 'localiza.glade'
PROG_NAME = 'POINT_LOCATION'
$prog = LocalizaGlade.new(PROG_PATH, nil, PROG_NAME)
update_bts
$mutex_pap = Mutex.new
$cv_pap = ConditionVariable.new
$mutex_jump = Mutex.new
$jump = false
$prog.change_drawing_widget
Gtk.main
end