From 32222315eb32177d4161ee2e417d9910a07f6039 Mon Sep 17 00:00:00 2001 From: Cristian Maureira-Fredes Date: Thu, 10 Jun 2021 22:46:36 +0200 Subject: examples: add screenshots to widgets and others Adding screenshots to: - declarative - multimedia - network - widgets Renaming examples to avoid conflict on images names. Task-number: PYSIDE-841 Pick-to: 6.1 Change-Id: I21f403ee62b45c0cb2a45eca6c5c99f11901610e Reviewed-by: Friedemann Kleint --- examples/declarative/scrolling/doc/scrolling.png | Bin 0 -> 79 bytes examples/declarative/scrolling/doc/scrolling.rst | 9 + .../textproperties/doc/textproperties.png | Bin 0 -> 14082 bytes .../textproperties/doc/textproperties.rst | 9 + examples/declarative/usingmodel/doc/usingmodel.png | Bin 0 -> 2791 bytes examples/declarative/usingmodel/doc/usingmodel.rst | 9 + .../multimedia/audiooutput/doc/audiooutput.png | Bin 0 -> 5082 bytes .../multimedia/audiooutput/doc/audiooutput.rst | 12 + examples/multimedia/camera/doc/camera.png | Bin 0 -> 63404 bytes examples/multimedia/camera/doc/camera.rst | 12 + examples/multimedia/player/doc/player.png | Bin 0 -> 2804 bytes examples/multimedia/player/doc/player.rst | 9 + examples/network/downloader/doc/downloader.png | Bin 0 -> 6215 bytes examples/network/downloader/doc/downloader.rst | 9 + .../animation/animatedtiles/doc/animatedtiles.png | Bin 0 -> 299266 bytes .../animation/animatedtiles/doc/animatedtiles.rst | 8 + .../animation/appchooser/doc/appchooser.png | Bin 0 -> 22259 bytes .../animation/appchooser/doc/appchooser.rst | 9 + examples/widgets/animation/easing/doc/easing.png | Bin 0 -> 24136 bytes examples/widgets/animation/easing/doc/easing.rst | 9 + examples/widgets/animation/states/doc/states.png | Bin 0 -> 21700 bytes examples/widgets/animation/states/doc/states.rst | 8 + examples/widgets/codeeditor/doc/codeeditor.png | Bin 0 -> 7759 bytes examples/widgets/codeeditor/doc/codeeditor.rst | 9 + .../itemviews/address_book/adddialogwidget.py | 103 +++++++++ .../widgets/itemviews/address_book/address_book.py | 131 +++++++++++ .../itemviews/address_book/address_book.pyproject | 4 + .../itemviews/address_book/addresswidget.py | 249 +++++++++++++++++++++ .../itemviews/address_book/doc/address_book.png | Bin 0 -> 4922 bytes .../itemviews/address_book/doc/address_book.rst | 9 + .../itemviews/address_book/newaddresstab.py | 94 ++++++++ .../widgets/itemviews/address_book/tablemodel.py | 147 ++++++++++++ .../itemviews/addressbook/adddialogwidget.py | 103 --------- .../widgets/itemviews/addressbook/addressbook.py | 131 ----------- .../itemviews/addressbook/addressbook.pyproject | 4 - .../widgets/itemviews/addressbook/addresswidget.py | 249 --------------------- .../widgets/itemviews/addressbook/newaddresstab.py | 94 -------- .../widgets/itemviews/addressbook/tablemodel.py | 147 ------------ .../widgets/itemviews/jsonmodel/doc/jsonmodel.png | Bin 0 -> 14837 bytes .../widgets/itemviews/jsonmodel/doc/jsonmodel.rst | 8 + .../layouts/basiclayouts/doc/basiclayouts.png | Bin 0 -> 20931 bytes .../layouts/basiclayouts/doc/basiclayouts.rst | 9 + .../layouts/dynamiclayouts/doc/dynamiclayouts.png | Bin 0 -> 14088 bytes .../layouts/dynamiclayouts/doc/dynamiclayouts.rst | 10 + .../widgets/layouts/flowlayout/doc/flowlayout.png | Bin 0 -> 29229 bytes .../widgets/layouts/flowlayout/doc/flowlayout.rst | 11 + .../mainwindows/dockwidgets/doc/dockwidgets.png | Bin 0 -> 41007 bytes .../mainwindows/dockwidgets/doc/dockwidgets.rst | 9 + .../painting/basicdrawing/doc/basicdrawing.png | Bin 0 -> 18655 bytes .../painting/basicdrawing/doc/basicdrawing.rst | 15 ++ .../concentriccircles/doc/concentriccircles.png | Bin 0 -> 51606 bytes .../concentriccircles/doc/concentriccircles.rst | 12 + examples/widgets/painting/painter/doc/painter.png | Bin 0 -> 11442 bytes examples/widgets/painting/painter/doc/painter.rst | 8 + .../syntaxhighlighter/doc/syntaxhighlighter.png | Bin 0 -> 43420 bytes .../syntaxhighlighter/doc/syntaxhighlighter.rst | 8 + .../widgets/richtext/textedit/doc/textedit.png | Bin 0 -> 120703 bytes .../widgets/richtext/textedit/doc/textedit.rst | 8 + .../widgets/richtext/textobject/doc/textobject.png | Bin 0 -> 5844 bytes .../widgets/richtext/textobject/doc/textobject.rst | 31 +++ .../trafficlight/doc/trafficlight.png | Bin 0 -> 79 bytes .../trafficlight/doc/trafficlight.rst | 10 + .../tutorials/addressbook/doc/addressbook.png | Bin 0 -> 4989 bytes .../tutorials/addressbook/doc/addressbook.rst | 9 + examples/widgets/tutorials/cannon/doc/cannon.png | Bin 0 -> 2845 bytes examples/widgets/tutorials/cannon/doc/cannon.rst | 8 + 66 files changed, 1005 insertions(+), 728 deletions(-) create mode 100644 examples/declarative/scrolling/doc/scrolling.png create mode 100644 examples/declarative/scrolling/doc/scrolling.rst create mode 100644 examples/declarative/textproperties/doc/textproperties.png create mode 100644 examples/declarative/textproperties/doc/textproperties.rst create mode 100644 examples/declarative/usingmodel/doc/usingmodel.png create mode 100644 examples/declarative/usingmodel/doc/usingmodel.rst create mode 100644 examples/multimedia/audiooutput/doc/audiooutput.png create mode 100644 examples/multimedia/audiooutput/doc/audiooutput.rst create mode 100644 examples/multimedia/camera/doc/camera.png create mode 100644 examples/multimedia/camera/doc/camera.rst create mode 100644 examples/multimedia/player/doc/player.png create mode 100644 examples/multimedia/player/doc/player.rst create mode 100644 examples/network/downloader/doc/downloader.png create mode 100644 examples/network/downloader/doc/downloader.rst create mode 100644 examples/widgets/animation/animatedtiles/doc/animatedtiles.png create mode 100644 examples/widgets/animation/animatedtiles/doc/animatedtiles.rst create mode 100644 examples/widgets/animation/appchooser/doc/appchooser.png create mode 100644 examples/widgets/animation/appchooser/doc/appchooser.rst create mode 100644 examples/widgets/animation/easing/doc/easing.png create mode 100644 examples/widgets/animation/easing/doc/easing.rst create mode 100644 examples/widgets/animation/states/doc/states.png create mode 100644 examples/widgets/animation/states/doc/states.rst create mode 100644 examples/widgets/codeeditor/doc/codeeditor.png create mode 100644 examples/widgets/codeeditor/doc/codeeditor.rst create mode 100644 examples/widgets/itemviews/address_book/adddialogwidget.py create mode 100644 examples/widgets/itemviews/address_book/address_book.py create mode 100644 examples/widgets/itemviews/address_book/address_book.pyproject create mode 100644 examples/widgets/itemviews/address_book/addresswidget.py create mode 100644 examples/widgets/itemviews/address_book/doc/address_book.png create mode 100644 examples/widgets/itemviews/address_book/doc/address_book.rst create mode 100644 examples/widgets/itemviews/address_book/newaddresstab.py create mode 100644 examples/widgets/itemviews/address_book/tablemodel.py delete mode 100644 examples/widgets/itemviews/addressbook/adddialogwidget.py delete mode 100644 examples/widgets/itemviews/addressbook/addressbook.py delete mode 100644 examples/widgets/itemviews/addressbook/addressbook.pyproject delete mode 100644 examples/widgets/itemviews/addressbook/addresswidget.py delete mode 100644 examples/widgets/itemviews/addressbook/newaddresstab.py delete mode 100644 examples/widgets/itemviews/addressbook/tablemodel.py create mode 100644 examples/widgets/itemviews/jsonmodel/doc/jsonmodel.png create mode 100644 examples/widgets/itemviews/jsonmodel/doc/jsonmodel.rst create mode 100644 examples/widgets/layouts/basiclayouts/doc/basiclayouts.png create mode 100644 examples/widgets/layouts/basiclayouts/doc/basiclayouts.rst create mode 100644 examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.png create mode 100644 examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.rst create mode 100644 examples/widgets/layouts/flowlayout/doc/flowlayout.png create mode 100644 examples/widgets/layouts/flowlayout/doc/flowlayout.rst create mode 100644 examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.png create mode 100644 examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.rst create mode 100644 examples/widgets/painting/basicdrawing/doc/basicdrawing.png create mode 100644 examples/widgets/painting/basicdrawing/doc/basicdrawing.rst create mode 100644 examples/widgets/painting/concentriccircles/doc/concentriccircles.png create mode 100644 examples/widgets/painting/concentriccircles/doc/concentriccircles.rst create mode 100644 examples/widgets/painting/painter/doc/painter.png create mode 100644 examples/widgets/painting/painter/doc/painter.rst create mode 100644 examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.png create mode 100644 examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.rst create mode 100644 examples/widgets/richtext/textedit/doc/textedit.png create mode 100644 examples/widgets/richtext/textedit/doc/textedit.rst create mode 100644 examples/widgets/richtext/textobject/doc/textobject.png create mode 100644 examples/widgets/richtext/textobject/doc/textobject.rst create mode 100644 examples/widgets/state-machine/trafficlight/doc/trafficlight.png create mode 100644 examples/widgets/state-machine/trafficlight/doc/trafficlight.rst create mode 100644 examples/widgets/tutorials/addressbook/doc/addressbook.png create mode 100644 examples/widgets/tutorials/addressbook/doc/addressbook.rst create mode 100644 examples/widgets/tutorials/cannon/doc/cannon.png create mode 100644 examples/widgets/tutorials/cannon/doc/cannon.rst diff --git a/examples/declarative/scrolling/doc/scrolling.png b/examples/declarative/scrolling/doc/scrolling.png new file mode 100644 index 000000000..6058c5930 Binary files /dev/null and b/examples/declarative/scrolling/doc/scrolling.png differ diff --git a/examples/declarative/scrolling/doc/scrolling.rst b/examples/declarative/scrolling/doc/scrolling.rst new file mode 100644 index 000000000..a16e39df7 --- /dev/null +++ b/examples/declarative/scrolling/doc/scrolling.rst @@ -0,0 +1,9 @@ +Scrolling Example +================= + +A Python application that demonstrates how to use a qml view +and make it scrollable. + +.. image:: scrolling.png + :width: 400 + :alt: Scrolling Screenshot diff --git a/examples/declarative/textproperties/doc/textproperties.png b/examples/declarative/textproperties/doc/textproperties.png new file mode 100644 index 000000000..b14bb73d7 Binary files /dev/null and b/examples/declarative/textproperties/doc/textproperties.png differ diff --git a/examples/declarative/textproperties/doc/textproperties.rst b/examples/declarative/textproperties/doc/textproperties.rst new file mode 100644 index 000000000..a67e35f0f --- /dev/null +++ b/examples/declarative/textproperties/doc/textproperties.rst @@ -0,0 +1,9 @@ +Text Properties Example +======================= + +A Python application that demonstrates how to load a qml file +using Material design, to change the look of text. + +.. image:: scrolling.png + :width: 400 + :alt: Text Properties Screenshot diff --git a/examples/declarative/usingmodel/doc/usingmodel.png b/examples/declarative/usingmodel/doc/usingmodel.png new file mode 100644 index 000000000..b4240951b Binary files /dev/null and b/examples/declarative/usingmodel/doc/usingmodel.png differ diff --git a/examples/declarative/usingmodel/doc/usingmodel.rst b/examples/declarative/usingmodel/doc/usingmodel.rst new file mode 100644 index 000000000..11b476d09 --- /dev/null +++ b/examples/declarative/usingmodel/doc/usingmodel.rst @@ -0,0 +1,9 @@ +Using Model Example +=================== + +A Python application that demonstrates how to use a :ref:`QAbstractListModel` +with QML. + +.. image:: usingmodel.png + :width: 400 + :alt: Using Model Screenshot diff --git a/examples/multimedia/audiooutput/doc/audiooutput.png b/examples/multimedia/audiooutput/doc/audiooutput.png new file mode 100644 index 000000000..d19f959cd Binary files /dev/null and b/examples/multimedia/audiooutput/doc/audiooutput.png differ diff --git a/examples/multimedia/audiooutput/doc/audiooutput.rst b/examples/multimedia/audiooutput/doc/audiooutput.rst new file mode 100644 index 000000000..bbd71c5d8 --- /dev/null +++ b/examples/multimedia/audiooutput/doc/audiooutput.rst @@ -0,0 +1,12 @@ +Audio Output Example +==================== + +Audio Output demonstrates the basic use cases of QAudioOutput. + +This example provides a tone generator to supply continuous audio playback. The +first button allows pause and resume of the playback, and the second button +allows toggling between push and pull modes of operation. + +.. image:: audiooutput.png + :width: 400 + :alt: Audio Output Screenshot diff --git a/examples/multimedia/camera/doc/camera.png b/examples/multimedia/camera/doc/camera.png new file mode 100644 index 000000000..7ebf1e826 Binary files /dev/null and b/examples/multimedia/camera/doc/camera.png differ diff --git a/examples/multimedia/camera/doc/camera.rst b/examples/multimedia/camera/doc/camera.rst new file mode 100644 index 000000000..0b37b2c6a --- /dev/null +++ b/examples/multimedia/camera/doc/camera.rst @@ -0,0 +1,12 @@ +Camera Example +=============== + +The Camera Example shows how to use the API to capture a still image or video. + +The Camera Example demonstrates how you can use Qt Multimedia to implement some +basic Camera functionality to take still images and record video clips with +audio. + +.. image:: camera.png + :width: 400 + :alt: Camera Screenshot diff --git a/examples/multimedia/player/doc/player.png b/examples/multimedia/player/doc/player.png new file mode 100644 index 000000000..f751d4a82 Binary files /dev/null and b/examples/multimedia/player/doc/player.png differ diff --git a/examples/multimedia/player/doc/player.rst b/examples/multimedia/player/doc/player.rst new file mode 100644 index 000000000..fdf5fa920 --- /dev/null +++ b/examples/multimedia/player/doc/player.rst @@ -0,0 +1,9 @@ +Player Example +============== + +Media Player demonstrates a simple multimedia player that can play audio and or +video files using various codecs. + +.. image:: player.png + :width: 400 + :alt: Player Screenshot diff --git a/examples/network/downloader/doc/downloader.png b/examples/network/downloader/doc/downloader.png new file mode 100644 index 000000000..92e0e137c Binary files /dev/null and b/examples/network/downloader/doc/downloader.png differ diff --git a/examples/network/downloader/doc/downloader.rst b/examples/network/downloader/doc/downloader.rst new file mode 100644 index 000000000..1ae7a3397 --- /dev/null +++ b/examples/network/downloader/doc/downloader.rst @@ -0,0 +1,9 @@ +Downloader Example +================== + +A Python application that demonstrates how to create a simple downloader +application based on Qt Widgets. + +.. image:: downloader.png + :width: 400 + :alt: Downloader Screenshot diff --git a/examples/widgets/animation/animatedtiles/doc/animatedtiles.png b/examples/widgets/animation/animatedtiles/doc/animatedtiles.png new file mode 100644 index 000000000..95a05fb06 Binary files /dev/null and b/examples/widgets/animation/animatedtiles/doc/animatedtiles.png differ diff --git a/examples/widgets/animation/animatedtiles/doc/animatedtiles.rst b/examples/widgets/animation/animatedtiles/doc/animatedtiles.rst new file mode 100644 index 000000000..083fee071 --- /dev/null +++ b/examples/widgets/animation/animatedtiles/doc/animatedtiles.rst @@ -0,0 +1,8 @@ +Animated Tiles Example +====================== + +The Animated Tiles example animates items in a graphics scene. + +.. image:: animatedtiles.png + :width: 400 + :alt: Animated Tiles Screenshot diff --git a/examples/widgets/animation/appchooser/doc/appchooser.png b/examples/widgets/animation/appchooser/doc/appchooser.png new file mode 100644 index 000000000..1bd31e755 Binary files /dev/null and b/examples/widgets/animation/appchooser/doc/appchooser.png differ diff --git a/examples/widgets/animation/appchooser/doc/appchooser.rst b/examples/widgets/animation/appchooser/doc/appchooser.rst new file mode 100644 index 000000000..05af3e8b8 --- /dev/null +++ b/examples/widgets/animation/appchooser/doc/appchooser.rst @@ -0,0 +1,9 @@ +Application Chooser Example +=========================== + +Simple application that shows the effect of selecting an +application from the corners of the widget. + +.. image:: appchooser.png + :width: 400 + :alt: Application Chooser Screenshot diff --git a/examples/widgets/animation/easing/doc/easing.png b/examples/widgets/animation/easing/doc/easing.png new file mode 100644 index 000000000..f310fa1b3 Binary files /dev/null and b/examples/widgets/animation/easing/doc/easing.png differ diff --git a/examples/widgets/animation/easing/doc/easing.rst b/examples/widgets/animation/easing/doc/easing.rst new file mode 100644 index 000000000..5061319db --- /dev/null +++ b/examples/widgets/animation/easing/doc/easing.rst @@ -0,0 +1,9 @@ +Easing Example +============== + +The Easing Curves example shows how to use easing curves to control the speed +of an animation. + +.. image:: easing.png + :width: 400 + :alt: Easing Screenshot diff --git a/examples/widgets/animation/states/doc/states.png b/examples/widgets/animation/states/doc/states.png new file mode 100644 index 000000000..37fb0c3c9 Binary files /dev/null and b/examples/widgets/animation/states/doc/states.png differ diff --git a/examples/widgets/animation/states/doc/states.rst b/examples/widgets/animation/states/doc/states.rst new file mode 100644 index 000000000..440becced --- /dev/null +++ b/examples/widgets/animation/states/doc/states.rst @@ -0,0 +1,8 @@ +States Example +============== + +The States example shows how to use the Qt state machine to play animations. + +.. image:: states.png + :width: 400 + :alt: States Screenshot diff --git a/examples/widgets/codeeditor/doc/codeeditor.png b/examples/widgets/codeeditor/doc/codeeditor.png new file mode 100644 index 000000000..e3def6b04 Binary files /dev/null and b/examples/widgets/codeeditor/doc/codeeditor.png differ diff --git a/examples/widgets/codeeditor/doc/codeeditor.rst b/examples/widgets/codeeditor/doc/codeeditor.rst new file mode 100644 index 000000000..16345eaa3 --- /dev/null +++ b/examples/widgets/codeeditor/doc/codeeditor.rst @@ -0,0 +1,9 @@ +Code Editor Example +=================== + +The Code Editor example shows how to create a simple editor that has line +numbers and that highlights the current line. + +.. image:: painter.png + :width: 400 + :alt: Code Editor Screenshot diff --git a/examples/widgets/itemviews/address_book/adddialogwidget.py b/examples/widgets/itemviews/address_book/adddialogwidget.py new file mode 100644 index 000000000..1462711a8 --- /dev/null +++ b/examples/widgets/itemviews/address_book/adddialogwidget.py @@ -0,0 +1,103 @@ + +############################################################################# +## +## Copyright (C) 2011 Arun Srinivasan +## 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$ +## +############################################################################# + +from PySide6.QtCore import Qt +from PySide6.QtWidgets import (QDialog, QLabel, QTextEdit, QLineEdit, + QDialogButtonBox, QGridLayout, QVBoxLayout) + + +class AddDialogWidget(QDialog): + """ A dialog to add a new address to the addressbook. """ + + def __init__(self, parent=None): + super().__init__(parent) + + name_label = QLabel("Name") + address_label = QLabel("Address") + button_box = QDialogButtonBox(QDialogButtonBox.Ok | + QDialogButtonBox.Cancel) + + self._name_text = QLineEdit() + self._address_text = QTextEdit() + + grid = QGridLayout() + grid.setColumnStretch(1, 2) + grid.addWidget(name_label, 0, 0) + grid.addWidget(self._name_text, 0, 1) + grid.addWidget(address_label, 1, 0, Qt.AlignLeft | Qt.AlignTop) + grid.addWidget(self._address_text, 1, 1, Qt.AlignLeft) + + layout = QVBoxLayout() + layout.addLayout(grid) + layout.addWidget(button_box) + + self.setLayout(layout) + + self.setWindowTitle("Add a Contact") + + button_box.accepted.connect(self.accept) + button_box.rejected.connect(self.reject) + + # These properties make using this dialog a little cleaner. It's much + # nicer to type "addDialog.address" to retrieve the address as compared + # to "addDialog.addressText.toPlainText()" + @property + def name(self): + return self._name_text.text() + + @property + def address(self): + return self._address_text.toPlainText() + + +if __name__ == "__main__": + import sys + from PySide6.QtWidgets import QApplication + + app = QApplication(sys.argv) + + dialog = AddDialogWidget() + if (dialog.exec()): + name = dialog.name + address = dialog.address + print(f"Name: {name}") + print(f"Address: {address}") diff --git a/examples/widgets/itemviews/address_book/address_book.py b/examples/widgets/itemviews/address_book/address_book.py new file mode 100644 index 000000000..89518f63e --- /dev/null +++ b/examples/widgets/itemviews/address_book/address_book.py @@ -0,0 +1,131 @@ + +############################################################################# +## +## Copyright (C) 2011 Arun Srinivasan +## 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$ +## +############################################################################# + +from PySide6.QtGui import QAction +from PySide6.QtWidgets import (QMainWindow, QFileDialog, QApplication) + +from addresswidget import AddressWidget + + +class MainWindow(QMainWindow): + + def __init__(self, parent=None): + super().__init__(parent) + + self._address_widget = AddressWidget() + self.setCentralWidget(self._address_widget) + self.create_menus() + self.setWindowTitle("Address Book") + + def create_menus(self): + # Create the main menuBar menu items + file_menu = self.menuBar().addMenu("&File") + tool_menu = self.menuBar().addMenu("&Tools") + + # Populate the File menu + open_action = self.create_action("&Open...", file_menu, self.open_file) + save_action = self.create_action("&Save As...", file_menu, self.save_file) + file_menu.addSeparator() + exit_action = self.create_action("E&xit", file_menu, self.close) + + # Populate the Tools menu + add_action = self.create_action("&Add Entry...", tool_menu, self._address_widget.add_entry) + self._edit_action = self.create_action("&Edit Entry...", tool_menu, self._address_widget.edit_entry) + tool_menu.addSeparator() + self._remove_action = self.create_action("&Remove Entry", tool_menu, self._address_widget.remove_entry) + + # Disable the edit and remove menu items initially, as there are + # no items yet. + self._edit_action.setEnabled(False) + self._remove_action.setEnabled(False) + + # Wire up the updateActions slot + self._address_widget.selection_changed.connect(self.update_actions) + + def create_action(self, text, menu, slot): + """ Helper function to save typing when populating menus + with action. + """ + action = QAction(text, self) + menu.addAction(action) + action.triggered.connect(slot) + return action + + # Quick gotcha: + # + # QFiledialog.getOpenFilename and QFileDialog.get.SaveFileName don't + # behave in PySide6 as they do in Qt, where they return a QString + # containing the filename. + # + # In PySide6, these functions return a tuple: (filename, filter) + + def open_file(self): + filename, _ = QFileDialog.getOpenFileName(self) + if filename: + self._address_widget.read_from_file(filename) + + def save_file(self): + filename, _ = QFileDialog.getSaveFileName(self) + if filename: + self._address_widget.write_to_file(filename) + + def update_actions(self, selection): + """ Only allow the user to remove or edit an item if an item + is actually selected. + """ + indexes = selection.indexes() + + if len(indexes) > 0: + self._remove_action.setEnabled(True) + self._edit_action.setEnabled(True) + else: + self._remove_action.setEnabled(False) + self._edit_action.setEnabled(False) + + +if __name__ == "__main__": + """ Run the application. """ + import sys + app = QApplication(sys.argv) + mw = MainWindow() + mw.show() + sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/address_book/address_book.pyproject b/examples/widgets/itemviews/address_book/address_book.pyproject new file mode 100644 index 000000000..1b5dd597c --- /dev/null +++ b/examples/widgets/itemviews/address_book/address_book.pyproject @@ -0,0 +1,4 @@ +{ + "files": ["tablemodel.py", "address_book.py", "adddialogwidget.py", + "addresswidget.py", "newaddresstab.py"] +} diff --git a/examples/widgets/itemviews/address_book/addresswidget.py b/examples/widgets/itemviews/address_book/addresswidget.py new file mode 100644 index 000000000..f0778d19f --- /dev/null +++ b/examples/widgets/itemviews/address_book/addresswidget.py @@ -0,0 +1,249 @@ + +############################################################################# +## +## Copyright (C) 2011 Arun Srinivasan +## 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$ +## +############################################################################# + +try: + import cpickle as pickle +except ImportError: + import pickle + +from PySide6.QtCore import (Qt, Signal, QRegularExpression, QModelIndex, + QItemSelection, QSortFilterProxyModel) +from PySide6.QtWidgets import QTabWidget, QMessageBox, QTableView, QAbstractItemView + +from tablemodel import TableModel +from newaddresstab import NewAddressTab +from adddialogwidget import AddDialogWidget + + +class AddressWidget(QTabWidget): + """ The central widget of the application. Most of the addressbook's + functionality is contained in this class. + """ + + selection_changed = Signal(QItemSelection) + + def __init__(self, parent=None): + """ Initialize the AddressWidget. """ + super().__init__(parent) + + self._table_model = TableModel() + self._new_address_tab = NewAddressTab() + self._new_address_tab.send_details.connect(self.add_entry) + + self.addTab(self._new_address_tab, "Address Book") + + self.setup_tabs() + + def add_entry(self, name=None, address=None): + """ Add an entry to the addressbook. """ + if name is None and address is None: + add_dialog = AddDialogWidget() + + if add_dialog.exec(): + name = add_dialog.name + address = add_dialog.address + + address = {"name": name, "address": address} + addresses = self._table_model.addresses[:] + + # The QT docs for this example state that what we're doing here + # is checking if the entered name already exists. What they + # (and we here) are actually doing is checking if the whole + # name/address pair exists already - ok for the purposes of this + # example, but obviously not how a real addressbook application + # should behave. + try: + addresses.remove(address) + QMessageBox.information(self, "Duplicate Name", + f'The name "{name}" already exists.') + except ValueError: + # The address didn't already exist, so let's add it to the model. + + # Step 1: create the row + self._table_model.insertRows(0) + + # Step 2: get the index of the newly created row and use it. + # to set the name + ix = self._table_model.index(0, 0, QModelIndex()) + self._table_model.setData(ix, address["name"], Qt.EditRole) + + # Step 3: lather, rinse, repeat for the address. + ix = self._table_model.index(0, 1, QModelIndex()) + self._table_model.setData(ix, address["address"], Qt.EditRole) + + # Remove the newAddressTab, as we now have at least one + # address in the model. + self.removeTab(self.indexOf(self._new_address_tab)) + + # The screenshot for the QT example shows nicely formatted + # multiline cells, but the actual application doesn't behave + # quite so nicely, at least on Ubuntu. Here we resize the newly + # created row so that multiline addresses look reasonable. + table_view = self.currentWidget() + table_view.resizeRowToContents(ix.row()) + + def edit_entry(self): + """ Edit an entry in the addressbook. """ + table_view = self.currentWidget() + proxy_model = table_view.model() + selection_model = table_view.selectionModel() + + # Get the name and address of the currently selected row. + indexes = selection_model.selectedRows() + if len(indexes) != 1: + return + + row = proxy_model.mapToSource(indexes[0]).row() + ix = self._table_model.index(row, 0, QModelIndex()) + name = self._table_model.data(ix, Qt.DisplayRole) + ix = self._table_model.index(row, 1, QModelIndex()) + address = self._table_model.data(ix, Qt.DisplayRole) + + # Open an addDialogWidget, and only allow the user to edit the address. + add_dialog = AddDialogWidget() + add_dialog.setWindowTitle("Edit a Contact") + + add_dialog._name_text.setReadOnly(True) + add_dialog._name_text.setText(name) + add_dialog._address_text.setText(address) + + # If the address is different, add it to the model. + if add_dialog.exec(): + new_address = add_dialog.address + if new_address != address: + ix = self._table_model.index(row, 1, QModelIndex()) + self._table_model.setData(ix, new_address, Qt.EditRole) + + def remove_entry(self): + """ Remove an entry from the addressbook. """ + table_view = self.currentWidget() + proxy_model = table_view.model() + selection_model = table_view.selectionModel() + + # Just like editEntry, but this time remove the selected row. + indexes = selection_model.selectedRows() + + for index in indexes: + row = proxy_model.mapToSource(index).row() + self._table_model.removeRows(row) + + # If we've removed the last address in the model, display the + # newAddressTab + if self._table_model.rowCount() == 0: + self.insertTab(0, self._new_address_tab, "Address Book") + + def setup_tabs(self): + """ Setup the various tabs in the AddressWidget. """ + groups = ["ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ"] + + for group in groups: + proxy_model = QSortFilterProxyModel(self) + proxy_model.setSourceModel(self._table_model) + proxy_model.setDynamicSortFilter(True) + + table_view = QTableView() + table_view.setModel(proxy_model) + table_view.setSortingEnabled(True) + table_view.setSelectionBehavior(QAbstractItemView.SelectRows) + table_view.horizontalHeader().setStretchLastSection(True) + table_view.verticalHeader().hide() + table_view.setEditTriggers(QAbstractItemView.NoEditTriggers) + table_view.setSelectionMode(QAbstractItemView.SingleSelection) + + # This here be the magic: we use the group name (e.g. "ABC") to + # build the regex for the QSortFilterProxyModel for the group's + # tab. The regex will end up looking like "^[ABC].*", only + # allowing this tab to display items where the name starts with + # "A", "B", or "C". Notice that we set it to be case-insensitive. + re = QRegularExpression(f"^[{group}].*") + assert re.isValid() + re.setPatternOptions(QRegularExpression.CaseInsensitiveOption) + proxy_model.setFilterRegularExpression(re) + proxy_model.setFilterKeyColumn(0) # Filter on the "name" column + proxy_model.sort(0, Qt.AscendingOrder) + + # This prevents an application crash (see: http://www.qtcentre.org/threads/58874-QListView-SelectionModel-selectionChanged-Crash) + viewselectionmodel = table_view.selectionModel() + table_view.selectionModel().selectionChanged.connect(self.selection_changed) + + self.addTab(table_view, group) + + # Note: the QT example uses a QDataStream for the saving and loading. + # Here we're using a python dictionary to store the addresses, which + # can't be streamed using QDataStream, so we just use cpickle for this + # example. + def read_from_file(self, filename): + """ Read contacts in from a file. """ + try: + f = open(filename, "rb") + addresses = pickle.load(f) + except IOError: + QMessageBox.information(self, f"Unable to open file: {filename}") + finally: + f.close() + + if len(addresses) == 0: + QMessageBox.information(self, f"No contacts in file: {filename}") + else: + for address in addresses: + self.add_entry(address["name"], address["address"]) + + def write_to_file(self, filename): + """ Save all contacts in the model to a file. """ + try: + f = open(filename, "wb") + pickle.dump(self._table_model.addresses, f) + + except IOError: + QMessageBox.information(self, f"Unable to open file: {filename}") + finally: + f.close() + + +if __name__ == "__main__": + import sys + from PySide6.QtWidgets import QApplication + + app = QApplication(sys.argv) + address_widget = AddressWidget() + address_widget.show() + sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/address_book/doc/address_book.png b/examples/widgets/itemviews/address_book/doc/address_book.png new file mode 100644 index 000000000..40f2d9e63 Binary files /dev/null and b/examples/widgets/itemviews/address_book/doc/address_book.png differ diff --git a/examples/widgets/itemviews/address_book/doc/address_book.rst b/examples/widgets/itemviews/address_book/doc/address_book.rst new file mode 100644 index 000000000..04b91054e --- /dev/null +++ b/examples/widgets/itemviews/address_book/doc/address_book.rst @@ -0,0 +1,9 @@ +Address Book Example +==================== + +The address book example shows how to use proxy models to display different +views onto data from a single model. + +.. image:: address_book.png + :width: 400 + :alt: Address Book Screenshot diff --git a/examples/widgets/itemviews/address_book/newaddresstab.py b/examples/widgets/itemviews/address_book/newaddresstab.py new file mode 100644 index 000000000..407c48aec --- /dev/null +++ b/examples/widgets/itemviews/address_book/newaddresstab.py @@ -0,0 +1,94 @@ + +############################################################################# +## +## Copyright (C) 2011 Arun Srinivasan +## 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$ +## +############################################################################# + +from PySide6.QtCore import (Qt, Signal) +from PySide6.QtWidgets import (QWidget, QLabel, QPushButton, QVBoxLayout) + +from adddialogwidget import AddDialogWidget + + +class NewAddressTab(QWidget): + """ An extra tab that prompts the user to add new contacts. + To be displayed only when there are no contacts in the model. + """ + + send_details = Signal(str, str) + + def __init__(self, parent=None): + super().__init__(parent) + + description_label = QLabel("There are no contacts in your address book." + "\nClick Add to add new contacts.") + + add_button = QPushButton("Add") + + layout = QVBoxLayout() + layout.addWidget(description_label) + layout.addWidget(add_button, 0, Qt.AlignCenter) + + self.setLayout(layout) + + add_button.clicked.connect(self.add_entry) + + def add_entry(self): + add_dialog = AddDialogWidget() + + if add_dialog.exec(): + name = add_dialog.name + address = add_dialog.address + self.send_details.emit(name, address) + + +if __name__ == "__main__": + + def print_address(name, address): + print(f"Name: {name}") + print(f"Address: {address}") + + import sys + from PySide6.QtWidgets import QApplication + + app = QApplication(sys.argv) + new_address_tab = NewAddressTab() + new_address_tab.send_details.connect(print_address) + new_address_tab.show() + sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/address_book/tablemodel.py b/examples/widgets/itemviews/address_book/tablemodel.py new file mode 100644 index 000000000..fc1a65e1c --- /dev/null +++ b/examples/widgets/itemviews/address_book/tablemodel.py @@ -0,0 +1,147 @@ + +############################################################################# +## +## Copyright (C) 2011 Arun Srinivasan +## 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$ +## +############################################################################# + +from PySide6.QtCore import (Qt, QAbstractTableModel, QModelIndex) + + +class TableModel(QAbstractTableModel): + + def __init__(self, addresses=None, parent=None): + super().__init__(parent) + + if addresses is None: + self.addresses = [] + else: + self.addresses = addresses + + def rowCount(self, index=QModelIndex()): + """ Returns the number of rows the model holds. """ + return len(self.addresses) + + def columnCount(self, index=QModelIndex()): + """ Returns the number of columns the model holds. """ + return 2 + + def data(self, index, role=Qt.DisplayRole): + """ Depending on the index and role given, return data. If not + returning data, return None (PySide equivalent of QT's + "invalid QVariant"). + """ + if not index.isValid(): + return None + + if not 0 <= index.row() < len(self.addresses): + return None + + if role == Qt.DisplayRole: + name = self.addresses[index.row()]["name"] + address = self.addresses[index.row()]["address"] + + if index.column() == 0: + return name + elif index.column() == 1: + return address + + return None + + def headerData(self, section, orientation, role=Qt.DisplayRole): + """ Set the headers to be displayed. """ + if role != Qt.DisplayRole: + return None + + if orientation == Qt.Horizontal: + if section == 0: + return "Name" + elif section == 1: + return "Address" + + return None + + def insertRows(self, position, rows=1, index=QModelIndex()): + """ Insert a row into the model. """ + self.beginInsertRows(QModelIndex(), position, position + rows - 1) + + for row in range(rows): + self.addresses.insert(position + row, {"name": "", "address": ""}) + + self.endInsertRows() + return True + + def removeRows(self, position, rows=1, index=QModelIndex()): + """ Remove a row from the model. """ + self.beginRemoveRows(QModelIndex(), position, position + rows - 1) + + del self.addresses[position:position + rows] + + self.endRemoveRows() + return True + + def setData(self, index, value, role=Qt.EditRole): + """ Adjust the data (set it to ) depending on the given + index and role. + """ + if role != Qt.EditRole: + return False + + if index.isValid() and 0 <= index.row() < len(self.addresses): + address = self.addresses[index.row()] + if index.column() == 0: + address["name"] = value + elif index.column() == 1: + address["address"] = value + else: + return False + + self.dataChanged.emit(index, index, 0) + return True + + return False + + def flags(self, index): + """ Set the item flags at the given index. Seems like we're + implementing this function just to see how it's done, as we + manually adjust each tableView to have NoEditTriggers. + """ + if not index.isValid(): + return Qt.ItemIsEnabled + return Qt.ItemFlags(QAbstractTableModel.flags(self, index) | + Qt.ItemIsEditable) diff --git a/examples/widgets/itemviews/addressbook/adddialogwidget.py b/examples/widgets/itemviews/addressbook/adddialogwidget.py deleted file mode 100644 index 1462711a8..000000000 --- a/examples/widgets/itemviews/addressbook/adddialogwidget.py +++ /dev/null @@ -1,103 +0,0 @@ - -############################################################################# -## -## Copyright (C) 2011 Arun Srinivasan -## 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$ -## -############################################################################# - -from PySide6.QtCore import Qt -from PySide6.QtWidgets import (QDialog, QLabel, QTextEdit, QLineEdit, - QDialogButtonBox, QGridLayout, QVBoxLayout) - - -class AddDialogWidget(QDialog): - """ A dialog to add a new address to the addressbook. """ - - def __init__(self, parent=None): - super().__init__(parent) - - name_label = QLabel("Name") - address_label = QLabel("Address") - button_box = QDialogButtonBox(QDialogButtonBox.Ok | - QDialogButtonBox.Cancel) - - self._name_text = QLineEdit() - self._address_text = QTextEdit() - - grid = QGridLayout() - grid.setColumnStretch(1, 2) - grid.addWidget(name_label, 0, 0) - grid.addWidget(self._name_text, 0, 1) - grid.addWidget(address_label, 1, 0, Qt.AlignLeft | Qt.AlignTop) - grid.addWidget(self._address_text, 1, 1, Qt.AlignLeft) - - layout = QVBoxLayout() - layout.addLayout(grid) - layout.addWidget(button_box) - - self.setLayout(layout) - - self.setWindowTitle("Add a Contact") - - button_box.accepted.connect(self.accept) - button_box.rejected.connect(self.reject) - - # These properties make using this dialog a little cleaner. It's much - # nicer to type "addDialog.address" to retrieve the address as compared - # to "addDialog.addressText.toPlainText()" - @property - def name(self): - return self._name_text.text() - - @property - def address(self): - return self._address_text.toPlainText() - - -if __name__ == "__main__": - import sys - from PySide6.QtWidgets import QApplication - - app = QApplication(sys.argv) - - dialog = AddDialogWidget() - if (dialog.exec()): - name = dialog.name - address = dialog.address - print(f"Name: {name}") - print(f"Address: {address}") diff --git a/examples/widgets/itemviews/addressbook/addressbook.py b/examples/widgets/itemviews/addressbook/addressbook.py deleted file mode 100644 index 89518f63e..000000000 --- a/examples/widgets/itemviews/addressbook/addressbook.py +++ /dev/null @@ -1,131 +0,0 @@ - -############################################################################# -## -## Copyright (C) 2011 Arun Srinivasan -## 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$ -## -############################################################################# - -from PySide6.QtGui import QAction -from PySide6.QtWidgets import (QMainWindow, QFileDialog, QApplication) - -from addresswidget import AddressWidget - - -class MainWindow(QMainWindow): - - def __init__(self, parent=None): - super().__init__(parent) - - self._address_widget = AddressWidget() - self.setCentralWidget(self._address_widget) - self.create_menus() - self.setWindowTitle("Address Book") - - def create_menus(self): - # Create the main menuBar menu items - file_menu = self.menuBar().addMenu("&File") - tool_menu = self.menuBar().addMenu("&Tools") - - # Populate the File menu - open_action = self.create_action("&Open...", file_menu, self.open_file) - save_action = self.create_action("&Save As...", file_menu, self.save_file) - file_menu.addSeparator() - exit_action = self.create_action("E&xit", file_menu, self.close) - - # Populate the Tools menu - add_action = self.create_action("&Add Entry...", tool_menu, self._address_widget.add_entry) - self._edit_action = self.create_action("&Edit Entry...", tool_menu, self._address_widget.edit_entry) - tool_menu.addSeparator() - self._remove_action = self.create_action("&Remove Entry", tool_menu, self._address_widget.remove_entry) - - # Disable the edit and remove menu items initially, as there are - # no items yet. - self._edit_action.setEnabled(False) - self._remove_action.setEnabled(False) - - # Wire up the updateActions slot - self._address_widget.selection_changed.connect(self.update_actions) - - def create_action(self, text, menu, slot): - """ Helper function to save typing when populating menus - with action. - """ - action = QAction(text, self) - menu.addAction(action) - action.triggered.connect(slot) - return action - - # Quick gotcha: - # - # QFiledialog.getOpenFilename and QFileDialog.get.SaveFileName don't - # behave in PySide6 as they do in Qt, where they return a QString - # containing the filename. - # - # In PySide6, these functions return a tuple: (filename, filter) - - def open_file(self): - filename, _ = QFileDialog.getOpenFileName(self) - if filename: - self._address_widget.read_from_file(filename) - - def save_file(self): - filename, _ = QFileDialog.getSaveFileName(self) - if filename: - self._address_widget.write_to_file(filename) - - def update_actions(self, selection): - """ Only allow the user to remove or edit an item if an item - is actually selected. - """ - indexes = selection.indexes() - - if len(indexes) > 0: - self._remove_action.setEnabled(True) - self._edit_action.setEnabled(True) - else: - self._remove_action.setEnabled(False) - self._edit_action.setEnabled(False) - - -if __name__ == "__main__": - """ Run the application. """ - import sys - app = QApplication(sys.argv) - mw = MainWindow() - mw.show() - sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/addressbook/addressbook.pyproject b/examples/widgets/itemviews/addressbook/addressbook.pyproject deleted file mode 100644 index 2aa763753..000000000 --- a/examples/widgets/itemviews/addressbook/addressbook.pyproject +++ /dev/null @@ -1,4 +0,0 @@ -{ - "files": ["tablemodel.py", "addressbook.py", "adddialogwidget.py", - "addresswidget.py", "newaddresstab.py"] -} diff --git a/examples/widgets/itemviews/addressbook/addresswidget.py b/examples/widgets/itemviews/addressbook/addresswidget.py deleted file mode 100644 index f0778d19f..000000000 --- a/examples/widgets/itemviews/addressbook/addresswidget.py +++ /dev/null @@ -1,249 +0,0 @@ - -############################################################################# -## -## Copyright (C) 2011 Arun Srinivasan -## 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$ -## -############################################################################# - -try: - import cpickle as pickle -except ImportError: - import pickle - -from PySide6.QtCore import (Qt, Signal, QRegularExpression, QModelIndex, - QItemSelection, QSortFilterProxyModel) -from PySide6.QtWidgets import QTabWidget, QMessageBox, QTableView, QAbstractItemView - -from tablemodel import TableModel -from newaddresstab import NewAddressTab -from adddialogwidget import AddDialogWidget - - -class AddressWidget(QTabWidget): - """ The central widget of the application. Most of the addressbook's - functionality is contained in this class. - """ - - selection_changed = Signal(QItemSelection) - - def __init__(self, parent=None): - """ Initialize the AddressWidget. """ - super().__init__(parent) - - self._table_model = TableModel() - self._new_address_tab = NewAddressTab() - self._new_address_tab.send_details.connect(self.add_entry) - - self.addTab(self._new_address_tab, "Address Book") - - self.setup_tabs() - - def add_entry(self, name=None, address=None): - """ Add an entry to the addressbook. """ - if name is None and address is None: - add_dialog = AddDialogWidget() - - if add_dialog.exec(): - name = add_dialog.name - address = add_dialog.address - - address = {"name": name, "address": address} - addresses = self._table_model.addresses[:] - - # The QT docs for this example state that what we're doing here - # is checking if the entered name already exists. What they - # (and we here) are actually doing is checking if the whole - # name/address pair exists already - ok for the purposes of this - # example, but obviously not how a real addressbook application - # should behave. - try: - addresses.remove(address) - QMessageBox.information(self, "Duplicate Name", - f'The name "{name}" already exists.') - except ValueError: - # The address didn't already exist, so let's add it to the model. - - # Step 1: create the row - self._table_model.insertRows(0) - - # Step 2: get the index of the newly created row and use it. - # to set the name - ix = self._table_model.index(0, 0, QModelIndex()) - self._table_model.setData(ix, address["name"], Qt.EditRole) - - # Step 3: lather, rinse, repeat for the address. - ix = self._table_model.index(0, 1, QModelIndex()) - self._table_model.setData(ix, address["address"], Qt.EditRole) - - # Remove the newAddressTab, as we now have at least one - # address in the model. - self.removeTab(self.indexOf(self._new_address_tab)) - - # The screenshot for the QT example shows nicely formatted - # multiline cells, but the actual application doesn't behave - # quite so nicely, at least on Ubuntu. Here we resize the newly - # created row so that multiline addresses look reasonable. - table_view = self.currentWidget() - table_view.resizeRowToContents(ix.row()) - - def edit_entry(self): - """ Edit an entry in the addressbook. """ - table_view = self.currentWidget() - proxy_model = table_view.model() - selection_model = table_view.selectionModel() - - # Get the name and address of the currently selected row. - indexes = selection_model.selectedRows() - if len(indexes) != 1: - return - - row = proxy_model.mapToSource(indexes[0]).row() - ix = self._table_model.index(row, 0, QModelIndex()) - name = self._table_model.data(ix, Qt.DisplayRole) - ix = self._table_model.index(row, 1, QModelIndex()) - address = self._table_model.data(ix, Qt.DisplayRole) - - # Open an addDialogWidget, and only allow the user to edit the address. - add_dialog = AddDialogWidget() - add_dialog.setWindowTitle("Edit a Contact") - - add_dialog._name_text.setReadOnly(True) - add_dialog._name_text.setText(name) - add_dialog._address_text.setText(address) - - # If the address is different, add it to the model. - if add_dialog.exec(): - new_address = add_dialog.address - if new_address != address: - ix = self._table_model.index(row, 1, QModelIndex()) - self._table_model.setData(ix, new_address, Qt.EditRole) - - def remove_entry(self): - """ Remove an entry from the addressbook. """ - table_view = self.currentWidget() - proxy_model = table_view.model() - selection_model = table_view.selectionModel() - - # Just like editEntry, but this time remove the selected row. - indexes = selection_model.selectedRows() - - for index in indexes: - row = proxy_model.mapToSource(index).row() - self._table_model.removeRows(row) - - # If we've removed the last address in the model, display the - # newAddressTab - if self._table_model.rowCount() == 0: - self.insertTab(0, self._new_address_tab, "Address Book") - - def setup_tabs(self): - """ Setup the various tabs in the AddressWidget. """ - groups = ["ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ"] - - for group in groups: - proxy_model = QSortFilterProxyModel(self) - proxy_model.setSourceModel(self._table_model) - proxy_model.setDynamicSortFilter(True) - - table_view = QTableView() - table_view.setModel(proxy_model) - table_view.setSortingEnabled(True) - table_view.setSelectionBehavior(QAbstractItemView.SelectRows) - table_view.horizontalHeader().setStretchLastSection(True) - table_view.verticalHeader().hide() - table_view.setEditTriggers(QAbstractItemView.NoEditTriggers) - table_view.setSelectionMode(QAbstractItemView.SingleSelection) - - # This here be the magic: we use the group name (e.g. "ABC") to - # build the regex for the QSortFilterProxyModel for the group's - # tab. The regex will end up looking like "^[ABC].*", only - # allowing this tab to display items where the name starts with - # "A", "B", or "C". Notice that we set it to be case-insensitive. - re = QRegularExpression(f"^[{group}].*") - assert re.isValid() - re.setPatternOptions(QRegularExpression.CaseInsensitiveOption) - proxy_model.setFilterRegularExpression(re) - proxy_model.setFilterKeyColumn(0) # Filter on the "name" column - proxy_model.sort(0, Qt.AscendingOrder) - - # This prevents an application crash (see: http://www.qtcentre.org/threads/58874-QListView-SelectionModel-selectionChanged-Crash) - viewselectionmodel = table_view.selectionModel() - table_view.selectionModel().selectionChanged.connect(self.selection_changed) - - self.addTab(table_view, group) - - # Note: the QT example uses a QDataStream for the saving and loading. - # Here we're using a python dictionary to store the addresses, which - # can't be streamed using QDataStream, so we just use cpickle for this - # example. - def read_from_file(self, filename): - """ Read contacts in from a file. """ - try: - f = open(filename, "rb") - addresses = pickle.load(f) - except IOError: - QMessageBox.information(self, f"Unable to open file: {filename}") - finally: - f.close() - - if len(addresses) == 0: - QMessageBox.information(self, f"No contacts in file: {filename}") - else: - for address in addresses: - self.add_entry(address["name"], address["address"]) - - def write_to_file(self, filename): - """ Save all contacts in the model to a file. """ - try: - f = open(filename, "wb") - pickle.dump(self._table_model.addresses, f) - - except IOError: - QMessageBox.information(self, f"Unable to open file: {filename}") - finally: - f.close() - - -if __name__ == "__main__": - import sys - from PySide6.QtWidgets import QApplication - - app = QApplication(sys.argv) - address_widget = AddressWidget() - address_widget.show() - sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/addressbook/newaddresstab.py b/examples/widgets/itemviews/addressbook/newaddresstab.py deleted file mode 100644 index 407c48aec..000000000 --- a/examples/widgets/itemviews/addressbook/newaddresstab.py +++ /dev/null @@ -1,94 +0,0 @@ - -############################################################################# -## -## Copyright (C) 2011 Arun Srinivasan -## 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$ -## -############################################################################# - -from PySide6.QtCore import (Qt, Signal) -from PySide6.QtWidgets import (QWidget, QLabel, QPushButton, QVBoxLayout) - -from adddialogwidget import AddDialogWidget - - -class NewAddressTab(QWidget): - """ An extra tab that prompts the user to add new contacts. - To be displayed only when there are no contacts in the model. - """ - - send_details = Signal(str, str) - - def __init__(self, parent=None): - super().__init__(parent) - - description_label = QLabel("There are no contacts in your address book." - "\nClick Add to add new contacts.") - - add_button = QPushButton("Add") - - layout = QVBoxLayout() - layout.addWidget(description_label) - layout.addWidget(add_button, 0, Qt.AlignCenter) - - self.setLayout(layout) - - add_button.clicked.connect(self.add_entry) - - def add_entry(self): - add_dialog = AddDialogWidget() - - if add_dialog.exec(): - name = add_dialog.name - address = add_dialog.address - self.send_details.emit(name, address) - - -if __name__ == "__main__": - - def print_address(name, address): - print(f"Name: {name}") - print(f"Address: {address}") - - import sys - from PySide6.QtWidgets import QApplication - - app = QApplication(sys.argv) - new_address_tab = NewAddressTab() - new_address_tab.send_details.connect(print_address) - new_address_tab.show() - sys.exit(app.exec()) diff --git a/examples/widgets/itemviews/addressbook/tablemodel.py b/examples/widgets/itemviews/addressbook/tablemodel.py deleted file mode 100644 index fc1a65e1c..000000000 --- a/examples/widgets/itemviews/addressbook/tablemodel.py +++ /dev/null @@ -1,147 +0,0 @@ - -############################################################################# -## -## Copyright (C) 2011 Arun Srinivasan -## 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$ -## -############################################################################# - -from PySide6.QtCore import (Qt, QAbstractTableModel, QModelIndex) - - -class TableModel(QAbstractTableModel): - - def __init__(self, addresses=None, parent=None): - super().__init__(parent) - - if addresses is None: - self.addresses = [] - else: - self.addresses = addresses - - def rowCount(self, index=QModelIndex()): - """ Returns the number of rows the model holds. """ - return len(self.addresses) - - def columnCount(self, index=QModelIndex()): - """ Returns the number of columns the model holds. """ - return 2 - - def data(self, index, role=Qt.DisplayRole): - """ Depending on the index and role given, return data. If not - returning data, return None (PySide equivalent of QT's - "invalid QVariant"). - """ - if not index.isValid(): - return None - - if not 0 <= index.row() < len(self.addresses): - return None - - if role == Qt.DisplayRole: - name = self.addresses[index.row()]["name"] - address = self.addresses[index.row()]["address"] - - if index.column() == 0: - return name - elif index.column() == 1: - return address - - return None - - def headerData(self, section, orientation, role=Qt.DisplayRole): - """ Set the headers to be displayed. """ - if role != Qt.DisplayRole: - return None - - if orientation == Qt.Horizontal: - if section == 0: - return "Name" - elif section == 1: - return "Address" - - return None - - def insertRows(self, position, rows=1, index=QModelIndex()): - """ Insert a row into the model. """ - self.beginInsertRows(QModelIndex(), position, position + rows - 1) - - for row in range(rows): - self.addresses.insert(position + row, {"name": "", "address": ""}) - - self.endInsertRows() - return True - - def removeRows(self, position, rows=1, index=QModelIndex()): - """ Remove a row from the model. """ - self.beginRemoveRows(QModelIndex(), position, position + rows - 1) - - del self.addresses[position:position + rows] - - self.endRemoveRows() - return True - - def setData(self, index, value, role=Qt.EditRole): - """ Adjust the data (set it to ) depending on the given - index and role. - """ - if role != Qt.EditRole: - return False - - if index.isValid() and 0 <= index.row() < len(self.addresses): - address = self.addresses[index.row()] - if index.column() == 0: - address["name"] = value - elif index.column() == 1: - address["address"] = value - else: - return False - - self.dataChanged.emit(index, index, 0) - return True - - return False - - def flags(self, index): - """ Set the item flags at the given index. Seems like we're - implementing this function just to see how it's done, as we - manually adjust each tableView to have NoEditTriggers. - """ - if not index.isValid(): - return Qt.ItemIsEnabled - return Qt.ItemFlags(QAbstractTableModel.flags(self, index) | - Qt.ItemIsEditable) diff --git a/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.png b/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.png new file mode 100644 index 000000000..8b5c8d0c3 Binary files /dev/null and b/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.png differ diff --git a/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.rst b/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.rst new file mode 100644 index 000000000..d5e2831bf --- /dev/null +++ b/examples/widgets/itemviews/jsonmodel/doc/jsonmodel.rst @@ -0,0 +1,8 @@ +JSON Model Example +================== + +Simple example to visualize the values of a JSON file. + +.. image:: jsonmodel.png + :width: 400 + :alt: JSON Model Screenshot diff --git a/examples/widgets/layouts/basiclayouts/doc/basiclayouts.png b/examples/widgets/layouts/basiclayouts/doc/basiclayouts.png new file mode 100644 index 000000000..e3b8537cd Binary files /dev/null and b/examples/widgets/layouts/basiclayouts/doc/basiclayouts.png differ diff --git a/examples/widgets/layouts/basiclayouts/doc/basiclayouts.rst b/examples/widgets/layouts/basiclayouts/doc/basiclayouts.rst new file mode 100644 index 000000000..caf04d428 --- /dev/null +++ b/examples/widgets/layouts/basiclayouts/doc/basiclayouts.rst @@ -0,0 +1,9 @@ +Basic Layouts Example +===================== + +Basic Layouts shows how to use the standard layout managers that are available +in Qt Widgets: QBoxLayout, QGridLayout, and QFormLayout. + +.. image:: basiclayouts.png + :width: 400 + :alt: Basic Layouts Screenshot diff --git a/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.png b/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.png new file mode 100644 index 000000000..505a328b3 Binary files /dev/null and b/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.png differ diff --git a/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.rst b/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.rst new file mode 100644 index 000000000..67179bf44 --- /dev/null +++ b/examples/widgets/layouts/dynamiclayouts/doc/dynamiclayouts.rst @@ -0,0 +1,10 @@ +Dynamic Layouts Example +======================= + +Dynamic Layouts implements dynamically placed widgets within running +applications. The widget placement depends on whether Horizontal or Vertical is +chosen. + +.. image:: basiclayouts.png + :width: 400 + :alt: Dynamic Layouts Screenshot diff --git a/examples/widgets/layouts/flowlayout/doc/flowlayout.png b/examples/widgets/layouts/flowlayout/doc/flowlayout.png new file mode 100644 index 000000000..7dece0611 Binary files /dev/null and b/examples/widgets/layouts/flowlayout/doc/flowlayout.png differ diff --git a/examples/widgets/layouts/flowlayout/doc/flowlayout.rst b/examples/widgets/layouts/flowlayout/doc/flowlayout.rst new file mode 100644 index 000000000..b138165fe --- /dev/null +++ b/examples/widgets/layouts/flowlayout/doc/flowlayout.rst @@ -0,0 +1,11 @@ +Flow Layout Example +=================== + +Flow Layout implements a layout that handles different window sizes +in a Qt Widgets application. +The widget placement changes depending on the width of the application +window. + +.. image:: flowlayout.png + :width: 400 + :alt: Flow Layout Screenshot diff --git a/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.png b/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.png new file mode 100644 index 000000000..bd7c27cfd Binary files /dev/null and b/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.png differ diff --git a/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.rst b/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.rst new file mode 100644 index 000000000..0e3a15c0d --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/doc/dockwidgets.rst @@ -0,0 +1,9 @@ +Dock Widget Example +=================== + +The Dock Widgets example shows how to add dock windows to an application. It +also shows how to use Qt's rich text engine. + +.. image:: dockwidgets.png + :width: 400 + :alt: Dock Widgets Screenshot diff --git a/examples/widgets/painting/basicdrawing/doc/basicdrawing.png b/examples/widgets/painting/basicdrawing/doc/basicdrawing.png new file mode 100644 index 000000000..30be31724 Binary files /dev/null and b/examples/widgets/painting/basicdrawing/doc/basicdrawing.png differ diff --git a/examples/widgets/painting/basicdrawing/doc/basicdrawing.rst b/examples/widgets/painting/basicdrawing/doc/basicdrawing.rst new file mode 100644 index 000000000..26aa8c997 --- /dev/null +++ b/examples/widgets/painting/basicdrawing/doc/basicdrawing.rst @@ -0,0 +1,15 @@ +Basic Drawing Example +===================== + +The Basic Drawing example shows how to display basic graphics primitives in +a variety of styles using the QPainter class. + +QPainter performs low-level painting on widgets and other paint devices. The +class can draw everything from simple lines to complex shapes like pies and +chords. It can also draw aligned text and pixmaps. Normally, it draws in +a "natural" coordinate system, but it can in addition do view and world +transformation. + +.. image:: stardelegate.png + :width: 400 + :alt: Basic Drawing Screenshot diff --git a/examples/widgets/painting/concentriccircles/doc/concentriccircles.png b/examples/widgets/painting/concentriccircles/doc/concentriccircles.png new file mode 100644 index 000000000..69ea16825 Binary files /dev/null and b/examples/widgets/painting/concentriccircles/doc/concentriccircles.png differ diff --git a/examples/widgets/painting/concentriccircles/doc/concentriccircles.rst b/examples/widgets/painting/concentriccircles/doc/concentriccircles.rst new file mode 100644 index 000000000..6c1efe0c5 --- /dev/null +++ b/examples/widgets/painting/concentriccircles/doc/concentriccircles.rst @@ -0,0 +1,12 @@ +Concentric Circles Examples +=========================== + +Demonstrates the improved quality that antialiasing and floating point +precision gives. + +The application's main window displays several widgets which are drawn using +the various combinations of precision and anti-aliasing. + +.. image:: concentriccircles.png + :width: 400 + :alt: Concentric Circles Screenshot diff --git a/examples/widgets/painting/painter/doc/painter.png b/examples/widgets/painting/painter/doc/painter.png new file mode 100644 index 000000000..991d2703d Binary files /dev/null and b/examples/widgets/painting/painter/doc/painter.png differ diff --git a/examples/widgets/painting/painter/doc/painter.rst b/examples/widgets/painting/painter/doc/painter.rst new file mode 100644 index 000000000..69e75a789 --- /dev/null +++ b/examples/widgets/painting/painter/doc/painter.rst @@ -0,0 +1,8 @@ +Painter Example +=============== + +Simple painter application based on Qt Widgets. + +.. image:: painter.png + :width: 400 + :alt: Painter Screenshot diff --git a/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.png b/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.png new file mode 100644 index 000000000..37b13b128 Binary files /dev/null and b/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.png differ diff --git a/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.rst b/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.rst new file mode 100644 index 000000000..d088ffd13 --- /dev/null +++ b/examples/widgets/richtext/syntaxhighlighter/doc/syntaxhighlighter.rst @@ -0,0 +1,8 @@ +Syntax Highlighter Example +========================== + +The Syntax Highlighter example shows how to perform simple syntax highlighting. + +.. image:: syntaxhighlighter.png + :width: 400 + :alt: Syntax Highlighter Screenshot diff --git a/examples/widgets/richtext/textedit/doc/textedit.png b/examples/widgets/richtext/textedit/doc/textedit.png new file mode 100644 index 000000000..e0e681f35 Binary files /dev/null and b/examples/widgets/richtext/textedit/doc/textedit.png differ diff --git a/examples/widgets/richtext/textedit/doc/textedit.rst b/examples/widgets/richtext/textedit/doc/textedit.rst new file mode 100644 index 000000000..2c8bc3232 --- /dev/null +++ b/examples/widgets/richtext/textedit/doc/textedit.rst @@ -0,0 +1,8 @@ +TextEdit Example +================ + +The Text Edit example shows Qt's rich text editing facilities in action. + +.. image:: textedit.png + :width: 400 + :alt: TextEdit Screenshot diff --git a/examples/widgets/richtext/textobject/doc/textobject.png b/examples/widgets/richtext/textobject/doc/textobject.png new file mode 100644 index 000000000..ebd2e6f7b Binary files /dev/null and b/examples/widgets/richtext/textobject/doc/textobject.png differ diff --git a/examples/widgets/richtext/textobject/doc/textobject.rst b/examples/widgets/richtext/textobject/doc/textobject.rst new file mode 100644 index 000000000..d5a1e253a --- /dev/null +++ b/examples/widgets/richtext/textobject/doc/textobject.rst @@ -0,0 +1,31 @@ +TextObject Example +================== + +A QTextDocument consists of a hierarchy of elements, such as text blocks and +frames. A text object describes the structure or format of one or more of these +elements. For instance, images imported from HTML are implemented using text +objects. Text objects are used by the document's layout to lay out and render +(paint) the document. Each object knows how to paint the elements they govern, +and calculates their size. + +To be able to insert an SVG image into a text document, we create a text +object, and implement painting for that object. This object can then be set on +a QTextCharFormat. We also register the text object with the layout of the +document, enabling it to draw QTextCharFormats governed by our text object. We +can summarize the procedure with the following steps: + + * Implement the text object. + * Register the text object with the layout of the text document. + * Set the text object on a QTextCharFormat. + * Insert a QChar::ObjectReplacementCharacter with that text char format into + the document. + +The example consists of the following classes: + + * SvgTextObject implements the text object. + * Window shows a QTextEdit into which SVG images can be inserted. + + +.. image:: textobject.png + :width: 400 + :alt: TextObject Screenshot diff --git a/examples/widgets/state-machine/trafficlight/doc/trafficlight.png b/examples/widgets/state-machine/trafficlight/doc/trafficlight.png new file mode 100644 index 000000000..ec88a8e8b Binary files /dev/null and b/examples/widgets/state-machine/trafficlight/doc/trafficlight.png differ diff --git a/examples/widgets/state-machine/trafficlight/doc/trafficlight.rst b/examples/widgets/state-machine/trafficlight/doc/trafficlight.rst new file mode 100644 index 000000000..57d369465 --- /dev/null +++ b/examples/widgets/state-machine/trafficlight/doc/trafficlight.rst @@ -0,0 +1,10 @@ +Traffic Light Example +===================== + +The Traffic Light example shows how to use The State Machine Framework to +implement the control flow of a traffic light. + + +.. image:: trafficlight.png + :width: 400 + :alt: Traffic Light Screenshot diff --git a/examples/widgets/tutorials/addressbook/doc/addressbook.png b/examples/widgets/tutorials/addressbook/doc/addressbook.png new file mode 100644 index 000000000..7d563c17c Binary files /dev/null and b/examples/widgets/tutorials/addressbook/doc/addressbook.png differ diff --git a/examples/widgets/tutorials/addressbook/doc/addressbook.rst b/examples/widgets/tutorials/addressbook/doc/addressbook.rst new file mode 100644 index 000000000..646d949d2 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/doc/addressbook.rst @@ -0,0 +1,9 @@ +Address Book Example +==================== + +The address book example shows how to use proxy models to display different +views onto data from a single model. + +.. image:: addressbook.png + :width: 400 + :alt: Address Book Screenshot diff --git a/examples/widgets/tutorials/cannon/doc/cannon.png b/examples/widgets/tutorials/cannon/doc/cannon.png new file mode 100644 index 000000000..1bdc9db88 Binary files /dev/null and b/examples/widgets/tutorials/cannon/doc/cannon.png differ diff --git a/examples/widgets/tutorials/cannon/doc/cannon.rst b/examples/widgets/tutorials/cannon/doc/cannon.rst new file mode 100644 index 000000000..da7cc556b --- /dev/null +++ b/examples/widgets/tutorials/cannon/doc/cannon.rst @@ -0,0 +1,8 @@ +Cannon Example +============== + +Simple Cannon example. + +.. image:: cannon.png + :width: 400 + :alt: Cannon Screenshot -- cgit v1.2.3