-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
180 lines (151 loc) · 4.3 KB
/
window.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
package yaac_frontend_opencv
import (
"fmt"
"image/color"
"io"
"log"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
yaac_shared "github.com/DHBW-SE-2023/YAAC/internal/shared"
)
var gv GlobalVars
type GlobalVars struct {
App fyne.App
Window fyne.Window
ImagePath string
ProgBar *widget.ProgressBar
InputImageContainer *fyne.Container
OutputImageContainer *fyne.Container
}
func (f *WindowOpenCV) Open() {
gv = GlobalVars{}
gv.App = *yaac_shared.GetApp()
// setuping window
gv.Window = gv.App.NewWindow("OpenCV Demo")
// set icon
gv.Window.SetIcon(yaac_shared.ResourceIconPng)
// handle main window
gv.Window.SetContent(makeWindow(f))
gv.Window.Resize(fyne.NewSize(800, 600))
gv.Window.Show()
gv.App.Run()
}
func (f *WindowOpenCV) UpdateProgress(value float64) {
gv.ProgBar.SetValue(value)
}
func makeWindow(f *WindowOpenCV) *fyne.Container {
header := widget.NewLabel("Please select an Input image:")
inputImage := canvas.NewLinearGradient(color.Transparent, color.Black, 0)
inputImageScroll := container.NewScroll(inputImage)
gv.InputImageContainer = container.NewAdaptiveGrid(1, inputImageScroll)
gv.OutputImageContainer = container.NewAdaptiveGrid(1, inputImageScroll)
openFile := widget.NewButton("File Open With Filter (.jpg or .png)", func() {
fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(fmt.Errorf("fehler bei der dateiauswahl\n%w", err), gv.Window)
return
}
if reader == nil {
log.Println("Cancelled")
return
}
gv.ImagePath = reader.URI().Path()
showImage(reader, gv.InputImageContainer)
}, gv.Window)
fd.SetFilter(storage.NewExtensionFileFilter([]string{".png", ".jpg", ".jpeg"}))
fd.Show()
})
startOpenCV := widget.NewButton("Run OpenCV", func() {
f.MVVM.StartGoCV(gv.ImagePath)
})
gv.ProgBar = widget.NewProgressBar()
/*
box := container.NewVBox(
header,
openFile,
inputImageContainer,
startOpenCV,
opencvDemoWindow.ProgBar,
)
*/
return container.NewAdaptiveGrid(1, container.NewScroll(container.NewAdaptiveGrid(
1,
container.NewVBox(
header,
openFile,
),
gv.InputImageContainer,
container.NewVBox(
startOpenCV,
gv.ProgBar,
),
gv.OutputImageContainer,
)))
}
func (f *WindowOpenCV) ShowGeneratedImage(outPath string) {
// Load the image resource directly from the file path
res, err := fyne.LoadResourceFromPath(outPath)
if err != nil {
log.Println("Error loading generated image:", err)
return
}
// Create an image from the resource
img := canvas.NewImageFromResource(res)
if img == nil {
log.Println("Error creating image from resource")
return
}
img.FillMode = canvas.ImageFillContain
imgScroll := container.NewScroll(img)
gv.OutputImageContainer.Objects = []fyne.CanvasObject{imgScroll}
// Refresh the content to display the new image
gv.Window.Content().Refresh()
gv.Window.RequestFocus()
gv.Window.Show()
}
func loadImage(f fyne.URIReadCloser) *canvas.Image {
data, err := io.ReadAll(f)
if err != nil {
fyne.LogError("Error at loading file", err)
return nil
}
res := fyne.NewStaticResource(f.URI().Name(), data)
img := canvas.NewImageFromResource(res)
if img == nil {
fyne.LogError("Error at creating file object", err)
return nil
}
return img
}
func showImage(f fyne.URIReadCloser, imgContainer *fyne.Container) {
if f == nil {
log.Println("Cancelled")
return
}
defer f.Close()
img := loadImage(f)
if img == nil {
log.Println("Error at loading image")
return
}
img.FillMode = canvas.ImageFillContain
// Create a container with dynamic sizing
//containerWithDynamicSizing := fyne.NewContainer(img)
// Set the content of the main container to the new container with dynamic sizing
//imgContainer.Objects = []fyne.CanvasObject{containerWithDynamicSizing}
//inputImage := canvas.NewImageFromFile(img.File)
//inputImageScroll := container.NewScroll(inputImage)
imgScroll := container.NewScroll(img)
//imgScroll.Resize(img.Size())
imgContainer.Objects = []fyne.CanvasObject{imgScroll}
//imgContainer.Resize(img.Size())
//fmt.Println(img.Size())
// Actualize and show window
gv.Window.Content().Refresh()
gv.Window.RequestFocus()
gv.Window.Show()
}