source: trunk/gui/scripts/textresult.tcl @ 438

Last change on this file since 438 was 413, checked in by mmc, 18 years ago
  • Added <description> capability to output objects, including axes.
  • Fixed the ResultSet? so that it is more compact and supports the simulation number as a parameter. This is useful when there are datasets with wildly varying parameters.
File size: 12.5 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: TextResult - Log output for ResultSet
3#
4#  This widget is used to show text output in a ResultSet.  The log
5#  output from a tool, for example, is rendered as a TextResult.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14package require BLT
15
16option add *TextResult.width 4i widgetDefault
17option add *TextResult.height 4i widgetDefault
18option add *TextResult.textBackground white widgetDefault
19option add *TextResult.font \
20    -*-helvetica-medium-r-normal-*-*-120-* widgetDefault
21option add *TextResult.textFont \
22    -*-courier-medium-r-normal-*-*-120-* widgetDefault
23
24itcl::class Rappture::TextResult {
25    inherit itk::Widget
26
27    constructor {args} { # defined below }
28
29    public method add {dataobj {settings ""}}
30    public method get {}
31    public method delete {args}
32    public method scale {args}
33    public method download {option}
34
35    public method select {option args}
36    public method find {option}
37
38    private variable _dataobj ""  ;# data object currently being displayed
39    private variable _raised      ;# maps all data objects => -raise param
40}
41                                                                               
42itk::usual TextResult {
43    keep -background -foreground -cursor -font
44}
45
46# ----------------------------------------------------------------------
47# CONSTRUCTOR
48# ----------------------------------------------------------------------
49itcl::body Rappture::TextResult::constructor {args} {
50    option add hull.width hull.height
51    pack propagate $itk_component(hull) no
52
53    #
54    # CONTROL BAR with find/select functions
55    #
56    itk_component add controls {
57        frame $itk_interior.cntls
58    }
59    pack $itk_component(controls) -side bottom -fill x -pady {4 0}
60
61    itk_component add selectall {
62        button $itk_component(controls).selall -text "Select All" \
63            -command [itcl::code $this select all]
64    }
65    pack $itk_component(selectall) -side right -fill y
66
67    itk_component add findl {
68        label $itk_component(controls).findl -text "Find:"
69    }
70    pack $itk_component(findl) -side left
71
72    itk_component add find {
73        entry $itk_component(controls).find -width 20
74    }
75    pack $itk_component(find) -side left
76
77    itk_component add finddown {
78        button $itk_component(controls).finddown \
79            -image [Rappture::icon finddn] \
80            -relief flat -overrelief raised \
81            -command [itcl::code $this find down]
82    } {
83        usual
84        ignore -relief
85    }
86    pack $itk_component(finddown) -side left
87
88    itk_component add findup {
89        button $itk_component(controls).findup \
90            -image [Rappture::icon findup] \
91            -relief flat -overrelief raised \
92            -command [itcl::code $this find up]
93    } {
94        usual
95        ignore -relief
96    }
97    pack $itk_component(findup) -side left
98
99    itk_component add findstatus {
100        label $itk_component(controls).finds -width 10 -anchor w
101    }
102    pack $itk_component(findstatus) -side left -expand yes -fill x
103
104    # shortcut for Return in search field
105    bind $itk_component(find) <KeyPress-Return> "
106        $itk_component(finddown) configure -relief sunken
107        update idletasks
108        after 200
109        $itk_component(finddown) configure -relief flat
110        $itk_component(finddown) invoke
111    "
112    bind $itk_component(find) <KeyPress> \
113        [itcl::code $this find reset]
114
115    #
116    # TEXT AREA
117    #
118    itk_component add scroller {
119        Rappture::Scroller $itk_interior.scroller \
120            -xscrollmode auto -yscrollmode auto
121    }
122    pack $itk_component(scroller) -expand yes -fill both
123
124    itk_component add text {
125        text $itk_component(scroller).text -width 1 -height 1 -wrap none
126    } {
127        usual
128        rename -background -textbackground textBackground Background
129        rename -font -textfont textFont Font
130    }
131    $itk_component(scroller) contents $itk_component(text)
132    $itk_component(text) configure -state disabled
133
134    $itk_component(text) tag configure ERROR -foreground red
135
136    eval itk_initialize $args
137}
138
139# ----------------------------------------------------------------------
140# USAGE: add <dataobj> ?<settings>?
141#
142# Clients use this to add a data object to the plot.  If the optional
143# <settings> are specified, then the are applied to the data.  Allowed
144# settings are -color and -brightness, -width, -linestyle and -raise.
145# (Many of these are ignored.)
146# ----------------------------------------------------------------------
147itcl::body Rappture::TextResult::add {dataobj {settings ""}} {
148    array set params {
149        -color ""
150        -brightness ""
151        -width ""
152        -linestyle ""
153        -raise 0
154    }
155    foreach {opt val} $settings {
156        if {![info exists params($opt)]} {
157            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
158        }
159        set params($opt) $val
160    }
161
162    set replace 0
163    if {"" != $dataobj} {
164        set _raised($dataobj) $params(-raise)
165        if {"" == $_dataobj} {
166            set replace 1
167        } elseif {$_raised($_dataobj) == 0 && $params(-raise)} {
168            set replace 1
169        }
170    }
171
172    if {$replace} {
173        $itk_component(text) configure -state normal
174        $itk_component(text) delete 1.0 end
175
176        if {[$dataobj element -as type] == "log"} {
177            # log output -- remove special =RAPPTURE-???=> messages
178            set message [$dataobj get]
179            while {[regexp -indices \
180                       {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \
181                        match type mesg]} {
182
183                foreach {i0 i1} $match break
184                set first [string range $message 0 [expr {$i0-1}]]
185                if {[string length $first] > 0} {
186                    $itk_component(text) insert end $first
187                }
188
189                foreach {t0 t1} $type break
190                set type [string range $message $t0 $t1]
191                foreach {m0 m1} $mesg break
192                set mesg [string range $message $m0 $m1]
193                if {[string length $mesg] > 0
194                       && $type != "RUN" && $type != "PROGRESS"} {
195                    $itk_component(text) insert end $mesg $type
196                    $itk_component(text) insert end \n $type
197                }
198                set message [string range $message [expr {$i1+1}] end]
199            }
200
201            if {[string length $message] > 0} {
202                $itk_component(text) insert end $message
203                if {[$itk_component(text) get end-2char] != "\n"} {
204                    $itk_component(text) insert end "\n"
205                }
206            }
207        } elseif {[$dataobj element -as type] == "string"} {
208            # add string values
209            $itk_component(text) insert end [$dataobj get current]
210        } else {
211            # any other string output -- add it directly
212            $itk_component(text) insert end [$dataobj get]
213        }
214        $itk_component(text) configure -state disabled
215
216        set _dataobj $dataobj
217    }
218}
219
220# ----------------------------------------------------------------------
221# USAGE: get
222#
223# Clients use this to query the list of objects being plotted, in
224# order from bottom to top of this result.
225# ----------------------------------------------------------------------
226itcl::body Rappture::TextResult::get {} {
227    return $_dataobj
228}
229
230# ----------------------------------------------------------------------
231# USAGE: delete ?<curve1> <curve2> ...?
232#
233# Clients use this to delete a curve from the plot.  If no curves
234# are specified, then all curves are deleted.
235# ----------------------------------------------------------------------
236itcl::body Rappture::TextResult::delete {args} {
237    if {[llength $args] == 0} {
238        # delete everything
239        catch {unset _raised}
240        set _dataobj ""
241        $itk_component(text) configure -state normal
242        $itk_component(text) delete 1.0 end
243        $itk_component(text) configure -state disabled
244    } else {
245        # delete these specific objects
246        foreach obj $args {
247            catch {unset _raised($obj)}
248            if {$obj == $_dataobj} {
249                set _dataobj ""
250                $itk_component(text) configure -state normal
251                $itk_component(text) delete 1.0 end
252                $itk_component(text) configure -state disabled
253            }
254        }
255    }
256}
257
258# ----------------------------------------------------------------------
259# USAGE: scale ?<curve1> <curve2> ...?
260#
261# Sets the default limits for the overall plot according to the
262# limits of the data for all of the given <curve> objects.  This
263# accounts for all curves--even those not showing on the screen.
264# Because of this, the limits are appropriate for all curves as
265# the user scans through data in the ResultSet viewer.
266# ----------------------------------------------------------------------
267itcl::body Rappture::TextResult::scale {args} {
268    # nothing to do for text
269}
270
271# ----------------------------------------------------------------------
272# USAGE: download coming
273# USAGE: download now
274#
275# Clients use this method to create a downloadable representation
276# of the plot.  Returns a list of the form {ext string}, where
277# "ext" is the file extension (indicating the type of data) and
278# "string" is the data itself.
279# ----------------------------------------------------------------------
280itcl::body Rappture::TextResult::download {option} {
281    switch $option {
282        coming {
283            # nothing to do
284        }
285        now {
286            return [list .txt [$itk_component(text) get 1.0 end]]
287        }
288        default {
289            error "bad option \"$option\": should be coming, now"
290        }
291    }
292}
293
294# ----------------------------------------------------------------------
295# USAGE: select all
296#
297# Handles various selection operations within the text.
298# ----------------------------------------------------------------------
299itcl::body Rappture::TextResult::select {option args} {
300    switch -- $option {
301        all {
302            $itk_component(text) tag add sel 1.0 end
303        }
304        default {
305            error "bad option \"$option\": should be all"
306        }
307    }
308}
309
310# ----------------------------------------------------------------------
311# USAGE: find up
312# USAGE: find down
313# USAGE: find reset
314#
315# Handles various find operations within the text.  These find a
316# bit of text and highlight it within the widget.  The find operation
317# starts from the end of the currently highlighted text, or from the
318# beginning if there is no highlight.
319# ----------------------------------------------------------------------
320itcl::body Rappture::TextResult::find {option} {
321    # handle the reset case...
322    $itk_component(find) configure \
323        -background $itk_option(-background) \
324        -foreground $itk_option(-foreground)
325    $itk_component(findstatus) configure -text ""
326
327    if {$option == "reset"} {
328        return
329    }
330
331    # handle the up/down cases...
332    set t $itk_component(text)
333    set pattern [string trim [$itk_component(find) get]]
334
335    if {"" == $pattern} {
336        $itk_component(find) configure -background red -foreground white
337        $itk_component(findstatus) configure -text "<< Enter a search string"
338        return
339    }
340
341    # find the starting point for the search
342    set seln [$t tag nextrange sel 1.0]
343    if {$seln == ""} {
344        set t0 1.0
345        set t1 end
346    } else {
347        foreach {t0 t1} $seln break
348    }
349    $t tag remove sel 1.0 end
350
351    # search up or down
352    switch -- $option {
353        up {
354            set start [$t index $t0-1char]
355            set next [$t search -backwards -nocase -- $pattern $start]
356        }
357        down {
358            set start [$t index $t1+1char]
359            set next [$t search -forwards -nocase -- $pattern $start]
360        }
361    }
362
363    if {"" != $next} {
364        set len [string length $pattern]
365        $t tag add sel $next $next+${len}chars
366        $t see $next
367        set lnum [lindex [split $next .] 0]
368        set lines [lindex [split [$t index end] .] 0]
369        set percent [expr {round(100.0*$lnum/$lines)}]
370        set status "line $lnum   --$percent%--"
371    } else {
372        set status "Not found"
373        $itk_component(find) configure -background red -foreground white
374    }
375    $itk_component(findstatus) configure -text $status
376}
Note: See TracBrowser for help on using the repository browser.