/camera/imageviewer

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/imageviewer

« back to all changes in this revision

Viewing changes to pfview.rb

  • Committer: Suren A. Chilingaryan
  • Date: 2011-02-13 01:34:55 UTC
  • Revision ID: csa@dside.dyndns.org-20110213013455-7999955h7v4uf9m8
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby
 
2
 
 
3
require 'gtk2'
 
4
require 'gtkglext'
 
5
#require 'libglade2'
 
6
 
 
7
require 'rb-inotify'
 
8
 
 
9
require 'camera'
 
10
require 'player'
 
11
 
 
12
RESOLUTIONS = [
 
13
    [ 1280, 1024 ],
 
14
    [ 1152, 864 ],
 
15
    [ 1024, 768 ],
 
16
    [ 800, 600 ],
 
17
    [ 640, 480 ],
 
18
    [ 512, 384 ],
 
19
    [ 480, 360 ],
 
20
    [ 320, 240 ],
 
21
    [ 240, 180 ],
 
22
    [ 160, 120 ],
 
23
    [ 80, 60 ],
 
24
    [ 32, 24 ]
 
25
]
 
26
 
 
27
class UI < Gtk::Builder
 
28
 def initialize(cl)
 
29
    @cl = cl
 
30
    
 
31
    super()
 
32
    
 
33
    add_from_file('pfview.glade')
 
34
    connect_signals { |handler| method(handler) }
 
35
 
 
36
    @area = get_object('draw')
 
37
    @area.set_gl_capability(Gdk::GLConfig.new(Gdk::GLConfig::MODE_DEPTH | Gdk::GLConfig::MODE_DOUBLE | Gdk::GLConfig::MODE_RGB))
 
38
    @area.set_size_request(@cl.max_width, @cl.max_height)
 
39
 
 
40
    status = get_object('statusbar')
 
41
 
 
42
    @empty_pixbuf = Gdk::Pixbuf.new((0..(3*@cl.max_width*@cl.max_height-1)).map { 0.chr }.join, Gdk::Pixbuf::ColorSpace::RGB, false, 8, @cl.max_width, @cl.max_height, @cl.max_width)
 
43
 
 
44
    rs = get_object('resolution_store')
 
45
    
 
46
    @invalid_resolutions = 0
 
47
    RESOLUTIONS.each { |i|
 
48
        if (i[0] > @cl.max_width) or (i[1] > @cl.max_height) then 
 
49
            @invalid_resolutions += 1
 
50
        else 
 
51
            rs.append()[0] = sprintf('%ix%i',i[0],i[1])
 
52
        end
 
53
    }
 
54
    
 
55
    resolution = get_object('resolution')
 
56
    resolution.active = 0
 
57
    resolution.signal_connect('changed') { |widget|
 
58
        @cl.set_resolution(RESOLUTIONS[widget.active + @invalid_resolutions])
 
59
        draw_expose_event_cb(@area, nil)
 
60
    }
 
61
    
 
62
    exposure = get_object('exposure')
 
63
    exposure.value = @cl.exposure
 
64
    
 
65
    camera_mode = get_object('camera_mode')
 
66
    camera_mode.active = (@cl.camera_mode > 0)?1:0
 
67
 
 
68
    update_archive_store
 
69
 
 
70
    notifier = INotify::Notifier.new
 
71
    notifier_io = notifier.to_io
 
72
 
 
73
    notifier.watch("saved_camera_images", :moved_from, :moved_to, :create, :delete) {
 
74
        update_archive_store
 
75
    }
 
76
 
 
77
    GLib::Timeout.add(1000) {
 
78
        notifier.process if IO.select([notifier_io], [], [], 0)
 
79
        true
 
80
    }
 
81
    
 
82
 
 
83
    @pending_text = nil
 
84
    
 
85
    GLib::Timeout.add(100) {
 
86
        if @pending_text
 
87
            status.text = @pending_text
 
88
            @pending_text = nil
 
89
        end
 
90
        true
 
91
    }
 
92
    
 
93
 
 
94
 
 
95
    get_object('window1').visible = true
 
96
 
 
97
    fps_value_changed_cb(get_object('fps'))
 
98
    display_mode_changed_cb(get_object('display_mode'))
 
99
 
 
100
    @cl.set_procs(
 
101
        lambda{ |text, atonce|
 
102
            if atonce 
 
103
                #Expecting this called from main thread
 
104
                status.text = text 
 
105
                Gtk.main_iteration while Gtk.events_pending?
 
106
            else 
 
107
                #This causing crash and blocking as well not working good
 
108
                #status.text = text 
 
109
                @pending_text = text
 
110
            end
 
111
        },
 
112
        lambda{ |width, height, data|
 
113
            Gdk::RGB.draw_gray_image(@area.window, @area.style.fg_gc(@area.state), 0, 0, width, height, Gdk::RGB::DITHER_NORMAL, data, width)
 
114
        }
 
115
    )
 
116
 end
 
117
 
 
118
 def gtk_widget_delete(user_data, ev)
 
119
 end
 
120
 
 
121
 def gtk_main_quit(user_data)
 
122
    Gtk.main_quit
 
123
 end
 
124
 
 
125
 def gtk_widget_hide(widget)
 
126
    widget.visible = false
 
127
 end
 
128
 
 
129
 def fps_value_changed_cb(widget)
 
130
    @cl.display_fps =  widget.value
 
131
 end
 
132
 
 
133
 def display_mode_changed_cb(widget)
 
134
    @cl.display_mode = widget.active
 
135
 end
 
136
 
 
137
 def exposure_value_changed_cb(widget)
 
138
    @cl.exposure = widget.value
 
139
 end
 
140
 
 
141
 def camera_mode_changed_cb(widget)
 
142
    @cl.camera_mode = widget.active
 
143
 end
 
144
 
 
145
 def draw_expose_event_cb(area, event)
 
146
    @area.window.draw_pixbuf(nil, @empty_pixbuf, 0, 0, 0, 0, @cl.max_width, @cl.max_height, Gdk::RGB::DITHER_NORMAL, 0, 0)
 
147
 end
 
148
 
 
149
 
 
150
 def capture_clicked_cb(widget)
 
151
    widget.sensitive = false
 
152
    get_object('stop').sensitive = true
 
153
    get_object('replay').sensitive = false
 
154
 
 
155
    save = get_object('save')
 
156
    storage = @cl.start_capture(save.active, get_object('time').value) {
 
157
        get_object('stop').sensitive = false
 
158
        get_object('capture').sensitive = true
 
159
        get_object('replay').sensitive = true
 
160
    }
 
161
    
 
162
    if (storage > 0) then 
 
163
        save.active = storage
 
164
    end
 
165
 end
 
166
 
 
167
 def stop_clicked_cb(widget)
 
168
    @cl.stop_capture
 
169
 end
 
170
 
 
171
 def replay_clicked_cb(widget)
 
172
    @cl.replay { |width, height, images, frames|
 
173
        Player.new(get_object('fps').value, width, height, images, frames)
 
174
    }
 
175
 end
 
176
 
 
177
 
 
178
 def update_archive_store()
 
179
    a = get_object('archive')
 
180
    s = get_object('archive_store')
 
181
 
 
182
    cur = a.active
 
183
    cur_dir = (a.active >= 0)?s.get_value(a.active_iter, 1):nil
 
184
 
 
185
    curid = 0
 
186
    active = -1
 
187
    
 
188
    s.clear
 
189
    Dir.foreach("saved_camera_images") { |fn|
 
190
        if fn =~ /^(\d{8}_\d{6})(\.(.+))?$/
 
191
            item = s.append()
 
192
            item[0] = $2?sprintf("%s (%s)",$3, $1):"Unknown (#{fn})"
 
193
            item[1] = fn
 
194
 
 
195
            active = curid if cur_dir == fn
 
196
            curid += 1
 
197
        end
 
198
    }
 
199
    
 
200
    if curid > 0 then
 
201
        a.sensitive = true
 
202
        a.active = active
 
203
        if active >= 0 then
 
204
            get_object('play').sensitive = true
 
205
            get_object('delete').sensitive = true
 
206
        else
 
207
            get_object('play').sensitive = false
 
208
            get_object('delete').sensitive = false
 
209
        end     
 
210
    else
 
211
        a.active = -1
 
212
        a.sensitive = false
 
213
        get_object('play').sensitive = false
 
214
        get_object('delete').sensitive = false
 
215
    end
 
216
 end
 
217
 
 
218
 def archive_changed_cb(widget)
 
219
    if widget.active >= 0
 
220
        get_object('play').sensitive = true 
 
221
        get_object('delete').sensitive = true 
 
222
    end
 
223
 end
 
224
 
 
225
 def play_clicked_cb(widget)
 
226
    active_iter = get_object('archive').active_iter
 
227
    active = get_object('archive_store').get_value(active_iter, 1)
 
228
    Player.new(get_object('fps').value, nil, nil, active, nil)
 
229
 end
 
230
 
 
231
 def delete_clicked_cb(widget)
 
232
    active_iter = get_object('archive').active_iter
 
233
    active = get_object('archive_store').get_value(active_iter, 1)
 
234
    system "rm -rf saved_camera_images/#{active}"
 
235
 end
 
236
 
 
237
 def destroy()
 
238
    get_object('window1').destroy
 
239
 end
 
240
end
 
241
 
 
242
Thread.abort_on_exception = true
 
243
 
 
244
$freemem = Integer(`free -b | awk 'NR==3 { print $4 }'`)
 
245
 
 
246
cl = Camera.new
 
247
ui = UI.new(cl)
 
248
 
 
249
#It interupts Fg_ function in second thread and exception is comming from there... 
 
250
#Kernel.trap("INT") {
 
251
#    ui.destroy
 
252
#}
 
253
 
 
254
 
 
255
#GLib::Idle.add { cl.iteration }
 
256
 
 
257
# We need to do it here, otherewise GC.start call in camera will took to long
 
258
GC.start
 
259
 
 
260
cl.open
 
261
Gtk.main
 
262
cl.close