itom 1.2.0
D:/git-itom/sources/itom/Qitom/widgets/abstractDockWidget.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 ABSTRACTDOCKWIDGET_H
00024 #define ABSTRACTDOCKWIDGET_H
00025 
00026 #include "../global.h"
00027 #include "../common/sharedStructures.h"
00028 
00029 #include <qmainwindow.h>
00030 #include <qdockwidget.h>
00031 #include <qmenubar.h>
00032 #include <qevent.h>
00033 #include <qmap.h>
00034 #include <qtoolbar.h>
00035 #include <qdebug.h>
00036 #include <qstring.h>
00037 #include <qaction.h>
00038 #include <qshortcut.h>
00039 #include <qrect.h>
00040 #include <qwidget.h>
00041 
00042 
00043 namespace ito
00044 {
00045     class AbstractDockWidget : public QDockWidget
00046     {
00047         Q_OBJECT
00048         
00049         //these properties are taken from QWidget in order to redirect them either to the QDockWidget or to the main window (depending on dock status)
00050         Q_PROPERTY(bool visible READ isVisible WRITE setVisible DESIGNABLE false)
00051         Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
00052 
00053         Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry)
00054         Q_PROPERTY(QRect frameGeometry READ frameGeometry)
00055         Q_PROPERTY(QRect normalGeometry READ normalGeometry)
00056         Q_PROPERTY(int x READ x)
00057         Q_PROPERTY(int y READ y)
00058         Q_PROPERTY(QPoint pos READ pos WRITE move DESIGNABLE false STORED false)
00059         Q_PROPERTY(QSize frameSize READ frameSize)
00060         Q_PROPERTY(QSize size READ size WRITE resize DESIGNABLE false STORED false)
00061         Q_PROPERTY(int width READ width)
00062         Q_PROPERTY(int height READ height)
00063         Q_PROPERTY(QRect rect READ rect)
00064         Q_PROPERTY(QRect childrenRect READ childrenRect)
00065         Q_PROPERTY(QRegion childrenRegion READ childrenRegion)
00066 
00067         public:
00068 
00069             enum tFloatingStyle { floatingNone, floatingStandard, floatingWindow }; 
00070             enum tMovingStyle { movingDisabled, movingEnabled };    
00071             enum tTopLevelStyle { topLevelOverall, topLevelParentOnly, topLevelNothing };
00072 
00073             struct Toolbar
00074             {
00075                 Toolbar() : section(0), key(""), tb(NULL) {}
00076                 Qt::ToolBarArea area;
00077                 int section;
00078                 QString key;
00079                 QToolBar *tb;
00080             };
00081 
00082             AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title = QString(), const QString &objName = QString(), QWidget *parent = 0);
00083             virtual ~AbstractDockWidget();
00084 
00085             inline bool docked() const { return m_docked; }    
00087             RetVal setAdvancedWindowTitle( QString newCompleteTitle = QString(), bool appendToBasicTitle = true );
00088 
00089             RetVal setTopLevel( tTopLevelStyle topLevel );
00090 
00091             void setParent ( QWidget * parent ) { m_overallParent = parent; QDockWidget::setParent(parent); }
00092 
00093             QWidget *getActiveInstance() 
00094             { 
00095                 if(!m_docked && m_floatingStyle == floatingWindow)
00096                 {
00097                     return m_pWindow;
00098                 }
00099                 return this;
00100             }
00101 
00102             //these methods are mainly redirected to QDockWidget or QMainWindow (depending on dock status)
00103             #define QWIDGETPROPGETTER(gettername) if(m_docked && m_dockAvailable) { return QDockWidget::gettername(); } else { return m_pWindow->gettername(); }
00104 
00105             #define QWIDGETPROPSETTER(settername,...) \
00106             if (m_docked) \
00107             { \
00108                 QDockWidget::settername(__VA_ARGS__);  \
00109             } \
00110             else \
00111             { \
00112                 if (m_floatingStyle == floatingWindow) \
00113                 { \
00114                     m_pWindow->settername(__VA_ARGS__); \
00115                     QDockWidget::settername(__VA_ARGS__); \
00116                 } \
00117                 else \
00118                 { \
00119                     QDockWidget::settername(__VA_ARGS__); \
00120                 } \
00121             }
00122 
00123             QRect frameGeometry() const;
00124             const QRect &geometry() const;
00125             QRect normalGeometry() const;
00126 
00127             int x() const;
00128             int y() const;
00129             QPoint pos() const;
00130             QSize frameSize() const;
00131             QSize size() const;
00132             int width() const;
00133             int height() const;
00134             QRect rect() const;
00135             QRect childrenRect() const;
00136             QRegion childrenRegion() const;
00137 
00138             void move(int x, int y);
00139             void move(const QPoint &);
00140             void resize(int w, int h);
00141             void resize(const QSize &);
00142             void setGeometry(int x, int y, int w, int h);
00143             void setGeometry(const QRect &);
00144 
00145             bool isEnabled() const;
00146             bool isVisible() const;
00147 
00148             void saveState(const QString &iniName) const;
00149             void restoreState(const QString &iniName) const;
00150 
00151         public Q_SLOTS:
00152             void setEnabled(bool);
00153             virtual void setVisible(bool visible);
00154    
00155         protected:
00156 
00157             class ShortcutAction : public QObject
00158             {
00159             public:
00160                 ShortcutAction(const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00161                 {
00162                     m_action = new QAction(text, parent);
00163                 }
00164 
00165                 ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00166                 {
00167                     m_action = new QAction(icon, text, parent);
00168                 }
00169 
00170                 ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent, const QKeySequence &key, Qt::ShortcutContext context = Qt::WindowShortcut) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00171                 {
00172                     QString text2 = text;
00173                     QString text3 = text;
00174                     text2.append( "\t" );
00175                     text2.append( key.toString(QKeySequence::NativeText) );
00176                     text3.append( " (" );
00177                     text3.append( key.toString(QKeySequence::NativeText) );
00178                     text3.append( ")" );
00179                     m_action = new QAction(icon, text2, parent);
00180                     m_action->setToolTip(text3);
00181                     m_shortcut = new QShortcut(key, parent->getCanvas());
00182                     m_shortcut->setContext(context);
00183                 }
00184 
00185                 ~ShortcutAction()
00186                 {
00187                     //do not delete action and shortcut here, since it will be deleted by common parent.
00188                 }
00189 
00190                 void connectTrigger(const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
00191                 {
00192                     if(m_action)
00193                     {
00194                         QObject::connect(m_action, SIGNAL(triggered()), receiver, method, type);
00195                     }
00196                     if(m_shortcut)
00197                     {
00198                         QObject::connect(m_shortcut, SIGNAL(activated()), receiver, method, type);
00199                     }
00200                 }
00201 
00202                 void setEnabled(bool actionEnabled, bool shortcutEnabled)
00203                 {
00204                     if(m_action) 
00205                     {
00206                         m_action->setEnabled(actionEnabled);
00207                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00208                     }
00209                 }
00210 
00211                 void setEnabled(bool enabled)
00212                 {
00213                     setEnabled(enabled,enabled);
00214                 }
00215 
00216                 void setVisible(bool actionVisible, bool shortcutEnabled)
00217                 {
00218                     if(m_action) 
00219                     {
00220                         m_action->setVisible(actionVisible);
00221                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00222                     }
00223                 }
00224 
00225                 void setVisible(bool visible)
00226                 {
00227                     setVisible(visible,visible);
00228                 }
00229 
00230                 QAction* action() { return m_action; }
00231 
00232             private:
00233                 QAction *m_action;
00234                 QShortcut *m_shortcut;
00235             };
00236 
00238 
00243             bool eventFilter(QObject *obj, QEvent *event)
00244             {
00245                 if (event->type() == QEvent::Close)
00246                 {
00247                     closeEvent((QCloseEvent*)event);
00248                     if(testAttribute(Qt::WA_DeleteOnClose) && event->isAccepted() ) //if window should be closed and dockwidget is assigned with WA_DeleteOnClose, delete this dockwidget first.
00249                     {
00250                        deleteLater();
00251                     }
00252                     
00253                     return true;
00254                 }
00255                 else
00256                 {
00257                     return QObject::eventFilter(obj,event);
00258                 }
00259             };
00260 
00261             void init();
00262 
00263             virtual void closeEvent(QCloseEvent *event);
00264 
00265             virtual void createActions() = 0;
00266             virtual void createMenus() = 0;
00267             virtual void createToolBars() = 0;
00268             virtual void createStatusBar() = 0;
00269             virtual void updateActions() {}
00270             virtual void updatePythonActions() = 0;
00271             
00272             Qt::WindowFlags modifyFlags(const Qt::WindowFlags &flags, const Qt::WindowFlags &setFlags, const Qt::WindowFlags &unsetFlags);
00273 
00274             virtual void windowStateChanged( bool /*windowNotToolbox*/ ) {}
00275 
00276             void setContentWidget(QWidget *widget);
00277             inline QWidget* getContentWidget() const { return m_pWindow->centralWidget(); }   
00278             inline QMainWindow* getCanvas() { return m_pWindow; }
00279 
00280             inline bool pythonBusy() const { return m_pythonBusy; }                    
00281             inline bool pythonDebugMode() const { return m_pythonDebugMode; }          
00282             inline bool pythonInWaitingMode() const { return m_pythonInWaitingMode; }  
00284             //RetVal addAndRegisterToolBar(QToolBar* tb, QString key);
00285             //RetVal unregisterToolBar(QString key);
00286             QToolBar* getToolBar(QString key) const;
00287             inline QMenuBar* getMenuBar() const { return (m_pWindow == NULL) ?  NULL : m_pWindow->menuBar(); }
00288 
00289             RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area = Qt::TopToolBarArea, int section = 1);
00290             RetVal removeToolBar(const QString &key);
00291 
00292             QAction *m_actStayOnTop;
00293             QAction *m_actStayOnTopOfApp;
00294 
00295         private:
00296 
00297             void contextMenuEvent(QContextMenuEvent *e)
00298             {
00299                 e->accept();
00300                 qDebug() << "context menu of abstractDockWidget clicked. [placeholder for (un)docking feature.] pos: " << e->pos();
00301             }
00302 
00303             QMainWindow *m_pWindow;
00304 
00305             bool m_docked;                      
00306             bool m_dockAvailable;               
00307             tFloatingStyle m_floatingStyle;     
00308             tMovingStyle m_movingStyle;         
00309             QString m_basicTitle;               
00310             QString m_completeTitle;            
00312             QMap<QString,QToolBar*> m_toolBars; 
00313             QList<Toolbar> m_toolbars;
00314 
00315             bool m_pythonBusy;                  
00316             bool m_pythonDebugMode;             
00317             bool m_pythonInWaitingMode;         
00319             QToolBar* m_dockToolbar;
00320             QAction* m_actDock;
00321             QAction* m_actUndock;
00322 
00323             QWidget* m_overallParent;           
00324             tTopLevelStyle m_recentTopLevelStyle;
00325 
00326             QSize m_oldMinSize;
00327             QSize m_oldMaxSize;
00328             QRect m_lastUndockedSize;
00329     
00330         //signals:
00331         //    void addDockToMainWindow(AbstractDockWidget *widget);       /*!<  signal emitted if widget should be docked to main window */
00332         //    void removeDockFromMainWindow(AbstractDockWidget *widget);  
00333         //    void dockCloseRequest(AbstractDockWidget *widget);
00334 
00335         public slots:
00336             virtual void dockedToMainWindow(AbstractDockWidget * /*widget*/){}
00337             virtual void removedFromMainWindow(AbstractDockWidget * /*widget*/){}
00338             virtual void pythonStateChanged(tPythonTransitions pyTransition);
00339 
00340             void raiseAndActivate();  
00342             void setDockSize(int newWidth, int newHeight);
00343 
00344             void dockWidget();
00345             void undockWidget();
00346 
00347         private slots:
00348             void mnuStayOnTop(bool checked);
00349             void mnuStayOnTopOfApp(bool checked);
00350 
00351             void returnToOldMinMaxSizes();
00352     
00353     };
00354 
00355 } //end namespace ito
00356 
00357 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends