aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/painting/painter/painter.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/painting/painter/painter.py')
-rw-r--r--examples/widgets/painting/painter/painter.py70
1 files changed, 18 insertions, 52 deletions
diff --git a/examples/widgets/painting/painter/painter.py b/examples/widgets/painting/painter/painter.py
index 3d5d95f10..2ca078ad9 100644
--- a/examples/widgets/painting/painter/painter.py
+++ b/examples/widgets/painting/painter/painter.py
@@ -1,42 +1,5 @@
-#############################################################################
-##
-## Copyright (C) 2021 The Qt Company Ltd.
-## Contact: https://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) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from PySide6.QtWidgets import (
QWidget,
@@ -46,7 +9,7 @@ from PySide6.QtWidgets import (
QStyle,
QColorDialog,
)
-from PySide6.QtCore import QPoint, Qt, QDir, Slot, QStandardPaths
+from PySide6.QtCore import Qt, Slot, QStandardPaths
from PySide6.QtGui import (
QMouseEvent,
QPaintEvent,
@@ -88,10 +51,8 @@ class PainterWidget(QWidget):
Paint the Pixmap into the widget
"""
- painter = QPainter()
- painter.begin(self)
- painter.drawPixmap(0, 0, self.pixmap)
- painter.end()
+ with QPainter(self) as painter:
+ painter.drawPixmap(0, 0, self.pixmap)
def mousePressEvent(self, event: QMouseEvent):
"""Override from QWidget
@@ -155,15 +116,17 @@ class MainWindow(QMainWindow):
self.bar = self.addToolBar("Menu")
self.bar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self._save_action = self.bar.addAction(
- qApp.style().standardIcon(QStyle.SP_DialogSaveButton), "Save", self.on_save
+ qApp.style().standardIcon(QStyle.SP_DialogSaveButton), # noqa: F821
+ "Save", self.on_save
)
self._save_action.setShortcut(QKeySequence.Save)
self._open_action = self.bar.addAction(
- qApp.style().standardIcon(QStyle.SP_DialogOpenButton), "Open", self.on_open
+ qApp.style().standardIcon(QStyle.SP_DialogOpenButton), # noqa: F821
+ "Open", self.on_open
)
self._open_action.setShortcut(QKeySequence.Open)
self.bar.addAction(
- qApp.style().standardIcon(QStyle.SP_DialogResetButton),
+ qApp.style().standardIcon(QStyle.SP_DialogResetButton), # noqa: F821
"Clear",
self.painter_widget.clear,
)
@@ -175,7 +138,8 @@ class MainWindow(QMainWindow):
self.setCentralWidget(self.painter_widget)
- self.set_color(Qt.black)
+ self.color = Qt.black
+ self.set_color(self.color)
self.mime_type_filters = ["image/png", "image/jpeg"]
@@ -214,19 +178,21 @@ class MainWindow(QMainWindow):
@Slot()
def on_color_clicked(self):
- color = QColorDialog.getColor(Qt.black, self)
+ color = QColorDialog.getColor(self.color, self)
+
if color:
self.set_color(color)
def set_color(self, color: QColor = Qt.black):
+ self.color = color
# Create color icon
pix_icon = QPixmap(32, 32)
- pix_icon.fill(color)
+ pix_icon.fill(self.color)
self.color_action.setIcon(QIcon(pix_icon))
- self.painter_widget.pen.setColor(color)
- self.color_action.setText(QColor(color).name())
+ self.painter_widget.pen.setColor(self.color)
+ self.color_action.setText(QColor(self.color).name())
if __name__ == "__main__":