Salome HOME
[bos #35160][EDF](2023-T1) Keyboard shortcuts.
[modules/gui.git] / src / STD / STD_TabDesktop.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
60   if ( resMgr ) {
61     myWorkstack->setIcon( QtxWorkstack::SplitVertical,
62                           resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VSPLIT" ) ) );
63     myWorkstack->setIcon( QtxWorkstack::SplitHorizontal,
64                           resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HSPLIT" ) ) );
65     myWorkstack->setIcon( QtxWorkstack::Close,
66                           resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ));
67   }
68
69   connect( myWorkstack, SIGNAL( windowActivated( QWidget* ) ),
70            this, SLOT( onWindowActivated( QWidget* ) ) );
71
72   createActions();
73 }
74
75 /*!
76   Destructor.
77 */
78 STD_TabDesktop::~STD_TabDesktop()
79 {
80 }
81
82 /*!
83   \retval SUIT_ViewWindow - return const active window.
84 */
85 SUIT_ViewWindow* STD_TabDesktop::activeWindow() const
86 {
87   SUIT_ViewWindow* wnd = 0;
88
89   QWidget* wid = myWorkstack->activeWindow();
90   if ( wid && wid->inherits( "SUIT_ViewWindow" ) )
91     wnd = (SUIT_ViewWindow*)wid;
92
93   return wnd;
94 }
95
96 /*!
97   Set active window
98   \param wnd - view window
99 */
100 void STD_TabDesktop::setActiveWindow(SUIT_ViewWindow* wnd)
101 {
102   if (wnd) {
103     myWorkstack->setActiveWindow(wnd);
104     wnd->setFocus();
105   }
106 }
107
108 /*!
109   \retval QPtrList<SUIT_ViewWindow> - return const active window list.
110 */
111 QList<SUIT_ViewWindow*> STD_TabDesktop::windows() const
112 {
113   QList<SUIT_ViewWindow*> winList;
114
115   QWidgetList children = myWorkstack->windowList();
116   for ( QWidgetList::iterator it = children.begin(); it != children.end(); ++it )
117   {
118     if ( (*it)->inherits( "SUIT_ViewWindow" ) )
119       winList.append( (SUIT_ViewWindow*)*it );
120   }
121
122   return winList;
123 }
124
125 /*!
126   Insert new widget into desktop.
127 */
128 void STD_TabDesktop::addWindow( QWidget* w )
129 {
130   if ( !w || !workstack() )
131     return;
132
133   workstack()->addWindow( w );
134 }
135
136 /*!
137   Call method perform for operation \a type.
138 */
139 void STD_TabDesktop::windowOperation( const int type )
140 {
141   myWorkstackAction->perform( operationFlag( type ) );
142 }
143
144 /*!
145   Sets window operations by \a first ... parameters.
146 */
147 void STD_TabDesktop::setWindowOperations( const int first, ... )
148 {
149   va_list ints;
150         va_start( ints, first );
151
152         QList<int> typeList;
153
154         int cur = first;
155         while ( cur )
156         {
157           typeList.append( cur );
158                 cur = va_arg( ints, int );
159   }
160
161         setWindowOperations( typeList );
162 }
163
164 /*!
165   Sets window operations by variable \a opList - operation list.
166 */
167 void STD_TabDesktop::setWindowOperations( const QList<int>& opList )
168 {
169   int flags = 0;
170
171   for ( QList<int>::const_iterator it = opList.begin(); it != opList.end(); ++it )
172     flags = flags | operationFlag( *it );
173
174 //  myWorkstackAction->setItems( flags );
175 }
176
177 /*!
178   \retval QtxWorkstack pointer - Qt work stack.
179 */
180 QtxWorkstack* STD_TabDesktop::workstack() const
181 {
182   return myWorkstack;
183 }
184
185 /*!
186   Emit window activated.
187 */
188 void STD_TabDesktop::onWindowActivated( QWidget* w )
189 {
190   if ( w && w->inherits( "SUIT_ViewWindow" ) )
191     emit windowActivated( (SUIT_ViewWindow*)w );
192 }
193
194 /*!
195   Create actions for window.
196 */
197 void STD_TabDesktop::createActions()
198 {
199   if ( myWorkstackAction )
200     return;
201
202   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
203   if ( !resMgr )
204     return;
205
206   myWorkstackAction = new QtxWorkstackAction( workstack(), this );
207
208   myWorkstackAction->setMenuActions( QtxWorkstackAction::Split | QtxWorkstackAction::Windows );
209
210   // Split Horizontal
211   myWorkstackAction->setIcon( QtxWorkstackAction::SplitHorizontal,
212                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HSPLIT" ) ) );
213   myWorkstackAction->setText( QtxWorkstackAction::SplitHorizontal, tr( "MEN_DESK_WINDOW_HSPLIT" ) );
214   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitHorizontal, tr( "PRP_DESK_WINDOW_HSPLIT" ) );
215
216   // Split Vertical
217   myWorkstackAction->setIcon( QtxWorkstackAction::SplitVertical,
218                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VSPLIT" ) ) );
219   myWorkstackAction->setText( QtxWorkstackAction::SplitVertical, tr( "MEN_DESK_WINDOW_VSPLIT" ) );
220   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitVertical, tr( "PRP_DESK_WINDOW_VSPLIT" ) );
221
222   QAction* anArrangeViewsAction = myWorkstackAction->getArrangeViewsAction();
223   if( anArrangeViewsAction )
224     connect( anArrangeViewsAction, SIGNAL( triggered() ), this, SLOT( onArrangeViews() ) );
225
226   QtxActionMenuMgr* mMgr = menuMgr();
227   if ( !mMgr )
228     return;
229
230   int winMenuId = mMgr->insert( tr( "MEN_DESK_WINDOW" ), -1, 100 );
231   mMgr->insert( anArrangeViewsAction, winMenuId, -1 );
232   mMgr->insert( myWorkstackAction, winMenuId, -1 );
233   mMgr->insert( QtxActionMenuMgr::separator(), winMenuId, -1 );
234 }
235
236 /*!
237   Emit Arrange Views menu activated.
238 */
239 void STD_TabDesktop::onArrangeViews()
240 {
241   QtxSplitDlg ArrangeViewsDlg( this, workstack(), ArrangeViews );
242   ArrangeViewsDlg.exec();
243 }
244
245 /*!
246   Convert STD_TabDesktop enumerations to QtxWorkstackAction
247 */
248 int STD_TabDesktop::operationFlag( const int type ) const
249 {
250   int res = 0;
251   switch ( type )
252   {
253   case SplitVertical:
254     res = QtxWorkstackAction::SplitVertical;
255     break;
256   case SplitHorizontal:
257     res = QtxWorkstackAction::SplitHorizontal;
258     break;
259   }
260
261   return res;
262 }