Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / STD / STD_TabDesktop.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_TabDesktop.h"
23
24 #include <SUIT_Session.h>
25 #include <SUIT_ViewWindow.h>
26 #include <SUIT_ResourceMgr.h>
27
28 #include <QtxWorkstack.h>
29 #include <QtxActionMenuMgr.h>
30 #include <QtxWorkstackAction.h>
31
32 #include <QFrame>
33 #include <QVBoxLayout>
34
35 #include <stdarg.h>
36
37 /*!Constructor.Create new instances of QVBox and QtxWorkstack.*/
38 STD_TabDesktop::STD_TabDesktop()
39 : SUIT_Desktop(),
40 myWorkstack( 0 ),
41 myWorkstackAction( 0 )
42 {
43   QFrame* base = new QFrame( this );
44   base->setFrameStyle( QFrame::Panel | QFrame::Sunken );
45
46   QVBoxLayout* main = new QVBoxLayout( base );
47   main->setMargin( 0 );
48
49   setCentralWidget( base );
50
51   myWorkstack = new QtxWorkstack( base );
52   main->addWidget( myWorkstack );
53   // setting Expanding size policy for central workstack.  If there are several widgets
54   // in central area of Desktop, other widgets will be added below the workstack (CATHARE, TRIPOLI modules).  
55   // But the workstack must occupy as much space as possible -- set Expanding for it.
56   myWorkstack->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
57
58   myWorkstack->setAccel( QtxWorkstack::SplitVertical,   Qt::SHIFT + Qt::Key_V );
59   myWorkstack->setAccel( QtxWorkstack::SplitHorizontal, Qt::SHIFT + Qt::Key_H );
60   myWorkstack->setAccel( QtxWorkstack::Close,           Qt::SHIFT + Qt::Key_C );
61
62   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
63   if ( resMgr ) {
64     myWorkstack->setIcon( QtxWorkstack::SplitVertical,   
65                           resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VSPLIT" ) ) );
66     myWorkstack->setIcon( QtxWorkstack::SplitHorizontal,
67                           resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HSPLIT" ) ) );
68     myWorkstack->setIcon( QtxWorkstack::Close,
69                           resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ));
70   }
71
72   connect( myWorkstack, SIGNAL( windowActivated( QWidget* ) ),
73            this, SLOT( onWindowActivated( QWidget* ) ) );
74
75   createActions();
76 }
77
78 /*!
79   Destructor.
80 */
81 STD_TabDesktop::~STD_TabDesktop()
82 {
83 }
84
85 /*!
86   \retval SUIT_ViewWindow - return const active window.
87 */
88 SUIT_ViewWindow* STD_TabDesktop::activeWindow() const
89 {
90   SUIT_ViewWindow* wnd = 0;
91
92   QWidget* wid = myWorkstack->activeWindow();
93   if ( wid && wid->inherits( "SUIT_ViewWindow" ) )
94     wnd = (SUIT_ViewWindow*)wid;
95
96   return wnd;
97 }
98
99 /*!
100   \retval QPtrList<SUIT_ViewWindow> - return const active window list.
101 */
102 QList<SUIT_ViewWindow*> STD_TabDesktop::windows() const
103 {
104   QList<SUIT_ViewWindow*> winList;
105
106   QWidgetList children = myWorkstack->windowList();
107   for ( QWidgetList::iterator it = children.begin(); it != children.end(); ++it )
108   {
109     if ( (*it)->inherits( "SUIT_ViewWindow" ) )
110       winList.append( (SUIT_ViewWindow*)*it );
111   }
112
113   return winList;
114 }
115
116 /*!
117   Insert new widget into desktop.
118 */
119 void STD_TabDesktop::addWindow( QWidget* w )
120 {
121   if ( !w || !workstack() )
122     return;
123
124   workstack()->addWindow( w );
125 }
126
127 /*!
128   Call method perform for operation \a type.
129 */
130 void STD_TabDesktop::windowOperation( const int type )
131 {
132   myWorkstackAction->perform( operationFlag( type ) );
133 }
134
135 /*!
136   Sets window operations by \a first ... parameters.
137 */
138 void STD_TabDesktop::setWindowOperations( const int first, ... )
139 {
140   va_list ints;
141         va_start( ints, first );
142
143         QList<int> typeList;
144
145         int cur = first;
146         while ( cur )
147         {
148           typeList.append( cur );
149                 cur = va_arg( ints, int );
150   }
151
152         setWindowOperations( typeList );
153 }
154
155 /*!
156   Sets window operations by variable \a opList - operation list.
157 */
158 void STD_TabDesktop::setWindowOperations( const QList<int>& opList )
159 {
160   int flags = 0;
161
162   for ( QList<int>::const_iterator it = opList.begin(); it != opList.end(); ++it )
163     flags = flags | operationFlag( *it );
164
165 //  myWorkstackAction->setItems( flags );
166 }
167
168 /*!
169   \retval QtxWorkstack pointer - Qt work stack.
170 */
171 QtxWorkstack* STD_TabDesktop::workstack() const
172 {
173   return myWorkstack;
174 }
175
176 /*!
177   Emit window activated.
178 */
179 void STD_TabDesktop::onWindowActivated( QWidget* w )
180 {
181   if ( w && w->inherits( "SUIT_ViewWindow" ) )
182     emit windowActivated( (SUIT_ViewWindow*)w );
183 }
184
185 /*!
186   Create actions for window.
187 */
188 void STD_TabDesktop::createActions()
189 {
190   if ( myWorkstackAction )
191     return;
192
193   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
194   if ( !resMgr )
195     return;
196
197   myWorkstackAction = new QtxWorkstackAction( workstack(), this );
198
199   myWorkstackAction->setMenuActions( QtxWorkstackAction::Split | QtxWorkstackAction::Windows );
200
201   // Split Horizontal
202   myWorkstackAction->setIcon( QtxWorkstackAction::SplitHorizontal,
203                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HSPLIT" ) ) );
204   myWorkstackAction->setText( QtxWorkstackAction::SplitHorizontal, tr( "MEN_DESK_WINDOW_HSPLIT" ) );
205   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitHorizontal, tr( "PRP_DESK_WINDOW_HSPLIT" ) );
206   myWorkstackAction->setAccel( QtxWorkstackAction::SplitHorizontal, Qt::SHIFT + Qt::Key_H );
207
208   // Split Vertical
209   myWorkstackAction->setIcon( QtxWorkstackAction::SplitVertical,
210                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VSPLIT" ) ) );
211   myWorkstackAction->setText( QtxWorkstackAction::SplitVertical, tr( "MEN_DESK_WINDOW_VSPLIT" ) );
212   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitVertical, tr( "PRP_DESK_WINDOW_VSPLIT" ) );
213   myWorkstackAction->setAccel( QtxWorkstackAction::SplitVertical,   Qt::SHIFT + Qt::Key_V );
214
215   QtxActionMenuMgr* mMgr = menuMgr();
216   if ( !mMgr )
217     return;
218
219   int winMenuId = mMgr->insert( tr( "MEN_DESK_WINDOW" ), -1, 100 );
220   mMgr->insert( myWorkstackAction, winMenuId, -1 );
221   mMgr->insert( QtxActionMenuMgr::separator(), winMenuId, -1 );
222 }
223
224 /*!
225   Convert STD_TabDesktop enumerations to QtxWorkstackAction
226 */
227 int STD_TabDesktop::operationFlag( const int type ) const
228 {
229   int res = 0;
230   switch ( type )
231   {
232   case SplitVertical:
233     res = QtxWorkstackAction::SplitVertical;
234     break;
235   case SplitHorizontal:
236     res = QtxWorkstackAction::SplitHorizontal;
237     break;
238   }
239
240   return res;
241 }