Salome HOME
Logos spacing reduced
[modules/gui.git] / src / Qtx / QtxLogoMgr.cxx
1 #include "QtxLogoMgr.h"
2
3 #include <qhbox.h>
4 #include <qlabel.h>
5 #include <qstyle.h>
6 #include <qimage.h>
7 #include <qbitmap.h>
8 #include <qlayout.h>
9 #include <qmenubar.h>
10 #include <qapplication.h>
11
12 QtxLogoMgr::QtxLogoMgr( QMenuBar* mb )
13 : QObject( mb ),
14 myMenus( mb ),
15 myId( 0 )
16 {
17 }
18
19 QtxLogoMgr::~QtxLogoMgr()
20 {
21 }
22
23 /*!
24   Returns the menubar.
25 */
26 QMenuBar* QtxLogoMgr::menuBar() const
27 {
28   return myMenus;
29 }
30
31 /*!
32   Returns the count of the existed logos.
33 */
34 int QtxLogoMgr::count() const
35 {
36   return myLogos.count();
37 }
38
39 /*!
40   Insert new logo to the menu bar area
41 */
42 void QtxLogoMgr::insert( const QString& id, const QPixmap& pix, const int index )
43 {
44   if ( pix.isNull() )
45     return;
46
47   LogoInfo* inf = 0;
48
49   int idx = find( id );
50   if ( idx < 0 )
51   {
52     idx = index < (int)myLogos.count() ? index : -1;
53     if ( idx < 0 )
54       inf = &( *myLogos.append( LogoInfo() ) );
55     else
56       inf = &( *myLogos.insert( myLogos.at( idx ), LogoInfo() ) );
57   }
58   else
59     inf = &( *myLogos.at( idx ) );
60
61
62   inf->id = id;
63   inf->pix = pix;
64
65   generate();
66 }
67
68 /*!
69   Removes a logo
70 */
71 void QtxLogoMgr::remove( const QString& id )
72 {
73   int idx = find( id );
74   if ( idx < 0 )
75     return;
76
77   myLogos.remove( myLogos.at( idx ) );
78
79   generate();
80 }
81
82 /*!
83   Removes all logos 
84 */
85 void QtxLogoMgr::clear()
86 {
87   myLogos.clear();
88   generate();
89 }
90
91 void QtxLogoMgr::generate()
92 {
93   if ( !menuBar() )
94     return;
95
96   if ( myId ) 
97     menuBar()->removeItem( myId );
98
99   myId = 0;
100
101   if ( myLogos.isEmpty() )
102     return;
103
104   class LogoBox : public QHBox
105   {
106   public:
107     LogoBox( QWidget* parent = 0, const char* name = 0, WFlags f = 0 ) : QHBox( parent, name, f ) {};
108
109     void addSpacing( int spacing )
110     {
111       QApplication::sendPostedEvents( this, QEvent::ChildInserted );
112       ((QHBoxLayout*)layout())->addSpacing( spacing );
113     }
114
115   protected:
116     void drawContents( QPainter* p )
117     {
118       if ( parentWidget()->inherits( "QMenuBar" ) )
119         style().drawControl( QStyle::CE_MenuBarEmptyArea, p, this, contentsRect(), colorGroup() );
120       else
121         QHBox::drawContents( p );
122     }
123   };
124
125   LogoBox* cnt = new LogoBox( menuBar() );
126   cnt->setSpacing( 3 );
127
128   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
129   {
130     QPixmap pix = (*it).pix;
131     if ( !pix.mask() )
132     {
133       QImage img = pix.convertToImage();
134       QBitmap bm;
135       if ( img.hasAlphaBuffer() )
136         bm = img.createAlphaMask();
137       else
138         bm = img.createHeuristicMask();
139       pix.setMask( bm );
140     }
141
142     QLabel* logoLab = new QLabel( cnt );
143     logoLab->setPixmap( (*it).pix );
144     logoLab->setScaledContents( false );
145     logoLab->setAlignment( QLabel::AlignCenter ); 
146
147     if ( pix.mask() )
148           logoLab->setMask( *pix.mask() );
149   }
150
151   QApplication::sendPostedEvents( cnt, QEvent::ChildInserted );
152   cnt->addSpacing( 2 );
153
154   myId = menuBar()->insertItem( cnt );
155
156   QApplication::sendPostedEvents( menuBar()->parentWidget(), QEvent::LayoutHint );
157   QApplication::postEvent( menuBar()->parentWidget(), new QEvent( QEvent::LayoutHint ) );
158 }
159
160 int QtxLogoMgr::find( const QString& id ) const
161 {
162   int idx = -1;
163   for ( uint i = 0; i < myLogos.count() && idx < 0; i++ )
164   {
165     if ( (*myLogos.at( i ) ).id == id )
166       idx = i;
167   }
168   return idx;
169 }