itom 2.0.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             #define QWIDGETPROPSETTERSPECIAL(settername,...) \
00124             if (m_docked) \
00125             { \
00126                 QDockWidget::settername(__VA_ARGS__);  \
00127             } \
00128             else \
00129             { \
00130                 if (m_floatingStyle == floatingWindow) \
00131                 { \
00132                     m_pWindow->settername(__VA_ARGS__); \
00133                 } \
00134                 else \
00135                 { \
00136                     QDockWidget::settername(__VA_ARGS__); \
00137                 } \
00138             }
00139 
00140             QRect frameGeometry() const;
00141             const QRect &geometry() const;
00142             QRect normalGeometry() const;
00143 
00144             int x() const;
00145             int y() const;
00146             QPoint pos() const;
00147             QSize frameSize() const;
00148             QSize size() const;
00149             int width() const;
00150             int height() const;
00151             QRect rect() const;
00152             QRect childrenRect() const;
00153             QRegion childrenRegion() const;
00154 
00155             void move(int x, int y);
00156             void move(const QPoint &);
00157             void resize(int w, int h);
00158             void resize(const QSize &);
00159             void setGeometry(int x, int y, int w, int h);
00160             void setGeometry(const QRect &);
00161 
00162             bool isEnabled() const;
00163             bool isVisible() const;
00164 
00165             void saveState(const QString &iniName) const;
00166             void restoreState(const QString &iniName);
00167 
00168             void synchronizeTopLevelState();
00169 
00170         public Q_SLOTS:
00171             void setEnabled(bool);
00172             virtual void setVisible(bool visible);
00173    
00174         protected:
00175 
00176             class ShortcutAction : public QObject
00177             {
00178             public:
00179                 ShortcutAction(const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00180                 {
00181                     m_action = new QAction(text, parent);
00182                     QString toolTipText = text;
00183                     toolTipText.replace("&", "");
00184                     m_action->setToolTip(toolTipText);
00185                 }
00186 
00187                 ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00188                 {
00189                     m_action = new QAction(icon, text, parent);
00190                     QString toolTipText = text;
00191                     toolTipText.replace("&", "");
00192                     m_action->setToolTip(toolTipText);
00193                 }
00194 
00195                 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)
00196                 {
00197                     QString text2 = text;
00198                     QString text3 = text;
00199                     text3.replace("&", "");
00200 
00201                     //some key sequences do not exist as default on all operating systems
00202                     if (key.isEmpty() == false)
00203                     {
00204                         text2 += "\t" + key.toString(QKeySequence::NativeText);
00205                         text3 += " (" + key.toString(QKeySequence::NativeText) + ")";
00206 
00207                         m_shortcut = new QShortcut(key, parent->getCanvas());
00208                         m_shortcut->setContext(context);
00209                     }
00210 
00211                     m_action = new QAction(icon, text2, parent);
00212                     m_action->setToolTip(text3);
00213                 }
00214 
00215                 ~ShortcutAction()
00216                 {
00217                     //do not delete action and shortcut here, since it will be deleted by common parent.
00218                 }
00219 
00220                 void connectTrigger(const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
00221                 {
00222                     if(m_action)
00223                     {
00224                         QObject::connect(m_action, SIGNAL(triggered()), receiver, method, type);
00225                     }
00226                     if(m_shortcut)
00227                     {
00228                         QObject::connect(m_shortcut, SIGNAL(activated()), receiver, method, type);
00229                     }
00230                 }
00231 
00232                 void setEnabled(bool actionEnabled, bool shortcutEnabled)
00233                 {
00234                     if(m_action) 
00235                     {
00236                         m_action->setEnabled(actionEnabled);
00237                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00238                     }
00239                 }
00240 
00241                 void setEnabled(bool enabled)
00242                 {
00243                     setEnabled(enabled,enabled);
00244                 }
00245 
00246                 void setVisible(bool actionVisible, bool shortcutEnabled)
00247                 {
00248                     if(m_action) 
00249                     {
00250                         m_action->setVisible(actionVisible);
00251                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00252                     }
00253                 }
00254 
00255                 void setVisible(bool visible)
00256                 {
00257                     setVisible(visible,visible);
00258                 }
00259 
00260                 QAction* action() { return m_action; }
00261 
00262             private:
00263                 QAction *m_action;
00264                 QShortcut *m_shortcut;
00265             };
00266 
00268 
00273             bool eventFilter(QObject *obj, QEvent *event)
00274             {
00275                 if (event->type() == QEvent::Close)
00276                 {
00277                     closeEvent((QCloseEvent*)event);
00278                     if(testAttribute(Qt::WA_DeleteOnClose) && event->isAccepted() ) //if window should be closed and dockwidget is assigned with WA_DeleteOnClose, delete this dockwidget first.
00279                     {
00280                        deleteLater();
00281                     }
00282                     
00283                     return true;
00284                 }
00285                 else
00286                 {
00287                     return QObject::eventFilter(obj,event);
00288                 }
00289             };
00290 
00291             void init();
00292 
00293             virtual void closeEvent(QCloseEvent *event);
00294 
00295             virtual void createActions() = 0;
00296             virtual void createMenus() = 0;
00297             virtual void createToolBars() = 0;
00298             virtual void createStatusBar() = 0;
00299             virtual void updateActions() {}
00300             virtual void updatePythonActions() = 0;
00301         
00302             Qt::WindowFlags modifyFlags(const Qt::WindowFlags &flags, const Qt::WindowFlags &setFlags, const Qt::WindowFlags &unsetFlags);
00303 
00304             virtual void windowStateChanged( bool /*windowNotToolbox*/ ) {}
00305 
00306             void setContentWidget(QWidget *widget);
00307             inline QWidget* getContentWidget() const { return m_pWindow->centralWidget(); }   
00308             inline QMainWindow* getCanvas() { return m_pWindow; }
00309 
00310             inline bool pythonBusy() const { return m_pythonBusy; }                    
00311             inline bool pythonDebugMode() const { return m_pythonDebugMode; }          
00312             inline bool pythonInWaitingMode() const { return m_pythonInWaitingMode; }  
00314             //RetVal addAndRegisterToolBar(QToolBar* tb, QString key);
00315             //RetVal unregisterToolBar(QString key);
00316             QToolBar* getToolBar(QString key) const;
00317             inline QMenuBar* getMenuBar() const { return (m_pWindow == NULL) ?  NULL : m_pWindow->menuBar(); }
00318 
00319             RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area = Qt::TopToolBarArea, int section = 1);
00320             RetVal removeToolBar(const QString &key);
00321 
00322             QAction *m_actStayOnTop;
00323             QAction *m_actStayOnTopOfApp;
00324 
00325         private:
00326 
00327             void contextMenuEvent(QContextMenuEvent *e)
00328             {
00329                 e->accept();
00330                 qDebug() << "context menu of abstractDockWidget clicked. [placeholder for (un)docking feature.] pos: " << e->pos();
00331             }
00332 
00333             QMainWindow *m_pWindow;
00334 
00335             bool m_docked;                      
00336             bool m_dockAvailable;               
00337             tFloatingStyle m_floatingStyle;     
00338             tMovingStyle m_movingStyle;         
00339             QString m_basicTitle;               
00340             QString m_completeTitle;            
00342             QMap<QString,QToolBar*> m_toolBars; 
00343             QList<Toolbar> m_toolbars;
00344 
00345             bool m_pythonBusy;                  
00346             bool m_pythonDebugMode;             
00347             bool m_pythonInWaitingMode;         
00349             QToolBar* m_dockToolbar;
00350             QAction* m_actDock;
00351             QAction* m_actUndock;
00352 
00353             QWidget* m_overallParent;           
00354             tTopLevelStyle m_recentTopLevelStyle;
00355 
00356             QSize m_oldMinSize;
00357             QSize m_oldMaxSize;
00358             QRect m_lastUndockedSize;
00359     
00360         //signals:
00361         //    void addDockToMainWindow(AbstractDockWidget *widget);       /*!<  signal emitted if widget should be docked to main window */
00362         //    void removeDockFromMainWindow(AbstractDockWidget *widget);  
00363         //    void dockCloseRequest(AbstractDockWidget *widget);
00364 
00365         public slots:
00366             virtual void dockedToMainWindow(AbstractDockWidget * /*widget*/){}
00367             virtual void removedFromMainWindow(AbstractDockWidget * /*widget*/){}
00368             virtual void pythonStateChanged(tPythonTransitions pyTransition);
00369 
00370             void raiseAndActivate();  
00372             void setDockSize(int newWidth, int newHeight);
00373 
00374             void dockWidget();
00375             void undockWidget(bool show_it = true);
00376 
00377         private slots:
00378             void mnuStayOnTop(bool checked);
00379             void mnuStayOnTopOfApp(bool checked);
00380 
00381             void returnToOldMinMaxSizes();
00382     };
00383 
00384 } //end namespace ito
00385 
00386 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends