1
2#############################################################################
3##
4## Copyright (C) 2016 The Qt Company Ltd.
5## Contact: http://www.qt.io/licensing/
6##
7## This file is part of the Qt for Python examples of the Qt Toolkit.
8##
9## $QT_BEGIN_LICENSE:BSD$
10## You may use this file under the terms of the BSD license as follows:
11##
12## "Redistribution and use in source and binary forms, with or without
13## modification, are permitted provided that the following conditions are
14## met:
15##   * Redistributions of source code must retain the above copyright
16##     notice, this list of conditions and the following disclaimer.
17##   * Redistributions in binary form must reproduce the above copyright
18##     notice, this list of conditions and the following disclaimer in
19##     the documentation and/or other materials provided with the
20##     distribution.
21##   * Neither the name of The Qt Company Ltd nor the names of its
22##     contributors may be used to endorse or promote products derived
23##     from this software without specific prior written permission.
24##
25##
26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37##
38## $QT_END_LICENSE$
39##
40#############################################################################
41
42# PySide2 tutorial 10
43
44
45import sys
46from PySide2 import QtCore, QtGui, QtWidgets
47
48
49class LCDRange(QtWidgets.QWidget):
50    valueChanged = QtCore.Signal(int)
51    def __init__(self, parent=None):
52        QtWidgets.QWidget.__init__(self, parent)
53
54        lcd = QtWidgets.QLCDNumber(2)
55        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
56        self.slider.setRange(0, 99)
57        self.slider.setValue(0)
58
59        self.connect(self.slider, QtCore.SIGNAL("valueChanged(int)"),
60                     lcd, QtCore.SLOT("display(int)"))
61        self.connect(self.slider, QtCore.SIGNAL("valueChanged(int)"),
62                     self, QtCore.SIGNAL("valueChanged(int)"))
63
64        layout = QtWidgets.QVBoxLayout()
65        layout.addWidget(lcd)
66        layout.addWidget(self.slider)
67        self.setLayout(layout)
68
69        self.setFocusProxy(self.slider)
70
71    def value(self):
72        return self.slider.value()
73
74    @QtCore.Slot(int)
75    def setValue(self, value):
76        self.slider.setValue(value)
77
78    def setRange(self, minValue, maxValue):
79        if minValue < 0 or maxValue > 99 or minValue > maxValue:
80            QtCore.qWarning("LCDRange::setRange(%d, %d)\n"
81                    "\tRange must be 0..99\n"
82                    "\tand minValue must not be greater than maxValue" % (minValue, maxValue))
83            return
84
85        self.slider.setRange(minValue, maxValue)
86
87
88class CannonField(QtWidgets.QWidget):
89    angleChanged = QtCore.Signal(int)
90    forceChanged = QtCore.Signal(int)
91    def __init__(self, parent=None):
92        QtWidgets.QWidget.__init__(self, parent)
93
94        self.currentAngle = 45
95        self.currentForce = 0
96        self.setPalette(QtGui.QPalette(QtGui.QColor(250, 250, 200)))
97        self.setAutoFillBackground(True)
98
99    def angle(self):
100        return self.currentAngle
101
102    @QtCore.Slot(int)
103    def setAngle(self, angle):
104        if angle < 5:
105            angle = 5
106        if angle > 70:
107            angle = 70
108        if self.currentAngle == angle:
109            return
110        self.currentAngle = angle
111        self.update()
112        self.emit(QtCore.SIGNAL("angleChanged(int)"), self.currentAngle)
113
114    def force(self):
115        return self.currentForce
116
117    @QtCore.Slot(int)
118    def setForce(self, force):
119        if force < 0:
120            force = 0
121        if self.currentForce == force:
122            return
123        self.currentForce = force
124        self.emit(QtCore.SIGNAL("forceChanged(int)"), self.currentForce)
125
126    def paintEvent(self, event):
127        painter = QtGui.QPainter(self)
128
129        painter.setPen(QtCore.Qt.NoPen)
130        painter.setBrush(QtCore.Qt.blue)
131
132        painter.translate(0, self.height())
133        painter.drawPie(QtCore.QRect(-35, -35, 70, 70), 0, 90 * 16)
134        painter.rotate(-self.currentAngle)
135        painter.drawRect(QtCore.QRect(33, -4, 15, 8))
136
137    def cannonRect(self):
138        result = QtCore.QRect(0, 0, 50, 50)
139        result.moveBottomLeft(self.rect().bottomLect())
140        return result
141
142
143class MyWidget(QtWidgets.QWidget):
144    def __init__(self, parent=None):
145        QtWidgets.QWidget.__init__(self, parent)
146
147        quit = QtWidgets.QPushButton("&Quit")
148        quit.setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
149
150        self.connect(quit, QtCore.SIGNAL("clicked()"),
151                     qApp, QtCore.SLOT("quit()"))
152
153        angle = LCDRange()
154        angle.setRange(5, 70)
155
156        force = LCDRange()
157        force.setRange(10, 50)
158
159        cannonField = CannonField()
160
161        self.connect(angle, QtCore.SIGNAL("valueChanged(int)"),
162                     cannonField.setAngle)
163        self.connect(cannonField, QtCore.SIGNAL("angleChanged(int)"),
164                     angle.setValue)
165
166        self.connect(force, QtCore.SIGNAL("valueChanged(int)"),
167                     cannonField.setForce)
168        self.connect(cannonField, QtCore.SIGNAL("forceChanged(int)"),
169                     force.setValue)
170
171        leftLayout = QtWidgets.QVBoxLayout()
172        leftLayout.addWidget(angle)
173        leftLayout.addWidget(force)
174
175        gridLayout = QtWidgets.QGridLayout()
176        gridLayout.addWidget(quit, 0, 0)
177        gridLayout.addLayout(leftLayout, 1, 0)
178        gridLayout.addWidget(cannonField, 1, 1, 2, 1)
179        gridLayout.setColumnStretch(1, 10)
180        self.setLayout(gridLayout)
181
182        angle.setValue(60)
183        force.setValue(25)
184        angle.setFocus()
185
186
187app = QtWidgets.QApplication(sys.argv)
188widget = MyWidget()
189widget.setGeometry(100, 100, 500, 355)
190widget.show()
191sys.exit(app.exec_())
192