| •1 |
#import add itcl |
| 2 |
package require Itcl |
| 3 |
namespace import itcl::* |
| 4 |
|
| 5 |
class ZoomStackGraph { |
| 6 |
|
| 7 |
# The name of graph (nee the namespace path) |
| 8 |
variable graph "" |
| 9 |
|
| 10 |
# Indicates which corner of the rectangular zoom region |
| 11 |
# is currently being choosen. |
| 12 |
variable corner "first" |
| 13 |
|
| 14 |
# Coordinates of the current zoom region. They represent the |
| 15 |
# two corners of a rectangular area. The two points are order |
| 16 |
# independent. |
| 17 |
variable x1 |
| 18 |
variable y1 |
| 19 |
variable x2 |
| 20 |
variable y2 |
| 21 |
|
| 22 |
# A list of axis configuration commmands. Acts as a stack to |
| 23 |
# unzoom the graph back to previous axis limits. |
| 24 |
variable stack {} |
| 25 |
|
| 26 |
constructor { args } { |
| 27 |
# This will need to change when we start using inheritance. |
| 28 |
set graph [info namespace tail $this] |
| 29 |
|
| 30 |
# What about collisions between the blt::graph instance |
| 31 |
# command and the ZoomStackGraph instance command? |
| 32 |
blt::graph $graph |
| 33 |
|
| 34 |
if { [llength $args] > 0 } { |
| 35 |
$graph configure $args |
| 36 |
} |
| 37 |
# Set up the bindings to select/deselect the zoom region |
| 38 |
bind $graph <1> [code $this SelectPoint %x %y] |
| 39 |
bind $graph <3> [code $this ClearZoom] |
| 40 |
# The particular mouse buttons should be configurable. |
| 41 |
} |
| 42 |
destructor { |
| 43 |
if { [winfo exists $graph] } { |
| 44 |
destroy $graph |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
# These methods are used internally, within this class, to manage the |
| 49 |
# zoom stack. |
| 50 |
private method SaveCoords { x y } |
| 51 |
private method Zoom {} |
| 52 |
private method Unzoom {} |
| •53 |
private method Empty {} |
| 54 |
private method Push { cmd } |
| 55 |
private method Pop {} |
| 56 |
private method MarkPoint { x y } |
| 57 |
private method SetTitle { title } |
| 58 |
private method DrawBox { } |
| 59 |
|
| 60 |
# These methods are called by "bind" and "after" from the Tk |
| 61 |
# event loop. Is there any way of hiding them, so that it |
| 62 |
# doesn't look to the user as part of the public interface? |
| 63 |
method ClearZoom {} |
| 64 |
method ClearTitle {} |
| 65 |
method UpdateOutline { x y } |
| 66 |
method SelectPoint { x y } |
| 67 |
} |
| 68 |
|
| 69 |
# ---------------------------------------------------------------------- |
| 70 |
# |
| 71 |
# SaveCoords -- |
| 72 |
# |
| 73 |
# Given a point on the screen, transforms the point into graph |
| 74 |
# coordinates and saves it as one of the points representing a |
| 75 |
# corner of the zoom region. |
| 76 |
# |
| 77 |
# ---------------------------------------------------------------------- |
| 78 |
body ZoomStackGraph::SaveCoords { x y } { |
| 79 |
|
| 80 |
set coords [$graph invtransform $x $y] |
| 81 |
set x [lindex $coords 0] |
| 82 |
set y [lindex $coords 1] |
| 83 |
|
| 84 |
scan [$graph xaxis limits] "%s %s" min max |
| 85 |
if { $x > $max } { |
| 86 |
set x $max |
| 87 |
} elseif { $x < $min } { |
| 88 |
set x $min |
| 89 |
} |
| 90 |
|
| 91 |
scan [$graph yaxis limits] "%s %s" min max |
| 92 |
if { $y > $max } { |
| 93 |
set y $max |
| 94 |
} elseif { $y < $min } { |
| 95 |
set y $min |
| 96 |
} |
| 97 |
|
| 98 |
if { $corner == "first" } { |
| 99 |
set x1 $x ; set y1 $y |
| 100 |
} else { |
| 101 |
set x2 $x ; set y2 $y |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
# ---------------------------------------------------------------------- |
| 106 |
# |
| 107 |
# MarkPoint -- |
| 108 |
# |
| 109 |
# Adds text around one of the corners of the zoom region. |
| 110 |
# The text consists of the x,y graph coordinates of the |
| 111 |
# corner. |
| 112 |
# |
| 113 |
# ---------------------------------------------------------------------- |
| 114 |
body ZoomStackGraph::MarkPoint { x y } { |
| 115 |
|
| 116 |
set marker "bltZoom_text_$corner" |
| 117 |
set text [format "x=%.4g\ny=%.4g" $x $y] |
| 118 |
|
| 119 |
if [$graph marker exists $marker] { |
| 120 |
$graph marker configure $marker -coords { $x $y } -text $text |
| 121 |
} else { |
| 122 |
$graph marker create text -coords { $x $y } -name $marker \ |
| 123 |
-font *lucida*-r-*-10-* \ |
| 124 |
-text $text -anchor center -bg {} -justify left |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
# ---------------------------------------------------------------------- |
| 129 |
# |
| 130 |
# Empty -- |
| 131 |
# |
| 132 |
# Indicates if the stack of axis configuration commands is |
| 133 |
# empty. |
| 134 |
# |
| 135 |
# ---------------------------------------------------------------------- |
| 136 |
body ZoomStackGraph::Empty { } { |
| 137 |
return [llength $stack] |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
# ---------------------------------------------------------------------- |
| 142 |
# |
| 143 |
# Push -- |
| 144 |
# |
| 145 |
# Appends a command on the list "stack" which can be used |
| 146 |
# to return to previous graph x and y axis ranges. |
| 147 |
# |
| 148 |
# ---------------------------------------------------------------------- |
| 149 |
body ZoomStackGraph::Push { cmd } { |
| 150 |
lappend stack $cmd |
| 151 |
} |
| 152 |
|
| 153 |
# ---------------------------------------------------------------------- |
| 154 |
# |
| 155 |
# Pop -- |
| 156 |
# |
| 157 |
# Remove the last item pushed onto the stack and returns it. |
| 158 |
# |
| 159 |
# ---------------------------------------------------------------------- |
| 160 |
body ZoomStackGraph::Pop { } { |
| 161 |
set cmd [lindex $stack end] |
| 162 |
set stack [lreplace $stack end end] |
| 163 |
return $cmd |
| 164 |
} |
| 165 |
|
| 166 |
# ---------------------------------------------------------------------- |
| 167 |
# |
| 168 |
# ClearTitle -- |
| 169 |
# |
| 170 |
# Clears the zoom title (displayed in the upper left corner |
| 171 |
# of the graph). This routine is called from the event queue |
| 172 |
# using "after". |
| 173 |
# |
| 174 |
# ---------------------------------------------------------------------- |
| 175 |
body ZoomStackGraph::ClearTitle {} { |
| 176 |
$graph marker delete "bltZoom_title" |
| 177 |
} |
| 178 |
|
| 179 |
# ---------------------------------------------------------------------- |
| 180 |
# |
| 181 |
# Unzoom -- |
| 182 |
# |
| 183 |
# Reverts to a previous zoom. Resets the x and y axis limits |
| 184 |
# back to a previous setting. First checks if there's anything |
| 185 |
# to pop back to. In addition, displays a title in the upper |
| 186 |
# left corner showing the current zoom level. |
| 187 |
# |
| 188 |
# ---------------------------------------------------------------------- |
| 189 |
body ZoomStackGraph::Unzoom { } { |
| 190 |
|
| 191 |
if ![Empty] { |
| 192 |
|
| 193 |
# Reset the x and y axis limits, by invoking the saved graph |
| 194 |
# command. |
| 195 |
eval [Pop] |
| 196 |
|
| 197 |
# Cheat: Using "Empty" to get the number of entries on the stack. |
| 198 |
set level [Empty] |
| 199 |
if { $level > 0 } { |
| 200 |
SetTitle "Zoom #$level" |
| 201 |
} |
| 202 |
|
| 203 |
blt::busy hold $graph |
| 204 |
update |
| 205 |
if { $corner == "first" } { |
| 206 |
# Remember to remove the zoom title in a couple of seconds |
| 207 |
after 2000 [code $this ClearTitle] |
| 208 |
} |
| 209 |
blt::busy release $graph |
| 210 |
} else { |
| 211 |
$graph marker delete "bltZoom_title" |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
# ---------------------------------------------------------------------- |
| 216 |
# |
| 217 |
# Zoom -- |
| 218 |
# |
| 219 |
# Push the old axis limits on the stack and set them to the |
| 220 |
# zoom region. |
| 221 |
# |
| 222 |
# ---------------------------------------------------------------------- |
| 223 |
body ZoomStackGraph::Zoom { } { |
| 224 |
$graph marker delete "bltZoom_*" |
| 225 |
|
| 226 |
if { ($x1 == $x2) && ($y1 == $y2) } { |
| 227 |
# The first and last points of the zoom region are the same. |
| 228 |
# Revert back to the start. |
| 229 |
return |
| 230 |
} |
| 231 |
|
| 232 |
# Put a command on the stack that lets us revert back to the current |
| 233 |
# axis limits. |
| 234 |
set cmd [format { |
| 235 |
%s xaxis configure -min "%s" -max "%s" |
| 236 |
%s yaxis configure -min "%s" -max "%s" |
| 237 |
} $graph [$graph xaxis cget -min] [$graph xaxis cget -max] \ |
| 238 |
$graph [$graph yaxis cget -min] [$graph yaxis cget -max] ] |
| 239 |
Push $cmd |
| 240 |
|
| 241 |
# The first and last corners of the zoom region don't have to be |
| 242 |
# selected in ascending order. So consider their relative positions |
| 243 |
# when setting min and max axis limits. |
| 244 |
|
| 245 |
if { $x1 > $x2 } { |
| 246 |
$graph xaxis configure -min $x2 -max $x1 |
| 247 |
} elseif { $x1 < $x2 } { |
| 248 |
$graph xaxis configure -min $x1 -max $x2 |
| 249 |
} |
| 250 |
if { $y1 > $y2 } { |
| 251 |
$graph yaxis configure -min $y2 -max $y1 |
| 252 |
} elseif { $y1 < $y2 } { |
| 253 |
$graph yaxis configure -min $y1 -max $y2 |
| 254 |
} |
| 255 |
|
| 256 |
# Call "update" explicitly here after the graph is made busy. |
| 257 |
# This prevents the user from inadvertantly selecting another zoom |
| 258 |
# region when the graph is recalculating and redrawing itself. |
| 259 |
|
| 260 |
blt::busy hold $graph |
| 261 |
update |
| 262 |
blt::busy release $graph |
| 263 |
} |
| 264 |
|
| 265 |
# ---------------------------------------------------------------------- |
| 266 |
# |
| 267 |
# ClearZoom -- |
| 268 |
# |
| 269 |
# ---------------------------------------------------------------------- |
| 270 |
body ZoomStackGraph::ClearZoom { } { |
| 271 |
|
| 272 |
$graph marker delete "bltZoom_*" |
| 273 |
if { $corner == "first" } { |
| 274 |
# We're haven't started to select a zoom region, so assume |
| 275 |
# that we want to revert back to a previous zoom level. |
| 276 |
Unzoom |
| 277 |
} else { |
| 278 |
# Let the user re-pick the first corner again. So reset the |
| 279 |
# indicator "corner" and turn off the <Motion> binding. |
| 280 |
set corner "first" |
| 281 |
bind $graph <Motion> {} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
# ---------------------------------------------------------------------- |
| 286 |
# |
| 287 |
# SetTitle -- |
| 288 |
# |
| 289 |
# ---------------------------------------------------------------------- |
| 290 |
body ZoomStackGraph::SetTitle { title } { |
| 291 |
|
| 292 |
$graph marker create text -name "bltZoom_title" -text $title \ |
| 293 |
-coords {-Inf Inf} -anchor nw -bg {} |
| 294 |
} |
| 295 |
|
| 296 |
# ---------------------------------------------------------------------- |
| 297 |
# |
| 298 |
# UpdateOutline -- |
| 299 |
# |
| 300 |
# ---------------------------------------------------------------------- |
| 301 |
body ZoomStackGraph::UpdateOutline { x y } { |
| 302 |
SaveCoords $x $y |
| 303 |
MarkPoint $x2 $y2 |
| 304 |
DrawBox |
| 305 |
} |
| 306 |
|
| 307 |
# ---------------------------------------------------------------------- |
| 308 |
# |
| 309 |
# SelectPoint -- |
| 310 |
# |
| 311 |
# Invoked from the binding to ButtonPress-1 events. Saves |
| 312 |
# a corner of zoom region. |
| 313 |
# |
| 314 |
# |
| 315 |
# ---------------------------------------------------------------------- |
| 316 |
body ZoomStackGraph::SelectPoint { x y } { |
| 317 |
|
| 318 |
SaveCoords $x $y |
| 319 |
if { $corner == "first" } { |
| 320 |
MarkPoint $x1 $y1 |
| 321 |
|
| 322 |
# Display a new title indicating zoom pick is active |
| 323 |
set level [expr [llength $stack] + 1] |
| 324 |
SetTitle "Zoom #$level" |
| 325 |
|
| 326 |
# Start watching now for motion events, drawing an outline |
| 327 |
bind $graph <Any-Motion> [code $this UpdateOutline %x %y] |
| 328 |
|
| 329 |
# Indicate the next corner is the last |
| 330 |
set corner last |
| 331 |
} else { |
| 332 |
|
| 333 |
# Stop watching motion events |
| 334 |
bind $graph <Any-Motion> {} |
| 335 |
|
| 336 |
# Zoom into the new region defined by the outline |
| 337 |
Zoom |
| 338 |
|
| 339 |
# Reset to select the first corner, again |
| 340 |
set corner first |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
# ---------------------------------------------------------------------- |
| 345 |
# |
| 346 |
# DrawBox -- |
| 347 |
# |
| 348 |
# ---------------------------------------------------------------------- |
| 349 |
body ZoomStackGraph::DrawBox { } { |
| 350 |
|
| 351 |
set coords { |
| 352 |
$x1 $y1 $x2 $y1 $x2 $y2 $x1 $y2 $x1 $y1 |
| 353 |
} |
| 354 |
if [$graph marker exists "bltZoom_outline"] { |
| 355 |
$graph marker configure "bltZoom_outline" -coords $coords |
| 356 |
} else { |
| 357 |
$graph marker create line -coords $coords -name "bltZoom_outline" \ |
| 358 |
-dashes { 4 2 } |
| 359 |
} |
| 360 |
$graph marker before "bltZoom_outline" |
| 361 |
} |
| 362 |
|