uic: support icons using iconset

This commit is contained in:
mappu 2024-10-03 18:51:50 +13:00
parent a0a4624563
commit 4c6062de1f
2 changed files with 63 additions and 0 deletions

View File

@ -57,6 +57,21 @@ type UiString struct {
Notr bool `xml:"notr,attr,omitempty"` Notr bool `xml:"notr,attr,omitempty"`
} }
type UiIcon struct {
ResourceFile string `xml:"resource,attr"`
NormalOff *string `xml:"normaloff,omitempty"`
NormalOn *string `xml:"normalon,omitempty"`
ActiveOff *string `xml:"activeoff,omitempty"`
ActiveOn *string `xml:"activeon,omitempty"`
DisabledOff *string `xml:"disabledoff,omitempty"`
DisabledOn *string `xml:"disabledon,omitempty"`
SelectedOff *string `xml:"selectedoff,omitempty"`
SelectedOn *string `xml:"selectedon,omitempty"`
Base string `xml:",chardata"`
}
type UiProperty struct { type UiProperty struct {
Name string `xml:"name,attr"` Name string `xml:"name,attr"`
StringVal *UiString `xml:"string,omitempty"` StringVal *UiString `xml:"string,omitempty"`
@ -64,6 +79,7 @@ type UiProperty struct {
BoolVal *bool `xml:"bool,omitempty"` // "true" or "false" BoolVal *bool `xml:"bool,omitempty"` // "true" or "false"
EnumVal *string `xml:"enum,omitempty"` EnumVal *string `xml:"enum,omitempty"`
RectVal *UiRect `xml:"rect,omitempty"` RectVal *UiRect `xml:"rect,omitempty"`
IconVal *UiIcon `xml:"iconset,omitempty"`
} }
type UiActionReference struct { type UiActionReference struct {

View File

@ -10,6 +10,7 @@ import (
var ( var (
DefaultGridMargin = 11 DefaultGridMargin = 11
DefaultSpacing = 6 DefaultSpacing = 6
IconCounter = 0
) )
func collectClassNames_Widget(u *UiWidget) []string { func collectClassNames_Widget(u *UiWidget) []string {
@ -63,6 +64,45 @@ func normalizeEnumName(s string) string {
return `qt.` + strings.Replace(s, `::`, `__`, -1) return `qt.` + strings.Replace(s, `::`, `__`, -1)
} }
func renderSetIcon(targetName, setterFunc string, iconVal *UiIcon, ret *strings.Builder) {
iconName := fmt.Sprintf("icon%d", IconCounter)
IconCounter++
ret.WriteString(iconName + " := qt.NewQIcon()\n")
// A base entry is a synonym for NormalOff. Don't need them both
if iconVal.NormalOff != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOff) + ", qt.NewQSize(), qt.QIcon__Normal, qt.QIcon__Off)\n")
} else {
ret.WriteString(iconName + ".AddFile(" + strconv.Quote(strings.TrimSpace(iconVal.Base)) + ")\n")
}
if iconVal.NormalOn != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Normal, qt.QIcon__On)\n")
}
if iconVal.ActiveOff != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Active, qt.QIcon__Off)\n")
}
if iconVal.ActiveOn != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Active, qt.QIcon__On)\n")
}
if iconVal.DisabledOff != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Disabled, qt.QIcon__Off)\n")
}
if iconVal.DisabledOn != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Disabled, qt.QIcon__On)\n")
}
if iconVal.SelectedOff != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Selected, qt.QIcon__Off)\n")
}
if iconVal.SelectedOn != nil {
ret.WriteString(iconName + ".AddFile4(" + strconv.Quote(*iconVal.NormalOn) + ", qt.NewQSize(), qt.QIcon__Selected, qt.QIcon__On)\n")
}
ret.WriteString(`ui.` + targetName + setterFunc + `(` + iconName + ")\n")
}
func renderProperties(properties []UiProperty, ret *strings.Builder, targetName, parentClass string, isLayout bool) error { func renderProperties(properties []UiProperty, ret *strings.Builder, targetName, parentClass string, isLayout bool) error {
contentsMargins := [4]int{DefaultGridMargin, DefaultGridMargin, DefaultGridMargin, DefaultGridMargin} // left, top, right, bottom contentsMargins := [4]int{DefaultGridMargin, DefaultGridMargin, DefaultGridMargin, DefaultGridMargin} // left, top, right, bottom
@ -122,6 +162,9 @@ func renderProperties(properties []UiProperty, ret *strings.Builder, targetName,
// detect the case and convert it to match // detect the case and convert it to match
ret.WriteString(`ui.` + targetName + setterFunc + `(` + normalizeEnumName(*prop.EnumVal) + ")\n") ret.WriteString(`ui.` + targetName + setterFunc + `(` + normalizeEnumName(*prop.EnumVal) + ")\n")
} else if prop.IconVal != nil {
renderSetIcon(targetName, setterFunc, prop.IconVal, ret)
} else { } else {
ret.WriteString("/* miqt-uic: no handler for " + targetName + " property '" + prop.Name + "' */\n") ret.WriteString("/* miqt-uic: no handler for " + targetName + " property '" + prop.Name + "' */\n")
} }
@ -273,6 +316,10 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
if prop, ok := propertyByName(a.Properties, "shortcut"); ok { if prop, ok := propertyByName(a.Properties, "shortcut"); ok {
ret.WriteString("ui." + a.Name + `.SetShortcut(qt.NewQKeySequence2(` + generateString(prop.StringVal, w.Class) + `))` + "\n") ret.WriteString("ui." + a.Name + `.SetShortcut(qt.NewQKeySequence2(` + generateString(prop.StringVal, w.Class) + `))` + "\n")
} }
if prop, ok := propertyByName(a.Properties, "icon"); ok {
renderSetIcon(a.Name, ".SetIcon", prop.IconVal, &ret)
}
} }
// Items // Items