From: stv Date: Wed, 11 Jul 2007 11:39:12 +0000 (+0000) Subject: no message X-Git-Tag: qt4_porting_delivery_220807~99 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2b132b06d381e188cd4f1c897855c500175fd83f;p=modules%2Fgui.git no message --- diff --git a/src/Qtx/QtxActionSet.cxx b/src/Qtx/QtxActionSet.cxx index b25caad93..7625022ec 100644 --- a/src/Qtx/QtxActionSet.cxx +++ b/src/Qtx/QtxActionSet.cxx @@ -21,6 +21,8 @@ #include "QtxActionSet.h" +#include + /*! \class QtxActionSet \brief An action class which is represented in the menu bar (or toolbar) as @@ -45,8 +47,6 @@ QtxActionSet::QtxActionSet( QObject* parent ) connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) ); setVisible( false ); - - update(); } /*! @@ -103,7 +103,7 @@ void QtxActionSet::insertActions( const QList& lst, const int index ) connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) ); } - update(); + updateAction(); } /*! @@ -130,7 +130,9 @@ int QtxActionSet::insertAction( QAction* a, const int id, const int index ) connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) ); - update(); + actionAdded( a ); + + updateAction(); return ident; } @@ -179,6 +181,7 @@ void QtxActionSet::removeAction( QAction* a ) return; mySet.removeAll( a ); + actionRemoved( a ); delete a; } @@ -204,7 +207,7 @@ void QtxActionSet::clear() qDeleteAll( mySet ); mySet.clear(); - update(); + updateAction(); } /*! @@ -247,7 +250,7 @@ void QtxActionSet::addedTo( QWidget* w ) { QtxAction::addedTo( w ); - update( w ); + updateAction( w ); } /*! @@ -258,7 +261,7 @@ void QtxActionSet::removedFrom( QWidget* w ) { QtxAction::removedFrom( w ); - update( w ); + updateAction( w ); } /*! @@ -303,6 +306,20 @@ void QtxActionSet::setActionId( QAction* a, const int id ) a->setData( id ); } +/*! + \brief Notify that action was added +*/ +void QtxActionSet::actionAdded( QAction* ) +{ +} + +/*! + \brief Notify that action was removed +*/ +void QtxActionSet::actionRemoved( QAction* ) +{ +} + /*! \brief Getneration unique action identifier \return generation action ID @@ -323,18 +340,18 @@ int QtxActionSet::generateId() const /*! \brief Update action set. */ -void QtxActionSet::update() +void QtxActionSet::updateAction() { QList lst = associatedWidgets(); for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) - update( *it ); + updateAction( *it ); } /*! \brief Update action set for the specified widget. \param w a widget this action is added to */ -void QtxActionSet::update( QWidget* w ) +void QtxActionSet::updateAction( QWidget* w ) { if ( !w ) return; @@ -342,14 +359,22 @@ void QtxActionSet::update( QWidget* w ) for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it ) w->removeAction( *it ); - if ( !associatedWidgets().contains( w ) ) - return; - + QAction* first = 0; for ( int i = 0; i < mySet.count(); i++ ) { QAction* a = mySet.at( i ); + if ( !first ) + first = a; w->insertAction( this, a ); } + if ( first ) + { + QApplication::instance()->removeEventFilter( this ); + + w->insertAction( first, this ); + + QApplication::instance()->installEventFilter( this ); + } } bool QtxActionSet::isEmptyAction() const diff --git a/src/Qtx/QtxActionSet.h b/src/Qtx/QtxActionSet.h index 4de899787..652ae6c15 100644 --- a/src/Qtx/QtxActionSet.h +++ b/src/Qtx/QtxActionSet.h @@ -64,19 +64,23 @@ protected: virtual void addedTo( QWidget* ); virtual void removedFrom( QWidget* ); + virtual void actionAdded( QAction* ); + virtual void actionRemoved( QAction* ); + QAction* action( int ) const; int actionId( QAction* ) const; void setActionId( QAction*, const int ); virtual bool isEmptyAction() const; + virtual void updateAction(); + virtual void updateAction( QWidget* ); + private: - void update(); - void update( QWidget* ); int generateId() const; private: - typedef QList ActionList; + typedef QList ActionList; private: ActionList mySet; //!< actions list diff --git a/src/Qtx/QtxMultiAction.cxx b/src/Qtx/QtxMultiAction.cxx new file mode 100644 index 000000000..d5ad32562 --- /dev/null +++ b/src/Qtx/QtxMultiAction.cxx @@ -0,0 +1,233 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "QtxMultiAction.h" + +#include +#include +#include +#include +#include +#include +#include + +class QtxMultiAction::Button : public QToolButton +{ +public: + Button( QWidget* parent = 0 ) : QToolButton( parent ) {} + ~Button() {}; + +protected: + virtual void paintEvent( QPaintEvent* e ) + { + QToolButton::paintEvent( e ); + + int s = 4; + int m = 2; + int w = width(); + int h = height(); + + QStyleOptionButton opt; + opt.initFrom( this ); + if ( isDown() ) + opt.rect = QRect( w - s - m, h - s - m, s, s ); + else + opt.rect = QRect( w - s - m - 1, h - s - m - 1, s, s ); + + QPainter p( this ); + style()->drawPrimitive( QStyle::PE_IndicatorArrowDown, &opt, &p ); + } +}; + +/*! + Constructor +*/ +QtxMultiAction::QtxMultiAction( QObject* parent ) +: QtxActionSet( parent ), +myCurrent( 0 ) +{ + setVisible( true ); + setMenu( new QMenu( 0 ) ); + + connect( this, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) ); +} + +QtxMultiAction::QtxMultiAction( const QString& txt, QObject* parent ) +: QtxActionSet( parent ), +myCurrent( 0 ) +{ + setText( txt ); + setVisible( true ); + setMenu( new QMenu( 0 ) ); + + connect( this, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) ); +} + +QtxMultiAction::QtxMultiAction( const QIcon& ico, const QString& txt, QObject* parent ) +: QtxActionSet( parent ), +myCurrent( 0 ) +{ + setIcon( ico ); + setText( txt ); + setVisible( true ); + setMenu( new QMenu( 0 ) ); + + connect( this, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) ); +} + +QtxMultiAction::~QtxMultiAction() +{ +} + +void QtxMultiAction::onClicked( bool ) +{ + if ( myCurrent ) + myCurrent->activate( QAction::Trigger ); +} + +void QtxMultiAction::onTriggered( QAction* a ) +{ + if ( !a ) + return; + + QList lst = createdWidgets(); + for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) + { + QToolButton* tb = ::qobject_cast( *it ); + if ( tb && tb->menu() ) + tb->menu()->hide(); + } + + if ( myCurrent != a ) + { + myCurrent = a; + updateAction(); + } +} + +void QtxMultiAction::updateAction() +{ + QtxActionSet::updateAction(); + + QList lst = createdWidgets(); + for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) + updateButton( ::qobject_cast( *it ) ); +} + +void QtxMultiAction::updateAction( QWidget* w ) +{ + if ( !w ) + return; + + if ( w->inherits( "QMenu" ) ) + { + QtxActionSet::updateAction( menu() ); + + QApplication::instance()->removeEventFilter( this ); + + menu()->removeAction( this ); + + QApplication::instance()->installEventFilter( this ); + } +} + +bool QtxMultiAction::isEmptyAction() const +{ + return false; +} + +QWidget* QtxMultiAction::createWidget( QWidget* parent ) +{ + QToolBar* tb = ::qobject_cast( parent ); + if ( !tb ) + return 0; + + QToolButton* w = new Button( tb ); + w->setMenu( new QMenu( w ) ); + w->setFocusPolicy( Qt::NoFocus ); + w->setIconSize( tb->iconSize() ); + w->setToolButtonStyle( tb->toolButtonStyle() ); + + connect( w, SIGNAL( clicked( bool ) ), this, SLOT( onClicked( bool ) ) ); + connect( tb, SIGNAL( iconSizeChanged( const QSize& ) ), w, SLOT( setIconSize( QSize ) ) ); + connect( tb, SIGNAL( toolButtonStyleChanged( Qt::ToolButtonStyle ) ), + w, SLOT( setToolButtonStyle( Qt::ToolButtonStyle ) ) ); + + updateButton( w ); + return w; +} + +void QtxMultiAction::actionAdded( QAction* a ) +{ + if ( !myCurrent ) + myCurrent = a; +} + +void QtxMultiAction::actionRemoved( QAction* a ) +{ + if ( myCurrent != a ) + return; + + myCurrent = actions().isEmpty() ? 0 : actions().first(); + + updateAction(); +} + +void QtxMultiAction::updateButton( QToolButton* btn ) +{ + if ( !btn ) + return; + + btn->setIcon( myCurrent ? myCurrent->icon() : QIcon() ); + btn->setText( myCurrent ? myCurrent->text() : QString() ); + btn->setToolTip( myCurrent ? myCurrent->toolTip() : QString() ); + + QMenu* pm = btn->menu(); + if ( !pm ) + return; + + pm->clear(); + for ( int i = 0; pm->layout() && i < pm->layout()->count(); i++ ) + delete pm->layout()->widget(); + + delete pm->layout(); + + QVBoxLayout* vbox = new QVBoxLayout( pm ); + vbox->setMargin( 1 ); + vbox->setSpacing( 0 ); + + QList actList = actions(); + for ( QList::iterator itr = actList.begin(); itr != actList.end(); ++itr ) + { + QToolButton* b = new QToolButton( pm ); + b->setDefaultAction( *itr ); + b->setToolTip( (*itr)->toolTip() ); + b->setAutoRaise( true ); + b->setIconSize( btn->iconSize() ); + b->setToolButtonStyle( btn->toolButtonStyle() ); + b->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); + vbox->addWidget( b ); + } + +/* + QList actList = actions(); + for ( QList::iterator itr = actList.begin(); itr != actList.end(); ++itr ) + pm->addAction( *itr ); +*/ +}