File: ZoomStack.itcl

package info (click to toggle)
blt 2.4m-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,316 kB
  • ctags: 7,778
  • sloc: ansic: 68,187; tcl: 12,491; sh: 1,918; makefile: 709; csh: 25
file content (359 lines) | stat: -rw-r--r-- 9,840 bytes parent folder | download | duplicates (13)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import add itcl

class ZoomStackGraph {

    # The name of graph (nee the namespace path)
    variable graph ""

    # Indicates which corner of the rectangular zoom region 
    # is currently being choosen.  
    variable corner "first"

    # Coordinates of the current zoom region. They represent the
    # two corners of a rectangular area.  The two points are order 
    # independent.
    variable x1
    variable y1
    variable x2
    variable y2

    # A list of axis configuration commmands.  Acts as a stack to
    # unzoom the graph back to previous axis limits.
    variable stack {}

    constructor { args } {
	# This will need to change when we start using inheritance.
	set graph [info namespace tail $this]

	# What about collisions between the blt::graph instance
	# command and the ZoomStackGraph instance command?
	blt::graph $graph

	if { [llength $args] > 0 } {
	    $graph configure $args
	}
	# Set up the bindings to select/deselect the zoom region
	bind $graph <1> [code $this SelectPoint %x %y]
	bind $graph <3> [code $this ClearZoom]
	# The particular mouse buttons should be configurable.
    }
    destructor {
	if { [winfo exists $graph] } {
	    destroy $graph
	}
    }

    # These methods are used internally, within this class, to manage the
    # zoom stack.
    private method SaveCoords { x y } 
    private method Zoom {}
    private method Unzoom {}
    private method Push { cmd }
    private method Pop {}
    private method MarkPoint { x y }
    private method SetTitle { title }
    private method DrawBox { } 

    # These methods are called by "bind" and "after" from the Tk 
    # event loop.  Is there any way of hiding them, so that it
    # doesn't look to the user as part of the public interface?
    method ClearZoom {}
    method ClearTitle {}
    method UpdateOutline { x y } 
    method SelectPoint { x y }
}

# ----------------------------------------------------------------------
# 
# SaveCoords --
#
#	Given a point on the screen, transforms the point into graph 
#	coordinates and saves it as one of the points representing a 
#	corner of the zoom region. 
#
# ----------------------------------------------------------------------
body ZoomStackGraph::SaveCoords { x y } {

    set coords [$graph invtransform $x $y]
    set x [lindex $coords 0]
    set y [lindex $coords 1]

    scan [$graph xaxis limits] "%s %s" min max
    if { $x > $max } { 
	set x $max 
    } elseif { $x < $min } { 
	set x $min 
    }

    scan [$graph yaxis limits] "%s %s" min max
    if { $y > $max } { 
	set y $max 
    } elseif { $y < $min } { 
	set y $min 
    }

    if { $corner == "first" } {
	set x1 $x ; set y1 $y 
    } else {
	set x2 $x ; set y2 $y
    }
}

# ----------------------------------------------------------------------
# 
# MarkPoint --
#
#	Adds text around one of the corners of the zoom region.
#	The text consists of the x,y graph coordinates of the 
#	corner.
#
# ----------------------------------------------------------------------
body ZoomStackGraph::MarkPoint { x y } {

    set marker "bltZoom_text_$corner"
    set text [format "x=%.4g\ny=%.4g" $x $y] 

    if [$graph marker exists $marker] {
     	$graph marker configure $marker -coords { $x $y } -text $text 
    } else {
    	$graph marker create text -coords { $x $y } -name $marker \
   	    -font *lucida*-r-*-10-* \
	    -text $text -anchor center -bg {} -justify left
    }
}

# ----------------------------------------------------------------------
# 
# Empty --
#
#	Indicates if the stack of axis configuration commands is
#	empty.
#
# ----------------------------------------------------------------------
body ZoomStackGraph::Empty { } {
    return [llength $stack]
}


# ----------------------------------------------------------------------
# 
# Push --
#
#	Appends a command on the list "stack" which can be used
#	to return to previous graph x and y axis ranges.  
#
# ----------------------------------------------------------------------
body ZoomStackGraph::Push { cmd } {
    lappend stack $cmd
}

# ----------------------------------------------------------------------
# 
# Pop --
#
#	Remove the last item pushed onto the stack and returns it.
#
# ----------------------------------------------------------------------
body ZoomStackGraph::Pop { } {
    set cmd [lindex $stack end]
    set stack [lreplace $stack end end]
    return $cmd
}

# ----------------------------------------------------------------------
# 
# ClearTitle --
#
#	Clears the zoom title (displayed in the upper left corner
#	of the graph).  This routine is called from the event queue 
#	using "after".
#
# ----------------------------------------------------------------------
body ZoomStackGraph::ClearTitle {} {
    $graph marker delete "bltZoom_title"
}

# ----------------------------------------------------------------------
# 
# Unzoom --
#
#	Reverts to a previous zoom.  Resets the x and y axis limits
#	back to a previous setting.  First checks if there's anything
#	to pop back to.  In addition, displays a title in the upper
#	left corner showing the current zoom level.
#
# ----------------------------------------------------------------------
body ZoomStackGraph::Unzoom { } {

    if ![Empty] {

	# Reset the x and y axis limits, by invoking the saved graph
	# command.
	eval [Pop]

	# Cheat:  Using "Empty" to get the number of entries on the stack.
	set level [Empty]
	if { $level > 0 } {
	    SetTitle "Zoom #$level"
	}

	blt::busy hold $graph
	update
	if { $corner == "first" } {
	    # Remember to remove the zoom title in a couple of seconds
	    after 2000 [code $this ClearTitle]
	}
	blt::busy release $graph
    } else {
	$graph marker delete "bltZoom_title"
    }
}

# ----------------------------------------------------------------------
#
# Zoom --
# 
#	Push the old axis limits on the stack and set them to the
#	zoom region.
#
# ----------------------------------------------------------------------
body ZoomStackGraph::Zoom { } {
    $graph marker delete "bltZoom_*" 

    if { ($x1 == $x2) && ($y1 == $y2) } { 
	# The first and last points of the zoom region are the same.
	# Revert back to the start.
	return
    }

    # Put a command on the stack that lets us revert back to the current 
    # axis limits.
    set cmd [format {
	%s xaxis configure -min "%s" -max "%s"
	%s yaxis configure -min "%s" -max "%s"
    } $graph [$graph xaxis cget -min] [$graph xaxis cget -max] \
		 $graph [$graph yaxis cget -min] [$graph yaxis cget -max] ]
    Push $cmd

    # The first and last corners of the zoom region don't have to be
    # selected in ascending order.  So consider their relative positions
    # when setting min and max axis limits.

    if { $x1 > $x2 } { 
	$graph xaxis configure -min $x2 -max $x1 
    } elseif { $x1 < $x2 } {
	$graph xaxis configure -min $x1 -max $x2
    } 
    if { $y1 > $y2 } { 
	$graph yaxis configure -min $y2 -max $y1
    } elseif { $y1 < $y2 } {
	$graph yaxis configure -min $y1 -max $y2
    } 

    # Call "update" explicitly here after the graph is made busy.
    # This prevents the user from inadvertantly selecting another zoom
    # region when the graph is recalculating and redrawing itself.

    blt::busy hold $graph
    update
    blt::busy release $graph
}

# ----------------------------------------------------------------------
# 
# ClearZoom --
#
# ----------------------------------------------------------------------
body ZoomStackGraph::ClearZoom { } {

    $graph marker delete "bltZoom_*" 
    if { $corner == "first" } {
	# We're haven't started to select a zoom region, so assume
	# that we want to revert back to a previous zoom level.
	Unzoom
    } else {
	# Let the user re-pick the first corner again. So reset the
	# indicator "corner" and turn off the <Motion> binding.
	set corner "first"
	bind $graph <Motion> {}
    }
}

# ----------------------------------------------------------------------
# 
# SetTitle --
#
# ----------------------------------------------------------------------
body ZoomStackGraph::SetTitle { title } {

    $graph marker create text -name "bltZoom_title" -text $title \
	    -coords {-Inf Inf} -anchor nw -bg {} 
}

# ----------------------------------------------------------------------
# 
# UpdateOutline --
#
# ----------------------------------------------------------------------
body ZoomStackGraph::UpdateOutline { x y } {
    SaveCoords $x $y
    MarkPoint $x2 $y2
    DrawBox
}

# ----------------------------------------------------------------------
# 
# SelectPoint --
#
#	Invoked from the binding to ButtonPress-1 events.  Saves
#	a corner of zoom region.
#
#
# ----------------------------------------------------------------------
body ZoomStackGraph::SelectPoint { x y } {

    SaveCoords $x $y 
    if { $corner == "first" } {
	MarkPoint $x1 $y1

	# Display a new title indicating zoom pick is active
	set level [expr [llength $stack] + 1]
	SetTitle "Zoom #$level"

	# Start watching now for motion events, drawing an outline
	bind $graph <Any-Motion> [code $this UpdateOutline %x %y]

	# Indicate the next corner is the last 
	set corner last
    } else {

	# Stop watching motion events
	bind $graph <Any-Motion> {}

	# Zoom into the new region defined by the outline
	Zoom

	# Reset to select the first corner, again
	set corner first
    }
}

# ----------------------------------------------------------------------
# 
# DrawBox --
#
# ----------------------------------------------------------------------
body ZoomStackGraph::DrawBox { } {

    set coords {
	$x1 $y1 $x2 $y1 $x2 $y2 $x1 $y2 $x1 $y1
    }
    if [$graph marker exists "bltZoom_outline"] {
	$graph marker configure "bltZoom_outline" -coords $coords
    } else {
	$graph marker create line -coords $coords -name "bltZoom_outline" \
	    -dashes { 4 2 }
    }
    $graph marker before "bltZoom_outline"
}