Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ examples/libraries/qt6-network-sctp/qt6-network-sctp
examples/libraries/qt6-network/qt6-network
examples/libraries/qt6-pdf/qt6-pdf
examples/libraries/qt6-positioning/qt6-positioning
examples/libraries/qt6-statemachine/qt6-statemachine
examples/libraries/qt6-uitools/qt6-uitools
examples/libraries/qt6-webengine/qt6-webengine
examples/libraries/restricted-extras-charts6/restricted-extras-charts6
Expand Down
15 changes: 15 additions & 0 deletions cmd/genbindings/config-libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,21 @@ func ProcessLibraries(clangBin, outDir, extraLibsDir string) {
ClangMatchSameHeaderDefinitionOnly,
)

// Qt 6 State Machine
// Depends on QtCore and QtQml
generate(
"qt6/statemachine",
[]string{
"/usr/include/x86_64-linux-gnu/qt6/QtStateMachine",
"/usr/include/x86_64-linux-gnu/qt6/QtStateMachineQml",
},
AllowAllHeaders,
clangBin,
"--std=c++17 "+pkgConfigCflags("Qt6StateMachineQml"),
outDir,
ClangMatchSameHeaderDefinitionOnly,
)

// Qt 6 Charts
// Depends on QtCore/Gui/Widgets
generate(
Expand Down
3 changes: 2 additions & 1 deletion docker/genbindings.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
qt6-multimedia-dev \
qt6-pdf-dev \
qt6-positioning-dev \
qt6-tools-dev \
qt6-scxml-dev \
qt6-svg-dev \
qt6-tools-dev \
qt6-webengine-dev \
libqscintilla2-qt5-dev \
libqscintilla2-qt6-dev \
Expand Down
179 changes: 179 additions & 0 deletions examples/libraries/qt6-statemachine/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package main

import (
"math"
"os"

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

type LightWidget struct {
color qt.GlobalColor
on bool
widget *qt.QWidget
}

type TrafficWidget struct {
red *LightWidget
yellow *LightWidget
green *LightWidget
widget *qt.QWidget
}

func NewLightWidget(color qt.GlobalColor) *LightWidget {
var self LightWidget

self.color = color
self.on = false
self.widget = qt.NewQWidget2()

self.widget.OnPaintEvent(func(super func(event *qt.QPaintEvent), event *qt.QPaintEvent) {
if !self.on {
return
}

painter := qt.NewQPainter2(self.widget.QPaintDevice)
defer painter.Delete()

brush := qt.NewQBrush4(self.color)
defer brush.Delete()

painter.SetRenderHint(qt.QPainter__Antialiasing)
painter.SetBrush(brush)

height := self.widget.Height()
width := self.widget.Width()
minVal := int(math.Min(float64(height), float64(width)))
size := (minVal * 2) / 3
x := (width - size) / 2
y := (height - size) / 2

painter.DrawEllipse2(x, y, size, size)
})

return &self
}

func (this *LightWidget) IsOn() bool {
return this.on
}

func (this *LightWidget) SetOn(on bool) {
if this.on == on {
return
}

this.on = on

this.widget.Update()
}

func (this *LightWidget) TurnOff() {
this.SetOn(false)
}

func (this *LightWidget) TurnOn() {
this.SetOn(true)
}

func NewTrafficWidget() *TrafficWidget {
var self TrafficWidget

self.red = NewLightWidget(qt.Red)
self.yellow = NewLightWidget(qt.Yellow)
self.green = NewLightWidget(qt.Green)

self.widget = qt.NewQWidget2()

layout := qt.NewQVBoxLayout(self.widget)
layout.AddWidget(self.red.widget)
layout.AddWidget(self.yellow.widget)
layout.AddWidget(self.green.widget)

palette := qt.NewQPalette()
defer palette.Delete()

color := qt.NewQColor2(qt.Black)
defer color.Delete()

palette.SetColor2(qt.QPalette__Window, color)
self.widget.SetPalette(palette)
self.widget.SetAutoFillBackground(true)

return &self
}

func (this *TrafficWidget) RedLight() *LightWidget {
return this.red
}

func (this *TrafficWidget) YellowLight() *LightWidget {
return this.yellow
}

func (this *TrafficWidget) GreenLight() *LightWidget {
return this.green
}

func CreateLightState(light *LightWidget, duration int) *statemachine.QState {
lightState := statemachine.NewQState()
timing := statemachine.NewQState3(lightState)

timer := qt.NewQTimer2(lightState.QObject)
timer.SetInterval(duration)
timer.SetSingleShot(true)

timing.OnEntered(func() {
light.TurnOn()
timer.Start2()
})

timing.OnExited(func() {
light.TurnOff()
})

done := statemachine.NewQFinalState2(lightState)
timing.AddTransition2(timer.QObject, "timeout()", done.QAbstractState)

lightState.SetInitialState(timing.QAbstractState)

return lightState
}

func main() {
qt.NewQApplication(os.Args)

trafficLight := qt.NewQWidget2()
defer trafficLight.Delete()

trafficLight.SetWindowTitle("Qt 6 State Machine Example")
trafficLight.Resize(300, 800)
trafficLight.SetMinimumHeight(450)
trafficLight.SetMinimumWidth(200)

layout := qt.NewQVBoxLayout(trafficLight)
trafficWidget := NewTrafficWidget()

layout.AddWidget(trafficWidget.widget)
layout.SetContentsMargins(0, 0, 0, 0)

redGoingGreen := CreateLightState(trafficWidget.RedLight(), 3000)
greenGoingYellow := CreateLightState(trafficWidget.GreenLight(), 3000)
yellowGoingRed := CreateLightState(trafficWidget.YellowLight(), 1000)

redGoingGreen.AddTransition2(redGoingGreen.QObject, "finished()", greenGoingYellow.QAbstractState)
greenGoingYellow.AddTransition2(greenGoingYellow.QObject, "finished()", yellowGoingRed.QAbstractState)
yellowGoingRed.AddTransition2(yellowGoingRed.QObject, "finished()", redGoingGreen.QAbstractState)

machine := statemachine.NewQStateMachine3(trafficLight.QObject)
machine.AddState(redGoingGreen.QAbstractState)
machine.AddState(greenGoingYellow.QAbstractState)
machine.AddState(yellowGoingRed.QAbstractState)
machine.SetInitialState(redGoingGreen.QAbstractState)
machine.Start()

trafficLight.Show()

qt.QApplication_Exec()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions qt6/statemachine/cflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package statemachine

/*
#cgo pkg-config: Qt6StateMachine Qt6StateMachineQml
*/
import "C"
108 changes: 108 additions & 0 deletions qt6/statemachine/gen_qabstractstate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <QAbstractState>
#include <QEvent>
#include <QMetaMethod>
#include <QMetaObject>
#include <QObject>
#include <QState>
#include <QStateMachine>
#include <QString>
#include <QByteArray>
#include <cstring>
#include <qabstractstate.h>
#include "gen_qabstractstate.h"

#ifdef __cplusplus
extern "C" {
#endif

void miqt_exec_callback_QAbstractState_activeChanged(intptr_t, bool);
void miqt_exec_callback_QAbstractState_entered(intptr_t);
void miqt_exec_callback_QAbstractState_exited(intptr_t);
#ifdef __cplusplus
} /* extern C */
#endif

void QAbstractState_virtbase(QAbstractState* src, QObject** outptr_QObject) {
*outptr_QObject = static_cast<QObject*>(src);
}

QMetaObject* QAbstractState_metaObject(const QAbstractState* self) {
return (QMetaObject*) self->metaObject();
}

void* QAbstractState_metacast(QAbstractState* self, const char* param1) {
return self->qt_metacast(param1);
}

struct miqt_string QAbstractState_tr(const char* s) {
QString _ret = QAbstractState::tr(s);
// Convert QString from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory
QByteArray _b = _ret.toUtf8();
struct miqt_string _ms;
_ms.len = _b.length();
_ms.data = static_cast<char*>(malloc(_ms.len));
memcpy(_ms.data, _b.data(), _ms.len);
return _ms;
}

QState* QAbstractState_parentState(const QAbstractState* self) {
return self->parentState();
}

QStateMachine* QAbstractState_machine(const QAbstractState* self) {
return self->machine();
}

bool QAbstractState_active(const QAbstractState* self) {
return self->active();
}

void QAbstractState_activeChanged(QAbstractState* self, bool active) {
self->activeChanged(active);
}

void QAbstractState_connect_activeChanged(QAbstractState* self, intptr_t slot) {
QAbstractState::connect(self, static_cast<void (QAbstractState::*)(bool)>(&QAbstractState::activeChanged), self, [=](bool active) {
bool sigval1 = active;
miqt_exec_callback_QAbstractState_activeChanged(slot, sigval1);
});
}

struct miqt_string QAbstractState_tr2(const char* s, const char* c) {
QString _ret = QAbstractState::tr(s, c);
// Convert QString from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory
QByteArray _b = _ret.toUtf8();
struct miqt_string _ms;
_ms.len = _b.length();
_ms.data = static_cast<char*>(malloc(_ms.len));
memcpy(_ms.data, _b.data(), _ms.len);
return _ms;
}

struct miqt_string QAbstractState_tr3(const char* s, const char* c, int n) {
QString _ret = QAbstractState::tr(s, c, static_cast<int>(n));
// Convert QString from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory
QByteArray _b = _ret.toUtf8();
struct miqt_string _ms;
_ms.len = _b.length();
_ms.data = static_cast<char*>(malloc(_ms.len));
memcpy(_ms.data, _b.data(), _ms.len);
return _ms;
}

void QAbstractState_connect_entered(QAbstractState* self, intptr_t slot) {
QAbstractState::connect(self, &QAbstractState::entered, self, [=]() {
miqt_exec_callback_QAbstractState_entered(slot);
});
}

void QAbstractState_connect_exited(QAbstractState* self, intptr_t slot) {
QAbstractState::connect(self, &QAbstractState::exited, self, [=]() {
miqt_exec_callback_QAbstractState_exited(slot);
});
}

void QAbstractState_delete(QAbstractState* self) {
delete self;
}

Loading