]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxLogoMgr.cxx
Salome HOME
bed55bebccb3219d47c68a457b8a4e63c09633f1
[modules/gui.git] / src / Qtx / QtxLogoMgr.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:      QtxLogoMgr.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxLogoMgr.h"
26
27 #include <QLabel>
28 #include <QImage>
29 #include <QBitmap>
30 #include <QHBoxLayout>
31 #include <QMenuBar>
32 #include <QPointer>
33 #include <QApplication>
34 #include <QMovie>
35
36 /*!
37   \class QtxLogoMgr::LogoBox
38   \internal
39   \brief Logo images container.
40 */
41
42 class QtxLogoMgr::LogoBox : public QWidget
43 {
44 public:
45   LogoBox( QMenuBar* );
46
47   QMenuBar*      menuBar() const;
48   virtual bool   eventFilter( QObject*, QEvent* );
49   void           setLabels( const QList<QLabel*>& );
50
51 protected:
52   virtual void   customEvent( QEvent* );
53
54 private:
55   void           updateCorner();
56   void           updateContents();
57
58 private:
59   typedef QPointer<QWidget> WidgetPtr;
60
61 private:
62   QMenuBar*      myMB;       //!< parent menu bar
63   QList<QLabel*> myLabels;   //!< list of labels containing logo images
64   WidgetPtr      myCornWid;  //!< corner widget
65 };
66
67 /*!
68   \brief Constructor.
69   \param mb menu bar
70 */
71 QtxLogoMgr::LogoBox::LogoBox( QMenuBar* mb )
72 : QWidget( mb ),
73   myMB( mb ),
74   myCornWid( 0 )
75 {
76   myMB->installEventFilter( this );
77   updateCorner();
78 }
79
80 /*!
81   \brief Get menu bar.
82   \return menu bar
83 */
84 QMenuBar* QtxLogoMgr::LogoBox::menuBar() const
85 {
86   return myMB;
87 }
88
89 /*!
90   \brief Custom event filter.
91   \param o event receiver object
92   \param e event sent to object
93   \return \c true if further event processing should be stopped
94 */
95 bool QtxLogoMgr::LogoBox::eventFilter( QObject* o, QEvent* e )
96 {
97   if ( o != menuBar() )
98     return false;
99
100   if ( e->type() == QEvent::MenubarUpdated || e->type() == QEvent::Resize )
101     updateCorner();
102
103   if ( e->type() == QEvent::ChildAdded || e->type() == QEvent::ChildRemoved )
104   {
105     updateCorner();
106     QApplication::postEvent( this, new QEvent( QEvent::User ) );
107   }
108
109   return false;
110 }
111
112 /*!
113   \brief Set label widgets (logo containers).
114   \param labs list of labels
115 */
116 void QtxLogoMgr::LogoBox::setLabels( const QList<QLabel*>& labs )
117 {
118   for ( QList<QLabel*>::iterator it = myLabels.begin(); it != myLabels.end(); ++it )
119   {
120     if ( !labs.contains( *it ) )
121       delete *it;
122   }
123
124   myLabels = labs;
125   updateContents();
126 }
127
128 /*!
129   \brief Custom event processing (update logo widget).
130   \param e event (not used)
131 */
132 void QtxLogoMgr::LogoBox::customEvent( QEvent* /*e*/ )
133 {
134   updateCorner();
135 }
136
137 /*!
138   \brief Update menu bar's corner widget.
139 */
140 void QtxLogoMgr::LogoBox::updateCorner()
141 {
142   if ( menuBar()->cornerWidget() == this )
143     return;
144
145   myCornWid = menuBar()->cornerWidget();
146   myMB->setCornerWidget( this );
147   updateContents();
148 }
149
150 /*!
151   \brief Update logo manager contents.
152 */
153 void QtxLogoMgr::LogoBox::updateContents()
154 {
155   if ( layout() )
156     delete layout();
157
158   QHBoxLayout* base = new QHBoxLayout( this );
159   base->setMargin( 0 );
160   base->setSpacing( 3 );
161
162   for ( QList<QLabel*>::const_iterator it = myLabels.begin(); it != myLabels.end(); ++it )
163     base->addWidget( *it );
164
165   if ( myCornWid )
166     base->addWidget( myCornWid );
167
168   QApplication::sendPostedEvents();
169 }
170
171 /*!
172   \class QtxLogoMgr
173   \brief Provides a way to install logo pictures to the application main window.
174
175   The class includes the following functionality:
176   - add the logo image
177   - remove logo image
178   - support static images and animated images (QMovie)
179   - start/stop and pause/resume the animated logos
180 */
181
182 /*!
183   \brief Constructor.
184   \param mb parent menu bar
185 */
186 QtxLogoMgr::QtxLogoMgr( QMenuBar* mb )
187 : QObject( mb )
188 {
189   myBox = new LogoBox( mb );
190 }
191
192 /*!
193   \brief Destructor.
194 */
195 QtxLogoMgr::~QtxLogoMgr()
196 {
197 }
198
199 /*!
200   \brief Get menu bar.
201   \return parent menu bar
202 */
203 QMenuBar* QtxLogoMgr::menuBar() const
204 {
205   return myBox->menuBar();
206 }
207
208 /*!
209   \brief Get number of logo images.
210   \return current number of logo images
211 */
212 int QtxLogoMgr::count() const
213 {
214   return myLogos.count();
215 }
216
217 /*!
218   \brief Insert new logo pixmap to the menu bar area.
219   \param id unique string identifier of the logo
220   \param pix logo pixmap
221   \param index logo position (if < 0, logo is added to the end)
222 */
223 void QtxLogoMgr::insert( const QString& id, const QPixmap& pix, const int index )
224 {
225   if ( pix.isNull() )
226     return;
227
228   LogoInfo& inf = insert( id, index );
229
230   inf.pix = pix;
231
232   generate();
233 }
234
235 /*!
236   \brief Insert new animated logo to the menu bar area.
237   \param id unique string identifier of the logo
238   \param pix logo movie
239   \param index logo position (if < 0, logo is added to the end)
240 */
241 void QtxLogoMgr::insert( const QString& id, QMovie* movie, const int index )
242 {
243   if ( !movie )
244     return;
245
246   LogoInfo& inf = insert( id, index );
247
248   inf.mov = movie;
249   movie->setParent( this );
250   movie->setCacheMode( QMovie::CacheAll );
251   movie->jumpToFrame( 0 );
252
253   generate();
254 }
255
256 /*!
257   \brief Insert new logo information structure into the logos list.
258   \param id unique string identifier of the logo
259   \param index logo position (if < 0, logo is added to the end)
260   \return logo information object
261 */
262 QtxLogoMgr::LogoInfo& QtxLogoMgr::insert( const QString& id, const int index )
263 {
264   LogoInfo empty;
265   empty.id = id;
266   empty.mov = 0;
267
268   int idx = find( id );
269   if ( idx < 0 )
270   {
271     idx = index < (int)myLogos.count() ? index : -1;
272     if ( idx < 0 )
273     {
274       myLogos.append( empty );
275       idx = myLogos.count() - 1;
276     }
277     else
278       myLogos.insert( idx, empty );
279   }
280
281   LogoInfo& inf = myLogos[idx];
282
283   return inf;
284 }
285
286 /*!
287   \brief Remove a logo.
288   \param id logo identifier
289 */
290 void QtxLogoMgr::remove( const QString& id )
291 {
292   int idx = find( id );
293   if ( idx < 0 )
294     return;
295
296   myLogos.removeAt( idx );
297
298   generate();
299 }
300
301 /*!
302   \brief Removes all logos.
303 */
304 void QtxLogoMgr::clear()
305 {
306   myLogos.clear();
307   generate();
308 }
309
310 /*!
311   \brief Start the animation of movie logo.
312
313   If \a id is empty, all movie logos animation are started.
314
315   \param id logo identifier
316 */
317 void QtxLogoMgr::startAnimation( const QString& id )
318 {
319   QList<QMovie*> movList;
320   movies( id, movList );
321
322   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
323     (*it)->start();
324 }
325
326 /*!
327   \brief Stop the animation of movie logo.
328
329   If \a id is empty, all movie logos animation are stopped.
330
331   \param id logo identifier
332 */
333 void QtxLogoMgr::stopAnimation( const QString& id )
334 {
335   QList<QMovie*> movList;
336   movies( id, movList );
337
338   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
339     (*it)->stop();
340 }
341
342 /*!
343   \brief Pause/resume the animation of movie logo.
344
345   If \a pause is \c true, the animation is paused; otherwise
346   it is resumed.
347   
348   If \a id is empty, the operation is performed for all movis logos.
349
350   \param pause if \c true, pause animation, otherwise resume it
351   \param id logo identifier
352 */
353 void QtxLogoMgr::pauseAnimation( const bool pause, const QString& id )
354 {
355   QList<QMovie*> movList;
356   movies( id, movList );
357
358   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
359     (*it)->setPaused( pause );
360 }
361
362 /*!
363   \brief Regenerate logo manager widget contents.
364   
365   Insert logo to menu bar if it not yet done, layout the widget.
366 */
367 void QtxLogoMgr::generate()
368 {
369   if ( !menuBar() )
370     return;
371
372   QList<QLabel*> labels;
373   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
374   {
375     QPixmap pix = (*it).pix;
376     QMovie* mov = (*it).mov;
377     if ( !pix.isNull() && !pix.mask() )
378     {
379       QBitmap bm;
380       QImage img = pix.toImage();
381       if ( img.hasAlphaChannel() )
382         bm = QPixmap::fromImage( img.createAlphaMask() );
383       else
384         bm = QPixmap::fromImage( img.createHeuristicMask() );
385       pix.setMask( bm );
386     }
387
388     QLabel* logoLab = new QLabel( myBox );
389     if ( mov )
390       logoLab->setMovie( mov );
391     else
392     {
393       logoLab->setPixmap( (*it).pix );
394 //      if ( !pix.mask().isNull() )
395 //          logoLab->setMask( pix.mask() );
396     }
397
398     logoLab->setScaledContents( false );
399     logoLab->setAlignment( Qt::AlignCenter );
400
401     labels.append( logoLab );
402   }
403
404   myBox->setLabels( labels );
405 }
406
407 /*!
408   \brief Search the logo by the specified \a id.
409   \param id logo identifier
410   \return index of logo or -1 if not found
411 */
412 int QtxLogoMgr::find( const QString& id ) const
413 {
414   int idx = -1;
415   for ( int i = 0; i < myLogos.count() && idx < 0; i++ )
416   {
417     if ( myLogos.at( i ).id == id )
418       idx = i;
419   }
420   return idx;
421 }
422
423 /*!
424   \brief Get movie logos by specified \a id.
425
426   If \a id is empty, all movie logos are returned.
427
428   \param id logo identifier
429   \param lst list of movies, which satisfy the \a id
430 */
431 void QtxLogoMgr::movies( const QString& id, QList<QMovie*>& lst ) const
432 {
433   lst.clear();
434   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
435   {
436     if ( (*it).mov && ( id.isEmpty() || id == (*it).id ) )
437       lst.append( (*it).mov );
438   }
439 }