1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: QtxWorkstackAction.cxx
20 // Author: Sergey TELKOV
22 #include "QtxWorkstackAction.h"
24 #include "QtxWorkstack.h"
26 #include <qpopupmenu.h>
27 #include <qwidgetlist.h>
32 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent, const char* name )
33 : QtxAction( tr( "Controls windows into workstack" ), tr( "Workstack management" ), 0, parent, name ),
37 myItem.insert( VSplit, new QtxAction( tr( "Split the active window on two vertical parts" ),
38 tr( "Split vertically" ), 0, this, 0, false ) );
39 myItem.insert( HSplit, new QtxAction( tr( "Split the active window on two horizontal parts" ),
40 tr( "Split horizontally" ), 0, this, 0, false ) );
42 connect( myItem[VSplit], SIGNAL( activated() ), ws, SLOT( splitVertical() ) );
43 connect( myItem[HSplit], SIGNAL( activated() ), ws, SLOT( splitHorizontal() ) );
49 QtxWorkstackAction::~QtxWorkstackAction()
54 \return corresponding workstack
56 QtxWorkstack* QtxWorkstackAction::workstack() const
62 \return set of action flags
64 int QtxWorkstackAction::items() const
71 \param flags - new set of flags
73 void QtxWorkstackAction::setItems( const int flags )
75 if ( !flags || flags == myFlags || !( flags & Split ) )
82 \return true if action contains all flags
83 \param flags - new set of flags
85 bool QtxWorkstackAction::hasItems( const int flags ) const
87 return ( myFlags & flags ) == flags;
91 \return accelerator of item
94 int QtxWorkstackAction::accel( const int id ) const
97 if ( myItem.contains( id ) )
98 a = myItem[id]->accel();
103 \return icons of item
106 QIconSet QtxWorkstackAction::iconSet( const int id ) const
109 if ( myItem.contains( id ) )
110 ico = myItem[id]->iconSet();
115 \return menu text of item
118 QString QtxWorkstackAction::menuText( const int id ) const
121 if ( myItem.contains( id ) )
122 txt = myItem[id]->menuText();
127 \return status tip of item
130 QString QtxWorkstackAction::statusTip( const int id ) const
133 if ( myItem.contains( id ) )
134 txt = myItem[id]->statusTip();
139 Changes accelerator of item
141 \param a - new accelerator
143 void QtxWorkstackAction::setAccel( const int id, const int a )
145 if ( myItem.contains( id ) )
146 myItem[id]->setAccel( a );
150 Changes icons of item
152 \param ico - new icons
154 void QtxWorkstackAction::setIconSet( const int id, const QIconSet& ico )
156 if ( myItem.contains( id ) )
157 myItem[id]->setIconSet( ico );
161 Changes menu text of item
163 \param txt - new menu text
165 void QtxWorkstackAction::setMenuText( const int id, const QString& txt )
167 if ( myItem.contains( id ) )
168 myItem[id]->setMenuText( txt );
172 Changes status tip of item
174 \param txt - new status tip
176 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
178 if ( myItem.contains( id ) )
179 myItem[id]->setStatusTip( txt );
183 Adds action to widget
186 bool QtxWorkstackAction::addTo( QWidget* wid )
188 return addTo( wid, -1 );
192 Adds action to widget
194 \param idx - position
196 bool QtxWorkstackAction::addTo( QWidget* wid, const int idx )
198 if ( !wid || !wid->inherits( "QPopupMenu" ) )
201 QPopupMenu* pm = (QPopupMenu*)wid;
204 if ( myMenu.contains( pm ) )
207 myMenu.insert( pm, QIntList() );
208 fillPopup( pm, idx );
210 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
211 connect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
217 Removes action from widget
220 bool QtxWorkstackAction::removeFrom( QWidget* wid )
222 if ( !wid || !wid->inherits( "QPopupMenu" ) )
225 QPopupMenu* pm = (QPopupMenu*)wid;
226 if ( !myMenu.contains( pm ) )
231 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
232 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
241 \param type - action type
243 void QtxWorkstackAction::perform( const int type )
248 workstack()->splitVertical();
251 workstack()->splitHorizontal();
257 SLOT: called just before the popup menu is displayed, updates popup
259 void QtxWorkstackAction::onAboutToShow()
261 const QObject* obj = sender();
262 if ( !obj || !obj->inherits( "QPopupMenu" ) )
265 QtxWorkstack* ws = workstack();
266 if ( ws && myItem.contains( VSplit ) )
267 myItem[VSplit]->setAccel( ws->accel( QtxWorkstack::SplitVertical ) );
268 if ( ws && myItem.contains( HSplit ) )
269 myItem[HSplit]->setAccel( ws->accel( QtxWorkstack::SplitHorizontal ) );
271 updatePopup( (QPopupMenu*)obj );
275 SLOT: called when popup menu is destroyed, removes it from menu
277 void QtxWorkstackAction::onPopupDestroyed( QObject* obj )
279 myMenu.remove( (QPopupMenu*)obj );
284 \param pm - popup menu
286 void QtxWorkstackAction::checkPopup( QPopupMenu* pm )
288 if ( !myMenu.contains( pm ) )
292 for ( QIntList::const_iterator it = myMenu[pm].begin(); it != myMenu[pm].end(); ++it )
294 if ( pm->indexOf( *it ) != -1 )
295 updList.append( *it );
300 if ( !updList.isEmpty() )
301 myMenu.insert( pm, updList );
304 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
305 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
310 Clears and refills popup and updates state of actions
311 \param pm - popup menu
313 void QtxWorkstackAction::updatePopup( QPopupMenu* pm )
315 if ( !myMenu.contains( pm ) )
318 fillPopup( pm, clearPopup( pm ) );
320 int count = workstack() ? workstack()->splitWindowList().count() : 0;
321 myItem[VSplit]->setEnabled( count > 1 );
322 myItem[HSplit]->setEnabled( count > 1 );
327 \param pm - popup menu
329 int QtxWorkstackAction::clearPopup( QPopupMenu* pm )
331 if ( !myMenu.contains( pm ) )
335 const QIntList& lst = myMenu[pm];
336 for ( QIntList::const_iterator it = lst.begin(); it != lst.end() && idx == -1; ++it )
337 idx = pm->indexOf( *it );
339 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
340 mit.data()->removeFrom( pm );
342 for ( QIntList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
343 pm->removeItem( *itr );
349 Fills popup with items
350 \param pm - popup menu
351 \param idx - position
353 void QtxWorkstackAction::fillPopup( QPopupMenu* pm, const int idx )
358 int index = idx < 0 ? pm->count() : QMIN( (int)pm->count(), idx );
360 myMenu.insert( pm, QIntList() );
361 QIntList& lst = myMenu[pm];
363 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
365 if ( !hasItems( mit.key() ) )
368 mit.data()->addTo( pm, index );
369 lst.append( pm->idAt( index++ ) );
372 QtxWorkstack* ws = workstack();
373 if ( !ws || !hasItems( Windows ) )
376 QWidgetList wList = ws->windowList();
377 if ( wList.isEmpty() )
380 lst.append( pm->insertSeparator( index++ ) );
383 pm->setCheckable( true );
384 for ( QWidgetListIt it( wList ); it.current(); ++it )
386 int id = pm->insertItem( it.current()->caption(), this, SLOT( onItemActivated( int ) ), 0, -1, index++ );
387 pm->setItemParameter( id, param++ );
388 pm->setItemChecked( id, it.current() == ws->activeWindow() );
394 SLOT: called when popup item corresponding to window is activated, activates window
396 void QtxWorkstackAction::onItemActivated( int idx )
398 QtxWorkstack* ws = workstack();
402 QWidgetList wList = ws->windowList();
403 if ( idx < 0 || idx >= (int)wList.count() )
406 wList.at( idx )->setFocus();