util.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """ Defines miscellaneous Qt-related helper classes and functions.
  2. """
  3. import inspect
  4. from qtpy import QtCore, QtGui
  5. from ipython_genutils.py3compat import iteritems
  6. from traitlets import HasTraits, TraitType
  7. #-----------------------------------------------------------------------------
  8. # Metaclasses
  9. #-----------------------------------------------------------------------------
  10. MetaHasTraits = type(HasTraits)
  11. MetaQObject = type(QtCore.QObject)
  12. class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
  13. """ A metaclass that inherits from the metaclasses of HasTraits and QObject.
  14. Using this metaclass allows a class to inherit from both HasTraits and
  15. QObject. Using SuperQObject instead of QObject is highly recommended. See
  16. QtKernelManager for an example.
  17. """
  18. def __new__(mcls, name, bases, classdict):
  19. # FIXME: this duplicates the code from MetaHasTraits.
  20. # I don't think a super() call will help me here.
  21. for k,v in iteritems(classdict):
  22. if isinstance(v, TraitType):
  23. v.name = k
  24. elif inspect.isclass(v):
  25. if issubclass(v, TraitType):
  26. vinst = v()
  27. vinst.name = k
  28. classdict[k] = vinst
  29. cls = MetaQObject.__new__(mcls, name, bases, classdict)
  30. return cls
  31. def __init__(mcls, name, bases, classdict):
  32. # Note: super() did not work, so we explicitly call these.
  33. MetaQObject.__init__(mcls, name, bases, classdict)
  34. MetaHasTraits.__init__(mcls, name, bases, classdict)
  35. #-----------------------------------------------------------------------------
  36. # Classes
  37. #-----------------------------------------------------------------------------
  38. def superQ(QClass):
  39. """ Permits the use of super() in class hierarchies that contain Qt classes.
  40. Unlike QObject, SuperQObject does not accept a QObject parent. If it did,
  41. super could not be emulated properly (all other classes in the heierarchy
  42. would have to accept the parent argument--they don't, of course, because
  43. they don't inherit QObject.)
  44. This class is primarily useful for attaching signals to existing non-Qt
  45. classes. See QtKernelManagerMixin for an example.
  46. """
  47. class SuperQClass(QClass):
  48. def __new__(cls, *args, **kw):
  49. # We initialize QClass as early as possible. Without this, Qt complains
  50. # if SuperQClass is not the first class in the super class list.
  51. inst = QClass.__new__(cls)
  52. QClass.__init__(inst)
  53. return inst
  54. def __init__(self, *args, **kw):
  55. # Emulate super by calling the next method in the MRO, if there is one.
  56. mro = self.__class__.mro()
  57. for qt_class in QClass.mro():
  58. mro.remove(qt_class)
  59. next_index = mro.index(SuperQClass) + 1
  60. if next_index < len(mro):
  61. mro[next_index].__init__(self, *args, **kw)
  62. return SuperQClass
  63. SuperQObject = superQ(QtCore.QObject)
  64. #-----------------------------------------------------------------------------
  65. # Functions
  66. #-----------------------------------------------------------------------------
  67. def get_font(family, fallback=None):
  68. """Return a font of the requested family, using fallback as alternative.
  69. If a fallback is provided, it is used in case the requested family isn't
  70. found. If no fallback is given, no alternative is chosen and Qt's internal
  71. algorithms may automatically choose a fallback font.
  72. Parameters
  73. ----------
  74. family : str
  75. A font name.
  76. fallback : str
  77. A font name.
  78. Returns
  79. -------
  80. font : QFont object
  81. """
  82. font = QtGui.QFont(family)
  83. # Check whether we got what we wanted using QFontInfo, since exactMatch()
  84. # is overly strict and returns false in too many cases.
  85. font_info = QtGui.QFontInfo(font)
  86. if fallback is not None and font_info.family() != family:
  87. font = QtGui.QFont(fallback)
  88. return font