Coverage for C:\Program Files\QGIS 3.10\apps\qgis-ltr\python\qgis\core\additions\metaenum.py: 17%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-10 14:40 +0900

1# -*- coding: utf-8 -*- 

2 

3""" 

4*************************************************************************** 

5 metaenum.py 

6 --------------------- 

7 Date : May 2018 

8 Copyright : (C) 2018 by Denis Rouzaud 

9 Email : denis@opengis.ch 

10*************************************************************************** 

11* * 

12* This program is free software; you can redistribute it and/or modify * 

13* it under the terms of the GNU General Public License as published by * 

14* the Free Software Foundation; either version 2 of the License, or * 

15* (at your option) any later version. * 

16* * 

17*************************************************************************** 

18""" 

19 

20META_OBJECT_BY_ENUM_CLASS = {} 

21META_ENUM_BY_ENUM_CLASS = {} 

22 

23 

24def metaEnumFromValue(enumValue, baseClass=None, raiseException=True): 

25 """ 

26 Returns the QMetaEnum for an enum value. 

27 The enum must have declared using the Q_ENUM macro 

28 :param enumValue: the enum value 

29 :param baseClass: the enum base class. If not given, it will try to get it by using `enumValue.__class__.baseClass` 

30 :param raiseException: if False, no exception will be raised and None will be return in case of failure 

31 :return: the QMetaEnum if it succeeds, None otherwise 

32 """ 

33 return metaEnumFromType(enumValue.__class__, baseClass, raiseException) 

34 

35 

36def metaEnumFromType(enumClass, baseClass=None, raiseException=True): 

37 """ 

38 Returns the QMetaEnum for an enum type. 

39 The enum must have declared using the Q_ENUM macro 

40 :param enumClass: the enum class 

41 :param baseClass: the enum base class. If not given, it will try to get it by using `enumValue.__class__.baseClass` 

42 :param raiseException: if False, no exception will be raised and None will be return in case of failure 

43 :return: the QMetaEnum if it succeeds, None otherwise 

44 """ 

45 global META_OBJECT_BY_ENUM_CLASS 

46 global META_ENUM_BY_ENUM_CLASS 

47 if enumClass in META_ENUM_BY_ENUM_CLASS: 

48 return META_ENUM_BY_ENUM_CLASS[enumClass] 

49 

50 if enumClass == int: 

51 if raiseException: 

52 raise TypeError("enumClass is an int, while it should be an enum") 

53 else: 

54 return None 

55 

56 if baseClass is None: 

57 try: 

58 baseClass = enumClass.baseClass 

59 return metaEnumFromType(enumClass, baseClass, raiseException) 

60 except AttributeError: 

61 if raiseException: 

62 raise ValueError("Enum type does not implement baseClass method. Provide the base class as argument.") 

63 

64 try: 

65 meta_object = baseClass.staticMetaObject 

66 META_OBJECT_BY_ENUM_CLASS[enumClass] = meta_object 

67 idx = meta_object.indexOfEnumerator(enumClass.__name__) 

68 meta_enum = meta_object.enumerator(idx) 

69 META_ENUM_BY_ENUM_CLASS[enumClass] = meta_enum 

70 except AttributeError: 

71 if raiseException: 

72 raise TypeError("could not get the metaEnum for {}".format(enumClass.__name__)) 

73 meta_enum = None 

74 

75 return meta_enum