Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Fix __init__ of widgets that inherit from SpyderWidgetMixin for PyQt5 #16601

Merged
merged 4 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
Type `python bootstrap.py -- --help` to read about Spyder
options.""")
parser.add_argument('--gui', default=None,
help="GUI toolkit: pyqt5 (for PyQt5), pyqt (for PyQt4) or "
"pyside (for PySide, deprecated)")
help="GUI toolkit: pyqt5 (for PyQt5) or pyside2 "
"(for PySide2)")
parser.add_argument('--show-console', action='store_true', default=False,
help="(Deprecated) Does nothing, now the default behavior "
"is to show the console")
Expand All @@ -67,7 +67,7 @@
# Store variable to be used in self.restart (restart spyder instance)
os.environ['SPYDER_BOOTSTRAP_ARGS'] = str(sys.argv[1:])

assert args.gui in (None, 'pyqt5', 'pyqt', 'pyside'), \
assert args.gui in (None, 'pyqt5', 'pyside2'), \
"Invalid GUI toolkit option '%s'" % args.gui

# Start Spyder with a clean configuration directory for testing purposes
Expand Down
3 changes: 2 additions & 1 deletion spyder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def get_versions(reporev=True):
'bitness': 64 if sys.maxsize > 2**32 else 32,
'qt': qtpy.QtCore.__version__,
'qt_api': qtpy.API_NAME, # PyQt5
'qt_api_ver': qtpy.PYSIDE_VERSION if qtpy.API == "pyside2" else qtpy.PYQT_VERSION,
'qt_api_ver': (qtpy.PYSIDE_VERSION if qtpy.API == "pyside2"
else qtpy.PYQT_VERSION),
'system': system, # Linux, Windows, ...
'release': platform.release(), # XP, 10.6, 2.2.0, etc.
'revision': revision, # '9fdf926eccce',
Expand Down
8 changes: 6 additions & 2 deletions spyder/api/widgets/main_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
status bar widgets or toolbars.
"""

from qtpy import PYQT5
from qtpy.QtCore import Signal
from qtpy.QtWidgets import QWidget

Expand Down Expand Up @@ -101,8 +102,11 @@ class PluginMainContainer(QWidget, SpyderWidgetMixin, SpyderToolbarMixin):
"""

def __init__(self, name, plugin, parent=None):
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=plugin)
if PYQT5:
super().__init__(parent=parent, class_parent=plugin)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=plugin)

# Attributes
# --------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions spyder/api/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# Standard library imports
from collections import OrderedDict
import sys
import textwrap
from typing import Optional

# Third party imports
from qtpy import PYQT5
from qtpy.QtCore import QSize, Qt, Signal, Slot
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import (QHBoxLayout, QSizePolicy, QToolButton, QVBoxLayout,
Expand Down Expand Up @@ -187,8 +187,11 @@ class PluginMainWidget(QWidget, SpyderWidgetMixin, SpyderToolbarMixin):
"""

def __init__(self, name, plugin, parent=None):
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=plugin)
if PYQT5:
super().__init__(parent=parent, class_parent=plugin)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=plugin)

# Attributes
# --------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions spyder/api/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""Status bar widgets."""

# Third party imports
from qtpy import PYQT5
from qtpy.QtCore import Qt, QSize, QTimer, Signal
from qtpy.QtGui import QFont, QIcon
from qtpy.QtWidgets import QHBoxLayout, QLabel, QWidget
Expand Down Expand Up @@ -73,8 +74,12 @@ def __init__(self, parent=None, show_icon=True, show_label=True,
1. To use an icon, you need to redefine the ``get_icon`` method.
2. To use a label, you need to call ``set_value``.
"""
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent, class_parent=parent)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

self._parent = parent

self.show_icon = show_icon
Expand Down
2 changes: 1 addition & 1 deletion spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ def add_dockwidget(self, plugin):
self.addDockWidget(location, dockwidget)
self.widgetlist.append(plugin)

def global_callback(self):
def global_callback(self):
"""Global callback"""
widget = QApplication.focusWidget()
action = self.sender()
Expand Down
8 changes: 6 additions & 2 deletions spyder/plugins/breakpoints/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sys

# Third party imports
from qtpy import PYQT5
from qtpy.compat import to_qvariant
from qtpy.QtCore import QAbstractTableModel, QModelIndex, Qt, Signal
from qtpy.QtWidgets import QItemDelegate, QTableView, QVBoxLayout
Expand Down Expand Up @@ -184,8 +185,11 @@ class BreakpointTableView(QTableView, SpyderWidgetMixin):
sig_conditional_breakpoint_requested = Signal()

def __init__(self, parent, data):
QTableView.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent, class_parent=parent)
else:
QTableView.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

# Widgets
self.model = BreakpointTableModel(self, data)
Expand Down
4 changes: 2 additions & 2 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2045,8 +2045,8 @@ def update_recent_file_menu(self):
action = create_action(
self, fname,
icon=ima.get_icon_by_extension_or_type(
fname, scale_factor=1.0))
action.triggered[bool].connect(self.load)
fname, scale_factor=1.0))
action.triggered[bool].connect(self.load)
action.setData(to_qvariant(fname))
self.recent_file_menu.addAction(action)
self.clear_recent_action.setEnabled(len(recent_files) > 0)
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/editor/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def __init__(self, parent, actions):
@Slot()
def show_in_external_file_explorer(self, fnames=None):
"""Show file in external file explorer"""
if fnames is None or isinstance(fnames, bool):
if fnames is None or isinstance(fnames, bool):
fnames = self.get_current_filename()
try:
show_in_external_file_explorer(fnames)
Expand Down
14 changes: 9 additions & 5 deletions spyder/plugins/explorer/widgets/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys

# Third party imports
from qtpy import PYQT5
from qtpy.compat import getexistingdirectory, getsavefilename
from qtpy.QtCore import QDir, QMimeData, Qt, QTimer, QUrl, Signal, Slot
from qtpy.QtGui import QDrag
Expand Down Expand Up @@ -249,8 +250,11 @@ def __init__(self, parent=None):
parent: QWidget
Parent QWidget of the widget.
"""
QTreeView.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent=parent, class_parent=parent)
else:
QTreeView.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

# Attributes
self._parent = parent
Expand Down Expand Up @@ -608,9 +612,9 @@ def update_actions(self):
self.open_interpreter_action.setVisible(only_dirs)
self.open_with_spyder_action.setVisible(only_files and only_valid)
self.open_with_submenu.menuAction().setVisible(False)
clipboard = QApplication.clipboard()
has_urls = clipboard.mimeData().hasUrls()
self.paste_action.setDisabled(not has_urls)
clipboard = QApplication.clipboard()
has_urls = clipboard.mimeData().hasUrls()
self.paste_action.setDisabled(not has_urls)

# VCS support is quite limited for now, so we are enabling the VCS
# related actions only when a single file/folder is selected:
Expand Down
8 changes: 6 additions & 2 deletions spyder/plugins/help/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sys

# Third party imports
from qtpy import PYQT5
from qtpy.QtCore import Qt, QUrl, Signal, Slot, QPoint
from qtpy.QtGui import QColor
from qtpy.QtWebEngineWidgets import WEBENGINE, QWebEnginePage
Expand Down Expand Up @@ -154,8 +155,11 @@ class RichText(QWidget, SpyderWidgetMixin):
sig_link_clicked = Signal(QUrl)

def __init__(self, parent):
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent, class_parent=parent)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

self.webview = FrameWebView(self)
self.webview.setup()
Expand Down
1 change: 0 additions & 1 deletion spyder/plugins/ipythonconsole/widgets/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,3 @@ def eventFilter(self, obj, event):
return True

return super().eventFilter(obj, event)

30 changes: 15 additions & 15 deletions spyder/plugins/outlineexplorer/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

# Third party imports
from intervaltree import IntervalTree
from pkg_resources import parse_version
from qtpy import PYSIDE2
from pkg_resources import parse_version
from qtpy import PYSIDE2
from qtpy.compat import from_qvariant
from qtpy.QtCore import Qt, QTimer, Signal, Slot
from qtpy.QtWidgets import QTreeWidgetItem, QTreeWidgetItemIterator
Expand All @@ -27,9 +27,9 @@
from spyder.plugins.completion.api import SymbolKind, SYMBOL_KIND_ICON
from spyder.utils.qthelpers import set_item_user_text
from spyder.widgets.onecolumntree import OneColumnTree

if PYSIDE2:
from qtpy import PYSIDE_VERSION
if PYSIDE2:
from qtpy import PYSIDE_VERSION


# ---- Constants
Expand Down Expand Up @@ -727,16 +727,16 @@ def __sort_toplevel_items(self):
self.ordered_editor_ids if
self.editor_items.get(e_id) is not None]

# PySide <= 5.15.0 doesn’t support == and != comparison for the data
# types inside the compared lists (see [1], [2])
#
# [1] https://bugreports.qt.io/browse/PYSIDE-74
# [2] https://codereview.qt-project.org/c/pyside/pyside-setup/+/312945
update = (
(PYSIDE2 and parse_version(PYSIDE_VERSION) <= parse_version("5.15.0"))
or (current_ordered_items != new_ordered_items)
)
if update:
# PySide <= 5.15.0 doesn’t support == and != comparison for the data
# types inside the compared lists (see [1], [2])
#
# [1] https://bugreports.qt.io/browse/PYSIDE-74
# [2] https://codereview.qt-project.org/c/pyside/pyside-setup/+/312945
update = (
(PYSIDE2 and parse_version(PYSIDE_VERSION) <= parse_version("5.15.0"))
or (current_ordered_items != new_ordered_items)
)
if update:
selected_items = self.selectedItems()
self.save_expanded_state()
for index in range(self.topLevelItemCount()):
Expand Down
17 changes: 13 additions & 4 deletions spyder/plugins/plots/widgets/figurebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Third library imports
from qtconsole.svg import svg_to_clipboard, svg_to_image
from qtpy import PYQT5
from qtpy.compat import getexistingdirectory, getsavefilename
from qtpy.QtCore import QEvent, QPoint, QRect, QSize, Qt, QTimer, Signal, Slot
from qtpy.QtGui import QPainter, QPixmap
Expand Down Expand Up @@ -137,8 +138,12 @@ class FigureBrowser(QWidget, SpyderWidgetMixin):
"""

def __init__(self, parent=None, background_color=None):
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent=parent, class_parent=parent)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

self.shellwidget = None
self.is_visible = True
self.figviewer = None
Expand Down Expand Up @@ -318,8 +323,12 @@ class FigureViewer(QScrollArea, SpyderWidgetMixin):
"""This signal is emitted when a new figure is loaded."""

def __init__(self, parent=None, background_color=None):
QScrollArea.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent, class_parent=parent)
else:
QScrollArea.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

self.setAlignment(Qt.AlignCenter)
self.viewport().setObjectName("figviewport")
self.viewport().setStyleSheet(
Expand Down
4 changes: 2 additions & 2 deletions spyder/plugins/preferences/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def load_from_conf(self):
cb_bold.setChecked(bold)
cb_italic.setChecked(italic)

edit.textChanged.connect(lambda _foo, opt=option, sect=sec:
self.has_been_modified(sect, opt))
edit.textChanged.connect(lambda _foo, opt=option, sect=sec:
self.has_been_modified(sect, opt))
btn.clicked[bool].connect(lambda _foo, opt=option, sect=sec:
self.has_been_modified(sect, opt))
cb_bold.clicked[bool].connect(lambda _foo, opt=option, sect=sec:
Expand Down
9 changes: 7 additions & 2 deletions spyder/plugins/profiler/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from itertools import islice

# Third party imports
from qtpy import PYQT5
from qtpy.compat import getopenfilename, getsavefilename
from qtpy.QtCore import QByteArray, QProcess, QProcessEnvironment, Qt, Signal
from qtpy.QtGui import QColor
Expand Down Expand Up @@ -663,8 +664,12 @@ class ProfilerDataTree(QTreeWidget, SpyderWidgetMixin):
sig_edit_goto_requested = Signal(str, int, str)

def __init__(self, parent=None):
QTreeWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent, class_parent=parent)
else:
QTreeWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

self.header_list = [_('Function/Module'), _('Total Time'), _('Diff'),
_('Local Time'), _('Diff'), _('Calls'), _('Diff'),
_('File:line')]
Expand Down
11 changes: 6 additions & 5 deletions spyder/plugins/toolbar/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,13 @@ def create_toolbars_menu(self):
if not PYSIDE2:
# Modifying __class__ of a QObject created by C++ [1] seems
# to invalidate the corresponding Python object when PySide
# is used (changing __class__ of a QObject created in Python
# seems to work).
# is used (changing __class__ of a QObject created in
# Python seems to work).
#
# [1] There are Qt functions such as QToolBar::toggleViewAction(),
# QToolBar::addAction(QString), QMainWindow::addToolbar(QString)
# wich return a pointer to an already existing QObject.
# [1] There are Qt functions such as
# QToolBar.toggleViewAction(), QToolBar.addAction(QString)
# and QMainWindow.addToolbar(QString), which return a
# pointer to an already existing QObject.
action.__class__ = QActionID
action.action_id = f'toolbar_{toolbar_id}'
section = (
Expand Down
10 changes: 7 additions & 3 deletions spyder/plugins/variableexplorer/widgets/namespacebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import os
import os.path as osp

# Third library imports (qtpy)
# Third library imports
from qtpy import PYQT5
from qtpy.compat import getopenfilenames, getsavefilename
from qtpy.QtCore import Qt, Signal, Slot
from qtpy.QtGui import QCursor
Expand Down Expand Up @@ -55,8 +56,11 @@ class NamespaceBrowser(QWidget, SpyderWidgetMixin):
sig_hide_finder_requested = Signal()

def __init__(self, parent):
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)
if PYQT5:
super().__init__(parent=parent, class_parent=parent)
else:
QWidget.__init__(self, parent)
SpyderWidgetMixin.__init__(self, class_parent=parent)

# Attributes
self.filename = None
Expand Down
4 changes: 2 additions & 2 deletions spyder/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def check_path():

def check_qt():
"""Check Qt binding requirements"""
qt_infos = dict(pyqt5=("PyQt5", "5.6"), pyside2=("PySide2", "5.6"))
qt_infos = dict(pyqt5=("PyQt5", "5.6"), pyside2=("PySide2", "5.6"))
try:
import qtpy
package_name, required_ver = qt_infos[qtpy.API]
actual_ver = qtpy.QT_VERSION
actual_ver = qtpy.QT_VERSION
if parse_version(actual_ver) < parse_version(required_ver):
show_warning("Please check Spyder installation requirements:\n"
"%s %s+ is required (found v%s)."
Expand Down
Loading