diff options
Diffstat (limited to 'examples/widgets/painting/basicdrawing/basicdrawing.py')
-rw-r--r-- | examples/widgets/painting/basicdrawing/basicdrawing.py | 171 |
1 files changed, 65 insertions, 106 deletions
diff --git a/examples/widgets/painting/basicdrawing/basicdrawing.py b/examples/widgets/painting/basicdrawing/basicdrawing.py index d7849b0cc..858a8cd9f 100644 --- a/examples/widgets/painting/basicdrawing/basicdrawing.py +++ b/examples/widgets/painting/basicdrawing/basicdrawing.py @@ -1,54 +1,17 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause """PySide6 port of the widgets/painting/basicdrawing example from Qt v5.x, originating from PyQt""" from PySide6.QtCore import QPoint, QRect, QSize, Qt, qVersion from PySide6.QtGui import (QBrush, QConicalGradient, QLinearGradient, QPainter, - QPainterPath, QPalette, QPen, QPixmap, QPolygon, QRadialGradient) + QPainterPath, QPalette, QPen, QPixmap, QPolygon, + QRadialGradient) from PySide6.QtWidgets import (QApplication, QCheckBox, QComboBox, QGridLayout, - QLabel, QSpinBox, QWidget) + QLabel, QSpinBox, QWidget) -import basicdrawing_rc +import basicdrawing_rc # noqa: F401 class RenderArea(QWidget): @@ -114,56 +77,56 @@ class RenderArea(QWidget): start_angle = 30 * 16 arc_length = 120 * 16 - painter = QPainter(self) - painter.setPen(self.pen) - painter.setBrush(self.brush) - if self.antialiased: - painter.setRenderHint(QPainter.Antialiasing) - - for x in range(0, self.width(), 100): - for y in range(0, self.height(), 100): - painter.save() - painter.translate(x, y) - if self.transformed: - painter.translate(50, 50) - painter.rotate(60.0) - painter.scale(0.6, 0.9) - painter.translate(-50, -50) - - if self.shape == RenderArea.Line: - painter.drawLine(rect.bottomLeft(), rect.topRight()) - elif self.shape == RenderArea.Points: - painter.drawPoints(RenderArea.points) - elif self.shape == RenderArea.Polyline: - painter.drawPolyline(RenderArea.points) - elif self.shape == RenderArea.Polygon: - painter.drawPolygon(RenderArea.points) - elif self.shape == RenderArea.Rect: - painter.drawRect(rect) - elif self.shape == RenderArea.RoundedRect: - painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize) - elif self.shape == RenderArea.Ellipse: - painter.drawEllipse(rect) - elif self.shape == RenderArea.Arc: - painter.drawArc(rect, start_angle, arc_length) - elif self.shape == RenderArea.Chord: - painter.drawChord(rect, start_angle, arc_length) - elif self.shape == RenderArea.Pie: - painter.drawPie(rect, start_angle, arc_length) - elif self.shape == RenderArea.Path: - painter.drawPath(path) - elif self.shape == RenderArea.Text: - qv = qVersion() - painter.drawText(rect, Qt.AlignCenter, - f"PySide 6\nQt {qv}") - elif self.shape == RenderArea.Pixmap: - painter.drawPixmap(10, 10, self.pixmap) - - painter.restore() - - painter.setPen(self.palette().dark().color()) - painter.setBrush(Qt.NoBrush) - painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1)) + with QPainter(self) as painter: + painter.setPen(self.pen) + painter.setBrush(self.brush) + if self.antialiased: + painter.setRenderHint(QPainter.Antialiasing) + + for x in range(0, self.width(), 100): + for y in range(0, self.height(), 100): + painter.save() + painter.translate(x, y) + if self.transformed: + painter.translate(50, 50) + painter.rotate(60.0) + painter.scale(0.6, 0.9) + painter.translate(-50, -50) + + if self.shape == RenderArea.Line: + painter.drawLine(rect.bottomLeft(), rect.topRight()) + elif self.shape == RenderArea.Points: + painter.drawPoints(RenderArea.points) + elif self.shape == RenderArea.Polyline: + painter.drawPolyline(RenderArea.points) + elif self.shape == RenderArea.Polygon: + painter.drawPolygon(RenderArea.points) + elif self.shape == RenderArea.Rect: + painter.drawRect(rect) + elif self.shape == RenderArea.RoundedRect: + painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize) + elif self.shape == RenderArea.Ellipse: + painter.drawEllipse(rect) + elif self.shape == RenderArea.Arc: + painter.drawArc(rect, start_angle, arc_length) + elif self.shape == RenderArea.Chord: + painter.drawChord(rect, start_angle, arc_length) + elif self.shape == RenderArea.Pie: + painter.drawPie(rect, start_angle, arc_length) + elif self.shape == RenderArea.Path: + painter.drawPath(path) + elif self.shape == RenderArea.Text: + qv = qVersion() + painter.drawText(rect, Qt.AlignCenter, + f"PySide 6\nQt {qv}") + elif self.shape == RenderArea.Pixmap: + painter.drawPixmap(10, 10, self.pixmap) + + painter.restore() + + painter.setPen(self.palette().dark().color()) + painter.setBrush(Qt.NoBrush) + painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1)) id_role = Qt.UserRole @@ -228,12 +191,9 @@ class Window(QWidget): pen_join_label.setBuddy(self._pen_join_combo_box) self._brush_style_combo_box = QComboBox() - self._brush_style_combo_box.addItem("Linear Gradient", - Qt.LinearGradientPattern) - self._brush_style_combo_box.addItem("Radial Gradient", - Qt.RadialGradientPattern) - self._brush_style_combo_box.addItem("Conical Gradient", - Qt.ConicalGradientPattern) + self._brush_style_combo_box.addItem("Linear Gradient", Qt.LinearGradientPattern) + self._brush_style_combo_box.addItem("Radial Gradient", Qt.RadialGradientPattern) + self._brush_style_combo_box.addItem("Conical Gradient", Qt.ConicalGradientPattern) self._brush_style_combo_box.addItem("Texture", Qt.TexturePattern) self._brush_style_combo_box.addItem("Solid", Qt.SolidPattern) self._brush_style_combo_box.addItem("Horizontal", Qt.HorPattern) @@ -298,24 +258,23 @@ class Window(QWidget): self.setWindowTitle("Basic Drawing") def shape_changed(self): - shape = self._shape_combo_box.itemData(self._shape_combo_box.currentIndex(), - id_role) + shape = self._shape_combo_box.itemData(self._shape_combo_box.currentIndex(), id_role) self._render_area.set_shape(shape) def pen_changed(self): width = self._pen_width_spin_box.value() style = Qt.PenStyle(self._pen_style_combo_box.itemData( - self._pen_style_combo_box.currentIndex(), id_role)) + self._pen_style_combo_box.currentIndex(), id_role)) cap = Qt.PenCapStyle(self._pen_cap_combo_box.itemData( - self._pen_cap_combo_box.currentIndex(), id_role)) + self._pen_cap_combo_box.currentIndex(), id_role)) join = Qt.PenJoinStyle(self._pen_join_combo_box.itemData( - self._pen_join_combo_box.currentIndex(), id_role)) + self._pen_join_combo_box.currentIndex(), id_role)) self._render_area.set_pen(QPen(Qt.blue, width, style, cap, join)) def brush_changed(self): style = Qt.BrushStyle(self._brush_style_combo_box.itemData( - self._brush_style_combo_box.currentIndex(), id_role)) + self._brush_style_combo_box.currentIndex(), id_role)) if style == Qt.LinearGradientPattern: linear_gradient = QLinearGradient(0, 0, 100, 100) |