Salome HOME
PAL10125 - by double click on reference original object becomes selected
[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: LogoBox
20  Level: Internal
21 */
22
23 class LogoBox : public QHBox
24 {
25 public:
26   LogoBox( QWidget* parent = 0, const char* name = 0, WFlags f = 0 ) : QHBox( parent, name, f )
27   {
28     setFrameStyle( Plain | NoFrame );
29     setMargin( 0 ); setSpacing( 2 );
30   }
31   void addSpacing( int spacing )
32   {
33     QApplication::sendPostedEvents( this, QEvent::ChildInserted );
34     ((QHBoxLayout*)layout())->addSpacing( spacing );
35   }
36 protected:
37   void drawContents( QPainter* p )
38   {
39     if ( parentWidget()->inherits( "QMenuBar" ) )
40       style().drawControl( QStyle::CE_MenuBarEmptyArea, p, this, contentsRect(), colorGroup() );
41     else
42       QHBox::drawContents( p );
43   }
44 };
45
46 /*!
47  Class: SUIT_Desktop::LogoManager
48  Level: Internal
49 */
50
51 SUIT_Desktop::LogoManager::LogoManager( SUIT_Desktop* desktop)
52 : myDesktop( desktop ), myId( 0 ) 
53 {
54
55
56 void SUIT_Desktop::LogoManager::addLogo( const QString& logoID, const QPixmap& logo )
57 {
58   if ( !myDesktop || logo.isNull() )
59     return;
60   myLogoMap[ logoID ] = logo;
61   generateLogo();
62 }
63
64 void SUIT_Desktop::LogoManager::removeLogo( const QString& logoID )
65 {
66   if ( !myDesktop || myLogoMap.find( logoID ) == myLogoMap.end() )
67     return;
68   myLogoMap.remove( logoID );
69   generateLogo();
70 }
71
72 void SUIT_Desktop::LogoManager::clearLogo()
73 {
74   myLogoMap.clear();
75   generateLogo();
76 }
77
78 void SUIT_Desktop::LogoManager::generateLogo()
79 {
80   if ( !myDesktop ) return;
81
82   if ( myId ) 
83     myDesktop->menuBar()->removeItem( myId );
84   myId = 0;
85
86   if ( !myLogoMap.count() )
87     return;
88
89   LogoBox* cnt = new LogoBox( myDesktop );
90   
91   QMap<QString, QPixmap>::Iterator it;
92   for ( it = myLogoMap.begin(); it != myLogoMap.end(); ++it ) {
93     QLabel* logoLab = new QLabel( cnt );
94     logoLab->setPixmap( *it );
95     logoLab->setAlignment( QLabel::AlignCenter ); 
96     logoLab->setScaledContents( false );
97   }
98   cnt->addSpacing( 2 );
99
100   myId = myDesktop->menuBar()->insertItem( cnt );
101   QApplication::sendPostedEvents( myDesktop->menuBar()->parentWidget(), QEvent::LayoutHint );
102   QApplication::postEvent( myDesktop->menuBar()->parentWidget(), new QEvent( QEvent::LayoutHint ) );
103 }
104
105
106 /*!\class SUIT_Desktop
107  * Provide desktop management:\n
108  * \li menu manager
109  * \li tool manager
110  * \li windows
111  */
112
113
114 /*!
115   Constructor.
116 */
117 SUIT_Desktop::SUIT_Desktop()
118 : QtxMainWindow(), myLogoMan( this )
119 {
120   myMenuMgr = new QtxActionMenuMgr( this );
121   myToolMgr = new QtxActionToolMgr( this );
122 }
123
124 /*!
125   Destructor.
126 */
127 SUIT_Desktop::~SUIT_Desktop()
128 {
129 }
130
131 /*!
132   Emit on event \a e.
133 */
134 bool SUIT_Desktop::event( QEvent* e )
135 {
136   if ( !e )
137     return false;
138
139   switch ( e->type() )
140   {
141   case QEvent::WindowActivate:
142     emit activated();
143     break;
144   case QEvent::WindowDeactivate:
145     emit deactivated();
146     break;
147   }
148
149   return QMainWindow::event( e );
150 }
151
152 /*!
153   Close event \a e.
154 */
155 void SUIT_Desktop::closeEvent( QCloseEvent* e )
156 {
157   emit closing( this, e );
158   e->ignore();
159 }
160
161 /*!
162   Child event.
163 */
164 void SUIT_Desktop::childEvent( QChildEvent* e )
165 {
166   if ( e->type() == QEvent::ChildInserted && parentArea() &&
167        e->child()->isWidgetType() && e->child()->inherits( "SUIT_ViewWindow" ) )
168     ((QWidget*)e->child())->reparent( parentArea(), QPoint( 0, 0 ), true );
169   else
170     QtxMainWindow::childEvent( e );
171 }
172
173 /*!
174   Gets menu manager.
175 */
176 QtxActionMenuMgr* SUIT_Desktop::menuMgr() const
177 {
178   return myMenuMgr;
179 }
180
181 /*!
182   Gets tool manager.
183 */
184 QtxActionToolMgr* SUIT_Desktop::toolMgr() const
185 {
186   return myToolMgr;
187 }
188
189 /*!
190   Adds new logo to the menu bar area
191 */
192 void SUIT_Desktop::addLogo( const QString& logoID, const QPixmap& logo )
193 {
194   myLogoMan.addLogo( logoID, logo );
195 }
196
197 /*!
198   Removes a logo
199 */
200 void SUIT_Desktop::removeLogo( const QString& logoID )
201 {
202   myLogoMan.removeLogo( logoID );
203 }
204
205 /*!
206   Removes all logos 
207 */
208 void SUIT_Desktop::clearLogo()
209 {
210   myLogoMan.clearLogo();
211 }
212
213