Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / STD / STD_MDIDesktop.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "STD_MDIDesktop.h"
23
24 #include <SUIT_Session.h>
25 #include <SUIT_ViewWindow.h>
26 #include <SUIT_ResourceMgr.h>
27
28 #include <QtxWorkspace.h>
29 #include <QtxActionMenuMgr.h>
30 #include <QtxWorkspaceAction.h>
31
32 #include <QFrame>
33 #include <QVBoxLayout>
34
35 #include <stdarg.h>
36
37 /*!
38   Constructor.
39 */
40 STD_MDIDesktop::STD_MDIDesktop()
41 : SUIT_Desktop(),
42 myWorkspace( 0 ),
43 myWorkspaceAction( 0 )
44 {
45   QFrame* base = new QFrame( this );
46   QVBoxLayout* main = new QVBoxLayout( base );
47   main->setMargin( 0 );
48   base->setFrameStyle( QFrame::Panel | QFrame::Sunken );
49
50   setCentralWidget( base );
51
52   myWorkspace = new QtxWorkspace( base );
53   main->addWidget( myWorkspace );
54
55   connect( myWorkspace, SIGNAL( windowActivated( QWidget* ) ),
56            this, SLOT( onWindowActivated( QWidget* ) ) );
57
58   createActions();
59 }
60
61 /*!
62   Destructor.
63 */
64 STD_MDIDesktop::~STD_MDIDesktop()
65 {
66 }
67
68 /*!
69   \retval SUIT_ViewWindow - return const active window.
70 */
71 SUIT_ViewWindow* STD_MDIDesktop::activeWindow() const
72 {
73   SUIT_ViewWindow* wnd = 0;
74
75   QWidget* wid = myWorkspace->activeWindow();
76   if ( wid && wid->inherits( "SUIT_ViewWindow" ) )
77     wnd = (SUIT_ViewWindow*)wid;
78
79   return wnd;
80 }
81
82 /*!
83   \retval QList<SUIT_ViewWindow> - return const active window list.
84 */
85 QList<SUIT_ViewWindow*> STD_MDIDesktop::windows() const
86 {
87   QList<SUIT_ViewWindow*> winList;
88
89   QWidgetList children = myWorkspace->windowList();
90   for ( QWidgetList::iterator it = children.begin(); it != children.end(); ++it )
91   {
92     SUIT_ViewWindow* vw = ::qobject_cast<SUIT_ViewWindow*>( *it );
93     if ( vw )
94       winList.append( vw );
95   }
96
97   return winList;
98 }
99
100 /*!
101   Add the new widget into desktop.
102 */
103 void STD_MDIDesktop::addWindow( QWidget* w )
104 {
105   if ( !w || !workspace() )
106     return;
107
108   workspace()->addWindow( w );
109 }
110
111 /*!Call method perform for operation \a type.*/
112 void STD_MDIDesktop::windowOperation( const int type )
113 {
114   myWorkspaceAction->perform( operationFlag( type ) );
115 }
116
117 /*!Sets window operations by \a first ... parameters.*/
118 void STD_MDIDesktop::setWindowOperations( const int first, ... )
119 {
120   va_list ints;
121   va_start( ints, first );
122
123   QList<int> typeList;
124   
125   int cur = first;
126   while ( cur )
127   {
128     typeList.append( cur );
129     cur = va_arg( ints, int );
130   }
131
132   setWindowOperations( typeList );
133 }
134
135 /*!Sets window operations by variable \a opList - operation list.*/
136 void STD_MDIDesktop::setWindowOperations( const QList<int>& opList )
137 {
138   int flags = 0;
139
140   for ( QList<int>::const_iterator it = opList.begin(); it != opList.end(); ++it )
141     flags = flags | operationFlag( *it );
142
143   myWorkspaceAction->setMenuActions( flags );
144 }
145
146 /*!
147   \retval QtxWorkspace pointer - work space.
148 */
149 QtxWorkspace* STD_MDIDesktop::workspace() const
150 {
151   return myWorkspace;
152 }
153
154 /*!
155   Emit window activated.
156 */
157 void STD_MDIDesktop::onWindowActivated( QWidget* w )
158 {
159   if ( w && w->inherits( "SUIT_ViewWindow" ) )
160     emit windowActivated( (SUIT_ViewWindow*)w );
161 }
162
163 /*!
164   Create actions: cascade, Tile, Tile Horizontal, Tile Vertical
165 */
166 void STD_MDIDesktop::createActions()
167 {
168   if ( myWorkspaceAction )
169     return;
170
171   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
172   if ( !resMgr )
173     return;
174
175   myWorkspaceAction = new QtxWorkspaceAction( workspace(), this );
176
177   myWorkspaceAction->setMenuActions( QtxWorkspaceAction::Cascade | QtxWorkspaceAction::Tile  |
178                                      QtxWorkspaceAction::HTile   | QtxWorkspaceAction::VTile |
179                                      QtxWorkspaceAction::Windows );
180
181   // Cascade
182   myWorkspaceAction->setIcon( QtxWorkspaceAction::Cascade,
183                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_CASCADE" ) ) );
184   myWorkspaceAction->setText( QtxWorkspaceAction::Cascade, tr( "MEN_DESK_WINDOW_CASCADE" ) );
185   myWorkspaceAction->setStatusTip( QtxWorkspaceAction::Cascade, tr( "PRP_DESK_WINDOW_CASCADE" ) );
186
187   // Tile
188   myWorkspaceAction->setIcon( QtxWorkspaceAction::Tile,
189                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_TILE" ) ) );
190   myWorkspaceAction->setText( QtxWorkspaceAction::Tile, tr( "MEN_DESK_WINDOW_TILE" ) );
191   myWorkspaceAction->setStatusTip( QtxWorkspaceAction::Tile, tr( "PRP_DESK_WINDOW_TILE" ) );
192
193   // Tile Horizontal
194   myWorkspaceAction->setIcon( QtxWorkspaceAction::HTile,
195                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HTILE" ) ) );
196   myWorkspaceAction->setText( QtxWorkspaceAction::HTile, tr( "MEN_DESK_WINDOW_HTILE" ) );
197   myWorkspaceAction->setStatusTip( QtxWorkspaceAction::HTile, tr( "PRP_DESK_WINDOW_HTILE" ) );
198
199   // Tile Vertical
200   myWorkspaceAction->setIcon( QtxWorkspaceAction::VTile,
201                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VTILE" ) ) );
202   myWorkspaceAction->setText( QtxWorkspaceAction::VTile, tr( "MEN_DESK_WINDOW_VTILE" ) );
203   myWorkspaceAction->setStatusTip( QtxWorkspaceAction::VTile, tr( "PRP_DESK_WINDOW_VTILE" ) );
204
205   QtxActionMenuMgr* mMgr = menuMgr();
206   if ( !mMgr )
207     return;
208
209   int winMenuId = mMgr->insert( tr( "MEN_DESK_WINDOW" ), -1, 100 );
210   mMgr->insert( myWorkspaceAction, winMenuId, -1 );
211   mMgr->insert( QtxActionMenuMgr::separator(), winMenuId, -1 );
212 }
213
214 /*!Convert STD_MDIDesktop enumerations to QtxWorkspaceAction.*/
215 int STD_MDIDesktop::operationFlag( const int type ) const
216 {
217   int res = 0;
218   switch ( type )
219   {
220   case Cascade:
221     res = QtxWorkspaceAction::Cascade;
222     break;
223   case Tile:
224     res = QtxWorkspaceAction::Tile;
225     break;
226   case HTile:
227     res = QtxWorkspaceAction::HTile;
228     break;
229   case VTile:
230     res = QtxWorkspaceAction::VTile;
231     break;
232   }
233   return res;
234 }