Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Change constructor overload ordering, to simplify UIC #54

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions cmd/genbindings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func generate(packageName string, srcDirs []string, clangBin, cflagsCombined, ou
astTransformChildClasses(parsed) // must be first
astTransformOptional(parsed)
astTransformOverloads(parsed)
astTransformConstructorOrder(parsed)
atr.Process(parsed)

// Update global state tracker (AFTER astTransformChildClasses)
Expand Down
38 changes: 38 additions & 0 deletions cmd/genbindings/transformctors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"sort"
)

// astTransformConstructorOrder creates a canonical ordering for constructors
// where the 0th entry is any entry taking solely a QWidget* parameter.
// This is so that UIC can safely assume this for types.
// @ref https://github.com/mappu/miqt/issues/46
func astTransformConstructorOrder(parsed *CppParsedHeader) {

// QFoo(QWidget* parent);
checkIsDefaultCtor := func(candidate CppMethod) bool {
return len(candidate.Parameters) == 1 &&
candidate.Parameters[0].ParameterType == "QWidget" &&
candidate.Parameters[0].Pointer
}

// Iterate all classes
for i, c := range parsed.Classes {

// Sort
sort.SliceStable(c.Ctors, func(i, j int) bool {
ic := checkIsDefaultCtor(c.Ctors[i])
jc := checkIsDefaultCtor(c.Ctors[j])

if ic && !jc {
return true
}
return false
})

// Persist mutation
parsed.Classes[i] = c

}
}
89 changes: 89 additions & 0 deletions cmd/miqt-rcc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// miqt-rcc compiles a Qt resource XML file (*.qrc) to a binary resource file and
// creates a Go stub to load it.
package main

import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"strings"
)

func main() {

// Parse arguments

input := flag.String("Input", "", "Path to .qrc input file")
outputRcc := flag.String("OutputRcc", "", "(Optional) Path to .rcc output file. If omitted, inferred from the input file path")
outputGo := flag.String("OutputGo", "", "(Optional) Path to .go output file. If omitted, interred from the input file path")
packageName := flag.String("Package", "main", "Package to use in generated Go files")
variableName := flag.String("VariableName", "_resourceRcc", "Temporary global variable name for loading embedded data")
flag.Parse()

// Check if input file exists

if _, err := os.Stat(*input); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Input file '%s' not found\n", *input)
os.Exit(1)
}

// Fill in default output names, if not specified

if *outputRcc == "" {
*outputRcc = strings.TrimSuffix(*input, `.qrc`) + `.rcc`
}
if *outputGo == "" {
*outputGo = strings.TrimSuffix(*input, `.qrc`) + `.go`
}

// Compile qrc to binary resource file

rccCmd := exec.Command(`rcc`, `--binary`, `-o`, *outputRcc, *input)
rccCmd.Stderr = os.Stderr
rccCmd.Stdout = os.Stdout
err := rccCmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "rcc: %s\n", err.Error())
os.Exit(1)
}

// Create Go file that loads the resource

goSrcData := `
package ` + *packageName + `

//go:generate miqt-rcc "` + strings.Join(os.Args[1:], `" "`) + `"

import (
"embed"

"github.com/mappu/miqt/qt"
)

//go:embed ` + *outputRcc + `
var ` + *variableName + ` []byte

func init() {
_ = embed.FS{}
qt.QResource_RegisterResourceWithRccData(& ` + *variableName + `[0])
}

`

outputData, err := format.Source([]byte(goSrcData))
if err != nil {
panic(err)
}

err = ioutil.WriteFile(*outputGo, outputData, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Writing to '%s': %s\n", *outputGo, err.Error())
os.Exit(1)
}

fmt.Println("RCC OK")
os.Exit(0)
}
Binary file added cmd/miqt-rcc/miqt-rcc
Binary file not shown.
55 changes: 0 additions & 55 deletions cmd/miqt-rcc/miqt-rcc.sh

This file was deleted.

169 changes: 0 additions & 169 deletions cmd/miqt-uic/constructors.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/miqt-uic/ui2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func renderProperties(properties []UiProperty, ret *strings.Builder, targetName,
func generateWidget(w UiWidget, parentName string, parentClass string) (string, error) {
ret := strings.Builder{}

ctor := constructorFunctionFor(w.Class)
ctor := "New" + w.Class

ret.WriteString(`
ui.` + w.Name + ` = qt.` + ctor + `(` + qwidgetName(parentName, parentClass) + `)
Expand Down Expand Up @@ -244,7 +244,7 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
// Layout

if w.Layout != nil {
ctor := constructorFunctionFor(w.Layout.Class)
ctor := "New" + w.Layout.Class

ret.WriteString(`
ui.` + w.Layout.Name + ` = qt.` + ctor + `(` + qwidgetName("ui."+w.Name, w.Class) + `)
Expand Down
2 changes: 1 addition & 1 deletion examples/android/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func myRealMainFunc() {

qt.NewQApplication(os.Args)

btn := qt.NewQPushButton2("Hello world!")
btn := qt.NewQPushButton3("Hello world!")
btn.SetFixedWidth(320)

var counter int = 0
Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {

qt.NewQApplication(os.Args)

btn := qt.NewQPushButton2("Hello world!")
btn := qt.NewQPushButton3("Hello world!")
btn.SetFixedWidth(320)

var counter int = 0
Expand Down
Loading