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