uic: initial commit

This commit is contained in:
mappu 2024-09-20 18:39:01 +12:00
parent 2a28c17a64
commit 691b7c42ab
3 changed files with 181 additions and 0 deletions

34
cmd/miqt-uic/main.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
)
func main() {
in, err := ioutil.ReadFile(`test.ui`)
if err != nil {
panic(err)
}
var parsed UiFile
err = xml.Unmarshal(in, &parsed)
if err != nil {
panic(err)
}
ret, err := xml.MarshalIndent(parsed, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(ret))
gosrc, err := generate("main", parsed)
if err != nil {
panic(err)
}
fmt.Println(string(gosrc))
}

82
cmd/miqt-uic/types.go Normal file
View File

@ -0,0 +1,82 @@
package main
import (
"encoding/xml"
)
type UiLayoutItem struct {
Row *int `xml:"row,attr"`
Column *int `xml:"column,attr"`
Widget UiWidget `xml:"widget"`
}
type UiLayout struct {
Class string `xml:"class,attr"`
Name string `xml:"name,attr"`
Items []UiLayoutItem `xml:"item"`
}
type UiPropertyContainer struct {
Properties []UiProperty `xml:"property"`
}
type UiWidget struct {
Class string `xml:"class,attr"`
Name string `xml:"name,attr"`
Properties []UiProperty `xml:"property"`
Attributes []UiProperty `xml:"attribute"`
Layout *UiLayout `xml:"layout,omitempty"`
Widgets []UiWidget `xml:"widget"` // If no layout
Columns []UiPropertyContainer `xml:"column"` // e.g. for QTreeWidget
Items []UiPropertyContainer `xml:"item"` // e.g. for QComboBox
AddActions []UiActionReference `xml:"addaction"`
Actions []UiAction `xml:"action"`
}
type UiRect struct {
X int `xml:"x"`
Y int `xml:"y"`
Width int `xml:"width"`
Height int `xml:"height"`
}
type UiString struct {
Value string `xml:",chardata"`
Notr bool `xml:"notr,attr,omitempty"`
}
type UiProperty struct {
Name string `xml:"name,attr"`
StringVal *UiString `xml:"string,omitempty"`
NumberVal *string `xml:"number,omitempty"` // Preserve as string literal
EnumVal *string `xml:"enum,omitempty"`
RectVal *UiRect `xml:"rect,omitempty"`
}
type UiActionReference struct {
Name string `xml:"name,attr"`
}
type UiAction struct {
Name string `xml:"name,attr"`
Properties []UiProperty `xml:"property"`
}
type UiResources struct {
}
type UiConnections struct {
}
type UiFile struct {
XMLName xml.Name // should always be xml.Name{Local: "ui"}
Class string `xml:"class"`
Version string `xml:"version,attr"` // e.g. 4.0
Widget []UiWidget `xml:"widget"`
Resources UiResources `xml:"resources"`
Connections UiConnections `xml:"connections"`
}

65
cmd/miqt-uic/ui2go.go Normal file
View File

@ -0,0 +1,65 @@
package main
import (
"go/format"
"strings"
)
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)...)
}
if u.Layout != nil {
for _, li := range u.Layout.Items {
ret = append(ret, collectClassNames_Widget(li.Widget)...)
}
}
for _, a := range u.Actions {
ret = append(ret, a.Name+" *qt.QAction")
}
return ret
}
func collectClassNames(u UiFile) []string {
var ret []string
for _, w := range u.Widget {
ret = append(ret, collectClassNames_Widget(w)...)
}
return ret
}
func generate(packageName string, u UiFile) ([]byte, error) {
ret := strings.Builder{}
ret.WriteString(`package ` + packageName + `
import (
"github.com/mappu/miqt"
)
type ` + u.Class + `Ui struct {
` + strings.Join(collectClassNames(u), "\n") + `
}
func New` + u.Class + `Ui() *` + u.Class + `Ui {
ui := &` + u.Class + `Ui{}
`)
// ...
ret.WriteString(`
return ui
}
func (u *` + u.Class + `Ui) RetranslateUi() {
panic("TODO")
}
`)
output := ret.String()
return format.Source([]byte(output))
}