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