uic: support for spacers

This commit is contained in:
mappu 2024-10-03 17:11:30 +13:00
parent 0ba1f0f6fa
commit 2c05d876f9
2 changed files with 67 additions and 45 deletions

View File

@ -7,7 +7,10 @@ import (
type UiLayoutItem struct {
Row *int `xml:"row,attr"`
Column *int `xml:"column,attr"`
Widget UiWidget `xml:"widget"`
// A layout item either has a widget, or a spacer
Widget *UiWidget `xml:"widget"`
Spacer *UiSpacer `xml:"spacer"`
}
type UiLayout struct {
@ -20,6 +23,11 @@ type UiPropertyContainer struct {
Properties []UiProperty `xml:"property"`
}
type UiSpacer struct {
Name string `xml:"name,attr"`
Properties []UiProperty `xml:"property"`
}
type UiWidget struct {
Class string `xml:"class,attr"`
Name string `xml:"name,attr"`

View File

@ -7,19 +7,24 @@ import (
"strings"
)
func collectClassNames_Widget(u UiWidget) []string {
func collectClassNames_Widget(u *UiWidget) []string {
var ret []string
if u.Name != "" {
ret = append(ret, u.Name+" *qt."+u.Class)
}
for _, w := range u.Widgets {
ret = append(ret, collectClassNames_Widget(w)...)
ret = append(ret, collectClassNames_Widget(&w)...)
}
if u.Layout != nil {
ret = append(ret, u.Layout.Name+" *qt."+u.Layout.Class)
for _, li := range u.Layout.Items {
if li.Widget != nil {
ret = append(ret, collectClassNames_Widget(li.Widget)...)
}
if li.Spacer != nil {
ret = append(ret, li.Spacer.Name+" *qt.QSpacerItem")
}
}
}
for _, a := range u.Actions {
ret = append(ret, a.Name+" *qt.QAction")
@ -127,14 +132,22 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
ui.` + w.Layout.Name + `.SetObjectName(` + strconv.Quote(w.Layout.Name) + `)
`)
for _, child := range w.Layout.Items {
for i, child := range w.Layout.Items {
// A layout item is either a widget, or a spacer
if child.Spacer != nil {
ret.WriteString("/* miqt-uic: no handler for spacer */\n")
}
if child.Widget != nil {
// Layout items have the parent as the real QWidget parent and are
// separately assigned to the layout afterwards
nest, err := generateWidget(child.Widget, `ui.`+w.Name, w.Class)
nest, err := generateWidget(*child.Widget, `ui.`+w.Name, w.Class)
if err != nil {
return "", fmt.Errorf(w.Name+": %w", err)
return "", fmt.Errorf(w.Name+"/Layout/Item[%d]: %w", i, err)
}
ret.WriteString(nest)
@ -180,6 +193,7 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
}
}
}
}
// Actions
@ -301,7 +315,7 @@ import (
)
type ` + u.Class + `Ui struct {
` + strings.Join(collectClassNames_Widget(u.Widget), "\n") + `
` + strings.Join(collectClassNames_Widget(&u.Widget), "\n") + `
}
// New` + u.Class + `Ui creates all Qt widget classes for ` + u.Class + `.