Vector.__init__ improvements

* added docstring
* fixed handling of QVector3D args (cannot list() them)
* refactor to no longer need return statements
This commit is contained in:
Martin 2020-08-05 11:31:51 -07:00
parent c8fc221e81
commit 9f996d041c

View File

@ -12,35 +12,36 @@ class Vector(QtGui.QVector3D):
"""Extension of QVector3D which adds a few helpful methods.""" """Extension of QVector3D which adds a few helpful methods."""
def __init__(self, *args): def __init__(self, *args):
"""
Handle additional constructions of a Vector
Parameters
----------
args
Could be any of:
* 3 numerics
* 2 numerics (`0` assumed for z)
* Either of the previous in a list-like collection
* 1 QSizeF (`0` assumed for z)
* 1 QPointF (`0` assumed for z)
* Any other valid QVector3D init args.
"""
init_args = args
if len(args) == 1: if len(args) == 1:
if isinstance(args[0], QtCore.QSizeF): if isinstance(args[0], QtCore.QSizeF):
x = float(args[0].width()) init_args = (float(args[0].width()), float(args[0].height()), 0)
y = float(args[0].height())
z = 0
elif isinstance(args[0], QtCore.QPoint) or isinstance(args[0], QtCore.QPointF): elif isinstance(args[0], QtCore.QPoint) or isinstance(args[0], QtCore.QPointF):
x = float(args[0].x()) init_args = (float(args[0].x()), float(args[0].y()), 0)
y = float(args[0].y()) elif hasattr(args[0], '__getitem__') and not isinstance(args[0], QtGui.QVector3D):
z = 0
elif isinstance(args[0], QtGui.QVector3D):
x = args[0].x()
y = args[0].y()
z = args[0].z()
elif hasattr(args[0], '__getitem__'):
vals = list(args[0]) vals = list(args[0])
if len(vals) == 2: if len(vals) == 2:
x, y = vals vals.append(0)
z = 0 if len(vals) != 3:
elif len(vals) == 3: raise Exception('Cannot init Vector with sequence of length %d' % len(args[0]))
x, y, z = vals init_args = vals
else:
raise ValueError('Cannot init Vector with sequence of length %d' % len(args[0]))
elif len(args) == 2: elif len(args) == 2:
x, y = args init_args = (args[0], args[1], 0)
z = 0 QtGui.QVector3D.__init__(self, *init_args)
else:
x, y, z = args # Could raise ValueError
QtGui.QVector3D.__init__(self, x, y, z)
def __len__(self): def __len__(self):
return 3 return 3