diff options
author | Oswald Buddenhagen <oswald.buddenhagen@qt.io> | 2017-05-22 16:44:51 +0200 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@qt.io> | 2017-05-22 18:24:16 +0200 |
commit | 77faaf1757c2054adf6a13ff1025621bb86a3934 (patch) | |
tree | beced043dde3f9da5927c20112921d4732b2a03e /sources/pyside2/doc/codesnippets/examples | |
parent | d4112e4a6ea25aee831da3cd123f139b5f7229bf (diff) |
move everying into sources/pyside2 (dev edition)
in preparation for a subtree merge.
this should not be necessary to do in a separate commit, but git is a
tad stupid about following history correctly without it.
Diffstat (limited to 'sources/pyside2/doc/codesnippets/examples')
49 files changed, 5949 insertions, 0 deletions
diff --git a/sources/pyside2/doc/codesnippets/examples/dbus/example-client.py b/sources/pyside2/doc/codesnippets/examples/dbus/example-client.py new file mode 100755 index 000000000..a294b6f20 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dbus/example-client.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################# + +# -*- coding: utf-8 -*- + +# DBUS Client using PySide integration + +import sys +from traceback import print_exc + +# import python dbus module +import dbus +# import python dbus GLib mainloop support +import dbus.mainloop.glib +# import QtCore +from PySide2.QtCore import * + +# signal handler +def button_clicked(): + print "button clicked" + +# main function +if __name__ == '__main__': + + # Enable glib main loop support + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + # Get the session bus + bus = dbus.SessionBus() + + try: + # Get the remote object + remote_object = bus.get_object("com.example.SampleService", + "/DBusWidget") + # Get the remote interface for the remote object + iface = dbus.Interface(remote_object, "com.example.SampleWidget") + except dbus.DBusException: + print_exc() + sys.exit(1) + + # Start the application + app = QCoreApplication([]) + + # Call some methods of the remote interface + iface.show() + iface.setText("Emit signal") + # connect the DBus signal clicked to the function button_clicked + iface.connect_to_signal("clicked", button_clicked) + iface.connect_to_signal("lastWindowClosed", app.quit) + + # enter in the main loop + app.exec_() diff --git a/sources/pyside2/doc/codesnippets/examples/dbus/example-server.py b/sources/pyside2/doc/codesnippets/examples/dbus/example-server.py new file mode 100755 index 000000000..c4b9ce13e --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dbus/example-server.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################# + +# -*- coding: utf-8 -*- + +# DBUS Server Example of use PySide with PyDBus library + +import dbus +import dbus.service +import dbus.mainloop.glib +import random + +from PySide2.QtCore import * +from PySide2.QtGui import QPushButton, QApplication + +# The adaptor, MUST inherit dbus.service.Object +class DBusWidget(dbus.service.Object): + def __init__(self, name, session): + # export this object to dbus + dbus.service.Object.__init__(self, name, session) + + # create a simple widget + self.widget = QPushButton() + self.widget.resize(200, 50) + + # To export a Qt signal as a DBus-signal, you need to connect it to a method in this class. + # The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal + QObject.connect(self.widget, SIGNAL("clicked()"), self.clicked) + QObject.connect(QApplication.instance(), SIGNAL("lastWindowClosed()"), self.lastWindowClosed) + + # You can export methods to dbus like you do in python-dbus. + @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='') + def show(self): + self.widget.show() + + # Another method... now with a parameter + @dbus.service.method("com.example.SampleWidget", in_signature='s', out_signature='') + def setText(self, value): + self.widget.setText(value) + + # Another one... + @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='') + def exit(self): + qApp().quit() + + # A signal that will be exported to dbus + @dbus.service.signal("com.example.SampleWidget", signature='') + def clicked(self): + pass + + # Another signal that will be exported to dbus + @dbus.service.signal("com.example.SampleWidget", signature='') + def lastWindowClosed(self): + pass + + +if __name__ == '__main__': + app = QApplication([]) + # Use qt/glib mainloop integration to get dbus mainloop working + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + session_bus = dbus.SessionBus() + # Export the service + name = dbus.service.BusName("com.example.SampleService", session_bus) + # Export the object + widget = DBusWidget(session_bus, '/DBusWidget') + + print "Running example service." + app.exec_() + diff --git a/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml new file mode 100644 index 000000000..ad51f2e7a --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +import QtQuick 1.0 +import "ImageProviderCore" // import the plugin that registers the color image provider + +//![0] +Column { + Image { source: "image://colors/yellow" } + Image { source: "image://colors/red" } +} +//![0] diff --git a/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider.cpp b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider.cpp new file mode 100644 index 000000000..ffbe8f564 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/imageprovider/imageprovider.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//![0] +class ColorImageProvider (QDeclarativeImageProvider): + def __init__(self): + QDeclarativeImageProvider.__init__(self, QDeclarativeImageProvider.Pixmap) + + def requestPixmap(id, size, requestedSize): + width = 100 + height = 50 + + if size: + size.setWidth(width) + size.setHeight(height) + + if requestedSize.width() > 0: + width = requestedSize.width() + if requestedSize.height() > 0: + height = requestedSize.height() + + pixmap = QPixmap(width, height) + pixmap.fill(QColor(id).rgba()) +//![0] + # write the color name + painter = QPainter(pixmap) + f = painter.font() + f.setPixelSize(20) + painter.setFont(f) + painter.setPen(Qt.black) + if requestedSize.isValid(): + painter.scale(requestedSize.width() / width, requestedSize.height() / height) + painter.drawText(QRectF(0, 0, width, height), Qt.AlignCenter, id) +//![1] + return pixmap +//![1] diff --git a/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugin.cpp b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugin.cpp new file mode 100644 index 000000000..822fded38 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugin.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//![0] +class TimeModel (QObject): + hour = Property(int, getHour, notify = timeChanged) + minute = Property(int, getMinute, notify = timeChanged) +//![0] + +//![plugin] +class QExampleQmlPlugin (QDeclarativeExtensionPlugin): + + def registerTypes(self, uri): + assert(uri == "com.nokia.TimeExample") + qmlRegisterType(TimeModel, uri, 1, 0, "Time") +//![plugin] + +//![export] +# This isn't supported by PySide yet. +# Q_EXPORT_PLUGIN2(qmlqtimeexampleplugin, QExampleQmlPlugin); +//![export] diff --git a/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugins.qml b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugins.qml new file mode 100644 index 000000000..094b81f2e --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/declarative/cppextensions/plugins/plugins.qml @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ +//![0] +import com.nokia.TimeExample 1.0 // import types from the plugin + +Clock { // this class is defined in QML (com/nokia/TimeExample/Clock.qml) + + Time { // this class is defined in C++ (plugin.cpp) + id: time + } + + hours: time.hour + minutes: time.minute +} +//![0] diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp new file mode 100644 index 000000000..897410ed7 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp @@ -0,0 +1,259 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [0] //! [1] +def __init__(self, parent): + QWizard.__init__(self, parent): + self.addPage(IntroPage()) + self.addPage(ClassInfoPage()) + self.addPage(CodeStylePage()) + self.addPage(OutputFilesPage()) + self.addPage(ConclusionPage()) +//! [0] + + self.setPixmap(QWizard.BannerPixmap, QPixmap(":/images/banner.png")) + self.setPixmap(QWizard.BackgroundPixmap, QPixmap(":/images/background.png")) + + self.setWindowTitle(self.tr("Class Wizard")) +//! [2] + +//! [1] //! [2] + +//! [3] +def accept(self): +//! [3] //! [4] + className = self.field("className") + baseClass = self.field("baseClass") + macroName = self.field("macroName") + baseInclude = self.field("baseInclude") + + outputDir = self.field("outputDir") + header = self.field("header") + implementation = self.field("implementation") +//! [4] + +... + +//! [5] + QDialog.accept(self) +//! [5] //! [6] +} +//! [6] + +//! [7] +class IntroPage (QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) + + self.setTitle(tr("Introduction")) + self.setPixmap(QWizard.WatermarkPixmap, QPixmap(":/images/watermark1.png")) + + label = QLabel(self.tr("This wizard will generate a skeleton C++ class " \ + "definition, including a few functions. You simply " \ + "need to specify the class name and set a few " \ + "options to produce a header file and an " \ + "implementation file for your new C++ class.")) + label.setWordWrap(True) + + layout = QVBoxLayout() + layout.addWidget(label) + self.setLayout(layout) +} +//! [7] + +//! [8] //! [9] +class ClassInfoPage(QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) +//! [8] + self.setTitle(self.tr("Class Information")) + self.setSubTitle(self.tr("Specify basic information about the class for which you " \ + "want to generate skeleton source code files.")) + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo1.png")) + +//! [10] + classNameLabel = QLabel(self.tr("&Class name:")) + classNameLineEdit = QLineEdit() + classNameLabel.setBuddy(classNameLineEdit) + + baseClassLabel = QLabel(self.tr("B&ase class:")) + baseClassLineEdit = QLineEdit() + baseClassLabel.setBuddy(baseClassLineEdit) + + qobjectMacroCheckBox = QCheckBox(self.tr("Generate Q_OBJECT ¯o")) + +//! [10] + groupBox = QGroupBox(self.tr("C&onstructor")) +//! [9] + + qobjectCtorRadioButton = QRadioButton(self.tr("&QObject-style constructor")) + qwidgetCtorRadioButton = QRadioButton(self.tr("Q&Widget-style constructor")) + defaultCtorRadioButton = QRadioButton(self.tr("&Default constructor")) + copyCtorCheckBox = QCheckBox(self.tr("&Generate copy constructor and operator=")) + + defaultCtorRadioButton.setChecked(True) + + self.connect(defaultCtorRadioButton, SIGNAL("toggled(bool)"), + copyCtorCheckBox, SLOT("setEnabled(bool)")) + +//! [11] //! [12] + registerField("className*", classNameLineEdit) + registerField("baseClass", baseClassLineEdit) + registerField("qobjectMacro", qobjectMacroCheckBox) +//! [11] + registerField("qobjectCtor", qobjectCtorRadioButton) + registerField("qwidgetCtor", qwidgetCtorRadioButton) + registerField("defaultCtor", defaultCtorRadioButton) + registerField("copyCtor", copyCtorCheckBox) + + groupBoxLayout = QVBoxLayout() +//! [12] + groupBoxLayout.addWidget(qobjectCtorRadioButton) + groupBoxLayout.addWidget(qwidgetCtorRadioButton) + groupBoxLayout.addWidget(defaultCtorRadioButton) + groupBoxLayout.addWidget(copyCtorCheckBox) + groupBox.setLayout(groupBoxLayout) + + layout = QGridLayout() + layout.addWidget(classNameLabel, 0, 0) + layout.addWidget(classNameLineEdit, 0, 1) + layout.addWidget(baseClassLabel, 1, 0) + layout.addWidget(baseClassLineEdit, 1, 1) + layout.addWidget(qobjectMacroCheckBox, 2, 0, 1, 2) + layout.addWidget(groupBox, 3, 0, 1, 2) + self.setLayout(layout) +//! [13] + +//! [13] + +//! [14] +class CodeStylePage(QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) + self.setTitle(tr("Code Style Options")) + self.setSubTitle(tr("Choose the formatting of the generated code.")) + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo2.png")) + + commentCheckBox = QCheckBox(self.tr("&Start generated files with a comment")) +//! [14] + commentCheckBox.setChecked(True) + + protectCheckBox = QCheckBox(self.tr("&Protect header file against multiple " \ + "inclusions")) + protectCheckBox.setChecked(True) + + macroNameLabel = QLabel(self.tr("&Macro name:")) + macroNameLineEdit = QLineEdit() + macroNameLabel.setBuddy(macroNameLineEdit) + + includeBaseCheckBox = QCheckBox(self.tr("&Include base class definition")) + baseIncludeLabel = QLabel(self.tr("Base class include:")) + baseIncludeLineEdit = QLineEdit() + baseIncludeLabel.setBuddy(baseIncludeLineEdit) + + self.connect(protectCheckBox, SIGNAL("toggled(bool)"), + macroNameLabel, SLOT("setEnabled(bool)")) + self.connect(protectCheckBox, SIGNAL("toggled(bool)"), + macroNameLineEdit, SLOT("setEnabled(bool)")) + self.connect(includeBaseCheckBox, SIGNAL("toggled(bool)"), + baseIncludeLabel, SLOT("setEnabled(bool)")) + self.connect(includeBaseCheckBox, SIGNAL(toggled(bool)), + baseIncludeLineEdit, SLOT("setEnabled(bool)")) + + self.registerField("comment", commentCheckBox) + self.registerField("protect", protectCheckBox) + self.registerField("macroName", macroNameLineEdit) + self.registerField("includeBase", includeBaseCheckBox) + self.registerField("baseInclude", baseIncludeLineEdit) + + layout = QGridLayout() + layout.setColumnMinimumWidth(0, 20) + layout.addWidget(commentCheckBox, 0, 0, 1, 3) + layout.addWidget(protectCheckBox, 1, 0, 1, 3) + layout.addWidget(macroNameLabel, 2, 1) + layout.addWidget(macroNameLineEdit, 2, 2) + layout.addWidget(includeBaseCheckBox, 3, 0, 1, 3) + layout.addWidget(baseIncludeLabel, 4, 1) + layout.addWidget(baseIncludeLineEdit, 4, 2) +//! [15] + self.setLayout(layout) +} +//! [15] + +//! [16] + def initializePage(self): + className = self.field("className") + self.macroNameLineEdit.setText(className.upper() + "_H") + + baseClass = self.field("baseClass") + + self.includeBaseCheckBox.setChecked(len(baseClass)) + self.includeBaseCheckBox.setEnabled(len(baseClass)) + self.baseIncludeLabel.setEnabled(len(baseClass)) + self.baseIncludeLineEdit.setEnabled(len(baseClass)) + + if not baseClass: + self.baseIncludeLineEdit.clear() + elsif QRegExp("Q[A-Z].*").exactMatch(baseClass): + baseIncludeLineEdit.setText("<" + baseClass + ">") + else: + baseIncludeLineEdit.setText("\"" + baseClass.lower() + ".h\"") +//! [16] + +//! [17] + def initializePage(self): + className = field("className") + self.headerLineEdit.setText(className.lower() + ".h") + self.implementationLineEdit.setText(className.lower() + ".cpp") + self.outputDirLineEdit.setText(QDir.convertSeparators(QDir.tempPath())) +//! [17] diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc new file mode 100644 index 000000000..41a5ddc7d --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc @@ -0,0 +1,11 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/background.png</file> + <file>images/banner.png</file> + <file>images/logo1.png</file> + <file>images/logo2.png</file> + <file>images/logo3.png</file> + <file>images/watermark1.png</file> + <file>images/watermark2.png</file> +</qresource> +</RCC> diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/background.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/background.png Binary files differnew file mode 100644 index 000000000..44c7badb8 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/background.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/banner.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/banner.png Binary files differnew file mode 100644 index 000000000..3169152b8 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/banner.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png Binary files differnew file mode 100644 index 000000000..f9b594aaf --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png Binary files differnew file mode 100644 index 000000000..5dcbd4669 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png Binary files differnew file mode 100644 index 000000000..9fd3ea235 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png Binary files differnew file mode 100644 index 000000000..0091f5c17 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png Binary files differnew file mode 100644 index 000000000..3b88f2e36 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/main.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/main.cpp new file mode 100644 index 000000000..9e9566d37 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/classwizard/main.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +#include "classwizard.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(classwizard); + + QApplication app(argc, argv); + + QString translatorFileName = QLatin1String("qt_"); + translatorFileName += QLocale::system().name(); + QTranslator *translator = new QTranslator(&app); + if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + app.installTranslator(translator); + + ClassWizard wizard; + wizard.show(); + return app.exec(); +} diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/extension/finddialog.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/extension/finddialog.cpp new file mode 100644 index 000000000..18612e4ba --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/extension/finddialog.cpp @@ -0,0 +1,119 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * + +//! [0] +def __init__(self, parent): + QDialog.__init__(self, parent) + label = QLabel(self.tr("Find &what:")) + lineEdit = QLineEdit() + label.setBuddy(lineEdit) + + caseCheckBox = QCheckBox(self.tr("Match &case")) + fromStartCheckBox = QCheckBox(self.tr("Search from &start")) + fromStartCheckBox.setChecked(True) + +//! [1] + findButton = QPushButton(self.tr("&Find")) + findButton.setDefault(True) + + moreButton = QPushButton(self.tr("&More")) + moreButton.setCheckable(True) +//! [0] + moreButton.setAutoDefault(False) + + buttonBox = QDialogButtonBox(Qt.Vertical) + buttonBox.addButton(findButton, QDialogButtonBox.ActionRole) + buttonBox.addButton(moreButton, QDialogButtonBox.ActionRole) +//! [1] + +//! [2] + extension = QWidget() + + wholeWordsCheckBox = QCheckBox(self.tr("&Whole words")) + backwardCheckBox = QCheckBox(self.tr("Search &backward")) + searchSelectionCheckBox = QCheckBox(self.tr("Search se&lection")) +//! [2] + +//! [3] + connect(moreButton, SIGNAL("toggled(bool)"), extension, SLOT("setVisible(bool)")) + + extensionLayout = QVBoxLayout() + extensionLayout.setMargin(0) + extensionLayout.addWidget(wholeWordsCheckBox) + extensionLayout.addWidget(backwardCheckBox) + extensionLayout.addWidget(searchSelectionCheckBox) + extension.setLayout(extensionLayout) +//! [3] + +//! [4] + topLeftLayout = QHBoxLayout() + topLeftLayout.addWidget(label) + topLeftLayout.addWidget(lineEdit) + + leftLayout = QVBoxLayout() + leftLayout.addLayout(topLeftLayout) + leftLayout.addWidget(caseCheckBox) + leftLayout.addWidget(fromStartCheckBox) + leftLayout.addSself.tretch(1) + + mainLayout = QGridLayout() + mainLayout.setSizeConsself.traint(QLayout.SetFixedSize) + mainLayout.addLayout(leftLayout, 0, 0) + mainLayout.addWidget(buttonBox, 0, 1) + mainLayout.addWidget(extension, 1, 0, 1, 2) + setLayout(mainLayout) + + setWindowTitle(self.tr("Extension")) +//! [4] //! [5] + extension.hide() +//! [5] diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png Binary files differnew file mode 100644 index 000000000..56f17a3e5 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png Binary files differnew file mode 100644 index 000000000..124a05068 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp new file mode 100644 index 000000000..453dac773 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp @@ -0,0 +1,358 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "licensewizard.h" + +//! [0] //! [1] //! [2] +# class LicenseWizard +def __init__(self, parent) + QWizard(self, parent) +//! [0] + self.setPage(self.Page_Intro, IntroPage()) + self.setPage(self.Page_Evaluate, EvaluatePage()) + self.setPage(self.Page_Register, RegisterPage()) + self.setPage(self.Page_Details, DetailsPage()) + self.setPage(self.Page_Conclusion, ConclusionPage()) +//! [1] + + self.setStartId(self.Page_Intro); +//! [2] + +//! [3] + +//! [3] //! [4] + self.setWizardStyle(QWizard.ModernStyle); + +//! [4] //! [5] + self.setOption(HaveHelpButton, True); +//! [5] //! [6] + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo.png")); + +//! [7] + connect(this, SIGNAL(helpRequested()), this, SLOT(showHelp())); +//! [7] + + setWindowTitle(tr("License Wizard")); +//! [8] +} +//! [6] //! [8] + +//! [9] //! [10] +void LicenseWizard::showHelp() +//! [9] //! [11] +{ + static QString lastHelpMessage; + + message = "" + + switch (currentId()) { + case Page_Intro: + message = tr("The decision you make here will affect which page you " + "get to see next."); + break; +//! [10] //! [11] + case Page_Evaluate: + message = tr("Make sure to provide a valid email address, such as " + "toni.buddenbrook@example.de."); + break; + case Page_Register: + message = tr("If you don't provide an upgrade key, you will be " + "asked to fill in your details."); + break; + case Page_Details: + message = tr("Make sure to provide a valid email address, such as " + "thomas.gradgrind@example.co.uk."); + break; + case Page_Conclusion: + message = tr("You must accept the terms and conditions of the " + "license to proceed."); + break; +//! [12] //! [13] + default: + message = tr("This help is likely not to be of any help."); + } +//! [12] + + if (lastHelpMessage == message) + message = tr("Sorry, I already gave what help I could. " + "Maybe you should try asking a human?"); + +//! [14] + QMessageBox::information(this, tr("License Wizard Help"), message); +//! [14] + + lastHelpMessage = message; +//! [15] +} +//! [13] //! [15] + +//! [16] +IntroPage::IntroPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Introduction")); + setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png")); + + topLabel = new QLabel(tr("This wizard will help you register your copy of " + "<i>Super Product One</i>™ or start " + "evaluating the product.")); + topLabel->setWordWrap(true); + + registerRadioButton = new QRadioButton(tr("&Register your copy")); + evaluateRadioButton = new QRadioButton(tr("&Evaluate the product for 30 " + "days")); + registerRadioButton->setChecked(true); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(topLabel); + layout->addWidget(registerRadioButton); + layout->addWidget(evaluateRadioButton); + setLayout(layout); +} +//! [16] //! [17] + +//! [18] +# class IntroPage +def nextId(self): +//! [17] //! [19] + if evaluateRadioButton.isChecked(): + return LicenseWizard.Page_Evaluate + else: + return LicenseWizard.Page_Register +//! [18] //! [19] + +//! [20] +EvaluatePage::EvaluatePage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Evaluate <i>Super Product One</i>™")); + setSubTitle(tr("Please fill both fields. Make sure to provide a valid " + "email address (e.g., john.smith@example.com).")); + + nameLabel = new QLabel(tr("N&ame:")); + nameLineEdit = new QLineEdit; +//! [20] + nameLabel->setBuddy(nameLineEdit); + + emailLabel = new QLabel(tr("&Email address:")); + emailLineEdit = new QLineEdit; + emailLineEdit->setValidator(new QRegExpValidator(QRegExp(".*@.*"), this)); + emailLabel->setBuddy(emailLineEdit); + +//! [21] + registerField("evaluate.name*", nameLineEdit); + registerField("evaluate.email*", emailLineEdit); +//! [21] + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + setLayout(layout); +//! [22] +} +//! [22] + +//! [23] +# class EvaluatePage +def nextId(self): + return LicenseWizard.Page_Conclusion +//! [23] + +RegisterPage::RegisterPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Register Your Copy of <i>Super Product One</i>™")); + setSubTitle(tr("If you have an upgrade key, please fill in " + "the appropriate field.")); + + nameLabel = new QLabel(tr("N&ame:")); + nameLineEdit = new QLineEdit; + nameLabel->setBuddy(nameLineEdit); + + upgradeKeyLabel = new QLabel(tr("&Upgrade key:")); + upgradeKeyLineEdit = new QLineEdit; + upgradeKeyLabel->setBuddy(upgradeKeyLineEdit); + + registerField("register.name*", nameLineEdit); + registerField("register.upgradeKey", upgradeKeyLineEdit); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(upgradeKeyLabel, 1, 0); + layout->addWidget(upgradeKeyLineEdit, 1, 1); + setLayout(layout); +} + +//! [24] +# class RegisterPage +def nextId(self): + if self.upgradeKeyLineEdit.text().isEmpty(): + return LicenseWizard::Page_Details + else: + return LicenseWizard::Page_Conclusion +//! [24] + +DetailsPage::DetailsPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Fill In Your Details")); + setSubTitle(tr("Please fill all three fields. Make sure to provide a valid " + "email address (e.g., tanaka.aya@example.co.jp).")); + + companyLabel = new QLabel(tr("&Company name:")); + companyLineEdit = new QLineEdit; + companyLabel->setBuddy(companyLineEdit); + + emailLabel = new QLabel(tr("&Email address:")); + emailLineEdit = new QLineEdit; + emailLineEdit->setValidator(new QRegExpValidator(QRegExp(".*@.*"), this)); + emailLabel->setBuddy(emailLineEdit); + + postalLabel = new QLabel(tr("&Postal address:")); + postalLineEdit = new QLineEdit; + postalLabel->setBuddy(postalLineEdit); + + registerField("details.company*", companyLineEdit); + registerField("details.email*", emailLineEdit); + registerField("details.postal*", postalLineEdit); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(companyLabel, 0, 0); + layout->addWidget(companyLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + layout->addWidget(postalLabel, 2, 0); + layout->addWidget(postalLineEdit, 2, 1); + setLayout(layout); +} + +//! [25] +# class DetailsPage +def nextId(self): + return LicenseWizard.Page_Conclusion +//! [25] + +ConclusionPage::ConclusionPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Complete Your Registration")); + setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png")); + + bottomLabel = new QLabel; + bottomLabel->setWordWrap(true); + + agreeCheckBox = new QCheckBox(tr("I agree to the terms of the license")); + + registerField("conclusion.agree*", agreeCheckBox); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(bottomLabel); + layout->addWidget(agreeCheckBox); + setLayout(layout); +} + +//! [26] +#class ConclusionPage +def nextId(self): + return -1 +//! [26] + +//! [27] +# class ConclusionPage +def initializePage(self): + if wizard().hasVisitedPage(LicenseWizard::Page_Evaluate): + licenseText = self.tr("<u>Evaluation License Agreement:</u> " \ + "You can use this software for 30 days and make one " \ + "backup, but you are not allowed to distribute it.") + elsif wizard().hasVisitedPage(LicenseWizard.Page_Details): + licenseText = self.tr("<u>First-Time License Agreement:</u> " \ + "You can use this software subject to the license " \ + "you will receive by email.") + else: + licenseText = self.tr("<u>Upgrade License Agreement:</u> " \ + "This software is licensed under the terms of your " \ + "current license.") + } + bottomLabel.setText(licenseText) +//! [27] + +//! [28] +void ConclusionPage::setVisible(bool visible) +{ + QWizardPage::setVisible(visible); + + if (visible) { +//! [29] + self.wizard().setButtonText(QWizard.CustomButton1, self.tr("&Print")) + self.wizard().setOption(QWizard.HaveCustomButton1, True) + self.connect(wizard(), SIGNAL("customButtonClicked(int)"), self, SLOT("printButtonClicked()")) +//! [29] + } else { + wizard()->setOption(QWizard::HaveCustomButton1, false); + disconnect(wizard(), SIGNAL(customButtonClicked(int)), + this, SLOT(printButtonClicked())); + } +} +//! [28] + +void ConclusionPage::printButtonClicked() +{ + QPrinter printer; + QPrintDialog dialog(&printer, this); + if (dialog.exec()) + QMessageBox::warning(this, tr("Print License"), + tr("As an environmentally friendly measure, the " + "license text will not actually be printed.")); +} diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h new file mode 100644 index 000000000..f0be5fa60 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#ifndef LICENSEWIZARD_H +#define LICENSEWIZARD_H + +#include <QWizard> + +QT_BEGIN_NAMESPACE +class QCheckBox; +class QLabel; +class QLineEdit; +class QRadioButton; +QT_END_NAMESPACE + +//! [0] //! [1] +class LicenseWizard (QWizard): +//! [0] +//! [2] + Page_Intro = 1 + Page_Evaluate = 2 + Page_Register = 3 + Page_Details = 4 + Page_Conclusion = 5 +//! [2] + + def __init__(self, parent): + ... + + def showHelp(self): + ... +//! [3] + +//! [1] //! [3] + +//! [4] +class IntroPage : public QWizardPage +{ + Q_OBJECT + +public: + IntroPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *topLabel; + QRadioButton *registerRadioButton; + QRadioButton *evaluateRadioButton; +}; +//! [4] + +//! [5] +class EvaluatePage : public QWizardPage +{ + Q_OBJECT + +public: + EvaluatePage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *nameLabel; + QLabel *emailLabel; + QLineEdit *nameLineEdit; + QLineEdit *emailLineEdit; +}; +//! [5] + +class RegisterPage : public QWizardPage +{ + Q_OBJECT + +public: + RegisterPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *nameLabel; + QLabel *upgradeKeyLabel; + QLineEdit *nameLineEdit; + QLineEdit *upgradeKeyLineEdit; +}; + +class DetailsPage : public QWizardPage +{ + Q_OBJECT + +public: + DetailsPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *companyLabel; + QLabel *emailLabel; + QLabel *postalLabel; + QLineEdit *companyLineEdit; + QLineEdit *emailLineEdit; + QLineEdit *postalLineEdit; +}; + +//! [6] +class ConclusionPage : public QWizardPage +{ + Q_OBJECT + +public: + ConclusionPage(QWidget *parent = 0); + + void initializePage(); + int nextId() const; + void setVisible(bool visible); + +private slots: + void printButtonClicked(); + +private: + QLabel *bottomLabel; + QCheckBox *agreeCheckBox; +}; +//! [6] + +#endif diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc new file mode 100644 index 000000000..b06993851 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/logo.png</file> + <file>images/watermark.png</file> +</qresource> +</RCC> diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/main.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/main.cpp new file mode 100644 index 000000000..e372e9c17 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/licensewizard/main.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +#include "licensewizard.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(licensewizard); + + QApplication app(argc, argv); + + QString translatorFileName = QLatin1String("qt_"); + translatorFileName += QLocale::system().name(); + QTranslator *translator = new QTranslator(&app); + if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + app.installTranslator(translator); + + LicenseWizard wizard; + wizard.show(); + return app.exec(); +} diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp new file mode 100644 index 000000000..ed305196a --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [0] + i = QInputDialog().getInteger(self, self.tr("QInputDialog().getInteger()"), + self.tr("Percentage:"), 25, 0, 100, 1, ok) + if ok: + self.integerLabel.setText(self.tr("%1%").arg(i)) +//! [0] + +//! [1] + d = QInputDialog().getDouble(self, self.tr("QInputDialog().getDouble()"), + self.tr("Amount:"), 37.56, -10000, 10000, 2, ok) + if ok: + doubleLabel.setText(QString("$%1").arg(d)) +//! [1] + +//! [2] + items = [self.tr("Spring"), self.tr("Summer"), self.tr("Fall"), self.tr("Winter")] + + item = QInputDialog().getItem(self, self.tr("QInputDialog().getItem()"), + selftr("Season:"), items, 0, False, ok) + if ok and not item.isEmpty(): + itemLabel.setText(item) +//! [2] + +//! [3] + text = QInputDialog::getText(self, self.tr("QInputDialog().getText()"), + self.tr("User name:"), QLineEdit.Normal, + QDir().home().dirName(), ok) + if ok and text: + textLabel.setText(text) +//! [3] diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/tabdialog/tabdialog.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/tabdialog/tabdialog.cpp new file mode 100644 index 000000000..2244c2275 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/tabdialog/tabdialog.cpp @@ -0,0 +1,193 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [0] +class TabDialog (QDialog): + def __init__(self, fileName, parent = None): + QDialog.__init__(self, parent) + fileInfo = QFileInfo(fileName) + + self.tabWidget = QTabWidget() + self.tabWidget.addTab(GeneralTab(fileInfo), "General") + self.tabWidget.addTab(PermissionsTab(fileInfo), "Permissions") + self.tabWidget.addTab(ApplicationsTab(fileInfo), "Applications") +//! [0] + +//! [1] //! [2] + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok +//! [1] //! [3] + | QDialogButtonBox.Cancel) + + self.buttonBox.accepted.connect(self.accept) + self.buttonBox.rejected.connect(self.reject) +//! [2] //! [3] + +//! [4] + mainLayout = QVBoxLayout() + mainLayout.addWidget(tabWidget) + mainLayout.addWidget(buttonBox) + self.setLayout(mainLayout) +//! [4] + +//! [5] + self.setWindowTitle("Tab Dialog") +//! [5] + +//! [6] +class GeneralTab (QWidget): + def __init__(self, fileInfo, parent = None): + QWidget.__init__(self, parent) + fileNameLabel = QLabel("File Name:") + fileNameEdit = QLineEdit(fileInfo.fileName()) + + pathLabel = QLabel("Path:") + pathValueLabel = QLabel(fileInfo.absoluteFilePath()) + pathValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + sizeLabel = QLabel("Size:") + size = fileInfo.size()/1024 + sizeValueLabel = QLabel("%d K" % size) + sizeValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + lastReadLabel = QLabel("Last Read:") + lastReadValueLabel = QLabel(fileInfo.lastRead().toString()) + lastReadValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + lastModLabel = QLabel("Last Modified:") + lastModValueLabel = QLabel(fileInfo.lastModified().toString()) + lastModValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + mainLayout = QVBoxLayout() + mainLayout.addWidget(fileNameLabel) + mainLayout.addWidget(fileNameEdit) + mainLayout.addWidget(pathLabel) + mainLayout.addWidget(pathValueLabel) + mainLayout.addWidget(sizeLabel) + mainLayout.addWidget(sizeValueLabel) + mainLayout.addWidget(lastReadLabel) + mainLayout.addWidget(lastReadValueLabel) + mainLayout.addWidget(lastModLabel) + mainLayout.addWidget(lastModValueLabel) + mainLayout.addStretch(1) + self.setLayout(mainLayout) +//! [6] + +//! [7] +class PermissionsTab (QWidget): + def __init__(self, fileInfo, parent = None): + QWidget.__init__(self, parent) + permissionsGroup = QGroupBox("Permissions") + + readable = QCheckBox("Readable") + if fileInfo.isReadable(): + readable.setChecked(True) + + writable = QCheckBox("Writable") + if fileInfo.isWritable(): + writable.setChecked(True) + + executable = QCheckBox("Executable") + if fileInfo.isExecutable(): + executable.setChecked(True) + + ownerGroup = QGroupBox("Ownership") + + ownerLabel = QLabel("Owner") + ownerValueLabel = QLabel(fileInfo.owner()) + ownerValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + groupLabel = QLabel("Group") + groupValueLabel = QLabel(fileInfo.group()) + groupValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) + + permissionsLayout = QVBoxLayout() + permissionsLayout.addWidget(readable) + permissionsLayout.addWidget(writable) + permissionsLayout.addWidget(executable) + permissionsGroup.setLayout(permissionsLayout) + + ownerLayout = QVBoxLayout() + ownerLayout.addWidget(ownerLabel) + ownerLayout.addWidget(ownerValueLabel) + ownerLayout.addWidget(groupLabel) + ownerLayout.addWidget(groupValueLabel) + ownerGroup.setLayout(ownerLayout) + + mainLayout = QVBoxLayout() + mainLayout.addWidget(permissionsGroup) + mainLayout.addWidget(ownerGroup) + mainLayout.addStretch(1) + self.setLayout(mainLayout) +//! [7] + +//! [8] +class ApplicationsTab (QWidget): + def __init__(self, fileInfo, parent = None): + QWidget.__init__(self, parent) + topLabel = QLabel("Open with:") + + applicationsListBox = QListWidget() + applications = [] + + for i in range(30): + applications.append("Application %d" %s i) + applicationsListBox.insertItems(0, applications) + + if fileInfo.suffix().isEmpty(): + alwaysCheckBox = QCheckBox("Always use this application to open this type of file") + else: + alwaysCheckBox = QCheckBox("Always use this application to open files with the extension '%s'" % fileInfo.suffix()) + + layout = QVBoxLayout() + layout.addWidget(topLabel) + layout.addWidget(applicationsListBox) + layout.addWidget(alwaysCheckBox) + self.setLayout(layout) +//! [8] diff --git a/sources/pyside2/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp b/sources/pyside2/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp new file mode 100644 index 000000000..6a06c1725 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp @@ -0,0 +1,143 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +//! [0] //! [1] +def createIntroPage(self): + page = QWizardPage() + page.setTitle("Introduction") + + label = QLabel("This wizard will help you register your copy of Super Product Two.") + label.setWordWrap(True) + + layout = QVBoxLayout() + layout.addWidget(label) + page.setLayout(layout) + + return page +//! [0] + +//! [2] +QWizardPage *createRegistrationPage() +//! [1] //! [3] + +//! [3] + QWizardPage *page = new QWizardPage; + page->setTitle("Registration"); + page->setSubTitle("Please fill both fields."); + + QLabel *nameLabel = new QLabel("Name:"); + QLineEdit *nameLineEdit = new QLineEdit; + + QLabel *emailLabel = new QLabel("Email address:"); + QLineEdit *emailLineEdit = new QLineEdit; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + page->setLayout(layout); + + return page; +//! [4] + +//! [2] //! [4] + +//! [5] //! [6] +def createConclusionPage(self): +//! [5] //! [7] + +//! [7] + QWizardPage *page = new QWizardPage; + page->setTitle("Conclusion"); + + QLabel *label = new QLabel("You are now successfully registered. Have a " + "nice day!"); + label->setWordWrap(true); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(label); + page->setLayout(layout); + + return page; +//! [8] + +//! [6] //! [8] + +//! [9] //! [10] + +//! [9] //! [11] +def main(): + app = QApplication(sys.argv) + + translatorFileName = "qt_" + translatorFileName += QLocale.system().name() + translator = QTranslator(app) + if translator.load(translatorFileName, QLibraryInfo.location(QLibraryInfo.TranslationsPath)): + app.installTranslator(translator) + + wizard = QWizard() + wizard.addPage(createIntroPage()) + wizard.addPage(createRegistrationPage()) + wizard.addPage(createConclusionPage()) + + wizard.setWindowTitle("Trivial Wizard") + wizard.show() + + return app.exec_() + +if __name__ == "__main__": + main() +//! [10] //! [11] diff --git a/sources/pyside2/doc/codesnippets/examples/graphicsview/simpleanchorlayout/main.cpp b/sources/pyside2/doc/codesnippets/examples/graphicsview/simpleanchorlayout/main.cpp new file mode 100644 index 000000000..73e1a40b3 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/graphicsview/simpleanchorlayout/main.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [adding a corner anchor in two steps] +layout.addAnchor(a, Qt.AnchorTop, layout, Qt.AnchorTop) +layout.addAnchor(a, Qt.AnchorLeft, layout, Qt.AnchorLeft) +//! [adding a corner anchor in two steps] + +//! [adding a corner anchor] +layout.addCornerAnchors(a, Qt.TopLeftCorner, layout, Qt.TopLeftCorner) +//! [adding a corner anchor] + +//! [adding anchors] +layout.addAnchor(b, Qt.AnchorLeft, a, Qt.AnchorRight) +layout.addAnchor(b, Qt.AnchorTop, a, Qt.AnchorBottom) +//! [adding anchors] + +//! [adding anchors to match sizes in two steps] +layout.addAnchor(b, Qt.AnchorLeft, c, Qt.AnchorLeft) +layout.addAnchor(b, Qt.AnchorRight, c, Qt.AnchorRight) +//! [adding anchors to match sizes in two steps] + +//! [adding anchors to match sizes] +layout.addAnchors(b, c, Qt.Horizontal) +//! [adding anchors to match sizes] diff --git a/sources/pyside2/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/sources/pyside2/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp new file mode 100644 index 000000000..4732b7773 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp @@ -0,0 +1,109 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################ + +//! [0] +def __init__(self, parent): + QSortFilterProxyModel.__init__(self, parent) +//! [0] + +//! [1] +def setFilterMinimumDate(self, date): + self.minDate = date + self.invalidateFilter() + +//! [1] + +//! [2] +def setFilterMaximumDate(self, date): + self.maxDate = date + self.invalidateFilter() + +//! [2] + +//! [3] +def filterAcceptsRow(self, sourceRow, sourceParent): + index0 = sourceModel().index(sourceRow, 0, sourceParent) + index1 = sourceModel().index(sourceRow, 1, sourceParent) + index2 = sourceModel().index(sourceRow, 2, sourceParent) + + regex = filterRegExp() + return (regex.indexIn(sourceModel().data(index0)) != -1 + or regex.indexIn(sourceModel().data(index1)) != -1 + and dateInRange(sourceModel().data(index2)) +//! [3] + +//! [4] //! [5] +def lessThan(self, left, right): + leftData = sourceModel().data(left) + rightData = sourceModel().data(right) +//! [4] + +//! [6] + if isinstance(leftData, QDateTime): + return leftData < rightData + else: + emailPattern = QRegExp("([\\w\\.]*@[\\w\\.]*)") + + if left.column() == 1 && emailPattern.indexIn(leftData) != -1: + leftData = emailPattern.cap(1) + + if right.column() == 1 && emailPattern.indexIn(rightData) != -1: + rightData = emailPattern.cap(1) + + return leftString < rightString + +//! [5] //! [6] + +//! [7] +def dateInRange(self, date): + return (!minDate.isValid() || date > minDate) + && (!maxDate.isValid() || date < maxDate) + +//! [7] diff --git a/sources/pyside2/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp b/sources/pyside2/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp new file mode 100644 index 000000000..77eaf55e6 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [0] +def __init__(self, parent): + QAbstractItemDelegate.__init__(self, parent) + self.pixelSize = 12 +//! [0] + +//! [1] +def paint(self, painter, option, index): +//! [2] + if option.state and QStyle.State_Selected: + painter.fillRect(option.rect, option.palette.highlight()) +//! [1] + +//! [3] + size = qMin(option.rect.width(), option.rect.height()) +//! [3] //! [4] + brightness = index.model().data(index, Qt.DisplayRole).toInt() + radius = (size/2.0) - (brightness/255.0 * size/2.0) + if radius == 0.0: + return +//! [4] + +//! [5] + painter.save() +//! [5] //! [6] + painter.setRenderHint(QPainter.Antialiasing, true) +//! [6] //! [7] + painter.setPen(Qt.NoPen) +//! [7] //! [8] + if option.state and QStyle.State_Selected: +//! [8] //! [9] + painter.setBrush(option.palette.highlightedText()) + else +//! [2] + painter.setBrush(QBrush(Qt.black)) +//! [9] + +//! [10] + painter.drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius, + option.rect.y() + option.rect.height()/2 - radius, + 2*radius, 2*radius)) + painter.restore() +//! [10] + +//! [11] +def sizeHint(self, option, index): + return QSize(self.pixelSize, self.pixelSize) +//! [11] + +//! [12] +def setPixelSize(self, size): + self.pixelSize = size +//! [12] diff --git a/sources/pyside2/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp b/sources/pyside2/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp new file mode 100644 index 000000000..b9f582640 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp @@ -0,0 +1,197 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> +#include <QtXml> + +#include "domitem.h" +#include "dommodel.h" + +//! [0] +DomModel::DomModel(QDomDocument document, QObject *parent) + : QAbstractItemModel(parent), domDocument(document) +{ + rootItem = new DomItem(domDocument, 0); +} +//! [0] + +//! [1] +DomModel::~DomModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +def columnCount(self, parent): + return 3 +//! [2] + +//! [3] +QVariant DomModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + DomItem *item = static_cast<DomItem*>(index.internalPointer()); + + QDomNode node = item->node(); +//! [3] //! [4] + QStringList attributes; + QDomNamedNodeMap attributeMap = node.attributes(); + + switch (index.column()) { + case 0: + return node.nodeName(); + case 1: + for (int i = 0; i < attributeMap.count(); ++i) { + QDomNode attribute = attributeMap.item(i); + attributes << attribute.nodeName() + "=\"" + +attribute.nodeValue() + "\""; + } + return attributes.join(" "); + case 2: + return node.nodeValue().split("\n").join(" "); + default: + return QVariant(); + } +} +//! [4] + +//! [5] +Qt::ItemFlags DomModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [5] + +//! [6] +QVariant DomModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch (section) { + case 0: + return tr("Name"); + case 1: + return tr("Attributes"); + case 2: + return tr("Value"); + default: + return QVariant(); + } + } + + return QVariant(); +} +//! [6] + +//! [7] +QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); +//! [7] + +//! [8] + DomItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [8] + +//! [9] +QModelIndex DomModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + DomItem *childItem = static_cast<DomItem*>(child.internalPointer()); + DomItem *parentItem = childItem->parent(); + + if (!parentItem || parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} +//! [9] + +//! [10] +int DomModel::rowCount(const QModelIndex &parent) const +{ + if (parent.column() > 0) + return 0; + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); + + return parentItem->node().childNodes().count(); +} +//! [10] diff --git a/sources/pyside2/doc/codesnippets/examples/linguist/hellotr/main.cpp b/sources/pyside2/doc/codesnippets/examples/linguist/hellotr/main.cpp new file mode 100644 index 000000000..7125841ef --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/linguist/hellotr/main.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QPushButton> +//! [0] +from PySide2.QtCore import QTranslator +//! [0] + +//! [1] //! [2] +def main(args): +//! [1] //! [3] //! [4] + app = QApplication(args) +//! [3] + +//! [5] + translator = QTranslator() +//! [5] //! [6] + translator.load("hellotr_la") +//! [6] //! [7] + app.installTranslator(translator) +//! [4] //! [7] + +//! [8] + hello = QPushButton(QPushButton.tr("Hello world!")) +//! [8] + hello.resize(100, 30) + + hello.show() + return app.exec_() +//! [2] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp new file mode 100644 index 000000000..d22a39151 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp @@ -0,0 +1,359 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################ + +//! [0] +from PySide2.QtGui import * +//! [0] + +//! [1] +def __init__(self): + QMainWindow.__init__(self) +//! [1] //! [2] + textEdit = QPlainTextEdit() + setCentralWidget(textEdit) + + createActions() + createMenus() + createToolBars() + createStatusBar() + + readSettings() + + connect(textEdit.document(), SIGNAL("contentsChanged()"), + self, SLOT("documentWasModified()")) + + setCurrentFile("") + setUnifiedTitleAndToolBarOnMac(True) + +//! [2] + +//! [3] +def closeEvent(self, event): +//! [3] //! [4] + if maybeSave(): + writeSettings() + event.accept() + else: + event.ignore() +//! [4] + +//! [5] +def File(self): +//! [5] //! [6] + if maybeSave(): + textEdit.clear() + setCurrentFile("") +//! [6] + +//! [7] +def open(self): +//! [7] //! [8] + if maybeSave(): + fileName = QFileDialog.getOpenFileName(self) + if !fileName.isEmpty(): + loadFile(fileName) +//! [8] + +//! [9] +def save(self): +//! [9] //! [10] + if curFile.isEmpty(): + return saveAs() + else: + return saveFile(curFile) +//! [10] + +//! [11] +def saveAs(self): +//! [11] //! [12] + fileName = QFileDialog.getSaveFileName(self) + if fileName.isEmpty(): + return False + + return saveFile(fileName) +//! [12] + +//! [13] +def about(self): +//! [13] //! [14] + QMessageBox.about(self, tr("About Application"), + tr("The <b>Application</b> example demonstrates how to " + "write modern GUI applications using Qt, with a menu bar, " + "toolbars, and a status bar.")) + +//! [14] + +//! [15] +def documentWasModified(self): +//! [15] //! [16] + setWindowModified(textEdit.document().isModified()) +//! [16] + +//! [17] +def MainWindow.createActions(self): +//! [17] //! [18] + Act = QAction(QIcon(":/images/new.png"), tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) + +//! [19] + openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [18] //! [19] + + saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + saveAsAct = QAction(tr("Save &As..."), self) + saveAsAct.setShortcuts(QKeySequence.SaveAs) + saveAsAct.setStatusTip(tr("Save the document under a name")) + connect(saveAsAct, SIGNAL("triggered()"), self, SLOT("saveAs()")) + +//! [20] + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) +//! [20] + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + +//! [21] + cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) +//! [21] + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), textEdit, SLOT("cut()")) + + copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) + copyAct.setShortcuts(QKeySequence.Copy) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), textEdit, SLOT("copy()")) + + pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), textEdit, SLOT("paste()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + +//! [22] + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) +//! [22] + +//! [23] + cutAct.setEnabled(False) +//! [23] //! [24] + copyAct.setEnabled(False) + connect(textEdit, SIGNAL("copyAvailable(bool)"), + cutAct, SLOT("setEnabled(bool)")) + connect(textEdit, SIGNAL("copyAvailable(bool)"), + copyAct, SLOT("setEnabled(bool)")) +} +//! [24] + +//! [25] //! [26] +def createMenus(self): +//! [25] //! [27] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [28] + fileMenu.addAction(openAct) +//! [28] + fileMenu.addAction(saveAct) +//! [26] + fileMenu.addAction(saveAsAct) + fileMenu.addSeparator() + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + + menuBar().addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) + +//! [27] + +//! [29] //! [30] +def createToolBars(self): + fileToolBar = addToolBar(tr("File")) + fileToolBar.addAction(Act) +//! [29] //! [31] + fileToolBar.addAction(openAct) +//! [31] + fileToolBar.addAction(saveAct) + + editToolBar = addToolBar(tr("Edit")) + editToolBar.addAction(cutAct) + editToolBar.addAction(copyAct) + editToolBar.addAction(pasteAct) +//! [30] + +//! [32] +def createStatusBar(self): +//! [32] //! [33] + statusBar().showMessage(tr("Ready")) + +//! [33] + +//! [34] //! [35] +def readSettings(self): +//! [34] //! [36] + settings("Trolltech", "Application Example") + pos = settings.value("pos", QPoint(200, 200)).toPoint() + size = settings.value("size", QSize(400, 400)).toSize() + resize(size) + move(pos) + +//! [35] //! [36] + +//! [37] //! [38] +def writeSettings(self): +//! [37] //! [39] + settings = QSettings("Trolltech", "Application Example") + settings.setValue("pos", pos()) + settings.setValue("size", size()) + +//! [38] //! [39] + +//! [40] +def maybeSave(self): +//! [40] //! [41] + if textEdit.document()->isModified(): + ret = QMessageBox.warning(self, tr("Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) + if ret == QMessageBox.Save: + return save() + elif ret == QMessageBox.Cancel: + return False + return True +//! [41] + +//! [42] +def loadFile(self, fileName): +//! [42] //! [43] + file = QFile(fileName) + if !file.open(QFile.ReadOnly | QFile.Text): + QMessageBox.warning(self, tr("Application"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return + + in = QTextStream(file) + QApplication.setOverrideCursor(Qt::WaitCursor) + textEdit.setPlainText(in.readAll()) + QApplication.restoreOverrideCursor() + + setCurrentFile(fileName) + statusBar().showMessage(tr("File loaded"), 2000) + +//! [43] + +//! [44] +def saveFile(self, fileName): +//! [44] //! [45] + file = QFile(fileName) + if !file.open(QFile.WriteOnly | QFile::Text): + QMessageBox.warning(self, tr("Application"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return False + + out = QTextStream(file) + QApplication.setOverrideCursor(Qt.WaitCursor) + out << textEdit.toPlainText() + QApplication.restoreOverrideCursor() + + setCurrentFile(fileName) + statusBar().showMessage(tr("File saved"), 2000) + return True + +//! [45] + +//! [46] +def setCurrentFile(fileName): +//! [46] //! [47] + curFile = fileName + textEdit.document().setModified(False) + setWindowModified(False) + + if curFile.isEmpty(): + shownName = "untitled.txt" + else: + shownName = strippedName(curFile) + + setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application"))) + +//! [47] + +//! [48] +def strippedName(self, fullFileName): +//! [48] //! [49] + return QFileInfo(fullFileName).fileName() +//! [49] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp new file mode 100644 index 000000000..c6fd2894b --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp @@ -0,0 +1,255 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################ + +//! [0] +from PySide2.QtGui import * +//! [0] + +//! [1] +def __init__(self): + textEdit = QTextEdit() + setCentralWidget(textEdit) + + createActions() + createMenus() + createToolBars() + createStatusBar() + createDockWindows() + + setWindowTitle(tr("Dock Widgets")) + + Letter() + setUnifiedTitleAndToolBarOnMac(True) +//! [1] + +//! [2] +def Letter(self) + textEdit.clear() + + cursor = QTextCursor(textEdit.textCursor()) + cursor.movePosition(QTextCursor.Start) + topFrame = cursor.currentFrame() + topFrameFormat = topFrame.frameFormat() + topFrameFormat.setPadding(16) + topFrame.setFrameFormat(topFrameFormat) + + textFormat = QTextCharFormat() + boldFormat = QTextCharFormat() + boldFormat.setFontWeight(QFont.Bold) + italicFormat = QTextCharFormat() + italicFormat.setFontItalic(True) + + tableFormat = QTextTableFormat() + tableFormat.setBorder(1) + tableFormat.setCellPadding(16) + tableFormat.setAlignment(Qt.AlignRight) + cursor.insertTable(1, 1, tableFormat) + cursor.insertText("The Firm", boldFormat) + cursor.insertBlock() + cursor.insertText("321 City Street", textFormat) + cursor.insertBlock() + cursor.insertText("Industry Park") + cursor.insertBlock() + cursor.insertText("Some Country") + cursor.setPosition(topFrame.lastPosition()) + cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) + cursor.insertBlock() + cursor.insertBlock() + cursor.insertText("Dear ", textFormat) + cursor.insertText("NAME", italicFormat) + cursor.insertText(",", textFormat) + for i in range(3): + cursor.insertBlock() + cursor.insertText(tr("Yours sincerely,"), textFormat) + for i in range(3): + cursor.insertBlock() + cursor.insertText("The Boss", textFormat) + cursor.insertBlock() + cursor.insertText("ADDRESS", italicFormat) +//! [2] + +//! [3] +def print(self) + document = textEdit.document() + printer = QPrinter() + + dlg = QPrintDialog(&printer, self) + if dlg.exec() != QDialog.Accepted: + return + + document.print(printer) + statusBar().showMessage(tr("Ready"), 2000) +//! [3] + +//! [4] +def save(self): + fileName = QFileDialog.getSaveFileName(self, + tr("Choose a file name"), ".", + tr("HTML (*.html *.htm)")) + if fileName.isEmpty(): + return + file = QFile(fileName) + if !file.open(QFile.WriteOnly | QFile::Text): + QMessageBox.warning(self, tr("Dock Widgets"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return + + + out = QTextStream(file) + QApplication.setOverrideCursor(Qt::WaitCursor) + out << textEdit.toHtml() + QApplication.restoreOverrideCursor() + + statusBar().showMessage(tr("Saved '%1'").arg(fileName), 2000) + +//! [4] + +//! [5] +def undo(self): + document = textEdit.document() + document.undo() + +//! [5] + +//! [6] +def insertCustomer(self, customer): + if customer.isEmpty(): + return + + customerList = customer.split(", ") + document = textEdit.document() + cursor = document.find("NAME") + if not cursor.isNull(): + cursor.beginEditBlock() + cursor.insertText(customerList.at(0)) + oldcursor = cursor + cursor = document.find("ADDRESS") + if not cursor.isNull(): + for i in range(customerList.size()): + cursor.insertBlock() + cursor.insertText(customerList.at(i)) + + cursor.endEditBlock() + else: + oldcursor.endEditBlock() +//! [6] + +//! [7] +def addParagraph(self, paragraph): + if (paragraph.isEmpty()) + return + + document = textEdit.document() + cursor = document.find(tr("Yours sincerely,")) + if cursor.isNull(): + return + cursor.beginEditBlock() + cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor, 2) + cursor.insertBlock() + cursor.insertText(paragraph) + cursor.insertBlock() + cursor.endEditBlock() + +//! [7] + + +//! [8] +def createStatusBar(self): + statusBar().showMessage(tr("Ready")) + +//! [8] + +//! [9] +def createDockWindows(self): + dock = QDockWidget(tr("Customers"), self) + dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) + customerList = QListWidget(dock) + customerList.addItems(QStringList() + << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" + << "Jane Doe, Memorabilia, 23 Watersedge, Beaton" + << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" + << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" + << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" + << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula") + dock.setWidget(customerList) + addDockWidget(Qt.RightDockWidgetArea, dock) + viewMenu.addAction(dock.toggleViewAction()) + + dock = QDockWidget(tr("Paragraphs"), self) + paragraphsList = QListWidget(dock) + paragraphsList.addItems(QStringList() + << "Thank you for your payment which we have received today." + << "Your order has been dispatched and should be with you " + "within 28 days." + << "We have dispatched those items that were in stock. The " + "rest of your order will be dispatched once all the " + "remaining items have arrived at our warehouse. No " + "additional shipping charges will be made." + << "You made a small overpayment (less than $5) which we " + "will keep on account for you, or return at your request." + << "You made a small underpayment (less than $1), but we have " + "sent your order anyway. We'll add self underpayment to " + "your next bill." + << "Unfortunately you did not send enough money. Please remit " + "an additional $. Your order will be dispatched as soon as " + "the complete amount has been received." + << "You made an overpayment (more than $5). Do you wish to " + "buy more items, or should we return the excess to you?") + dock.setWidget(paragraphsList) + addDockWidget(Qt.RightDockWidgetArea, dock) + viewMenu.addAction(dock.toggleViewAction()) + + connect(customerList, SIGNAL("currentTextChanged(const QString &)"), + self, SLOT("insertCustomer(const QString &)")) + connect(paragraphsList, SIGNAL("currentTextChanged(const QString &)"), + self, SLOT("addParagraph(const QString &)")) +//! [9] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/mainwindow.cpp new file mode 100644 index 000000000..33cb540b4 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/mainwindow.cpp @@ -0,0 +1,367 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * + +//! [0] +def __init__(self): + Q__init__(self) + + widget = QWidget() + setCentralWidget(widget) +//! [0] + +//! [1] + topFiller = QWidget() + topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + infoLabel = QLabel(tr("<i>Choose a menu option, or right-click to " + "invoke a context menu</i>")) + infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) + infoLabel.setAlignment(Qt.AlignCenter) + + bottomFiller = QWidget() + bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + layout = QVBoxLayout() + layout.setMargin(5) + layout.addWidget(topFiller) + layout.addWidget(infoLabel) + layout.addWidget(bottomFiller) + widget.setLayout(layout) +//! [1] + +//! [2] + createActions() + createMenus() + + message = tr("A context menu is available by right-clicking") + statusBar().showMessage(message) + + setWindowTitle(tr("Menus")) + setMinimumSize(160, 160) + resize(480, 320) + +//! [2] + +//! [3] +def contextMenuEvent(self, event): + menu = QMenu(self) + menu.addAction(cutAct) + menu.addAction(copyAct) + menu.addAction(pasteAct) + menu.exec_(event.globalPos()") + +//! [3] + +def File(self): + infoLabel.setText(tr("Invoked <b>File|New</b>")) + + +def open(self): + infoLabel.setText(tr("Invoked <b>File|Open</b>")) + + +def save(self): + infoLabel.setText(tr("Invoked <b>File|Save</b>")) + +def print_(self): + infoLabel.setText(tr("Invoked <b>File|Print</b>")) + +def undo(self): + infoLabel.setText(tr("Invoked <b>Edit|Undo</b>")) + +def redo(self): + infoLabel.setText(tr("Invoked <b>Edit|Redo</b>")) + +def cut(self): + + infoLabel.setText(tr("Invoked <b>Edit|Cut</b>")) + + +def copy(self): + + infoLabel.setText(tr("Invoked <b>Edit|Copy</b>")) + + +def paste(self): + + infoLabel.setText(tr("Invoked <b>Edit|Paste</b>")) + + +def bold(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Bold</b>")) + + +def italic(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Italic</b>")) + + +def leftAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Left Align</b>")) + + +def rightAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Right Align</b>")) + + +def justify(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Justify</b>")) + + +def center(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Center</b>")) + + +def setLineSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")) + + +def setParagraphSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")) + + +def about(self): + + infoLabel.setText(tr("Invoked <b>Help|About</b>")) + QMessageBox.about(self, tr("About Menu"), + tr("The <b>Menu</b> example shows how to create " + "menu-bar menus and context menus.")) + + +def aboutQt(self): + + infoLabel.setText(tr("Invoked <b>Help|About Qt</b>")) + + +//! [4] +def createActions(self): + +//! [5] + Act = new QAction(tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) +//! [4] + + openAct = QAction(tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [5] + + saveAct = QAction(tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + printAct = QAction(tr("&Print..."), self) + printAct.setShortcuts(QKeySequence.Print) + printAct.setStatusTip(tr("Print the document")) + connect(printAct, SIGNAL("triggered()"), self, SLOT("print_()")) + + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + + undoAct = QAction(tr("&Undo"), self) + undoAct.setShortcuts(QKeySequence.Undo) + undoAct.setStatusTip(tr("Undo the last operation")) + connect(undoAct, SIGNAL("triggered()"), self, SLOT("undo()")) + + redoAct = QAction(tr("&Redo"), self) + redoAct.setShortcuts(QKeySequence.Redo) + redoAct.setStatusTip(tr("Redo the last operation")) + connect(redoAct, SIGNAL("triggered()"), self, SLOT("redo()")) + + cutAct = QAction(tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(tr("&Copy"), self) + copyAct.setShortcut(tr("Ctrl+C")) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + boldAct = QAction(tr("&Bold"), self) + boldAct.setCheckable(True) + boldAct.setShortcut(tr("Ctrl+B")) + boldAct.setStatusTip(tr("Make the text bold")) + connect(boldAct, SIGNAL("triggered()"), self, SLOT("bold()")) + + QFont boldFont = boldAct.font() + boldFont.setBold(True) + boldAct.setFont(boldFont) + + italicAct = QAction(tr("&Italic"), self) + italicAct.setCheckable(True) + italicAct.setShortcut(tr("Ctrl+I")) + italicAct.setStatusTip(tr("Make the text italic")) + connect(italicAct, SIGNAL("triggered()"), self, SLOT("italic()")) + + QFont italicFont = italicAct.font() + italicFont.setItalic(True) + italicAct.setFont(italicFont) + + setLineSpacingAct = QAction(tr("Set &Line Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between the lines of a " + "paragraph")) + connect(setLineSpacingAct, SIGNAL("triggered()"), self, SLOT("setLineSpacing()")) + + setParagraphSpacingAct = QAction(tr("Set &Paragraph Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between paragraphs")) + connect(setParagraphSpacingAct, SIGNAL("triggered()"), + self, SLOT("setParagraphSpacing()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + connect(aboutQtAct, SIGNAL("triggered()"), self, SLOT("aboutQt()")) + + leftAlignAct = QAction(tr("&Left Align"), self) + leftAlignAct.setCheckable(True) + leftAlignAct.setShortcut(tr("Ctrl+L")) + leftAlignAct.setStatusTip(tr("Left align the selected text")) + connect(leftAlignAct, SIGNAL("triggered()"), self, SLOT("leftAlign()")) + + rightAlignAct = QAction(tr("&Right Align"), self) + rightAlignAct.setCheckable(True) + rightAlignAct.setShortcut(tr("Ctrl+R")) + rightAlignAct.setStatusTip(tr("Right align the selected text")) + connect(rightAlignAct, SIGNAL("triggered()"), self, SLOT("rightAlign()")) + + justifyAct = QAction(tr("&Justify"), self) + justifyAct.setCheckable(True) + justifyAct.setShortcut(tr("Ctrl+J")) + justifyAct.setStatusTip(tr("Justify the selected text")) + connect(justifyAct, SIGNAL("triggered()"), self, SLOT("justify()")) + + centerAct = QAction(tr("&Center"), self) + centerAct.setCheckable(True) + centerAct.setShortcut(tr("Ctrl+E")) + centerAct.setStatusTip(tr("Center the selected text")) + connect(centerAct, SIGNAL("triggered()"), self, SLOT("center()")) + +//! [6] //! [7] + alignmentGroup = QActionGroup(self) + alignmentGroup.addAction(leftAlignAct) + alignmentGroup.addAction(rightAlignAct) + alignmentGroup.addAction(justifyAct) + alignmentGroup.addAction(centerAct) + leftAlignAct.setChecked(True) +//! [6] + +//! [7] + +//! [8] +def createMenus(self): + +//! [9] //! [10] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [9] + fileMenu.addAction(openAct) +//! [10] + fileMenu.addAction(saveAct) + fileMenu.addAction(printAct) +//! [11] + fileMenu.addSeparator() +//! [11] + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(undoAct) + editMenu.addAction(redoAct) + editMenu.addSeparator() + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + editMenu.addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) +//! [8] + +//! [12] + formatMenu = editMenu.addMenu(tr("&Format")) + formatMenu.addAction(boldAct) + formatMenu.addAction(italicAct) + formatMenu.addSeparator()->setText(tr("Alignment")) + formatMenu.addAction(leftAlignAct) + formatMenu.addAction(rightAlignAct) + formatMenu.addAction(justifyAct) + formatMenu.addAction(centerAct) + formatMenu.addSeparator() + formatMenu.addAction(setLineSpacingAct) + formatMenu.addAction(setParagraphSpacingAct) +//! [12] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp new file mode 100644 index 000000000..ed23e83eb --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp @@ -0,0 +1,381 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * + +def __init__(self): + + mdiArea = QMdiArea() + mdiArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) + mdiArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) + setCentralWidget(mdiArea) + connect(mdiArea, SIGNAL("subWindowActivated(QMdiSubWindow *)"), + self, SLOT("updateMenus()")) + windowMapper = QSignalMapper(self) + connect(windowMapper, SIGNAL("mapped(QWidget *)"), + self, SLOT("setActiveSubWindow(QWidget *)")) + + createActions() + createMenus() + createToolBars() + createStatusBar() + updateMenus() + + readSettings() + + setWindowTitle(tr("MDI")) + setUnifiedTitleAndToolBarOnMac(True) + + +def closeEvent(self, event): + mdiArea.closeAllSubWindows() + if self.activeMdiChild(): + event.ignore() + else: + self.writeSettings() + event.accept() + +def File(self): + child = self.createMdiChild() + child.File() + child.show() + + +def open(self): + fileName = QFileDialog.getOpenFileName(self) + if !fileName.isEmpty()): + existing = self.findMdiChild(fileName) + if existing: + mdiArea.setActiveSubWindow(existing) + return + + child = createMdiChild() + if child.loadFile(fileName)): + statusBar().showMessage(tr("File loaded"), 2000) + child.show() + else: + child.close() + +def save(self): + if self.activeMdiChild() && self.activeMdiChild().save(): + self.statusBar().showMessage(tr("File saved"), 2000) + +def saveAs(self): + if self.activeMdiChild() && self.activeMdiChild().saveAs(): + self.statusBar().showMessage(tr("File saved"), 2000) + +def cut(self): + if self.activeMdiChild(): + self.activeMdiChild().cut() + +def copy(self): + if self.activeMdiChild(): + activeMdiChild().copy() + +def paste(self): + if self.activeMdiChild(): + activeMdiChild().paste() + +def about(self): + QMessageBox.about(self, tr("About MDI"), + tr("The <b>MDI</b> example demonstrates how to write multiple " + "document interface applications using Qt.")") + +def updateMenus(self): + hasMdiChild = (activeMdiChild() != 0) + self.saveAct.setEnabled(hasMdiChild) + self.saveAsAct.setEnabled(hasMdiChild) + self.pasteAct.setEnabled(hasMdiChild) + self.closeAct.setEnabled(hasMdiChild) + self.closeAllAct.setEnabled(hasMdiChild) + self.tileAct.setEnabled(hasMdiChild) + self.cascadeAct.setEnabled(hasMdiChild) + self.nextAct.setEnabled(hasMdiChild) + self.previousAct.setEnabled(hasMdiChild) + self.separatorAct.setVisible(hasMdiChild) + + hasSelection = (self.activeMdiChild() && + self.activeMdiChild().textCursor().hasSelection()") + self.cutAct.setEnabled(hasSelection) + self.copyAct.setEnabled(hasSelection) + +def updateWindowMenu(self): + self.windowMenu.clear() + self.windowMenu.addAction(closeAct) + self.windowMenu.addAction(closeAllAct) + self.windowMenu.addSeparator() + self.windowMenu.addAction(tileAct) + self.windowMenu.addAction(cascadeAct) + self.windowMenu.addSeparator() + self.windowMenu.addAction(nextAct) + self.windowMenu.addAction(previousAct) + self.windowMenu.addAction(separatorAct) + + windows = mdiArea.subWindowList() + separatorAct.setVisible(!windows.isEmpty()") + + for i in range((int i = 0 i < windows.size(); ++i) + MdiChild *child = qobject_cast<MdiChild *>(windows.at(i).widget()") + + QString text + if (i < 9) + text = tr("&%1 %2").arg(i + 1) + .arg(child.userFriendlyCurrentFile()") + else + text = tr("%1 %2").arg(i + 1) + .arg(child.userFriendlyCurrentFile()") + + QAction *action = windowMenu.addAction(text) + action.setCheckable(True) + action .setChecked(child == activeMdiChild()") + connect(action, SIGNAL("triggered()"), windowMapper, SLOT("map()")) + windowMapper.setMapping(action, windows.at(i)") + + + +MdiChild *createMdiChild() + + MdiChild *child = MdiChild + mdiArea.addSubWindow(child) + + connect(child, SIGNAL("copyAvailable(bool)"), + cutAct, SLOT("setEnabled(bool)")) + connect(child, SIGNAL("copyAvailable(bool)"), + copyAct, SLOT("setEnabled(bool)")) + + return child + + +def createActions() + + Act = new QAction(QIcon(":/images/new.png"), tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")") + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) + + openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")") + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) + + saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")") + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + saveAsAct = QAction(tr("Save &As..."), self) + saveAsAct.setShortcuts(QKeySequence.SaveAs) + saveAsAct.setStatusTip(tr("Save the document under a name")") + connect(saveAsAct, SIGNAL("triggered()"), self, SLOT("saveAs()")) + +//! [0] + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")") + exitAct.setStatusTip(tr("Exit the application")") + connect(exitAct, SIGNAL("triggered()"), qApp, SLOT("closeAllWindows()")) +//! [0] + + cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")") + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) + copyAct.setShortcuts(QKeySequence.Copy) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")") + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")") + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + closeAct = QAction(tr("Cl&ose"), self) + closeAct.setShortcut(tr("Ctrl+F4")") + closeAct.setStatusTip(tr("Close the active window")") + connect(closeAct, SIGNAL("triggered()"), + mdiArea, SLOT("closeActiveSubWindow()")) + + closeAllAct = QAction(tr("Close &All"), self) + closeAllAct.setStatusTip(tr("Close all the windows")") + connect(closeAllAct, SIGNAL("triggered()"), + mdiArea, SLOT("closeAllSubWindows()")) + + tileAct = QAction(tr("&Tile"), self) + tileAct.setStatusTip(tr("Tile the windows")") + connect(tileAct, SIGNAL("triggered()"), mdiArea, SLOT("tileSubWindows()")) + + cascadeAct = QAction(tr("&Cascade"), self) + cascadeAct.setStatusTip(tr("Cascade the windows")") + connect(cascadeAct, SIGNAL("triggered()"), mdiArea, SLOT("cascadeSubWindows()")) + + nextAct = QAction(tr("Ne&xt"), self) + nextAct.setShortcuts(QKeySequence.NextChild) + nextAct.setStatusTip(tr("Move the focus to the next window")") + connect(nextAct, SIGNAL("triggered()"), + mdiArea, SLOT("activateNextSubWindow()")) + + previousAct = QAction(tr("Pre&vious"), self) + previousAct.setShortcuts(QKeySequence.PreviousChild) + previousAct.setStatusTip(tr("Move the focus to the previous " + "window")") + connect(previousAct, SIGNAL("triggered()"), + mdiArea, SLOT("activatePreviousSubWindow()")) + + separatorAct = QAction(self) + separatorAct.setSeparator(True) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")") + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")") + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + + +def createMenus() + + fileMenu = menuBar().addMenu(tr("&File")") + fileMenu.addAction(Act) + fileMenu.addAction(openAct) + fileMenu.addAction(saveAct) + fileMenu.addAction(saveAsAct) + fileMenu.addSeparator() + QAction *action = fileMenu.addAction(tr("Switch layout direction")") + connect(action, SIGNAL("triggered()"), self, SLOT("switchLayoutDirection()")) + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")") + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + + windowMenu = menuBar().addMenu(tr("&Window")") + updateWindowMenu() + connect(windowMenu, SIGNAL("aboutToShow()"), self, SLOT("updateWindowMenu()")) + + menuBar().addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")") + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) + + +def createToolBars() + + fileToolBar = addToolBar(tr("File")") + fileToolBar.addAction(Act) + fileToolBar.addAction(openAct) + fileToolBar.addAction(saveAct) + + editToolBar = addToolBar(tr("Edit")") + editToolBar.addAction(cutAct) + editToolBar.addAction(copyAct) + editToolBar.addAction(pasteAct) + + +def createStatusBar() + + statusBar().showMessage(tr("Ready")") + + +def readSettings() + + QSettings settings("Trolltech", "MDI Example") + QPoint pos = settings.value("pos", QPoint(200, 200)").toPoint() + QSize size = settings.value("size", QSize(400, 400)").toSize() + move(pos) + resize(size) + + +def writeSettings() + + QSettings settings("Trolltech", "MDI Example") + settings.setValue("pos", pos()") + settings.setValue("size", size()") + + +MdiChild *activeMdiChild() + + if (QMdiSubWindow *activeSubWindow = mdiArea.activeSubWindow()") + return qobject_cast<MdiChild *>(activeSubWindow.widget()") + return 0 + + +QMdiSubWindow *findMdiChild(const QString &fileName) + + QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath() + + foreach (QMdiSubWindow *window, mdiArea.subWindowList()") + MdiChild *mdiChild = qobject_cast<MdiChild *>(window.widget()") + if (mdiChild.currentFile() == canonicalFilePath) + return window + + return 0 + + +def switchLayoutDirection() + + if (layoutDirection() == Qt.LeftToRight) + qApp.setLayoutDirection(Qt.RightToLeft) + else + qApp.setLayoutDirection(Qt.LeftToRight) + + +def setActiveSubWindow(QWidget *window) + + if (!window) + return + mdiArea.setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window)") + diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp new file mode 100644 index 000000000..33cb540b4 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp @@ -0,0 +1,367 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * + +//! [0] +def __init__(self): + Q__init__(self) + + widget = QWidget() + setCentralWidget(widget) +//! [0] + +//! [1] + topFiller = QWidget() + topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + infoLabel = QLabel(tr("<i>Choose a menu option, or right-click to " + "invoke a context menu</i>")) + infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) + infoLabel.setAlignment(Qt.AlignCenter) + + bottomFiller = QWidget() + bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + layout = QVBoxLayout() + layout.setMargin(5) + layout.addWidget(topFiller) + layout.addWidget(infoLabel) + layout.addWidget(bottomFiller) + widget.setLayout(layout) +//! [1] + +//! [2] + createActions() + createMenus() + + message = tr("A context menu is available by right-clicking") + statusBar().showMessage(message) + + setWindowTitle(tr("Menus")) + setMinimumSize(160, 160) + resize(480, 320) + +//! [2] + +//! [3] +def contextMenuEvent(self, event): + menu = QMenu(self) + menu.addAction(cutAct) + menu.addAction(copyAct) + menu.addAction(pasteAct) + menu.exec_(event.globalPos()") + +//! [3] + +def File(self): + infoLabel.setText(tr("Invoked <b>File|New</b>")) + + +def open(self): + infoLabel.setText(tr("Invoked <b>File|Open</b>")) + + +def save(self): + infoLabel.setText(tr("Invoked <b>File|Save</b>")) + +def print_(self): + infoLabel.setText(tr("Invoked <b>File|Print</b>")) + +def undo(self): + infoLabel.setText(tr("Invoked <b>Edit|Undo</b>")) + +def redo(self): + infoLabel.setText(tr("Invoked <b>Edit|Redo</b>")) + +def cut(self): + + infoLabel.setText(tr("Invoked <b>Edit|Cut</b>")) + + +def copy(self): + + infoLabel.setText(tr("Invoked <b>Edit|Copy</b>")) + + +def paste(self): + + infoLabel.setText(tr("Invoked <b>Edit|Paste</b>")) + + +def bold(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Bold</b>")) + + +def italic(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Italic</b>")) + + +def leftAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Left Align</b>")) + + +def rightAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Right Align</b>")) + + +def justify(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Justify</b>")) + + +def center(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Center</b>")) + + +def setLineSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")) + + +def setParagraphSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")) + + +def about(self): + + infoLabel.setText(tr("Invoked <b>Help|About</b>")) + QMessageBox.about(self, tr("About Menu"), + tr("The <b>Menu</b> example shows how to create " + "menu-bar menus and context menus.")) + + +def aboutQt(self): + + infoLabel.setText(tr("Invoked <b>Help|About Qt</b>")) + + +//! [4] +def createActions(self): + +//! [5] + Act = new QAction(tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) +//! [4] + + openAct = QAction(tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [5] + + saveAct = QAction(tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + printAct = QAction(tr("&Print..."), self) + printAct.setShortcuts(QKeySequence.Print) + printAct.setStatusTip(tr("Print the document")) + connect(printAct, SIGNAL("triggered()"), self, SLOT("print_()")) + + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + + undoAct = QAction(tr("&Undo"), self) + undoAct.setShortcuts(QKeySequence.Undo) + undoAct.setStatusTip(tr("Undo the last operation")) + connect(undoAct, SIGNAL("triggered()"), self, SLOT("undo()")) + + redoAct = QAction(tr("&Redo"), self) + redoAct.setShortcuts(QKeySequence.Redo) + redoAct.setStatusTip(tr("Redo the last operation")) + connect(redoAct, SIGNAL("triggered()"), self, SLOT("redo()")) + + cutAct = QAction(tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(tr("&Copy"), self) + copyAct.setShortcut(tr("Ctrl+C")) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + boldAct = QAction(tr("&Bold"), self) + boldAct.setCheckable(True) + boldAct.setShortcut(tr("Ctrl+B")) + boldAct.setStatusTip(tr("Make the text bold")) + connect(boldAct, SIGNAL("triggered()"), self, SLOT("bold()")) + + QFont boldFont = boldAct.font() + boldFont.setBold(True) + boldAct.setFont(boldFont) + + italicAct = QAction(tr("&Italic"), self) + italicAct.setCheckable(True) + italicAct.setShortcut(tr("Ctrl+I")) + italicAct.setStatusTip(tr("Make the text italic")) + connect(italicAct, SIGNAL("triggered()"), self, SLOT("italic()")) + + QFont italicFont = italicAct.font() + italicFont.setItalic(True) + italicAct.setFont(italicFont) + + setLineSpacingAct = QAction(tr("Set &Line Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between the lines of a " + "paragraph")) + connect(setLineSpacingAct, SIGNAL("triggered()"), self, SLOT("setLineSpacing()")) + + setParagraphSpacingAct = QAction(tr("Set &Paragraph Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between paragraphs")) + connect(setParagraphSpacingAct, SIGNAL("triggered()"), + self, SLOT("setParagraphSpacing()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + connect(aboutQtAct, SIGNAL("triggered()"), self, SLOT("aboutQt()")) + + leftAlignAct = QAction(tr("&Left Align"), self) + leftAlignAct.setCheckable(True) + leftAlignAct.setShortcut(tr("Ctrl+L")) + leftAlignAct.setStatusTip(tr("Left align the selected text")) + connect(leftAlignAct, SIGNAL("triggered()"), self, SLOT("leftAlign()")) + + rightAlignAct = QAction(tr("&Right Align"), self) + rightAlignAct.setCheckable(True) + rightAlignAct.setShortcut(tr("Ctrl+R")) + rightAlignAct.setStatusTip(tr("Right align the selected text")) + connect(rightAlignAct, SIGNAL("triggered()"), self, SLOT("rightAlign()")) + + justifyAct = QAction(tr("&Justify"), self) + justifyAct.setCheckable(True) + justifyAct.setShortcut(tr("Ctrl+J")) + justifyAct.setStatusTip(tr("Justify the selected text")) + connect(justifyAct, SIGNAL("triggered()"), self, SLOT("justify()")) + + centerAct = QAction(tr("&Center"), self) + centerAct.setCheckable(True) + centerAct.setShortcut(tr("Ctrl+E")) + centerAct.setStatusTip(tr("Center the selected text")) + connect(centerAct, SIGNAL("triggered()"), self, SLOT("center()")) + +//! [6] //! [7] + alignmentGroup = QActionGroup(self) + alignmentGroup.addAction(leftAlignAct) + alignmentGroup.addAction(rightAlignAct) + alignmentGroup.addAction(justifyAct) + alignmentGroup.addAction(centerAct) + leftAlignAct.setChecked(True) +//! [6] + +//! [7] + +//! [8] +def createMenus(self): + +//! [9] //! [10] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [9] + fileMenu.addAction(openAct) +//! [10] + fileMenu.addAction(saveAct) + fileMenu.addAction(printAct) +//! [11] + fileMenu.addSeparator() +//! [11] + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(undoAct) + editMenu.addAction(redoAct) + editMenu.addSeparator() + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + editMenu.addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) +//! [8] + +//! [12] + formatMenu = editMenu.addMenu(tr("&Format")) + formatMenu.addAction(boldAct) + formatMenu.addAction(italicAct) + formatMenu.addSeparator()->setText(tr("Alignment")) + formatMenu.addAction(leftAlignAct) + formatMenu.addAction(rightAlignAct) + formatMenu.addAction(justifyAct) + formatMenu.addAction(centerAct) + formatMenu.addSeparator() + formatMenu.addAction(setLineSpacingAct) + formatMenu.addAction(setParagraphSpacingAct) +//! [12] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/sdi/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/sdi/mainwindow.cpp new file mode 100644 index 000000000..fe52f0fbb --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/sdi/mainwindow.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [implicit tr context] +def createMenus(self): + fileMenu = menuBar().addMenu("&File") +//! [implicit tr context] + +//! [0] + fileToolBar = addToolBar("File") + fileToolBar.addAction(newAct) + fileToolBar.addAction(openAct) +//! [0] diff --git a/sources/pyside2/doc/codesnippets/examples/painting/svggenerator/window.cpp b/sources/pyside2/doc/codesnippets/examples/painting/svggenerator/window.cpp new file mode 100644 index 000000000..60c415949 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/painting/svggenerator/window.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QColorDialog> +#include <QFileDialog> +#include <QPainter> +#include <QSvgGenerator> +#include "window.h" +#include "displaywidget.h" + +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); +} + +void Window::updateBackground(int background) +{ + displayWidget->setBackground(DisplayWidget::Background(background)); +} + +void Window::updateColor() +{ + QColor color = QColorDialog::getColor(displayWidget->color()); + if (color.isValid()) + displayWidget->setColor(color); +} + +void Window::updateShape(int shape) +{ + displayWidget->setShape(DisplayWidget::Shape(shape)); +} + +//! [save SVG] +def saveSvg(self): + newPath = QFileDialog.getSaveFileName(self, QObject.tr("Save SVG"), path, QObject.tr("SVG files (*.svg)")) + + if newPath.isEmpty(): + return + + path = newPath + +//![configure SVG generator] + generator = QSvgGenerator() + generator.setFileName(path) + generator.setSize(QSize(200, 200)) + generator.setViewBox(QRect(0, 0, 200, 200)) + generator.setTitle(QObject.tr("SVG Generator Example Drawing")) + generator.setDescription(QObject.tr("An SVG drawing created by the SVG Generator Example provided with Qt.")) +//![configure SVG generator] +//![begin painting] + painter = QPainter() + painter.begin(generator) +//![begin painting] + displayWidget->paint(painter) +//![end painting] + painter.end() +//![end painting] + +//! [save SVG] diff --git a/sources/pyside2/doc/codesnippets/examples/richtext/textobject/svgtextobject.h b/sources/pyside2/doc/codesnippets/examples/richtext/textobject/svgtextobject.h new file mode 100644 index 000000000..0c9b7c06e --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/richtext/textobject/svgtextobject.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#ifndef SVGTEXTOBJECT_H +#define SVGTEXTOBJECT_H + +#include <QTextObjectInterface> + +QT_BEGIN_NAMESPACE +class QTextDocument; +class QTextFormat; +class QPainter; +class QRectF; +class QSizeF; +QT_END_NAMESPACE + +//![0] //![1] +class SvgTextObject(QObject, QTextObjectInterface): + def __init__(self,...): + super(SvgTextObject, self).__init__(...) + ... +//![1] + +public: + QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, + const QTextFormat &format); + void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, + int posInDocument, const QTextFormat &format); +}; +//![0] + +#endif diff --git a/sources/pyside2/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp b/sources/pyside2/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp new file mode 100644 index 000000000..844ee7711 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtSql> + +#include "editablesqlmodel.h" + +EditableSqlModel::EditableSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +def flags(self, index): + flags = QSqlQueryModel.flags(index) + if index.column() == 1 or index.column() == 2: + flags |= Qt.ItemIsEditable + return flags +//! [0] + +//! [1] +def setData(self, index, value, role): + if index.column() < 1 or index.column() > 2: + return False + + primaryKeyIndex = QSqlQueryModel.index(index.row(), 0) + id = self.data(primaryKeyIndex).toInt() + + self.clear() + + ok = False + if index.column() == 1: + ok = self.setFirstName(id, value) + else: + ok = self.setLastName(id, value) + self.refresh() + return ok +} +//! [1] + +void EditableSqlModel::refresh() +{ + setQuery("select * from person"); + setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +//! [2] +def setFirstName(self, personId, firstName): + query = QSqlQuery() + query.prepare("update person set firstname = ? where id = ?") + query.addBindValue(firstName) + query.addBindValue(personId) + return query.exec() +//! [2] + +bool EditableSqlModel::setLastName(int personId, const QString &lastName) +{ + QSqlQuery query; + query.prepare("update person set lastname = ? where id = ?"); + query.addBindValue(lastName); + query.addBindValue(personId); + return query.exec(); +} diff --git a/sources/pyside2/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp b/sources/pyside2/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp new file mode 100644 index 000000000..8df1d73b2 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp @@ -0,0 +1,120 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * +from PySide2.QtSql import * + +def initializeModel(model): +//! [0] + model.setTable("employee") +//! [0] + + model.setEditStrategy(QSqlTableModel.OnManualSubmit) +//! [1] + model.setRelation(2, QSqlRelation("city", "id", "name")) +//! [1] //! [2] + model.setRelation(3, QSqlRelation("country", "id", "name")) +//! [2] + +//! [3] + model.setHeaderData(0, Qt.Horizontal, QObject::tr("ID")) + model.setHeaderData(1, Qt.Horizontal, QObject::tr("Name")) + model.setHeaderData(2, Qt.Horizontal, QObject::tr("City")) + model.setHeaderData(3, Qt.Horizontal, QObject::tr("Country")) +//! [3] + + model.select() + + +def createView(title, model): +//! [4] + view = QTableView() + view.setModel(model) + view.setItemDelegate(QSqlRelationalDelegate(view)) +//! [4] + view.setWindowTitle(title) + return view + + +def createRelationalTables(): + query = QSqlQuery() + query.exec_("create table employee(id int primary key, name varchar(20), city int, country int)") + query.exec_("insert into employee values(1, 'Espen', 5000, 47)") + query.exec_("insert into employee values(2, 'Harald', 80000, 49)") + query.exec_("insert into employee values(3, 'Sam', 100, 1)") + + query.exec_("create table city(id int, name varchar(20))") + query.exec_("insert into city values(100, 'San Jose')") + query.exec_("insert into city values(5000, 'Oslo')") + query.exec_("insert into city values(80000, 'Munich')") + + query.exec_("create table country(id int, name varchar(20))") + query.exec_("insert into country values(1, 'USA')") + query.exec_("insert into country values(47, 'Norway')") + query.exec_("insert into country values(49, 'Germany')") + + +def main(): + + app = QApplication([]) + if !createConnection(): + return 1 + + createRelationalTables() + + model = QSqlRelationalTableModel() + + initializeModel(model) + + view = createView(QObject.tr("Relational Table Model"), model) + view.show() + + return app.exec_() + diff --git a/sources/pyside2/doc/codesnippets/examples/uitools/textfinder/textfinder.cpp b/sources/pyside2/doc/codesnippets/examples/uitools/textfinder/textfinder.cpp new file mode 100644 index 000000000..e493945c9 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/uitools/textfinder/textfinder.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtUiTools> +#include <QtWidgets> +#include "textfinder.h" + +//! [0] +def __init__(self, parent = None): + QWidget.__init__(self. parent) + formWidget = self.loadUiFile() + +//! [1] + self.ui_findButton = qFindChild(QPushButton, self, "findButton") + self.ui_textEdit = qFindChild(QTextEdit, self, "textEdit") + self.ui_lineEdit = qFindChild(QLineEdit, self, "lineEdit") +//! [0] //! [1] + +//! [2] + QMetaObject.connectSlotsByName(self) +//! [2] + +//! [3a] + self.loadTextFile() +//! [3a] + +//! [3b] + layout = QVBoxLayout() + layout.addWidget(formWidget) + self.setLayout(layout) +//! [3b] + +//! [3c] + self.setWindowTitle("Text Finder") + self.isFirstTime = True +//! [3c] + +//! [4] +def loadUiFile(self): + loader = QUiLoader() + return loader.load(":/forms/textfinder.ui", self) +//! [4] + +//! [5] +def loadTextFile(self): + inputFile = QFile(":/forms/input.txt") + inputFile.open(QIODevice.ReadOnly) + in = QTextStream(inputFile) + line = in.readAll() + inputFile.close() + + self.ui_textEdit.append(line) + self.ui_textEdit.setUndoRedoEnabled(False) + self.ui_textEdit.setUndoRedoEnabled(True) +//! [5] + +//! [6] //! [7] +@Slot() +def on_findButton_clicked(self): + searchString = self.ui_lineEdit.text() + document = self.ui_textEdit.document() + + found = False + + if not self.isFirstTime: + document.undo() + + if not searchString: + QMessageBox.information(self, "Empty Search Field", + "The search field is empty. Please enter a word and click Find.") + else: + highlightCursor = QTextCursor(document) + cursor = QTextCursor(document) + cursor.beginEditBlock() +//! [6] + plainFormat = QTextCharFormat(highlightCursor.charFormat()) + colorFormat = QTextCharFormat(plainFormat) + colorFormat.setForeground(Qt.red) + + while not highlightCursor.isNull() and not highlightCursor.atEnd(): + highlightCursor = document.find(searchString, highlightCursor, QTextDocument.FindWholeWords) + + if not highlightCursor.isNull(): + found = True + highlightCursor.movePosition(QTextCursor.WordRight, QTextCursor.KeepAnchor) + highlightCursor.mergeCharFormat(colorFormat) +//! [8] + cursor.endEditBlock() +//! [7] //! [9] + self.isFirstTime = False + + if not found: + QMessageBox.information(self, "Word Not Found", "Sorry, the word cannot be found."); +//! [8] //! [9] diff --git a/sources/pyside2/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp b/sources/pyside2/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp new file mode 100644 index 000000000..dcd24b275 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "analogclock.h" + +//! [0] //! [1] +AnalogClock::AnalogClock(QWidget *parent) +//! [0] //! [2] + : QWidget(parent) +//! [2] //! [3] +{ +//! [3] //! [4] + timer = QTimer(self) +//! [4] //! [5] + self.connect(timer, SIGNAL("timeout()"), self.update) +//! [5] //! [6] + timer.start(1000) +//! [6] + + setWindowTitle(tr("Analog Clock")); + resize(200, 200); +//! [7] +} +//! [1] //! [7] + +//! [8] //! [9] +void AnalogClock::paintEvent(QPaintEvent *) +//! [8] //! [10] +{ + static const QPoint hourHand[3] = { + QPoint(7, 8), + QPoint(-7, 8), + QPoint(0, -40) + }; + static const QPoint minuteHand[3] = { + QPoint(7, 8), + QPoint(-7, 8), + QPoint(0, -70) + }; + + QColor hourColor(127, 0, 127); + QColor minuteColor(0, 127, 127, 191); + + int side = qMin(width(), height()); + QTime time = QTime::currentTime(); +//! [10] + +//! [11] + QPainter painter(this); +//! [11] //! [12] + painter.setRenderHint(QPainter::Antialiasing); +//! [12] //! [13] + painter.translate(width() / 2, height() / 2); +//! [13] //! [14] + painter.scale(side / 200.0, side / 200.0); +//! [9] //! [14] + +//! [15] + painter.setPen(Qt::NoPen); +//! [15] //! [16] + painter.setBrush(hourColor); +//! [16] + +//! [17] //! [18] + painter.save(); +//! [17] //! [19] + painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); + painter.drawConvexPolygon(hourHand, 3); + painter.restore(); +//! [18] //! [19] + +//! [20] + painter.setPen(hourColor); +//! [20] //! [21] + + for (int i = 0; i < 12; ++i) { + painter.drawLine(88, 0, 96, 0); + painter.rotate(30.0); + } +//! [21] + +//! [22] + painter.setPen(Qt::NoPen); +//! [22] //! [23] + painter.setBrush(minuteColor); + +//! [24] + painter.save(); + painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); + painter.drawConvexPolygon(minuteHand, 3); + painter.restore(); +//! [23] //! [24] + +//! [25] + painter.setPen(minuteColor); +//! [25] //! [26] + +//! [27] + for (int j = 0; j < 60; ++j) { + if ((j % 5) != 0) + painter.drawLine(92, 0, 96, 0); + painter.rotate(6.0); + } +//! [27] +} +//! [26] diff --git a/sources/pyside2/doc/codesnippets/examples/widgets/groupbox/window.cpp b/sources/pyside2/doc/codesnippets/examples/widgets/groupbox/window.cpp new file mode 100644 index 000000000..9203e5ba3 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/widgets/groupbox/window.cpp @@ -0,0 +1,187 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of PySide2. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +//! [0] +def __init__(self, parent = None): + QWidget.__init__(self, parent) + + grid = QGridLayout() + grid.addWidget(createFirstExclusiveGroup(), 0, 0) + grid.addWidget(createSecondExclusiveGroup(), 1, 0) + grid.addWidget(createNonExclusiveGroup(), 0, 1) + grid.addWidget(createPushButtonGroup(), 1, 1) + setLayout(grid) + + setWindowTitle("Group Boxes") + resize(480, 320) + +//! [0] + +//! [1] +def createFirstExclusiveGroup(self): +//! [2] + groupBox = QGroupBox("Exclusive Radio Buttons") + + radio1 = QRadioButton("&Radio button 1") + radio2 = QRadioButton("R&adio button 2") + radio3 = QRadioButton("Ra&dio button 3") + + radio1.setChecked(True) +//! [1] //! [3] + + vbox = QVBoxLayout() + vbox.addWidget(radio1) + vbox.addWidget(radio2) + vbox.addWidget(radio3) + vbox.addStretch(1) + groupBox.setLayout(vbox) +//! [2] + return groupBox +//! [3] + +//! [4] +def createSecondExclusiveGroup(self): + groupBox = QGroupBox("E&xclusive Radio Buttons") + groupBox.setCheckable(True) + groupBox.setChecked(False) +//! [4] + +//! [5] + radio1 = QRadioButton("Rad&io button 1") + radio2 = QRadioButton("Radi&o button 2") + radio3 = QRadioButton("Radio &button 3") + radio1.setChecked(True) + checkBox = QCheckBox("Ind&ependent checkbox") + checkBox.setChecked(True) +//! [5] + +//! [6] + vbox = QVBoxLayout() + vbox.addWidget(radio1) + vbox.addWidget(radio2) + vbox.addWidget(radio3) + vbox.addWidget(checkBox) + vbox.addStretch(1) + groupBox.setLayout(vbox) + + return groupBox +//! [6] + +//! [7] +def createNonExclusiveGroup(self): + groupBox = QGroupBox("Non-Exclusive Checkboxes") + groupBox.setFlat(True) +//! [7] + +//! [8] + checkBox1 = QCheckBox("&Checkbox 1") + checkBox2 = QCheckBox("C&heckbox 2") + checkBox2.setChecked(True) + tristateBox = QCheckBox("Tri-&state button") + tristateBox.setTristate(True) +//! [8] + tristateBox.setCheckState(Qt.PartiallyChecked) + +//! [9] + vbox = QVBoxLayout() + vbox.addWidget(checkBox1) + vbox.addWidget(checkBox2) + vbox.addWidget(tristateBox) + vbox.addStretch(1) + groupBox.setLayout(vbox) + + return groupBox +//! [9] + +//! [10] +def createPushButtonGroup(self): + groupBox = QGroupBox("&Push Buttons") + groupBox.setCheckable(True) + groupBox.setChecked(True) +//! [10] + +//! [11] + pushButton = QPushButton("&Normal Button") + toggleButton = QPushButton("&Toggle Button") + toggleButton.setCheckable(True) + toggleButton.setChecked(True) + flatButton = QPushButton("&Flat Button") + flatButton.setFlat(True) +//! [11] + +//! [12] + popupButton = QPushButton("Pop&up Button") + menu = QMenu(self) + menu.addAction("&First Item") + menu.addAction("&Second Item") + menu.addAction("&Third Item") + menu.addAction("F&ourth Item") + popupButton.setMenu(menu) +//! [12] + + newAction = menu.addAction("Submenu") + QMenu *subMenu = QMenu("Popup Submenu") + subMenu.addAction("Item 1") + subMenu.addAction("Item 2") + subMenu.addAction("Item 3") + newAction.setMenu(subMenu) + +//! [13] + vbox = QVBoxLayout() + vbox.addWidget(pushButton) + vbox.addWidget(toggleButton) + vbox.addWidget(flatButton) + vbox.addWidget(popupButton) + vbox.addStretch(1) + groupBox.setLayout(vbox) + + return groupBox +} +//! [13] diff --git a/sources/pyside2/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp b/sources/pyside2/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp new file mode 100644 index 000000000..f0d1f2ac6 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp @@ -0,0 +1,72 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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$ +## +############################################################################ + + +//! [0] +def __init__(self, parent): + QSpinBox.__init__(self, parent) + +//! [0] + +//! [1] +def valueFromText(self, text): + regExp = QRegExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?")) + + if regExp.exactMatch(text): + return regExp.cap(1).toInt() + else: + return 0 +//! [1] + +//! [2] +def textFromValue(self, value): + return self.tr("%1 x %1").arg(value) + +//! [2] diff --git a/sources/pyside2/doc/codesnippets/examples/widgets/spinboxes/window.cpp b/sources/pyside2/doc/codesnippets/examples/widgets/spinboxes/window.cpp new file mode 100644 index 000000000..bf07fb73c --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/widgets/spinboxes/window.cpp @@ -0,0 +1,249 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of PySide2. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, 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 PySide2.QtGui import * + +//! [0] +def __init__(self): + createSpinBoxes() + createDateTimeEdits() + createDoubleSpinBoxes() + + layout = QHBoxLayout() + layout.addWidget(spinBoxesGroup) + layout.addWidget(editsGroup) + layout.addWidget(doubleSpinBoxesGroup) + setLayout(layout) + + setWindowTitle(tr("Spin Boxes")) +//! [0] + +//! [1] +def createSpinBoxes(self): + spinBoxesGroup = QGroupBox(tr("Spinboxes")) + + integerLabel = QLabel(tr("Enter a value between " + "%1 and %2:").arg(-20).arg(20)) + integerSpinBox = QSpinBox() + integerSpinBox.setRange(-20, 20) + integerSpinBox.setSingleStep(1) + integerSpinBox.setValue(0) +//! [1] + +//! [2] + zoomLabel = QLabel(tr("Enter a zoom value between " + "%1 and %2:").arg(0).arg(1000)) +//! [3] + zoomSpinBox = QSpinBox() + zoomSpinBox.setRange(0, 1000) + zoomSpinBox.setSingleStep(10) + zoomSpinBox.setSuffix("%") + zoomSpinBox.setSpecialValueText(tr("Automatic")) + zoomSpinBox.setValue(100) +//! [2] //! [3] + +//! [4] + priceLabel = QLabel(tr("Enter a price between " + "%1 and %2:").arg(0).arg(999)) + priceSpinBox = QSpinBox() + priceSpinBox.setRange(0, 999) + priceSpinBox.setSingleStep(1) + priceSpinBox.setPrefix("$") + priceSpinBox.setValue(99) +//! [4] //! [5] + + spinBoxLayout = QVBoxLayout() + spinBoxLayout.addWidget(integerLabel) + spinBoxLayout.addWidget(integerSpinBox) + spinBoxLayout.addWidget(zoomLabel) + spinBoxLayout.addWidget(zoomSpinBox) + spinBoxLayout.addWidget(priceLabel) + spinBoxLayout.addWidget(priceSpinBox) + spinBoxesGroup.setLayout(spinBoxLayout) + +//! [5] + +//! [6] +def createDateTimeEdits(self): + editsGroup = QGroupBox(tr("Date and time spin boxes")) + + dateLabel = QLabel() + dateEdit = QDateEdit(QDate.currentDate()) + dateEdit.setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31)) + dateLabel.setText(tr("Appointment date (between %0 and %1):") + .arg(dateEdit.minimumDate().toString(Qt.ISODate)) + .arg(dateEdit.maximumDate().toString(Qt.ISODate))) +//! [6] + +//! [7] + timeLabel = QLabel() + timeEdit = QTimeEdit(QTime.currentTime()) + timeEdit.setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0)) + timeLabel.setText(tr("Appointment time (between %0 and %1):") + .arg(timeEdit.minimumTime().toString(Qt.ISODate)) + .arg(timeEdit.maximumTime().toString(Qt.ISODate))) +//! [7] + +//! [8] + meetingLabel = QLabel() + meetingEdit = QDateTimeEdit(QDateTime.currentDateTime()) +//! [8] + +//! [9] + formatLabel = QLabel(tr("Format string for the meeting date " + "and time:")) + formatComboBox = QComboBox() + formatComboBox.addItem("yyyy-MM-dd hh:mm:ss (zzz 'ms')") + formatComboBox.addItem("hh:mm:ss MM/dd/yyyy") + formatComboBox.addItem("hh:mm:ss dd/MM/yyyy") + formatComboBox.addItem("hh:mm:ss") + formatComboBox.addItem("hh:mm ap") +//! [9] //! [10] + + connect(formatComboBox, SIGNAL("activated(const QString &)"), + self, SLOT("setFormatString(const QString &)")) +//! [10] + + setFormatString(formatComboBox.currentText()) + +//! [11] + editsLayout = QVBoxLayout() + editsLayout.addWidget(dateLabel) + editsLayout.addWidget(dateEdit) + editsLayout.addWidget(timeLabel) + editsLayout.addWidget(timeEdit) + editsLayout.addWidget(meetingLabel) + editsLayout.addWidget(meetingEdit) + editsLayout.addWidget(formatLabel) + editsLayout.addWidget(formatComboBox) + editsGroup.setLayout(editsLayout) +//! [11] + +//! [12] +def setFormatString(self, formatString): + meetingEdit.setDisplayFormat(formatString) +//! [12] //! [13] + if meetingEdit.displayedSections() & QDateTimeEdit.DateSections_Mask: + meetingEdit.setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30)) + meetingLabel.setText(tr("Meeting date (between %0 and %1):") + .arg(meetingEdit.minimumDate().toString(Qt.ISODate)) + .arg(meetingEdit.maximumDate().toString(Qt.ISODate))) + else: + meetingEdit.setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0)) + meetingLabel.setText(tr("Meeting time (between %0 and %1):") + .arg(meetingEdit.minimumTime().toString(Qt.ISODate)) + .arg(meetingEdit.maximumTime().toString(Qt.ISODate))) +//! [13] + +//! [14] +def createDoubleSpinBoxes(): + doubleSpinBoxesGroup = QGroupBox(tr("Double precision spinboxes")) + + precisionLabel = QLabel(tr("Number of decimal places " + "to show:")) + precisionSpinBox = QSpinBox() + precisionSpinBox.setRange(0, 100) + precisionSpinBox.setValue(2) +//! [14] + +//! [15] + doubleLabel = QLabel(tr("Enter a value between " + "%1 and %2:").arg(-20).arg(20)) + doubleSpinBox = QDoubleSpinBox () + doubleSpinBox.setRange(-20.0, 20.0) + doubleSpinBox.setSingleStep(1.0) + doubleSpinBox.setValue(0.0) +//! [15] + +//! [16] + scaleLabel = QLabel(tr("Enter a scale factor between " + "%1 and %2:").arg(0).arg(1000.0)) + scaleSpinBox = QDoubleSpinBox() + scaleSpinBox.setRange(0.0, 1000.0) + scaleSpinBox.setSingleStep(10.0) + scaleSpinBox.setSuffix("%") + scaleSpinBox.setSpecialValueText(tr("No scaling")) + scaleSpinBox.setValue(100.0) +//! [16] + +//! [17] + priceLabel = QLabel(tr("Enter a price between " + "%1 and %2:").arg(0).arg(1000)) + priceSpinBox = QDoubleSpinBox() + priceSpinBox.setRange(0.0, 1000.0) + priceSpinBox.setSingleStep(1.0) + priceSpinBox.setPrefix("$") + priceSpinBox.setValue(99.99) + + connect(precisionSpinBox, SIGNAL("valueChanged(int)"), +//! [17] + self, SLOT("changePrecision(int))") + +//! [18] + spinBoxLayout = QVBoxLayout() + spinBoxLayout.addWidget(precisionLabel) + spinBoxLayout.addWidget(precisionSpinBox) + spinBoxLayout.addWidget(doubleLabel) + spinBoxLayout.addWidget(doubleSpinBox) + spinBoxLayout.addWidget(scaleLabel) + spinBoxLayout.addWidget(scaleSpinBox) + spinBoxLayout.addWidget(priceLabel) + spinBoxLayout.addWidget(priceSpinBox) + doubleSpinBoxesGroup.setLayout(spinBoxLayout) +} +//! [18] + +//! [19] +def changePrecision(self, int) + doubleSpinBox.setDecimals(decimals) + scaleSpinBox.setDecimals(decimals) + priceSpinBox.setDecimals(decimals) + +//! [19] diff --git a/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp new file mode 100644 index 000000000..c82f02752 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "xbelreader.h" + +//! [0] +XbelReader::XbelReader(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + QStyle *style = treeWidget->style(); + + folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), + QIcon::Normal, QIcon::Off); + folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), + QIcon::Normal, QIcon::On); + bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon)); +} +//! [0] + +//! [1] +def read(self, device): + self.setDevice(device) + + while not atEnd(): + readNext() + + if isStartElement(): + if self.name() == "xbel" and self.attributes().value("version") == "1.0": + self.readXBEL() + else: + self.raiseError(QObject.tr("The file is not an XBEL version 1.0 file.")); + + return not self.error(); +//! [1] + +//! [2] +void XbelReader::readUnknownElement() +{ + Q_ASSERT(isStartElement()); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) + readUnknownElement(); + } +} +//! [2] + +//! [3] +void XbelReader::readXBEL() +{ + Q_ASSERT(isStartElement() && name() == "xbel"); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "folder") + readFolder(0); + else if (name() == "bookmark") + readBookmark(0); + else if (name() == "separator") + readSeparator(0); + else + readUnknownElement(); + } + } +} +//! [3] + +//! [4] +void XbelReader::readTitle(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "title"); + + QString title = readElementText(); + item->setText(0, title); +} +//! [4] + +//! [5] +void XbelReader::readSeparator(QTreeWidgetItem *item) +{ + QTreeWidgetItem *separator = createChildItem(item); + separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); + separator->setText(0, QString(30, 0xB7)); + readElementText(); +} +//! [5] + +void XbelReader::readFolder(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "folder"); + + QTreeWidgetItem *folder = createChildItem(item); + bool folded = (attributes().value("folded") != "no"); + treeWidget->setItemExpanded(folder, !folded); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "title") + readTitle(folder); + else if (name() == "folder") + readFolder(folder); + else if (name() == "bookmark") + readBookmark(folder); + else if (name() == "separator") + readSeparator(folder); + else + readUnknownElement(); + } + } +} + +void XbelReader::readBookmark(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "bookmark"); + + QTreeWidgetItem *bookmark = createChildItem(item); + bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); + bookmark->setIcon(0, bookmarkIcon); + bookmark->setText(0, QObject::tr("Unknown title")); + bookmark->setText(1, attributes().value("href").toString()); + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "title") + readTitle(bookmark); + else + readUnknownElement(); + } + } +} + +QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) +{ + QTreeWidgetItem *childItem; + if (item) { + childItem = new QTreeWidgetItem(item); + } else { + childItem = new QTreeWidgetItem(treeWidget); + } + childItem->setData(0, Qt::UserRole, name().toString()); + return childItem; +} diff --git a/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h new file mode 100644 index 000000000..cb7207333 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#ifndef XBELREADER_H +#define XBELREADER_H + +#include <QIcon> +#include <QXmlStreamReader> + +QT_BEGIN_NAMESPACE +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +//! [0] +class XbelReader (QXmlStreamReader): +//! [1] + def __init__(self, treeWidget): + ... +//! [1] + + def read(self, device); + ... + +//! [2] + def readUnknownElement(self): + ... + def readXBEL(self): + ... + def readTitle(self, item): + ... + def readSeparator(self, item): + ... + def readFolder(self, item): + ... + def readBookmark(self, item): + ... + def createChildItem(self, item): + ... +//! [2] +//! [0] + +#endif diff --git a/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp new file mode 100644 index 000000000..372bc53bf --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "xbelwriter.h" + +//! [0] +XbelWriter::XbelWriter(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + setAutoFormatting(true); +} +//! [0] + +//! [1] +def writeFile(self, device): + self.setDevice(device) + + self.writeStartDocument() + self.writeDTD("<!DOCTYPE xbel>") + self.writeStartElement("xbel") + self.writeAttribute("version", "1.0") + for i in range(0, self.treeWidget.topLevelItemCount()): + self.writeItem(self.treeWidget.topLevelItem(i)) + + self.writeEndDocument() + return True; +//! [1] + +//! [2] +void XbelWriter::writeItem(QTreeWidgetItem *item) +{ + QString tagName = item->data(0, Qt::UserRole).toString(); + if (tagName == "folder") { + bool folded = !treeWidget->isItemExpanded(item); + writeStartElement(tagName); + writeAttribute("folded", folded ? "yes" : "no"); + writeTextElement("title", item->text(0)); + for (int i = 0; i < item->childCount(); ++i) + writeItem(item->child(i)); + writeEndElement(); + } else if (tagName == "bookmark") { + writeStartElement(tagName); + if (!item->text(1).isEmpty()) + writeAttribute("href", item->text(1)); + writeTextElement("title", item->text(0)); + writeEndElement(); + } else if (tagName == "separator") { + writeEmptyElement(tagName); + } +} +//! [2] |