From: stv Date: Thu, 24 Nov 2005 10:07:11 +0000 (+0000) Subject: New class added: QtxLogoMgr X-Git-Tag: BR_3_1_0_deb~41 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c6b331268096596311d266faca1dba7b5db03e76;p=modules%2Fgui.git New class added: QtxLogoMgr --- diff --git a/src/Qtx/Makefile.in b/src/Qtx/Makefile.in index eda7470b7..d433050d1 100755 --- a/src/Qtx/Makefile.in +++ b/src/Qtx/Makefile.in @@ -25,6 +25,7 @@ EXPORT_HEADERS= Qtx.h \ QtxListAction.h \ QtxListBox.h \ QtxListOfOperations.h \ + QtxLogoMgr.h \ QtxMenuButton.h \ QtxMRUAction.h \ QtxOperations.h \ @@ -71,6 +72,7 @@ LIB_SRC= \ QtxListAction.cxx \ QtxListBox.cxx \ QtxListOfOperations.cxx \ + QtxLogoMgr.cxx \ QtxMenuButton.cxx \ QtxMRUAction.cxx \ QtxPathDialog.cxx \ @@ -107,6 +109,7 @@ LIB_MOC = \ QtxIntSpinBox.h \ QtxListAction.h \ QtxListBox.h \ + QtxLogoMgr.h \ QtxMenuButton.h \ QtxMRUAction.h \ QtxPathDialog.h \ diff --git a/src/Qtx/QtxLogoMgr.cxx b/src/Qtx/QtxLogoMgr.cxx new file mode 100644 index 000000000..f045d9c95 --- /dev/null +++ b/src/Qtx/QtxLogoMgr.cxx @@ -0,0 +1,169 @@ +#include "QtxLogoMgr.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +QtxLogoMgr::QtxLogoMgr( QMenuBar* mb ) +: QObject( mb ), +myMenus( mb ), +myId( 0 ) +{ +} + +QtxLogoMgr::~QtxLogoMgr() +{ +} + +/*! + Returns the menubar. +*/ +QMenuBar* QtxLogoMgr::menuBar() const +{ + return myMenus; +} + +/*! + Returns the count of the existed logos. +*/ +int QtxLogoMgr::count() const +{ + return myLogos.count(); +} + +/*! + Insert new logo to the menu bar area +*/ +void QtxLogoMgr::insert( const QString& id, const QPixmap& pix, const int index ) +{ + if ( pix.isNull() ) + return; + + LogoInfo* inf = 0; + + int idx = find( id ); + if ( idx < 0 ) + { + idx = index < (int)myLogos.count() ? index : -1; + if ( idx < 0 ) + inf = &( *myLogos.append( LogoInfo() ) ); + else + inf = &( *myLogos.insert( myLogos.at( idx ), LogoInfo() ) ); + } + else + inf = &( *myLogos.at( idx ) ); + + + inf->id = id; + inf->pix = pix; + + generate(); +} + +/*! + Removes a logo +*/ +void QtxLogoMgr::remove( const QString& id ) +{ + int idx = find( id ); + if ( idx < 0 ) + return; + + myLogos.remove( myLogos.at( idx ) ); + + generate(); +} + +/*! + Removes all logos +*/ +void QtxLogoMgr::clear() +{ + myLogos.clear(); + generate(); +} + +void QtxLogoMgr::generate() +{ + if ( !menuBar() ) + return; + + if ( myId ) + menuBar()->removeItem( myId ); + + myId = 0; + + if ( myLogos.isEmpty() ) + return; + + class LogoBox : public QHBox + { + public: + LogoBox( QWidget* parent = 0, const char* name = 0, WFlags f = 0 ) : QHBox( parent, name, f ) {}; + + void addSpacing( int spacing ) + { + QApplication::sendPostedEvents( this, QEvent::ChildInserted ); + ((QHBoxLayout*)layout())->addSpacing( spacing ); + } + + protected: + void drawContents( QPainter* p ) + { + if ( parentWidget()->inherits( "QMenuBar" ) ) + style().drawControl( QStyle::CE_MenuBarEmptyArea, p, this, contentsRect(), colorGroup() ); + else + QHBox::drawContents( p ); + } + }; + + LogoBox* cnt = new LogoBox( menuBar() ); + cnt->setSpacing( 10 ); + + for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it ) + { + QPixmap pix = (*it).pix; + if ( !pix.mask() ) + { + QImage img = pix.convertToImage(); + QBitmap bm; + if ( img.hasAlphaBuffer() ) + bm = img.createAlphaMask(); + else + bm = img.createHeuristicMask(); + pix.setMask( bm ); + } + + QLabel* logoLab = new QLabel( cnt ); + logoLab->setPixmap( (*it).pix ); + logoLab->setScaledContents( false ); + logoLab->setAlignment( QLabel::AlignCenter ); + + if ( pix.mask() ) + logoLab->setMask( *pix.mask() ); + } + + QApplication::sendPostedEvents( cnt, QEvent::ChildInserted ); + cnt->addSpacing( 2 ); + + myId = menuBar()->insertItem( cnt ); + + QApplication::sendPostedEvents( menuBar()->parentWidget(), QEvent::LayoutHint ); + QApplication::postEvent( menuBar()->parentWidget(), new QEvent( QEvent::LayoutHint ) ); +} + +int QtxLogoMgr::find( const QString& id ) const +{ + int idx = -1; + for ( uint i = 0; i < myLogos.count() && idx < 0; i++ ) + { + if ( (*myLogos.at( i ) ).id == id ) + idx = i; + } + return idx; +} diff --git a/src/Qtx/QtxLogoMgr.h b/src/Qtx/QtxLogoMgr.h new file mode 100644 index 000000000..bc18c9d36 --- /dev/null +++ b/src/Qtx/QtxLogoMgr.h @@ -0,0 +1,49 @@ +#ifndef QTX_LOGOMGR_H +#define QTX_LOGOMGR_H + +#include "Qtx.h" + +class QMenuBar; + +#include +#include + +#ifdef WIN32 +#pragma warning( disable : 4251 ) +#endif + +class QTX_EXPORT QtxLogoMgr : public QObject +{ + Q_OBJECT + +public: + QtxLogoMgr( QMenuBar* ); + virtual ~QtxLogoMgr(); + + int count() const; + + void insert( const QString&, const QPixmap&, const int = -1 ); + void remove( const QString& ); + void clear(); + + QMenuBar* menuBar() const; + +private: + void generate(); + int find( const QString& ) const; + +private: + typedef struct { QString id; QPixmap pix; } LogoInfo; + typedef QValueList LogoList; + +private: + int myId; + QMenuBar* myMenus; + LogoList myLogos; +}; + +#ifdef WIN32 +#pragma warning( default : 4251 ) +#endif + +#endif