Skip to content

Commit

Permalink
Rough implementation for QtCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
j0yu committed Nov 18, 2020
1 parent 6ae1a6f commit 0fa580a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions Qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,16 +1813,21 @@ def _pyqt_enums_patch():
if not name.startswith("_")
}

known_enums = set()
for enum_class in qt_attrs.values():
known_enums = {}
for class_name, enum_class in qt_attrs.items():
if inspect.isclass(enum_class) and issubclass(enum_class, int):
known_enums.add(enum_class)
enum_class.values = {}
class EnumFlags(enum_class):
values = {}

known_enums[enum_class] = class_name
setattr(Qt.QtCompat, class_name, EnumFlags)

for attr_name, attr in qt_attrs.items():
if isinstance(attr, int) and attr.__class__ in known_enums:
attr.__class__.values[attr_name] = attr
setattr(attr.__class__, attr_name, attr)
class_name = known_enums.get(attr.__class__)
if isinstance(attr, int) and class_name is not None:
enum_class = getattr(Qt.QtCompat, class_name)
enum_class.values[attr_name] = attr
setattr(enum_class, attr_name, attr)


def _install():
Expand Down
8 changes: 4 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,16 +1162,16 @@ def test_multiple_preferred():

def test_pyqt_enums():
"""Check some known enums/flags mappings."""
from Qt import QtCore
from Qt import QtCompat

class_attributes = { # Version agnostic flags and enums
"CheckState": ["Checked", "Unchecked", "PartiallyChecked"],
"Orientation": ["Horizontal", "Vertical"],
}
for class_name, attributes in class_attributes.items():
qt_class = getattr(QtCore.Qt, class_name)
qt_class = getattr(QtCompat, class_name)

values_path = "QtCore.Qt.%s.%s" % (class_name, "values")
values_path = "QtCompat.%s.%s" % (class_name, "values")
assert hasattr(qt_class, "values"), (
"%s should exist, but is missing" % values_path)

Expand All @@ -1181,7 +1181,7 @@ def test_pyqt_enums():
% (values_path, cls_values))

for attr_name in attributes:
attr_path = "QtCore.Qt.%s.%s" % (class_name, attr_name)
attr_path = "QtCompat.%s.%s" % (class_name, attr_name)

assert hasattr(qt_class, attr_name), (
"%s should exist, but is missing" % attr_path)
Expand Down

0 comments on commit 0fa580a

Please sign in to comment.