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 450 of file PythonQtConversion.h.

451 {
452  MapType* map = (MapType*)inMap;
453  static int innerType = -1;
454  if (innerType == -1) {
455  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
456  QList<QByteArray> names = innerTypes.split(',');
457  innerType = QMetaType::type(names.at(1).trimmed());
458  }
459  if (innerType == QVariant::Invalid) {
460  std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
461  }
462 
463  PyObject* result = PyDict_New();
464  typename MapType::const_iterator t = map->constBegin();
465  PyObject* key;
466  PyObject* val;
467  for (; t != map->constEnd(); t++) {
468  key = PyInt_FromLong(t.key());
469  val = PythonQtConv::convertQtValueToPythonInternal(innerType, &t.value());
470  PyDict_SetItem(result, key, val);
471  Py_DECREF(key);
472  Py_DECREF(val);
473  }
474  return result;
475 }
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 269 of file PythonQtConversion.h.

270 {
271  ListType* list = (ListType*)inList;
273  if (innerType == NULL) {
274  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
275  }
276  PyObject* result = PyTuple_New(list->size());
277  int i = 0;
278  Q_FOREACH(const T& value, *list) {
279  T* newObject = new T(value);
281  wrap->_ownedByPythonQt = true;
282  PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
283  i++;
284  }
285  return result;
286 }
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 398 of file PythonQtConversion.h.

399 {
400  ListType* list = (ListType*)inList;
401  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
402  if (innerType == QVariant::Invalid) {
403  std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
404  }
405  PyObject* result = PyTuple_New(list->size());
406  int i = 0;
407  typedef const QPair<T1, T2> Pair;
408  Q_FOREACH(Pair& value, *list) {
409  PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
410  PyTuple_SET_ITEM(result, i, object);
411  i++;
412  }
413  return result;
414 }
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 219 of file PythonQtConversion.h.

220 {
221  ListType* list = (ListType*)inList;
222  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
223  if (innerType == QVariant::Invalid) {
224  std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
225  }
226  PyObject* result = PyTuple_New(list->size());
227  int i = 0;
228  Q_FOREACH (const T& value, *list) {
229  PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
230  i++;
231  }
232  return result;
233 }

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

◆ PythonQtConvertPairToPython()

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

Definition at line 329 of file PythonQtConversion.h.

330 {
331  QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
332  static int innerType1 = -1;
333  static int innerType2 = -1;
334  if (innerType1==-1) {
335  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
336  QList<QByteArray> names = innerTypes.split(',');
337  innerType1 = QMetaType::type(names.at(0).trimmed());
338  innerType2 = QMetaType::type(names.at(1).trimmed());
339  }
340  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
341  std::cerr << "PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
342  }
343  PyObject* result = PyTuple_New(2);
344  PyTuple_SET_ITEM(result, 0, PythonQtConv::convertQtValueToPythonInternal(innerType1, &pair->first));
345  PyTuple_SET_ITEM(result, 1, PythonQtConv::convertQtValueToPythonInternal(innerType2, &pair->second));
346  return result;
347 }

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 289 of file PythonQtConversion.h.

290 {
291  ListType* list = (ListType*)outList;
293  if (innerType == NULL) {
294  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
295  }
296  bool result = false;
297  if (PySequence_Check(obj)) {
298  int count = PySequence_Size(obj);
299  if (count >= 0) {
300  result = true;
301  PyObject* value;
302  for (int i = 0; i < count; i++) {
303  value = PySequence_GetItem(obj, i);
304  if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
306  bool ok;
307  T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
308  Py_XDECREF(value);
309  if (ok) {
310  list->push_back(*object);
311  } else {
312  result = false;
313  break;
314  }
315  } else {
316  Py_XDECREF(value);
317  result = false;
318  break;
319  }
320  }
321  }
322  }
323  return result;
324 }
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 417 of file PythonQtConversion.h.

418 {
419  ListType* list = (ListType*)outList;
420  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
421  if (innerType == QVariant::Invalid) {
422  std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
423  }
424  bool result = false;
425  if (PySequence_Check(obj)) {
426  int count = PySequence_Size(obj);
427  if (count >= 0) {
428  result = true;
429  PyObject* value;
430  for (int i = 0; i < count; i++) {
431  QPair<T1, T2> pair;
432  value = PySequence_GetItem(obj, i);
433  if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType, false)) {
434  Py_XDECREF(value);
435  list->push_back(pair);
436  } else {
437  Py_XDECREF(value);
438  result = false;
439  break;
440  }
441  }
442  }
443  }
444  return result;
445 }

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

◆ PythonQtConvertPythonListToListOfValueType()

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

Definition at line 236 of file PythonQtConversion.h.

237 {
238  ListType* list = (ListType*)outList;
239  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
240  if (innerType == QVariant::Invalid) {
241  std::cerr << "PythonQtConvertPythonListToListOfValueType: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
242  }
243  bool result = false;
244  if (PySequence_Check(obj)) {
245  int count = PySequence_Size(obj);
246  if (count >= 0) {
247  result = true;
248  PyObject* value;
249  for (int i = 0;i<count;i++) {
250  value = PySequence_GetItem(obj,i);
251  // this is quite some overhead, but it avoids having another large switch...
252  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
253  Py_XDECREF(value);
254  if (v.isValid()) {
255  list->push_back(qvariant_cast<T>(v));
256  } else {
257  result = false;
258  break;
259  }
260  }
261  }
262  }
263  return result;
264 }
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 478 of file PythonQtConversion.h.

479 {
480  MapType* map = (MapType*)outMap;
481  static int innerType = -1;
482  if (innerType == -1) {
483  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
484  QList<QByteArray> names = innerTypes.split(',');
485  innerType = QMetaType::type(names.at(1).trimmed());
486  }
487  if (innerType == QVariant::Invalid) {
488  std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
489  }
490  bool result = false;
491  if (PyMapping_Check(val)) {
492  result = true;
493  PyObject* items = PyMapping_Items(val);
494  if (items) {
495  int count = PyList_Size(items);
496  PyObject* value;
497  PyObject* key;
498  PyObject* tuple;
499  for (int i = 0; i < count; i++) {
500  tuple = PyList_GetItem(items, i);
501  key = PyTuple_GetItem(tuple, 0);
502  value = PyTuple_GetItem(tuple, 1);
503 
504  bool ok;
505  int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
506  // this is quite some overhead, but it avoids having another large switch...
507  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
508  if (v.isValid() && ok) {
509  map->insert(intKey, qvariant_cast<T>(v));
510  } else {
511  result = false;
512  break;
513  }
514  }
515  Py_DECREF(items);
516  }
517  }
518  return result;
519 }
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 350 of file PythonQtConversion.h.

351 {
352  QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
353  static int innerType1 = -1;
354  static int innerType2 = -1;
355  if (innerType1 == -1) {
356  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
357  QList<QByteArray> names = innerTypes.split(',');
358  innerType1 = QMetaType::type(names.at(0).trimmed());
359  innerType2 = QMetaType::type(names.at(1).trimmed());
360  }
361  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
362  std::cerr << "PythonQtConvertPythonToPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
363  }
364  bool result = false;
365  if (PySequence_Check(obj)) {
366  int count = PySequence_Size(obj);
367  if (count == 2) {
368  result = true;
369  PyObject* value;
370 
371  value = PySequence_GetItem(obj, 0);
372  // this is quite some overhead, but it avoids having another large switch...
373  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType1);
374  Py_XDECREF(value);
375  if (v.isValid()) {
376  pair->first = qvariant_cast<T1>(v);
377  } else {
378  return false;
379  }
380 
381  value = PySequence_GetItem(obj, 1);
382  // this is quite some overhead, but it avoids having another large switch...
383  v = PythonQtConv::PyObjToQVariant(value, innerType2);
384  Py_XDECREF(value);
385  if (v.isValid()) {
386  pair->second = qvariant_cast<T2>(v);
387  } else {
388  return false;
389  }
390  }
391  }
392  return result;
393 }

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