itom 1.0.14
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 
00131     UiContainer(QWidget *widget, tUiType type) : 
00132         m_type(type)
00133     {
00134         m_weakDialog = QWeakPointer<QWidget>(widget);
00135     }
00136     
00138     UiContainer(const UiContainer &cpy)
00139     {
00140         m_weakDialog = QWeakPointer<QWidget>(cpy.getUiWidget());
00141         m_type = cpy.m_type;
00142     }
00143     
00144 
00145     ~UiContainer(); //comment in source file
00146 
00147 
00149     inline UserUiDialog *getUiDialog() const 
00150     { 
00151         if(m_type == uiTypeUiDialog)
00152         {
00153             if(m_weakDialog.isNull()) return NULL;
00154             return qobject_cast<UserUiDialog*>(m_weakDialog.data()); 
00155         }
00156         return NULL;
00157     }
00158 
00160 
00164     inline QWidget *getUiWidget() const 
00165     { 
00166         if(m_weakDialog.isNull()) return NULL;
00167         return m_weakDialog.data();
00168     }
00169         
00171     inline tUiType getType() const { return m_type; }
00172 
00173 private:
00174     QWeakPointer<QWidget> m_weakDialog;         
00175     tUiType m_type;                                                     
00176 };
00177 
00178 struct UiContainerItem
00179 {
00180 public:
00181     UiContainerItem() : container(NULL) {}
00182     
00183     UiContainerItem(const UiContainerItem &cpy)
00184     {
00185         guardedHandle = cpy.guardedHandle;
00186         container = cpy.container;
00187     }
00188 
00189     QWeakPointer< unsigned int > guardedHandle;
00190     UiContainer *container;
00191 };
00192 
00193 
00194 
00195 
00196 
00197 
00198 class UiOrganizer : public QObject
00199 {
00200     Q_OBJECT
00201 public:
00202     enum tPropertyFlags  
00203     {
00204         propValid      =   0x0001,
00205         propConstant   =   0x0002,
00206         propFinal      =   0x0004,
00207         propReadable   =   0x0008,
00208         propWritable   =   0x0010,
00209         propResettable =   0x0020
00210     };
00211 
00212     enum tErrorCode  
00213     {
00214         errorUiHandleInvalid = 0x1001,
00215         errorObjDoesNotExist = 0x1002,
00216         errorObjPropWrite = 0x1003,
00217         errorObjPropRead = 0x1004,
00218         errorObjPropDoesNotExist = 0x1005,
00219         errorUnregisteredType = 0x1006,
00220         errorSlotDoesNotExist = 0x1007,
00221         errorSignalDoesNotExist = 0x1008,
00222         errorConnectionError = 0x1009
00223     };
00224 
00225     enum tWinType 
00226     {
00227         typeDialog     = 0x0000,
00228         typeMainWindow = 0x0001,
00229         typeDockWidget = 0x0002
00230     };
00231 
00232     enum tObjectInfo
00233     {
00234                 infoShowNoInheritance = 0x0001,
00235                 infoShowItomInheritance = 0x0002,
00236                 infoShowAllInheritance =0x0004 | infoShowItomInheritance,
00237     };
00238 
00239     UiOrganizer();
00240     ~UiOrganizer();
00241 
00242     void showDialog(QWidget *parent);
00243     //inline QHash<QString, PluginInfo> & getLoadedPluginList(void) { return m_pluginInfoList; }
00244     inline QObject *getPluginReference(unsigned int objectID) { return getWeakObjectReference(objectID); }
00245 //    ito::RetVal getNewPluginWindow(const QString pluginName, ito::uint32 &objectID, QObject **newWidget);
00246 
00247     static inline void parseUiDescription(int uiDescription, int* uiType = NULL, int* buttonBarType = NULL, bool* childOfMainWindow = NULL, bool* deleteOnClose = NULL)
00248     {
00249         if(uiType) *uiType = (uiDescription & 0x000000FF); //bits 1-8
00250         if(buttonBarType) *buttonBarType = ((uiDescription & 0x0000FF00) >> 8); //bits 9-16
00251         if(childOfMainWindow) *childOfMainWindow = ((uiDescription & 0x00FF0000) > 0); //bits 17-24
00252         if(deleteOnClose) *deleteOnClose = ((uiDescription & 0xFF000000) > 0); //bits 25-32
00253     }
00254 
00255     static inline int createUiDescription(int uiType, int buttonBarType, bool childOfMainWindow, bool deleteOnClose) 
00256     { 
00257         int v = uiType; //bits 1-8
00258         if(childOfMainWindow) v += (1 << 16); //bits 17-24
00259         if(deleteOnClose) v+= (1 << 24); //bits 25-32
00260         v += (buttonBarType << 8); //bits 9-16
00261         return v;
00262     }
00263 
00264     RetVal getNewPluginWindow(QString pluginName, unsigned int &objectID, QWidget** newWidget, QWidget *parent = NULL);
00265 
00266     QWidget* loadDesignerPluginWidget(const QString &name, RetVal &retValue, AbstractFigure::WindowMode winMode, QWidget *parent = NULL);
00267 
00268 protected:
00269 
00270     static void threadSafeDeleteUi(unsigned int *handle);
00271 
00272 private:
00273     UiContainer* getUiDialogByHandle(unsigned int uiHandle);
00274 
00275     void execGarbageCollection();
00276 
00277     unsigned int addObjectToList(QObject* objPtr);
00278     QObject *getWeakObjectReference(unsigned int objectID);
00279 
00280     void timerEvent(QTimerEvent *event);
00281 
00282         WidgetWrapper *m_widgetWrapper;                                 
00283     //QHash<QString, PluginInfo> m_pluginInfoList;
00284 
00285     QHash<unsigned int, UiContainerItem> m_dialogList; 
00286     QHash<unsigned int, QWeakPointer<QObject> > m_objectList;  
00287     int m_garbageCollectorTimer;                                        
00288     QMap<QObject*, QThread*> m_watcherThreads;   
00290     static unsigned int autoIncUiDialogCounter;         
00291     static unsigned int autoIncObjectCounter;           
00292     ito::RetVal scanPlugins(QString path, QHash<QString, PluginInfo> &pluginInfoList);
00293 
00294     void setApiPointersToWidgetAndChildren(QWidget *widget);
00295     //moved the uiLoader object to here from loadDesignerPluginWidget and createNewDialog methods as according
00296     //to valgrind it causes memory leaks. So better have only one instance created and maintain mem leaks low ;-)
00297     QUiLoader m_uiLoader;
00298 
00299 signals:
00300 
00301 public slots:
00302     void pythonKeyboardInterrupt(bool checked);
00303 
00304     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);
00305     RetVal addWidgetToOrganizer(QWidget *widget, QSharedPointer<unsigned int>dialogHandle, QSharedPointer<unsigned int>initSlotCount, QWidget *parent = NULL, ItomSharedSemaphore *semaphore = NULL);
00306     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);
00307     RetVal deleteDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00308     RetVal showDialog(unsigned int handle, int modalLevel, QSharedPointer<int> retCodeIfModal, ItomSharedSemaphore *semaphore = NULL);
00309     RetVal hideDialog(unsigned int handle, ItomSharedSemaphore *semaphore = NULL);
00310     RetVal setAttribute(unsigned int handle, Qt::WidgetAttribute attribute, bool on = true, ItomSharedSemaphore *semaphore = NULL);
00311     RetVal isVisible(unsigned int handle, QSharedPointer<bool> visible, ItomSharedSemaphore *semaphore = NULL);
00312 
00313     RetVal getDockedStatus(unsigned int uiHandle, QSharedPointer<bool> docked, ItomSharedSemaphore *semaphore = NULL);
00314     RetVal setDockedStatus(unsigned int uiHandle, bool docked, ItomSharedSemaphore *semaphore = NULL);
00315 
00316     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 );
00317     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 );
00318     RetVal showInputDialogGetItem(QString title, QString label, QStringList stringList, QSharedPointer<bool> ok, QSharedPointer<QString> value, int currentIndex = 0, bool editable = false, ItomSharedSemaphore *semaphore = NULL );
00319     RetVal showInputDialogGetText(QString title, QString label, QString defaultString, QSharedPointer<bool> ok, QSharedPointer<QString> value, ItomSharedSemaphore *semaphore = NULL );
00320     RetVal showMessageBox(unsigned int uiHandle, int type, QString title, QString text, int buttons, int defaultButton, QSharedPointer<int> retButton, QSharedPointer<QString> retButtonText, ItomSharedSemaphore *semaphore = NULL );
00321 
00322     RetVal showFileDialogExistingDir(unsigned int uiHandle, QString caption, QSharedPointer<QString> directory, int options = QFileDialog::ShowDirsOnly, ItomSharedSemaphore *semaphore = NULL); //options are of type QFileDialog::Options
00323     RetVal showFileOpenDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00324     RetVal showFileSaveDialog(unsigned int uiHandle, QString caption, QString directory, QString filter, QSharedPointer<QString> file, int selectedFilterIndex = 0, int options = 0, ItomSharedSemaphore *semaphore = NULL);
00325 
00326     RetVal getPropertyInfos(unsigned int objectID, QSharedPointer<QVariantMap> retPropertyMap, ItomSharedSemaphore *semaphore = NULL);
00327     RetVal readProperties(unsigned int objectID, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00328     RetVal writeProperties(unsigned int objectID, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00329     RetVal readProperties(unsigned int handle, QString widgetName, QSharedPointer<QVariantMap> properties, ItomSharedSemaphore *semaphore = NULL);
00330     RetVal writeProperties(unsigned int handle, QString widgetName, QVariantMap properties, ItomSharedSemaphore *semaphore = NULL);
00331     RetVal getAttribute(unsigned int objectID, int attributeNumber, QSharedPointer<bool> value, ItomSharedSemaphore *semaphore = NULL);
00332     RetVal setAttribute(unsigned int objectID, int attributeNumber, bool value, ItomSharedSemaphore *semaphore = NULL);
00333     RetVal getWindowFlags(unsigned int objectID, QSharedPointer<int> flags, ItomSharedSemaphore *semaphore = NULL);
00334     RetVal setWindowFlags(unsigned int objectID, int flags, ItomSharedSemaphore *semaphore = NULL);
00335     RetVal widgetMetaObjectCounts(unsigned int objectID, QSharedPointer<int> classInfoCount, QSharedPointer<int> enumeratorCount, QSharedPointer<int> methodCount, QSharedPointer<int> propertyCount, ItomSharedSemaphore *semaphore = NULL );
00336 
00337     RetVal getChildObject(unsigned int uiHandle, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00338     RetVal getChildObject2(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, ItomSharedSemaphore *semaphore = NULL);
00339     RetVal getChildObject3(unsigned int parentObjectID, QString objectName, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00340     RetVal getSignalIndex(unsigned int objectID, QString signalSignature, QSharedPointer<int> signalIndex, QSharedPointer<QObject*> objPtr, QSharedPointer<IntList> argTypes, ItomSharedSemaphore *semaphore = NULL);
00341     RetVal callSlotOrMethod(bool slotNotMethod, unsigned int objectID, int slotOrMethodIndex, QSharedPointer<FctCallParamContainer> args, ItomSharedSemaphore *semaphore = NULL);
00342 
00343     RetVal getObjectInfo(unsigned int objectID, int type, QSharedPointer<QVariantMap> infoMap, ItomSharedSemaphore *semaphore = NULL);
00344 
00345     RetVal connectWithKeyboardInterrupt(unsigned int objectID, QString signalSignature, ItomSharedSemaphore *semaphore = NULL);
00346 
00347     RetVal getMethodDescriptions(unsigned int objectID, QSharedPointer<MethodDescriptionList> methodList, ItomSharedSemaphore *semaphore = NULL);
00348 
00349     RetVal getObjectInfo(unsigned int objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00350 
00351     /*RetVal plotImage(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> plotHandle, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);    
00352     RetVal liveData(AddInDataIO* dataIO, QString widget, QObject **window, ItomSharedSemaphore *semaphore = NULL);
00353     RetVal liveImage(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);
00354     RetVal liveLine(AddInDataIO* dataIO, QString plotClassName = "", ItomSharedSemaphore *semaphore = NULL);*/
00355 
00356     RetVal createFigure(QSharedPointer< QSharedPointer<unsigned int> > guardedFigureHandle, QSharedPointer<unsigned int> initSlotCount, QSharedPointer<unsigned int> objectID, QSharedPointer<int> rows, QSharedPointer<int> cols, ItomSharedSemaphore *semaphore = NULL);
00357     RetVal getSubplot(QSharedPointer<unsigned int> figHandle, unsigned int subplotIndex, QSharedPointer<unsigned int> objectID, QSharedPointer<QByteArray> objectName, QSharedPointer<QByteArray> widgetClassName, ItomSharedSemaphore *semaphore = NULL);
00358 
00359     RetVal figurePlot(QSharedPointer<ito::DataObject> dataObj, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00360     RetVal figureLiveImage(AddInDataIO* dataIO, QSharedPointer<unsigned int> figHandle, QSharedPointer<unsigned int> objectID, int areaRow, int areaCol, QString className, ItomSharedSemaphore *semaphore = NULL);
00361     
00362     RetVal figureRemoveGuardedHandle(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00363     RetVal figureClose(unsigned int figHandle, ItomSharedSemaphore *semaphore = NULL);
00364     RetVal figurePickPoints(unsigned int objectID, QSharedPointer<ito::DataObject> coords, int maxNrPoints, ItomSharedSemaphore *semaphore);
00365 
00366     void figureDestroyed(QObject *obj)
00367     {
00368         qDebug() << obj;
00369     }
00370 
00371 private slots:
00372     void watcherThreadFinished();
00373 
00374 };
00375 
00376 } //namespace ito
00377 
00378 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends