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