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