1 // Copyright (C) 2007-2008 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
22 // File: QtxWorkspaceAction.cxx
23 // Author: Sergey TELKOV
25 #include "QtxWorkspaceAction.h"
27 #include "QtxWorkspace.h"
30 #include <QWidgetList>
33 \class QtxWorkspaceAction
34 \brief Implements actions group for menu Windows with standard operations, like
35 "Cascade", "Tile", "Tile Horizontally", etc.
40 \param ws parent workspace
41 \param parent parent object (owner of the action)
43 QtxWorkspaceAction::QtxWorkspaceAction( QtxWorkspace* ws, QObject* parent )
44 : QtxActionSet( parent ),
48 insertAction( new QtxAction( tr( "Arranges the windows as overlapping tiles" ),
49 tr( "Cascade" ), 0, this ), Cascade );
50 insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping tiles" ),
51 tr( "Tile" ), 0, this ), Tile );
52 insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping horizontal tiles" ),
53 tr( "Tile horizontally" ), 0, this ), HTile );
54 insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping vertical tiles" ),
55 tr( "Tile vertically" ), 0, this ), VTile );
57 connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
59 setMenuActions( Standard );
65 QtxWorkspaceAction::~QtxWorkspaceAction()
71 \return parent workspace
73 QtxWorkspace* QtxWorkspaceAction::workspace() const
79 \brief Set actions to be visible in the menu.
81 Actions, which IDs are set in \a flags parameter, will be shown in the
82 menu bar. Other actions will not be shown.
84 \param flags ORed together actions flags
86 void QtxWorkspaceAction::setMenuActions( const int flags )
88 action( Cascade )->setVisible( flags & Cascade );
89 action( Tile )->setVisible( flags & Tile );
90 action( VTile )->setVisible( flags & VTile );
91 action( HTile )->setVisible( flags & HTile );
92 myWindowsFlag = flags & Windows;
96 \brief Get menu actions which are currently visible in the menu bar.
97 \return ORed together actions flags
100 int QtxWorkspaceAction::menuActions() const
103 ret = ret | ( action( Cascade )->isVisible() ? Cascade : 0 );
104 ret = ret | ( action( Tile )->isVisible() ? Tile : 0 );
105 ret = ret | ( action( VTile )->isVisible() ? VTile : 0 );
106 ret = ret | ( action( HTile )->isVisible() ? HTile : 0 );
107 ret = ret | ( myWindowsFlag ? Windows : 0 );
112 \brief Get keyboard accelerator for the specified action.
113 \param id menu action ID
114 \return keyboard accelerator of menu item or 0 if there is no such action
116 int QtxWorkspaceAction::accel( const int id ) const
120 a = action( id )->shortcut();
125 \brief Get icon for the specified action.
127 If \a id is invalid, null icon is returned.
129 \param id menu action ID
130 \return menu item icon
132 QIcon QtxWorkspaceAction::icon( const int id ) const
136 ico = action( id )->icon();
141 \brief Get menu item text for the specified action.
142 \param id menu action ID
143 \return menu item text or null QString if there is no such action
145 QString QtxWorkspaceAction::text( const int id ) const
149 txt = action( id )->text();
154 \brief Get status bar tip for the specified action.
155 \param id menu action ID
156 \return status bar tip menu item or null QString if there is no such action
158 QString QtxWorkspaceAction::statusTip( const int id ) const
162 txt = action( id )->statusTip();
167 \brief Set keyboard accelerator for the specified action.
168 \param id menu action ID
169 \param a new keyboard accelerator
171 void QtxWorkspaceAction::setAccel( const int id, const int a )
174 action( id )->setShortcut( a );
178 \brief Set menu item icon for the specified action.
179 \param id menu action ID
180 \param ico new menu item icon
182 void QtxWorkspaceAction::setIcon( const int id, const QIcon& icon )
185 action( id )->setIcon( icon );
189 \brief Set menu item text for the specified action.
190 \param id menu action ID
191 \param txt new menu item text
193 void QtxWorkspaceAction::setText( const int id, const QString& txt )
196 action( id )->setText( txt );
200 \brief Set menu item status bar tip for the specified action.
201 \param id menu action ID
202 \param txt new menu item status bar tip
204 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
207 action( id )->setStatusTip( txt );
211 \brief Process action activated by the user.
212 \param type action ID
214 void QtxWorkspaceAction::perform( const int type )
234 \brief Tile child windows in the workspace.
236 void QtxWorkspaceAction::tile()
238 QtxWorkspace* ws = workspace();
244 \brief Cascade child windows in the workspace.
246 void QtxWorkspaceAction::cascade()
248 QtxWorkspace* ws = workspace();
255 int h = ws->height();
257 QWidgetList winList = ws->windowList();
258 for ( QWidgetList::iterator it = winList.begin(); it != winList.end(); ++it )
259 (*it)->resize( int( w * 0.8 ), int( h * 0.8 ) );
263 \brief Tile child windows in the workspace in the vertical direction.
265 void QtxWorkspaceAction::tileVertical()
267 QtxWorkspace* ws = workspace();
273 \brief Tile child windows in the workspace in the horizontal direction.
275 void QtxWorkspaceAction::tileHorizontal()
277 QtxWorkspace* ws = workspace();
279 ws->tileHorizontal();
283 \brief Called when action is added to the menu bar.
284 \param w menu bar widget this action is being added to
286 void QtxWorkspaceAction::addedTo( QWidget* w )
288 QtxActionSet::addedTo( w );
290 QMenu* pm = ::qobject_cast<QMenu*>( w );
292 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
296 \brief Called when action is removed from the menu bar.
297 \param w menu bar widget this action is being removed from
299 void QtxWorkspaceAction::removedFrom( QWidget* w )
301 QtxActionSet::removedFrom( w );
303 QMenu* pm = ::qobject_cast<QMenu*>( w );
305 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
309 \brief Update all menu action state.
311 void QtxWorkspaceAction::updateContent()
313 bool count = workspace() ? workspace()->windowList().count() : 0;
314 action( Cascade )->setEnabled( count );
315 action( Tile )->setEnabled( count );
316 action( HTile )->setEnabled( count );
317 action( VTile )->setEnabled( count );
323 \brief Update actions which refer to the opened child windows.
325 void QtxWorkspaceAction::updateWindows()
327 QtxWorkspace* ws = workspace();
331 QList<QAction*> lst = actions();
332 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
334 int id = actionId( *it );
339 bool base = action( Cascade )->isVisible() || action( Tile )->isVisible() ||
340 action( HTile )->isVisible() || action( VTile )->isVisible();
342 QList<QAction*> items;
343 QMap<QAction*, int> map;
344 if ( menuActions() & Windows )
347 QWidgetList wList = ws->windowList();
348 for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
351 QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
352 a->setChecked( wid == ws->activeWindow() );
354 map.insert( a, Windows + index );
357 if ( base && !items.isEmpty() )
359 QAction* sep = new QtxAction( this );
360 sep->setSeparator( true );
361 items.prepend( sep );
362 map.insert( sep, Windows );
366 if ( !items.isEmpty() )
367 insertActions( items );
369 for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
370 setActionId( itr.key(), itr.value() );
374 \brief Called when parent menu is about to show.
376 Updates all menu items.
378 void QtxWorkspaceAction::onAboutToShow()
380 QMenu* pm = ::qobject_cast<QMenu*>( sender() );
386 \brief Called when menu item corresponding to some child window is activated.
388 Activates correposponding child window.
390 \param idx menu item index
392 void QtxWorkspaceAction::activateItem( const int idx )
394 QtxWorkspace* ws = workspace();
398 QWidgetList wList = ws->windowList();
399 if ( idx >= 0 && idx < (int)wList.count() )
400 wList.at( idx )->setFocus();
404 \brief Called when menu item is activated by the user.
406 Perform the corresponding action.
408 \param id menu item identifier
410 void QtxWorkspaceAction::onTriggered( int id )
415 activateItem( id - Windows - 1 );