PythonQt
Classes | Macros | Typedefs | Functions
PythonQtConversion.h File Reference
#include "PythonQtPythonInclude.h"
#include "PythonQt.h"
#include "PythonQtMisc.h"
#include "PythonQtClassInfo.h"
#include "PythonQtMethodInfo.h"
#include <QList>
#include <vector>

Go to the source code of this file.

Classes

class  PythonQtConv
 a static class that offers methods for type conversion More...
 

Macros

#define PythonQtRegisterListTemplateConverter(type, innertype)
 
#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype)
 
#define PythonQtRegisterQPairConverter(type1, type2)
 
#define PythonQtRegisterIntegerMapConverter(type, innertype)
 
#define PythonQtRegisterListTemplateQPairConverter(listtype, type1, type2)
 
#define PythonQtRegisterToolClassesTemplateConverter(innertype)
 
#define PythonQtRegisterToolClassesTemplateConverterForKnownClass(innertype)
 

Typedefs

typedef PyObjectPythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)
 
typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)
 
typedef QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)
 

Functions

template<class ListType , class T >
PyObjectPythonQtConvertListOfValueTypeToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class ListType , class T >
PyObjectPythonQtConvertListOfKnownClassToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class T1 , class T2 >
PyObjectPythonQtConvertPairToPython (const void *inPair, int metaTypeId)
 
template<class T1 , class T2 >
bool PythonQtConvertPythonToPair (PyObject *obj, void *outPair, int metaTypeId, bool)
 
template<class ListType , class T1 , class T2 >
PyObjectPythonQtConvertListOfPairToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class MapType , class T >
PyObjectPythonQtConvertIntegerMapToPython (const void *inMap, int metaTypeId)
 
template<class MapType , class T >
bool PythonQtConvertPythonToIntegerMap (PyObject *val, void *outMap, int metaTypeId, bool)
 

Detailed Description

Author
Florian Link
Last changed by
Author
florian
Date
2006-05

Definition in file PythonQtConversion.h.

Macro Definition Documentation

◆ PythonQtRegisterIntegerMapConverter

#define PythonQtRegisterIntegerMapConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<int, innertype > >(#type"<int, "#innertype">"); \
}
PyObject * PythonQtConvertIntegerMapToPython(const void *inMap, int metaTypeId)
bool PythonQtConvertPythonToIntegerMap(PyObject *val, void *outMap, int metaTypeId, bool)
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype

Definition at line 76 of file PythonQtConversion.h.

◆ PythonQtRegisterListTemplateConverter

#define PythonQtRegisterListTemplateConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
}
PyObject * PythonQtConvertListOfValueTypeToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfValueType(PyObject *obj, void *outList, int metaTypeId, bool)

Definition at line 58 of file PythonQtConversion.h.

◆ PythonQtRegisterListTemplateConverterForKnownClass

#define PythonQtRegisterListTemplateConverterForKnownClass (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
}
PyObject * PythonQtConvertListOfKnownClassToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfKnownClass(PyObject *obj, void *outList, int metaTypeId, bool)

Definition at line 64 of file PythonQtConversion.h.

◆ PythonQtRegisterListTemplateQPairConverter

#define PythonQtRegisterListTemplateQPairConverter (   listtype,
  type1,
  type2 
)
Value:
{ \
qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
int typeId = qRegisterMetaType<listtype<QPair<type1, type2> > >(#listtype"<QPair<"#type1","#type2">>"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfPair<listtype<QPair<type1, type2> >, type1, type2>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfPairToPythonList<listtype<QPair<type1, type2> >, type1, type2>); \
}
bool PythonQtConvertPythonListToListOfPair(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfPairToPythonList(const void *inList, int metaTypeId)

Definition at line 82 of file PythonQtConversion.h.

◆ PythonQtRegisterQPairConverter

#define PythonQtRegisterQPairConverter (   type1,
  type2 
)
Value:
{ int typeId = qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToPair<type1, type2>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertPairToPython<type1, type2>); \
}

Definition at line 70 of file PythonQtConversion.h.

◆ PythonQtRegisterToolClassesTemplateConverter

#define PythonQtRegisterToolClassesTemplateConverter (   innertype)
Value:
PythonQtRegisterListTemplateConverter(QVector, innertype); \
PythonQtRegisterListTemplateConverter(std::vector, innertype);
#define PythonQtRegisterListTemplateConverter(type, innertype)

Definition at line 90 of file PythonQtConversion.h.

◆ PythonQtRegisterToolClassesTemplateConverterForKnownClass

#define PythonQtRegisterToolClassesTemplateConverterForKnownClass (   innertype)
Value:
PythonQtRegisterListTemplateConverterForKnownClass(QVector, innertype); \
PythonQtRegisterListTemplateConverterForKnownClass(std::vector, innertype);
#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype)

Definition at line 95 of file PythonQtConversion.h.

Typedef Documentation

◆ PythonQtConvertMetaTypeToPythonCB

typedef PyObject* PythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)

Definition at line 54 of file PythonQtConversion.h.

◆ PythonQtConvertPythonSequenceToQVariantListCB

typedef QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)

Definition at line 56 of file PythonQtConversion.h.

◆ PythonQtConvertPythonToMetaTypeCB

typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)

Definition at line 55 of file PythonQtConversion.h.

Function Documentation

◆ PythonQtConvertIntegerMapToPython()

template<class MapType , class T >
PyObject* PythonQtConvertIntegerMapToPython ( const void *  inMap,
int  metaTypeId 
)

Definition at line 468 of file PythonQtConversion.h.

469 {
470  MapType* map = (MapType*)inMap;
471  static int innerType = -1;
472  if (innerType == -1) {
473  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
474  QList<QByteArray> names = innerTypes.split(',');
475  innerType = QMetaType::type(names.at(1).trimmed());
476  }
477  if (innerType == QVariant::Invalid) {
478  std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
479  }
480 
481  PyObject* result = PyDict_New();
482  typename MapType::const_iterator t = map->constBegin();
483  PyObject* key;
484  PyObject* val;
485  for (; t != map->constEnd(); t++) {
486  key = PyInt_FromLong(t.key());
487  val = PythonQtConv::convertQtValueToPythonInternal(innerType, &t.value());
488  PyDict_SetItem(result, key, val);
489  Py_DECREF(key);
490  Py_DECREF(val);
491  }
492  return result;
493 }
struct _object PyObject
static PyObject * convertQtValueToPythonInternal(int type, const void *data)
converts the Qt parameter given in data, interpreting it as a type registered qvariant/meta type,...
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject<InnerType>
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateTypeName(), and PythonQtUtils::typeName().

◆ PythonQtConvertListOfKnownClassToPythonList()

template<class ListType , class T >
PyObject* PythonQtConvertListOfKnownClassToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 287 of file PythonQtConversion.h.

288 {
289  ListType* list = (ListType*)inList;
291  if (innerType == nullptr) {
292  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type for " << QMetaType::typeName(metaTypeId) << std::endl;
293  }
294  PyObject* result = PyTuple_New(list->size());
295  int i = 0;
296  Q_FOREACH(const T& value, *list) {
297  T* newObject = new T(value);
299  wrap->_ownedByPythonQt = true;
300  PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
301  i++;
302  }
303  return result;
304 }
a class that stores all required information about a Qt object (and an optional associated C++ class ...
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
static QByteArray getInnerListTypeName(const QByteArray &typeName)
returns the inner type name of a simple template or the typename without appended "List".
PyObject * wrapPtr(void *ptr, const QByteArray &name, bool passOwnership=false)
PythonQtClassInfo * getClassInfo(const QMetaObject *meta)
get the class info for a meta object (if available)
static PythonQtPrivate * priv()
get access to internal data (should not be used on the public API, but is used by some C functions)
Definition: PythonQt.h:557
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...
bool _ownedByPythonQt
flag that stores if the object is owned by pythonQt

References PythonQtInstanceWrapper::_ownedByPythonQt, PythonQtClassInfo::className(), PythonQtPrivate::getClassInfo(), PythonQtMethodInfo::getInnerListTypeName(), PythonQt::priv(), PythonQtUtils::typeName(), and PythonQtPrivate::wrapPtr().

◆ PythonQtConvertListOfPairToPythonList()

template<class ListType , class T1 , class T2 >
PyObject* PythonQtConvertListOfPairToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 416 of file PythonQtConversion.h.

417 {
418  ListType* list = (ListType*)inList;
419  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
420  if (innerType == QVariant::Invalid) {
421  std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
422  }
423  PyObject* result = PyTuple_New(list->size());
424  int i = 0;
425  typedef const QPair<T1, T2> Pair;
426  Q_FOREACH(Pair& value, *list) {
427  PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
428  PyTuple_SET_ITEM(result, i, object);
429  i++;
430  }
431  return result;
432 }
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject<InnerType>

References PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

◆ PythonQtConvertListOfValueTypeToPythonList()

template<class ListType , class T >
PyObject* PythonQtConvertListOfValueTypeToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 237 of file PythonQtConversion.h.

238 {
239  ListType* list = (ListType*)inList;
240  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
241  if (innerType == QVariant::Invalid) {
242  std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
243  }
244  PyObject* result = PyTuple_New(list->size());
245  int i = 0;
246  Q_FOREACH (const T& value, *list) {
247  PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
248  i++;
249  }
250  return result;
251 }

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

◆ PythonQtConvertPairToPython()

template<class T1 , class T2 >
PyObject* PythonQtConvertPairToPython ( const void *  inPair,
int  metaTypeId 
)

Definition at line 347 of file PythonQtConversion.h.

348 {
349  QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
350  static int innerType1 = -1;
351  static int innerType2 = -1;
352  if (innerType1==-1) {
353  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
354  QList<QByteArray> names = innerTypes.split(',');
355  innerType1 = QMetaType::type(names.at(0).trimmed());
356  innerType2 = QMetaType::type(names.at(1).trimmed());
357  }
358  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
359  std::cerr << "PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
360  }
361  PyObject* result = PyTuple_New(2);
362  PyTuple_SET_ITEM(result, 0, PythonQtConv::convertQtValueToPythonInternal(innerType1, &pair->first));
363  PyTuple_SET_ITEM(result, 1, PythonQtConv::convertQtValueToPythonInternal(innerType2, &pair->second));
364  return result;
365 }

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateTypeName(), and PythonQtUtils::typeName().

◆ PythonQtConvertPythonListToListOfKnownClass()

template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 307 of file PythonQtConversion.h.

308 {
309  ListType* list = (ListType*)outList;
311  if (innerType == nullptr) {
312  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type for " << QMetaType::typeName(metaTypeId) << std::endl;
313  }
314  bool result = false;
315  if (PySequence_Check(obj)) {
316  int count = PySequence_Size(obj);
317  if (count >= 0) {
318  result = true;
319  PyObject* value;
320  for (int i = 0; i < count; i++) {
321  value = PySequence_GetItem(obj, i);
322  if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
324  bool ok;
325  T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
326  Py_XDECREF(value);
327  if (ok) {
328  list->push_back(*object);
329  } else {
330  result = false;
331  break;
332  }
333  } else {
334  Py_XDECREF(value);
335  result = false;
336  break;
337  }
338  }
339  }
340  }
341  return result;
342 }
PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type
static void * castWrapperTo(PythonQtInstanceWrapper *wrapper, const QByteArray &className, bool &ok)
cast wrapper to given className if possible

References PythonQtConv::castWrapperTo(), PythonQtClassInfo::className(), PythonQtPrivate::getClassInfo(), PythonQtMethodInfo::getInnerListTypeName(), PythonQt::priv(), PythonQtInstanceWrapper_Type, and PythonQtUtils::typeName().

◆ PythonQtConvertPythonListToListOfPair()

template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 435 of file PythonQtConversion.h.

436 {
437  ListType* list = (ListType*)outList;
438  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
439  if (innerType == QVariant::Invalid) {
440  std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
441  }
442  bool result = false;
443  if (PySequence_Check(obj)) {
444  int count = PySequence_Size(obj);
445  if (count >= 0) {
446  result = true;
447  PyObject* value;
448  for (int i = 0; i < count; i++) {
449  QPair<T1, T2> pair;
450  value = PySequence_GetItem(obj, i);
451  if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType, false)) {
452  Py_XDECREF(value);
453  list->push_back(pair);
454  } else {
455  Py_XDECREF(value);
456  result = false;
457  break;
458  }
459  }
460  }
461  }
462  return result;
463 }

References PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

◆ PythonQtConvertPythonListToListOfValueType()

template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 254 of file PythonQtConversion.h.

255 {
256  ListType* list = (ListType*)outList;
257  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
258  if (innerType == QVariant::Invalid) {
259  std::cerr << "PythonQtConvertPythonListToListOfValueType: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
260  }
261  bool result = false;
262  if (PySequence_Check(obj)) {
263  int count = PySequence_Size(obj);
264  if (count >= 0) {
265  result = true;
266  PyObject* value;
267  for (int i = 0;i<count;i++) {
268  value = PySequence_GetItem(obj,i);
269  // this is quite some overhead, but it avoids having another large switch...
270  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
271  Py_XDECREF(value);
272  if (v.isValid()) {
273  list->push_back(qvariant_cast<T>(v));
274  } else {
275  result = false;
276  break;
277  }
278  }
279  }
280  }
281  return result;
282 }
static QVariant PyObjToQVariant(PyObject *val, int type=-1)

References PythonQtMethodInfo::getInnerTemplateMetaType(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().

◆ PythonQtConvertPythonToIntegerMap()

template<class MapType , class T >
bool PythonQtConvertPythonToIntegerMap ( PyObject val,
void *  outMap,
int  metaTypeId,
bool   
)

Definition at line 496 of file PythonQtConversion.h.

497 {
498  MapType* map = (MapType*)outMap;
499  static int innerType = -1;
500  if (innerType == -1) {
501  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
502  QList<QByteArray> names = innerTypes.split(',');
503  innerType = QMetaType::type(names.at(1).trimmed());
504  }
505  if (innerType == QVariant::Invalid) {
506  std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
507  }
508  bool result = false;
509  if (PyMapping_Check(val)) {
510  result = true;
511  PyObject* items = PyMapping_Items(val);
512  if (items) {
513  int count = PyList_Size(items);
514  PyObject* value;
515  PyObject* key;
516  PyObject* tuple;
517  for (int i = 0; i < count; i++) {
518  tuple = PyList_GetItem(items, i);
519  key = PyTuple_GetItem(tuple, 0);
520  value = PyTuple_GetItem(tuple, 1);
521 
522  bool ok;
523  int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
524  // this is quite some overhead, but it avoids having another large switch...
525  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
526  if (v.isValid() && ok) {
527  map->insert(intKey, qvariant_cast<T>(v));
528  } else {
529  result = false;
530  break;
531  }
532  }
533  Py_DECREF(items);
534  }
535  }
536  return result;
537 }
static int PyObjGetInt(PyObject *val, bool strict, bool &ok)
get int from py object

References PythonQtMethodInfo::getInnerTemplateTypeName(), PythonQtConv::PyObjGetInt(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().

◆ PythonQtConvertPythonToPair()

template<class T1 , class T2 >
bool PythonQtConvertPythonToPair ( PyObject obj,
void *  outPair,
int  metaTypeId,
bool   
)

Definition at line 368 of file PythonQtConversion.h.

369 {
370  QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
371  static int innerType1 = -1;
372  static int innerType2 = -1;
373  if (innerType1 == -1) {
374  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
375  QList<QByteArray> names = innerTypes.split(',');
376  innerType1 = QMetaType::type(names.at(0).trimmed());
377  innerType2 = QMetaType::type(names.at(1).trimmed());
378  }
379  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
380  std::cerr << "PythonQtConvertPythonToPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
381  }
382  bool result = false;
383  if (PySequence_Check(obj)) {
384  int count = PySequence_Size(obj);
385  if (count == 2) {
386  result = true;
387  PyObject* value;
388 
389  value = PySequence_GetItem(obj, 0);
390  // this is quite some overhead, but it avoids having another large switch...
391  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType1);
392  Py_XDECREF(value);
393  if (v.isValid()) {
394  pair->first = qvariant_cast<T1>(v);
395  } else {
396  return false;
397  }
398 
399  value = PySequence_GetItem(obj, 1);
400  // this is quite some overhead, but it avoids having another large switch...
401  v = PythonQtConv::PyObjToQVariant(value, innerType2);
402  Py_XDECREF(value);
403  if (v.isValid()) {
404  pair->second = qvariant_cast<T2>(v);
405  } else {
406  return false;
407  }
408  }
409  }
410  return result;
411 }

References PythonQtMethodInfo::getInnerTemplateTypeName(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().