Ejemplo n.º 1
0
PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::ParameterInfo& info, const void* data) {
  // is it an enum value?
  if (info.enumWrapper) {
    if (info.pointerCount==0) {
      return PythonQtPrivate::createEnumValueInstance(info.enumWrapper, *((unsigned int*)data));
    } else {
      // we do not support pointers to enums (who needs them?)
      Py_INCREF(Py_None);
      return Py_None;
    }
  }

  if (info.typeId == QMetaType::Void) {
    Py_INCREF(Py_None);
    return Py_None;
  } else if ((info.pointerCount == 1) && (info.typeId == QMetaType::Char)) {
    // a char ptr will probably be a null terminated string, so we support that:
    return PyString_FromString(*((char**)data));
  } else if ((info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) &&
             info.name.startsWith("QList<")) {
    // it is a QList template:
    QByteArray innerType = info.name.mid(6,info.name.length()-7);
    if (innerType.endsWith("*")) {
      innerType.truncate(innerType.length()-1);
      QList<void*>* listPtr = NULL;
      if (info.pointerCount == 1) {
        listPtr = *((QList<void*>**)data);
      } else if (info.pointerCount == 0) {
        listPtr = (QList<void*>*)data;
      }
      if (listPtr) {
        return ConvertQListOfPointerTypeToPythonList(listPtr, innerType);
      } else {
        return NULL;
      }
    }
  }

  if (info.typeId >= QMetaType::User) {
    // if a converter is registered, we use is:
    PythonQtConvertMetaTypeToPythonCB* converter = _metaTypeToPythonConverters.value(info.typeId);
    if (converter) {
      return (*converter)(data, info.typeId);
    }
  }

  // special handling did not match, so we convert the usual way (either pointer or value version):
  if (info.pointerCount == 1) {
    // convert the pointer to a Python Object (we can handle ANY C++ object, in the worst case we just know the type and the pointer)
    return PythonQt::priv()->wrapPtr(*((void**)data), info.name);
  } else if (info.pointerCount == 0) {
    // handle values that are not yet handled and not pointers
    return ConvertQtValueToPythonInternal(info.typeId, data);
  } else {
    return NULL;
  }
}
Ejemplo n.º 2
0
PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::ParameterInfo& info, const void* data) {
  // is it an enum value?
  if (info.enumWrapper) {
    if (info.pointerCount==0) {
      return PythonQtPrivate::createEnumValueInstance(info.enumWrapper, *((unsigned int*)data));
    } else {
      // we do not support pointers to enums (who needs them?)
      Py_INCREF(Py_None);
      return Py_None;
    }
  }

  if (info.typeId == QMetaType::Void) {
    Py_INCREF(Py_None);
    return Py_None;
  } else if ((info.pointerCount == 1) && (info.typeId == QMetaType::Char)) {
    // a char ptr will probably be a null terminated string, so we support that:
    char* charPtr = *((char**)data);
    if (charPtr) {
      return PyString_FromString(charPtr);
    } else {
      Py_INCREF(Py_None);
      return Py_None;
    }
  } else if ((info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) &&
             info.isQList && (info.innerNamePointerCount == 1)) {
    // it is a QList<Obj*> template:
    QList<void*>* listPtr = NULL;
    if (info.pointerCount == 1) {
      listPtr = *((QList<void*>**)data);
    } else if (info.pointerCount == 0) {
      listPtr = (QList<void*>*)data;
    }
    if (listPtr) {
      return ConvertQListOfPointerTypeToPythonList(listPtr, info.innerName);
    } else {
      return NULL;
    }
  }

  if (info.typeId >= QMetaType::User) {
    // if a converter is registered, we use is:
    PythonQtConvertMetaTypeToPythonCB* converter = _metaTypeToPythonConverters.value(info.typeId);
    if (converter) {
      return (*converter)(info.pointerCount==0?data:*((void**)data), info.typeId);
    }
  }

  // special handling did not match, so we convert the usual way (either pointer or value version):
  if (info.pointerCount == 1) {
    // convert the pointer to a Python Object (we can handle ANY C++ object, in the worst case we just know the type and the pointer)
    return PythonQt::priv()->wrapPtr(*((void**)data), info.name);
  } else if (info.pointerCount == 0) {
    if (info.typeId != PythonQtMethodInfo::Unknown) {
      // handle values that are const& or by value and have a metatype
      return ConvertQtValueToPythonInternal(info.typeId, data);
    } else {
      // the type does not have a typeid, we need to make a copy using the copy constructor
      PythonQtClassInfo* classInfo = PythonQt::priv()->getClassInfo(info.name);
      if (classInfo) {
        PyObject* result = classInfo->copyObject((void*)data);
        if (result) {
          return result;
        }
      }
    }
  }
  Py_INCREF(Py_None);
  return Py_None;
}