chiark / gitweb /
where-vessels: filtering UI
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 13 Dec 2009 16:37:36 +0000 (16:37 +0000)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 13 Dec 2009 16:37:36 +0000 (16:37 +0000)
yarrg/where-vessels

index 6f4433fa9e7efc7521820ad94f64d5507756c270..c382c6a4b92f913218c2e6c73e40401723a19feb 100755 (executable)
@@ -244,8 +244,8 @@ proc display-note-infos {} {
 #---------- vessel properties ----------
 
 proc vesselclasses-init {} {
-    global vc_game2code vc_code2abbrev vc_code2full
-    foreach {game code abbrev full} {
+    global vc_game2code vc_code2abbrev vc_code2full vc_codes
+    set vcl {
        smsloop         am      sl      Sloop
        lgsloop         bm      ct      Cutter
        dhow            cm      dh      Dhow
@@ -257,25 +257,32 @@ proc vesselclasses-init {} {
        warfrig         im      wf      {War Frigate}
        merchgal        jm      mg      {Merchant Galleon}
        grandfrig       km      gf      {Grand Frigate}
-    } {
+    }
+    set vc_codes {}
+    foreach {game code abbrev full} $vcl {
+       lappend vc_codes $code
        set vc_game2code($game) $code
        set vc_code2abbrev($code) $abbrev
        set vc_code2full($code) $full
     }
 }
 
+proc code2abbrev-lock {lockown} {
+    manyset [split $lockown ""] lock notown
+    append abbrev [lindex {* + -} $lock]
+    append abbrev [lindex {= - ?} [regsub {\D} $notown 2]]
+}    
+
 proc code2abbrev {code} {
     global vc_code2abbrev
 
     manyset [split $code _] inport class subclass lockown xabbrev
-    manyset [split $lockown ""] lock notown
 
     set abbrev {}
     append abbrev [lindex {? {}} $inport]
     append abbrev $vc_code2abbrev($class)
     append abbrev $subclass
-    append abbrev [lindex {* + -} $lock]
-    append abbrev [lindex {= - ?} [regsub {\D} $notown 2]]
+    append abbrev [code2abbrev-lock $lockown]
     append abbrev $xabbrev
 
     debug "CODE2ABBREV $code $abbrev"
@@ -315,6 +322,141 @@ proc show-report-decode {code} {
        report-set xabbrev "No flags in notes"
     }
 }
+
+#---------- filtering ----------
+
+set filters {}
+
+#proc make-size-filter {minmax minmaxdesc} {
+#    set n [llength $vc_codes]
+#    $w configure 
+
+#    make-filter size${minmax} "$minmaxdesc size" scale \
+#      [list \
+#           -tickinterval 0 \
+#           -variable filter_size{$minmax} \
+#           -from 0 -to [expr {$n-1}] \
+#           -resolution 1 \
+#           -length [expr {$n*5}] \
+#           ]
+
+
+proc filter-values/size {} { global vc_codes; return $vc_codes }
+proc filter-map/size {code} { upvar #0 vc_code2abbrev($code) abb; return $abb }
+proc filter-default/size {code} { return 1 }
+
+proc filter-values/lockown {} {
+    foreach lv {0 1 2} {
+       foreach ov {0 1 X} {
+           lappend vals "$lv$ov"
+       }
+    }
+    return $vals
+}
+proc filter-map/lockown {lockown} { return [code2abbrev-lock $lockown] }
+proc filter-default/lockown {lockown} {
+    return [regexp {^[01]|^2[^1]} $lockown]
+}
+
+proc filter-validate/xabbre {re} {
+    if {[catch {
+       regexp -- $re {}
+    } emsg]} {
+       regsub {^.*:\s*} $emsg {} emsg
+       regsub {^.*(.{30})$} $emsg {\1} emsg
+       return $emsg
+    }
+    return {}
+}
+
+proc filter-tickbox-flip {fil} {
+    upvar #0 filter_$fil vars
+    set values [filter-values/$fil]
+    foreach val $values {
+       set vars($val) [expr {!$vars($val)}]
+    }
+    refilter-needed
+}
+
+proc make-tickbox-filter {fil label rows inrow} {
+    upvar #0 filter_$fil vars
+    set fw [make-filter tickbox $fil $label frame]
+    set values [filter-values/$fil]
+    set nvalues [llength $values]
+    if {!$inrow} {
+       set inrow [expr {($nvalues + $rows) / $rows}]
+    }
+
+    for {set ix 0} {$ix < $nvalues} {incr ix} {
+       set val [lindex $values $ix]
+       set vars($val) [filter-default/$fil $val]
+       checkbutton $fw.$ix -variable filter_${fil}($val) \
+           -text [filter-map/$fil $val] -font fixed \
+           -command refilter-needed
+       grid configure $fw.$ix -sticky sw \
+           -row [expr {$ix / $inrow}] \
+           -column [expr {$ix % $inrow}]
+    }
+    button $fw.invert -text flip -command [list filter-tickbox-flip $fil] \
+       -padx 0 -pady 0
+    grid configure $fw.invert -sticky se \
+       -row [expr {$rows-1}] \
+       -column [expr {$inrow-1}]
+}
+
+proc entry-filter-changed {fw fil n1 n2 op} {
+    global errorInfo
+    upvar #0 filter_$fil realvar
+    upvar #0 filterentered_$fil entryvar
+    global def_background
+    debug "entry-filter-changed $fw $fil $entryvar"
+    if {[catch {
+       set error [filter-validate/$fil $entryvar]
+       if {[string length $error]} {
+           $fw.error configure -text $error -foreground white -background red
+       } else {
+           $fw.error configure -text { } -background $def_background
+           set realvar $entryvar
+           refilter-needed
+       }
+    } emsg]} {
+       puts stderr "FILTER CHECK ERROR $emsg $errorInfo"
+    }
+}
+
+proc make-entry-filter {fil label def} {
+    global filterentered_$fil
+    upvar #0 filter_$fil realvar
+    set realvar $def
+    set fw [make-filter entry $fil $label frame]
+    entry $fw.entry -textvariable filterentered_$fil
+    label $fw.error
+    glset def_background [$fw.error cget -background]
+    trace add variable filterentered_$fil write \
+       [list entry-filter-changed $fw $fil]
+    pack $fw.entry $fw.error -side top -anchor w
+}
+
+proc refilter-needed {} {
+    debug "REFILTER NEEDED"
+}
+
+proc make-filter {kind fil label ekind} {
+    global filters
+    label .filter.lab_$fil -text $label -justify left
+    $ekind .filter.$fil
+    lappend filters [list $kind $fil]
+    set nfilters [llength $filters]
+    grid configure .filter.lab_$fil -row $nfilters -column 0 -sticky nw -pady 4
+    grid configure .filter.$fil -row $nfilters -column 1 -sticky w -pady 3
+    return .filter.$fil
+}
+
+proc make-filters {} {
+    make-tickbox-filter size Size 2 0
+    make-tickbox-filter lockown "Lock/\nowner" 2 6
+    make-entry-filter xabbre "Flags\n regexp" {}
+}
     
 #---------- loading and parsing the clipboard (vessel locations) ----------
 
@@ -693,6 +835,14 @@ proc widgets-setup {} {
     pack $canvas -expand 1 -fill both
     pack .f -expand 1 -fill both -side left
 
+    #----- filter -----
+
+    frame .filter -relief groove -bd 2
+    pack .filter -side bottom
+
+    label .filter.title -text Filter
+    grid configure .filter.title -row 0 -column 0 -columnspan 2
+
     #----- control panel -----
 
     frame .ctrl
@@ -839,6 +989,7 @@ argdefaults
 httpclientsetup where-vessels
 load-chart
 widgets-setup
+make-filters
 
 set notes_data {}
 if {[catch { parse-clipboard } emsg]} {