itom 1.2.0
D:/git-itom/sources/itom/Qitom/organizer/uiOrganizer.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2013, Institut für Technische Optik (ITO),
00005     Universität 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 UIORGANIZER_H
00024 #define UIORGANIZER_H
00025 
00026 //#include "../python/pythonQtConversion.h"
00027 #include "../python/pythonItomMetaObject.h"
00028 
00029 #include <qwidget.h>
00030 
00031 #include "../common/sharedStructuresQt.h"
00032 #include "../DataObject/dataobj.h"
00033 #if ITOM_POINTCLOUDLIBRARY > 0
00034 #include "../../PointCloud/pclStructures.h"
00035 #endif
00036 
00037 #include "../widgets/userUiDialog.h"
00038 #include "../widgets/figureWidget.h"
00039 
00040 #include "../global.h"
00041 #include "../common/sharedStructuresGraphics.h"
00042 #include "../common/addInInterface.h"
00043 
00044 #include "../../plot/AbstractFigure.h"
00045 
00046 #include <qmap.h>
00047 #include <qsharedpointer.h>
00048 #include <qstring.h>
00049 #include <qvariant.h>
00050 #include <qhash.h>
00051 #include <qtimer.h>
00052 #include <qfiledialog.h>
00053 #include <qmainwindow.h>
00054 #include <QtUiTools/quiloader.h>
00055 #include <qthread.h>
00056 
00057 namespace ito
00058 {
00059 
00060 class WidgetWrapper; //forward declaration
00061 
00067 struct UiContainer
00068 {
00069 public:
00070     enum tUiType     
00071     {
00072         uiTypeUiDialog    = 0x0001,
00073         uiTypeQDialog     = 0x0002,
00074         uiTypeQMainWindow = 0x0003,
00075         uiTypeQDockWidget = 0x0004,
00076         uiTypeFigure      = 0x0005
00077     };
00078 
00080 
00085     UiContainer(UserUiDialog *uiDialog) : 
00086         m_type(uiTypeUiDialog) 
00087     {
00088         m_weakDialog = QPointer<QWidget>(uiDialog);
00089     }
00090 
00092 
00097     UiContainer(QDialog *dialog) : 
00098         m_type(uiTypeQDialog) 
00099     {
00100         m_weakDialog = QPointer<QDialog>(dialog);
00101     }
00102     
00104 
00109     UiContainer(QMainWindow *mainWindow) : 
00110         m_type(uiTypeQMainWindow) 
00111     {
00112         m_weakDialog = QPointer<QWidget>(mainWindow);
00113     }
00114 
00116 
00121     UiContainer(FigureWidget *figureWidget) : 
00122         m_type(uiTypeFigure) 
00123     {
00124         m_weakDialog = QPointer<QWidget>(figureWidget);
00125     }
00126 
00128 
00133     UiContainer(QDockWidget *dockWidget) : 
00134         m_type(uiTypeQDockWidget) 
00135     {
00136         m_weakDialog = QPointer<QWidget>(dockWidget);
00137     }
00138     
00140 
00146     UiContainer(QWidget *widget, tUiType type) : 
00147         m_type(type)
00148     {
00149         m_weakDialog = QPointer<QWidget>(widget);
00150     }
00151     
00153     UiContainer(const UiContainer &cpy)
00154     {
00155         m_weakDialog = QPointer<QWidget>(cpy.getUiWidget());
00156         m_type = cpy.m_type;
00157     }
00158     
00159 
00160     ~UiContainer(); //comment in source file
00161 
00162 
00164     inline UserUiDialog *getUiDialog() const 
00165     { 
00166         if(m_type == uiTypeUiDialog)
00167         {
00168             if(m_weakDialog.isNull()) return NULL;
00169             return qobject_cast<UserUiDialog*>(m_weakDialog.data()); 
00170         }
00171         return NULL;
00172     }
00173 
00175 
00179     inline QWidget *getUiWidget() const 
00180     { 
00181         if(m_weakDialog.isNull()) return NULL;
00182         return m_weakDialog.data();
00183     }
00184     
00186     inline tUiType getType() const { return m_type; }
00187 
00188 private:
00189     QPointer<QWidget> m_weakDialog;        
00190     tUiType m_type;                            
00191 };
00192 
00193 struct UiContainerItem
00194 {
00195 public:
00196     UiContainerItem() : container(NULL) {}
00197     
00198     UiContainerItem(const UiContainerItem &cpy)
00199     {
00200         guardedHandle = cpy.guardedHandle;
00201         container = cpy.container;
00202     }
00203 
00204     QWeakPointer< unsigned int > guardedHandle;
00205     UiContainer *container;
00206 };
00207 
00208 class UiDataContainer 
00209 {
00210 private:
00211     ito::ParamBase::Type m_dataType;
00212     QSharedPointer<ito::DataObject> m_dObjPtr;
00213 #if ITOM_POINTCLOUDLIBRARY > 0
00214     QSharedPointer<ito::PCLPointCloud> m_dPCPtr;
00215     QSharedPointer<ito::PCLPolygonMesh> m_dPMPtr;
00216 #endif
00217 
00218 public:
00219     UiDataContainer() : m_dataType(ito::ParamBase::DObjPtr) {};
00220     ~UiDataContainer() {};
00221     UiDataContainer(const QSharedPointer<ito::DataObject> &sharedDataObject) : m_dataType(ito::ParamBase::DObjPtr), m_dObjPtr(sharedDataObject) {}
00222 #if ITOM_POINTCLOUDLIBRARY > 0
00223     UiDataContainer(const QSharedPointer<ito::PCLPointCloud> &sharedPointCloud) : m_dataType(ito::ParamBase::PointCloudPtr), m_dPCPtr(sharedPointCloud) {}
00224     UiDataContainer(const QSharedPointer<ito::PCLPolygonMesh> &sharedPolygonMesh) : m_dataType(ito::ParamBase::PolygonMeshPtr), m_dPMPtr(sharedPolygonMesh) {}
00225 
00226     inline UiDataContainer & operator = (QSharedPointer<ito::PCLPointCloud> sharedPointCloud)
00227     {
00228         m_dataType = ito::ParamBase::PointCloudPtr;
00229         m_dPCPtr = sharedPointCloud;
00230         m_dObjPtr.clear();
00231         m_dPMPtr.clear();
00232         return *this;
00233     }
00234 
00235     inline UiDataContainer & operator = (QSharedPointer<ito::PCLPolygonMesh> sharedPolygonMesh)
00236     {
00237         m_dataType = ito::ParamBase::PolygonMeshPtr;
00238         m_dPMPtr = sharedPolygonMesh;
00239         m_dObjPtr.clear();
00240         m_dPCPtr.clear();
00241         return *this;
00242     }
00243 #endif
00244 
00245     inline UiDataContainer & operator = (QSharedPointer<ito::DataObject> sharedDataObject)
00246     {
00247         m_dataType = ito::ParamBase::DObjPtr;
00248         m_dObjPtr = sharedDataObject;
00249 #if ITOM_POINTCLOUDLIBRARY > 0
00250         m_dPCPtr.clear();
00251         m_dPMPtr.clear();
00252 #endif
00253         return *this;
00254     }
00255 
00256     inline ito::ParamBase::Type getType() const { return m_dataType; }
00257     inline QSharedPointer<ito::DataObject> getDataObject() const { return m_dObjPtr; }
00258 #if ITOM_POINTCLOUDLIBRARY > 0
00259     inline QSharedPointer<ito::PCLPointCloud> getPointCloud() const { return m_dPCPtr; }
00260     inline QSharedPointer<ito::PCLPolygonMesh> getPolygonMesh() const { return m_dPMPtr; }
00261 #endif
00262 };
00263 
00264 class UiOrganizer : public QObject
00265 {
00266     Q_OBJECT
00267 public:
00268     enum tPropertyFlags  
00269     {
00270         propValid      =   0x0001,
00271         propConstant   =   0x0002,
00272         propFinal      =   0x0004,
00273         propReadable   =   0x0008,
00274         propWritable   =   0x0010,
00275         propResettable =   0x0020
00276     };
00277 
00278     enum tErrorCode  
00279     {
00280         errorUiHandleInvalid = 0x1001,
00281         errorObjDoesNotExist = 0x1002,
00282         errorObjPropWrite = 0x1003,
00283         errorObjPropRead = 0x1004,
00284         errorObjPropDoesNotExist = 0x1005,
00285         errorUnregisteredType = 0x1006,
00286         errorSlotDoesNotExist = 0x1007,
00287         errorSignalDoesNotExist = 0x1008,
00288         errorConnectionError = 0x1009
00289     };
00290 
00291     enum tWinType 
00292     {
00293         typeDialog     = 0x0000,
00294         typeMainWindow = 0x0001,
00295         typeDockWidget = 0x0002
00296     };
00297 
00298     enum tObjectInfo
00299     {
00300         infoShowNoInheritance = 0x0001,
00301         infoShowItomInheritance = 0x0002,
00302         infoShowAllInheritance =0x0004 | infoShowItomInheritance
00303     };
00304 
00305     UiOrganizer();
00306     ~UiOrganizer();
00307 
00308     void showDialog(QWidget *parent);
00309     //inline QHash<QString, PluginInfo> & getLoadedPluginList(void) { return m_pluginInfoList; }
00310     inline QObject *getPluginReference(unsigned int objectID) { return getWeakObjectReference(objectID); }
00311 //    ito::RetVal getNewPluginWindow(const QString pluginName, ito::uint32 &objectID, QObject **newWidget);
00312 
00313     static inline void parseUiDescription(int uiDescription, int* uiType = NULL, int* buttonBarType = NULL, bool* childOfMainWindow = NULL, bool* deleteOnClose = NULL)
00314     {
00315         if(uiType) *uiType = (uiDescription & 0x000000FF); //bits 1-8
00316         if(buttonBarType) *buttonBarType = ((uiDescription & 0x0000FF00) >> 8); //bits 9-16
00317         if(childOfMainWindow) *childOfMainWindow = ((uiDescription & 0x00FF0000) > 0); //bits 17-24
00318         if(deleteOnClose) *deleteOnClose = ((uiDescription & 0xFF000000) > 0); //bits 25-32
00319     }
00320 
00321     static inline int createUiDescription(int uiType, int buttonBarType, bool childOfMainWindow, bool deleteOnClose) 
00322     { 
00323         int v = uiType; //bits 1-8
00324         if(childOfMainWindow) v += (1 << 16); //bits 17-24
00325         if(deleteOnClose) v+= (1 << 24); //bits 25-32
00326         v += (buttonBarType << 8); //bits 9-16
00327         return v;
00328     }
00329 
00330     RetVal getNewPluginWindow(QString pluginName, unsigned int &objectID, QWidget** newWidget, QWidget *parent = NULL);
00331 
00332     QWidget* loadDesignerPluginWidget(const QString &name, RetVal &retValue, AbstractFigure::WindowMode winMode, QWidget *parent = NULL);
00333 
00334 protected:
00335 
00336     static void threadSafeDeleteUi(unsigned int *handle);
00337 
00338 private:
00339     void execGarbageCollection();
00340 
00341     unsigned int addObjectToList(QObject* objPtr);
00342     QObject *getWeakObjectReference(unsigned int objectID);
00343 
00344     void timerEvent(QTimerEvent *event);
00345 
00346     WidgetWrapper *m_widgetWrapper;                    
00347     //QHash<QString, PluginInfo> m_pluginInfoList;
00348 
00349     QHash<unsigned int, UiContainerItem> m_dialogList; 
00350     QHash<unsigned int, QPointer<QObject> > m_objectList;  
00351     int m_garbageCollectorTimer;                    
00352     QMap<QObject*, QThread*> m_watcherThreads;   
00354     static unsigned int autoIncUiDialogCounter;        
00355     static unsigned int autoIncObjectCounter;        
00357     void setApiPointersToWidgetAndChildren(QWidget *widget);
00358     //moved the uiLoader object to here from loadDesignerPluginWidget and createNewDialog methods as according
00359     //to valgrind it causes memory leaks. So better have only one instance created and maintain mem leaks low ;-)
00360     QUiLoader m_uiLoader;
00361 
00362 signals:
00363 
00364 public slots:
00365     void pythonKeyboardInterrupt(bool checked);
00366 
00367     RetVal loadPluginWidget(void* algoWidgetFunc, QVector<ito::ParamBase> *paramsMand, QVector<ito::ParamBase> *paramsOpt, QSharedPointer<unsigned int>dialogHandle, QSharedPointer<unsigned int>initSlotCount, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> className, ItomSharedSemaphore *semaphore = NULL);
00368     RetVal addWidgetToOrganizer(QWidget *widget, QSharedPointer<unsigned int>dialogHandle, QSharedPointer<unsigned int>initSlotCount, QWidget *parent = NULL, ItomSharedSemaphore *semaphore = NULL);
00369     RetVal createNewDialog(QString filename, int uiDescription, StringMap dialogButtons, QSharedPointer<unsigned int> dialogHandle, QSharedPointer<unsigned int> initSlotCount, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> className, ItomSharedSemaphore *semaphore = NULL);
00370     RetVal deleteDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00371     RetVal showDialog(unsigned int handle, int modalLevel, QSharedPointer<int> retCodeIfModal, ItomSharedSemaphore *semaphore = NULL);
00372     RetVal hideDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00373     RetVal setAttribute(unsigned int handle, Qt::WidgetAttribute attribute, bool on = true, ItomSharedSemaphore *semaphore = NULL);
00374     RetVal isVisible(unsigned int handle, QSharedPointer<bool> visible, ItomSharedSemaphore *semaphore = NULL);
00375     UiContainer* getUiDialogByHandle(unsigned int uiHandle);
00376 
00377     RetVal getDockedStatus(unsigned int uiHandle, QSharedPointer<bool> docked, ItomSharedSemaphore *semaphore = NULL);
00378     RetVal setDockedStatus(unsigned int uiHandle, bool docked, ItomSharedSemaphore *semaphore = NULL);
00379 
00380     RetVal showInputDialogGetDouble(QString title, QString label, double defaultValue, QSharedPointer<bool> ok, QSharedPointer<double> value, double min = -2147483647, double max = 2147483647, int decimals = 1, ItomSharedSemaphore *semaphore = NULL );
00381     RetVal showInputDialogGetInt(QString title, QString label, int defaultValue, QSharedPointer<bool> ok, QSharedPointer<int> value, int min = -2147483647, int max = 2147483647, int step = 1, ItomSharedSemaphore *semaphore = NULL );
00382     RetVal showInputDialogGetItem(QString title, QString label, QStringList stringList, QSharedPointer<bool> ok, QSharedPointer<QString> value, int currentIndex = 0, bool editable = false, ItomSharedSemaphore *semaphore = NULL );
00383     RetVal showInputDialogGetText(QString title, QString label, QString defaultString, QSharedPointer<bool> ok, QSharedPointer<QString> value, ItomSharedSemaphore *semaphore = NULL );
00384     RetVal showMessageBox(unsigned int uiHandle, int type, QString title, QString text, int buttons, int defaultButton, QSharedPointer<int> retButton, QSharedPointer<QString> retButtonText, ItomSharedSemaphore *semaphore = NULL );
00385 
00386     RetVal showFileDialogExistingDir(unsigned int uiHandle, QString caption, QSharedPointer<QString> directory, int options = QFileDialog::ShowDirsOnly, ItomSharedSemaphore *semaphore = NULL); //options are of type QFileDialog::Options
00387     RetVal showFileOpenDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00388     RetVal showFileSaveDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00389 
00390     RetVal getPropertyInfos(unsigned int objectID, QSharedPointer<QVariantMap> retPropertyMap, ItomSharedSemaphore *semaphore = NULL);
00391     RetVal readProperties(unsigned int objectID, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00392     RetVal writeProperties(unsigned int objectID, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00393     RetVal readProperties(unsigned int handle, QString widgetName, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00394     RetVal writeProperties(unsigned int handle, QString widgetName, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00395     RetVal getAttribute(unsigned int objectID, int attributeNumber, QSharedPointer<bool> value, ItomSharedSemaphore *semaphore = NULL);
00396     RetVal setAttribute(unsigned int objectID, int attributeNumber, bool value, ItomSharedSemaphore *semaphore = NULL);
00397     RetVal getWindowFlags(unsigned int objectID, QSharedPointer<int> flags, ItomSharedSemaphore *semaphore = NULL);
00398     RetVal setWindowFlags(unsigned int objectID, int flags, ItomSharedSemaphore *semaphore = NULL);
00399     RetVal widgetMetaObjectCounts(unsigned int objectID, QSharedPointer<int> classInfoCount, QSharedPointer<int> enumeratorCount, QSharedPointer<int> methodCount, QSharedPointer<int> propertyCount, ItomSharedSemaphore *semaphore = NULL );
00400 
00401     RetVal getChildObject(unsigned int uiHandle, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00402     RetVal getChildObject2(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00403     RetVal getChildObject3(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00404     RetVal getSignalIndex(unsigned int objectID, QString signalSignature, QSharedPointer<int> signalIndex, QSharedPointer<QObject*> objPtr, QSharedPointer<IntList> argTypes, ItomSharedSemaphore *semaphore = NULL);
00405     RetVal callSlotOrMethod(bool slotNotMethod, unsigned int objectID, int slotOrMethodIndex, QSharedPointer<FctCallParamContainer> args, ItomSharedSemaphore *semaphore = NULL);
00406 
00407     RetVal getObjectInfo(unsigned int objectID, int type, QSharedPointer<QVariantMap> infoMap, ItomSharedSemaphore *semaphore = NULL);
00408 
00409     RetVal connectWithKeyboardInterrupt(unsigned int objectID, QString signalSignature, ItomSharedSemaphore *semaphore = NULL);
00410 
00411     RetVal getMethodDescriptions(unsigned int objectID, QSharedPointer<MethodDescriptionList> methodList, ItomSharedSemaphore *semaphore = NULL);
00412 
00413     RetVal getObjectInfo(unsigned int objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00414 
00415     /*RetVal plotImage(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> plotHandle, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);    
00416     RetVal liveData(AddInDataIO* dataIO, QString widget, QObject **window, ItomSharedSemaphore *semaphore = NULL);
00417     RetVal liveImage(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);
00418     RetVal liveLine(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);*/
00419 
00420     RetVal createFigure(QSharedPointer< QSharedPointer<unsigned int> > guardedFigureHandle, QSharedPointer<unsigned int> initSlotCount, QSharedPointer<unsigned int> objectID, QSharedPointer<int> rows, QSharedPointer<int> cols, ItomSharedSemaphore *semaphore = NULL);
00421     RetVal getSubplot(QSharedPointer<unsigned int> figHandle, unsigned int subplotIndex, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00422 
00423 //    RetVal figurePlot(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00424     RetVal figurePlot(ito::UiDataContainer &dataCont, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00425     RetVal figureLiveImage(AddInDataIO* dataIO, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00426     
00427     RetVal figureRemoveGuardedHandle(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00428     RetVal figureClose(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00429     RetVal figurePickPoints(unsigned int objectID, QSharedPointer<ito::DataObject> coords, int maxNrPoints, ItomSharedSemaphore *semaphore);
00430     RetVal figureDrawGeometricElements(unsigned int objectID, QSharedPointer<ito::DataObject> coords, int elementType, int maxNrElements, ItomSharedSemaphore *semaphore);
00431     RetVal figurePickPointsInterrupt(unsigned int objectID);
00432 
00433     void figureDestroyed(QObject *obj)
00434     {
00435         qDebug() << obj;
00436     }
00437 
00438 private slots:
00439     void watcherThreadFinished();
00440 
00441 };
00442 
00443 } //namespace ito
00444 
00445 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends