diff options
Diffstat (limited to 'examples/widgets/mainwindows/application/application.py')
-rw-r--r-- | examples/widgets/mainwindows/application/application.py | 159 |
1 files changed, 63 insertions, 96 deletions
diff --git a/examples/widgets/mainwindows/application/application.py b/examples/widgets/mainwindows/application/application.py index 3ec9344cb..f69eade2e 100644 --- a/examples/widgets/mainwindows/application/application.py +++ b/examples/widgets/mainwindows/application/application.py @@ -1,55 +1,17 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2021 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 from argparse import ArgumentParser, RawTextHelpFormatter import sys from PySide6.QtCore import (QByteArray, QFile, QFileInfo, QSaveFile, QSettings, - QTextStream, Qt) + QTextStream, Qt, Slot) from PySide6.QtGui import QAction, QIcon, QKeySequence from PySide6.QtWidgets import (QApplication, QFileDialog, QMainWindow, - QMessageBox, QTextEdit, QWidget) + QMessageBox, QTextEdit) -import application_rc +import application_rc # noqa: F401 class MainWindow(QMainWindow): @@ -80,23 +42,27 @@ class MainWindow(QMainWindow): else: event.ignore() + @Slot() def new_file(self): if self.maybe_save(): self._text_edit.clear() self.set_current_file('') + @Slot() def open(self): if self.maybe_save(): fileName, filtr = QFileDialog.getOpenFileName(self) if fileName: self.load_file(fileName) + @Slot() def save(self): if self._cur_file: return self.save_file(self._cur_file) return self.save_as() + @Slot() def save_as(self): fileName, filtr = QFileDialog.getSaveFileName(self) if fileName: @@ -104,62 +70,67 @@ class MainWindow(QMainWindow): return False + @Slot() def about(self): QMessageBox.about(self, "About Application", - "The <b>Application</b> example demonstrates how to write " - "modern GUI applications using Qt, with a menu bar, " - "toolbars, and a status bar.") + "The <b>Application</b> example demonstrates how to write " + "modern GUI applications using Qt, with a menu bar, " + "toolbars, and a status bar.") + @Slot() def document_was_modified(self): self.setWindowModified(self._text_edit.document().isModified()) def create_actions(self): - icon = QIcon.fromTheme("document-new", QIcon(':/images/new.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.DocumentNew, QIcon(':/images/new.png')) self._new_act = QAction(icon, "&New", self, shortcut=QKeySequence.New, - statusTip="Create a new file", triggered=self.new_file) + statusTip="Create a new file", triggered=self.new_file) - icon = QIcon.fromTheme("document-open", QIcon(':/images/open.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.DocumentOpen, QIcon(':/images/open.png')) self._open_act = QAction(icon, "&Open...", self, - shortcut=QKeySequence.Open, statusTip="Open an existing file", - triggered=self.open) + shortcut=QKeySequence.Open, statusTip="Open an existing file", + triggered=self.open) - icon = QIcon.fromTheme("document-save", QIcon(':/images/save.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.DocumentSave, QIcon(':/images/save.png')) self._save_act = QAction(icon, "&Save", self, - shortcut=QKeySequence.Save, - statusTip="Save the document to disk", triggered=self.save) + shortcut=QKeySequence.Save, + statusTip="Save the document to disk", triggered=self.save) self._save_as_act = QAction("Save &As...", self, - shortcut=QKeySequence.SaveAs, - statusTip="Save the document under a new name", - triggered=self.save_as) + shortcut=QKeySequence.SaveAs, + statusTip="Save the document under a new name", + triggered=self.save_as) - self._exit_act = QAction("E&xit", self, shortcut="Ctrl+Q", - statusTip="Exit the application", triggered=self.close) + icon = QIcon.fromTheme(QIcon.ThemeIcon.ApplicationExit) + self._exit_act = QAction(icon, "E&xit", self, shortcut="Ctrl+Q", + statusTip="Exit the application", triggered=self.close) - icon = QIcon.fromTheme("edit-cut", QIcon(':/images/cut.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.EditCut, QIcon(':/images/cut.png')) self._cut_act = QAction(icon, "Cu&t", self, shortcut=QKeySequence.Cut, - statusTip="Cut the current selection's contents to the clipboard", - triggered=self._text_edit.cut) + statusTip="Cut the current selection's contents to the clipboard", + triggered=self._text_edit.cut) - icon = QIcon.fromTheme("edit-copy", QIcon(':/images/copy.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.EditCopy, QIcon(':/images/copy.png')) self._copy_act = QAction(icon, "&Copy", - self, shortcut=QKeySequence.Copy, - statusTip="Copy the current selection's contents to the clipboard", - triggered=self._text_edit.copy) + self, shortcut=QKeySequence.Copy, + statusTip="Copy the current selection's contents to the clipboard", + triggered=self._text_edit.copy) - icon = QIcon.fromTheme("edit-paste", QIcon(':/images/paste.png')) + icon = QIcon.fromTheme(QIcon.ThemeIcon.EditPaste, QIcon(':/images/paste.png')) self._paste_act = QAction(icon, "&Paste", - self, shortcut=QKeySequence.Paste, - statusTip="Paste the clipboard's contents into the current selection", - triggered=self._text_edit.paste) + self, shortcut=QKeySequence.Paste, + statusTip="Paste the clipboard's contents into the current " + "selection", + triggered=self._text_edit.paste) - self._about_act = QAction("&About", self, - statusTip="Show the application's About box", - triggered=self.about) + icon = QIcon.fromTheme(QIcon.ThemeIcon.HelpAbout) + self._about_act = QAction(icon, "&About", self, + statusTip="Show the application's About box", + triggered=self.about) self._about_qt_act = QAction("About &Qt", self, - statusTip="Show the Qt library's About box", - triggered=qApp.aboutQt) + statusTip="Show the Qt library's About box", + triggered=qApp.aboutQt) # noqa: F821 self._cut_act.setEnabled(False) self._copy_act.setEnabled(False) @@ -213,10 +184,9 @@ class MainWindow(QMainWindow): def maybe_save(self): if self._text_edit.document().isModified(): ret = QMessageBox.warning(self, "Application", - "The document has been modified.\nDo you want to save " - "your changes?", - QMessageBox.Save | QMessageBox.Discard | - QMessageBox.Cancel) + "The document has been modified.\nDo you want to save " + "your changes?", + QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) if ret == QMessageBox.Save: return self.save() elif ret == QMessageBox.Cancel: @@ -227,32 +197,29 @@ class MainWindow(QMainWindow): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): reason = file.errorString() - QMessageBox.warning(self, "Application", - f"Cannot read file {fileName}:\n{reason}.") + QMessageBox.warning(self, "Application", f"Cannot read file {fileName}:\n{reason}.") return inf = QTextStream(file) - QApplication.setOverrideCursor(Qt.WaitCursor) - self._text_edit.setPlainText(inf.readAll()) - QApplication.restoreOverrideCursor() + with QApplication.setOverrideCursor(Qt.WaitCursor): + self._text_edit.setPlainText(inf.readAll()) self.set_current_file(fileName) self.statusBar().showMessage("File loaded", 2000) def save_file(self, fileName): error = None - QApplication.setOverrideCursor(Qt.WaitCursor) - file = QSaveFile(fileName) - if file.open(QFile.WriteOnly | QFile.Text): - outf = QTextStream(file) - outf << self._text_edit.toPlainText() - if not file.commit(): + with QApplication.setOverrideCursor(Qt.WaitCursor): + file = QSaveFile(fileName) + if file.open(QFile.WriteOnly | QFile.Text): + outf = QTextStream(file) + outf << self._text_edit.toPlainText() + if not file.commit(): + reason = file.errorString() + error = f"Cannot write file {fileName}:\n{reason}." + else: reason = file.errorString() - error = f"Cannot write file {fileName}:\n{reason}." - else: - reason = file.errorString() - error = f"Cannot open file {fileName}:\n{reason}." - QApplication.restoreOverrideCursor() + error = f"Cannot open file {fileName}:\n{reason}." if error: QMessageBox.warning(self, "Application", error) |