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.""" """Extension of QVector3D which adds a few helpful methods."""
def __init__(self, *args): 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 len(args) == 1:
if isinstance(args[0], QtCore.QSizeF): if isinstance(args[0], QtCore.QSizeF):
x = float(args[0].width()) initArgs = (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()) initArgs = (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 initArgs = 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 initArgs = (args[0], args[1], 0)
z = 0 QtGui.QVector3D.__init__(self, *initArgs)
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