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

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

426 {
427  MapType* map = (MapType*)inMap;
428  static int innerType = -1;
429  if (innerType == -1) {
430  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
431  QList<QByteArray> names = innerTypes.split(',');
432  innerType = QMetaType::type(names.at(1).trimmed());
433  }
434  if (innerType == QVariant::Invalid) {
435  std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
436  }
437 
438  PyObject* result = PyDict_New();
439  typename MapType::const_iterator t = map->constBegin();
440  PyObject* key;
441  PyObject* val;
442  for (; t != map->constEnd(); t++) {
443  key = PyInt_FromLong(t.key());
444  val = PythonQtConv::ConvertQtValueToPythonInternal(innerType, &t.value());
445  PyDict_SetItem(result, key, val);
446  Py_DECREF(key);
447  Py_DECREF(val);
448  }
449  return result;
450 }
template<class ListType , class T >
PyObject* PythonQtConvertListOfKnownClassToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 250 of file PythonQtConversion.h.

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

251 {
252  ListType* list = (ListType*)inList;
254  if (innerType == NULL) {
255  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
256  }
257  PyObject* result = PyTuple_New(list->size());
258  int i = 0;
259  Q_FOREACH(const T& value, *list) {
260  T* newObject = new T(value);
262  wrap->_ownedByPythonQt = true;
263  PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
264  i++;
265  }
266  return result;
267 }
template<class ListType , class T1 , class T2 >
PyObject* PythonQtConvertListOfPairToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 375 of file PythonQtConversion.h.

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

376 {
377  ListType* list = (ListType*)inList;
378  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
379  if (innerType == QVariant::Invalid) {
380  std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
381  }
382  PyObject* result = PyTuple_New(list->size());
383  int i = 0;
384  typedef const QPair<T1, T2> Pair;
385  Q_FOREACH(Pair& value, *list) {
386  PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
387  PyTuple_SET_ITEM(result, i, object);
388  i++;
389  }
390  return result;
391 }
template<class ListType , class T >
PyObject* PythonQtConvertListOfValueTypeToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 201 of file PythonQtConversion.h.

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

202 {
203  ListType* list = (ListType*)inList;
204  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
205  if (innerType == QVariant::Invalid) {
206  std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
207  }
208  PyObject* result = PyTuple_New(list->size());
209  int i = 0;
210  Q_FOREACH (const T& value, *list) {
211  PyTuple_SET_ITEM(result, i, PythonQtConv::ConvertQtValueToPythonInternal(innerType, &value));
212  i++;
213  }
214  return result;
215 }
template<class T1 , class T2 >
PyObject* PythonQtConvertPairToPython ( const void *  inPair,
int  metaTypeId 
)

Definition at line 308 of file PythonQtConversion.h.

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

309 {
310  QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
311  static int innerType1 = -1;
312  static int innerType2 = -1;
313  if (innerType1==-1) {
314  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
315  QList<QByteArray> names = innerTypes.split(',');
316  innerType1 = QMetaType::type(names.at(0).trimmed());
317  innerType2 = QMetaType::type(names.at(1).trimmed());
318  }
319  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
320  std::cerr << "PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
321  }
322  PyObject* result = PyTuple_New(2);
323  PyTuple_SET_ITEM(result, 0, PythonQtConv::ConvertQtValueToPythonInternal(innerType1, &pair->first));
324  PyTuple_SET_ITEM(result, 1, PythonQtConv::ConvertQtValueToPythonInternal(innerType2, &pair->second));
325  return result;
326 }
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 270 of file PythonQtConversion.h.

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

271 {
272  ListType* list = (ListType*)outList;
274  if (innerType == NULL) {
275  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
276  }
277  bool result = false;
278  if (PySequence_Check(obj)) {
279  int count = PySequence_Size(obj);
280  if (count >= 0) {
281  result = true;
282  PyObject* value;
283  for (int i = 0; i < count; i++) {
284  value = PySequence_GetItem(obj, i);
285  if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
287  bool ok;
288  T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
289  if (ok) {
290  list->push_back(*object);
291  } else {
292  result = false;
293  break;
294  }
295  } else {
296  result = false;
297  break;
298  }
299  }
300  }
301  }
302  return result;
303 }
template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 394 of file PythonQtConversion.h.

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

395 {
396  ListType* list = (ListType*)outList;
397  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
398  if (innerType == QVariant::Invalid) {
399  std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
400  }
401  bool result = false;
402  if (PySequence_Check(obj)) {
403  int count = PySequence_Size(obj);
404  if (count >= 0) {
405  result = true;
406  PyObject* value;
407  for (int i = 0; i < count; i++) {
408  QPair<T1, T2> pair;
409  value = PySequence_GetItem(obj, i);
410  if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType, false)) {
411  list->push_back(pair);
412  } else {
413  result = false;
414  break;
415  }
416  }
417  }
418  }
419  return result;
420 }
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 218 of file PythonQtConversion.h.

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

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

Definition at line 453 of file PythonQtConversion.h.

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

454 {
455  MapType* map = (MapType*)outMap;
456  static int innerType = -1;
457  if (innerType == -1) {
458  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
459  QList<QByteArray> names = innerTypes.split(',');
460  innerType = QMetaType::type(names.at(1).trimmed());
461  }
462  if (innerType == QVariant::Invalid) {
463  std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
464  }
465  bool result = false;
466  if (PyMapping_Check(val)) {
467  result = true;
468  PyObject* items = PyMapping_Items(val);
469  if (items) {
470  int count = PyList_Size(items);
471  PyObject* value;
472  PyObject* key;
473  PyObject* tuple;
474  for (int i = 0; i < count; i++) {
475  tuple = PyList_GetItem(items, i);
476  key = PyTuple_GetItem(tuple, 0);
477  value = PyTuple_GetItem(tuple, 1);
478 
479  bool ok;
480  int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
481  // this is quite some overhead, but it avoids having another large switch...
482  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
483  if (v.isValid() && ok) {
484  map->insert(intKey, qvariant_cast<T>(v));
485  } else {
486  result = false;
487  break;
488  }
489  }
490  Py_DECREF(items);
491  }
492  }
493  return result;
494 }
template<class T1 , class T2 >
bool PythonQtConvertPythonToPair ( PyObject obj,
void *  outPair,
int  metaTypeId,
bool   
)

Definition at line 329 of file PythonQtConversion.h.

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

330 {
331  QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
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 << "PythonQtConvertPythonToPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
342  }
343  bool result = false;
344  if (PySequence_Check(obj)) {
345  int count = PySequence_Size(obj);
346  if (count == 2) {
347  result = true;
348  PyObject* value;
349 
350  value = PySequence_GetItem(obj, 0);
351  // this is quite some overhead, but it avoids having another large switch...
352  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType1);
353  if (v.isValid()) {
354  pair->first = qvariant_cast<T1>(v);
355  } else {
356  return false;
357  }
358 
359  value = PySequence_GetItem(obj, 1);
360  // this is quite some overhead, but it avoids having another large switch...
361  v = PythonQtConv::PyObjToQVariant(value, innerType2);
362  if (v.isValid()) {
363  pair->second = qvariant_cast<T2>(v);
364  } else {
365  return false;
366  }
367  }
368  }
369  return result;
370 }