]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxWorkstackAction.cxx
Salome HOME
c26a9b0aa45e3ae1efe253b56208194cb1d07cfd
[modules/gui.git] / src / Qtx / QtxWorkstackAction.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 // File:      QtxWorkstackAction.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxWorkstackAction.h"
23
24 #include "QtxWorkstack.h"
25
26 #include <qpopupmenu.h>
27 #include <qwidgetlist.h>
28
29 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent, const char* name )
30 : QtxAction( tr( "Controls windows into workstack" ), tr( "Workstack management" ), 0, parent, name ),
31 myFlags( Standard ),
32 myWorkstack( ws )
33 {
34   myItem.insert( VSplit, new QtxAction( tr( "Split the active window on two vertical parts" ),
35                                         tr( "Split vertically" ), 0, this, 0, false ) );
36   myItem.insert( HSplit, new QtxAction( tr( "Split the active window on two horizontal parts" ),
37                                         tr( "Split horizontally" ), 0, this, 0, false ) );
38
39   connect( myItem[VSplit], SIGNAL( activated() ), ws, SLOT( splitVertical() ) );
40   connect( myItem[HSplit], SIGNAL( activated() ), ws, SLOT( splitHorizontal() ) );
41 }
42
43 QtxWorkstackAction::~QtxWorkstackAction()
44 {
45 }
46
47 QtxWorkstack* QtxWorkstackAction::workstack() const
48 {
49   return myWorkstack;
50 }
51
52 int QtxWorkstackAction::items() const
53 {
54   return myFlags;
55 }
56
57 void QtxWorkstackAction::setItems( const int flags )
58 {
59   if ( !flags || flags == myFlags || !( flags & Split ) )
60     return;
61
62   myFlags = flags;
63 }
64
65 bool QtxWorkstackAction::hasItems( const int flags ) const
66 {
67   return ( myFlags & flags ) == flags;
68 }
69
70 int QtxWorkstackAction::accel( const int id ) const
71 {
72   int a = 0;
73   if ( myItem.contains( id ) )
74     a = myItem[id]->accel();
75   return a;
76 }
77
78 QIconSet QtxWorkstackAction::iconSet( const int id ) const
79 {
80   QIconSet ico;
81   if ( myItem.contains( id ) )
82     ico = myItem[id]->iconSet();
83   return ico;
84 }
85
86 QString QtxWorkstackAction::menuText( const int id ) const
87 {
88   QString txt;
89   if ( myItem.contains( id ) )
90     txt = myItem[id]->menuText();
91   return txt;
92 }
93
94 QString QtxWorkstackAction::statusTip( const int id ) const
95 {
96   QString txt;
97   if ( myItem.contains( id ) )
98     txt = myItem[id]->statusTip();
99   return txt;
100 }
101
102 void QtxWorkstackAction::setAccel( const int id, const int a )
103 {
104   if ( myItem.contains( id ) )
105     myItem[id]->setAccel( a );
106 }
107
108 void QtxWorkstackAction::setIconSet( const int id, const QIconSet& ico )
109 {
110   if ( myItem.contains( id ) )
111     myItem[id]->setIconSet( ico );
112 }
113
114 void QtxWorkstackAction::setMenuText( const int id, const QString& txt )
115 {
116   if ( myItem.contains( id ) )
117     myItem[id]->setMenuText( txt );
118 }
119
120 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
121 {
122   if ( myItem.contains( id ) )
123     myItem[id]->setStatusTip( txt );
124 }
125
126 bool QtxWorkstackAction::addTo( QWidget* wid )
127 {
128   return addTo( wid, -1 );
129 }
130
131 bool QtxWorkstackAction::addTo( QWidget* wid, const int idx )
132 {
133   if ( !wid || !wid->inherits( "QPopupMenu" ) )
134     return false;
135
136   QPopupMenu* pm = (QPopupMenu*)wid;
137   checkPopup( pm );
138
139   if ( myMenu.contains( pm ) )
140     return false;
141
142   myMenu.insert( pm, QIntList() );
143   fillPopup( pm, idx );
144
145   connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
146   connect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
147
148   return true;
149 }
150
151 bool QtxWorkstackAction::removeFrom( QWidget* wid )
152 {
153   if ( !wid || !wid->inherits( "QPopupMenu" ) )
154     return false;
155
156   QPopupMenu* pm = (QPopupMenu*)wid;
157   if ( !myMenu.contains( pm ) )
158     return false;
159
160   clearPopup( pm );
161
162   disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
163   disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
164
165   myMenu.remove( pm );
166
167   return true;
168 }
169
170 void QtxWorkstackAction::perform( const int type )
171 {
172   switch ( type )
173   {
174   case VSplit:
175     workstack()->splitVertical();
176     break;
177   case HSplit:
178     workstack()->splitHorizontal();
179     break;
180   }
181 }
182
183 void QtxWorkstackAction::onAboutToShow()
184 {
185   const QObject* obj = sender();
186   if ( !obj || !obj->inherits( "QPopupMenu" ) )
187     return;
188
189   QtxWorkstack* ws = workstack();
190   if ( ws && myItem.contains( VSplit ) )
191     myItem[VSplit]->setAccel( ws->accel( QtxWorkstack::SplitVertical ) );
192   if ( ws && myItem.contains( HSplit ) )
193     myItem[HSplit]->setAccel( ws->accel( QtxWorkstack::SplitHorizontal ) );
194
195   updatePopup( (QPopupMenu*)obj );
196 }
197
198 void QtxWorkstackAction::onPopupDestroyed( QObject* obj )
199 {
200   myMenu.remove( (QPopupMenu*)obj );
201 }
202
203 void QtxWorkstackAction::checkPopup( QPopupMenu* pm )
204 {
205   if ( !myMenu.contains( pm ) )
206     return;
207
208   QIntList updList;
209   for ( QIntList::const_iterator it = myMenu[pm].begin(); it != myMenu[pm].end(); ++it )
210   {
211     if ( pm->indexOf( *it ) != -1 )
212       updList.append( *it );
213   }
214
215   myMenu.remove( pm );
216
217   if ( !updList.isEmpty() )
218     myMenu.insert( pm, updList );
219   else
220   {
221     disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
222     disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onPopupDestroyed( QObject* ) ) );
223   }
224 }
225
226 void QtxWorkstackAction::updatePopup( QPopupMenu* pm )
227 {
228   if ( !myMenu.contains( pm ) )
229     return;
230
231   fillPopup( pm, clearPopup( pm ) );
232
233   int count = workstack() ? workstack()->splitWindowList().count() : 0;
234   myItem[VSplit]->setEnabled( count > 1 );
235   myItem[HSplit]->setEnabled( count > 1 );
236 }
237
238 int QtxWorkstackAction::clearPopup( QPopupMenu* pm )
239 {
240   if ( !myMenu.contains( pm ) )
241     return -1;
242
243   int idx = -1;
244   const QIntList& lst = myMenu[pm];
245   for ( QIntList::const_iterator it = lst.begin(); it != lst.end() && idx == -1; ++it )
246     idx = pm->indexOf( *it );
247
248   for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
249     mit.data()->removeFrom( pm );
250
251   for ( QIntList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
252     pm->removeItem( *itr );
253
254   return idx;
255 }
256
257 void QtxWorkstackAction::fillPopup( QPopupMenu* pm, const int idx )
258 {
259   if ( !pm )
260     return;
261
262   int index = idx < 0 ? pm->count() : QMIN( (int)pm->count(), idx );
263
264   myMenu.insert( pm, QIntList() );
265   QIntList& lst = myMenu[pm];
266
267   for ( ItemMap::ConstIterator mit = myItem.begin(); mit != myItem.end(); ++mit )
268   {
269     if ( !hasItems( mit.key() ) )
270       continue;
271
272     mit.data()->addTo( pm, index );
273     lst.append( pm->idAt( index++ ) );
274   }
275
276   QtxWorkstack* ws = workstack();
277   if ( !ws || !hasItems( Windows ) )
278     return;
279
280   QWidgetList wList = ws->windowList();
281   if ( wList.isEmpty() )
282     return;
283
284   lst.append( pm->insertSeparator( index++ ) );
285
286   int param = 0;
287   pm->setCheckable( true );
288   for ( QWidgetListIt it( wList ); it.current(); ++it )
289   {
290     int id = pm->insertItem( it.current()->caption(), this, SLOT( onItemActivated( int ) ), 0, -1, index++ );
291     pm->setItemParameter( id, param++ );
292     pm->setItemChecked( id, it.current() == ws->activeWindow() );
293     lst.append( id );
294   }
295 }
296
297 void QtxWorkstackAction::onItemActivated( int idx )
298 {
299   QtxWorkstack* ws = workstack();
300   if ( !ws )
301     return;
302
303   QWidgetList wList = ws->windowList();
304   if ( idx < 0 || idx >= (int)wList.count() )
305     return;
306
307   wList.at( idx )->setFocus();
308 }