-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableview.go
1025 lines (937 loc) · 28.7 KB
/
tableview.go
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
"fmt"
"image"
"log"
"reflect"
"strings"
"github.com/chewxy/math32"
"github.com/goki/gi/gi"
"github.com/goki/gi/girl"
"github.com/goki/gi/gist"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/cursor"
"github.com/goki/gi/units"
"github.com/goki/ki/ints"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// todo:
// * search option, both as a search field and as simple type-to-search
// * popup menu option -- when user does right-mouse on item, a provided func is called
// -- use in fileview
// * could have a native context menu for add / delete etc.
// TableView represents a slice-of-structs as a table, where the fields are
// the columns, within an overall frame. It has two modes, determined by
// Inactive flag: if Inactive, it functions as a mutually-exclusive item
// selector, highlighting the selected row and emitting a WidgetSig
// WidgetSelected signal, and TableViewDoubleClick for double clicks (can be
// used for closing dialogs). If !Inactive, it is a full-featured editor with
// multiple-selection, cut-and-paste, and drag-and-drop, reporting each action
// taken using the TableViewSig signals
// Automatically has a toolbar with Slice ToolBar props if defined
// set prop toolbar = false to turn off
type TableView struct {
SliceViewBase
StyleFunc TableViewStyleFunc `copy:"-" view:"-" json:"-" xml:"-" desc:"optional styling function"`
SelField string `copy:"-" view:"-" json:"-" xml:"-" desc:"current selection field -- initially select value in this field"`
SortIdx int `desc:"current sort index"`
SortDesc bool `desc:"whether current sort order is descending"`
StruType reflect.Type `copy:"-" view:"-" json:"-" xml:"-" desc:"struct type for each row"`
VisFields []reflect.StructField `copy:"-" view:"-" json:"-" xml:"-" desc:"the visible fields"`
NVisFields int `copy:"-" view:"-" json:"-" xml:"-" desc:"number of visible fields"`
}
var KiT_TableView = kit.Types.AddType(&TableView{}, TableViewProps)
// AddNewTableView adds a new tableview to given parent node, with given name.
func AddNewTableView(parent ki.Ki, name string) *TableView {
return parent.AddNewChild(KiT_TableView, name).(*TableView)
}
// check for interface impl
var _ SliceViewer = (*TableView)(nil)
// TableViewStyleFunc is a styling function for custom styling /
// configuration of elements in the view. If style properties are set
// then you must call widg.AsNode2dD().SetFullReRender() to trigger
// re-styling during re-render
type TableViewStyleFunc func(tv *TableView, slice interface{}, widg gi.Node2D, row, col int, vv ValueView)
// SetSlice sets the source slice that we are viewing -- rebuilds the children
// to represent this slice (does Update if already viewing).
func (tv *TableView) SetSlice(sl interface{}) {
if kit.IfaceIsNil(sl) {
tv.Slice = nil
return
}
if tv.Slice == sl && tv.IsConfiged() {
tv.Update()
return
}
if !tv.IsInactive() {
tv.SelectedIdx = -1
}
tv.StartIdx = 0
tv.SortIdx = -1
tv.SortDesc = false
slpTyp := reflect.TypeOf(sl)
if slpTyp.Kind() != reflect.Ptr {
log.Printf("TableView requires that you pass a pointer to a slice of struct elements -- type is not a Ptr: %v\n", slpTyp.String())
return
}
if slpTyp.Elem().Kind() != reflect.Slice {
log.Printf("TableView requires that you pass a pointer to a slice of struct elements -- ptr doesn't point to a slice: %v\n", slpTyp.Elem().String())
return
}
tv.Slice = sl
tv.SliceNPVal = kit.NonPtrValue(reflect.ValueOf(tv.Slice))
struTyp := tv.StructType()
if struTyp.Kind() != reflect.Struct {
log.Printf("TableView requires that you pass a slice of struct elements -- type is not a Struct: %v\n", struTyp.String())
return
}
updt := tv.UpdateStart()
tv.ResetSelectedIdxs()
tv.SelectMode = false
tv.SetFullReRender()
tv.ShowIndex = true
if sidxp, err := tv.PropTry("index"); err == nil {
tv.ShowIndex, _ = kit.ToBool(sidxp)
}
tv.InactKeyNav = true
if siknp, err := tv.PropTry("inact-key-nav"); err == nil {
tv.InactKeyNav, _ = kit.ToBool(siknp)
}
tv.Config()
tv.UpdateEnd(updt)
}
var TableViewProps = ki.Props{
"EnumType:Flag": gi.KiT_NodeFlags,
"background-color": &gi.Prefs.Colors.Background,
"color": &gi.Prefs.Colors.Font,
"max-width": -1,
"max-height": -1,
}
// StructType sets the StruType and returns the type of the struct within the
// slice -- this is a non-ptr type even if slice has pointers to structs
func (tv *TableView) StructType() reflect.Type {
tv.StruType = kit.NonPtrType(kit.SliceElType(tv.Slice))
return tv.StruType
}
// CacheVisFields computes the number of visible fields in nVisFields and
// caches those to skip in fieldSkip
func (tv *TableView) CacheVisFields() {
styp := tv.StructType()
tv.VisFields = make([]reflect.StructField, 0, 20)
kit.FlatFieldsTypeFunc(styp, func(typ reflect.Type, fld reflect.StructField) bool {
tvtag := fld.Tag.Get("tableview")
add := true
if tvtag != "" {
if tvtag == "-" {
add = false
} else if tvtag == "-select" && tv.IsInactive() {
add = false
} else if tvtag == "-edit" && !tv.IsInactive() {
add = false
}
}
vtag := fld.Tag.Get("view")
if vtag == "-" {
add = false
}
if add {
if typ != styp {
rfld, has := styp.FieldByName(fld.Name)
if has {
tv.VisFields = append(tv.VisFields, rfld)
} else {
fmt.Printf("TableView: Field name: %v is ambiguous from base struct type: %v, cannot be used in view!\n", fld.Name, styp.String())
}
} else {
tv.VisFields = append(tv.VisFields, fld)
}
}
return true
})
tv.NVisFields = len(tv.VisFields)
}
// IsConfiged returns true if the widget is fully configured
func (tv *TableView) IsConfiged() bool {
if len(tv.Kids) == 0 {
return false
}
sf := tv.SliceFrame()
if len(sf.Kids) == 0 {
return false
}
return true
}
// Config configures the view
func (tv *TableView) Config() {
tv.Lay = gi.LayoutVert
tv.SetProp("spacing", gi.StdDialogVSpaceUnits)
config := kit.TypeAndNameList{}
config.Add(gi.KiT_ToolBar, "toolbar")
config.Add(gi.KiT_Frame, "frame")
mods, updt := tv.ConfigChildren(config, ki.UniqueNames)
tv.ConfigSliceGrid()
tv.ConfigToolbar()
if mods {
tv.SetFullReRender()
tv.UpdateEnd(updt)
}
}
// SliceFrame returns the outer frame widget, which contains all the header,
// fields and values
func (tv *TableView) SliceFrame() *gi.Frame {
return tv.ChildByName("frame", 0).(*gi.Frame)
}
// GridLayout returns the SliceGrid grid-layout widget, with grid and scrollbar
func (tv *TableView) GridLayout() *gi.Layout {
if !tv.IsConfiged() {
return nil
}
return tv.SliceFrame().ChildByName("grid-lay", 0).(*gi.Layout)
}
// SliceGrid returns the SliceGrid grid frame widget, which contains all the
// fields and values, within SliceFrame
func (tv *TableView) SliceGrid() *gi.Frame {
if !tv.IsConfiged() {
return nil
}
return tv.GridLayout().ChildByName("grid", 0).(*gi.Frame)
}
// ScrollBar returns the SliceGrid scrollbar
func (tv *TableView) ScrollBar() *gi.ScrollBar {
return tv.GridLayout().ChildByName("scrollbar", 1).(*gi.ScrollBar)
}
// SliceHeader returns the Toolbar header for slice grid
func (tv *TableView) SliceHeader() *gi.ToolBar {
return tv.SliceFrame().Child(0).(*gi.ToolBar)
}
// ToolBar returns the toolbar widget
func (tv *TableView) ToolBar() *gi.ToolBar {
return tv.ChildByName("toolbar", 0).(*gi.ToolBar)
}
// RowWidgetNs returns number of widgets per row and offset for index label
func (tv *TableView) RowWidgetNs() (nWidgPerRow, idxOff int) {
nWidgPerRow = 1 + tv.NVisFields
if !tv.IsInactive() {
if !tv.NoAdd {
nWidgPerRow += 1
}
if !tv.NoDelete {
nWidgPerRow += 1
}
}
idxOff = 1
if !tv.ShowIndex {
nWidgPerRow -= 1
idxOff = 0
}
return
}
// ConfigSliceGrid configures the SliceGrid for the current slice
// this is only called by global Config and updates are guarded by that
func (tv *TableView) ConfigSliceGrid() {
if kit.IfaceIsNil(tv.Slice) {
return
}
tv.CacheVisFields()
sz := tv.This().(SliceViewer).UpdtSliceSize()
if sz == 0 {
return
}
nWidgPerRow, idxOff := tv.RowWidgetNs()
sg := tv.SliceFrame()
updt := sg.UpdateStart()
defer sg.UpdateEnd(updt)
sg.Lay = gi.LayoutVert
sg.SetMinPrefWidth(units.NewCh(20))
sg.SetProp("overflow", gist.OverflowScroll) // this still gives it true size during PrefSize
sg.SetStretchMax() // for this to work, ALL layers above need it too
sg.SetProp("border-width", 0)
sg.SetProp("margin", 0)
sg.SetProp("padding", 0)
sgcfg := kit.TypeAndNameList{}
sgcfg.Add(gi.KiT_ToolBar, "header")
sgcfg.Add(gi.KiT_Layout, "grid-lay")
sg.ConfigChildren(sgcfg, ki.UniqueNames)
sgh := tv.SliceHeader()
sgh.Lay = gi.LayoutHoriz
sgh.SetProp("overflow", gist.OverflowHidden) // no scrollbars!
sgh.SetProp("spacing", 0)
// sgh.SetStretchMaxWidth()
gl := tv.GridLayout()
gl.Lay = gi.LayoutHoriz
gl.SetStretchMax() // for this to work, ALL layers above need it too
gconfig := kit.TypeAndNameList{}
gconfig.Add(gi.KiT_Frame, "grid")
gconfig.Add(gi.KiT_ScrollBar, "scrollbar")
gl.ConfigChildren(gconfig, ki.UniqueNames) // covered by above
sgf := tv.SliceGrid()
sgf.Lay = gi.LayoutGrid
sgf.Stripes = gi.RowStripes
sgf.SetMinPrefHeight(units.NewEm(6))
sgf.SetStretchMax() // for this to work, ALL layers above need it too
sgf.SetProp("columns", nWidgPerRow)
sgf.SetProp("overflow", gist.OverflowScroll) // this still gives it true size during PrefSize
// this causes everything to get off, especially resizing: not taking it into account presumably:
// sgf.SetProp("spacing", gi.StdDialogVSpaceUnits)
// Configure Header
hcfg := kit.TypeAndNameList{}
if tv.ShowIndex {
hcfg.Add(gi.KiT_Label, "head-idx")
}
for fli := 0; fli < tv.NVisFields; fli++ {
fld := tv.VisFields[fli]
labnm := fmt.Sprintf("head-%v", fld.Name)
hcfg.Add(gi.KiT_Action, labnm)
}
if !tv.IsInactive() {
hcfg.Add(gi.KiT_Label, "head-add")
hcfg.Add(gi.KiT_Label, "head-del")
}
sgh.ConfigChildren(hcfg, ki.NonUniqueNames) // headers SHOULD be unique, but with labels..
// at this point, we make one dummy row to get size of widgets
sgf.DeleteChildren(ki.DestroyKids)
sgf.Kids = make(ki.Slice, nWidgPerRow)
itxt := fmt.Sprintf("%05d", 0)
labnm := fmt.Sprintf("index-%v", itxt)
if tv.ShowIndex {
lbl := sgh.Child(0).(*gi.Label)
lbl.Text = "Index"
idxlab := &gi.Label{}
sgf.SetChild(idxlab, 0, labnm)
idxlab.Text = itxt
}
for fli := 0; fli < tv.NVisFields; fli++ {
field := tv.VisFields[fli]
hdr := sgh.Child(idxOff + fli).(*gi.Action)
hdr.SetText(field.Name)
if fli == tv.SortIdx {
if tv.SortDesc {
hdr.SetIcon("wedge-down")
} else {
hdr.SetIcon("wedge-up")
}
}
hdr.Data = fli
hdr.Tooltip = field.Name + " (click to sort by)"
dsc := field.Tag.Get("desc")
if dsc != "" {
hdr.Tooltip += ": " + dsc
}
hdr.ActionSig.ConnectOnly(tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
tvv := recv.Embed(KiT_TableView).(*TableView)
act := send.(*gi.Action)
fldIdx := act.Data.(int)
tvv.SortSliceAction(fldIdx)
})
val := kit.OnePtrUnderlyingValue(tv.SliceNPVal.Index(0)) // deal with pointer lists
stru := val.Interface()
fval := val.Elem().FieldByIndex(field.Index)
vv := ToValueView(fval.Interface(), "")
if vv == nil { // shouldn't happen
continue
}
vv.SetStructValue(fval.Addr(), stru, &field, tv.TmpSave, tv.ViewPath)
vtyp := vv.WidgetType()
valnm := fmt.Sprintf("value-%v.%v", fli, itxt)
cidx := idxOff + fli
widg := ki.NewOfType(vtyp).(gi.Node2D)
sgf.SetChild(widg, cidx, valnm)
vv.ConfigWidget(widg)
}
if !tv.IsInactive() {
cidx := tv.NVisFields + idxOff
if !tv.NoAdd {
lbl := sgh.Child(cidx).(*gi.Label)
lbl.Text = "+"
lbl.Tooltip = "insert row"
addnm := fmt.Sprintf("add-%v", itxt)
addact := gi.Action{}
sgf.SetChild(&addact, cidx, addnm)
addact.SetIcon("plus")
cidx++
}
if !tv.NoDelete {
lbl := sgh.Child(cidx).(*gi.Label)
lbl.Text = "-"
lbl.Tooltip = "delete row"
delnm := fmt.Sprintf("del-%v", itxt)
delact := gi.Action{}
sgf.SetChild(&delact, cidx, delnm)
delact.SetIcon("minus")
cidx++
}
}
if tv.SortIdx >= 0 {
tv.SortSlice()
}
tv.ConfigScroll()
}
// LayoutSliceGrid does the proper layout of slice grid depending on allocated size
// returns true if UpdateSliceGrid should be called after this
func (tv *TableView) LayoutSliceGrid() bool {
sg := tv.SliceGrid()
if kit.IfaceIsNil(tv.Slice) {
if sg != nil {
sg.DeleteChildren(ki.DestroyKids)
}
return false
}
if sg == nil {
return false
}
sz := tv.This().(SliceViewer).UpdtSliceSize()
if sz == 0 {
sg.DeleteChildren(ki.DestroyKids)
return false
}
nWidgPerRow, _ := tv.RowWidgetNs()
if len(sg.GridData) > 0 && len(sg.GridData[gi.Row]) > 0 {
tv.RowHeight = sg.GridData[gi.Row][0].AllocSize + sg.Spacing.Dots
}
if tv.Sty.Font.Face == nil {
girl.OpenFont(&tv.Sty.Font, &tv.Sty.UnContext)
}
tv.RowHeight = math32.Max(tv.RowHeight, tv.Sty.Font.Face.Metrics.Height)
mvp := tv.ViewportSafe()
if mvp != nil && mvp.HasFlag(int(gi.VpFlagPrefSizing)) {
tv.VisRows = ints.MinInt(gi.LayoutPrefMaxRows, tv.SliceSize)
tv.LayoutHeight = float32(tv.VisRows) * tv.RowHeight
} else {
sgHt := tv.AvailHeight()
tv.LayoutHeight = sgHt
if sgHt == 0 {
return false
}
tv.VisRows = int(math32.Floor(sgHt / tv.RowHeight))
}
tv.DispRows = ints.MinInt(tv.SliceSize, tv.VisRows)
nWidg := nWidgPerRow * tv.DispRows
updt := sg.UpdateStart()
defer sg.UpdateEnd(updt)
if tv.Values == nil || sg.NumChildren() != nWidg {
sg.DeleteChildren(ki.DestroyKids)
tv.Values = make([]ValueView, tv.NVisFields*tv.DispRows)
sg.Kids = make(ki.Slice, nWidg)
}
tv.ConfigScroll()
tv.LayoutHeader()
return true
}
// LayoutHeader updates the header layout based on field widths
func (tv *TableView) LayoutHeader() {
_, idxOff := tv.RowWidgetNs()
nfld := tv.NVisFields + idxOff
sgh := tv.SliceHeader()
sgf := tv.SliceGrid()
spc := sgh.Spacing.Dots
gd := sgf.GridData[gi.Col]
if gd == nil {
return
}
sumwd := float32(0)
for fli := 0; fli < nfld; fli++ {
lbl := sgh.Child(fli).(gi.Node2D).AsWidget()
wd := gd[fli].AllocSize - spc
if fli == 0 {
wd += spc
}
lbl.SetMinPrefWidth(units.NewValue(wd, units.Dot))
lbl.SetProp("max-width", units.NewValue(wd, units.Dot))
sumwd += wd
}
if !tv.IsInactive() {
mx := len(sgf.GridData[gi.Col])
for fli := nfld; fli < mx; fli++ {
lbl := sgh.Child(fli).(gi.Node2D).AsWidget()
wd := gd[fli].AllocSize - spc
lbl.SetMinPrefWidth(units.NewValue(wd, units.Dot))
lbl.SetProp("max-width", units.NewValue(wd, units.Dot))
sumwd += wd
}
}
sgh.SetMinPrefWidth(units.NewValue(sumwd+spc, units.Dot))
}
// UpdateSliceGrid updates grid display -- robust to any time calling
func (tv *TableView) UpdateSliceGrid() {
if kit.IfaceIsNil(tv.Slice) {
return
}
sz := tv.This().(SliceViewer).UpdtSliceSize()
if sz == 0 {
return
}
sg := tv.SliceGrid()
tv.DispRows = ints.MinInt(tv.SliceSize, tv.VisRows)
nWidgPerRow, idxOff := tv.RowWidgetNs()
nWidg := nWidgPerRow * tv.DispRows
wupdt := tv.TopUpdateStart()
defer tv.TopUpdateEnd(wupdt)
updt := sg.UpdateStart()
defer sg.UpdateEnd(updt)
if tv.Values == nil || sg.NumChildren() != nWidg { // shouldn't happen..
tv.LayoutSliceGrid()
nWidg = nWidgPerRow * tv.DispRows
}
if sg.NumChildren() != nWidg || sg.NumChildren() == 0 {
return
}
if sz > tv.DispRows {
sb := tv.ScrollBar()
tv.StartIdx = int(sb.Value)
lastSt := sz - tv.DispRows
tv.StartIdx = ints.MinInt(lastSt, tv.StartIdx)
tv.StartIdx = ints.MaxInt(0, tv.StartIdx)
} else {
tv.StartIdx = 0
}
for i := 0; i < tv.DispRows; i++ {
ridx := i * nWidgPerRow
si := tv.StartIdx + i // slice idx
issel := tv.IdxIsSelected(si)
val := kit.OnePtrUnderlyingValue(tv.SliceNPVal.Index(si)) // deal with pointer lists
stru := val.Interface()
itxt := fmt.Sprintf("%05d", i)
sitxt := fmt.Sprintf("%05d", si)
labnm := fmt.Sprintf("index-%v", itxt)
if tv.ShowIndex {
var idxlab *gi.Label
if sg.Kids[ridx] != nil {
idxlab = sg.Kids[ridx].(*gi.Label)
} else {
idxlab = &gi.Label{}
sg.SetChild(idxlab, ridx, labnm)
idxlab.SetProp("tv-row", i)
idxlab.Selectable = true
idxlab.Redrawable = true
idxlab.Sty.Template = "giv.TableView.IndexLabel"
idxlab.WidgetSig.ConnectOnly(tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.WidgetSelected) {
wbb := send.(gi.Node2D).AsWidget()
row := wbb.Prop("tv-row").(int)
tvv := recv.Embed(KiT_TableView).(*TableView)
tvv.UpdateSelectRow(row, wbb.IsSelected())
}
})
}
idxlab.CurBgColor = gi.Prefs.Colors.Background
idxlab.SetSelectedState(issel)
idxlab.SetText(sitxt)
}
vpath := tv.ViewPath + "[" + sitxt + "]"
if lblr, ok := tv.Slice.(gi.SliceLabeler); ok {
slbl := lblr.ElemLabel(si)
if slbl != "" {
vpath = tv.ViewPath + "[" + slbl + "]"
}
}
for fli := 0; fli < tv.NVisFields; fli++ {
field := tv.VisFields[fli]
fval := val.Elem().FieldByIndex(field.Index)
vvi := i*tv.NVisFields + fli
var vv ValueView
if tv.Values[vvi] == nil {
tags := ""
if fval.Kind() == reflect.Slice || fval.Kind() == reflect.Map {
tags = `view:"no-inline"`
}
vv = ToValueView(fval.Interface(), tags)
tv.Values[vvi] = vv
} else {
vv = tv.Values[vvi]
}
if vv == nil {
fmt.Printf("field: %v %v has nil valueview: %v -- should not happen -- fix ToValueView\n", fli, field.Name, fval.String())
continue
}
vv.SetStructValue(fval.Addr(), stru, &field, tv.TmpSave, vpath)
vtyp := vv.WidgetType()
valnm := fmt.Sprintf("value-%v.%v", fli, itxt)
cidx := ridx + idxOff + fli
var widg gi.Node2D
if sg.Kids[cidx] != nil {
widg = sg.Kids[cidx].(gi.Node2D)
vv.UpdateWidget()
if tv.IsInactive() {
widg.AsNode2D().SetInactive()
}
widg.AsNode2D().SetSelectedState(issel)
} else {
widg = ki.NewOfType(vtyp).(gi.Node2D)
sg.SetChild(widg, cidx, valnm)
vv.ConfigWidget(widg)
wb := widg.AsWidget()
if wb != nil {
// totally not worth it now:
// wb.Sty.Template = "giv.TableViewView.ItemWidget." + vtyp.Name()
wb.SetProp("tv-row", i)
wb.ClearSelected()
wb.WidgetSig.ConnectOnly(tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.WidgetSelected) { // || sig == int64(gi.WidgetFocused) {
wbb := send.(gi.Node2D).AsWidget()
row := wbb.Prop("tv-row").(int)
tvv := recv.Embed(KiT_TableView).(*TableView)
// if sig != int64(gi.WidgetFocused) || !tvv.InFocusGrab {
tvv.UpdateSelectRow(row, wbb.IsSelected())
// }
}
})
}
if tv.IsInactive() {
widg.AsNode2D().SetInactive()
} else {
vvb := vv.AsValueViewBase()
vvb.ViewSig.ConnectOnly(tv.This(), // todo: do we need this?
func(recv, send ki.Ki, sig int64, data interface{}) {
tvv, _ := recv.Embed(KiT_TableView).(*TableView)
tvv.SetChanged()
})
}
}
tv.This().(SliceViewer).StyleRow(tv.SliceNPVal, widg, si, fli, vv)
}
if !tv.IsInactive() {
cidx := ridx + tv.NVisFields + idxOff
if !tv.NoAdd {
if sg.Kids[cidx] == nil {
addnm := fmt.Sprintf("add-%v", itxt)
addact := gi.Action{}
sg.SetChild(&addact, cidx, addnm)
addact.SetIcon("plus")
addact.Tooltip = "insert a new element at this index"
addact.Data = i
addact.Sty.Template = "giv.TableView.AddAction"
addact.ActionSig.ConnectOnly(tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
act := send.(*gi.Action)
tvv := recv.Embed(KiT_TableView).(*TableView)
tvv.SliceNewAtRow(act.Data.(int) + 1)
})
}
cidx++
}
if !tv.NoDelete {
if sg.Kids[cidx] == nil {
delnm := fmt.Sprintf("del-%v", itxt)
delact := gi.Action{}
sg.SetChild(&delact, cidx, delnm)
delact.SetIcon("minus")
delact.Tooltip = "delete this element"
delact.Data = i
delact.Sty.Template = "giv.TableView.DelAction"
delact.ActionSig.ConnectOnly(tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
act := send.(*gi.Action)
tvv := recv.Embed(KiT_TableView).(*TableView)
tvv.SliceDeleteAtRow(act.Data.(int), true)
})
}
cidx++
}
}
}
if tv.SelField != "" && tv.SelVal != nil {
tv.SelectedIdx, _ = StructSliceIdxByValue(tv.Slice, tv.SelField, tv.SelVal)
}
if tv.IsInactive() && tv.SelectedIdx >= 0 {
tv.SelectIdx(tv.SelectedIdx)
}
tv.UpdateScroll()
}
func (tv *TableView) StyleRow(svnp reflect.Value, widg gi.Node2D, idx, fidx int, vv ValueView) {
if tv.StyleFunc != nil {
tv.StyleFunc(tv, svnp.Interface(), widg, idx, fidx, vv)
}
}
// SliceNewAt inserts a new blank element at given index in the slice -- -1
// means the end
func (tv *TableView) SliceNewAt(idx int) {
wupdt := tv.TopUpdateStart()
defer tv.TopUpdateEnd(wupdt)
updt := tv.UpdateStart()
defer tv.UpdateEnd(updt)
kit.SliceNewAt(tv.Slice, idx)
if idx < 0 {
idx = tv.SliceSize
}
if tv.TmpSave != nil {
tv.TmpSave.SaveTmp()
}
tv.SetChanged()
tv.ScrollBar().SetFullReRender()
tv.This().(SliceViewer).LayoutSliceGrid()
tv.This().(SliceViewer).UpdateSliceGrid()
tv.ViewSig.Emit(tv.This(), 0, nil)
tv.SliceViewSig.Emit(tv.This(), int64(SliceViewInserted), idx)
}
// SliceDeleteAt deletes element at given index from slice -- doupdt means
// call UpdateSliceGrid to update display
func (tv *TableView) SliceDeleteAt(idx int, doupdt bool) {
if idx < 0 {
return
}
wupdt := tv.TopUpdateStart()
defer tv.TopUpdateEnd(wupdt)
updt := tv.UpdateStart()
defer tv.UpdateEnd(updt)
kit.SliceDeleteAt(tv.Slice, idx)
if tv.TmpSave != nil {
tv.TmpSave.SaveTmp()
}
tv.SetChanged()
if doupdt {
tv.ScrollBar().SetFullReRender()
tv.This().(SliceViewer).LayoutSliceGrid()
tv.This().(SliceViewer).UpdateSliceGrid()
}
tv.ViewSig.Emit(tv.This(), 0, nil)
tv.SliceViewSig.Emit(tv.This(), int64(SliceViewDeleted), idx)
}
// SortSlice sorts the slice according to current settings
func (tv *TableView) SortSlice() {
if tv.SortIdx < 0 || tv.SortIdx >= len(tv.VisFields) {
return
}
rawIdx := tv.VisFields[tv.SortIdx].Index
kit.StructSliceSort(tv.Slice, rawIdx, !tv.SortDesc)
}
// SortSliceAction sorts the slice for given field index -- toggles ascending
// vs. descending if already sorting on this dimension
func (tv *TableView) SortSliceAction(fldIdx int) {
oswin.TheApp.Cursor(tv.ParentWindow().OSWin).Push(cursor.Wait)
defer oswin.TheApp.Cursor(tv.ParentWindow().OSWin).Pop()
wupdt := tv.TopUpdateStart()
defer tv.TopUpdateEnd(wupdt)
updt := tv.UpdateStart()
sgh := tv.SliceHeader()
sgh.SetFullReRender()
_, idxOff := tv.RowWidgetNs()
ascending := true
for fli := 0; fli < tv.NVisFields; fli++ {
hdr := sgh.Child(idxOff + fli).(*gi.Action)
if fli == fldIdx {
if tv.SortIdx == fli {
tv.SortDesc = !tv.SortDesc
ascending = !tv.SortDesc
} else {
tv.SortDesc = false
}
if ascending {
hdr.SetIcon("wedge-up")
} else {
hdr.SetIcon("wedge-down")
}
} else {
hdr.SetIcon("none")
}
}
tv.SortIdx = fldIdx
tv.SortSlice()
tv.UpdateSliceGrid()
tv.UpdateEnd(updt)
}
// ConfigToolbar configures the toolbar actions
func (tv *TableView) ConfigToolbar() {
if kit.IfaceIsNil(tv.Slice) {
return
}
if tv.ToolbarSlice == tv.Slice {
return
}
if pv, ok := tv.PropInherit("toolbar", ki.NoInherit, ki.TypeProps); ok {
pvb, _ := kit.ToBool(pv)
if !pvb {
tv.ToolbarSlice = tv.Slice
return
}
}
tb := tv.ToolBar()
ndef := 2 // number of default actions
if tv.isArray || tv.IsInactive() || tv.NoAdd {
ndef = 1
}
if len(*tb.Children()) < ndef {
tb.SetStretchMaxWidth()
tb.AddAction(gi.ActOpts{Label: "UpdtView", Icon: "update", Tooltip: "update this TableView to reflect current state of table"},
tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
tvv := recv.Embed(KiT_TableView).(*TableView)
tvv.UpdateSliceGrid()
})
if ndef > 1 {
tb.AddAction(gi.ActOpts{Label: "Add", Icon: "plus", Tooltip: "add a new element to the table"},
tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
tvv := recv.Embed(KiT_TableView).(*TableView)
tvv.SliceNewAt(-1)
})
}
}
sz := len(*tb.Children())
if sz > ndef {
for i := sz - 1; i >= ndef; i-- {
tb.DeleteChildAtIndex(i, ki.DestroyKids)
}
}
if HasToolBarView(tv.Slice) {
ToolBarView(tv.Slice, tv.ViewportSafe(), tb)
tb.SetFullReRender()
}
tv.ToolbarSlice = tv.Slice
}
// SortFieldName returns the name of the field being sorted, along with :up or
// :down depending on descending
func (tv *TableView) SortFieldName() string {
if tv.SortIdx >= 0 && tv.SortIdx < tv.NVisFields {
nm := tv.VisFields[tv.SortIdx].Name
if tv.SortDesc {
nm += ":down"
} else {
nm += ":up"
}
return nm
}
return ""
}
// SetSortField sets sorting to happen on given field and direction -- see
// SortFieldName for details
func (tv *TableView) SetSortFieldName(nm string) {
if nm == "" {
return
}
spnm := strings.Split(nm, ":")
for fli := 0; fli < tv.NVisFields; fli++ {
fld := tv.VisFields[fli]
if fld.Name == spnm[0] {
tv.SortIdx = fli
}
}
if len(spnm) == 2 {
if spnm[1] == "down" {
tv.SortDesc = true
} else {
tv.SortDesc = false
}
}
}
func (tv *TableView) Layout2D(parBBox image.Rectangle, iter int) bool {
redo := tv.Frame.Layout2D(parBBox, iter)
if !tv.IsConfiged() {
return redo
}
tv.LayoutHeader()
tv.SliceHeader().Layout2D(parBBox, iter)
return redo
}
// RowFirstVisWidget returns the first visible widget for given row (could be
// index or not) -- false if out of range
func (tv *TableView) RowFirstVisWidget(row int) (*gi.WidgetBase, bool) {
if !tv.IsRowInBounds(row) {
return nil, false
}
nWidgPerRow, idxOff := tv.RowWidgetNs()
sg := tv.SliceGrid()
widg := sg.Kids[row*nWidgPerRow].(gi.Node2D).AsWidget()
if widg.VpBBox != image.ZR {
return widg, true
}
ridx := nWidgPerRow * row
for fli := 0; fli < tv.NVisFields; fli++ {
widg := sg.Child(ridx + idxOff + fli).(gi.Node2D).AsWidget()
if widg.VpBBox != image.ZR {
return widg, true
}
}
return nil, false
}
// RowGrabFocus grabs the focus for the first focusable widget in given row --
// returns that element or nil if not successful -- note: grid must have
// already rendered for focus to be grabbed!
func (tv *TableView) RowGrabFocus(row int) *gi.WidgetBase {
if !tv.IsRowInBounds(row) || tv.InFocusGrab { // range check
return nil
}
nWidgPerRow, idxOff := tv.RowWidgetNs()
ridx := nWidgPerRow * row
sg := tv.SliceGrid()
// first check if we already have focus
for fli := 0; fli < tv.NVisFields; fli++ {
widg := sg.Child(ridx + idxOff + fli).(gi.Node2D).AsWidget()
if widg.HasFocus() || widg.ContainsFocus() {
return widg
}
}
tv.InFocusGrab = true
defer func() { tv.InFocusGrab = false }()
for fli := 0; fli < tv.NVisFields; fli++ {
widg := sg.Child(ridx + idxOff + fli).(gi.Node2D).AsWidget()
if widg.CanFocus() {
widg.GrabFocus()
return widg
}
}
return nil
}
// SelectRowWidgets sets the selection state of given row of widgets
func (tv *TableView) SelectRowWidgets(row int, sel bool) {
if row < 0 {
return
}
wupdt := tv.TopUpdateStart()
defer tv.TopUpdateEnd(wupdt)
sg := tv.SliceGrid()
nWidgPerRow, idxOff := tv.RowWidgetNs()
ridx := row * nWidgPerRow
for fli := 0; fli < tv.NVisFields; fli++ {
seldx := ridx + idxOff + fli
if sg.Kids.IsValidIndex(seldx) == nil {
widg := sg.Child(seldx).(gi.Node2D).AsNode2D()
widg.SetSelectedState(sel)
widg.UpdateSig()
}
}
if tv.ShowIndex {
if sg.Kids.IsValidIndex(ridx) == nil {
widg := sg.Child(ridx).(gi.Node2D).AsNode2D()
widg.SetSelectedState(sel)
widg.UpdateSig()
}
}
}
// SelectFieldVal sets SelField and SelVal and attempts to find corresponding
// row, setting SelectedIdx and selecting row if found -- returns true if
// found, false otherwise
func (tv *TableView) SelectFieldVal(fld, val string) bool {
tv.SelField = fld
tv.SelVal = val
if tv.SelField != "" && tv.SelVal != nil {
idx, _ := StructSliceIdxByValue(tv.Slice, tv.SelField, tv.SelVal)
if idx >= 0 {
tv.ScrollToIdx(idx)
tv.UpdateSelectIdx(idx, true)
return true
}
}