File: area-display.tcl

package info (click to toggle)
tklib 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,156 kB
  • sloc: tcl: 105,088; sh: 2,573; ansic: 792; pascal: 359; makefile: 69; sed: 53; exp: 21
file content (229 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
## -*- tcl -*-
# # ## ### ##### ######## ############# ######################
## (c) 2022 Andreas Kupries

# @@ Meta Begin
# Package map::area::display 0.1
# Meta author      {Andreas Kupries}
# Meta location    https://core.tcl.tk/tklib
# Meta platform    tcl
# Meta summary	   Widget to display a single area definition
# Meta description Widget to display the information of a single area definition
# Meta subject     {area display, tabular}
# Meta subject     {tabular, area display}
# Meta require     {Tcl 8.6-}
# Meta require     {Tk  8.6-}
# Meta require     debug
# Meta require     debug::caller
# Meta require     {map::slippy 0.8}
# Meta require     scrollutil
# Meta require     snit
# Meta require     tablelist
# @@ Meta End

## TODO / focus - active vertex / row map ...

package provide map::area::display 0.1

# # ## ### ##### ######## ############# ######################
## API
#
##  <class> OBJ
#
##  <obj> set AREA	-> VOID		Show this area, or nothing, if empty
#
##  -on-selection	Report changes to the vertext selection
#
# # ## ### ##### ######## ############# ######################
## Requirements

package require Tcl 8.6-
package require Tk  8.6-
#                                       ;# Tcllib
package require debug                   ;# - Narrative Tracing
package require debug::caller           ;#
package require map::slippy 0.8         ;# - Map utilities
package require snit                    ;# - OO system
#                                       ;# Tklib
package require scrollutil              ;# - Scroll framework
package require tablelist               ;# - Tabular display

# # ## ### ##### ######## ############# ######################
## Ensemble setup.

namespace eval map       { namespace export area    ; namespace ensemble create }
namespace eval map::area { namespace export display ; namespace ensemble create }

debug level  tklib/map/area/display
debug prefix tklib/map/area/display {<[pid]> [debug caller] | }

# # ## ### ##### ######## ############# ######################

snit::widget ::map::area::display {
    # . . .. ... ..... ........ ............. .....................
    # User configuration

    option -on-selection -default {}

    # . . .. ... ..... ........ ............. .....................
    ## State

    variable myspec {}	 ;# Table data derived from the area specification
    variable myparts     ;# Area statistics: Number of parts
    variable myperimeter ;# Area statistics: Perimeter
    variable mydiameter  ;# Area statistics: Diameter
    variable myclat      ;# Area statistics: Center Latitude
    variable myclon      ;# Area statistics: Center Longitude

    # . . .. ... ..... ........ ............. .....................
    ## Lifecycle

    constructor {args} {
	debug.tklib/map/area/display {}

	$self configurelist $args

	label $win.lcenter   -text Center
	label $win.clat      -textvariable  [myvar myclat]
	label $win.clon      -textvariable  [myvar myclon]
	label $win.lparts    -text Parts
	label $win.parts     -textvariable  [myvar myparts]
	label $win.llength   -text Perimeter
	label $win.length    -textvariable  [myvar myperimeter]
	label $win.ldiameter -text Diameter
	label $win.diameter  -textvariable  [myvar mydiameter]

	scrollutil::scrollarea $win.sa
	tablelist::tablelist   $win.sa.table -width 60 \
	    -columntitles {\# Latitude Longitude Distance Total}
	$win.sa setwidget      $win.sa.table

	pack $win.sa        -in $win -side bottom -fill both -expand 1

	pack $win.lcenter   -in $win -side left
	pack $win.clat      -in $win -side left
	pack $win.clon      -in $win -side left
	pack $win.lparts    -in $win -side left
	pack $win.parts     -in $win -side left
	pack $win.llength   -in $win -side left
	pack $win.length    -in $win -side left
	pack $win.ldiameter -in $win -side left
	pack $win.diameter  -in $win -side left

	$win.sa.table configure -listvariable [myvar myspec]

	bind $win.sa.table <<TablelistSelect>> [mymethod SelectionChanged]
	return
    }

    destructor {
	debug.tklib/map/area/display {}
	return
    }

    # . . .. ... ..... ........ ............. .....................
    ## API

    method focus {index} {
	debug.tklib/map/area/display {}

	$win.sa.table selection clear 0 end
	$win.sa.table selection set $index
	$win.sa.table see           $index
	return
    }

    method set {geos} {
	debug.tklib/map/area/display {}

	if {![llength $geos]} {
	    set myspec     {}
	    set mydiameter n/a
	    set myperimeter   n/a
	    set myparts    n/a
	    set myclat     n/a
	    set myclon     n/a
	    return
	}

	set parts    [llength $geos] ; if {$parts < 3} { incr parts -1 }
	set diameter [map slippy geo diameter-list $geos]
	set center   [map slippy geo center-list   $geos]
	lassign [map slippy geo limit $center] clat clon

	# Assemble table data

	set last {}
	set total 0
	set rows [lmap g $geos {
	    set dd {}
	    set dt {}
	    if {$last ne {}} {
		set d     [map slippy geo distance $last $g]
		set total [expr {$total + $d}]
		# Format for display
		set dd    [map slipp pretty-distance $d]
		set dt    [map slipp pretty-distance $total]
	    }

	    lassign [map slippy geo limit $g] lat lon
	    set last $g

	    set data {}
	    lappend data [incr rowid]
	    lappend data $lat
	    lappend data $lon
	    lappend data $dd
	    lappend data $dt
	    set data
	}]

	# A last line to close the perimeter
	set d [map slippy geo distance $last [lindex $geos 0]]
	set total [expr {$total + $d}]
	# Format for display
	set dd    [map slipp pretty-distance $d]
	set dt    [map slipp pretty-distance $total]

	lappend rows [list 1 {} {} $dd $dt]

	# ... and commit
	set myparts     $parts
	set myperimeter $dt
	set mydiameter  [map slippy pretty-distance $diameter]
	set myspec      $rows
	set myclat      $clat
	set myclon      $clon
	return
    }

    # . . .. ... ..... ........ ............. .....................
    # Internal

    method SelectionChanged {} {
	debug.tklib/map/area/display {}

	after idle [mymethod ReportSelectionChange]
	return
    }

    method ReportSelectionChange {} {
	debug.tklib/map/area/display {}

	if {![llength $options(-on-selection)]} return

	set row [$win.sa.table curselection]
	if {$row eq {}} return

	set row [lindex $myspec $row 0]
	incr row -1

	uplevel #0 [list {*}$options(-on-selection) $row]
	return
    }

    # . . .. ... ..... ........ ............. .....................
}

# # ## ### ##### ######## ############# ######################
return