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: QtxWorkstackAction.cxx
24 // Author: Sergey TELKOV
26 #include "QtxWorkstackAction.h"
28 #include "QtxWorkstack.h"
31 #include <QWidgetList>
34 \class QtxWorkstackAction
35 \brief Implements actions group for menu Windows with standard operations, like
36 "Split vertical", "Split horizontal", etc.
42 \param parent parent object (owner of the action)
44 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent )
45 : QtxActionSet( parent ),
50 insertAction( myWorkstack->action( QtxWorkstack::SplitVertical ), SplitVertical );
52 insertAction( new QtxAction( tr( "Split the active window on two vertical parts" ),
53 tr( "Split vertically" ), 0, this ), SplitVertical );
56 insertAction( myWorkstack->action( QtxWorkstack::SplitHorizontal ), SplitHorizontal );
58 insertAction( new QtxAction( tr( "Split the active window on two horizontal parts" ),
59 tr( "Split horizontally" ), 0, this ), SplitHorizontal );
61 connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
63 setMenuActions( Standard );
69 QtxWorkstackAction::~QtxWorkstackAction()
75 \return parent workstack
77 QtxWorkstack* QtxWorkstackAction::workstack() const
83 \brief Set actions to be visible in the menu.
85 Actions, which IDs are set in \a flags parameter, will be shown in the
86 menu bar. Other actions will not be shown.
88 \param flags ORed together actions flags
90 void QtxWorkstackAction::setMenuActions( const int flags )
92 action( SplitVertical )->setVisible( flags & SplitVertical );
93 action( SplitHorizontal )->setVisible( flags & SplitHorizontal );
94 myWindowsFlag = flags & Windows;
98 \brief Get menu actions which are currently visible in the menu bar.
99 \return ORed together actions flags
102 int QtxWorkstackAction::menuActions() const
105 ret = ret | ( action( SplitVertical )->isVisible() ? SplitVertical : 0 );
106 ret = ret | ( action( SplitHorizontal )->isVisible() ? SplitHorizontal : 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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::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 QtxWorkstackAction::perform( const int type )
222 case SplitHorizontal:
230 \brief Split the window area in the workstack in the vertical direction.
232 void QtxWorkstackAction::splitVertical()
234 QtxWorkstack* ws = workstack();
240 \brief Split the window area in the workstack in the horizontal direction.
242 void QtxWorkstackAction::splitHorizontal()
244 QtxWorkstack* ws = workstack();
246 ws->splitHorizontal();
250 \brief Called when action is added to the menu bar.
251 \param w menu bar widget this action is being added to
253 void QtxWorkstackAction::addedTo( QWidget* w )
255 QtxActionSet::addedTo( w );
257 QMenu* pm = ::qobject_cast<QMenu*>( w );
259 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
263 \brief Called when action is removed from the menu bar.
264 \param w menu bar widget this action is being removed from
266 void QtxWorkstackAction::removedFrom( QWidget* w )
268 QtxActionSet::removedFrom( w );
270 QMenu* pm = ::qobject_cast<QMenu*>( w );
272 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
276 \brief Update all menu action state.
278 void QtxWorkstackAction::updateContent()
280 bool count = workstack() ? workstack()->splitWindowList().count() > 1 : 0;
281 action( SplitVertical )->setEnabled( count );
282 action( SplitHorizontal )->setEnabled( count );
288 \brief Update actions which refer to the opened child windows.
290 void QtxWorkstackAction::updateWindows()
292 QtxWorkstack* ws = workstack();
296 QList<QAction*> lst = actions();
297 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
299 int id = actionId( *it );
304 bool base = action( SplitVertical )->isVisible() || action( SplitHorizontal )->isVisible();
306 QList<QAction*> items;
307 QMap<QAction*, int> map;
308 if ( menuActions() & Windows )
311 QWidgetList wList = ws->windowList();
312 for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
315 QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
316 a->setChecked( wid == ws->activeWindow() );
318 map.insert( a, Windows + index );
321 if ( base && !items.isEmpty() )
323 QAction* sep = new QtxAction( this );
324 sep->setSeparator( true );
325 items.prepend( sep );
326 map.insert( sep, Windows );
330 if ( !items.isEmpty() )
331 insertActions( items );
333 for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
334 setActionId( itr.key(), itr.value() );
338 \brief Called when parent menu is about to show.
340 Updates all menu items.
342 void QtxWorkstackAction::onAboutToShow()
344 QMenu* pm = ::qobject_cast<QMenu*>( sender() );
350 \brief Called when menu item corresponding to some child window is activated.
352 Activates correposponding child window.
354 \param idx menu item index
356 void QtxWorkstackAction::activateItem( const int idx )
358 QtxWorkstack* ws = workstack();
362 QWidgetList wList = ws->windowList();
363 if ( idx >= 0 && idx < (int)wList.count() )
364 wList.at( idx )->setFocus();
368 \brief Called when menu item is activated by the user.
370 Perform the corresponding action.
372 \param id menu item identifier
374 void QtxWorkstackAction::onTriggered( int id )
379 activateItem( id - Windows - 1 );