1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: QtxWorkspaceAction.cxx
24 // Author: Sergey TELKOV
26 #include "QtxWorkspaceAction.h"
28 #include "QtxWorkspace.h"
31 #include <QMdiSubWindow>
34 \class QtxWorkspaceAction
35 \brief Implements actions group for menu Windows with standard operations, like
36 "Cascade", "Tile", "Tile Horizontally", etc.
41 \param ws parent workspace
42 \param parent parent object (owner of the action)
44 QtxWorkspaceAction::QtxWorkspaceAction( QtxWorkspace* ws, QObject* parent )
45 : QtxActionSet( parent ),
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 );
58 connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
60 setMenuActions( Standard );
66 QtxWorkspaceAction::~QtxWorkspaceAction()
72 \return parent workspace
74 QtxWorkspace* QtxWorkspaceAction::workspace() const
80 \brief Set actions to be visible in the menu.
82 Actions, which IDs are set in \a flags parameter, will be shown in the
83 menu bar. Other actions will not be shown.
85 \param flags ORed together actions flags
87 void QtxWorkspaceAction::setMenuActions( const int flags )
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;
97 \brief Get menu actions which are currently visible in the menu bar.
98 \return ORed together actions flags
101 int QtxWorkspaceAction::menuActions() const
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 );
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
117 int QtxWorkspaceAction::accel( const int id ) const
121 a = action( id )->shortcut()[0];
126 \brief Get icon for the specified action.
128 If \a id is invalid, null icon is returned.
130 \param id menu action ID
131 \return menu item icon
133 QIcon QtxWorkspaceAction::icon( const int id ) const
137 ico = action( id )->icon();
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
146 QString QtxWorkspaceAction::text( const int id ) const
150 txt = action( id )->text();
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
159 QString QtxWorkspaceAction::statusTip( const int id ) const
163 txt = action( id )->statusTip();
168 \brief Set menu item icon for the specified action.
169 \param id menu action ID
170 \param ico new menu item icon
172 void QtxWorkspaceAction::setIcon( const int id, const QIcon& icon )
175 action( id )->setIcon( icon );
179 \brief Set menu item text for the specified action.
180 \param id menu action ID
181 \param txt new menu item text
183 void QtxWorkspaceAction::setText( const int id, const QString& txt )
186 action( id )->setText( txt );
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
194 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
197 action( id )->setStatusTip( txt );
201 \brief Process action activated by the user.
202 \param type action ID
204 void QtxWorkspaceAction::perform( const int type )
224 \brief Tile child windows in the workspace.
226 void QtxWorkspaceAction::tile()
228 QtxWorkspace* ws = workspace();
230 ws->tileSubWindows();
234 \brief Cascade child windows in the workspace.
236 void QtxWorkspaceAction::cascade()
238 QtxWorkspace* ws = workspace();
242 ws->cascadeSubWindows();
245 int h = ws->height();
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 ) );
253 \brief Tile child windows in the workspace in the vertical direction.
255 void QtxWorkspaceAction::tileVertical()
257 QtxWorkspace* ws = workspace();
263 \brief Tile child windows in the workspace in the horizontal direction.
265 void QtxWorkspaceAction::tileHorizontal()
267 QtxWorkspace* ws = workspace();
269 ws->tileHorizontal();
273 \brief Called when action is added to the menu bar.
274 \param w menu bar widget this action is being added to
276 void QtxWorkspaceAction::addedTo( QWidget* w )
278 QtxActionSet::addedTo( w );
280 QMenu* pm = ::qobject_cast<QMenu*>( w );
282 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
286 \brief Called when action is removed from the menu bar.
287 \param w menu bar widget this action is being removed from
289 void QtxWorkspaceAction::removedFrom( QWidget* w )
291 QtxActionSet::removedFrom( w );
293 QMenu* pm = ::qobject_cast<QMenu*>( w );
295 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
299 \brief Update all menu action state.
301 void QtxWorkspaceAction::updateContent()
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 );
313 \brief Update actions which refer to the opened child windows.
315 void QtxWorkspaceAction::updateWindows()
317 QtxWorkspace* ws = workspace();
321 QList<QAction*> lst = actions();
322 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
324 int id = actionId( *it );
329 bool base = action( Cascade )->isVisible() || action( Tile )->isVisible() ||
330 action( HTile )->isVisible() || action( VTile )->isVisible();
332 QList<QAction*> items;
333 QMap<QAction*, int> map;
334 if ( menuActions() & Windows )
337 QList<QMdiSubWindow *> wList = ws->subWindowList();
338 for ( QList<QMdiSubWindow *>::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
341 QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
342 a->setChecked( wid == ws->activeSubWindow() );
344 map.insert( a, Windows + index );
347 if ( base && !items.isEmpty() )
349 QAction* sep = new QtxAction( this );
350 sep->setSeparator( true );
351 items.prepend( sep );
352 map.insert( sep, Windows );
356 if ( !items.isEmpty() )
357 insertActions( items );
359 for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
360 setActionId( itr.key(), itr.value() );
364 \brief Called when parent menu is about to show.
366 Updates all menu items.
368 void QtxWorkspaceAction::onAboutToShow()
370 QMenu* pm = ::qobject_cast<QMenu*>( sender() );
376 \brief Called when menu item corresponding to some child window is activated.
378 Activates correposponding child window.
380 \param idx menu item index
382 void QtxWorkspaceAction::activateItem( const int idx )
384 QtxWorkspace* ws = workspace();
388 QList<QMdiSubWindow *> wList = ws->subWindowList();
389 if ( idx >= 0 && idx < (int)wList.count() )
390 wList.at( idx )->setFocus();
394 \brief Called when menu item is activated by the user.
396 Perform the corresponding action.
398 \param id menu item identifier
400 void QtxWorkspaceAction::onTriggered( int id )
405 activateItem( id - Windows - 1 );