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