Salome HOME
updated copyright message
[modules/gui.git] / src / Qtx / QtxDockAction.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
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
23 // File:      QtxDockAction.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "QtxDockAction.h"
27
28 #include <QMenu>
29 #include <QToolBar>
30 #include <QDockWidget>
31 #include <QMainWindow>
32
33 /*!
34   \class QtxDockAction
35   \brief Dockable windows & toolbars list action.
36
37   Implements the action which provides the popup menu with the list
38   of toolbars and/or dockable windows list owned by the main window.
39   This action can be used, for example, in the menu "Windows".
40 */
41
42 /*!
43   \brief Constructor.
44   \param mw parent main window
45 */
46 QtxDockAction::QtxDockAction( QMainWindow* mw )
47 : QtxAction( "Windows and Toolbars", "Windows and Toolbars", 0, mw ),
48   myType( Both ),
49   myMain( mw )
50 {
51   initialize();
52 }
53
54 /*!
55   \brief Constructor.
56   \param text description (tooltip) text
57   \param menuText menu text
58   \param mw parent main window
59 */
60 QtxDockAction::QtxDockAction( const QString& text, const QString& menuText, QMainWindow* mw )
61 : QtxAction( text, menuText, 0, mw ),
62   myType( Both ),
63   myMain( mw )
64 {
65   initialize();
66 }
67
68 /*!
69   \brief Constructor.
70   \param text description (tooltip) text
71   \param icon action icon
72   \param menuText menu text
73   \param mw parent main window
74 */
75 QtxDockAction::QtxDockAction( const QString& text, const QIcon& icon, const QString& menuText, QMainWindow* mw )
76 : QtxAction( text, icon, menuText, 0, mw ),
77   myType( Both ),
78   myMain( mw )
79 {
80   initialize();
81 }
82
83 /*!
84   \brief Desctructor
85   
86   Does nothing currently.
87 */
88 QtxDockAction::~QtxDockAction()
89 {
90   // to avoid memory leak
91   if( QMenu* aMenu = menu() )
92   {
93     delete aMenu;
94     aMenu = 0;
95   }
96 }
97
98 /*!
99   \brief Update associated menu
100 */
101 void QtxDockAction::update()
102 {
103   updateMenu();
104 }
105
106 /*!
107   \brief Get parent main window.
108   \return main window pointer.
109 */
110 QMainWindow* QtxDockAction::mainWindow() const
111 {
112   return myMain;
113 }
114
115 /*!
116   \brief Get dock action type.
117   \return dock type (QtxDockAction::DockType)
118 */
119 int QtxDockAction::dockType() const
120 {
121   return myType;
122 }
123
124 /*!
125   \brief Set dock action type.
126   \param type dock type (QtxDockAction::DockType)
127 */
128 void QtxDockAction::setDockType( const int type )
129 {
130   myType = type;
131 }
132
133 /*!
134   \brief Prepare popup menu with dock windows list when 
135          parent popup menu is shown.
136 */
137 void QtxDockAction::onAboutToShow()
138 {
139   updateMenu();
140   setVisible( menu() && !menu()->isEmpty() );
141 }
142
143 /*!
144   \brief Get all toolbars owned by parent main window.
145   \param lst returned list of all toolbars owned by main window
146 */
147 void QtxDockAction::toolBars( QList<QToolBar*>& lst ) const
148 {
149   lst.clear();
150
151   QMainWindow* mw = mainWindow();
152   if ( !mw )
153     return;
154
155   QList<QToolBar*> toolbars = mw->findChildren<QToolBar*>();
156   for ( QList<QToolBar*>::iterator it = toolbars.begin(); it != toolbars.end(); ++it )
157   {
158     QToolBar* tb = *it;
159     if ( tb->parentWidget() == mw )
160       lst.append( tb );
161   }
162 }
163
164 /*!
165   \brief Get all dockable windows owned by parent main window.
166   \param lst returned list of all dockable windows owned by main window
167 */
168 void QtxDockAction::dockWidgets( QList<QDockWidget*>& lst ) const
169 {
170   lst.clear();
171
172   QMainWindow* mw = mainWindow();
173   if ( !mw )
174     return;
175
176   QList<QDockWidget*> dockwidgets = mw->findChildren<QDockWidget*>();
177   for ( QList<QDockWidget*>::iterator it = dockwidgets.begin(); it != dockwidgets.end(); ++it )
178   {
179     QDockWidget* dw = *it;
180     if ( dw->parentWidget() == mw )
181       lst.append( dw );
182   }
183 }
184
185 /*
186 void QtxDockAction::updateInfo( QDockWindow* dw )
187 {
188   QAction* a = action( dw );
189   if ( !a )
190     return;
191
192   a->setText( dw->caption() );
193   a->setMenuText( dw->caption() );
194
195   if ( isToolBar( dw ) )
196     a->setStatusTip( tr( "Toggles toolbar \"%1\" on/off" ).arg( dw->caption() ) );
197   else
198     a->setStatusTip( tr( "Toggles window \"%1\" on/off" ).arg( dw->caption() ) );
199
200   const QPixmap* icon = dw->icon();
201   if ( icon )
202     a->setIconSet( *icon );
203 }
204 */
205
206 /*!
207   \brief Customize action adding to the widget operation.
208   
209   Called when the action is added to the widget.
210   Reimplemented from QtxAction class.
211
212   \param w widget this action is added to (menu or toolbar)
213 */
214 void QtxDockAction::addedTo( QWidget* w )
215 {
216   if ( w->inherits( "QMenu" ) )
217     connect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
218 }
219
220 /*!
221   \brief Customize action removing from the widget operation.
222   
223   Called when the action is removed from the widget.
224   Reimplemented from QtxAction class.
225
226   \param w widget this action is removed from to (menu or toolbar)
227 */
228 void QtxDockAction::removedFrom( QWidget* w )
229 {
230   if ( w->inherits( "QMenu" ) )
231     disconnect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
232 }
233
234 /*!
235   \brief Initialize the action.
236 */
237 void QtxDockAction::initialize()
238 {
239   setMenu( new QMenu( 0 ) );
240 }
241
242 /*!
243   \brief Update action child popup menu.
244 */
245 void QtxDockAction::updateMenu()
246 {
247   QMenu* pm = menu();
248   if ( !pm )
249     return;
250
251   pm->clear();
252
253   if ( dockType() == DockWidget || dockType() == Both )
254   {
255     QList<QDockWidget*> dwList;
256     dockWidgets( dwList );
257     for ( QList<QDockWidget*>::iterator it = dwList.begin(); it != dwList.end(); ++it )
258       pm->addAction( (*it)->toggleViewAction() );
259   }
260
261   pm->addSeparator();
262
263   if ( dockType() == ToolBar || dockType() == Both )
264   {
265     QList<QToolBar*> tbList;
266     toolBars( tbList );
267     for ( QList<QToolBar*>::iterator it = tbList.begin(); it != tbList.end(); ++it )
268       pm->addAction( (*it)->toggleViewAction() );
269   }
270
271   Qtx::simplifySeparators( pm );
272 }