Back to index

salome-gui  6.5.0
QtxLogoMgr.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxLogoMgr.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxLogoMgr.h"
00027 
00028 #include <QLabel>
00029 #include <QImage>
00030 #include <QBitmap>
00031 #include <QHBoxLayout>
00032 #include <QMenuBar>
00033 #include <QPointer>
00034 #include <QApplication>
00035 #include <QMovie>
00036 
00043 class QtxLogoMgr::LogoBox : public QWidget
00044 {
00045 public:
00046   LogoBox( QMenuBar* );
00047 
00048   QMenuBar*      menuBar() const;
00049   virtual bool   eventFilter( QObject*, QEvent* );
00050   void           setLabels( const QList<QLabel*>& );
00051 
00052 protected:
00053   virtual void   customEvent( QEvent* );
00054 
00055 private:
00056   void           updateCorner();
00057   void           updateContents();
00058 
00059 private:
00060   typedef QPointer<QWidget> WidgetPtr;
00061 
00062 private:
00063   QMenuBar*      myMB;       
00064   QList<QLabel*> myLabels;   
00065   WidgetPtr      myCornWid;  
00066 };
00067 
00072 QtxLogoMgr::LogoBox::LogoBox( QMenuBar* mb )
00073 : QWidget( mb ),
00074   myMB( mb ),
00075   myCornWid( 0 )
00076 {
00077   myMB->installEventFilter( this );
00078   updateCorner();
00079 }
00080 
00085 QMenuBar* QtxLogoMgr::LogoBox::menuBar() const
00086 {
00087   return myMB;
00088 }
00089 
00096 bool QtxLogoMgr::LogoBox::eventFilter( QObject* o, QEvent* e )
00097 {
00098   if ( o != menuBar() )
00099     return false;
00100 
00101   if ( e->type() == QEvent::MenubarUpdated || e->type() == QEvent::Resize )
00102     updateCorner();
00103 
00104   if ( e->type() == QEvent::ChildAdded || e->type() == QEvent::ChildRemoved )
00105   {
00106     updateCorner();
00107     QApplication::postEvent( this, new QEvent( QEvent::User ) );
00108   }
00109 
00110   return false;
00111 }
00112 
00117 void QtxLogoMgr::LogoBox::setLabels( const QList<QLabel*>& labs )
00118 {
00119   for ( QList<QLabel*>::iterator it = myLabels.begin(); it != myLabels.end(); ++it )
00120   {
00121     if ( !labs.contains( *it ) )
00122       delete *it;
00123   }
00124 
00125   myLabels = labs;
00126   updateContents();
00127 }
00128 
00133 void QtxLogoMgr::LogoBox::customEvent( QEvent* /*e*/ )
00134 {
00135   updateCorner();
00136 }
00137 
00141 void QtxLogoMgr::LogoBox::updateCorner()
00142 {
00143   if ( menuBar()->cornerWidget() == this )
00144     return;
00145 
00146   myCornWid = menuBar()->cornerWidget();
00147   myMB->setCornerWidget( this );
00148   updateContents();
00149 }
00150 
00154 void QtxLogoMgr::LogoBox::updateContents()
00155 {
00156   if ( layout() )
00157     delete layout();
00158 
00159   QHBoxLayout* base = new QHBoxLayout( this );
00160   base->setMargin( 0 );
00161   base->setSpacing( 3 );
00162 
00163   for ( QList<QLabel*>::const_iterator it = myLabels.begin(); it != myLabels.end(); ++it )
00164     base->addWidget( *it );
00165 
00166   if ( myCornWid )
00167     base->addWidget( myCornWid );
00168 
00169   QApplication::sendPostedEvents();
00170 }
00171 
00187 QtxLogoMgr::QtxLogoMgr( QMenuBar* mb )
00188 : QObject( mb )
00189 {
00190   myBox = new LogoBox( mb );
00191 }
00192 
00196 QtxLogoMgr::~QtxLogoMgr()
00197 {
00198 }
00199 
00204 QMenuBar* QtxLogoMgr::menuBar() const
00205 {
00206   return myBox->menuBar();
00207 }
00208 
00213 int QtxLogoMgr::count() const
00214 {
00215   return myLogos.count();
00216 }
00217 
00224 void QtxLogoMgr::insert( const QString& id, const QPixmap& pix, const int index )
00225 {
00226   if ( pix.isNull() )
00227     return;
00228 
00229   LogoInfo& inf = insert( id, index );
00230 
00231   inf.pix = pix;
00232 
00233   generate();
00234 }
00235 
00242 void QtxLogoMgr::insert( const QString& id, QMovie* movie, const int index )
00243 {
00244   if ( !movie )
00245     return;
00246 
00247   LogoInfo& inf = insert( id, index );
00248 
00249   inf.mov = movie;
00250   movie->setParent( this );
00251   movie->setCacheMode( QMovie::CacheAll );
00252   movie->jumpToFrame( 0 );
00253 
00254   generate();
00255 }
00256 
00263 QtxLogoMgr::LogoInfo& QtxLogoMgr::insert( const QString& id, const int index )
00264 {
00265   LogoInfo empty;
00266   empty.id = id;
00267   empty.mov = 0;
00268 
00269   int idx = find( id );
00270   if ( idx < 0 )
00271   {
00272     idx = index < (int)myLogos.count() ? index : -1;
00273     if ( idx < 0 )
00274     {
00275       myLogos.append( empty );
00276       idx = myLogos.count() - 1;
00277     }
00278     else
00279       myLogos.insert( idx, empty );
00280   }
00281 
00282   LogoInfo& inf = myLogos[idx];
00283 
00284   return inf;
00285 }
00286 
00291 void QtxLogoMgr::remove( const QString& id )
00292 {
00293   int idx = find( id );
00294   if ( idx < 0 )
00295     return;
00296 
00297   myLogos.removeAt( idx );
00298 
00299   generate();
00300 }
00301 
00305 void QtxLogoMgr::clear()
00306 {
00307   myLogos.clear();
00308   generate();
00309 }
00310 
00318 void QtxLogoMgr::startAnimation( const QString& id )
00319 {
00320   QList<QMovie*> movList;
00321   movies( id, movList );
00322 
00323   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
00324     (*it)->start();
00325 }
00326 
00334 void QtxLogoMgr::stopAnimation( const QString& id )
00335 {
00336   QList<QMovie*> movList;
00337   movies( id, movList );
00338 
00339   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
00340     (*it)->stop();
00341 }
00342 
00354 void QtxLogoMgr::pauseAnimation( const bool pause, const QString& id )
00355 {
00356   QList<QMovie*> movList;
00357   movies( id, movList );
00358 
00359   for ( QList<QMovie*>::iterator it = movList.begin(); it != movList.end(); ++it )
00360     (*it)->setPaused( pause );
00361 }
00362 
00368 void QtxLogoMgr::generate()
00369 {
00370   if ( !menuBar() )
00371     return;
00372 
00373   QList<QLabel*> labels;
00374   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
00375   {
00376     QPixmap pix = (*it).pix;
00377     QMovie* mov = (*it).mov;
00378     if ( !pix.isNull() && !pix.mask() )
00379     {
00380       QBitmap bm;
00381       QImage img = pix.toImage();
00382       if ( img.hasAlphaChannel() )
00383         bm = QPixmap::fromImage( img.createAlphaMask() );
00384       else
00385         bm = QPixmap::fromImage( img.createHeuristicMask() );
00386       pix.setMask( bm );
00387     }
00388 
00389     QLabel* logoLab = new QLabel( myBox );
00390     if ( mov )
00391       logoLab->setMovie( mov );
00392     else
00393     {
00394       logoLab->setPixmap( (*it).pix );
00395 //      if ( !pix.mask().isNull() )
00396 //          logoLab->setMask( pix.mask() );
00397     }
00398 
00399     logoLab->setScaledContents( false );
00400     logoLab->setAlignment( Qt::AlignCenter );
00401 
00402     labels.append( logoLab );
00403   }
00404 
00405   myBox->setLabels( labels );
00406 }
00407 
00413 int QtxLogoMgr::find( const QString& id ) const
00414 {
00415   int idx = -1;
00416   for ( int i = 0; i < myLogos.count() && idx < 0; i++ )
00417   {
00418     if ( myLogos.at( i ).id == id )
00419       idx = i;
00420   }
00421   return idx;
00422 }
00423 
00432 void QtxLogoMgr::movies( const QString& id, QList<QMovie*>& lst ) const
00433 {
00434   lst.clear();
00435   for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
00436   {
00437     if ( (*it).mov && ( id.isEmpty() || id == (*it).id ) )
00438       lst.append( (*it).mov );
00439   }
00440 }