2024-09-20 06:39:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-28 01:04:31 +00:00
|
|
|
"fmt"
|
2024-09-20 06:39:01 +00:00
|
|
|
"go/format"
|
2024-09-28 01:04:31 +00:00
|
|
|
"strconv"
|
2024-09-20 06:39:01 +00:00
|
|
|
"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 {
|
2024-09-28 01:04:31 +00:00
|
|
|
ret = append(ret, u.Layout.Name+" *qt."+u.Layout.Class)
|
2024-09-20 06:39:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
func generateString(s *UiString, parentClass string) string {
|
|
|
|
if s.Notr || parentClass == "" {
|
2024-09-28 01:04:31 +00:00
|
|
|
return strconv.Quote(s.Value)
|
2024-09-20 06:39:01 +00:00
|
|
|
}
|
2024-09-28 02:27:19 +00:00
|
|
|
return `qt.` + parentClass + `_Tr(` + strconv.Quote(s.Value) + `)`
|
2024-09-28 01:04:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
// qwidgetName creates the T.QWidget name that MIQT needs to access the base class.
|
|
|
|
func qwidgetName(name string, class string) string {
|
|
|
|
if name == "" {
|
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
if class == "QWidget" {
|
|
|
|
return name // It's already the right type
|
|
|
|
}
|
|
|
|
return name + ".QWidget"
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateWidget(w UiWidget, parentName string, parentClass string) (string, error) {
|
2024-09-28 01:04:31 +00:00
|
|
|
ret := strings.Builder{}
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
ctor, ok := constructorFunctionFor(w.Class)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("No known constructor function for %q class %q", w.Name, w.Class)
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:04:31 +00:00
|
|
|
ret.WriteString(`
|
2024-09-28 02:27:19 +00:00
|
|
|
ui.` + w.Name + ` = qt.` + ctor + `(` + qwidgetName(parentName, parentClass) + `)
|
2024-09-28 01:04:31 +00:00
|
|
|
ui.` + w.Name + `.SetObjectName(` + strconv.Quote(w.Name) + `)
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
for _, prop := range w.Properties {
|
|
|
|
setterFunc := `.Set` + strings.ToUpper(string(prop.Name[0])) + prop.Name[1:]
|
|
|
|
|
|
|
|
if prop.Name == "geometry" {
|
|
|
|
if !(prop.RectVal.X == 0 && prop.RectVal.Y == 0) {
|
|
|
|
// Set all 4x properties
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.SetGeometry(qt.NewQRect(` + fmt.Sprintf("%d, %d, %d, %d", prop.RectVal.X, prop.RectVal.Y, prop.RectVal.Width, prop.RectVal.Height) + "))\n")
|
|
|
|
|
|
|
|
} else if !(prop.RectVal.Width == 0 && prop.RectVal.Height == 0) {
|
|
|
|
// Only width/height were supplied
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.Resize(` + fmt.Sprintf("%d, %d", prop.RectVal.Width, prop.RectVal.Height) + ")\n")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if prop.StringVal != nil {
|
|
|
|
// "windowTitle", "title", "text"
|
2024-09-28 02:27:19 +00:00
|
|
|
ret.WriteString(`ui.` + w.Name + setterFunc + `(` + generateString(prop.StringVal, parentClass) + ")\n")
|
2024-09-28 01:04:31 +00:00
|
|
|
|
|
|
|
} else if prop.EnumVal != nil {
|
|
|
|
// "frameShape"
|
|
|
|
ret.WriteString(`ui.` + w.Name + setterFunc + `(qt.` + strings.Replace(*prop.EnumVal, `::`, `__`, -1) + ")\n")
|
|
|
|
|
|
|
|
} else {
|
2024-09-28 02:39:56 +00:00
|
|
|
ret.WriteString("/* miqt-uic: no handler for " + w.Name + " property '" + prop.Name + "' */\n")
|
2024-09-28 01:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
// Attributes
|
2024-09-28 02:39:56 +00:00
|
|
|
|
|
|
|
for _, attr := range w.Attributes {
|
|
|
|
if parentClass == "QTabWidget" && attr.Name == "title" {
|
|
|
|
ret.WriteString(parentName + `.SetTabText(` + parentName + ".IndexOf(ui." + w.Name + "), " + generateString(attr.StringVal, parentClass) + ")\n")
|
|
|
|
|
|
|
|
} else if w.Class == "QDockWidget" && parentClass == "QMainWindow" && attr.Name == "dockWidgetArea" {
|
|
|
|
ret.WriteString(parentName + `.AddDockWidget(qt.DockWidgetArea(` + *attr.NumberVal + `), ui.` + w.Name + `)` + "\n")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ret.WriteString("/* miqt-uic: no handler for " + w.Name + " attribute '" + attr.Name + "' */\n")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 02:27:19 +00:00
|
|
|
// TODO
|
|
|
|
// w.Attributes
|
|
|
|
|
2024-09-28 01:04:31 +00:00
|
|
|
// Layout
|
|
|
|
if w.Layout != nil {
|
2024-09-28 02:27:19 +00:00
|
|
|
ctor, ok := constructorFunctionFor(w.Layout.Class)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("No known constructor function for %q class %q", w.Layout.Name, w.Layout.Class)
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:04:31 +00:00
|
|
|
ret.WriteString(`
|
2024-09-28 02:27:19 +00:00
|
|
|
ui.` + w.Layout.Name + ` = qt.` + ctor + `(` + qwidgetName("ui."+w.Name, w.Class) + `)
|
2024-09-28 01:04:31 +00:00
|
|
|
ui.` + w.Layout.Name + `.SetObjectName(` + strconv.Quote(w.Layout.Name) + `)
|
|
|
|
`)
|
|
|
|
|
|
|
|
for _, child := range w.Layout.Items {
|
|
|
|
|
|
|
|
// Layout items have the parent as the real QWidget parent and are
|
|
|
|
// separately assigned to the layout afterwards
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
nest, err := generateWidget(child.Widget, `ui.`+w.Name, w.Class)
|
2024-09-28 01:04:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(w.Name+": %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(nest)
|
|
|
|
|
|
|
|
// Assign to layout
|
|
|
|
|
|
|
|
switch w.Layout.Class {
|
|
|
|
case `QFormLayout`:
|
|
|
|
// Row and Column are always populated.
|
|
|
|
rowPos := fmt.Sprintf("%d", *child.Row)
|
|
|
|
var colPos string
|
|
|
|
if *child.Column == 0 {
|
2024-09-28 02:27:19 +00:00
|
|
|
colPos = `qt.QFormLayout__LabelRole`
|
2024-09-28 01:04:31 +00:00
|
|
|
} else if *child.Column == 1 {
|
2024-09-28 02:27:19 +00:00
|
|
|
colPos = `qt.QFormLayout__FieldRole`
|
2024-09-28 01:04:31 +00:00
|
|
|
} else {
|
|
|
|
ret.WriteString("/* miqt-uic: QFormLayout does not understand column index */\n")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// For QFormLayout it's SetWidget
|
|
|
|
ret.WriteString(`
|
2024-09-28 02:27:19 +00:00
|
|
|
ui.` + w.Layout.Name + `.SetWidget(` + rowPos + `, ` + colPos + `, ` + qwidgetName(`ui.`+child.Widget.Name, child.Widget.Class) + `)
|
2024-09-28 01:04:31 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
case `QGridLayout`:
|
2024-09-28 02:27:19 +00:00
|
|
|
// For QGridLayout it's AddWidget2
|
2024-09-28 01:04:31 +00:00
|
|
|
// FIXME in Miqt this function has optionals, needs to be called with the correct arity
|
|
|
|
// TODO support rowSpan, columnSpan
|
|
|
|
ret.WriteString(`
|
2024-09-28 02:27:19 +00:00
|
|
|
ui.` + w.Layout.Name + `.AddWidget2(` + qwidgetName(`ui.`+child.Widget.Name, child.Widget.Class) + `, ` + fmt.Sprintf("%d, %d", *child.Row, *child.Column) + `)
|
|
|
|
`)
|
|
|
|
|
|
|
|
case "QVBoxLayout", "QHBoxLayout":
|
|
|
|
// For box layout it's AddWidget
|
|
|
|
ret.WriteString(`
|
|
|
|
ui.` + w.Layout.Name + `.AddWidget(` + qwidgetName(`ui.`+child.Widget.Name, child.Widget.Class) + `)
|
2024-09-28 01:04:31 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret.WriteString("/* miqt-uic: no handler for layout '" + w.Layout.Class + "' */\n")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
for _, a := range w.Actions {
|
|
|
|
ret.WriteString(`
|
|
|
|
ui.` + a.Name + ` = qt.NewQAction(` + parentName + `)
|
|
|
|
ui.` + a.Name + `.SetObjectName(` + strconv.Quote(a.Name) + `)
|
|
|
|
`)
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
// QActions are translated in the parent window's context
|
|
|
|
if prop, ok := propertyByName(a.Properties, "text"); ok {
|
|
|
|
ret.WriteString("ui." + a.Name + `.SetText(` + generateString(prop.StringVal, w.Class) + `)` + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if prop, ok := propertyByName(a.Properties, "shortcut"); ok {
|
|
|
|
ret.WriteString("ui." + a.Name + `.SetShortcut(qt.NewQKeySequence2(` + generateString(prop.StringVal, w.Class) + `))` + "\n")
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 01:04:31 +00:00
|
|
|
|
|
|
|
// Items
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
for itemNo, itm := range w.Items {
|
|
|
|
ret.WriteString("ui." + w.Name + `.AddItem("")` + "\n")
|
2024-09-28 01:04:31 +00:00
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
// Check for a "text" property and update the item's text
|
|
|
|
// Do this as a 2nd step so that the SetItemText can be trapped for retranslateUi()
|
|
|
|
// TODO Abstract for all SetItem{Foo} properties
|
|
|
|
if prop, ok := propertyByName(itm.Properties, "text"); ok {
|
|
|
|
ret.WriteString("ui." + w.Name + `.SetItemText(` + fmt.Sprintf("%d", itemNo) + `, ` + generateString(prop.StringVal, w.Class) + `)` + "\n")
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 01:04:31 +00:00
|
|
|
|
|
|
|
// Columns
|
|
|
|
// TODO
|
|
|
|
// w.Columns
|
|
|
|
|
|
|
|
// Recurse children
|
2024-09-28 02:27:19 +00:00
|
|
|
var (
|
|
|
|
setCentralWidget = false
|
|
|
|
setMenuBar = false
|
|
|
|
setStatusBar = false
|
|
|
|
)
|
|
|
|
|
2024-09-28 01:04:31 +00:00
|
|
|
for _, child := range w.Widgets {
|
2024-09-28 02:27:19 +00:00
|
|
|
nest, err := generateWidget(child, `ui.`+w.Name, w.Class)
|
2024-09-28 01:04:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(w.Name+": %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(nest)
|
|
|
|
|
|
|
|
// QMainWindow CentralWidget handling
|
|
|
|
// The first listed class can be the central widget.
|
|
|
|
// TODO should it be the first child with a layout? But need to handle windows with no layout
|
2024-09-28 02:27:19 +00:00
|
|
|
if w.Class == `QMainWindow` && !setCentralWidget {
|
2024-09-28 01:04:31 +00:00
|
|
|
ret.WriteString(`ui.` + w.Name + `.SetCentralWidget(ui.` + child.Name + ") // Set central widget \n")
|
2024-09-28 02:27:19 +00:00
|
|
|
setCentralWidget = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// QDockWidget also has something like a central widget
|
|
|
|
if w.Class == `QDockWidget` && !setCentralWidget {
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.SetWidget(ui.` + child.Name + ") // Set central widget \n")
|
|
|
|
setCentralWidget = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Class == "QMainWindow" && child.Class == "QMenuBar" && !setMenuBar {
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.SetMenuBar(ui.` + child.Name + `)` + "\n")
|
|
|
|
setMenuBar = true
|
|
|
|
}
|
|
|
|
if w.Class == "QMainWindow" && child.Class == "QStatusBar" && !setStatusBar {
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.SetStatusBar(ui.` + child.Name + `)` + "\n")
|
|
|
|
setStatusBar = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// QTabWidget->QTab handling
|
|
|
|
if w.Class == `QTabWidget` {
|
|
|
|
ret.WriteString(`ui.` + w.Name + `.AddTab(` + qwidgetName(`ui.`+child.Name, child.Class) + `, "")` + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddActions
|
|
|
|
// n.b. This must be *after* all children have been constructed, in case we
|
|
|
|
// are adding a direct child
|
|
|
|
|
|
|
|
for _, a := range w.AddActions {
|
|
|
|
if a.Name == "separator" {
|
|
|
|
// TODO how does Qt Designer disambiguate a real QAction with name="separator" ?
|
|
|
|
ret.WriteString("ui." + w.Name + ".AddSeparator()\n")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If we are a menubar, then <addaction> refers to top-level QMenu instead of QAction
|
|
|
|
if w.Class == "QMenuBar" {
|
|
|
|
ret.WriteString("ui." + w.Name + ".AddMenu(ui." + a.Name + ")\n")
|
|
|
|
} else if w.Class == "QMenu" {
|
|
|
|
// QMenu has its own .AddAction() implementation that takes plain string
|
|
|
|
// That's convenient, but it shadows the AddAction version that takes a QAction*
|
|
|
|
// We need to use the underlying QWidget.AddAction explicitly
|
|
|
|
ret.WriteString("ui." + w.Name + ".QWidget.AddAction(ui." + a.Name + ")\n")
|
|
|
|
} else {
|
|
|
|
ret.WriteString("ui." + w.Name + ".AddAction(ui." + a.Name + ")\n")
|
|
|
|
}
|
2024-09-28 01:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.String(), nil
|
2024-09-20 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 06:51:47 +00:00
|
|
|
func generate(packageName string, goGenerateArgs string, u UiFile) ([]byte, error) {
|
2024-09-20 06:39:01 +00:00
|
|
|
ret := strings.Builder{}
|
2024-09-20 06:51:47 +00:00
|
|
|
ret.WriteString(`// Generated by miqt-uic. To update this file, edit the .ui file in
|
|
|
|
// Qt Designer, and then run 'go generate'.
|
|
|
|
//
|
|
|
|
//go:` + `generate miqt-uic ` + goGenerateArgs + `
|
|
|
|
|
|
|
|
package ` + packageName + `
|
2024-09-20 06:39:01 +00:00
|
|
|
|
|
|
|
import (
|
2024-09-28 02:27:19 +00:00
|
|
|
"github.com/mappu/miqt/qt"
|
2024-09-20 06:39:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ` + u.Class + `Ui struct {
|
2024-09-28 01:04:31 +00:00
|
|
|
` + strings.Join(collectClassNames_Widget(u.Widget), "\n") + `
|
2024-09-20 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 01:04:31 +00:00
|
|
|
// New` + u.Class + `Ui creates all Qt widget classes for ` + u.Class + `.
|
2024-09-20 06:39:01 +00:00
|
|
|
func New` + u.Class + `Ui() *` + u.Class + `Ui {
|
|
|
|
ui := &` + u.Class + `Ui{}
|
|
|
|
`)
|
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
nest, err := generateWidget(u.Widget, "", "")
|
2024-09-28 01:04:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-20 06:39:01 +00:00
|
|
|
|
2024-09-28 02:27:19 +00:00
|
|
|
// Don't emit any of the lines that included .Tr(), move them into the
|
|
|
|
// retranslateUi() function
|
|
|
|
var translateFunc []string
|
2024-09-28 01:04:31 +00:00
|
|
|
for _, line := range strings.Split(nest, "\n") {
|
2024-09-28 02:27:19 +00:00
|
|
|
if strings.Contains(line, `_Tr(`) {
|
|
|
|
translateFunc = append(translateFunc, line)
|
|
|
|
} else {
|
2024-09-28 01:04:31 +00:00
|
|
|
ret.WriteString(line + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(`
|
2024-09-28 02:27:19 +00:00
|
|
|
ui.Retranslate()
|
|
|
|
|
|
|
|
return ui
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retranslate reapplies all text translations.
|
|
|
|
func (ui *` + u.Class + `Ui) Retranslate() {
|
|
|
|
` + strings.Join(translateFunc, "\n") + `
|
2024-09-20 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
output := ret.String()
|
2024-09-28 01:04:31 +00:00
|
|
|
|
|
|
|
formatted, err := format.Source([]byte(output))
|
|
|
|
if err != nil {
|
|
|
|
// Return unformatted so it can be fixed
|
|
|
|
return []byte(output), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatted, nil
|
2024-09-20 06:39:01 +00:00
|
|
|
}
|