diff options
Diffstat (limited to 'examples/xml/dombookmarks/dombookmarks.py')
-rw-r--r-- | examples/xml/dombookmarks/dombookmarks.py | 113 |
1 files changed, 34 insertions, 79 deletions
diff --git a/examples/xml/dombookmarks/dombookmarks.py b/examples/xml/dombookmarks/dombookmarks.py index 6a97c8cc6..a35aeb0f2 100644 --- a/examples/xml/dombookmarks/dombookmarks.py +++ b/examples/xml/dombookmarks/dombookmarks.py @@ -1,52 +1,16 @@ - -############################################################################# -## -## 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 xml/dombookmarks example from Qt v5.x""" import sys -from PySide6.QtCore import QDir, QFile, Qt -from PySide6.QtGui import QAction, QIcon -from PySide6.QtWidgets import (QApplication, QFileDialog, QHeaderView, QMainWindow, QMessageBox, QStyle, QTreeWidget, QTreeWidgetItem, QWidget) +from PySide6.QtCore import QDir, QFile, Qt, QTextStream +from PySide6.QtGui import QAction, QIcon, QKeySequence +from PySide6.QtWidgets import (QApplication, QFileDialog, QHeaderView, + QMainWindow, QMessageBox, QStyle, QTreeWidget, + QTreeWidgetItem) from PySide6.QtXml import QDomDocument @@ -57,7 +21,6 @@ class MainWindow(QMainWindow): self._xbel_tree = XbelTree() self.setCentralWidget(self._xbel_tree) - self.create_actions() self.create_menus() self.statusBar().showMessage("Ready") @@ -67,8 +30,8 @@ class MainWindow(QMainWindow): def open(self): file_name = QFileDialog.getOpenFileName(self, - "Open Bookmark File", QDir.currentPath(), - "XBEL Files (*.xbel *.xml)")[0] + "Open Bookmark File", QDir.currentPath(), + "XBEL Files (*.xbel *.xml)")[0] if not file_name: return @@ -77,7 +40,7 @@ class MainWindow(QMainWindow): if not in_file.open(QFile.ReadOnly | QFile.Text): reason = in_file.errorString() QMessageBox.warning(self, "DOM Bookmarks", - f"Cannot read file {file_name}:\n{reason}.") + f"Cannot read file {file_name}:\n{reason}.") return if self._xbel_tree.read(in_file): @@ -85,8 +48,8 @@ class MainWindow(QMainWindow): def save_as(self): file_name = QFileDialog.getSaveFileName(self, - "Save Bookmark File", QDir.currentPath(), - "XBEL Files (*.xbel *.xml)")[0] + "Save Bookmark File", QDir.currentPath(), + "XBEL Files (*.xbel *.xml)")[0] if not file_name: return @@ -95,7 +58,7 @@ class MainWindow(QMainWindow): if not out_file.open(QFile.WriteOnly | QFile.Text): reason = out_file.errorString() QMessageBox.warning(self, "DOM Bookmarks", - "Cannot write file {fileName}:\n{reason}.") + f"Cannot write file {file_name}:\n{reason}.") return if self._xbel_tree.write(out_file): @@ -103,35 +66,26 @@ class MainWindow(QMainWindow): def about(self): QMessageBox.about(self, "About DOM Bookmarks", - "The <b>DOM Bookmarks</b> example demonstrates how to use Qt's " - "DOM classes to read and write XML documents.") - - def create_actions(self): - self._open_act = QAction("&Open...", self, shortcut="Ctrl+O", - triggered=self.open) - - self._save_as_act = QAction("&Save As...", self, shortcut="Ctrl+S", - triggered=self.save_as) - - self._exit_act = QAction("E&xit", self, shortcut="Ctrl+Q", - triggered=self.close) - - self._about_act = QAction("&About", self, triggered=self.about) - - self._about_qt_act = QAction("About &Qt", self, - triggered=qApp.aboutQt) + "The <b>DOM Bookmarks</b> example demonstrates how to use Qt's " + "DOM classes to read and write XML documents.") def create_menus(self): self._file_menu = self.menuBar().addMenu("&File") - self._file_menu.addAction(self._open_act) - self._file_menu.addAction(self._save_as_act) - self._file_menu.addAction(self._exit_act) + self._file_menu.addAction(QAction("&Open...", self, + shortcut=QKeySequence( + Qt.CTRL | Qt.Key_O), triggered=self.open)) + self._file_menu.addAction(QAction("&Save As...", self, + shortcut=QKeySequence( + Qt.CTRL | Qt.Key_S), triggered=self.save_as)) + self._file_menu.addAction(QAction("E&xit", self, + shortcut=QKeySequence( + Qt.CTRL | Qt.Key_Q), triggered=self.close)) self.menuBar().addSeparator() self._help_menu = self.menuBar().addMenu("&Help") - self._help_menu.addAction(self._about_act) - self._help_menu.addAction(self._about_qt_act) + self._help_menu.addAction(QAction("&About", self, triggered=self.about)) + self._help_menu.addAction(QAction("About &Qt", self, triggered=qApp.aboutQt)) # noqa: F821 class XbelTree(QTreeWidget): @@ -149,26 +103,27 @@ class XbelTree(QTreeWidget): self._bookmark_icon = QIcon() self._folder_icon.addPixmap(self.style().standardPixmap(QStyle.SP_DirClosedIcon), - QIcon.Normal, QIcon.Off) + QIcon.Normal, QIcon.Off) self._folder_icon.addPixmap(self.style().standardPixmap(QStyle.SP_DirOpenIcon), - QIcon.Normal, QIcon.On) + QIcon.Normal, QIcon.On) self._bookmark_icon.addPixmap(self.style().standardPixmap(QStyle.SP_FileIcon)) def read(self, device): ok, errorStr, errorLine, errorColumn = self._dom_document.setContent(device, True) if not ok: QMessageBox.information(self.window(), "DOM Bookmarks", - f"Parse error at line {errorLine}, column {errorColumn}:\n{errorStr}") + f"Parse error at line {errorLine}, " + f"column {errorColumn}:\n{errorStr}") return False root = self._dom_document.documentElement() if root.tagName() != 'xbel': QMessageBox.information(self.window(), "DOM Bookmarks", - "The file is not an XBEL file.") + "The file is not an XBEL file.") return False elif root.hasAttribute('version') and root.attribute('version') != '1.0': QMessageBox.information(self.window(), "DOM Bookmarks", - "The file is not an XBEL version 1.0 file.") + "The file is not an XBEL version 1.0 file.") return False self.clear() @@ -176,7 +131,7 @@ class XbelTree(QTreeWidget): # It might not be connected. try: self.itemChanged.disconnect(self.update_dom_element) - except: + except RuntimeError: pass child = root.firstChildElement('folder') |