Scid  4.6.5
stats.tcl
Go to the documentation of this file.
1 ###
2 ### windows/stats.tcl: Filter Statistics window for Scid
3 ###
4 
5 set ::windows::stats::isOpen 0
6 
7 proc ::windows::stats::Open {} {
8  set w .statsWin
9  if {[winfo exists $w]} {
10  focus .
11  destroy $w
12  set ::windows::stats::isOpen 0
13  return
14  }
15  toplevel $w
16  wm title $w "Scid: Filter Statistics"
18  bind $w <Configure> "recordWinSize $w"
19 
20  menu $w.menu
21  $w configure -menu $w.menu
22  $w.menu add cascade -label StatsFile -menu $w.menu.file
23  $w.menu add cascade -label StatsOpt -menu $w.menu.opt
24  menu $w.menu.file
25  $w.menu.file add command -label StatsFilePrint -command {
26  set ftype {
27  { "Text files" {".txt"} }
28  { "All files" {"*"} }
29  }
30  set fname [tk_getSaveFile -initialdir [pwd] -filetypes $ftype -title "Save text file"]
31  if {$fname != ""} {
32  if {[catch {set tempfile [open $fname w]}]} {
33  tk_messageBox -title "Scid: Error saving file" -type ok -icon warning \
34  -message "Unable to save the file: $fname\n\n"
35  } else {
36  puts $tempfile [.statsWin.stats get 1.0 end-1c]
37  close $tempfile
38  }
39  }
40  }
41  $w.menu.file add separator
42  $w.menu.file add command -label StatsFileClose -accelerator Esc \
43  -command "destroy $w"
44 
45  menu $w.menu.opt
46  $w.menu.opt add cascade -label $::tr(OprepStatBoth) -menu $w.menu.opt.elo
47  menu $w.menu.opt.elo
48  foreach i [lsort -decreasing [array names ::windows::stats::display r*]] {
49  set elo [string range $i 1 end]
50  $w.menu.opt.elo add checkbutton -label "$::tr(OprepStatBoth) $elo+" \
51  -variable ::windows::stats::display($i) -command ::windows::stats::Refresh
52  }
53  $w.menu.opt add separator
54  $w.menu.opt add cascade -label $::tr(OprepStatSince) \
55  -menu $w.menu.opt.year
56  menu $w.menu.opt.year
57  foreach i [lsort [array names ::windows::stats::display y*]] {
58  set year [string range $i 1 end]
59  $w.menu.opt.year add checkbutton \
60  -label "$::tr(OprepStatSince) $year.01.01" \
61  -variable ::windows::stats::display($i) -command ::windows::stats::Refresh
62  }
63 
64 # text $w.stats -borderwidth 0 \
65  -width $::winWidth($w) -height $::winHeight($w) -font font_Fixed \
66  -foreground black -background white -cursor top_left_arrow -wrap none \
67  -setgrid 1 -yscrollcommand {.statsWin.sb set }
68  autoscrollframe $w.statsasb text $w.stats -width 80 -height 40\
69  -foreground black -background white -font font_Fixed\
70  -wrap none
71 
72  pack $w.statsasb -side top -fill both -expand yes
73  set ::windows::stats::isOpen 1
74  bind $w <Control-q> "destroy $w"
75  bind $w <Escape> "destroy $w"
76  bind $w <F1> { helpWindow Index }
77  bind $w <Destroy> {
78  set ::windows::stats::isOpen 0
79  }
81 #Klimmek: enable Resize vertical
82  wm resizable $w 0 1
85 }
86 
87 proc ::windows::stats::Refresh {} {
88  variable display
89  if {[winfo exists .playerInfoWin]} { ::pinfo::playerInfo}
92  #Klimmek. Update Absfilter window
94  if {! [winfo exists .statsWin]} { return}
95 
96  # Set up variables for translated phrases:
97  set all [::utils::string::Capital $::tr(allGames)]
98  set both $::tr(OprepStatBoth)
99  set since $::tr(OprepStatSince)
100  set games [::utils::string::Capital $::tr(games)]
101  set score [::utils::string::Capital $::tr(score)]
102 
103  # Find length of longest left-hand column:
104  set alen [expr {[string length $all] + 1}]
105  set blen [expr {[string length $both] + 7}]
106  set slen [expr {[string length $since] + 14}]
107  set len $alen
108  if {$len < $blen} { set len $blen}
109  if {$len < $slen} { set len $slen}
110 
111  set height 4
112  set ratings 0
113  set years 0
114  set rlist [lsort -decreasing [array names display r*]]
115  set ylist [lsort [array names display y*]]
116 
117  foreach i $rlist {
118  if {$display($i)} { set ratings 1}
119  }
120  foreach i $ylist {
121  if {$display($i)} { set years 1}
122  }
123 
124  if {$ratings} { incr height}
125  if {$years} { incr height}
126 
127  set s ""
128  set sm "\n"
129  set stat ""
130  append s " [::utils::string::Pad $stat [expr $len - 4]] [::utils::string::PadRight $games 10]"
131  append s " 1-0 =-= 0-1 [::utils::string::PadRight $score 8]\n"
132  append s "------------------------------------------------------------------------"
133  append s "\n [::utils::string::Pad $all $len]" [sc_filter stats all]
134 #Klimmek: New Statistic: Count the games in intervals "start elo - end elo"
135 # if elo is deselected in option menu, then enlarge the interval to next selected elo.
136 #Klimmek: New Statistic: Count the games in intervals
137  if {$ratings} {
138  append s "\n"
139  set j 0
140  set k [lindex $rlist $j]
141  while { $k!= "" && ! $display($k) } {
142  incr j
143  set k [lindex $ylist $j]
144  if { $k == "" } { break}
145  }
146  set nelo [string range [lindex $rlist $j] 1 end]
147  incr height
148 #first line searches all games greater 2600 Elo
149  set stat "min. Elo $nelo-3500"
150  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo 9999]
151  set stat "max. Elo $nelo-3500"
152  append sm "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo -9999]
153  set j 0
154  foreach i $rlist {
155  incr j
156  if {$display($i)} {
157  incr height
158  set elo [string range $i 1 end]
159  set l $j
160  set k [lindex $rlist $l]
161  while { $k!= "" && ! $display($k) } {
162  incr l
163  set k [lindex $rlist $l]
164  if { $k == "" } { break}
165  }
166  set nelo [string range [lindex $rlist $l] 1 end]
167  if { $nelo == "" } { set nelo 0}
168 #count all games where player with lowest Elo is in the specific range
169  set stat "min. Elo $nelo-$elo"
170  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo $elo]
171  set stat "max. Elo $nelo-$elo"
172 #count all games where player with highest Elo is in the specific range
173 # +10000 workaround to trigger max elo in filter function
174  append sm "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo [expr {$elo + 10000}]]
175  }
176  }
177  }
178  append s $sm
179 #Klimmek: New Statistic: Count the games in intervals "from year - to year"
180 # if year is deselected in option menu, then enlarge the interval to next selected year.
181  if {$years} {
182  append s "\n"
183  set j 0
184  foreach i $ylist {
185  incr j
186  if {$display($i)} {
187  incr height
188  set year [string range $i 1 end]
189  set l $j
190  set k [lindex $ylist $l]
191  while { $k!= "" && ! $display($k) } {
192  incr l
193  set k [lindex $ylist $l]
194  if { $k == "" } { break}
195  }
196  set nyear [string range $k 1 end]
197  if { $nyear == "" } { set nyear 2099}
198  set stat "$year - $nyear"
199  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats year $year $nyear]
200  }
201  }
202  }
203 #Old statistic: count the games from specific value to maximum value
204  set stat ""
205  append s "\n\n"
206  append s " [::utils::string::Pad $stat [expr $len - 4]] [::utils::string::PadRight $games 10]"
207  append s " 1-0 =-= 0-1 [::utils::string::PadRight $score 8]\n"
208  append s "------------------------------------------------------------------------"
209  append s "\n [::utils::string::Pad $all $len]" [sc_filter stats all]
210 
211  if {$ratings} {
212  append s "\n"
213  foreach i $rlist {
214  if {$display($i)} {
215  incr height
216  set elo [string range $i 1 end]
217  set stat "$both $elo+"
218  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $elo 0]
219  }
220  }
221  }
222 
223  if {$years} {
224  append s "\n"
225  foreach i $ylist {
226  if {$display($i)} {
227  incr height
228  set year [string range $i 1 end]
229  set stat "$since $year.01.01"
230  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats year $year 0]
231  }
232  }
233  }
234  append s "\n"
235 
236  set w .statsWin.stats
237  if { $height > 50 } { set height 50}
238  $w configure -state normal
239  $w delete 1.0 end
240  $w insert end $s
241  $w tag configure bgGray -background gray95
242  # Shade every second line to help readability:
243  set lastLineNum [expr {int([$w index end])}]
244  for {set i 2} {$i <= $lastLineNum} {incr i 2} {
245  $w tag add bgGray $i.0 "$i.0 lineend +1c"
246  }
247  $w tag configure blue -foreground darkBlue
248  $w tag configure red -foreground red
249  $w tag add blue 1.0 2.0
250  $w tag add red 2.0 3.0
251  $w configure -height $height
252  $w configure -state disabled
253 }
254 
255 proc ::windows::stats::ConfigMenus {{lang ""}} {
256  if {! [winfo exists .statsWin]} { return}
257  if {$lang == ""} { set lang $::language}
258  set m .statsWin.menu
259  foreach idx {0 1} tag {File Opt} {
260  configMenuText $m $idx Stats$tag $lang
261  }
262  foreach idx {0 2} tag {Print Close} {
263  configMenuText $m.file $idx StatsFile$tag $lang
264  }
265 }