Salome HOME
Don't allow to show hidden child widgets.
[modules/gui.git] / src / SUIT / SUIT_Desktop.cxx
1 #include "SUIT_Desktop.h"
2
3 #include "SUIT_Tools.h"
4 #include "SUIT_ViewWindow.h"
5
6 #include <QtxActionMenuMgr.h>
7 #include <QtxActionToolMgr.h>
8
9 #include <qtoolbar.h>
10 #include <qmenubar.h>
11 #include <qdockarea.h>
12 #include <qstatusbar.h>
13 #include <qapplication.h>
14 #include <qhbox.h>
15 #include <qlabel.h>
16 #include <qstyle.h>
17
18 /*!
19  Class: SUIT_Desktop::LogoMgr
20  Level: Internal
21 */
22
23 class SUIT_Desktop::LogoMgr : public QObject
24 {
25 public:
26   LogoMgr( QMenuBar* );
27   virtual ~LogoMgr();
28
29   int                    count() const;
30
31   void                   insert( const QString&, const QPixmap&, const int = -1 );
32   void                   remove( const QString& );
33   void                   clear();
34
35   QMenuBar*              menuBar() const;
36
37 private:
38   void                   generate();
39   int                    find( const QString& ) const;
40
41 private:
42   typedef struct { QString id; QPixmap pix; } LogoInfo;
43   typedef QValueList<LogoInfo>                LogoList;
44
45 private:
46   int                    myId;
47   QMenuBar*              myMenus;
48   LogoList               myLogos;
49 };
50
51 SUIT_Desktop::LogoMgr::LogoMgr( QMenuBar* mb )
52 : QObject( mb ),
53 myMenus( mb ),
54 myId( 0 )
55 {
56
57
58 SUIT_Desktop::LogoMgr::~LogoMgr()
59 {
60 }
61
62 QMenuBar* SUIT_Desktop::LogoMgr::menuBar() const
63 {
64   return myMenus;
65 }
66
67 int SUIT_Desktop::LogoMgr::count() const
68 {
69   return myLogos.count();
70 }
71
72 void SUIT_Desktop::LogoMgr::insert( const QString& id, const QPixmap& pix, const int index )
73 {
74   if ( pix.isNull() )
75     return;
76
77   LogoInfo* inf = 0;
78
79   int idx = find( id );
80   if ( idx < 0 )
81   {
82     idx = index < (int)myLogos.count() ? index : -1;
83     if ( idx < 0 )
84       inf = &( *myLogos.append( LogoInfo() ) );
85     else
86       inf = &( *myLogos.insert( myLogos.at( idx ), LogoInfo() ) );
87   }
88   else
89     inf = &( *myLogos.at( idx ) );
90
91
92   inf->id = id;
93   inf->pix = pix;
94
95   generate();
96 }
97
98 void SUIT_Desktop::LogoMgr::remove( const QString& id )
99 {
100   int idx = find( id );
101   if ( idx < 0 )
102     return;
103
104   myLogos.remove( myLogos.at( idx ) );
105
106   generate();
107 }
108
109 void SUIT_Desktop::LogoMgr::clear()
110 {
111   myLogos.clear();
112   generate();
113 }
114
115 void SUIT_Desktop::LogoMgr::generate()
116 {
117   if ( !menuBar() )
118     return;
119
120   if ( myId ) 
121     menuBar()->removeItem( myId );
122
123   myId = 0;
124
125   if ( myLogos.isEmpty() )
126     return;
127
128   class LogoBox : public QHBox
129   {
130   public:
131     LogoBox( QWidget* parent = 0, const char* name = 0, WFlags f = 0 ) : QHBox( parent, name, f ) {};
132
133     void addSpacing( int spacing )
134     {
135       QApplication::sendPostedEvents( this, QEvent::ChildInserted );
136       ((QHBoxLayout*)layout())->addSpacing( spacing );
137     }
138
139   protected:
140     void drawContents( QPainter* p )
141     {
142       if ( parentWidget()->inherits( "QMenuBar" ) )
143         style().drawControl( QStyle::CE_MenuBarEmptyArea, p, this, contentsRect(), colorGroup() );
144       else
145         QHBox::drawContents( p );
146     }
147   };
148
149   LogoBox* cnt = new LogoBox( menuBar() );
150   cnt->setSpacing( 2 );
151
152   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
153   {
154     QLabel* logoLab = new QLabel( cnt );
155     logoLab->setPixmap( (*it).pix );
156     logoLab->setScaledContents( false );
157     logoLab->setAlignment( QLabel::AlignCenter ); 
158   }
159   QApplication::sendPostedEvents( cnt, QEvent::ChildInserted );
160   cnt->addSpacing( 2 );
161
162   myId = menuBar()->insertItem( cnt );
163
164   QApplication::sendPostedEvents( menuBar()->parentWidget(), QEvent::LayoutHint );
165   QApplication::postEvent( menuBar()->parentWidget(), new QEvent( QEvent::LayoutHint ) );
166 }
167
168 int SUIT_Desktop::LogoMgr::find( const QString& id ) const
169 {
170   int idx = -1;
171   for ( uint i = 0; i < myLogos.count() && idx < 0; i++ )
172   {
173     if ( (*myLogos.at( i ) ).id == id )
174       idx = i;
175   }
176   return idx;
177 }
178
179 /*!\class SUIT_Desktop
180  * Provide desktop management:\n
181  * \li menu manager
182  * \li tool manager
183  * \li windows
184  */
185
186
187 /*!
188   Constructor.
189 */
190 SUIT_Desktop::SUIT_Desktop()
191 : QtxMainWindow()
192 {
193   myMenuMgr = new QtxActionMenuMgr( this );
194   myToolMgr = new QtxActionToolMgr( this );
195   myLogoMgr = new LogoMgr( menuBar() );
196 }
197
198 /*!
199   Destructor.
200 */
201 SUIT_Desktop::~SUIT_Desktop()
202 {
203 }
204
205 /*!
206   Emit on event \a e.
207 */
208 bool SUIT_Desktop::event( QEvent* e )
209 {
210   if ( !e )
211     return false;
212
213   switch ( e->type() )
214   {
215   case QEvent::WindowActivate:
216     emit activated();
217     break;
218   case QEvent::WindowDeactivate:
219     emit deactivated();
220     break;
221   }
222
223   return QMainWindow::event( e );
224 }
225
226 /*!
227   Close event \a e.
228 */
229 void SUIT_Desktop::closeEvent( QCloseEvent* e )
230 {
231   emit closing( this, e );
232   e->ignore();
233 }
234
235 /*!
236   Child event.
237 */
238 void SUIT_Desktop::childEvent( QChildEvent* e )
239 {
240   if ( e->type() == QEvent::ChildInserted && parentArea() &&
241        e->child()->isWidgetType() && e->child()->inherits( "SUIT_ViewWindow" ) )
242   {
243     QWidget* wid = (QWidget*)e->child();
244     bool vis = wid->isVisibleTo( wid->parentWidget() );
245     wid->reparent( parentArea(), QPoint( 0, 0 ), vis );
246     wid->setShown( vis );
247   }
248   else
249     QtxMainWindow::childEvent( e );
250 }
251
252 /*!
253   Gets menu manager.
254 */
255 QtxActionMenuMgr* SUIT_Desktop::menuMgr() const
256 {
257   return myMenuMgr;
258 }
259
260 /*!
261   Gets tool manager.
262 */
263 QtxActionToolMgr* SUIT_Desktop::toolMgr() const
264 {
265   return myToolMgr;
266 }
267
268 /*!
269   Returns the count of the existed logos.
270 */
271 int SUIT_Desktop::logoCount() const
272 {
273   if ( !myLogoMgr )
274     return 0;
275   else
276     return myLogoMgr->count();
277 }
278
279 /*!
280   Adds new logo to the menu bar area.
281   Obsolete. Not should be used.
282   Use SUIT_Desktop::logoInsert();
283 */
284 void SUIT_Desktop::addLogo( const QString& id, const QPixmap& pix )
285 {
286   logoInsert( id, pix );
287 }
288
289 /*!
290   Removes a logo.
291   Obsolete. Not should be used.
292   Use SUIT_Desktop::logoRemove();
293 */
294 void SUIT_Desktop::removeLogo( const QString& id )
295 {
296   logoRemove( id );
297 }
298
299 /*!
300   Adds new logo to the menu bar area
301 */
302 void SUIT_Desktop::logoInsert( const QString& logoID, const QPixmap& logo, const int idx )
303 {
304   if ( myLogoMgr )
305     myLogoMgr->insert( logoID, logo, idx );
306 }
307
308 /*!
309   Removes a logo
310 */
311 void SUIT_Desktop::logoRemove( const QString& logoID )
312 {
313   if ( myLogoMgr )
314     myLogoMgr->remove( logoID );
315 }
316
317 /*!
318   Removes all logos 
319 */
320 void SUIT_Desktop::logoClear()
321 {
322   if ( myLogoMgr )
323     myLogoMgr->clear();
324 }
325
326