]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxWorkstackAction.cxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / Qtx / QtxWorkstackAction.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 // File:      QtxWorkstackAction.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "QtxWorkstackAction.h"
27
28 #include "QtxWorkstack.h"
29
30 #include <QMenu>
31 #include <QWidgetList>
32
33 /*!
34   \class QtxWorkstackAction
35   \brief Implements actions group for menu Windows with standard operations, like
36          "Split vertical", "Split horizontal", etc.
37 */
38
39 /*!
40   \brief Constructor.
41   \param ws workstack
42   \param parent parent object (owner of the action)
43 */
44 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent )
45 : QtxActionSet( parent ),
46   myWorkstack( ws ),
47   myWindowsFlag( true )
48 {
49   if ( myWorkstack )
50     insertAction( myWorkstack->action( QtxWorkstack::SplitVertical ), SplitVertical );
51   else
52     insertAction( new QtxAction( tr( "Split the active window on two vertical parts" ),
53                                  tr( "Split vertically" ), 0, this ), SplitVertical );
54
55   if ( myWorkstack )
56     insertAction( myWorkstack->action( QtxWorkstack::SplitHorizontal ), SplitHorizontal );
57   else
58     insertAction( new QtxAction( tr( "Split the active window on two horizontal parts" ),
59                                  tr( "Split horizontally" ), 0, this ), SplitHorizontal );
60
61   connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
62
63   setMenuActions( Standard );
64 }
65
66 /*!
67   \brief Destructor.
68 */
69 QtxWorkstackAction::~QtxWorkstackAction()
70 {
71 }
72
73 /*!
74   \brief Get workstack.
75   \return parent workstack
76 */
77 QtxWorkstack* QtxWorkstackAction::workstack() const
78 {
79   return myWorkstack;
80 }
81
82 /*!
83   \brief Set actions to be visible in the menu.
84
85   Actions, which IDs are set in \a flags parameter, will be shown in the
86   menu bar. Other actions will not be shown.
87
88   \param flags ORed together actions flags
89 */
90 void QtxWorkstackAction::setMenuActions( const int flags )
91 {
92   action( SplitVertical )->setVisible( flags & SplitVertical );
93   action( SplitHorizontal )->setVisible( flags & SplitHorizontal );
94   myWindowsFlag = flags & Windows;
95 }
96
97 /*!
98   \brief Get menu actions which are currently visible in the menu bar.
99   \return ORed together actions flags
100   \sa setMenuActions()
101 */
102 int QtxWorkstackAction::menuActions() const
103 {
104   int ret = 0;
105   ret = ret | ( action( SplitVertical )->isVisible() ? SplitVertical : 0 );
106   ret = ret | ( action( SplitHorizontal )->isVisible() ? SplitHorizontal : 0 );
107   ret = ret | ( myWindowsFlag ? Windows : 0 );
108   return ret;
109 }
110
111 /*!
112   \brief Get keyboard accelerator for the specified action.
113   \param id menu action ID
114   \return keyboard accelerator of menu item or 0 if there is no such action
115 */
116 int QtxWorkstackAction::accel( const int id ) const
117 {
118   int a = 0;
119   if ( action( id ) )
120     a = action( id )->shortcut();
121   return a;
122 }
123
124 /*!
125   \brief Get icon for the specified action.
126
127   If \a id is invalid, null icon is returned.
128
129   \param id menu action ID
130   \return menu item icon
131 */
132 QIcon QtxWorkstackAction::icon( const int id ) const
133 {
134   QIcon ico;
135   if ( action( id ) )
136     ico = action( id )->icon();
137   return ico;
138 }
139
140 /*!
141   \brief Get menu item text for the specified action.
142   \param id menu action ID
143   \return menu item text or null QString if there is no such action
144 */
145 QString QtxWorkstackAction::text( const int id ) const
146 {
147   QString txt;
148   if ( action( id ) )
149     txt = action( id )->text();
150   return txt;
151 }
152
153 /*!
154   \brief Get status bar tip for the specified action.
155   \param id menu action ID
156   \return status bar tip menu item or null QString if there is no such action
157 */
158 QString QtxWorkstackAction::statusTip( const int id ) const
159 {
160   QString txt;
161   if ( action( id ) )
162     txt = action( id )->statusTip();
163   return txt;
164 }
165
166 /*!
167   \brief Set keyboard accelerator for the specified action.
168   \param id menu action ID
169   \param a new keyboard accelerator
170 */
171 void QtxWorkstackAction::setAccel( const int id, const int a )
172 {
173   if ( action( id ) )
174     action( id )->setShortcut( a );
175 }
176
177 /*!
178   \brief Set menu item icon for the specified action.
179   \param id menu action ID
180   \param ico new menu item icon
181 */
182 void QtxWorkstackAction::setIcon( const int id, const QIcon& icon )
183 {
184   if ( action( id ) )
185     action( id )->setIcon( icon );
186 }
187
188 /*!
189   \brief Set menu item text for the specified action.
190   \param id menu action ID
191   \param txt new menu item text
192 */
193 void QtxWorkstackAction::setText( const int id, const QString& txt )
194 {
195   if ( action( id ) )
196     action( id )->setText( txt );
197 }
198
199 /*!
200   \brief Set menu item status bar tip for the specified action.
201   \param id menu action ID
202   \param txt new menu item status bar tip
203 */
204 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
205 {
206   if ( action( id ) )
207     action( id )->setStatusTip( txt );
208 }
209
210 /*!
211   \brief Process action activated by the user.
212   \param type action ID
213 */
214 void QtxWorkstackAction::perform( const int type )
215 {
216   /*
217   switch ( type )
218   {
219   case SplitVertical:
220     splitVertical();
221     break;
222   case SplitHorizontal:
223     splitHorizontal();
224     break;
225   }
226   */
227 }
228
229 /*!
230   \brief Split the window area in the workstack in the vertical direction.
231 */
232 void QtxWorkstackAction::splitVertical()
233 {
234   QtxWorkstack* ws = workstack();
235   if ( ws )
236     ws->splitVertical();
237 }
238
239 /*!
240   \brief Split the window area in the workstack in the horizontal direction.
241 */
242 void QtxWorkstackAction::splitHorizontal()
243 {
244   QtxWorkstack* ws = workstack();
245   if ( ws )
246     ws->splitHorizontal();
247 }
248
249 /*!
250   \brief Called when action is added to the menu bar.
251   \param w menu bar widget this action is being added to
252 */
253 void QtxWorkstackAction::addedTo( QWidget* w )
254 {
255   QtxActionSet::addedTo( w );
256
257   QMenu* pm = ::qobject_cast<QMenu*>( w );
258   if ( pm )
259     connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
260 }
261
262 /*!
263   \brief Called when action is removed from the menu bar.
264   \param w menu bar widget this action is being removed from
265 */
266 void QtxWorkstackAction::removedFrom( QWidget* w )
267 {
268   QtxActionSet::removedFrom( w );
269
270   QMenu* pm = ::qobject_cast<QMenu*>( w );
271   if ( pm )
272     disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
273 }
274
275 /*!
276   \brief Update all menu action state.
277 */
278 void QtxWorkstackAction::updateContent()
279 {
280   bool count = workstack() ? workstack()->splitWindowList().count() > 1 : 0;
281   action( SplitVertical )->setEnabled( count );
282   action( SplitHorizontal )->setEnabled( count );
283
284   updateWindows();
285 }
286
287 /*!
288   \brief Update actions which refer to the opened child windows.
289 */
290 void QtxWorkstackAction::updateWindows()
291 {
292   QtxWorkstack* ws = workstack();
293   if ( !ws )
294     return;
295
296   QList<QAction*> lst = actions();
297   for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
298   {
299     int id = actionId( *it );
300     if ( id >= Windows )
301       removeAction( *it );
302   }
303
304   bool base = action( SplitVertical )->isVisible() || action( SplitHorizontal )->isVisible();
305
306   QList<QAction*> items;
307   QMap<QAction*, int> map;
308   if ( menuActions() & Windows )
309   {
310     int index = 1;
311     QWidgetList wList = ws->windowList();
312     for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
313     {
314       QWidget* wid = *it;
315       QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
316       a->setChecked( wid == ws->activeWindow() );
317       items.append( a );
318       map.insert( a, Windows + index );
319     }
320
321     if ( base && !items.isEmpty() )
322     {
323       QAction* sep = new QtxAction( this );
324       sep->setSeparator( true );
325       items.prepend( sep );
326       map.insert( sep, Windows );
327     }
328   }
329
330   if ( !items.isEmpty() )
331     insertActions( items );
332
333   for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
334     setActionId( itr.key(), itr.value() );
335 }
336
337 /*!
338   \brief Called when parent menu is about to show.
339
340   Updates all menu items.
341 */
342 void QtxWorkstackAction::onAboutToShow()
343 {
344   QMenu* pm = ::qobject_cast<QMenu*>( sender() );
345   if ( pm )
346     updateContent();
347 }
348
349 /*!
350   \brief Called when menu item corresponding to some child window is activated.
351
352   Activates correposponding child window.
353
354   \param idx menu item index
355 */
356 void QtxWorkstackAction::activateItem( const int idx )
357 {
358   QtxWorkstack* ws = workstack();
359   if ( !ws )
360     return;
361
362   QWidgetList wList = ws->windowList();
363   if ( idx >= 0 && idx < (int)wList.count() )
364     wList.at( idx )->setFocus();
365 }
366
367 /*!
368   \brief Called when menu item is activated by the user.
369
370   Perform the corresponding action.
371
372   \param id menu item identifier
373 */
374 void QtxWorkstackAction::onTriggered( int id )
375 {
376   if ( id < Windows )
377     perform( id );
378   else
379     activateItem( id - Windows - 1 );
380 }