utils.debug: Add a broken qflags_key.

This commit is contained in:
Florian Bruhin 2014-08-07 14:41:39 +02:00
parent 318805a088
commit 837b2b386e
2 changed files with 81 additions and 0 deletions

View File

@ -73,6 +73,49 @@ class QEnumKeyTests(unittest.TestCase):
# FIXME maybe this should return the right thing anyways?
debug.qenum_key(Qt, Qt.Alignment(int(Qt.AlignLeft)))
class QFlagsKeyTests(unittest.TestCase):
@unittest.skip('FIXME')
def test_single(self):
"""Test with single value."""
flags = debug.qflags_key(Qt, Qt.AlignTop)
self.assertEqual(flags, 'AlignTop')
@unittest.skip('FIXME')
def test_multiple(self):
"""Test with multiple values."""
flags = debug.qflags_key(Qt, Qt.AlignLeft | Qt.AlignTop)
self.assertEqual(flags, 'AlignLeft|AlignTop')
def test_combined(self):
"""Test with a combined value."""
flags = debug.qflags_key(Qt, Qt.AlignCenter)
self.assertEqual(flags, 'AlignHCenter|AlignVCenter')
@unittest.skip('FIXME')
def test_add_base(self):
"""Test with add_base=True."""
flags = debug.qflags_key(Qt, Qt.AlignTop, add_base=True)
self.assertEqual(flags, 'Qt.AlignTop')
def test_int_noklass(self):
"""Test passing an int without explicit klass given."""
with self.assertRaises(TypeError):
debug.qflags_key(Qt, 42)
@unittest.skip('FIXME')
def test_int(self):
"""Test passing an int with explicit klass given."""
flags = debug.qflags_key(Qt, 0x0021, klass=Qt.Alignment)
self.assertEqual(flags, 'AlignLeft|AlignTop')
def test_unknown(self):
"""Test passing an unknown value."""
flags = debug.qflags_key(Qt, 0x1100, klass=Qt.Alignment)
self.assertEqual(flags, '0x0100|0x1000')
class TestDebug(unittest.TestCase):
"""Test signal debug output functions."""

View File

@ -167,6 +167,44 @@ def qenum_key(base, value, add_base=False, klass=None):
return ret
def qflags_key(base, value, add_base=False, klass=None):
"""Convert a Qt QFlags value to its keys as string.
Note: Passing a combined value (such as Qt.AlignCenter) will get the names
for the individual bits (e.g. Qt.AlignVCenter | Qt.AlignHCenter). FIXME
Args:
base: The object the flags are in, e.g. QtCore.Qt
value: The value to get.
add_base: Whether the base should be added to the printed names.
klass: The flags class the value belongs to.
If None, the class will be auto-guessed.
Return:
The keys associated with the flags as a '|' separated string if they
could be found. Hex values as a string if not.
"""
if klass is None:
# We have to store klass here because it will be lost when iterating
# over the bits.
klass = value.__class__
if klass == int:
raise TypeError("Can't guess enum class of an int!")
bits = []
names = []
mask = 0x01
value = int(value)
while mask < value:
if value & mask:
bits.append(mask)
mask <<= 1
for bit in bits:
# We have to re-convert to an enum type here or we'll sometimes get an
# empty string back.
names.append(qenum_key(base, klass(bit), add_base))
return '|'.join(names)
def signal_name(sig):
"""Get a cleaned up name of a signal.