summaryrefslogtreecommitdiff
path: root/table.go
blob: e98e8a09ed1374bcc2ebd48674a6579a20ab87b2 (plain)
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
// a table is a grid with X columns and 1 row of header names
// that means that the toolkits might be able to sort the table
// pushing traditional application development to the display
// is that a good idea?

package gui

import (
	"go.wit.com/lib/protobuf/guipb"
	"go.wit.com/log"
	"go.wit.com/widget"
	"google.golang.org/protobuf/types/known/anypb"
)

// makes a grid inside an existing Widget
func (n *Node) NewTable(title string) *NodeTable {
	t := new(NodeTable)

	t.grid = n.RawGrid()

	t.tb = t.grid.newNode(title, widget.Table)
	t.tb.label = title

	return t
}

type NodeColumn struct {
	Title string
	Vals  []string
}

type NodeTable struct {
	grid   *Node
	tb     *Node
	Custom func()
}

/*
func ShowTable(pb *guipb.Table) {
	// make a new action and populate the current node state
	a := getNewAction(me.rootNode, widget.Show)

	makeTableWidgets(pb)

	nt := guipb.NewTables()
	nt.Append(pb)
	a.TablePB, err = nt.Marshal()
	if err != nil {
		log.Info("unmarshal error", err)
		return
	}
	log.Info("send action to plugin", "pb len =", len(a.TablePB))
	sendActionToPlugin(a)

}
*/

func (parent *Node) ShowTable(pb *guipb.Table) {
	// make a new action and populate the current node state
	// this should be parent and not rootNode?
	a := getNewAction(me.rootNode, widget.Show)

	pb.Parent = new(guipb.Widget)
	pb.Parent.Id = int64(parent.id)
	pb.Parent.Name = pb.Title

	makeTableGrid(pb)

	nt := guipb.NewTables()
	nt.Append(pb)
	a.TablePB, err = nt.Marshal()
	if err != nil {
		log.Info("unmarshal error", err)
		return
	}
	log.Info("send action to plugin", "pb len =", len(a.TablePB))
	sendActionToPlugin(a)
}

func (parent *Node) UpdateTable(pb *guipb.Table) {
	log.Info("UpdateTable:", pb.Title)
	a := getNewAction(parent, widget.Show)

	nt := guipb.NewTables()
	nt.Append(pb)
	a.TablePB, err = nt.Marshal()
	if err != nil {
		log.Info("unmarshal error", err)
		return
	}
	log.Info("send action to plugin", "pb len =", len(a.TablePB))
	sendActionToPlugin(a)
}

func makeTableWidgets(pb *guipb.Table) {
	if pb == nil {
		log.Info("pb was nil")
		return
	}
	if me.rootNode == nil {
		log.Info("me.rootNode was nil")
		return
	}
	win := addNode()
	pb.Window = new(guipb.Widget)
	pb.Window.Id = int64(win.id)
	pb.Window.Name = pb.Title

	makeTableGrid(pb)
}

func makeTableGrid(pb *guipb.Table) {
	grid := addNode()
	pb.Grid = new(guipb.Widget)
	pb.Grid.Id = int64(grid.id)
	pb.Grid.Name = "gridtablePB"

	/*
		// get unique widget ID numbers for the headers
		for i, r := range pb.Order {
			n := addNode()
			r.Id = int64(n.id)
		}
	*/

	for _, r := range pb.StringRows {
		// log.Info("gui: got string row:", pb.Title, i, r.Header, r.Vals)
		header := addNode()
		r.Header.Id = int64(header.id)
		for _, v := range r.Vals {
			label := addNode()
			pbwidget := new(guipb.Widget)
			pbwidget.Id = int64(label.id)
			pbwidget.Name = v
			r.Widgets = append(r.Widgets, pbwidget)
			// log.Info("gui: added new string", pbwidget)
		}
	}

	for _, r := range pb.IntRows {
		// log.Info("gui: got int row:", i, r.Header, r.Vals)
		header := addNode()
		r.Header.Id = int64(header.id)
		for _, v := range r.Vals {
			label := addNode()
			pbwidget := new(guipb.Widget)
			pbwidget.Id = int64(label.id)
			pbwidget.Size = v
			r.Widgets = append(r.Widgets, pbwidget)
			// log.Info("gui: added new int", pbwidget)
		}
	}

	for _, r := range pb.TimeRows {
		// log.Info("gui: got time row:", i, r.Header, r.Vals)
		header := addNode()
		r.Header.Id = int64(header.id)
		for _, v := range r.Vals {
			label := addNode()
			pbwidget := new(guipb.Widget)
			pbwidget.Id = int64(label.id)
			anyValue, err := anypb.New(v)
			if err != nil {
				log.Info("gui: failed to add time", err)
				continue
			}
			pbwidget.Val = anyValue
			r.Widgets = append(r.Widgets, pbwidget)
			// log.Info("gui: added new val", pbwidget)
		}
	}
}