Skip to content

Commit

Permalink
Merge pull request #114 from minrk/superq
Browse files Browse the repository at this point in the history
Make SuperQObject logic reusable with other Qt bases
  • Loading branch information
minrk committed Mar 22, 2016
2 parents 62fc55d + 114b69c commit 513d739
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
9 changes: 5 additions & 4 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from traitlets.config.configurable import LoggingConfigurable
from qtconsole.rich_text import HtmlExporter
from qtconsole.util import MetaQObjectHasTraits, get_font
from qtconsole.util import MetaQObjectHasTraits, get_font, superQ
from ipython_genutils.text import columnize
from traitlets import Bool, Enum, Integer, Unicode
from .ansi_code_processor import QtAnsiCodeProcessor
Expand All @@ -35,7 +35,7 @@ def is_letter_or_number(char):
# Classes
#-----------------------------------------------------------------------------

class ConsoleWidget(MetaQObjectHasTraits('NewBase', (LoggingConfigurable, QtGui.QWidget), {})):
class ConsoleWidget(MetaQObjectHasTraits('NewBase', (LoggingConfigurable, superQ(QtGui.QWidget)), {})):
""" An abstract base class for console-type widgets. This class has
functionality for:
Expand Down Expand Up @@ -207,8 +207,9 @@ def __init__(self, parent=None, **kw):
parent : QWidget, optional [default None]
The parent for this widget.
"""
QtGui.QWidget.__init__(self, parent)
LoggingConfigurable.__init__(self, **kw)
super(ConsoleWidget, self).__init__(**kw)
if parent:
self.setParent(parent)

# While scrolling the pager on Mac OS X, it tears badly. The
# NativeGesture is platform and perhaps build-specific hence
Expand Down
41 changes: 23 additions & 18 deletions qtconsole/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def __init__(mcls, name, bases, classdict):
# Classes
#-----------------------------------------------------------------------------

class SuperQObject(QtCore.QObject):
""" Permits the use of super() in class hierarchies that contain QObject.
def superQ(QClass):
""" Permits the use of super() in class hierarchies that contain Qt classes.
Unlike QObject, SuperQObject does not accept a QObject parent. If it did,
super could not be emulated properly (all other classes in the heierarchy
Expand All @@ -56,22 +56,27 @@ class SuperQObject(QtCore.QObject):
This class is primarily useful for attaching signals to existing non-Qt
classes. See QtKernelManagerMixin for an example.
"""

def __new__(cls, *args, **kw):
# We initialize QObject as early as possible. Without this, Qt complains
# if SuperQObject is not the first class in the super class list.
inst = QtCore.QObject.__new__(cls)
QtCore.QObject.__init__(inst)
return inst

def __init__(self, *args, **kw):
# Emulate super by calling the next method in the MRO, if there is one.
mro = self.__class__.mro()
for qt_class in QtCore.QObject.mro():
mro.remove(qt_class)
next_index = mro.index(SuperQObject) + 1
if next_index < len(mro):
mro[next_index].__init__(self, *args, **kw)
class SuperQClass(QClass):

def __new__(cls, *args, **kw):
# We initialize QClass as early as possible. Without this, Qt complains
# if SuperQClass is not the first class in the super class list.
inst = QClass.__new__(cls)
QClass.__init__(inst)
return inst

def __init__(self, *args, **kw):
# Emulate super by calling the next method in the MRO, if there is one.
mro = self.__class__.mro()
for qt_class in QClass.mro():
mro.remove(qt_class)
next_index = mro.index(SuperQClass) + 1
if next_index < len(mro):
mro[next_index].__init__(self, *args, **kw)

return SuperQClass

SuperQObject = superQ(QtCore.QObject)

#-----------------------------------------------------------------------------
# Functions
Expand Down

0 comments on commit 513d739

Please sign in to comment.