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/
19 // File: QtxWorkstackAction.cxx
20 // Author: Sergey TELKOV
22 #include "QtxWorkstackAction.h"
24 #include "QtxWorkstack.h"
26 #include <qpopupmenu.h>
27 #include <qwidgetlist.h>
29 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent, const char* name )
30 : QtxAction( tr( "Controls windows into workstack" ), tr( "Workstack management" ), 0, parent, name ),
34 myItem.insert( VSplit, new QtxAction( tr( "Split the active window on two vertical parts" ),
35 tr( "Split vertically" ), 0, this, 0, false ) );
36 myItem.insert( HSplit, new QtxAction( tr( "Split the active window on two horizontal parts" ),
37 tr( "Split horizontally" ), 0, this, 0, false ) );
39 connect( myItem[VSplit], SIGNAL( activated() ), ws, SLOT( splitVertical() ) );
40 connect( myItem[HSplit], SIGNAL( activated() ), ws, SLOT( splitHorizontal() ) );
43 QtxWorkstackAction::~QtxWorkstackAction()
47 QtxWorkstack* QtxWorkstackAction::workstack() const
52 int QtxWorkstackAction::items() const
57 void QtxWorkstackAction::setItems( const int flags )
59 if ( !flags || flags == myFlags || !( flags & Split ) )
65 bool QtxWorkstackAction::hasItems( const int flags ) const
67 return ( myFlags & flags ) == flags;
70 int QtxWorkstackAction::accel( const int id ) const
73 if ( myItem.contains( id ) )
74 a = myItem[id]->accel();
78 QIconSet QtxWorkstackAction::iconSet( const int id ) const
81 if ( myItem.contains( id ) )
82 ico = myItem[id]->iconSet();
86 QString QtxWorkstackAction::menuText( const int id ) const
89 if ( myItem.contains( id ) )
90 txt = myItem[id]->menuText();
94 QString QtxWorkstackAction::statusTip( const int id ) const
97 if ( myItem.contains( id ) )
98 txt = myItem[id]->statusTip();
102 void QtxWorkstackAction::setAccel( const int id, const int a )
104 if ( myItem.contains( id ) )
105 myItem[id]->setAccel( a );
108 void QtxWorkstackAction::setIconSet( const int id, const QIconSet& ico )
110 if ( myItem.contains( id ) )
111 myItem[id]->setIconSet( ico );
114 void QtxWorkstackAction::setMenuText( const int id, const QString& txt )
116 if ( myItem.contains( id ) )
117 myItem[id]->setMenuText( txt );
120 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
122 if ( myItem.contains( id ) )
123 myItem[id]->setStatusTip( txt );
126 bool QtxWorkstackAction::addTo( QWidget* wid )
128 return addTo( wid, -1 );
131 bool QtxWorkstackAction::addTo( QWidget* wid, const int idx )
133 if ( !wid || !wid->inherits( "QPopupMenu" ) )
136 QPopupMenu* pm = (QPopupMenu*)wid;
139 if ( myMenu.contains( pm ) )
142 myMenu.insert( pm, QIntList() );
143 fillPopup( pm, idx );
145 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
146 connect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
151 bool QtxWorkstackAction::removeFrom( QWidget* wid )
153 if ( !wid || !wid->inherits( "QPopupMenu" ) )
156 QPopupMenu* pm = (QPopupMenu*)wid;
157 if ( !myMenu.contains( pm ) )
162 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
163 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
170 void QtxWorkstackAction::perform( const int type )
175 workstack()->splitVertical();
178 workstack()->splitHorizontal();
183 void QtxWorkstackAction::onAboutToShow()
185 const QObject* obj = sender();
186 if ( !obj || !obj->inherits( "QPopupMenu" ) )
189 QtxWorkstack* ws = workstack();
190 if ( ws && myItem.contains( VSplit ) )
191 myItem[VSplit]->setAccel( ws->accel( QtxWorkstack::SplitVertical ) );
192 if ( ws && myItem.contains( HSplit ) )
193 myItem[HSplit]->setAccel( ws->accel( QtxWorkstack::SplitHorizontal ) );
195 updatePopup( (QPopupMenu*)obj );
198 void QtxWorkstackAction::onPopupDestroyed( QObject* obj )
200 myMenu.remove( (QPopupMenu*)obj );
203 void QtxWorkstackAction::checkPopup( QPopupMenu* pm )
205 if ( !myMenu.contains( pm ) )
209 for ( QIntList::const_iterator it = myMenu[pm].begin(); it != myMenu[pm].end(); ++it )
211 if ( pm->indexOf( *it ) != -1 )
212 updList.append( *it );
217 if ( !updList.isEmpty() )
218 myMenu.insert( pm, updList );
221 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
222 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
226 void QtxWorkstackAction::updatePopup( QPopupMenu* pm )
228 if ( !myMenu.contains( pm ) )
231 fillPopup( pm, clearPopup( pm ) );
233 int count = workstack() ? workstack()->splitWindowList().count() : 0;
234 myItem[VSplit]->setEnabled( count > 1 );
235 myItem[HSplit]->setEnabled( count > 1 );
238 int QtxWorkstackAction::clearPopup( QPopupMenu* pm )
240 if ( !myMenu.contains( pm ) )
244 const QIntList& lst = myMenu[pm];
245 for ( QIntList::const_iterator it = lst.begin(); it != lst.end() && idx == -1; ++it )
246 idx = pm->indexOf( *it );
248 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
249 mit.data()->removeFrom( pm );
251 for ( QIntList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
252 pm->removeItem( *itr );
257 void QtxWorkstackAction::fillPopup( QPopupMenu* pm, const int idx )
262 int index = idx < 0 ? pm->count() : QMIN( (int)pm->count(), idx );
264 myMenu.insert( pm, QIntList() );
265 QIntList& lst = myMenu[pm];
267 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
269 if ( !hasItems( mit.key() ) )
272 mit.data()->addTo( pm, index );
273 lst.append( pm->idAt( index++ ) );
276 QtxWorkstack* ws = workstack();
277 if ( !ws || !hasItems( Windows ) )
280 QWidgetList wList = ws->windowList();
281 if ( wList.isEmpty() )
284 lst.append( pm->insertSeparator( index++ ) );
287 pm->setCheckable( true );
288 for ( QWidgetListIt it( wList ); it.current(); ++it )
290 int id = pm->insertItem( it.current()->caption(), this, SLOT( onItemActivated( int ) ), 0, -1, index++ );
291 pm->setItemParameter( id, param++ );
292 pm->setItemChecked( id, it.current() == ws->activeWindow() );
297 void QtxWorkstackAction::onItemActivated( int idx )
299 QtxWorkstack* ws = workstack();
303 QWidgetList wList = ws->windowList();
304 if ( idx < 0 || idx >= (int)wList.count() )
307 wList.at( idx )->setFocus();