itom 1.1.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 
00034 #include "../widgets/userUiDialog.h"
00035 #include "../widgets/figureWidget.h"
00036 
00037 #include "../global.h"
00038 #include "../common/sharedStructuresGraphics.h"
00039 #include "../common/addInInterface.h"
00040 
00041 #include "../../plot/AbstractFigure.h"
00042 
00043 #include <qmap.h>
00044 #include <qsharedpointer.h>
00045 #include <qstring.h>
00046 #include <qvariant.h>
00047 #include <qhash.h>
00048 #include <qtimer.h>
00049 #include <qfiledialog.h>
00050 #include <qmainwindow.h>
00051 #include <QtUiTools/quiloader.h>
00052 #include <qthread.h>
00053 
00054 namespace ito
00055 {
00056 
00057 class WidgetWrapper; //forward declaration
00058 
00064 struct UiContainer
00065 {
00066 public:
00067     enum tUiType     
00068     {
00069         uiTypeUiDialog    = 0x0001,
00070         uiTypeQDialog     = 0x0002,
00071         uiTypeQMainWindow = 0x0003,
00072         uiTypeQDockWidget = 0x0004,
00073         uiTypeFigure      = 0x0005
00074     };
00075 
00077 
00082     UiContainer(UserUiDialog *uiDialog) : 
00083         m_type(uiTypeUiDialog) 
00084     {
00085         m_weakDialog = QWeakPointer<QWidget>(qobject_cast<QWidget*>(uiDialog));
00086     }
00087 
00089 
00094     UiContainer(QDialog *dialog) : 
00095         m_type(uiTypeQDialog) 
00096     {
00097         m_weakDialog = QWeakPointer<QWidget>(qobject_cast<QWidget*>(dialog));
00098     }
00099     
00101 
00106     UiContainer(QMainWindow *mainWindow) : 
00107         m_type(uiTypeQMainWindow) 
00108     {
00109         m_weakDialog = QWeakPointer<QWidget>(qobject_cast<QWidget*>(mainWindow));
00110     }
00111 
00113 
00118     UiContainer(FigureWidget *figureWidget) : 
00119         m_type(uiTypeFigure) 
00120     {
00121         m_weakDialog = QWeakPointer<QWidget>(qobject_cast<QWidget*>(figureWidget));
00122     }
00123 
00125 
00130     UiContainer(QDockWidget *dockWidget) : 
00131         m_type(uiTypeQDockWidget) 
00132     {
00133         m_weakDialog = QWeakPointer<QWidget>(qobject_cast<QWidget*>(dockWidget));
00134     }
00135     
00137 
00143     UiContainer(QWidget *widget, tUiType type) : 
00144         m_type(type)
00145     {
00146         m_weakDialog = QWeakPointer<QWidget>(widget);
00147     }
00148     
00150     UiContainer(const UiContainer &cpy)
00151     {
00152         m_weakDialog = QWeakPointer<QWidget>(cpy.getUiWidget());
00153         m_type = cpy.m_type;
00154     }
00155     
00156 
00157     ~UiContainer(); //comment in source file
00158 
00159 
00161     inline UserUiDialog *getUiDialog() const 
00162     { 
00163         if(m_type == uiTypeUiDialog)
00164         {
00165             if(m_weakDialog.isNull()) return NULL;
00166             return qobject_cast<UserUiDialog*>(m_weakDialog.data()); 
00167         }
00168         return NULL;
00169     }
00170 
00172 
00176     inline QWidget *getUiWidget() const 
00177     { 
00178         if(m_weakDialog.isNull()) return NULL;
00179         return m_weakDialog.data();
00180     }
00181     
00183     inline tUiType getType() const { return m_type; }
00184 
00185 private:
00186     QWeakPointer<QWidget> m_weakDialog;        
00187     tUiType m_type;                            
00188 };
00189 
00190 struct UiContainerItem
00191 {
00192 public:
00193     UiContainerItem() : container(NULL) {}
00194     
00195     UiContainerItem(const UiContainerItem &cpy)
00196     {
00197         guardedHandle = cpy.guardedHandle;
00198         container = cpy.container;
00199     }
00200 
00201     QWeakPointer< unsigned int > guardedHandle;
00202     UiContainer *container;
00203 };
00204 
00205 
00206 class UiOrganizer : public QObject
00207 {
00208     Q_OBJECT
00209 public:
00210     enum tPropertyFlags  
00211     {
00212         propValid      =   0x0001,
00213         propConstant   =   0x0002,
00214         propFinal      =   0x0004,
00215         propReadable   =   0x0008,
00216         propWritable   =   0x0010,
00217         propResettable =   0x0020
00218     };
00219 
00220     enum tErrorCode  
00221     {
00222         errorUiHandleInvalid = 0x1001,
00223         errorObjDoesNotExist = 0x1002,
00224         errorObjPropWrite = 0x1003,
00225         errorObjPropRead = 0x1004,
00226         errorObjPropDoesNotExist = 0x1005,
00227         errorUnregisteredType = 0x1006,
00228         errorSlotDoesNotExist = 0x1007,
00229         errorSignalDoesNotExist = 0x1008,
00230         errorConnectionError = 0x1009
00231     };
00232 
00233     enum tWinType 
00234     {
00235         typeDialog     = 0x0000,
00236         typeMainWindow = 0x0001,
00237         typeDockWidget = 0x0002
00238     };
00239 
00240     enum tObjectInfo
00241     {
00242         infoShowNoInheritance = 0x0001,
00243         infoShowItomInheritance = 0x0002,
00244         infoShowAllInheritance =0x0004 | infoShowItomInheritance
00245     };
00246 
00247     UiOrganizer();
00248     ~UiOrganizer();
00249 
00250     void showDialog(QWidget *parent);
00251     //inline QHash<QString, PluginInfo> & getLoadedPluginList(void) { return m_pluginInfoList; }
00252     inline QObject *getPluginReference(unsigned int objectID) { return getWeakObjectReference(objectID); }
00253 //    ito::RetVal getNewPluginWindow(const QString pluginName, ito::uint32 &objectID, QObject **newWidget);
00254 
00255     static inline void parseUiDescription(int uiDescription, int* uiType = NULL, int* buttonBarType = NULL, bool* childOfMainWindow = NULL, bool* deleteOnClose = NULL)
00256     {
00257         if(uiType) *uiType = (uiDescription & 0x000000FF); //bits 1-8
00258         if(buttonBarType) *buttonBarType = ((uiDescription & 0x0000FF00) >> 8); //bits 9-16
00259         if(childOfMainWindow) *childOfMainWindow = ((uiDescription & 0x00FF0000) > 0); //bits 17-24
00260         if(deleteOnClose) *deleteOnClose = ((uiDescription & 0xFF000000) > 0); //bits 25-32
00261     }
00262 
00263     static inline int createUiDescription(int uiType, int buttonBarType, bool childOfMainWindow, bool deleteOnClose) 
00264     { 
00265         int v = uiType; //bits 1-8
00266         if(childOfMainWindow) v += (1 << 16); //bits 17-24
00267         if(deleteOnClose) v+= (1 << 24); //bits 25-32
00268         v += (buttonBarType << 8); //bits 9-16
00269         return v;
00270     }
00271 
00272     RetVal getNewPluginWindow(QString pluginName, unsigned int &objectID, QWidget** newWidget, QWidget *parent = NULL);
00273 
00274     QWidget* loadDesignerPluginWidget(const QString &name, RetVal &retValue, AbstractFigure::WindowMode winMode, QWidget *parent = NULL);
00275 
00276 protected:
00277 
00278     static void threadSafeDeleteUi(unsigned int *handle);
00279 
00280 private:
00281     UiContainer* getUiDialogByHandle(unsigned int uiHandle);
00282 
00283     void execGarbageCollection();
00284 
00285     unsigned int addObjectToList(QObject* objPtr);
00286     QObject *getWeakObjectReference(unsigned int objectID);
00287 
00288     void timerEvent(QTimerEvent *event);
00289 
00290     WidgetWrapper *m_widgetWrapper;                    
00291     //QHash<QString, PluginInfo> m_pluginInfoList;
00292 
00293     QHash<unsigned int, UiContainerItem> m_dialogList; 
00294     QHash<unsigned int, QWeakPointer<QObject> > m_objectList;  
00295     int m_garbageCollectorTimer;                    
00296     QMap<QObject*, QThread*> m_watcherThreads;   
00298     static unsigned int autoIncUiDialogCounter;        
00299     static unsigned int autoIncObjectCounter;        
00301     void setApiPointersToWidgetAndChildren(QWidget *widget);
00302     //moved the uiLoader object to here from loadDesignerPluginWidget and createNewDialog methods as according
00303     //to valgrind it causes memory leaks. So better have only one instance created and maintain mem leaks low ;-)
00304     QUiLoader m_uiLoader;
00305 
00306 signals:
00307 
00308 public slots:
00309     void pythonKeyboardInterrupt(bool checked);
00310 
00311     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);
00312     RetVal addWidgetToOrganizer(QWidget *widget, QSharedPointer<unsigned int>dialogHandle, QSharedPointer<unsigned int>initSlotCount, QWidget *parent = NULL, ItomSharedSemaphore *semaphore = NULL);
00313     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);
00314     RetVal deleteDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00315     RetVal showDialog(unsigned int handle, int modalLevel, QSharedPointer<int> retCodeIfModal, ItomSharedSemaphore *semaphore = NULL);
00316     RetVal hideDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00317     RetVal setAttribute(unsigned int handle, Qt::WidgetAttribute attribute, bool on = true, ItomSharedSemaphore *semaphore = NULL);
00318     RetVal isVisible(unsigned int handle, QSharedPointer<bool> visible, ItomSharedSemaphore *semaphore = NULL);
00319 
00320     RetVal getDockedStatus(unsigned int uiHandle, QSharedPointer<bool> docked, ItomSharedSemaphore *semaphore = NULL);
00321     RetVal setDockedStatus(unsigned int uiHandle, bool docked, ItomSharedSemaphore *semaphore = NULL);
00322 
00323     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 );
00324     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 );
00325     RetVal showInputDialogGetItem(QString title, QString label, QStringList stringList, QSharedPointer<bool> ok, QSharedPointer<QString> value, int currentIndex = 0, bool editable = false, ItomSharedSemaphore *semaphore = NULL );
00326     RetVal showInputDialogGetText(QString title, QString label, QString defaultString, QSharedPointer<bool> ok, QSharedPointer<QString> value, ItomSharedSemaphore *semaphore = NULL );
00327     RetVal showMessageBox(unsigned int uiHandle, int type, QString title, QString text, int buttons, int defaultButton, QSharedPointer<int> retButton, QSharedPointer<QString> retButtonText, ItomSharedSemaphore *semaphore = NULL );
00328 
00329     RetVal showFileDialogExistingDir(unsigned int uiHandle, QString caption, QSharedPointer<QString> directory, int options = QFileDialog::ShowDirsOnly, ItomSharedSemaphore *semaphore = NULL); //options are of type QFileDialog::Options
00330     RetVal showFileOpenDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00331     RetVal showFileSaveDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00332 
00333     RetVal getPropertyInfos(unsigned int objectID, QSharedPointer<QVariantMap> retPropertyMap, ItomSharedSemaphore *semaphore = NULL);
00334     RetVal readProperties(unsigned int objectID, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00335     RetVal writeProperties(unsigned int objectID, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00336     RetVal readProperties(unsigned int handle, QString widgetName, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00337     RetVal writeProperties(unsigned int handle, QString widgetName, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00338     RetVal getAttribute(unsigned int objectID, int attributeNumber, QSharedPointer<bool> value, ItomSharedSemaphore *semaphore = NULL);
00339     RetVal setAttribute(unsigned int objectID, int attributeNumber, bool value, ItomSharedSemaphore *semaphore = NULL);
00340     RetVal getWindowFlags(unsigned int objectID, QSharedPointer<int> flags, ItomSharedSemaphore *semaphore = NULL);
00341     RetVal setWindowFlags(unsigned int objectID, int flags, ItomSharedSemaphore *semaphore = NULL);
00342     RetVal widgetMetaObjectCounts(unsigned int objectID, QSharedPointer<int> classInfoCount, QSharedPointer<int> enumeratorCount, QSharedPointer<int> methodCount, QSharedPointer<int> propertyCount, ItomSharedSemaphore *semaphore = NULL );
00343 
00344     RetVal getChildObject(unsigned int uiHandle, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00345     RetVal getChildObject2(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00346     RetVal getChildObject3(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00347     RetVal getSignalIndex(unsigned int objectID, QString signalSignature, QSharedPointer<int> signalIndex, QSharedPointer<QObject*> objPtr, QSharedPointer<IntList> argTypes, ItomSharedSemaphore *semaphore = NULL);
00348     RetVal callSlotOrMethod(bool slotNotMethod, unsigned int objectID, int slotOrMethodIndex, QSharedPointer<FctCallParamContainer> args, ItomSharedSemaphore *semaphore = NULL);
00349 
00350     RetVal getObjectInfo(unsigned int objectID, int type, QSharedPointer<QVariantMap> infoMap, ItomSharedSemaphore *semaphore = NULL);
00351 
00352     RetVal connectWithKeyboardInterrupt(unsigned int objectID, QString signalSignature, ItomSharedSemaphore *semaphore = NULL);
00353 
00354     RetVal getMethodDescriptions(unsigned int objectID, QSharedPointer<MethodDescriptionList> methodList, ItomSharedSemaphore *semaphore = NULL);
00355 
00356     RetVal getObjectInfo(unsigned int objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00357 
00358     /*RetVal plotImage(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> plotHandle, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);    
00359     RetVal liveData(AddInDataIO* dataIO, QString widget, QObject **window, ItomSharedSemaphore *semaphore = NULL);
00360     RetVal liveImage(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);
00361     RetVal liveLine(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);*/
00362 
00363     RetVal createFigure(QSharedPointer< QSharedPointer<unsigned int> > guardedFigureHandle, QSharedPointer<unsigned int> initSlotCount, QSharedPointer<unsigned int> objectID, QSharedPointer<int> rows, QSharedPointer<int> cols, ItomSharedSemaphore *semaphore = NULL);
00364     RetVal getSubplot(QSharedPointer<unsigned int> figHandle, unsigned int subplotIndex, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00365 
00366     RetVal figurePlot(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00367     RetVal figureLiveImage(AddInDataIO* dataIO, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00368     
00369     RetVal figureRemoveGuardedHandle(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00370     RetVal figureClose(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00371     RetVal figurePickPoints(unsigned int objectID, QSharedPointer<ito::DataObject> coords, int maxNrPoints, ItomSharedSemaphore *semaphore);
00372     RetVal figureDrawGeometricElements(unsigned int objectID, QSharedPointer<ito::DataObject> coords, int elementType, int maxNrElements, ItomSharedSemaphore *semaphore);
00373     RetVal figurePickPointsInterrupt(unsigned int objectID);
00374 
00375     void figureDestroyed(QObject *obj)
00376     {
00377         qDebug() << obj;
00378     }
00379 
00380 private slots:
00381     void watcherThreadFinished();
00382 
00383 };
00384 
00385 } //namespace ito
00386 
00387 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends