source: tags/20110826/gui/scripts/videochooserinfo.tcl @ 4643

Last change on this file since 4643 was 2069, checked in by dkearney, 13 years ago

adding ability to pass a directory holding thumbnail images to the videochooer. if it can find a premade thumbnail image, it doesn't have to open each video and dynamically create the images

File size: 10.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: videochooserinfo - video info
3#
4# ======================================================================
5#  AUTHOR:  Derrick Kearney, Purdue University
6#  Copyright (c) 2005-2010  Purdue Research Foundation
7#
8# See the file "license.terms" for information on usage and redistribution of
9# this file, and for a DISCLAIMER OF ALL WARRANTIES.
10# ======================================================================
11
12package require Itk
13package require BLT
14package require Img
15package require Rappture
16
17itcl::class Rappture::VideoChooserInfo {
18    inherit itk::Widget
19
20    itk_option define -width width Width 96
21    itk_option define -height height Height 54
22    itk_option define -selectedwidth selectedwidth Selectedwidth 112
23    itk_option define -selectedheight selectedheight Selectedheight 63
24    itk_option define -variable variable Variable ""
25    itk_option define -thumbsdir thumbsdir Thumbsdir ""
26
27    constructor { args } {
28        # defined below
29    }
30    destructor {
31        # defined below
32    }
33
34    public method load {path about}
35    public method select {}
36    public method query {what}
37
38    protected method _fixSize {}
39    protected method _fixValue {args}
40
41    private method _bindings {sequence}
42
43    private variable _preview           ""
44    private variable _selected          ""
45    private variable _path              ""
46    private variable _width             0
47    private variable _height            0
48    private variable _selectedwidth     0
49    private variable _selectedheight    0
50    private variable _variable          "" ;# variable associated with -variable
51}
52
53
54itk::usual VideoChooserInfo {
55}
56
57# ----------------------------------------------------------------------
58# CONSTRUCTOR
59# ----------------------------------------------------------------------
60itcl::body Rappture::VideoChooserInfo::constructor {args} {
61
62    itk_component add main {
63        canvas $itk_interior.main
64    } {
65        usual
66        rename -background -controlbackground controlBackground Background
67    }
68    bind $itk_component(main) <Configure> [itcl::code $this _fixSize]
69
70    blt::table $itk_interior \
71        0,0 $itk_component(main) -fill both -pady 2
72
73    blt::table configure $itk_interior c* -resize both
74    blt::table configure $itk_interior r0 -resize both
75
76    eval itk_initialize $args
77}
78
79# ----------------------------------------------------------------------
80# DESTRUCTOR
81# ----------------------------------------------------------------------
82itcl::body Rappture::VideoChooserInfo::destructor {} {
83    image delete ${_preview}
84    image delete ${_selected}
85}
86
87# ----------------------------------------------------------------------
88# USAGE: load
89# ----------------------------------------------------------------------
90itcl::body Rappture::VideoChooserInfo::load {path about} {
91
92    if {![file readable $path]} {
93        error "bad path $path: file not readable"
94    }
95
96    set _path $path
97    set preview_fname ""
98    set selected_fname ""
99
100    set _preview [image create photo]
101    set _selected [image create photo]
102
103    if {[string compare "" $itk_option(-thumbsdir)] != 0} {
104        set root [file tail [file rootname ${_path}]]
105        set preview_fname [file join $itk_option(-thumbsdir) ${root}_${_width}x${_height}.png]
106        set selected_fname [file join $itk_option(-thumbsdir) ${root}_${_selectedwidth}x${_selectedheight}.png]
107
108        if {[file readable $preview_fname] && [file readable $selected_fname]} {
109            set fid [open $preview_fname r]
110            fconfigure $fid -translation binary
111            ${_preview} put [read $fid]
112            close $fid
113
114            set fid [open $selected_fname r]
115            fconfigure $fid -translation binary
116            ${_selected} put [read $fid]
117            close $fid
118        }
119    } else {
120        # there are some incomplete movies that don't open up
121        set err [catch {
122            set movie [Rappture::Video file ${_path}]
123            $movie seek 60
124
125            ${_preview} put [$movie get image ${_width} ${_height}]
126            ${_selected} put [$movie get image ${_selectedwidth} ${_selectedheight}]
127
128            $movie release
129        }]
130        if {$err} {
131            puts stderr "Error while opening movie: $movie"
132            return
133        }
134    }
135
136    set cw [expr round(${_selectedwidth}/2.0)]
137    set ch [expr round(${_selectedheight}/2.0)]
138    $itk_component(main) create image $cw $ch \
139        -anchor center \
140        -image ${_preview} \
141        -activeimage ${_selected} \
142        -tags preview
143
144    $itk_component(main) bind preview <ButtonPress-1> [itcl::code $this _bindings b1press]
145    $itk_component(main) bind preview <Enter> [itcl::code $this _bindings enter]
146    $itk_component(main) bind preview <Leave> [itcl::code $this _bindings leave]
147
148    _fixSize
149}
150
151# ----------------------------------------------------------------------
152# USAGE: select
153# ----------------------------------------------------------------------
154itcl::body Rappture::VideoChooserInfo::select {} {
155    if {"" == $_variable} {
156        return
157    }
158    # send this object's movie to the preview window
159
160    upvar #0 $_variable var
161    set var ${_path}
162
163    foreach {x0 y0 x1 y1} [$itk_component(main) bbox preview] break
164    set x0 [expr 0]
165    set y0 [expr 2]
166    set x1 [expr [$itk_component(main) cget -width]-2]
167    set y1 [expr [$itk_component(main) cget -height]-2]
168
169    # there is something wrong with the way we place the
170    # images on the canvas, or how the canvas is shapeed.
171    # the placement of the small image seems to be off
172    # by a pixel or two. for now we fudge the numbers to
173    # make the red line accesnts look decent.
174
175    $itk_component(main) create line $x0 $y0 $x1 $y0 \
176        -fill red \
177        -width 3 \
178        -tags previewselected
179
180    $itk_component(main) create line $x0 $y1 $x1 $y1 \
181        -fill red \
182        -width 3 \
183        -tags previewselected
184
185#    $itk_component(main) create rectangle \
186        [$itk_component(main) bbox preview] \
187        -outline red \
188        -width 4 \
189        -tags previewselected
190#    $itk_component(main) configure -background red
191}
192
193
194# ----------------------------------------------------------------------
195# USAGE: _bindings
196# ----------------------------------------------------------------------
197itcl::body Rappture::VideoChooserInfo::_bindings {sequence} {
198    switch -- $sequence {
199        "b1press" {
200            select
201        }
202        "enter" {
203            $itk_component(main) itemconfigure previewselected -state hidden
204        }
205        "leave" {
206            $itk_component(main) itemconfigure previewselected -state normal
207        }
208        default {}
209    }
210}
211
212# ----------------------------------------------------------------------
213# USAGE: _fixValue ?<name1> <name2> <op>?
214#
215# Invoked automatically whenever the -variable associated with this
216# widget is modified.  Copies the value to the current settings for
217# the widget.
218# ----------------------------------------------------------------------
219itcl::body Rappture::VideoChooserInfo::_fixValue {args} {
220    if {"" == $itk_option(-variable)} {
221        return
222    }
223    upvar #0 $itk_option(-variable) var
224    if {[string compare $var ${_path}] != 0} {
225        # unselect this object
226        $itk_component(main) delete previewselected
227        #$itk_component(main) configure -background black
228    }
229}
230
231# ----------------------------------------------------------------------
232# USAGE: _fixSize
233# ----------------------------------------------------------------------
234itcl::body Rappture::VideoChooserInfo::_fixSize {} {
235    $itk_component(main) configure -width ${_selectedwidth} -height ${_selectedheight}
236}
237
238# ----------------------------------------------------------------------
239# USAGE: query
240# ----------------------------------------------------------------------
241itcl::body Rappture::VideoChooserInfo::query {what} {
242    switch -- $what {
243        "preview" {return ${_preview}}
244        "selected" {return ${_selected}}
245        "file" {return ${_path}}
246    }
247}
248
249# ----------------------------------------------------------------------
250# CONFIGURE: -variable
251# ----------------------------------------------------------------------
252itcl::configbody Rappture::VideoChooserInfo::variable {
253    if {"" != $_variable} {
254        upvar #0 $_variable var
255        trace remove variable var write [itcl::code $this _fixValue]
256    }
257
258    set _variable $itk_option(-variable)
259
260    if {"" != $_variable} {
261        upvar #0 $_variable var
262        trace add variable var write [itcl::code $this _fixValue]
263
264        # sync to the current value of this variable
265        if {[info exists var]} {
266            _fixValue
267        }
268    }
269}
270
271# ----------------------------------------------------------------------
272# OPTION: -width
273# ----------------------------------------------------------------------
274itcl::configbody Rappture::VideoChooserInfo::width {
275    # $_dispatcher event -idle !fixsize
276    if {[string is integer $itk_option(-width)] == 0} {
277        error "bad value: \"$itk_option(-width)\": width should be an integer"
278    }
279    set _width $itk_option(-width)
280    after idle [itcl::code $this _fixSize]
281}
282
283# ----------------------------------------------------------------------
284# OPTION: -height
285# ----------------------------------------------------------------------
286itcl::configbody Rappture::VideoChooserInfo::height {
287    # $_dispatcher event -idle !fixsize
288    if {[string is integer $itk_option(-height)] == 0} {
289        error "bad value: \"$itk_option(-height)\": height should be an integer"
290    }
291    set _height $itk_option(-height)
292    after idle [itcl::code $this _fixSize]
293}
294
295# ----------------------------------------------------------------------
296# OPTION: -selectedwidth
297# ----------------------------------------------------------------------
298itcl::configbody Rappture::VideoChooserInfo::selectedwidth {
299    # $_dispatcher event -idle !fixsize
300    if {[string is integer $itk_option(-selectedwidth)] == 0} {
301        error "bad value: \"$itk_option(-selectedwidth)\": selectedwidth should be an integer"
302    }
303    set _selectedwidth $itk_option(-selectedwidth)
304    after idle [itcl::code $this _fixSize]
305}
306
307# ----------------------------------------------------------------------
308# OPTION: -selectedheight
309# ----------------------------------------------------------------------
310itcl::configbody Rappture::VideoChooserInfo::selectedheight {
311    # $_dispatcher event -idle !fixsize
312    if {[string is integer $itk_option(-selectedheight)] == 0} {
313        error "bad value: \"$itk_option(-selectedheight)\": selectedheight should be an integer"
314    }
315    set _selectedheight $itk_option(-selectedheight)
316    after idle [itcl::code $this _fixSize]
317}
Note: See TracBrowser for help on using the repository browser.