SF.net SVN: fclient: [578] trunk/fclient/src/fclient/lib/qt4ex/qtools.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-07-11 16:35:08
|
Revision: 578
http://fclient.svn.sourceforge.net/fclient/?rev=578&view=rev
Author: jUrner
Date: 2008-07-11 09:35:17 -0700 (Fri, 11 Jul 2008)
Log Message:
-----------
new fuction castVariant() to tame QVariant a bit
Modified Paths:
--------------
trunk/fclient/src/fclient/lib/qt4ex/qtools.py
Modified: trunk/fclient/src/fclient/lib/qt4ex/qtools.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/qtools.py 2008-07-11 16:34:31 UTC (rev 577)
+++ trunk/fclient/src/fclient/lib/qt4ex/qtools.py 2008-07-11 16:35:17 UTC (rev 578)
@@ -56,10 +56,51 @@
#**********************************************************************
#
#**********************************************************************
+class _castVariant(object):
+ """for some reason PyQt does the right thing for int, long (...) variant type conversions returning (value, ok)
+ emulating it here for all other types as well"""
+ _pyqt_does_the_right_thing_ = (
+ QtCore.QVariant.Double,
+ QtCore.QVariant.Int,
+ QtCore.QVariant.LongLong,
+ QtCore.QVariant.UInt,
+ QtCore.QVariant.ULongLong,
+ )
+ #NOTE: if QVariant gets equipped with another enum we are f***d
+ _variant_type_mapping = {}
+ _reversed_variant_type_mapping = {}
+ for name in dir(QtCore.QVariant):
+ if name[0].isupper():
+ value = getattr(QtCore.QVariant, name)
+ if isinstance(value, int):
+ _variant_type_mapping[value] = name
+ _reversed_variant_type_mapping[name] = value
+ del name, value
+ @classmethod
+ def castVariant(clss, v, variantType):
+ """casts a variant to an object of the desired type
+ @param v: (QVariant) to convert
+ @param variantType: (QVariant.Type*) to convert to. alternatively the variantType can be a specified
+ as string corrosponding to QVariant.to* methods e.g. 'Bool', 'UInt'
+ @return: (tuple) object, ok
+ """
+ if isinstance(variantType, int):
+ szVariantType = clss._variant_type_mapping[variantType]
+ else:
+ szVariantType = variantType
+ variantType = clss._reversed_variant_type_mapping[variantType]
+ mthd = getattr(v, 'to' + szVariantType)
+ if variantType in clss._pyqt_does_the_right_thing_:
+ obj, ok = mthd()
+ else:
+ ok = v.convert(variantType)
+ obj = mthd()
+ return obj, ok
+castVariant =_castVariant.castVariant
+
-
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|