Merge pull request #1339 from outofculture/vector-init-qt5

Vector init in qt5
This commit is contained in:
Kenneth Lyons 2020-08-16 08:40:06 -07:00 committed by GitHub
commit 11b76a1a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,35 +12,37 @@ class Vector(QtGui.QVector3D):
"""Extension of QVector3D which adds a few helpful methods."""
def __init__(self, *args):
"""
Handle additional constructions of a Vector
============== ================================================================================================
**Arguments:**
*args* Could be any of:
* 3 numerics (x, y, and z)
* 2 numerics (x, y, and `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.
============== ================================================================================================
"""
initArgs = args
if len(args) == 1:
if isinstance(args[0], QtCore.QSizeF):
x = float(args[0].width())
y = float(args[0].height())
z = 0
initArgs = (float(args[0].width()), float(args[0].height()), 0)
elif isinstance(args[0], QtCore.QPoint) or isinstance(args[0], QtCore.QPointF):
x = float(args[0].x())
y = float(args[0].y())
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__'):
initArgs = (float(args[0].x()), float(args[0].y()), 0)
elif hasattr(args[0], '__getitem__') and not isinstance(args[0], QtGui.QVector3D):
vals = list(args[0])
if len(vals) == 2:
x, y = vals
z = 0
elif len(vals) == 3:
x, y, z = vals
else:
raise ValueError('Cannot init Vector with sequence of length %d' % len(args[0]))
vals.append(0)
if len(vals) != 3:
raise Exception('Cannot init Vector with sequence of length %d' % len(args[0]))
initArgs = vals
elif len(args) == 2:
x, y = args
z = 0
else:
x, y, z = args # Could raise ValueError
QtGui.QVector3D.__init__(self, x, y, z)
initArgs = (args[0], args[1], 0)
QtGui.QVector3D.__init__(self, *initArgs)
def __len__(self):
return 3