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: QtxWorkspaceAction.cxx
20 // Author: Sergey TELKOV
22 #include "QtxWorkspaceAction.h"
24 #include <qpopupmenu.h>
25 #include <qworkspace.h>
26 #include <qwidgetlist.h>
28 QtxWorkspaceAction::QtxWorkspaceAction( QWorkspace* ws, QObject* parent, const char* name )
29 : QtxAction( tr( "Controls windows into workspace" ), tr( "Workspace management" ), 0, parent, name ),
33 myItem.insert( Cascade, new QtxAction( tr( "Arranges the windows as overlapping tiles" ),
34 tr( "Cascade" ), 0, this, 0, false ) );
35 myItem.insert( Tile, new QtxAction( tr( "Arranges the windows as nonoverlapping tiles" ),
36 tr( "Tile" ), 0, this, 0, false ) );
37 myItem.insert( HTile, new QtxAction( tr( "Arranges the windows as nonoverlapping horizontal tiles" ),
38 tr( "Tile horizontally" ), 0, this, 0, false ) );
39 myItem.insert( VTile, new QtxAction( tr( "Arranges the windows as nonoverlapping vertical tiles" ),
40 tr( "Tile vertically" ), 0, this, 0, false ) );
42 connect( myItem[Tile], SIGNAL( activated() ), this, SLOT( tile() ) );
43 connect( myItem[Cascade], SIGNAL( activated() ), this, SLOT( cascade() ) );
44 connect( myItem[HTile], SIGNAL( activated() ), this, SLOT( tileVertical() ) );
45 connect( myItem[VTile], SIGNAL( activated() ), this, SLOT( tileHorizontal() ) );
48 QtxWorkspaceAction::~QtxWorkspaceAction()
52 QWorkspace* QtxWorkspaceAction::workspace() const
57 int QtxWorkspaceAction::items() const
62 void QtxWorkspaceAction::setItems( const int flags )
64 if ( !flags || flags == myFlags || !( flags & Operations ) )
70 bool QtxWorkspaceAction::hasItems( const int flags ) const
72 return ( myFlags & flags ) == flags;
75 int QtxWorkspaceAction::accel( const int id ) const
78 if ( myItem.contains( id ) )
79 a = myItem[id]->accel();
83 QIconSet QtxWorkspaceAction::iconSet( const int id ) const
86 if ( myItem.contains( id ) )
87 ico = myItem[id]->iconSet();
91 QString QtxWorkspaceAction::menuText( const int id ) const
94 if ( myItem.contains( id ) )
95 txt = myItem[id]->menuText();
99 QString QtxWorkspaceAction::statusTip( const int id ) const
102 if ( myItem.contains( id ) )
103 txt = myItem[id]->statusTip();
107 void QtxWorkspaceAction::setAccel( const int id, const int a )
109 if ( myItem.contains( id ) )
110 myItem[id]->setAccel( a );
113 void QtxWorkspaceAction::setIconSet( const int id, const QIconSet& ico )
115 if ( myItem.contains( id ) )
116 myItem[id]->setIconSet( ico );
119 void QtxWorkspaceAction::setMenuText( const int id, const QString& txt )
121 if ( myItem.contains( id ) )
122 myItem[id]->setMenuText( txt );
125 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
127 if ( myItem.contains( id ) )
128 myItem[id]->setStatusTip( txt );
131 bool QtxWorkspaceAction::addTo( QWidget* wid )
133 return addTo( wid, -1 );
136 bool QtxWorkspaceAction::addTo( QWidget* wid, const int idx )
138 if ( !wid || !wid->inherits( "QPopupMenu" ) )
141 QPopupMenu* pm = (QPopupMenu*)wid;
144 if ( myMenu.contains( pm ) )
147 myMenu.insert( pm, QIntList() );
148 fillPopup( pm, idx );
150 connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
151 connect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
156 bool QtxWorkspaceAction::removeFrom( QWidget* wid )
158 if ( !wid || !wid->inherits( "QPopupMenu" ) )
161 QPopupMenu* pm = (QPopupMenu*)wid;
162 if ( !myMenu.contains( pm ) )
167 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
168 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
175 void QtxWorkspaceAction::perform( const int type )
194 void QtxWorkspaceAction::tile()
196 QWorkspace* ws = workspace();
203 void QtxWorkspaceAction::cascade()
205 QWorkspace* ws = workspace();
212 int h = ws->height();
214 QWidgetList winList = ws->windowList();
215 for ( QWidgetListIt it( winList ); it.current(); ++it )
216 it.current()->resize( int( w * 0.8 ), int( h * 0.8 ) );
219 void QtxWorkspaceAction::tileVertical()
221 QWorkspace* wrkSpace = workspace();
225 QWidgetList winList = wrkSpace->windowList();
226 if ( winList.isEmpty() )
230 for ( QWidgetListIt itr( winList ); itr.current(); ++itr )
231 if ( !itr.current()->testWState( WState_Minimized ) )
239 int heightForEach = wrkSpace->height() / count;
240 for ( QWidgetListIt it( winList ); it.current(); ++it )
242 QWidget* win = it.current();
243 if ( win->testWState( WState_Minimized ) )
246 if ( win->testWState( WState_Maximized ) )
251 int prefH = win->minimumHeight() + win->parentWidget()->baseSize().height();
252 int actualH = QMAX( heightForEach, prefH );
254 win->parentWidget()->setGeometry( 0, y, wrkSpace->width(), actualH );
259 void QtxWorkspaceAction::tileHorizontal()
261 QWorkspace* wrkSpace = workspace();
265 QWidgetList winList = wrkSpace->windowList();
266 if ( winList.isEmpty() )
270 for ( QWidgetListIt itr( winList ); itr.current(); ++itr )
271 if ( !itr.current()->testWState( WState_Minimized ) )
278 int widthForEach = wrkSpace->width() / count;
279 for ( QWidgetListIt it( winList ); it.current(); ++it )
281 QWidget* win = it.current();
282 if ( win->testWState( WState_Minimized ) )
285 if ( win->testWState( WState_Maximized ) )
290 int prefW = win->minimumWidth();
291 int actualW = QMAX( widthForEach, prefW );
293 win->parentWidget()->setGeometry( x, 0, actualW, wrkSpace->height() );
298 void QtxWorkspaceAction::onAboutToShow()
300 const QObject* obj = sender();
301 if ( !obj || !obj->inherits( "QPopupMenu" ) )
304 updatePopup( (QPopupMenu*)obj );
307 void QtxWorkspaceAction::onPopupDestroyed( QObject* obj )
309 myMenu.remove( (QPopupMenu*)obj );
312 void QtxWorkspaceAction::checkPopup( QPopupMenu* pm )
314 if ( !myMenu.contains( pm ) )
318 for ( QIntList::const_iterator it = myMenu[pm].begin(); it != myMenu[pm].end(); ++it )
320 if ( pm->indexOf( *it ) != -1 )
321 updList.append( *it );
326 if ( !updList.isEmpty() )
327 myMenu.insert( pm, updList );
330 disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
331 disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
335 void QtxWorkspaceAction::updatePopup( QPopupMenu* pm )
337 if ( !myMenu.contains( pm ) )
340 fillPopup( pm, clearPopup( pm ) );
342 bool count = workspace() ? workspace()->windowList().count() : 0;
343 myItem[Cascade]->setEnabled( count );
344 myItem[Tile]->setEnabled( count );
345 myItem[HTile]->setEnabled( count );
346 myItem[VTile]->setEnabled( count );
349 int QtxWorkspaceAction::clearPopup( QPopupMenu* pm )
351 if ( !myMenu.contains( pm ) )
355 const QIntList& lst = myMenu[pm];
356 for ( QIntList::const_iterator it = lst.begin(); it != lst.end() && idx == -1; ++it )
357 idx = pm->indexOf( *it );
359 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
360 mit.data()->removeFrom( pm );
362 for ( QIntList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
363 pm->removeItem( *itr );
368 void QtxWorkspaceAction::fillPopup( QPopupMenu* pm, const int idx )
373 int index = idx < 0 ? pm->count() : QMIN( (int)pm->count(), idx );
375 myMenu.insert( pm, QIntList() );
376 QIntList& lst = myMenu[pm];
378 for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
380 if ( !hasItems( mit.key() ) )
383 mit.data()->addTo( pm, index );
384 lst.append( pm->idAt( index++ ) );
387 QWorkspace* ws = workspace();
388 if ( !ws || !hasItems( Windows ) )
391 QWidgetList wList = ws->windowList();
392 if ( wList.isEmpty() )
395 lst.append( pm->insertSeparator( index++ ) );
398 pm->setCheckable( true );
399 for ( QWidgetListIt it( wList ); it.current(); ++it )
401 int id = pm->insertItem( it.current()->caption(), this, SLOT( onItemActivated( int ) ), 0, -1, index++ );
402 pm->setItemParameter( id, param++ );
403 pm->setItemChecked( id, it.current() == ws->activeWindow() );
408 void QtxWorkspaceAction::onItemActivated( int idx )
410 QWorkspace* ws = workspace();
414 QWidgetList wList = ws->windowList();
415 if ( idx < 0 || idx >= (int)wList.count() )
418 wList.at( idx )->setFocus();