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: QtxWorkstackAction.cxx
23 // Author: Sergey TELKOV
25 #include "QtxWorkstackAction.h"
27 #include "QtxWorkstack.h"
30 #include <QWidgetList>
33 \class QtxWorkstackAction
34 \brief Implements actions group for menu Windows with standard operations, like
35 "Split vertical", "Split horizontal", etc.
41 \param parent parent object (owner of the action)
43 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent )
44 : QtxActionSet( parent ),
48 insertAction( new QtxAction( tr( "Split the active window on two vertical parts" ),
49 tr( "Split vertically" ), 0, this ), SplitVertical );
50 insertAction( new QtxAction( tr( "Split the active window on two horizontal parts" ),
51 tr( "Split horizontally" ), 0, this ), SplitHorizontal );
53 connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
55 setMenuActions( Standard );
61 QtxWorkstackAction::~QtxWorkstackAction()
67 \return parent workstack
69 QtxWorkstack* QtxWorkstackAction::workstack() const
75 \brief Set actions to be visible in the menu.
77 Actions, which IDs are set in \a flags parameter, will be shown in the
78 menu bar. Other actions will not be shown.
80 \param flags ORed together actions flags
82 void QtxWorkstackAction::setMenuActions( const int flags )
84 action( SplitVertical )->setVisible( flags & SplitVertical );
85 action( SplitHorizontal )->setVisible( flags & SplitHorizontal );
86 myWindowsFlag = flags & Windows;
90 \brief Get menu actions which are currently visible in the menu bar.
91 \return ORed together actions flags
94 int QtxWorkstackAction::menuActions() const
97 ret = ret | ( action( SplitVertical )->isVisible() ? SplitVertical : 0 );
98 ret = ret | ( action( SplitHorizontal )->isVisible() ? SplitHorizontal : 0 );
99 ret = ret | ( myWindowsFlag ? Windows : 0 );
104 \brief Get keyboard accelerator for the specified action.
105 \param id menu action ID
106 \return keyboard accelerator of menu item or 0 if there is no such action
108 int QtxWorkstackAction::accel( const int id ) const
112 a = action( id )->shortcut();
117 \brief Get icon for the specified action.
119 If \a id is invalid, null icon is returned.
121 \param id menu action ID
122 \return menu item icon
124 QIcon QtxWorkstackAction::icon( const int id ) const
128 ico = action( id )->icon();
133 \brief Get menu item text for the specified action.
134 \param id menu action ID
135 \return menu item text or null QString if there is no such action
137 QString QtxWorkstackAction::text( const int id ) const
141 txt = action( id )->text();
146 \brief Get status bar tip for the specified action.
147 \param id menu action ID
148 \return status bar tip menu item or null QString if there is no such action
150 QString QtxWorkstackAction::statusTip( const int id ) const
154 txt = action( id )->statusTip();
159 \brief Set keyboard accelerator for the specified action.
160 \param id menu action ID
161 \param a new keyboard accelerator
163 void QtxWorkstackAction::setAccel( const int id, const int a )
166 action( id )->setShortcut( a );
170 \brief Set menu item icon for the specified action.
171 \param id menu action ID
172 \param ico new menu item icon
174 void QtxWorkstackAction::setIcon( const int id, const QIcon& icon )
177 action( id )->setIcon( icon );
181 \brief Set menu item text for the specified action.
182 \param id menu action ID
183 \param txt new menu item text
185 void QtxWorkstackAction::setText( const int id, const QString& txt )
188 action( id )->setText( txt );
192 \brief Set menu item status bar tip for the specified action.
193 \param id menu action ID
194 \param txt new menu item status bar tip
196 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
199 action( id )->setStatusTip( txt );
203 \brief Process action activated by the user.
204 \param type action ID
206 void QtxWorkstackAction::perform( const int type )
213 case SplitHorizontal:
220 \brief Split the window area in the workstack in the vertical direction.
222 void QtxWorkstackAction::splitVertical()
224 QtxWorkstack* ws = workstack();
230 \brief Split the window area in the workstack in the horizontal direction.
232 void QtxWorkstackAction::splitHorizontal()
234 QtxWorkstack* ws = workstack();
236 ws->splitHorizontal();
240 \brief Called when action is added to the menu bar.
241 \param w menu bar widget this action is being added to
243 void QtxWorkstackAction::addedTo( QWidget* w )
245 QtxActionSet::addedTo( w );
247 QMenu* pm = ::qobject_cast<QMenu*>( w );
249 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
253 \brief Called when action is removed from the menu bar.
254 \param w menu bar widget this action is being removed from
256 void QtxWorkstackAction::removedFrom( QWidget* w )
258 QtxActionSet::removedFrom( w );
260 QMenu* pm = ::qobject_cast<QMenu*>( w );
262 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
266 \brief Update all menu action state.
268 void QtxWorkstackAction::updateContent()
270 bool count = workstack() ? workstack()->splitWindowList().count() > 1 : 0;
271 action( SplitVertical )->setEnabled( count );
272 action( SplitHorizontal )->setEnabled( count );
278 \brief Update actions which refer to the opened child windows.
280 void QtxWorkstackAction::updateWindows()
282 QtxWorkstack* ws = workstack();
286 QList<QAction*> lst = actions();
287 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
289 int id = actionId( *it );
294 bool base = action( SplitVertical )->isVisible() || action( SplitHorizontal )->isVisible();
296 QList<QAction*> items;
297 QMap<QAction*, int> map;
298 if ( menuActions() & Windows )
301 QWidgetList wList = ws->windowList();
302 for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
305 QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
306 a->setChecked( wid == ws->activeWindow() );
308 map.insert( a, Windows + index );
311 if ( base && !items.isEmpty() )
313 QAction* sep = new QtxAction( this );
314 sep->setSeparator( true );
315 items.prepend( sep );
316 map.insert( sep, Windows );
320 if ( !items.isEmpty() )
321 insertActions( items );
323 for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
324 setActionId( itr.key(), itr.value() );
328 \brief Called when parent menu is about to show.
330 Updates all menu items.
332 void QtxWorkstackAction::onAboutToShow()
334 QMenu* pm = ::qobject_cast<QMenu*>( sender() );
340 \brief Called when menu item corresponding to some child window is activated.
342 Activates correposponding child window.
344 \param idx menu item index
346 void QtxWorkstackAction::activateItem( const int idx )
348 QtxWorkstack* ws = workstack();
352 QWidgetList wList = ws->windowList();
353 if ( idx >= 0 && idx < (int)wList.count() )
354 wList.at( idx )->setFocus();
358 \brief Called when menu item is activated by the user.
360 Perform the corresponding action.
362 \param id menu item identifier
364 void QtxWorkstackAction::onTriggered( int id )
369 activateItem( id - Windows - 1 );