]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxWorkspaceAction.cxx
Salome HOME
[bos #42871] Clipping plane remains applied after being deleted
[modules/gui.git] / src / Qtx / QtxWorkspaceAction.cxx
1 // Copyright (C) 2007-2024  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:      QtxWorkspaceAction.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "QtxWorkspaceAction.h"
27
28 #include "QtxWorkspace.h"
29
30 #include <QMenu>
31 #include <QMdiSubWindow>
32
33 /*!
34   \class QtxWorkspaceAction
35   \brief Implements actions group for menu Windows with standard operations, like
36          "Cascade", "Tile", "Tile Horizontally", etc.
37 */
38
39 /*!
40   \brief Constructor.
41   \param ws parent workspace
42   \param parent parent object (owner of the action)
43 */
44 QtxWorkspaceAction::QtxWorkspaceAction( QtxWorkspace* ws, QObject* parent )
45 : QtxActionSet( parent ),
46   myWorkspace( ws ),
47   myWindowsFlag( true )
48 {
49   insertAction( new QtxAction( tr( "Arranges the windows as overlapping tiles" ),
50                                tr( "Cascade" ), 0, this ), Cascade );
51   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping tiles" ),
52                                tr( "Tile" ), 0, this ), Tile );
53   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping horizontal tiles" ),
54                                tr( "Tile horizontally" ), 0, this ), HTile );
55   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping vertical tiles" ),
56                                tr( "Tile vertically" ), 0, this ), VTile );
57
58   connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
59
60   setMenuActions( Standard );
61 }
62
63 /*!
64   \brief Destructor.
65 */
66 QtxWorkspaceAction::~QtxWorkspaceAction()
67 {
68 }
69
70 /*!
71   \brief Get workspace.
72   \return parent workspace
73 */
74 QtxWorkspace* QtxWorkspaceAction::workspace() const
75 {
76   return myWorkspace;
77 }
78
79 /*!
80   \brief Set actions to be visible in the menu.
81
82   Actions, which IDs are set in \a flags parameter, will be shown in the
83   menu bar. Other actions will not be shown.
84
85   \param flags ORed together actions flags
86 */
87 void QtxWorkspaceAction::setMenuActions( const int flags )
88 {
89   action( Cascade )->setVisible( flags & Cascade );
90   action( Tile )->setVisible( flags & Tile );
91   action( VTile )->setVisible( flags & VTile );
92   action( HTile )->setVisible( flags & HTile );
93   myWindowsFlag = flags & Windows;
94 }
95
96 /*!
97   \brief Get menu actions which are currently visible in the menu bar.
98   \return ORed together actions flags
99   \sa setMenuActions()
100 */
101 int QtxWorkspaceAction::menuActions() const
102 {
103   int ret = 0;
104   ret = ret | ( action( Cascade )->isVisible() ? Cascade : 0 );
105   ret = ret | ( action( Tile )->isVisible() ? Tile : 0 );
106   ret = ret | ( action( VTile )->isVisible() ? VTile : 0 );
107   ret = ret | ( action( HTile )->isVisible() ? HTile : 0 );
108   ret = ret | ( myWindowsFlag ? Windows : 0 );
109   return ret;
110 }
111
112 /*!
113   \brief Get keyboard accelerator for the specified action.
114   \param id menu action ID
115   \return keyboard accelerator of menu item or 0 if there is no such action
116 */
117 int QtxWorkspaceAction::accel( const int id ) const
118 {
119   int a = 0;
120   if ( action( id ) )
121     a = action( id )->shortcut()[0];
122   return a;
123 }
124
125 /*!
126   \brief Get icon for the specified action.
127
128   If \a id is invalid, null icon is returned.
129
130   \param id menu action ID
131   \return menu item icon
132 */
133 QIcon QtxWorkspaceAction::icon( const int id ) const
134 {
135   QIcon ico;
136   if ( action( id ) )
137     ico = action( id )->icon();
138   return ico;
139 }
140
141 /*!
142   \brief Get menu item text for the specified action.
143   \param id menu action ID
144   \return menu item text or null QString if there is no such action
145 */
146 QString QtxWorkspaceAction::text( const int id ) const
147 {
148   QString txt;
149   if ( action( id ) )
150     txt = action( id )->text();
151   return txt;
152 }
153
154 /*!
155   \brief Get status bar tip for the specified action.
156   \param id menu action ID
157   \return status bar tip menu item or null QString if there is no such action
158 */
159 QString QtxWorkspaceAction::statusTip( const int id ) const
160 {
161   QString txt;
162   if ( action( id ) )
163     txt = action( id )->statusTip();
164   return txt;
165 }
166
167 /*!
168   \brief Set menu item icon for the specified action.
169   \param id menu action ID
170   \param ico new menu item icon
171 */
172 void QtxWorkspaceAction::setIcon( const int id, const QIcon& icon )
173 {
174   if ( action( id ) )
175     action( id )->setIcon( icon );
176 }
177
178 /*!
179   \brief Set menu item text for the specified action.
180   \param id menu action ID
181   \param txt new menu item text
182 */
183 void QtxWorkspaceAction::setText( const int id, const QString& txt )
184 {
185   if ( action( id ) )
186     action( id )->setText( txt );
187 }
188
189 /*!
190   \brief Set menu item status bar tip for the specified action.
191   \param id menu action ID
192   \param txt new menu item status bar tip
193 */
194 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
195 {
196   if ( action( id ) )
197     action( id )->setStatusTip( txt );
198 }
199
200 /*!
201   \brief Process action activated by the user.
202   \param type action ID
203 */
204 void QtxWorkspaceAction::perform( const int type )
205 {
206   switch ( type )
207   {
208   case Cascade:
209     cascade();
210     break;
211   case Tile:
212     tile();
213     break;
214   case VTile:
215     tileVertical();
216     break;
217   case HTile:
218     tileHorizontal();
219     break;
220   }
221 }
222
223 /*!
224   \brief Tile child windows in the workspace.
225 */
226 void QtxWorkspaceAction::tile()
227 {
228   QtxWorkspace* ws = workspace();
229   if ( ws )
230     ws->tileSubWindows();
231 }
232
233 /*!
234   \brief Cascade child windows in the workspace.
235 */
236 void QtxWorkspaceAction::cascade()
237 {
238   QtxWorkspace* ws = workspace();
239   if ( !ws )
240     return;
241
242   ws->cascadeSubWindows();
243
244         int w = ws->width();
245         int h = ws->height();
246
247         QList<QMdiSubWindow *> winList = ws->subWindowList();
248   for ( QList<QMdiSubWindow *>::iterator it = winList.begin(); it != winList.end(); ++it )
249                 (*it)->resize( int( w * 0.8 ), int( h * 0.8 ) );
250 }
251
252 /*!
253   \brief Tile child windows in the workspace in the vertical direction.
254 */
255 void QtxWorkspaceAction::tileVertical()
256 {
257   QtxWorkspace* ws = workspace();
258   if ( ws )
259     ws->tileVertical();
260 }
261
262 /*!
263   \brief Tile child windows in the workspace in the horizontal direction.
264 */
265 void QtxWorkspaceAction::tileHorizontal()
266 {
267   QtxWorkspace* ws = workspace();
268   if ( ws )
269     ws->tileHorizontal();
270 }
271
272 /*!
273   \brief Called when action is added to the menu bar.
274   \param w menu bar widget this action is being added to
275 */
276 void QtxWorkspaceAction::addedTo( QWidget* w )
277 {
278   QtxActionSet::addedTo( w );
279
280   QMenu* pm = ::qobject_cast<QMenu*>( w );
281   if ( pm )
282     connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
283 }
284
285 /*!
286   \brief Called when action is removed from the menu bar.
287   \param w menu bar widget this action is being removed from
288 */
289 void QtxWorkspaceAction::removedFrom( QWidget* w )
290 {
291   QtxActionSet::removedFrom( w );
292
293   QMenu* pm = ::qobject_cast<QMenu*>( w );
294   if ( pm )
295     disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
296 }
297
298 /*!
299   \brief Update all menu action state.
300 */
301 void QtxWorkspaceAction::updateContent()
302 {
303   bool hasWindows = workspace() && workspace()->subWindowList().count() > 0;
304   action( Cascade )->setEnabled( hasWindows );
305   action( Tile )->setEnabled( hasWindows );
306   action( HTile )->setEnabled( hasWindows );
307   action( VTile )->setEnabled( hasWindows );
308
309   updateWindows();
310 }
311
312 /*!
313   \brief Update actions which refer to the opened child windows.
314 */
315 void QtxWorkspaceAction::updateWindows()
316 {
317   QtxWorkspace* ws = workspace();
318   if ( !ws )
319     return;
320
321   QList<QAction*> lst = actions();
322   for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
323   {
324     int id = actionId( *it );
325     if ( id >= Windows )
326       removeAction( *it );
327   }
328
329   bool base = action( Cascade )->isVisible() || action( Tile )->isVisible() ||
330               action( HTile )->isVisible() || action( VTile )->isVisible();
331
332   QList<QAction*> items;
333   QMap<QAction*, int> map;
334   if ( menuActions() & Windows )
335   {
336     int index = 1;
337     QList<QMdiSubWindow *> wList = ws->subWindowList();
338     for ( QList<QMdiSubWindow *>::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
339     {
340       QWidget* wid = *it;
341       QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
342       a->setChecked( wid == ws->activeSubWindow() );
343       items.append( a );
344       map.insert( a, Windows + index );
345     }
346
347     if ( base && !items.isEmpty() )
348     {
349       QAction* sep = new QtxAction( this );
350       sep->setSeparator( true );
351       items.prepend( sep );
352       map.insert( sep, Windows );
353     }
354   }
355
356   if ( !items.isEmpty() )
357     insertActions( items );
358
359   for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
360     setActionId( itr.key(), itr.value() );
361 }
362
363 /*!
364   \brief Called when parent menu is about to show.
365
366   Updates all menu items.
367 */
368 void QtxWorkspaceAction::onAboutToShow()
369 {
370   QMenu* pm = ::qobject_cast<QMenu*>( sender() );
371   if ( pm )
372     updateContent();
373 }
374
375 /*!
376   \brief Called when menu item corresponding to some child window is activated.
377
378   Activates correposponding child window.
379
380   \param idx menu item index
381 */
382 void QtxWorkspaceAction::activateItem( const int idx )
383 {
384   QtxWorkspace* ws = workspace();
385   if ( !ws )
386     return;
387
388   QList<QMdiSubWindow *> wList = ws->subWindowList();
389   if ( idx >= 0 && idx < (int)wList.count() )
390     wList.at( idx )->setFocus();
391 }
392
393 /*!
394   \brief Called when menu item is activated by the user.
395
396   Perform the corresponding action.
397
398   \param id menu item identifier
399 */
400 void QtxWorkspaceAction::onTriggered( int id )
401 {
402   if ( id < Windows )
403     perform( id );
404   else
405     activateItem( id - Windows - 1 );
406 }