itom 1.0.14
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         public:
00049 
00050             enum tFloatingStyle { floatingNone, floatingStandard, floatingWindow }; 
00051             enum tMovingStyle { movingDisabled, movingEnabled };    
00052             enum tTopLevelStyle { topLevelOverall, topLevelParentOnly, topLevelNothing };
00053 
00054             struct Toolbar
00055             {
00056                 Qt::ToolBarArea area;
00057                 int section;
00058                 QString key;
00059                 QToolBar *tb;
00060             };
00061 
00062             AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title = QString(), QWidget *parent = 0);
00063             virtual ~AbstractDockWidget();
00064 
00065             inline bool docked() const { return m_docked; }    
00067             RetVal setAdvancedWindowTitle( QString newCompleteTitle = QString(), bool appendToBasicTitle = true );
00068 
00069             RetVal setTopLevel( tTopLevelStyle topLevel );
00070 
00071             void setParent ( QWidget * parent ) { m_overallParent = parent; QDockWidget::setParent(parent); }
00072 
00073             QWidget *getActiveInstance() 
00074             { 
00075                 if(!m_docked && m_floatingStyle == floatingWindow)
00076                 {
00077                     return m_pWindow;
00078                 }
00079                 return this;
00080             }
00081 
00082             void setVisible(bool visible);
00083    
00084         protected:
00085 
00086             class ShortcutAction : public QObject
00087             {
00088             public:
00089                 ShortcutAction(const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00090                 {
00091                     m_action = new QAction(text, parent);
00092                 }
00093 
00094                 ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
00095                 {
00096                     m_action = new QAction(icon, text, parent);
00097                 }
00098 
00099                 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)
00100                 {
00101                     QString text2 = text;
00102                     QString text3 = text;
00103                     text2.append( "\t" );
00104                     text2.append( key.toString(QKeySequence::NativeText) );
00105                     text3.append( " (" );
00106                     text3.append( key.toString(QKeySequence::NativeText) );
00107                     text3.append( ")" );
00108                     m_action = new QAction(icon, text2, parent);
00109                     m_action->setToolTip(text3);
00110                     m_shortcut = new QShortcut(key, parent->getCanvas());
00111                     m_shortcut->setContext(context);
00112                 }
00113 
00114                 ~ShortcutAction()
00115                 {
00116                     //do not delete action and shortcut here, since it will be deleted by common parent.
00117                 }
00118 
00119                 void connectTrigger(const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
00120                 {
00121                     if(m_action)
00122                     {
00123                         QObject::connect(m_action, SIGNAL(triggered()), receiver, method, type);
00124                     }
00125                     if(m_shortcut)
00126                     {
00127                         QObject::connect(m_shortcut, SIGNAL(activated()), receiver, method, type);
00128                     }
00129                 }
00130 
00131                 void setEnabled(bool actionEnabled, bool shortcutEnabled)
00132                 {
00133                     if(m_action) 
00134                     {
00135                         m_action->setEnabled(actionEnabled);
00136                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00137                     }
00138                 }
00139 
00140                 void setEnabled(bool enabled)
00141                 {
00142                     setEnabled(enabled,enabled);
00143                 }
00144 
00145                 void setVisible(bool actionVisible, bool shortcutEnabled)
00146                 {
00147                     if(m_action) 
00148                     {
00149                         m_action->setVisible(actionVisible);
00150                         if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
00151                     }
00152                 }
00153 
00154                 void setVisible(bool visible)
00155                 {
00156                     setVisible(visible,visible);
00157                 }
00158 
00159                 QAction* action() { return m_action; }
00160 
00161             private:
00162                 QAction *m_action;
00163                 QShortcut *m_shortcut;
00164             };
00165 
00167 
00172             bool eventFilter(QObject *obj, QEvent *event)
00173             {
00174                 if (event->type() == QEvent::Close)
00175                 {
00176                     closeEvent((QCloseEvent*)event);
00177                     if(testAttribute(Qt::WA_DeleteOnClose) && event->isAccepted() ) //if window should be closed and dockwidget is assigned with WA_DeleteOnClose, delete this dockwidget first.
00178                     {
00179                        deleteLater();
00180                     }
00181                     
00182                     return true;
00183                 }
00184                 else
00185                 {
00186                     return QObject::eventFilter(obj,event);
00187                 }
00188             };
00189 
00190             void init();
00191             void resizeDockWidget(int width, int height);
00192 
00193             virtual void closeEvent(QCloseEvent *event);
00194 
00195             virtual void createActions() = 0;
00196             virtual void createMenus() = 0;
00197             virtual void createToolBars() = 0;
00198             virtual void createStatusBar() = 0;
00199             virtual void updateActions() {}
00200             virtual void updatePythonActions() = 0;
00201 
00202             virtual void windowStateChanged( bool /*windowNotToolbox*/ ) {}
00203 
00204             void setContentWidget(QWidget *widget);
00205             inline QWidget* getContentWidget() const { return m_pWindow->centralWidget(); }   
00206             inline QMainWindow* getCanvas() { return m_pWindow; }
00207 
00208             inline bool pythonBusy() const { return m_pythonBusy; }                    
00209             inline bool pythonDebugMode() const { return m_pythonDebugMode; }          
00210             inline bool pythonInWaitingMode() const { return m_pythonInWaitingMode; }  
00212             //RetVal addAndRegisterToolBar(QToolBar* tb, QString key);
00213             //RetVal unregisterToolBar(QString key);
00214             QToolBar* getToolBar(QString key) const;
00215             inline QMenuBar* getMenuBar() const { return (m_pWindow == NULL) ?  NULL : m_pWindow->menuBar(); }
00216 
00217             RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area = Qt::TopToolBarArea, int section = 1);
00218             RetVal removeToolBar(const QString &key);
00219 
00220             QAction *m_actStayOnTop;
00221             QAction *m_actStayOnTopOfApp;
00222 
00223         private:
00224 
00225             void contextMenuEvent(QContextMenuEvent *e)
00226             {
00227                 e->accept();
00228                 qDebug() << "context menu of abstractDockWidget clicked. [placeholder for (un)docking feature.] pos: " << e->pos();
00229             }
00230 
00231             QMainWindow *m_pWindow;
00232 
00233             bool m_docked;                      
00234             bool m_dockAvailable;               
00235             tFloatingStyle m_floatingStyle;     
00236             tMovingStyle m_movingStyle;         
00237             QString m_basicTitle;               
00238             QString m_completeTitle;            
00240             QMap<QString,QToolBar*> m_toolBars; 
00241             QList<Toolbar> m_toolbars;
00242 
00243             bool m_pythonBusy;                  
00244             bool m_pythonDebugMode;             
00245             bool m_pythonInWaitingMode;         
00247             QToolBar* m_dockToolbar;
00248             QAction* m_actDock;
00249             QAction* m_actUndock;
00250 
00251             QWidget* m_overallParent;           
00252             tTopLevelStyle m_recentTopLevelStyle;
00253 
00254                         QSize m_oldMinSize;
00255                         QSize m_oldMaxSize;
00256             QRect m_lastUndockedSize;
00257     
00258         //signals:
00259         //    void addDockToMainWindow(AbstractDockWidget *widget);       /*!<  signal emitted if widget should be docked to main window */
00260         //    void removeDockFromMainWindow(AbstractDockWidget *widget);  
00261         //    void dockCloseRequest(AbstractDockWidget *widget);
00262 
00263         public slots:
00264             virtual void dockedToMainWindow(AbstractDockWidget * /*widget*/){}
00265             virtual void removedFromMainWindow(AbstractDockWidget * /*widget*/){}
00266             virtual void pythonStateChanged(tPythonTransitions pyTransition);
00267 
00268             void raiseAndActivate();  
00270                         void setDockSize(int newWidth, int newHeight);
00271 
00272             void dockWidget();
00273             void undockWidget();
00274 
00275         private slots:
00276             void mnuStayOnTop(bool checked);
00277             void mnuStayOnTopOfApp(bool checked);
00278 
00279                         void returnToOldMinMaxSizes();
00280     
00281     };
00282 
00283 } //end namespace ito
00284 
00285 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends