-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingsPanel.go
782 lines (677 loc) · 25.2 KB
/
settingsPanel.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
// Copyright (C) 2022 dhowlett99.
// This is the dmxlights fixture editor it is attached to a fixture and
// describes the fixtures properties which is then saved in the fixtures.yaml
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package editor
import (
"fmt"
"image/color"
"strconv"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/dhowlett99/dmxlights/pkg/fixture"
)
const TABLE_HEIGHT int = 7
const MAX_NUMBER_SETTINGS = 512
type SettingsPanel struct {
UseFixtureName string
SettingsPanel *widget.Table
SettingsList []fixture.Setting
SettingMaxDegrees *int
SettingsOptions []string
ChannelOptions []string
SelectedValueOptions []string
CurrentChannel int
UpdateThisChannel int
UpdateSettings bool
MaxNumberSettings int
NameEntryError map[int]bool
DMXValueEntryError map[int]bool
Fixtures *fixture.Fixtures
}
const (
SETTING_NUMBER int = iota
SETTING_NAME
SETTING_CHANNEL
SETTING_VALUE
SETTING_SELECT_VALUE
SETTING_DELETE
SETTING_ADD
)
const (
COLUMN_ID int = iota
COLUMN_NAME
COLUMN_CHANNEL
COLUMN_VALUE
COLUMN_SELECT_VALUE
COLUMN_DELETE
COLUMN_ADD
)
const (
TITLE int = iota
MESSAGE
REPORT
)
func makeDMXoptions() (options []string) {
for x := 0; x < 256; x++ {
options = append(options, strconv.Itoa(x))
}
return options
}
func headerSettingsCreate() fyne.CanvasObject {
h := &ActiveHeader{}
h.ExtendBaseWidget(h)
h.SetText("000")
return h
}
func headerSettingsUpdate(id widget.TableCellID, o fyne.CanvasObject) {
header := o.(*ActiveHeader)
header.TextStyle.Bold = true
switch id.Col {
case -1:
header.SetText(strconv.Itoa(id.Row + 1))
case 0:
header.SetText("ID")
case 1:
header.SetText("Name")
case 2:
header.SetText("Channel")
case 3:
header.SetText("Value")
case 4:
header.SetText("Select")
case 5:
header.SetText("-")
case 6:
header.SetText("+")
}
}
func headerSettingsUpdateWithOutChannel(id widget.TableCellID, o fyne.CanvasObject) {
header := o.(*ActiveHeader)
header.TextStyle.Bold = true
switch id.Col {
case -1:
header.SetText(strconv.Itoa(id.Row + 1))
case 0:
header.SetText("ID")
case 1:
header.SetText("Name")
case 2:
header.SetText("")
case 3:
header.SetText("Value")
case 4:
header.SetText("Select")
case 5:
header.SetText("-")
case 6:
header.SetText("+")
}
}
func NewSettingsPanel(w fyne.Window, channelPanel bool, SettingsList []fixture.Setting, maxNumberSettings int, buttonSave *widget.Button) *SettingsPanel {
if debug {
fmt.Printf("NewSettingsPanel\n")
}
var data = [][]string{}
st := SettingsPanel{}
st.SettingsList = SettingsList
st.SettingsOptions = []string{"Off", "On", "Red", "Green", "Blue", "Soft", "Sharp", "Sound", "Rotate"}
st.ChannelOptions = []string{"None"}
st.SelectedValueOptions = makeDMXoptions()
Red := color.RGBA{}
Red.R = uint8(255)
Red.G = uint8(0)
Red.B = uint8(0)
Red.A = 255
White := color.White
// Storage for error flags for each fixture.
st.NameEntryError = make(map[int]bool, len(st.SettingsList))
st.DMXValueEntryError = make(map[int]bool, len(st.SettingsList))
selectValue := make([]*widget.Select, maxNumberSettings)
// Create a dialog for error messages.
var reports []string
popupErrorPanel := &widget.PopUp{}
// Ok button.
button := widget.NewButton("OK", func() {
popupErrorPanel.Hide()
})
popupErrorPanel = widget.NewModalPopUp(
container.NewVBox(
widget.NewLabel("Title"),
widget.NewLabel("Error Message"),
widget.NewLabel("Report"),
container.NewHBox(layout.NewSpacer(), button),
),
w.Canvas(),
)
// Settingses Selection Panel.
st.SettingsPanel = widget.NewTableWithHeaders(
// Function to find length.
func() (int, int) {
height := len(data)
width := TABLE_HEIGHT
return height, width
},
// Function to create table.
func() (o fyne.CanvasObject) {
// Load the settings into the array used by the table.
data = makeSettingsArray(st.SettingsList)
return container.NewStack(
// SETTING_NUMBER
widget.NewLabel("template"),
// SETTING_NAME
container.NewStack(
canvas.NewRectangle(color.White),
widget.NewEntry(),
),
// SETTING_CHANNEL
widget.NewSelect(st.ChannelOptions, func(value string) {}),
// SETTING_VALUE
container.NewStack(
canvas.NewRectangle(color.White),
widget.NewEntry(),
),
// SETTING_SELECT_VALUE
widget.NewSelect(st.SelectedValueOptions, func(value string) {}),
// SETTING_DELETE
widget.NewButton("-", func() {}),
// SETTING_ADD
widget.NewButton("+", func() {}),
)
},
// Function to update item in this table.
func(i widget.TableCellID, o fyne.CanvasObject) {
// Hide all field types.
hideAllSettingsFields(o)
// Show the setting a number.
if i.Col == SETTING_NUMBER {
showSettingsField(SETTING_NUMBER, channelPanel, o)
o.(*fyne.Container).Objects[SETTING_NUMBER].(*widget.Label).SetText(data[i.Row][i.Col])
}
// Show and Edit the Setting Name.
if i.Col == SETTING_NAME {
showSettingsField(SETTING_NAME, channelPanel, o)
if st.NameEntryError[st.SettingsList[i.Row].Number] {
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).FillColor = Red
} else {
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).FillColor = White
}
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).OnChanged = nil
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(data[i.Row][i.Col])
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).OnChanged = func(settingName string) {
if settingName != "" {
newSetting := makeNewSetting(st.SettingsList, i.Row)
newSetting.Label = settingName
newSetting.Name = settingName
st.SettingsList = updateSettingsItem(st.SettingsList, newSetting.Number, newSetting)
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
// Clear all errors in all rows.
for row := 0; row < len(data); row++ {
st.NameEntryError[row] = false
}
// Check the text entered.
err := checkTextEntry(settingName)
if err != nil {
st.NameEntryError[st.SettingsList[i.Row].Number] = true
st.SettingsPanel.Refresh()
// Populate error message panel.
popupErrorPanel.Content.(*fyne.Container).Objects[TITLE].(*widget.Label).Text = "Name Entry Error"
popupErrorPanel.Content.(*fyne.Container).Objects[MESSAGE].(*widget.Label).Text = err.Error()
popupErrorPanel.Content.(*fyne.Container).Objects[REPORT].(*widget.Label).Text = strings.Join(reports, "\n")
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(data[i.Row][i.Col])
st.SettingsList[i.Row].Name = data[i.Row][i.Col]
popupErrorPanel.Show()
// Disable the save button.
buttonSave.Disable()
} else {
st.NameEntryError[st.SettingsList[i.Row].Number] = false
// And make sure we refresh every row, when we update this field.
// So all the red error rectangls will disappear
st.SettingsPanel.Refresh()
// Enable the save button.
buttonSave.Enable()
}
}
}
}
// Channel number.
if i.Col == SETTING_CHANNEL {
showSettingsField(SETTING_CHANNEL, channelPanel, o)
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).OnChanged = nil
// Update the options to include any thing that might specified in the config file.
st.ChannelOptions = addOption(st.ChannelOptions, data[i.Row][i.Col])
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).Options = st.ChannelOptions
// Match the options to the data in the field and display in anyway
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).SetSelected(data[i.Row][i.Col])
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).OnChanged = func(settingChannel string) {
newSetting := makeNewSetting(st.SettingsList, i.Row)
newSetting.Channel = settingChannel
st.SettingsList = updateSettingsItem(st.SettingsList, newSetting.Number, newSetting)
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
// Now if this channel has some settings, populate the options for the select value.
if !channelPanel {
if channelHasSettings(st.UseFixtureName, data[i.Row][SETTING_CHANNEL], st.Fixtures) {
st.SelectedValueOptions = getSettingsForChannel(st.UseFixtureName, data[i.Row][SETTING_CHANNEL], st.Fixtures)
} else {
st.SelectedValueOptions = makeDMXoptions()
}
}
// Set selectable channel options.
selectValue[i.Row].Options = st.SelectedValueOptions
selectValue[i.Row].ClearSelected()
st.SettingsPanel.Refresh()
}
}
// Show and Edit the Setting Value.
if i.Col == SETTING_VALUE {
showSettingsField(SETTING_VALUE, channelPanel, o)
// Change the color of the value box if there's a DMX value error.
if st.DMXValueEntryError[st.SettingsList[i.Row].Number] {
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).FillColor = Red
} else {
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).FillColor = White
}
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).OnChanged = nil
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(data[i.Row][i.Col])
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).OnChanged = func(settingValue string) {
if settingValue != "" {
newSetting := makeNewSetting(st.SettingsList, i.Row)
newSetting.Value = settingValue
st.SettingsList = updateSettingsItem(st.SettingsList, newSetting.Number, newSetting)
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
// Clear all errors in all rows.
for row := 0; row < len(data); row++ {
st.NameEntryError[row] = false
}
if newSetting.Name == "MaxDegrees" {
// We're adding a MaxDegrees setting to the channel.
// Populate MaxDegrees - If we are a channel name of Pan or Tilt and any of the settings contain the name MaxDegrees
// add the value to newChannel.MaxDegrees
maxDegrees, _ := strconv.Atoi(newSetting.Value)
st.SettingMaxDegrees = &maxDegrees
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
} else {
// Check the text entered.
err := checkDMXValue(settingValue)
if err != nil {
st.DMXValueEntryError[st.SettingsList[i.Row].Number] = true
st.SettingsPanel.Refresh()
popupErrorPanel.Content.(*fyne.Container).Objects[0].(*widget.Label).Text = "Value Entry Error"
popupErrorPanel.Content.(*fyne.Container).Objects[1].(*widget.Label).Text = err.Error()
popupErrorPanel.Content.(*fyne.Container).Objects[2].(*widget.Label).Text = strings.Join(reports, "\n")
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(data[i.Row][i.Col])
st.SettingsList[i.Row].Value = data[i.Row][i.Col]
popupErrorPanel.Show()
// Disable the save button.
buttonSave.Disable()
} else {
st.DMXValueEntryError[st.SettingsList[i.Row].Number] = false
// And make sure we refresh every row, when we update this field.
// So all the red error rectangls will disappear
st.SettingsPanel.Refresh()
// Enable the save button.
buttonSave.Enable()
}
}
}
}
}
if i.Col == SETTING_SELECT_VALUE {
showSettingsField(SETTING_SELECT_VALUE, channelPanel, o)
if i.Row < maxNumberSettings {
selectValue[i.Row] = o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select)
}
if !channelPanel {
st.SelectedValueOptions = populateSelectedValueNames(data[i.Row][SETTING_CHANNEL], st.UseFixtureName, st.Fixtures)
}
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).OnChanged = nil
// Populate the options.
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).Options = st.SelectedValueOptions
// Match the options to the data in the field and display in anyway.
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).SetSelected(data[i.Row][SETTING_SELECT_VALUE])
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).OnChanged = func(settingSelectValue string) {
newSetting := makeNewSetting(st.SettingsList, i.Row)
// Is this selected value a number.
if _, err := strconv.ParseInt(settingSelectValue, 10, 64); err == nil {
newSetting.Value = settingSelectValue
} else {
// Must contain letters that form a label that can be looked up in the settings.
newSetting.Value = findSettingValueByName(st.UseFixtureName, data[i.Row][SETTING_CHANNEL], settingSelectValue, st.Fixtures)
}
// Now set the value field based on what we've selected.
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(newSetting.Value)
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).Refresh()
// And use the name as the default name for this setting.
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).SetText(settingSelectValue)
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).Refresh()
// Update the fields.
newSetting.Name = settingSelectValue
newSetting.Label = settingSelectValue
newSetting.SelectedValue = settingSelectValue
st.SettingsList = updateSettingsItem(st.SettingsList, newSetting.Number, newSetting)
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
st.SettingsPanel.Refresh()
}
}
// Show the Delete Setting Button.
if i.Col == SETTING_DELETE {
showSettingsField(SETTING_DELETE, channelPanel, o)
o.(*fyne.Container).Objects[SETTING_DELETE].(*widget.Button).OnTapped = nil
o.(*fyne.Container).Objects[SETTING_DELETE].(*widget.Button).OnTapped = func() {
if len(st.SettingsList) != 0 {
st.SettingsList = deleteSettingsItem(st.SettingsList, st.SettingsList[i.Row].Number-1)
}
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
if len(st.SettingsList) == 0 {
st.SettingsPanel.Hide()
} else {
st.SettingsPanel.Refresh()
}
}
}
// Show the Add Setting Button.
if i.Col == SETTING_ADD {
showSettingsField(SETTING_ADD, channelPanel, o)
o.(*fyne.Container).Objects[SETTING_ADD].(*widget.Button).OnTapped = nil
o.(*fyne.Container).Objects[SETTING_ADD].(*widget.Button).OnTapped = func() {
if len(data) < maxNumberSettings {
if len(st.SettingsList) != 0 {
st.SettingsList = addSettingsItem(st.SettingsList, st.SettingsList[i.Row].Number)
} else {
st.SettingsList = addSettingsItem(st.SettingsList, 0)
}
data = makeSettingsArray(st.SettingsList)
st.UpdateSettings = true
st.UpdateThisChannel = st.CurrentChannel - 1
st.SettingsPanel.Refresh()
} else {
popupErrorPanel = widget.NewModalPopUp(
container.NewVBox(
widget.NewLabel("Error"),
widget.NewLabel("Max number of settings exceeded"),
container.NewHBox(layout.NewSpacer(), button),
),
w.Canvas(),
)
popupErrorPanel.Show()
}
}
}
},
)
st.SettingsPanel.ShowHeaderColumn = false
st.SettingsPanel.CreateHeader = headerSettingsCreate
if !channelPanel {
st.SettingsPanel.UpdateHeader = headerSettingsUpdate
} else {
st.SettingsPanel.UpdateHeader = headerSettingsUpdateWithOutChannel
}
// Setup the columns of this table.
st.SettingsPanel.SetColumnWidth(COLUMN_ID, 40) // Number
st.SettingsPanel.SetColumnWidth(COLUMN_NAME, 100) // Name
st.SettingsPanel.SetColumnWidth(COLUMN_CHANNEL, 90) // Channel
st.SettingsPanel.SetColumnWidth(COLUMN_VALUE, 50) // Value
st.SettingsPanel.SetColumnWidth(COLUMN_SELECT_VALUE, 90) // Select Value
st.SettingsPanel.SetColumnWidth(COLUMN_DELETE, 20) // Delete
st.SettingsPanel.SetColumnWidth(COLUMN_ADD, 20) // Add
// If we creating channel hide the channel selection box.
if channelPanel {
st.SettingsPanel.SetColumnWidth(COLUMN_CHANNEL, 0) // Channel
}
return &st
}
func addOption(options []string, newOption string) []string {
newOptions := []string{}
for _, option := range options {
if option != newOption {
newOptions = append(newOptions, option)
}
}
// now add the new option.
newOptions = append(newOptions, newOption)
return newOptions
}
func addSettingsItem(items []fixture.Setting, number int) (outItems []fixture.Setting) {
if debug {
fmt.Printf("addSettingsItem\n")
}
newItems := []fixture.Setting{}
newItem := fixture.Setting{}
newItem.Number = int(number) + 1
newItem.Name = "New"
newItem.Label = "New"
newItem.Channel = "(Select one)"
newItem.Value = "0"
var added bool // Only add once.
if number == 0 {
items = append(items, newItem)
}
for no, item := range items {
// Add at the start of an empty list.
if len(items) == 0 && !added {
newItems = append(newItems, newItem)
added = true
}
// Insert at this position.
if item.Number == number+1 && !added {
if debug {
fmt.Printf("Insert at this position %+v\n", newItem)
}
newItems = append(newItems, newItem)
added = true
}
newItems = append(newItems, item)
// Append an item at the very end.
if no == len(items)-1 && !added {
newItems = append(newItems, newItem)
added = true
}
}
// Now fix the item numbers
for number, indexedItem := range newItems {
indexedItem.Number = number + 1
outItems = append(outItems, indexedItem)
}
return outItems
}
func deleteSettingsItem(settingsList []fixture.Setting, id int) (outItems []fixture.Setting) {
if debug {
fmt.Printf("deleteSettingsItem\n")
}
newSettings := []fixture.Setting{}
for settingNumber, setting := range settingsList {
if settingNumber != id {
newSettings = append(newSettings, setting)
}
}
// Now fix the item numbers
for number, indexedItem := range newSettings {
indexedItem.Number = number + 1
outItems = append(outItems, indexedItem)
}
return outItems
}
func makeNewSetting(settingList []fixture.Setting, row int) fixture.Setting {
if debug {
fmt.Printf("makeNewSetting\n")
}
newSetting := fixture.Setting{}
newSetting.Label = settingList[row].Label
newSetting.Name = settingList[row].Name
newSetting.Number = settingList[row].Number
newSetting.Channel = settingList[row].Channel
newSetting.Value = settingList[row].Value
newSetting.SelectedValue = settingList[row].SelectedValue
return newSetting
}
func updateSettingsItem(settingList []fixture.Setting, row int, newItem fixture.Setting) []fixture.Setting {
if debug {
fmt.Printf("updateSettingsItem\n")
}
newItems := []fixture.Setting{}
for _, item := range settingList {
if item.Number == row {
// update the settings information.
newItems = append(newItems, newItem)
} else {
// just add what was there before.
newItems = append(newItems, item)
}
}
return newItems
}
// makeSettingsArray - Convert the list of settings to an array of strings containing and array of strings with
// the values from each fixture.
// This is done once when the settings panel is loaded.
func makeSettingsArray(settings []fixture.Setting) [][]string {
if debug {
fmt.Printf("makeSettingsArray\n")
}
var data = [][]string{}
for _, setting := range settings {
newSetting := []string{}
newSetting = append(newSetting, fmt.Sprintf("%d", setting.Number))
newSetting = append(newSetting, setting.Name)
newSetting = append(newSetting, setting.Channel)
newSetting = append(newSetting, setting.Value)
newSetting = append(newSetting, setting.SelectedValue)
newSetting = append(newSetting, "")
newSetting = append(newSetting, "-")
newSetting = append(newSetting, "+")
newSetting = append(newSetting, "Channels")
data = append(data, newSetting)
}
return data
}
func showSettingsField(field int, channelPanel bool, o fyne.CanvasObject) {
if debug {
fmt.Printf("showSettingsField\n")
}
// Now show the selected field.
switch {
case field == SETTING_NUMBER:
o.(*fyne.Container).Objects[SETTING_NUMBER].(*widget.Label).Hidden = false
case field == SETTING_NAME:
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).Hidden = false
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).Hidden = false
case field == SETTING_CHANNEL && !channelPanel:
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).Hidden = false
case field == SETTING_VALUE:
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).Hidden = false
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).Hidden = false
case field == SETTING_SELECT_VALUE:
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).Hidden = false
case field == SETTING_DELETE:
o.(*fyne.Container).Objects[SETTING_DELETE].(*widget.Button).Hidden = false
case field == SETTING_ADD:
o.(*fyne.Container).Objects[SETTING_ADD].(*widget.Button).Hidden = false
}
}
func hideAllSettingsFields(o fyne.CanvasObject) {
if debug {
fmt.Printf("hideAllSettingsFields\n")
}
o.(*fyne.Container).Objects[SETTING_NUMBER].(*widget.Label).Hidden = true
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[TEXT].(*widget.Entry).Hidden = true
o.(*fyne.Container).Objects[SETTING_NAME].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).Hidden = true
o.(*fyne.Container).Objects[SETTING_CHANNEL].(*widget.Select).Hidden = true
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[TEXT].(*widget.Entry).Hidden = true
o.(*fyne.Container).Objects[SETTING_VALUE].(*fyne.Container).Objects[RECTANGLE].(*canvas.Rectangle).Hidden = true
o.(*fyne.Container).Objects[SETTING_SELECT_VALUE].(*widget.Select).Hidden = true
o.(*fyne.Container).Objects[SETTING_DELETE].(*widget.Button).Hidden = true
o.(*fyne.Container).Objects[SETTING_ADD].(*widget.Button).Hidden = true
}
func channelHasSettings(useFixtureName string, channelName string, fixtures *fixture.Fixtures) bool {
if debug {
fmt.Printf("channelHasSettings for Fixture Name %s Name %s\n", useFixtureName, channelName)
}
for _, fixture := range fixtures.Fixtures {
if fixture.Label == useFixtureName {
if debug {
fmt.Printf("channelHasSettings found fixture %+v\n", fixture)
}
for _, channel := range fixture.Channels {
if strings.Contains(channel.Name, channelName) {
if channel.Settings != nil {
return true
}
}
}
}
}
return false
}
func getSettingsForChannel(useFixtureName string, channelName string, fixtures *fixture.Fixtures) []string {
if debug {
fmt.Printf("getSettingsForChannel for Fixture Name %s Channel Name %s\n", useFixtureName, channelName)
}
settings := []string{}
for _, fixture := range fixtures.Fixtures {
if fixture.Label == useFixtureName {
for _, channel := range fixture.Channels {
if strings.Contains(channel.Name, channelName) {
if channel.Settings != nil {
for _, setting := range channel.Settings {
settings = append(settings, setting.Name)
}
if debug {
fmt.Printf("getSettingsForChannel settings %+v\n", settings)
}
return settings
}
}
}
}
}
return nil
}
func findSettingValueByName(useFixtureName string, channelName string, settingName string, fixtures *fixture.Fixtures) string {
if debug {
fmt.Printf("findSettingValueByName for Fixture Name %s Channel Name %s\n", useFixtureName, channelName)
}
for _, fixture := range fixtures.Fixtures {
if fixture.Label == useFixtureName {
for _, channel := range fixture.Channels {
if strings.Contains(channel.Name, channelName) {
if channel.Settings != nil {
for _, setting := range channel.Settings {
if strings.Contains(setting.Name, settingName) {
if debug {
fmt.Printf("settings %s value %s\n", setting.Name, setting.Value)
}
return setting.Value
}
}
}
}
}
}
}
return ""
}