aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/state-machine
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/state-machine')
-rw-r--r--examples/widgets/state-machine/eventtrans/eventtrans.py1
-rw-r--r--examples/widgets/state-machine/factstates/factstates.py13
-rw-r--r--examples/widgets/state-machine/pingpong/pingpong.py18
-rw-r--r--examples/widgets/state-machine/rogue/rogue.py10
-rw-r--r--examples/widgets/state-machine/trafficlight/trafficlight.py9
5 files changed, 47 insertions, 4 deletions
diff --git a/examples/widgets/state-machine/eventtrans/eventtrans.py b/examples/widgets/state-machine/eventtrans/eventtrans.py
index 8fe67ace8..8ed1ccc9c 100644
--- a/examples/widgets/state-machine/eventtrans/eventtrans.py
+++ b/examples/widgets/state-machine/eventtrans/eventtrans.py
@@ -86,6 +86,7 @@ class MainWindow(QMainWindow):
self.setCentralWidget(button)
self.show()
+
if __name__ == '__main__':
import sys
diff --git a/examples/widgets/state-machine/factstates/factstates.py b/examples/widgets/state-machine/factstates/factstates.py
index 47cf4d841..053e89908 100644
--- a/examples/widgets/state-machine/factstates/factstates.py
+++ b/examples/widgets/state-machine/factstates/factstates.py
@@ -48,49 +48,62 @@ from PySide6.QtStateMachine import (QFinalState, QSignalTransition, QState,
class Factorial(QObject):
x_changed = Signal(int)
+
def __init__(self):
super().__init__()
self.xval = -1
self.facval = 1
+
def get_x(self):
return self.xval
+
def set_x(self, x):
if self.xval == x:
return
self.xval = x
self.x_changed.emit(x)
x = Property(int, get_x, set_x)
+
def get_fact(self):
return self.facval
+
def set_fact(self, fac):
self.facval = fac
+
fac = Property(int, get_fact, set_fact)
+
class FactorialLoopTransition(QSignalTransition):
def __init__(self, fact):
super().__init__(fact, SIGNAL('x_changed(int)'))
self.fact = fact
+
def eventTest(self, e):
if not super(FactorialLoopTransition, self).eventTest(e):
return False
return e.arguments()[0] > 1
+
def onTransition(self, e):
x = e.arguments()[0]
fac = self.fact.fac
self.fact.fac = x * fac
self.fact.x = x - 1
+
class FactorialDoneTransition(QSignalTransition):
def __init__(self, fact):
super().__init__(fact, SIGNAL('x_changed(int)'))
self.fact = fact
+
def eventTest(self, e):
if not super(FactorialDoneTransition, self).eventTest(e):
return False
return e.arguments()[0] <= 1
+
def onTransition(self, e):
print(self.fact.fac)
+
if __name__ == '__main__':
import sys
app = QCoreApplication(sys.argv)
diff --git a/examples/widgets/state-machine/pingpong/pingpong.py b/examples/widgets/state-machine/pingpong/pingpong.py
index 12de2bb69..ec4d7cfec 100644
--- a/examples/widgets/state-machine/pingpong/pingpong.py
+++ b/examples/widgets/state-machine/pingpong/pingpong.py
@@ -47,34 +47,44 @@ from PySide6.QtStateMachine import QAbstractTransition, QState, QStateMachine
class PingEvent(QEvent):
def __init__(self):
- super().__init__(QEvent.Type(QEvent.User+2))
+ super().__init__(QEvent.Type(QEvent.User + 2))
+
+
class PongEvent(QEvent):
def __init__(self):
- super().__init__(QEvent.Type(QEvent.User+3))
+ super().__init__(QEvent.Type(QEvent.User + 3))
+
class Pinger(QState):
def __init__(self, parent):
super().__init__(parent)
+
def onEntry(self, e):
self.p = PingEvent()
self.machine().postEvent(self.p)
print('ping?')
+
class PongTransition(QAbstractTransition):
def eventTest(self, e):
- return e.type() == QEvent.User+3
+ return e.type() == QEvent.User + 3
+
def onTransition(self, e):
self.p = PingEvent()
machine.postDelayedEvent(self.p, 500)
print('ping?')
+
+
class PingTransition(QAbstractTransition):
def eventTest(self, e):
- return e.type() == QEvent.User+2
+ return e.type() == QEvent.User + 2
+
def onTransition(self, e):
self.p = PongEvent()
machine.postDelayedEvent(self.p, 500)
print('pong!')
+
if __name__ == '__main__':
import sys
app = QCoreApplication(sys.argv)
diff --git a/examples/widgets/state-machine/rogue/rogue.py b/examples/widgets/state-machine/rogue/rogue.py
index 67caef1f0..5bbc7dabc 100644
--- a/examples/widgets/state-machine/rogue/rogue.py
+++ b/examples/widgets/state-machine/rogue/rogue.py
@@ -51,6 +51,7 @@ class MovementTransition(QEventTransition):
def __init__(self, window):
super().__init__(window, QEvent.KeyPress)
self.window = window
+
def eventTest(self, event):
if event.type() == QEvent.StateMachineWrapped and \
event.event().type() == QEvent.KeyPress:
@@ -58,6 +59,7 @@ class MovementTransition(QEventTransition):
return key == Qt.Key_2 or key == Qt.Key_8 or \
key == Qt.Key_6 or key == Qt.Key_4
return False
+
def onTransition(self, event):
key = event.event().key()
if key == Qt.Key_4:
@@ -69,6 +71,7 @@ class MovementTransition(QEventTransition):
if key == Qt.Key_2:
self.window.move_player(self.window.down)
+
class Custom(QState):
def __init__(self, parent, mw):
super().__init__(parent)
@@ -77,6 +80,7 @@ class Custom(QState):
def onEntry(self, e):
print(self.mw.status)
+
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
@@ -98,6 +102,7 @@ class MainWindow(QMainWindow):
self.setup_map()
self.build_machine()
self.show()
+
def setup_map(self):
self.map = []
generator = QRandomGenerator().global_()
@@ -150,6 +155,7 @@ class MainWindow(QMainWindow):
metrics = QFontMetrics(self.font())
return QSize(metrics.horizontalAdvance('X') * self.width,
metrics.height() * (self.height + 1))
+
def paintEvent(self, event):
metrics = QFontMetrics(self.font())
painter = QPainter(self)
@@ -171,6 +177,7 @@ class MainWindow(QMainWindow):
painter.drawText(QPoint(x_pos, y_pos), self.map[x][y])
x_pos += font_width
painter.drawText(QPoint(self.pX * font_width, (self.pY + 2) * font_height), '@')
+
def move_player(self, direction):
if direction == self.left:
if self.map[self.pX - 1][self.pY] != '#':
@@ -185,8 +192,10 @@ class MainWindow(QMainWindow):
if self.map[self.pX][self.pY + 1] != '#':
self.pY += 1
self.repaint()
+
def get_status(self):
return self._status_str
+
def set_status(self, status):
self._status_str = status
self.repaint()
@@ -198,6 +207,7 @@ class MainWindow(QMainWindow):
width = 35
height = 20
+
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
diff --git a/examples/widgets/state-machine/trafficlight/trafficlight.py b/examples/widgets/state-machine/trafficlight/trafficlight.py
index e807d7388..3964c4eb8 100644
--- a/examples/widgets/state-machine/trafficlight/trafficlight.py
+++ b/examples/widgets/state-machine/trafficlight/trafficlight.py
@@ -51,19 +51,24 @@ class LightWidget(QWidget):
super().__init__()
self.color = color
self._on_val = False
+
def is_on(self):
return self._on_val
+
def set_on(self, on):
if self._on_val == on:
return
self._on_val = on
self.update()
+
@Slot()
def turn_off(self):
self.set_on(False)
+
@Slot()
def turn_on(self):
self.set_on(True)
+
def paintEvent(self, e):
if not self._on_val:
return
@@ -74,6 +79,7 @@ class LightWidget(QWidget):
on = Property(bool, is_on, set_on)
+
class TrafficLightWidget(QWidget):
def __init__(self):
super().__init__()
@@ -89,6 +95,7 @@ class TrafficLightWidget(QWidget):
self.setPalette(pal)
self.setAutoFillBackground(True)
+
def create_light_state(light, duration, parent=None):
light_state = QState(parent)
timer = QTimer(light_state)
@@ -103,6 +110,7 @@ def create_light_state(light, duration, parent=None):
light_state.setInitialState(timing)
return light_state
+
class TrafficLight(QWidget):
def __init__(self):
super().__init__()
@@ -132,6 +140,7 @@ class TrafficLight(QWidget):
machine.setInitialState(red_going_yellow)
machine.start()
+
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)