Salome HOME
no message
[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     ((QWidget*)e->child())->reparent( parentArea(), QPoint( 0, 0 ), true );
243   else
244     QtxMainWindow::childEvent( e );
245 }
246
247 /*!
248   Gets menu manager.
249 */
250 QtxActionMenuMgr* SUIT_Desktop::menuMgr() const
251 {
252   return myMenuMgr;
253 }
254
255 /*!
256   Gets tool manager.
257 */
258 QtxActionToolMgr* SUIT_Desktop::toolMgr() const
259 {
260   return myToolMgr;
261 }
262
263 /*!
264   Returns the count of the existed logos.
265 */
266 int SUIT_Desktop::logoCount() const
267 {
268   if ( !myLogoMgr )
269     return 0;
270   else
271     return myLogoMgr->count();
272 }
273
274 /*!
275   Adds new logo to the menu bar area.
276   Obsolete. Not should be used.
277   Use SUIT_Desktop::logoInsert();
278 */
279 void SUIT_Desktop::addLogo( const QString& id, const QPixmap& pix )
280 {
281   logoInsert( id, pix );
282 }
283
284 /*!
285   Removes a logo.
286   Obsolete. Not should be used.
287   Use SUIT_Desktop::logoRemove();
288 */
289 void SUIT_Desktop::removeLogo( const QString& id )
290 {
291   logoRemove( id );
292 }
293
294 /*!
295   Adds new logo to the menu bar area
296 */
297 void SUIT_Desktop::logoInsert( const QString& logoID, const QPixmap& logo, const int idx )
298 {
299   if ( myLogoMgr )
300     myLogoMgr->insert( logoID, logo, idx );
301 }
302
303 /*!
304   Removes a logo
305 */
306 void SUIT_Desktop::logoRemove( const QString& logoID )
307 {
308   if ( myLogoMgr )
309     myLogoMgr->remove( logoID );
310 }
311
312 /*!
313   Removes all logos 
314 */
315 void SUIT_Desktop::logoClear()
316 {
317   if ( myLogoMgr )
318     myLogoMgr->clear();
319 }
320
321