/* Landesamt für Geoinformation und Landentwicklung, Referat 35 Einführung in die Sprache Go, 25.7.2017 Lektion 9: Darstellung mit Qt-Framework */ package main import ( "fmt" "math" "os" "runtime" "strconv" "sync" "time" "Präsentation3hGolang4LGLRef35/go2lgl_l09/myGui" "github.com/therecipe/qt/core" "github.com/therecipe/qt/gui" "github.com/therecipe/qt/widgets" "github.com/0xAX/notificator" ) const ( anzahlTöpfchen uint8 = 35 anzahlKugeln uint32 = 1000000 intervallWartezeitSec uint8 = 1 höhenFaktor uint8 = 7 ) var timestamp [2]int64 var notify *notificator.Notificator var wg, wgcli, wggui sync.WaitGroup func main() { runtime.GOMAXPROCS(runtime.NumCPU()) timestamp[0] = time.Now().UnixNano() notify = notificator.New(notificator.Options{ DefaultIcon: "icon/default.png", AppName: "LGL-Demo", }) var töpfchenAlle []int = make([]int, anzahlTöpfchen, anzahlTöpfchen) var c chan bool = make(chan bool, 1) //Eröffne eine neue Applikation (als eine Sammlung von Widgets) widgets.NewQApplication(len(os.Args), os.Args) var c_widget = widgets.NewQWidget(nil, 0) var ui_widget = myGui.LeseUIFile(c_widget) var ui_textBrowser, ui_lcdNummer, ui_progressBar, ui_sliderArea = myGui.ErzeugeLayout(c_widget, ui_widget, &töpfchenAlle, c) // Beispiel für Parameterrückgabe als Tupel ui_lcdNummer.SetDigitCount(len(strconv.Itoa((int)(anzahlKugeln)))) var c_slider [anzahlTöpfchen]*widgets.QSlider for i := uint8(0); i < anzahlTöpfchen; i++ { c_slider[i] = widgets.NewQSlider2(core.Qt__Vertical, nil) c_slider[i].SetMinimum(0) c_slider[i].SetMaximum(int(anzahlKugeln) / int(höhenFaktor)) c_slider[i].SetValue(0) // c_slider[i].Maximum() / 4 c_slider[i].SetSizePolicy2(widgets.QSizePolicy__MinimumExpanding, widgets.QSizePolicy__MinimumExpanding) c_slider[i].ConnectResizeEvent(func(*gui.QResizeEvent) { for j := uint8(0); j < anzahlTöpfchen; j++ { c_slider[j].SetFixedHeight(c_widget.Height() * 93 / 100) } }) ui_sliderArea.AddWidget(c_slider[i], 0, core.Qt__AlignBottom) } wgcli.Add(1) go func(t *[]int, ch chan bool) { defer wgcli.Done() sum := func(mySlice *[]int) int { // Zuweisung anonymer Funktion var amount int for _, v := range *mySlice { amount += v } return amount } for i := int(0); true; i = sum(t) { select { case <-ch: ui_textBrowser.Append(fmt.Sprint("\u2690", *t)) ui_lcdNummer.Display2(int(anzahlKugeln)) ui_progressBar.SetValue(100) for j, v := range *t { c_slider[j].SetValue(v) //fmt.Println(j, v) } return default: ui_textBrowser.Append(fmt.Sprint("\u231B ", *t, runtime.NumGoroutine())) ui_lcdNummer.Display2(i) ui_progressBar.SetValue(int(math.Floor((float64(i) / float64(anzahlKugeln) * 100) + .5))) for j, v := range *t { c_slider[j].SetValue(v) //fmt.Println(j, v) } time.Sleep(time.Second * time.Duration(intervallWartezeitSec)) } } }(&töpfchenAlle, c) go func(wg *sync.WaitGroup, töpfchenAlle *[]int, c chan bool) { for k := (uint32)(1); k <= anzahlKugeln; k++ { wg.Add(1) go zeigeMirDenGauß(int(anzahlKugeln), int(anzahlTöpfchen), töpfchenAlle) for runtime.NumGoroutine() >= 50 { // Limitiere die Anzahl der go-Routinen time.Sleep(time.Millisecond * 1) } } c <- true }(&wg, &töpfchenAlle, c) widgets.QApplication_Exec() wg.Wait() wgcli.Wait() close(c) timestamp[1] = time.Now().UnixNano() notify.Push("LGL-Demo", fmt.Sprintf("\nProzess ist fertig und hat %v Sekunden gedauert!\n\n", (float64(timestamp[1]-timestamp[0])*math.Pow(10, -9))), "/home/user/icon.png", notificator.UR_CRITICAL) }