Krita Source Code Documentation
Loading...
Searching...
No Matches
mikro Namespace Reference

Classes

class  Error
 
class  PyQtClass
 
class  PyQtMethod
 
class  PyQtProperty
 

Functions

 convert_value (value)
 
 create_pyqt_class (metaobject)
 
 create_pyqt_object (obj)
 
 from_variant (variant)
 
 is_qobject (obj)
 
 is_scripter_child (qobj)
 
 unwrap (obj)
 
 wrap (obj, force=False)
 
 wrap_variant_object (variant)
 

Variables

dict pyqt_classes = {}
 
dict qtclasses = {}
 
dict variant_converter
 

Detailed Description

SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>

SPDX-License-Identifier: LGPL-2.0-or-later

Function Documentation

◆ convert_value()

mikro.convert_value ( value)
Convert a given value, upcasting to the highest QObject-based class if possible,
unpacking lists and dicts.

Definition at line 92 of file mikro.py.

92def convert_value(value):
93 """
94 Convert a given value, upcasting to the highest QObject-based class if possible,
95 unpacking lists and dicts.
96 """
97
98 # Check whether it's a dict: if so, convert the keys/values
99 if hasattr(value, '__class__') and issubclass(value.__class__, dict) and len(value) > 0:
100 return {convert_value(k): convert_value(v) for k, v in value.items()}
101
102 # Check whether it's a list: if so, convert the values
103 if hasattr(value, '__class__') and issubclass(value.__class__, list) and len(value) > 0:
104 return [convert_value(v) for v in value]
105
106 if isinstance(value, str):
107 # prefer Python strings
108 return str(value)
109
110 elif isinstance(value, PyQtClass):
111 # already wrapped
112 return value
113
114 # Check whether it's a QObject
115 if hasattr(value, '__class__') and issubclass(value.__class__, QObject):
116 return wrap(value, True)
117
118 if hasattr(value, '__type__') and not (value is None or value.type() is None):
119 return from_variant(value)
120
121 return value
122

References convert_value(), from_variant(), and wrap().

◆ create_pyqt_class()

mikro.create_pyqt_class ( metaobject)

Definition at line 356 of file mikro.py.

356def create_pyqt_class(metaobject):
357 class_name = str(metaobject.className())
358 cls = pyqt_classes.get(class_name)
359 if cls:
360 return cls
361 attrs = {}
362
363 properties = attrs["__properties__"] = {}
364 for i in range(metaobject.propertyCount()):
365 prop = PyQtProperty(metaobject.property(i))
366 prop_name = str(prop.name)
367 if prop.read_only:
368 properties[prop_name] = attrs[prop_name] = property(prop.get, doc=prop.__doc__)
369 else:
370 properties[prop_name] = attrs[prop_name] = property(
371 prop.get, prop.set, doc=prop.__doc__)
372
373 methods = attrs["__methods__"] = {}
374 signals = attrs["__signals__"] = {}
375 for i in range(metaobject.methodCount()):
376 meta_method = metaobject.method(i)
377 if meta_method.methodType() != QMetaMethod.MethodType.Signal:
378 method = PyQtMethod(meta_method)
379 method_name = method.name
380 if method_name in attrs:
381 # There is already a property with this name
382 # So append an underscore
383 method_name += "_"
384 instance_method = method.instancemethod()
385 instance_method.__doc__ = method.__doc__
386 methods[method_name] = attrs[method_name] = instance_method
387 else:
388 method_name = meta_method.name()
389 signal_attrs = []
390 properties[bytes(method_name).decode('ascii')] = pyqtSignal(meta_method.parameterTypes())
391
392 # Dynamically create a class with a base class and a dictionary
393 cls = type(class_name, (PyQtClass,), attrs)
394 pyqt_classes[class_name] = cls
395 return cls
396
397

◆ create_pyqt_object()

mikro.create_pyqt_object ( obj)
 Wrap a QObject and make all slots and properties dynamically available.
 @type obj:  QObject
 @param obj: an unwrapped QObject
 @rtype:     PyQtClass object
 @return:    dynamically created object with all available properties and slots

 This is probably the only function you need from this module.
 Everything else are helper functions and classes.

Definition at line 398 of file mikro.py.

398def create_pyqt_object(obj):
399 """
400 Wrap a QObject and make all slots and properties dynamically available.
401 @type obj: QObject
402 @param obj: an unwrapped QObject
403 @rtype: PyQtClass object
404 @return: dynamically created object with all available properties and slots
405
406 This is probably the only function you need from this module.
407 Everything else are helper functions and classes.
408 """
409 cls = create_pyqt_class(obj.metaObject())
410 return cls(obj)

References create_pyqt_class().

◆ from_variant()

mikro.from_variant ( variant)
convert a QVariant to a Python value

Definition at line 74 of file mikro.py.

74def from_variant(variant):
75 """
76 convert a QVariant to a Python value
77 """
78 # Check whether it's really a QVariant
79 if hasattr(variant, '__type__') and not (variant is None or variant.type() is None):
80 typeName = variant.typeName()
81 convert = variant_converter.get(typeName)
82 if not convert:
83 raise ValueError("Could not convert value to %s" % typeName)
84 else:
85 v = convert(variant)
86 return v
87
88 # Give up and return
89 return variant
90
91

◆ is_qobject()

mikro.is_qobject ( obj)
checks if class or wrapped class is a subclass of QObject

Definition at line 158 of file mikro.py.

158def is_qobject(obj):
159 """
160 checks if class or wrapped class is a subclass of QObject
161 """
162 if hasattr(obj, "__bases__") and issubclass(unwrap(obj), QObject):
163 return True
164 else:
165 return False
166
167

References unwrap().

◆ is_scripter_child()

mikro.is_scripter_child ( qobj)
walk up the object tree until Scripter or the root is found

Definition at line 168 of file mikro.py.

168def is_scripter_child(qobj):
169 """
170 walk up the object tree until Scripter or the root is found
171 """
172 found = False
173 p = qobj.parent()
174 while p and not found:
175 if str(p.objectName()) == "Krita":
176 found = True
177 break
178 else:
179 p = p.parent()
180 return found
181
182

◆ unwrap()

mikro.unwrap ( obj)
if wrapped returns the wrapped object

Definition at line 149 of file mikro.py.

149def unwrap(obj):
150 """
151 if wrapped returns the wrapped object
152 """
153 if hasattr(obj, "qt"):
154 obj = obj.qt
155 return obj
156
157

◆ wrap()

mikro.wrap ( obj,
force = False )
If a class is not known by PyQt it will be automatically
casted to a known wrapped super class.

But that limits access to methods and properties of this super class.
So instead this functions returns a wrapper class (PyQtClass)
which queries the metaObject and provides access to
all slots and all properties.

Definition at line 126 of file mikro.py.

126def wrap(obj, force=False):
127 """
128 If a class is not known by PyQt it will be automatically
129 casted to a known wrapped super class.
130
131 But that limits access to methods and properties of this super class.
132 So instead this functions returns a wrapper class (PyQtClass)
133 which queries the metaObject and provides access to
134 all slots and all properties.
135 """
136 if isinstance(obj, str):
137 # prefer Python strings
138 return str(obj)
139 elif isinstance(obj, PyQtClass):
140 # already wrapped
141 return obj
142 elif obj and isinstance(obj, QObject):
143 if force or obj.__class__.__name__ != obj.metaObject().className():
144 # Ah this is an unwrapped class
145 obj = create_pyqt_object(obj)
146 return obj
147
148

References create_pyqt_object().

◆ wrap_variant_object()

mikro.wrap_variant_object ( variant)
convert a QObject or a QWidget to its wrapped superclass

Definition at line 66 of file mikro.py.

66def wrap_variant_object(variant):
67 """
68 convert a QObject or a QWidget to its wrapped superclass
69 """
70 o = Krita.fromVariant(variant)
71 return wrap(o, True)
72
73
static QObject * fromVariant(const QVariant &v)
Definition Krita.cpp:406

References Krita.fromVariant(), and wrap().

Variable Documentation

◆ pyqt_classes

dict mikro.pyqt_classes = {}

Definition at line 353 of file mikro.py.

◆ qtclasses

dict mikro.qtclasses = {}

Definition at line 123 of file mikro.py.

◆ variant_converter

dict mikro.variant_converter
Initial value:
1= {
2 "QVariantList": lambda v: v.toList(v),
3 "QVariantMap": lambda v: toPyObject(v),
4 "QPoint": lambda v: v.toPoint(),
5 "str": lambda v: v.toString(),
6 "int": lambda v: v.toInt()[0],
7 "double": lambda v: v.toDouble()[0],
8 "char": lambda v: v.toChar(),
9 "QByteArray": lambda v: v.toByteArray(),
10 "QPoint": lambda v: v.toPoint(),
11 "QPointF": lambda v: v.toPointF(),
12 "QSize": lambda v: v.toSize(),
13 "QLine": lambda v: v.toLine(),
14 "QStringList": lambda v: v.toStringList(),
15 "QTime": lambda v: v.toTime(),
16 "QDateTime": lambda v: v.toDateTime(),
17 "QDate": lambda v: v.toDate(),
18 "QLocale": lambda v: v.toLocale(),
19 "QUrl": lambda v: v.toUrl(),
20 "QRect": lambda v: v.toRect(),
21 "QBrush": lambda v: QBrush(v),
22 "QFont": lambda v: QFont(v),
23 "QPalette": lambda v: QPalette(v),
24 "QPixmap": lambda v: QPixmap(v),
25 "QImage": lambda v: QImage(v),
26 "bool": lambda v: v.toBool(),
27 "QObject*": lambda v: wrap_variant_object(v),
28 "QWidget*": lambda v: wrap_variant_object(v),
29 "ActionMap": lambda v: int(v.count())
30}

Definition at line 34 of file mikro.py.