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