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