SourceForge.net Logo
PythonQt
Classes | Macros | Typedefs | Functions
PythonQtConversion.h File Reference
#include "PythonQt.h"
#include "PythonQtMisc.h"
#include "PythonQtClassInfo.h"
#include "PythonQtMethodInfo.h"
#include <QWidget>
#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)
 

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

#define PythonQtRegisterIntegerMapConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<int, innertype > >(#type"<int, "#innertype">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToIntegerMap<type<int, innertype >, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertIntegerMapToPython<type<int, innertype >, innertype>); \
}

Definition at line 75 of file PythonQtConversion.h.

#define PythonQtRegisterListTemplateConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfValueType<type<innertype >, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfValueTypeToPythonList<type<innertype >, innertype>); \
}

Definition at line 57 of file PythonQtConversion.h.

#define PythonQtRegisterListTemplateConverterForKnownClass (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfKnownClass<type<innertype >, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfKnownClassToPythonList<type<innertype >, innertype>); \
}

Definition at line 63 of file PythonQtConversion.h.

#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>); \
}

Definition at line 81 of file PythonQtConversion.h.

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

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

Definition at line 89 of file PythonQtConversion.h.

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

Definition at line 94 of file PythonQtConversion.h.

Typedef Documentation

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

Definition at line 54 of file PythonQtConversion.h.

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

Definition at line 55 of file PythonQtConversion.h.

Function Documentation

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

Definition at line 442 of file PythonQtConversion.h.

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

443 {
444  MapType* map = (MapType*)inMap;
445  static int innerType = -1;
446  if (innerType == -1) {
447  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
448  QList<QByteArray> names = innerTypes.split(',');
449  innerType = QMetaType::type(names.at(1).trimmed());
450  }
451  if (innerType == QVariant::Invalid) {
452  std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
453  }
454 
455  PyObject* result = PyDict_New();
456  typename MapType::const_iterator t = map->constBegin();
457  PyObject* key;
458  PyObject* val;
459  for (; t != map->constEnd(); t++) {
460  key = PyInt_FromLong(t.key());
461  val = PythonQtConv::convertQtValueToPythonInternal(innerType, &t.value());
462  PyDict_SetItem(result, key, val);
463  Py_DECREF(key);
464  Py_DECREF(val);
465  }
466  return result;
467 }
template<class ListType , class T >
PyObject* PythonQtConvertListOfKnownClassToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 261 of file PythonQtConversion.h.

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

262 {
263  ListType* list = (ListType*)inList;
265  if (innerType == NULL) {
266  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
267  }
268  PyObject* result = PyTuple_New(list->size());
269  int i = 0;
270  Q_FOREACH(const T& value, *list) {
271  T* newObject = new T(value);
273  wrap->_ownedByPythonQt = true;
274  PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
275  i++;
276  }
277  return result;
278 }
template<class ListType , class T1 , class T2 >
PyObject* PythonQtConvertListOfPairToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 390 of file PythonQtConversion.h.

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

391 {
392  ListType* list = (ListType*)inList;
393  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
394  if (innerType == QVariant::Invalid) {
395  std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
396  }
397  PyObject* result = PyTuple_New(list->size());
398  int i = 0;
399  typedef const QPair<T1, T2> Pair;
400  Q_FOREACH(Pair& value, *list) {
401  PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
402  PyTuple_SET_ITEM(result, i, object);
403  i++;
404  }
405  return result;
406 }
template<class ListType , class T >
PyObject* PythonQtConvertListOfValueTypeToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 211 of file PythonQtConversion.h.

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

212 {
213  ListType* list = (ListType*)inList;
214  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
215  if (innerType == QVariant::Invalid) {
216  std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
217  }
218  PyObject* result = PyTuple_New(list->size());
219  int i = 0;
220  Q_FOREACH (const T& value, *list) {
221  PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
222  i++;
223  }
224  return result;
225 }
template<class T1 , class T2 >
PyObject* PythonQtConvertPairToPython ( const void *  inPair,
int  metaTypeId 
)

Definition at line 321 of file PythonQtConversion.h.

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

322 {
323  QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
324  static int innerType1 = -1;
325  static int innerType2 = -1;
326  if (innerType1==-1) {
327  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
328  QList<QByteArray> names = innerTypes.split(',');
329  innerType1 = QMetaType::type(names.at(0).trimmed());
330  innerType2 = QMetaType::type(names.at(1).trimmed());
331  }
332  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
333  std::cerr << "PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
334  }
335  PyObject* result = PyTuple_New(2);
336  PyTuple_SET_ITEM(result, 0, PythonQtConv::convertQtValueToPythonInternal(innerType1, &pair->first));
337  PyTuple_SET_ITEM(result, 1, PythonQtConv::convertQtValueToPythonInternal(innerType2, &pair->second));
338  return result;
339 }
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 281 of file PythonQtConversion.h.

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

282 {
283  ListType* list = (ListType*)outList;
285  if (innerType == NULL) {
286  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
287  }
288  bool result = false;
289  if (PySequence_Check(obj)) {
290  int count = PySequence_Size(obj);
291  if (count >= 0) {
292  result = true;
293  PyObject* value;
294  for (int i = 0; i < count; i++) {
295  value = PySequence_GetItem(obj, i);
296  if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
298  bool ok;
299  T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
300  Py_XDECREF(value);
301  if (ok) {
302  list->push_back(*object);
303  } else {
304  result = false;
305  break;
306  }
307  } else {
308  Py_XDECREF(value);
309  result = false;
310  break;
311  }
312  }
313  }
314  }
315  return result;
316 }
template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 409 of file PythonQtConversion.h.

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

410 {
411  ListType* list = (ListType*)outList;
412  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
413  if (innerType == QVariant::Invalid) {
414  std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
415  }
416  bool result = false;
417  if (PySequence_Check(obj)) {
418  int count = PySequence_Size(obj);
419  if (count >= 0) {
420  result = true;
421  PyObject* value;
422  for (int i = 0; i < count; i++) {
423  QPair<T1, T2> pair;
424  value = PySequence_GetItem(obj, i);
425  if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType, false)) {
426  Py_XDECREF(value);
427  list->push_back(pair);
428  } else {
429  Py_XDECREF(value);
430  result = false;
431  break;
432  }
433  }
434  }
435  }
436  return result;
437 }
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 228 of file PythonQtConversion.h.

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

229 {
230  ListType* list = (ListType*)outList;
231  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
232  if (innerType == QVariant::Invalid) {
233  std::cerr << "PythonQtConvertPythonListToListOfValueType: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
234  }
235  bool result = false;
236  if (PySequence_Check(obj)) {
237  int count = PySequence_Size(obj);
238  if (count >= 0) {
239  result = true;
240  PyObject* value;
241  for (int i = 0;i<count;i++) {
242  value = PySequence_GetItem(obj,i);
243  // this is quite some overhead, but it avoids having another large switch...
244  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
245  Py_XDECREF(value);
246  if (v.isValid()) {
247  list->push_back(qvariant_cast<T>(v));
248  } else {
249  result = false;
250  break;
251  }
252  }
253  }
254  }
255  return result;
256 }
template<class MapType , class T >
bool PythonQtConvertPythonToIntegerMap ( PyObject val,
void *  outMap,
int  metaTypeId,
bool   
)

Definition at line 470 of file PythonQtConversion.h.

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

471 {
472  MapType* map = (MapType*)outMap;
473  static int innerType = -1;
474  if (innerType == -1) {
475  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
476  QList<QByteArray> names = innerTypes.split(',');
477  innerType = QMetaType::type(names.at(1).trimmed());
478  }
479  if (innerType == QVariant::Invalid) {
480  std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
481  }
482  bool result = false;
483  if (PyMapping_Check(val)) {
484  result = true;
485  PyObject* items = PyMapping_Items(val);
486  if (items) {
487  int count = PyList_Size(items);
488  PyObject* value;
489  PyObject* key;
490  PyObject* tuple;
491  for (int i = 0; i < count; i++) {
492  tuple = PyList_GetItem(items, i);
493  key = PyTuple_GetItem(tuple, 0);
494  value = PyTuple_GetItem(tuple, 1);
495 
496  bool ok;
497  int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
498  // this is quite some overhead, but it avoids having another large switch...
499  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
500  if (v.isValid() && ok) {
501  map->insert(intKey, qvariant_cast<T>(v));
502  } else {
503  result = false;
504  break;
505  }
506  }
507  Py_DECREF(items);
508  }
509  }
510  return result;
511 }
template<class T1 , class T2 >
bool PythonQtConvertPythonToPair ( PyObject obj,
void *  outPair,
int  metaTypeId,
bool   
)

Definition at line 342 of file PythonQtConversion.h.

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

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