Salome HOME
caba437b64980f2fcf94bf3b22abd5876b1bed48
[modules/gui.git] / src / Qtx / QtxDockAction.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:      QtxDockAction.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxDockAction.h"
26
27 #include <QMenu>
28 #include <QToolBar>
29 #include <QDockWidget>
30 #include <QMainWindow>
31
32 /*!
33   \class QtxDockAction
34   \brief Dockable windows & toolbars list action.
35
36   Implements the action which provides the popup menu with the list
37   of toolbars and/or dockable windows list owned by the main window.
38   This action can be used, for example, in the menu "Windows".
39 */
40
41 /*!
42   \brief Constructor.
43   \param mw parent main window
44 */
45 QtxDockAction::QtxDockAction( QMainWindow* mw )
46 : QtxAction( "Windows and Toolbars", "Windows and Toolbars", 0, mw ),
47   myType( Both ),
48   myMain( mw )
49 {
50   initialize();
51 }
52
53 /*!
54   \brief Constructor.
55   \param text description (tooltip) text
56   \param menuText menu text
57   \param mw parent main window
58 */
59 QtxDockAction::QtxDockAction( const QString& text, const QString& menuText, QMainWindow* mw )
60 : QtxAction( text, menuText, 0, mw ),
61   myType( Both ),
62   myMain( mw )
63 {
64   initialize();
65 }
66
67 /*!
68   \brief Constructor.
69   \param text description (tooltip) text
70   \param icon action icon
71   \param menuText menu text
72   \param mw parent main window
73 */
74 QtxDockAction::QtxDockAction( const QString& text, const QIcon& icon, const QString& menuText, QMainWindow* mw )
75 : QtxAction( text, icon, menuText, 0, mw ),
76   myType( Both ),
77   myMain( mw )
78 {
79   initialize();
80 }
81
82 /*!
83   \brief Desctructor
84   
85   Does nothing currently.
86 */
87 QtxDockAction::~QtxDockAction()
88 {
89 }
90
91 /*!
92   \brief Get parent main window.
93   \return main window pointer.
94 */
95 QMainWindow* QtxDockAction::mainWindow() const
96 {
97   return myMain;
98 }
99
100 /*!
101   \brief Get dock action type.
102   \return dock type (QtxDockAction::DockType)
103 */
104 int QtxDockAction::dockType() const
105 {
106   return myType;
107 }
108
109 /*!
110   \brief Set dock action type.
111   \param type dock type (QtxDockAction::DockType)
112 */
113 void QtxDockAction::setDockType( const int type )
114 {
115   myType = type;
116 }
117
118 /*!
119   \brief Prepare popup menu with dock windows list when 
120          parent popup menu is shown.
121 */
122 void QtxDockAction::onAboutToShow()
123 {
124   updateMenu();
125   setVisible( menu() && !menu()->isEmpty() );
126 }
127
128 /*!
129   \brief Get all toolbars owned by parent main window.
130   \param lst returned list of all toolbars owned by main window
131 */
132 void QtxDockAction::toolBars( QList<QToolBar*>& lst ) const
133 {
134   lst.clear();
135
136   QMainWindow* mw = mainWindow();
137   if ( !mw )
138     return;
139
140   QList<QToolBar*> toolbars = qFindChildren<QToolBar*>( mw );
141   for ( QList<QToolBar*>::iterator it = toolbars.begin(); it != toolbars.end(); ++it )
142   {
143     QToolBar* tb = *it;
144     if ( tb->parentWidget() == mw )
145       lst.append( tb );
146   }
147 }
148
149 /*!
150   \brief Get all dockable windows owned by parent main window.
151   \param lst returned list of all dockable windows owned by main window
152 */
153 void QtxDockAction::dockWidgets( QList<QDockWidget*>& lst ) const
154 {
155   lst.clear();
156
157   QMainWindow* mw = mainWindow();
158   if ( !mw )
159     return;
160
161   QList<QDockWidget*> dockwidgets = qFindChildren<QDockWidget*>( mw );
162   for ( QList<QDockWidget*>::iterator it = dockwidgets.begin(); it != dockwidgets.end(); ++it )
163   {
164     QDockWidget* dw = *it;
165     if ( dw->parentWidget() == mw )
166       lst.append( dw );
167   }
168 }
169
170 /*
171 void QtxDockAction::updateInfo( QDockWindow* dw )
172 {
173   QAction* a = action( dw );
174   if ( !a )
175     return;
176
177   a->setText( dw->caption() );
178   a->setMenuText( dw->caption() );
179
180   if ( isToolBar( dw ) )
181     a->setStatusTip( tr( "Toggles toolbar \"%1\" on/off" ).arg( dw->caption() ) );
182   else
183     a->setStatusTip( tr( "Toggles window \"%1\" on/off" ).arg( dw->caption() ) );
184
185   const QPixmap* icon = dw->icon();
186   if ( icon )
187     a->setIconSet( *icon );
188 }
189 */
190
191 /*!
192   \brief Customize action adding to the widget operation.
193   
194   Called when the action is added to the widget.
195   Reimplemented from QtxAction class.
196
197   \param w widget this action is added to (menu or toolbar)
198 */
199 void QtxDockAction::addedTo( QWidget* w )
200 {
201   if ( w->inherits( "QMenu" ) )
202     connect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
203 }
204
205 /*!
206   \brief Customize action removing from the widget operation.
207   
208   Called when the action is removed from the widget.
209   Reimplemented from QtxAction class.
210
211   \param w widget this action is removed from to (menu or toolbar)
212 */
213 void QtxDockAction::removedFrom( QWidget* w )
214 {
215   if ( w->inherits( "QMenu" ) )
216     disconnect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
217 }
218
219 /*!
220   \brief Initialize the action.
221 */
222 void QtxDockAction::initialize()
223 {
224   setMenu( new QMenu( 0 ) );
225 }
226
227 /*!
228   \brief Update action child popup menu.
229 */
230 void QtxDockAction::updateMenu()
231 {
232   QMenu* pm = menu();
233   if ( !pm )
234     return;
235
236   pm->clear();
237
238   if ( dockType() == DockWidget || dockType() == Both )
239   {
240     QList<QDockWidget*> dwList;
241     dockWidgets( dwList );
242     for ( QList<QDockWidget*>::iterator it = dwList.begin(); it != dwList.end(); ++it )
243       pm->addAction( (*it)->toggleViewAction() );
244   }
245
246   pm->addSeparator();
247
248   if ( dockType() == ToolBar || dockType() == Both )
249   {
250     QList<QToolBar*> tbList;
251     toolBars( tbList );
252     for ( QList<QToolBar*>::iterator it = tbList.begin(); it != tbList.end(); ++it )
253       pm->addAction( (*it)->toggleViewAction() );
254   }
255
256   Qtx::simplifySeparators( pm );
257 }