itom 2.2.1
K:/git-itom/sources/itom/Qitom/python/pythonQtConversion.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2016, Institut fuer Technische Optik (ITO),
00005     Universitaet Stuttgart, Germany
00006 
00007     This file is part of itom.
00008   
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013 
00014     itom is distributed in the hope that it will be useful, but
00015     WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00017     General Public Licence for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with itom. If not, see <http://www.gnu.org/licenses/>.
00021 *********************************************************************** */
00022 
00023 #ifndef PYTHONQTCONVERSION_H
00024 #define PYTHONQTCONVERSION_H
00025 
00026 #include "pythonDataObject.h"
00027 #include "pythonPlugins.h"
00028 #include "pythonRegion.h"
00029 #include "pythonPCL.h"
00030 #include "pythonAutoInterval.h"
00031 #include "pythonNone.h"
00032 #include "datetime.h"
00033 
00034 #include "../global.h"
00035 #include "../../common/itomPlotHandle.h"
00036 #include "../../common/shape.h"
00037 
00038 #include "opencv/cv.h"
00039 #include <qvariant.h>
00040 #include <qmetaobject.h>
00041 #include <qlist.h>
00042 #include <qbytearray.h>
00043 #include <qpointer.h>
00044 
00045 #include "../helper/qpropertyHelper.h"
00046 
00047 //#if ITOM_POINTCLOUDLIBRARY > 0
00048 //Q_DECLARE_METATYPE(ito::PCLPointCloud)
00049 //Q_DECLARE_METATYPE(QSharedPointer<ito::PCLPointCloud>)
00050 //Q_DECLARE_METATYPE(QSharedPointer<ito::PCLPolygonMesh>)
00051 //Q_DECLARE_METATYPE(ito::PCLPoint)
00052 //Q_DECLARE_METATYPE(ito::PCLPolygonMesh)
00053 //#endif
00054 //
00055 //Q_DECLARE_METATYPE(QSharedPointer<ito::DataObject>)
00056 //Q_DECLARE_METATYPE(QPointer<ito::AddInDataIO>)
00057 //Q_DECLARE_METATYPE(QPointer<ito::AddInActuator>)
00058 //Q_DECLARE_METATYPE(ito::Shape)
00059 //Q_DECLARE_METATYPE(QVector<ito::Shape>)
00060 
00061 
00062 Q_DECLARE_METATYPE(ito::ItomPlotHandle)
00063 Q_DECLARE_METATYPE(ito::PythonNone)
00064 
00065 namespace ito
00066 {
00067 
00068 class PythonQtConversion
00069 {
00070 public:
00071     enum unicodeEncodings { utf_8, latin_1, iso_8859_1, mbcs, ascii, utf_16, utf_16_LE, utf_16_BE, utf_32, utf_32_BE, utf_32_LE, other };
00072 
00074     static PyObject* QStringToPyObject(const QString& str);
00075 
00077     static PyObject* QStringListToPyObject(const QStringList& list);
00078 
00080     static PyObject* QStringListToPyList(const QStringList& list);
00081 
00083     static PyObject* QDateToPyDate(const QDate& date);
00084 
00086     static PyObject* QDateTimeToPyDateTime(const QDateTime& datetime);
00087 
00089     static PyObject* QTimeToPyTime(const QTime& time);
00090 
00092     static QString PyObjGetRepresentation(PyObject* val);
00093 
00095     static QString PyObjGetString(PyObject* val) { bool ok; QString s = PyObjGetString(val, false, ok); return s; }
00097     static QString PyObjGetString(PyObject* val, bool strict, bool &ok);
00099     static std::string PyObjGetStdStringAsLatin1(PyObject* val, bool strict, bool &ok);
00101     static QByteArray PyObjGetBytes(PyObject* val, bool strict, bool &ok);
00103     static QSharedPointer<char> PyObjGetBytesShared(PyObject* val, bool strict, bool &ok);
00105     static int     PyObjGetInt(PyObject* val, bool strict, bool &ok);
00107     static qint64  PyObjGetLongLong(PyObject* val, bool strict, bool &ok);
00109     static quint64  PyObjGetULongLong(PyObject* val, bool strict, bool &ok);
00111     static double  PyObjGetDouble(PyObject* val, bool strict, bool &ok);
00113     static QVector<double>  PyObjGetDoubleArray(PyObject* val, bool strict, bool &ok);
00115     static complex128  PyObjGetComplex(PyObject* val, bool strict, bool &ok);
00117     static QVector<complex128>  PyObjGetComplexArray(PyObject* val, bool strict, bool &ok);
00119     static QVector<int>  PyObjGetIntArray(PyObject* val, bool strict, bool &ok);
00121     static bool    PyObjGetBool(PyObject* val, bool strict, bool &ok);
00123     static QVector<ito::Shape> PyObjGetShapeVector(PyObject* val, bool &ok);
00124 
00125 #if ITOM_POINTCLOUDLIBRARY > 0
00126     static ito::PCLPointCloud PyObjGetPointCloud(PyObject *val, bool strict, bool &ok);
00127     static ito::PCLPoint PyObjGetPoint(PyObject *val, bool strict, bool &ok);
00128     static ito::PCLPolygonMesh PyObjGetPolygonMesh(PyObject *val, bool strict, bool &ok);
00129 
00130     
00131     static ito::PCLPointCloud* PyObjGetPointCloudNewPtr(PyObject *val, bool strict, bool &ok);
00132     static ito::PCLPolygonMesh* PyObjGetPolygonMeshNewPtr(PyObject *val, bool strict, bool &ok);
00133 #endif
00134 
00135     static ito::DataObject* PyObjGetDataObjectNewPtr(PyObject *val, bool strict, bool &ok);
00136 
00137     static QSharedPointer<ito::DataObject> PyObjGetSharedDataObject(PyObject *val, bool &ok); //is always strict, only dataobjects are allowed
00138 
00140     static QStringList PyObjToStringList(PyObject* val, bool strict, bool& ok);
00141 
00142 
00145     static QVariant PyObjToQVariant(PyObject* val, int type = -1);
00146 
00149     static bool PyObjToVoidPtr(PyObject* val, void **retPtr, int *retType, int type = -1, bool strict = false);
00150 
00152     static PyObject* GetPyBool(bool val);
00153     static PyObject* QVariantToPyObject(const QVariant& v);
00154 
00155     static PyObject* QVariantMapToPyObject(const QVariantMap& m);
00156     static PyObject* QVariantListToPyObject(const QVariantList& l);
00157 
00158 #if ITOM_POINTCLOUDLIBRARY > 0
00159     static PyObject* PCLPointCloudToPyObject(const ito::PCLPointCloud& c);
00160     static PyObject* PCLPointToPyObject(const ito::PCLPoint& c);
00161     static PyObject* PCLPolygonMeshToPyObject(const ito::PCLPolygonMesh& c);
00162 #endif
00163 
00164     static PyObject* DataObjectToPyObject(const ito::DataObject& dObj);
00165     static PyObject* AddInBaseToPyObject(ito::AddInBase* aib);
00166 
00167     static PyObject* ConvertQtValueToPythonInternal(int type, const void* data); 
00168 
00169     static PyObject* QByteArrayToPyUnicode(const QByteArray &ba, const char *errors = "replace");
00170     static PyObject* QByteArrayToPyUnicodeSecure(const QByteArray &ba, const char *errors = "replace");
00171     static PyObject* ByteArrayToPyUnicode(const char* byteArray, Py_ssize_t len = 0, const char *errors = "replace");
00172 
00173     static PyObject* PyUnicodeToPyByteObject(PyObject *unicode, const char *errors = "replace");
00174 
00175     friend class PythonEngine; //such that the pythonEngine can set the encoding values below
00176 
00177 private:
00178     static unicodeEncodings textEncoding;
00179     static QByteArray textEncodingName;
00180 
00185     static QHash<char*,PyObject*> m_pyBaseObjectStorage; 
00186     static void baseObjectDeleterDataObject(ito::DataObject *sharedObject)
00187     {
00188         QHash<char*,PyObject*>::iterator i = m_pyBaseObjectStorage.find((char*)sharedObject);
00189         if(i != m_pyBaseObjectStorage.end())
00190         {
00191             Py_XDECREF(i.value());
00192             m_pyBaseObjectStorage.erase(i);
00193         }
00194 
00195         delete sharedObject;
00196     }
00197 };
00198 
00199 } //end namespace ito
00200 
00201 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Friends