Salome HOME
0022171: EDF 2477 GUI: Redesign of the "split" action and addition of a "tile" action
[modules/gui.git] / src / STD / STD_TabDesktop.cxx
1 // Copyright (C) 2007-2013  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_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   \retval QPtrList<SUIT_ViewWindow> - return const active window list.
102 */
103 QList<SUIT_ViewWindow*> STD_TabDesktop::windows() const
104 {
105   QList<SUIT_ViewWindow*> winList;
106
107   QWidgetList children = myWorkstack->windowList();
108   for ( QWidgetList::iterator it = children.begin(); it != children.end(); ++it )
109   {
110     if ( (*it)->inherits( "SUIT_ViewWindow" ) )
111       winList.append( (SUIT_ViewWindow*)*it );
112   }
113
114   return winList;
115 }
116
117 /*!
118   Insert new widget into desktop.
119 */
120 void STD_TabDesktop::addWindow( QWidget* w )
121 {
122   if ( !w || !workstack() )
123     return;
124
125   workstack()->addWindow( w );
126 }
127
128 /*!
129   Call method perform for operation \a type.
130 */
131 void STD_TabDesktop::windowOperation( const int type )
132 {
133   myWorkstackAction->perform( operationFlag( type ) );
134 }
135
136 /*!
137   Sets window operations by \a first ... parameters.
138 */
139 void STD_TabDesktop::setWindowOperations( const int first, ... )
140 {
141   va_list ints;
142         va_start( ints, first );
143
144         QList<int> typeList;
145
146         int cur = first;
147         while ( cur )
148         {
149           typeList.append( cur );
150                 cur = va_arg( ints, int );
151   }
152
153         setWindowOperations( typeList );
154 }
155
156 /*!
157   Sets window operations by variable \a opList - operation list.
158 */
159 void STD_TabDesktop::setWindowOperations( const QList<int>& opList )
160 {
161   int flags = 0;
162
163   for ( QList<int>::const_iterator it = opList.begin(); it != opList.end(); ++it )
164     flags = flags | operationFlag( *it );
165
166 //  myWorkstackAction->setItems( flags );
167 }
168
169 /*!
170   \retval QtxWorkstack pointer - Qt work stack.
171 */
172 QtxWorkstack* STD_TabDesktop::workstack() const
173 {
174   return myWorkstack;
175 }
176
177 /*!
178   Emit window activated.
179 */
180 void STD_TabDesktop::onWindowActivated( QWidget* w )
181 {
182   if ( w && w->inherits( "SUIT_ViewWindow" ) )
183     emit windowActivated( (SUIT_ViewWindow*)w );
184 }
185
186 /*!
187   Create actions for window.
188 */
189 void STD_TabDesktop::createActions()
190 {
191   if ( myWorkstackAction )
192     return;
193
194   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
195   if ( !resMgr )
196     return;
197
198   myWorkstackAction = new QtxWorkstackAction( workstack(), this );
199
200   myWorkstackAction->setMenuActions( QtxWorkstackAction::Split | QtxWorkstackAction::Windows );
201
202   // Split Horizontal
203   myWorkstackAction->setIcon( QtxWorkstackAction::SplitHorizontal,
204                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_HSPLIT" ) ) );
205   myWorkstackAction->setText( QtxWorkstackAction::SplitHorizontal, tr( "MEN_DESK_WINDOW_HSPLIT" ) );
206   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitHorizontal, tr( "PRP_DESK_WINDOW_HSPLIT" ) );
207   myWorkstackAction->setAccel( QtxWorkstackAction::SplitHorizontal, Qt::ALT + Qt::SHIFT + Qt::Key_H );
208
209   // Split Vertical
210   myWorkstackAction->setIcon( QtxWorkstackAction::SplitVertical,
211                               resMgr->loadPixmap( "STD", tr( "ICON_DESK_WINDOW_VSPLIT" ) ) );
212   myWorkstackAction->setText( QtxWorkstackAction::SplitVertical, tr( "MEN_DESK_WINDOW_VSPLIT" ) );
213   myWorkstackAction->setStatusTip( QtxWorkstackAction::SplitVertical, tr( "PRP_DESK_WINDOW_VSPLIT" ) );
214   myWorkstackAction->setAccel( QtxWorkstackAction::SplitVertical,   Qt::ALT + Qt::SHIFT + Qt::Key_V );
215
216   QAction* anArrangeViewsAction = myWorkstackAction->getArrangeViewsAction();
217   if( anArrangeViewsAction )
218     connect( anArrangeViewsAction, SIGNAL( triggered() ), this, SLOT( onArrangeViews() ) );
219
220   QtxActionMenuMgr* mMgr = menuMgr();
221   if ( !mMgr )
222     return;
223
224   int winMenuId = mMgr->insert( tr( "MEN_DESK_WINDOW" ), -1, 100 );
225   mMgr->insert( anArrangeViewsAction, winMenuId, -1 );
226   mMgr->insert( myWorkstackAction, winMenuId, -1 );
227   mMgr->insert( QtxActionMenuMgr::separator(), winMenuId, -1 );
228 }
229
230 /*!
231   Emit Arrange Views menu activated.
232 */
233 void STD_TabDesktop::onArrangeViews()
234 {
235   QtxSplitDlg ArrangeViewsDlg( this, workstack(), ArrangeViews );
236   ArrangeViewsDlg.exec();
237 }
238
239 /*!
240   Convert STD_TabDesktop enumerations to QtxWorkstackAction
241 */
242 int STD_TabDesktop::operationFlag( const int type ) const
243 {
244   int res = 0;
245   switch ( type )
246   {
247   case SplitVertical:
248     res = QtxWorkstackAction::SplitVertical;
249     break;
250   case SplitHorizontal:
251     res = QtxWorkstackAction::SplitHorizontal;
252     break;
253   }
254
255   return res;
256 }