1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, 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.
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 <QWidgetList>
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();
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 keyboard accelerator for the specified action.
169 \param id menu action ID
170 \param a new keyboard accelerator
172 void QtxWorkspaceAction::setAccel( const int id, const int a )
175 action( id )->setShortcut( a );
179 \brief Set menu item icon for the specified action.
180 \param id menu action ID
181 \param ico new menu item icon
183 void QtxWorkspaceAction::setIcon( const int id, const QIcon& icon )
186 action( id )->setIcon( icon );
190 \brief Set menu item text for the specified action.
191 \param id menu action ID
192 \param txt new menu item text
194 void QtxWorkspaceAction::setText( const int id, const QString& txt )
197 action( id )->setText( txt );
201 \brief Set menu item status bar tip for the specified action.
202 \param id menu action ID
203 \param txt new menu item status bar tip
205 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
208 action( id )->setStatusTip( txt );
212 \brief Process action activated by the user.
213 \param type action ID
215 void QtxWorkspaceAction::perform( const int type )
235 \brief Tile child windows in the workspace.
237 void QtxWorkspaceAction::tile()
239 QtxWorkspace* ws = workspace();
245 \brief Cascade child windows in the workspace.
247 void QtxWorkspaceAction::cascade()
249 QtxWorkspace* ws = workspace();
256 int h = ws->height();
258 QWidgetList winList = ws->windowList();
259 for ( QWidgetList::iterator it = winList.begin(); it != winList.end(); ++it )
260 (*it)->resize( int( w * 0.8 ), int( h * 0.8 ) );
264 \brief Tile child windows in the workspace in the vertical direction.
266 void QtxWorkspaceAction::tileVertical()
268 QtxWorkspace* ws = workspace();
274 \brief Tile child windows in the workspace in the horizontal direction.
276 void QtxWorkspaceAction::tileHorizontal()
278 QtxWorkspace* ws = workspace();
280 ws->tileHorizontal();
284 \brief Called when action is added to the menu bar.
285 \param w menu bar widget this action is being added to
287 void QtxWorkspaceAction::addedTo( QWidget* w )
289 QtxActionSet::addedTo( w );
291 QMenu* pm = ::qobject_cast<QMenu*>( w );
293 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
297 \brief Called when action is removed from the menu bar.
298 \param w menu bar widget this action is being removed from
300 void QtxWorkspaceAction::removedFrom( QWidget* w )
302 QtxActionSet::removedFrom( w );
304 QMenu* pm = ::qobject_cast<QMenu*>( w );
306 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
310 \brief Update all menu action state.
312 void QtxWorkspaceAction::updateContent()
314 bool count = workspace() ? workspace()->windowList().count() : 0;
315 action( Cascade )->setEnabled( count );
316 action( Tile )->setEnabled( count );
317 action( HTile )->setEnabled( count );
318 action( VTile )->setEnabled( count );
324 \brief Update actions which refer to the opened child windows.
326 void QtxWorkspaceAction::updateWindows()
328 QtxWorkspace* ws = workspace();
332 QList<QAction*> lst = actions();
333 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
335 int id = actionId( *it );
340 bool base = action( Cascade )->isVisible() || action( Tile )->isVisible() ||
341 action( HTile )->isVisible() || action( VTile )->isVisible();
343 QList<QAction*> items;
344 QMap<QAction*, int> map;
345 if ( menuActions() & Windows )
348 QWidgetList wList = ws->windowList();
349 for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
352 QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
353 a->setChecked( wid == ws->activeWindow() );
355 map.insert( a, Windows + index );
358 if ( base && !items.isEmpty() )
360 QAction* sep = new QtxAction( this );
361 sep->setSeparator( true );
362 items.prepend( sep );
363 map.insert( sep, Windows );
367 if ( !items.isEmpty() )
368 insertActions( items );
370 for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
371 setActionId( itr.key(), itr.value() );
375 \brief Called when parent menu is about to show.
377 Updates all menu items.
379 void QtxWorkspaceAction::onAboutToShow()
381 QMenu* pm = ::qobject_cast<QMenu*>( sender() );
387 \brief Called when menu item corresponding to some child window is activated.
389 Activates correposponding child window.
391 \param idx menu item index
393 void QtxWorkspaceAction::activateItem( const int idx )
395 QtxWorkspace* ws = workspace();
399 QWidgetList wList = ws->windowList();
400 if ( idx >= 0 && idx < (int)wList.count() )
401 wList.at( idx )->setFocus();
405 \brief Called when menu item is activated by the user.
407 Perform the corresponding action.
409 \param id menu item identifier
411 void QtxWorkspaceAction::onTriggered( int id )
416 activateItem( id - Windows - 1 );