aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-02-18 09:32:29 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-03-18 09:23:18 +0100
commitf81168387be7879f9167405b28d29ba26e7b3f14 (patch)
tree98b0bb11a86703c9621b0be2886d245fd232fa05
parented6bb6cf8f8f6005309f038f16134d478f15a1f3 (diff)
Add a context manager for override cursors
[ChangeLog][PySide] A context manager for override cursors has been added. It is now possible to write code like: with QApplication.setOverrideCursor(Qt.WaitCursor):... Change-Id: I443ce82389b48656f21c98df17d97e1b3b3323b5 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--examples/widgets/mainwindows/application/application.py26
-rw-r--r--examples/widgets/mainwindows/dockwidgets/dockwidgets.py5
-rw-r--r--examples/widgets/mainwindows/mdi/mdi.py26
-rw-r--r--sources/pyside6/PySide6/QtGui/CMakeLists.txt5
-rw-r--r--sources/pyside6/PySide6/QtGui/QtGui_global.post.h.in1
-rw-r--r--sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml20
-rw-r--r--sources/pyside6/PySide6/glue/qtgui.cpp6
-rw-r--r--sources/pyside6/PySide6/qtguihelper.h69
8 files changed, 125 insertions, 33 deletions
diff --git a/examples/widgets/mainwindows/application/application.py b/examples/widgets/mainwindows/application/application.py
index 3ec9344cb..b22f2b7cb 100644
--- a/examples/widgets/mainwindows/application/application.py
+++ b/examples/widgets/mainwindows/application/application.py
@@ -232,27 +232,25 @@ class MainWindow(QMainWindow):
return
inf = QTextStream(file)
- QApplication.setOverrideCursor(Qt.WaitCursor)
- self._text_edit.setPlainText(inf.readAll())
- QApplication.restoreOverrideCursor()
+ with QApplication.setOverrideCursor(Qt.WaitCursor):
+ self._text_edit.setPlainText(inf.readAll())
self.set_current_file(fileName)
self.statusBar().showMessage("File loaded", 2000)
def save_file(self, fileName):
error = None
- QApplication.setOverrideCursor(Qt.WaitCursor)
- file = QSaveFile(fileName)
- if file.open(QFile.WriteOnly | QFile.Text):
- outf = QTextStream(file)
- outf << self._text_edit.toPlainText()
- if not file.commit():
+ with QApplication.setOverrideCursor(Qt.WaitCursor):
+ file = QSaveFile(fileName)
+ if file.open(QFile.WriteOnly | QFile.Text):
+ outf = QTextStream(file)
+ outf << self._text_edit.toPlainText()
+ if not file.commit():
+ reason = file.errorString()
+ error = f"Cannot write file {fileName}:\n{reason}."
+ else:
reason = file.errorString()
- error = f"Cannot write file {fileName}:\n{reason}."
- else:
- reason = file.errorString()
- error = f"Cannot open file {fileName}:\n{reason}."
- QApplication.restoreOverrideCursor()
+ error = f"Cannot open file {fileName}:\n{reason}."
if error:
QMessageBox.warning(self, "Application", error)
diff --git a/examples/widgets/mainwindows/dockwidgets/dockwidgets.py b/examples/widgets/mainwindows/dockwidgets/dockwidgets.py
index 61b1dd781..db08895d6 100644
--- a/examples/widgets/mainwindows/dockwidgets/dockwidgets.py
+++ b/examples/widgets/mainwindows/dockwidgets/dockwidgets.py
@@ -145,9 +145,8 @@ class MainWindow(QMainWindow):
return
out = QTextStream(file)
- QApplication.setOverrideCursor(Qt.WaitCursor)
- out << self._text_edit.toHtml()
- QApplication.restoreOverrideCursor()
+ with QApplication.setOverrideCursor(Qt.WaitCursor):
+ out << self._text_edit.toHtml()
self.statusBar().showMessage(f"Saved '{filename}'", 2000)
diff --git a/examples/widgets/mainwindows/mdi/mdi.py b/examples/widgets/mainwindows/mdi/mdi.py
index a6ea6284d..06d80b1a1 100644
--- a/examples/widgets/mainwindows/mdi/mdi.py
+++ b/examples/widgets/mainwindows/mdi/mdi.py
@@ -81,9 +81,8 @@ class MdiChild(QTextEdit):
return False
instr = QTextStream(file)
- QApplication.setOverrideCursor(Qt.WaitCursor)
- self.setPlainText(instr.readAll())
- QApplication.restoreOverrideCursor()
+ with QApplication.setOverrideCursor(Qt.WaitCursor):
+ self.setPlainText(instr.readAll())
self.set_current_file(fileName)
@@ -106,18 +105,17 @@ class MdiChild(QTextEdit):
def save_file(self, fileName):
error = None
- QApplication.setOverrideCursor(Qt.WaitCursor)
- file = QSaveFile(fileName)
- if file.open(QFile.WriteOnly | QFile.Text):
- outstr = QTextStream(file)
- outstr << self.toPlainText()
- if not file.commit():
+ with QApplication.setOverrideCursor(Qt.WaitCursor):
+ file = QSaveFile(fileName)
+ if file.open(QFile.WriteOnly | QFile.Text):
+ outstr = QTextStream(file)
+ outstr << self.toPlainText()
+ if not file.commit():
+ reason = file.errorString()
+ error = f"Cannot write file {fileName}:\n{reason}."
+ else:
reason = file.errorString()
- error = f"Cannot write file {fileName}:\n{reason}."
- else:
- reason = file.errorString()
- error = f"Cannot open file {fileName}:\n{reason}."
- QApplication.restoreOverrideCursor()
+ error = f"Cannot open file {fileName}:\n{reason}."
if error:
QMessageBox.warning(self, "MDI", error)
diff --git a/sources/pyside6/PySide6/QtGui/CMakeLists.txt b/sources/pyside6/PySide6/QtGui/CMakeLists.txt
index 5bdb38253..2c95c71af 100644
--- a/sources/pyside6/PySide6/QtGui/CMakeLists.txt
+++ b/sources/pyside6/PySide6/QtGui/CMakeLists.txt
@@ -189,6 +189,7 @@ ${QtGui_GEN_DIR}/qtexttable_wrapper.cpp
${QtGui_GEN_DIR}/qtexttablecell_wrapper.cpp
${QtGui_GEN_DIR}/qtexttablecellformat_wrapper.cpp
${QtGui_GEN_DIR}/qtexttableformat_wrapper.cpp
+${QtGui_GEN_DIR}/qtguihelper_qoverridecursorguard_wrapper.cpp
${QtGui_GEN_DIR}/qtoolbarchangeevent_wrapper.cpp
${QtGui_GEN_DIR}/qtouchevent_wrapper.cpp
${QtGui_GEN_DIR}/qtransform_wrapper.cpp
@@ -233,5 +234,7 @@ create_pyside_module(NAME QtGui
TYPESYSTEM_NAME ${QtGui_BINARY_DIR}/typesystem_gui.xml
DROPPED_ENTRIES QtGui_DROPPED_ENTRIES)
-install(FILES ${pyside6_SOURCE_DIR}/qpytextobject.h DESTINATION include/PySide6/QtGui/)
+install(FILES ${pyside6_SOURCE_DIR}/qpytextobject.h
+ ${pyside6_SOURCE_DIR}/qtguihelper.h
+ DESTINATION include/PySide6/QtGui/)
diff --git a/sources/pyside6/PySide6/QtGui/QtGui_global.post.h.in b/sources/pyside6/PySide6/QtGui/QtGui_global.post.h.in
index 6d3a3eeac..deb7cf805 100644
--- a/sources/pyside6/PySide6/QtGui/QtGui_global.post.h.in
+++ b/sources/pyside6/PySide6/QtGui/QtGui_global.post.h.in
@@ -1 +1,2 @@
#include "qpytextobject.h" // PySide class
+#include <qtguihelper.h>
diff --git a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
index 70edaa30c..6010af37f 100644
--- a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
+++ b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
@@ -2666,7 +2666,13 @@
<add-function signature="exec_()" return-type="int">
<inject-code file="../glue/qtgui.cpp" snippet="qguiapplication-exec"/>
</add-function>
-
+ <modify-function signature="setOverrideCursor(const QCursor&amp;)">
+ <modify-argument index="return">
+ <replace-type modified-type="QtGuiHelper::QOverrideCursorGuard*"/>
+ </modify-argument>
+ <inject-code class="target" position="end" file="../glue/qtgui.cpp"
+ snippet="qguiapplication-setoverridecursor"/>
+ </modify-function>
</object-type>
<object-type name="QOpenGLContext">
@@ -3098,4 +3104,16 @@
</modify-argument>
</modify-function>
</object-type>
+
+ <namespace-type name="QtGuiHelper" visible="no">
+ <object-type name="QOverrideCursorGuard" copyable="no">
+ <add-function signature="__enter__()" return-type="QOverrideCursorGuard">
+ <inject-code file="../glue/qtcore.cpp" snippet="default-enter"/>
+ </add-function>
+ <add-function signature="__exit__(PyObject*,PyObject*,PyObject*)">
+ <inject-code>%CPPSELF.restoreOverrideCursor();</inject-code>
+ </add-function>
+ </object-type>
+ </namespace-type>
+
</typesystem>
diff --git a/sources/pyside6/PySide6/glue/qtgui.cpp b/sources/pyside6/PySide6/glue/qtgui.cpp
index f7bf2c57e..b312180b7 100644
--- a/sources/pyside6/PySide6/glue/qtgui.cpp
+++ b/sources/pyside6/PySide6/glue/qtgui.cpp
@@ -654,6 +654,12 @@ if (!PyTuple_SetItem(empty, 0, PyList_New(0))) {
}
// @snippet qguiapplication-2
+// @snippet qguiapplication-setoverridecursor
+auto *cppResult = new QtGuiHelper::QOverrideCursorGuard();
+%PYARG_0 = %CONVERTTOPYTHON[QtGuiHelper::QOverrideCursorGuard*](cppResult);
+Shiboken::Object::getOwnership(%PYARG_0); // Ensure the guard is removed
+// @snippet qguiapplication-setoverridecursor
+
// @snippet qscreen-grabWindow
WId id = %1;
%RETURN_TYPE retval = %CPPSELF.%FUNCTION_NAME(id, %2, %3, %4, %5);
diff --git a/sources/pyside6/PySide6/qtguihelper.h b/sources/pyside6/PySide6/qtguihelper.h
new file mode 100644
index 000000000..4fee6c53e
--- /dev/null
+++ b/sources/pyside6/PySide6/qtguihelper.h
@@ -0,0 +1,69 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qt for Python.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QTGUIHELPER_H
+#define QTGUIHELPER_H
+
+#include <QtGui/QGuiApplication>
+
+namespace QtGuiHelper {
+
+ class QOverrideCursorGuard
+ {
+ public:
+ Q_DISABLE_COPY_MOVE(QOverrideCursorGuard)
+
+ QOverrideCursorGuard() = default;
+ ~QOverrideCursorGuard() { restoreOverrideCursor(); }
+
+ void restoreOverrideCursor()
+ {
+ if (m_guard) {
+ QGuiApplication::restoreOverrideCursor();
+ m_guard = false;
+ }
+ }
+
+ private:
+ bool m_guard = true;
+ };
+
+} // namespace QtGuiHelper
+
+#endif // QTGUIHELPER_H