aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/tutorials/cannon/t12.py
diff options
context:
space:
mode:
authorFriedemann Kleint <[email protected]>2021-03-24 11:23:58 +0100
committerFriedemann Kleint <[email protected]>2021-03-24 11:46:30 +0100
commit76043639abc918feafe8d59c6019775e7e7684cd (patch)
tree2f3b516ab229b46ad2e8f75b65d0feadda9cc548 /examples/widgets/tutorials/cannon/t12.py
parentca2b448344b3b1ed8715fba85ddbb3be9f766fbb (diff)
Port the widget tutorials to snake case
Task-number: PYSIDE-1112 Change-Id: I60056e1cf0bac1279b7ff780fba908643339847e Reviewed-by: Christian Tismer <[email protected]>
Diffstat (limited to 'examples/widgets/tutorials/cannon/t12.py')
-rw-r--r--examples/widgets/tutorials/cannon/t12.py178
1 files changed, 89 insertions, 89 deletions
diff --git a/examples/widgets/tutorials/cannon/t12.py b/examples/widgets/tutorials/cannon/t12.py
index b94a28688..411cbba41 100644
--- a/examples/widgets/tutorials/cannon/t12.py
+++ b/examples/widgets/tutorials/cannon/t12.py
@@ -49,7 +49,7 @@ from PySide6 import QtCore, QtGui, QtWidgets
class LCDRange(QtWidgets.QWidget):
- valueChanged = QtCore.Signal(int)
+ value_changed = QtCore.Signal(int)
def __init__(self, text=None, parent=None):
if isinstance(text, QtWidgets.QWidget):
parent = text
@@ -60,7 +60,7 @@ class LCDRange(QtWidgets.QWidget):
self.init()
if text:
- self.setText(text)
+ self.set_text(text)
def init(self):
lcd = QtWidgets.QLCDNumber(2)
@@ -87,13 +87,13 @@ class LCDRange(QtWidgets.QWidget):
return self.slider.value()
@QtCore.Slot(int)
- def setValue(self, value):
+ def set_value(self, value):
self.slider.setValue(value)
def text(self):
return self.label.text()
- def setRange(self, minValue, maxValue):
+ def set_range(self, minValue, maxValue):
if minValue < 0 or maxValue > 99 or minValue > maxValue:
QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n"
"\tRange must be 0..99\n"
@@ -102,70 +102,70 @@ class LCDRange(QtWidgets.QWidget):
self.slider.setRange(minValue, maxValue)
- def setText(self, text):
+ def set_text(self, text):
self.label.setText(text)
class CannonField(QtWidgets.QWidget):
- angleChanged = QtCore.Signal(int)
- forceChanged = QtCore.Signal(int)
+ angle_changed = QtCore.Signal(int)
+ force_changed = QtCore.Signal(int)
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
- self.currentAngle = 45
- self.currentForce = 0
- self.timerCount = 0
- self.autoShootTimer = QtCore.QTimer(self)
- self.connect(self.autoShootTimer, QtCore.SIGNAL("timeout()"),
- self.moveShot)
- self.shootAngle = 0
- self.shootForce = 0
+ self._current_angle = 45
+ self._current_force = 0
+ self._timer_count = 0
+ self._auto_shoot_timer = QtCore.QTimer(self)
+ self.connect(self._auto_shoot_timer, QtCore.SIGNAL("timeout()"),
+ self.move_shot)
+ self._shoot_angle = 0
+ self._shoot_force = 0
self.target = QtCore.QPoint(0, 0)
self.setPalette(QtGui.QPalette(QtGui.QColor(250, 250, 200)))
self.setAutoFillBackground(True)
- self.newTarget()
+ self.new_target()
def angle(self):
- return self.currentAngle
+ return self._current_angle
@QtCore.Slot(int)
- def setAngle(self, angle):
+ def set_angle(self, angle):
if angle < 5:
angle = 5
if angle > 70:
angle = 70
- if self.currentAngle == angle:
+ if self._current_angle == angle:
return
- self.currentAngle = angle
+ self._current_angle = angle
self.update()
- self.emit(QtCore.SIGNAL("angleChanged(int)"), self.currentAngle)
+ self.emit(QtCore.SIGNAL("angleChanged(int)"), self._current_angle)
def force(self):
- return self.currentForce
+ return self._current_force
@QtCore.Slot(int)
- def setForce(self, force):
+ def set_force(self, force):
if force < 0:
force = 0
- if self.currentForce == force:
+ if self._current_force == force:
return
- self.currentForce = force
- self.emit(QtCore.SIGNAL("forceChanged(int)"), self.currentForce)
+ self._current_force = force
+ self.emit(QtCore.SIGNAL("forceChanged(int)"), self._current_force)
@QtCore.Slot()
def shoot(self):
- if self.autoShootTimer.isActive():
+ if self._auto_shoot_timer.isActive():
return
- self.timerCount = 0
- self.shootAngle = self.currentAngle
- self.shootForce = self.currentForce
- self.autoShootTimer.start(5)
+ self._timer_count = 0
+ self._shoot_angle = self._current_angle
+ self._shoot_force = self._current_force
+ self._auto_shoot_timer.start(5)
- firstTime = True
+ first_time = True
- def newTarget(self):
- if CannonField.firstTime:
- CannonField.firstTime = False
+ def new_target(self):
+ if CannonField.first_time:
+ CannonField.first_time = False
midnight = QtCore.QTime(0, 0, 0)
random.seed(midnight.secsTo(QtCore.QTime.currentTime()))
@@ -173,71 +173,71 @@ class CannonField(QtWidgets.QWidget):
self.update()
@QtCore.Slot()
- def moveShot(self):
- region = QtGui.QRegion(self.shotRect())
- self.timerCount += 1
+ def move_shot(self):
+ region = QtGui.QRegion(self.shot_rect())
+ self._timer_count += 1
- shotR = self.shotRect()
+ shot_r = self.shot_rect()
- if shotR.intersects(self.targetRect()):
- self.autoShootTimer.stop()
+ if shot_r.intersects(self.target_rect()):
+ self._auto_shoot_timer.stop()
self.emit(QtCore.SIGNAL("hit()"))
- elif shotR.x() > self.width() or shotR.y() > self.height():
- self.autoShootTimer.stop()
+ elif shot_r.x() > self.width() or shot_r.y() > self.height():
+ self._auto_shoot_timer.stop()
self.emit(QtCore.SIGNAL("missed()"))
else:
- region = region.united(QtGui.QRegion(shotR))
+ region = region.united(QtGui.QRegion(shot_r))
self.update(region)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
- self.paintCannon(painter)
- if self.autoShootTimer.isActive():
- self.paintShot(painter)
+ self.paint_cannon(painter)
+ if self._auto_shoot_timer.isActive():
+ self.paint_shot(painter)
- self.paintTarget(painter)
+ self.paint_target(painter)
- def paintShot(self, painter):
+ def paint_shot(self, painter):
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtCore.Qt.black)
- painter.drawRect(self.shotRect())
+ painter.drawRect(self.shot_rect())
- def paintTarget(self, painter):
+ def paint_target(self, painter):
painter.setPen(QtCore.Qt.black)
painter.setBrush(QtCore.Qt.red)
- painter.drawRect(self.targetRect())
+ painter.drawRect(self.target_rect())
- barrelRect = QtCore.QRect(33, -4, 15, 8)
+ barrel_rect = QtCore.QRect(33, -4, 15, 8)
- def paintCannon(self, painter):
+ def paint_cannon(self, painter):
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtCore.Qt.blue)
painter.save()
painter.translate(0, self.height())
painter.drawPie(QtCore.QRect(-35, -35, 70, 70), 0, 90 * 16)
- painter.rotate(-self.currentAngle)
- painter.drawRect(CannonField.barrelRect)
+ painter.rotate(-self._current_angle)
+ painter.drawRect(CannonField.barrel_rect)
painter.restore()
- def cannonRect(self):
+ def cannon_rect(self):
result = QtCore.QRect(0, 0, 50, 50)
result.moveBottomLeft(self.rect().bottomLect())
return result
- def shotRect(self):
+ def shot_rect(self):
gravity = 4.0
- time = self.timerCount / 40.0
- velocity = self.shootForce
- radians = self.shootAngle * 3.14159265 / 180
+ time = self._timer_count / 40.0
+ velocity = self._shoot_force
+ radians = self._shoot_angle * 3.14159265 / 180
velx = velocity * math.cos(radians)
vely = velocity * math.sin(radians)
- x0 = (CannonField.barrelRect.right() + 5) * math.cos(radians)
- y0 = (CannonField.barrelRect.right() + 5) * math.sin(radians)
+ x0 = (CannonField.barrel_rect.right() + 5) * math.cos(radians)
+ y0 = (CannonField.barrel_rect.right() + 5) * math.sin(radians)
x = x0 + velx * time
y = y0 + vely * time - 0.5 * gravity * time * time
@@ -245,7 +245,7 @@ class CannonField(QtWidgets.QWidget):
result.moveCenter(QtCore.QPoint(round(x), self.height() - 1 - round(y)))
return result
- def targetRect(self):
+ def target_rect(self):
result = QtCore.QRect(0, 0, 20, 10)
result.moveCenter(QtCore.QPoint(self.target.x(), self.height() - 1 - self.target.y()))
return result
@@ -262,46 +262,46 @@ class MyWidget(QtWidgets.QWidget):
qApp, QtCore.SLOT("quit()"))
angle = LCDRange("ANGLE")
- angle.setRange(5, 70)
+ angle.set_range(5, 70)
force = LCDRange("FORCE")
- force.setRange(10, 50)
+ force.set_range(10, 50)
- cannonField = CannonField()
+ cannon_field = CannonField()
self.connect(angle, QtCore.SIGNAL("valueChanged(int)"),
- cannonField.setAngle)
- self.connect(cannonField, QtCore.SIGNAL("angleChanged(int)"),
- angle.setValue)
+ cannon_field.set_angle)
+ self.connect(cannon_field, QtCore.SIGNAL("angleChanged(int)"),
+ angle.set_value)
self.connect(force, QtCore.SIGNAL("valueChanged(int)"),
- cannonField.setForce)
- self.connect(cannonField, QtCore.SIGNAL("forceChanged(int)"),
- force.setValue)
+ cannon_field.set_force)
+ self.connect(cannon_field, QtCore.SIGNAL("forceChanged(int)"),
+ force.set_value)
shoot = QtWidgets.QPushButton("&Shoot")
shoot.setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
- self.connect(shoot, QtCore.SIGNAL("clicked()"), cannonField.shoot)
+ self.connect(shoot, QtCore.SIGNAL("clicked()"), cannon_field.shoot)
- topLayout = QtWidgets.QHBoxLayout()
- topLayout.addWidget(shoot)
- topLayout.addStretch(1)
+ top_layout = QtWidgets.QHBoxLayout()
+ top_layout.addWidget(shoot)
+ top_layout.addStretch(1)
- leftLayout = QtWidgets.QVBoxLayout()
- leftLayout.addWidget(angle)
- leftLayout.addWidget(force)
+ left_layout = QtWidgets.QVBoxLayout()
+ left_layout.addWidget(angle)
+ left_layout.addWidget(force)
- gridLayout = QtWidgets.QGridLayout()
- gridLayout.addWidget(quit, 0, 0)
- gridLayout.addLayout(topLayout, 0, 1)
- gridLayout.addLayout(leftLayout, 1, 0)
- gridLayout.addWidget(cannonField, 1, 1, 2, 1)
- gridLayout.setColumnStretch(1, 10)
- self.setLayout(gridLayout)
+ grid_layout = QtWidgets.QGridLayout()
+ grid_layout.addWidget(quit, 0, 0)
+ grid_layout.addLayout(top_layout, 0, 1)
+ grid_layout.addLayout(left_layout, 1, 0)
+ grid_layout.addWidget(cannon_field, 1, 1, 2, 1)
+ grid_layout.setColumnStretch(1, 10)
+ self.setLayout(grid_layout)
- angle.setValue(60)
- force.setValue(25)
+ angle.set_value(60)
+ force.set_value(25)
angle.setFocus()