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