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 9
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    def __init__(self, parent=None):
91        QtWidgets.QWidget.__init__(self, parent)
92
93        self.currentAngle = 45
94        self.setPalette(QtGui.QPalette(QtGui.QColor(250, 250, 200)))
95        self.setAutoFillBackground(True)
96
97    def angle(self):
98        return self.currentAngle
99
100    @QtCore.Slot(int)
101    def setAngle(self, angle):
102        if angle < 5:
103            angle = 5
104        if angle > 70:
105            angle = 70
106        if self.currentAngle == angle:
107            return
108        self.currentAngle = angle
109        self.update()
110        self.emit(QtCore.SIGNAL("angleChanged(int)"), self.currentAngle)
111
112    def paintEvent(self, event):
113        painter = QtGui.QPainter(self)
114
115        painter.setPen(QtCore.Qt.NoPen)
116        painter.setBrush(QtCore.Qt.blue)
117
118        painter.translate(0, self.rect().height())
119        painter.drawPie(QtCore.QRect(-35, -35, 70, 70), 0, 90 * 16)
120        painter.rotate(-self.currentAngle)
121        painter.drawRect(QtCore.QRect(33, -4, 15, 8))
122
123
124class MyWidget(QtWidgets.QWidget):
125    def __init__(self, parent=None):
126        QtWidgets.QWidget.__init__(self, parent)
127
128        quit = QtWidgets.QPushButton("Quit")
129        quit.setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
130
131        self.connect(quit, QtCore.SIGNAL("clicked()"),
132                     qApp, QtCore.SLOT("quit()"))
133
134        angle = LCDRange()
135        angle.setRange(5, 70)
136
137        cannonField = CannonField()
138
139        self.connect(angle, QtCore.SIGNAL("valueChanged(int)"),
140                     cannonField.setAngle)
141        self.connect(cannonField, QtCore.SIGNAL("angleChanged(int)"),
142                     angle.setValue)
143
144        gridLayout = QtWidgets.QGridLayout()
145        gridLayout.addWidget(quit, 0, 0)
146        gridLayout.addWidget(angle, 1, 0)
147        gridLayout.addWidget(cannonField, 1, 1, 2, 1)
148        gridLayout.setColumnStretch(1, 10)
149        self.setLayout(gridLayout)
150
151        angle.setValue(60)
152        angle.setFocus()
153
154
155app = QtWidgets.QApplication(sys.argv)
156widget = MyWidget()
157widget.setGeometry(100, 100, 500, 355)
158widget.show()
159sys.exit(app.exec_())
160