1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 // File: QtxMenuButton.cxx
20 // Author: Sergey TELKOV
22 #include "QtxMenuButton.h"
26 #include <qpopupmenu.h>
27 #include <qpointarray.h>
28 #include <qapplication.h>
30 class QtxMenuButton::PopupMenu : public QPopupMenu
33 PopupMenu( QtxMenuButton* mb ) : QPopupMenu( mb ), myMenuButton( mb ) {};
34 virtual ~PopupMenu() {};
36 virtual void setMinimumSize( int, int );
39 QtxMenuButton* myMenuButton;
42 void QtxMenuButton::PopupMenu::setMinimumSize( int w, int h )
44 if ( myMenuButton->isAlignWidth() &&
45 ( myMenuButton->position() == Top || myMenuButton->position() == Bottom ) )
46 w = QMAX( w, myMenuButton->width() );
48 QPopupMenu::setMinimumSize( w, h );
52 QtxMenuButton::QtxMenuButton( int pos, QWidget* parent, const char* name )
53 : QPushButton( parent, name ),
59 QtxMenuButton::QtxMenuButton( const QString& text, QWidget* parent, const char* name )
60 : QPushButton( parent, name ),
67 QtxMenuButton::QtxMenuButton( int pos, const QString& text, QWidget* parent, const char* name )
68 : QPushButton( parent, name ),
75 QtxMenuButton::QtxMenuButton( QWidget* parent, const char* name )
76 : QPushButton( parent, name ),
82 QtxMenuButton::~QtxMenuButton()
86 void QtxMenuButton::initialize()
91 setAutoDefault( false );
92 myPopup = new PopupMenu( this );
95 connect( myPopup, SIGNAL( activated( int ) ), this, SIGNAL( activated( int ) ) );
96 connect( this, SIGNAL( clicked() ), this, SLOT( onShowPopup() ) );
99 int QtxMenuButton::position() const
104 bool QtxMenuButton::isAlignWidth() const
109 bool QtxMenuButton::isArrowEnabled() const
114 void QtxMenuButton::setPosition( const int pos )
120 if ( myPopup->isVisible() )
124 void QtxMenuButton::setAlignWidth( const bool on )
133 void QtxMenuButton::setArrowEnabled( const bool on )
142 void QtxMenuButton::clear()
150 void QtxMenuButton::removeItem( int id )
157 int QtxMenuButton::insertSeparator( int id )
161 res = myPopup->insertSeparator( id );
165 int QtxMenuButton::insertItem( const QString& t, int id, int index )
169 resId = myPopup->insertItem( t, id, index );
177 int QtxMenuButton::insertItem( const QIconSet& is, const QString& t, int id, int index )
181 resId = myPopup->insertItem( is, t, id, index );
189 void QtxMenuButton::onShowPopup()
191 if ( !myPopup || !myPopup->count() )
197 QPoint p = mapToGlobal( QPoint( 0, 0 ) );
203 switch ( position() )
206 xoffset = -1 * ( myPopup->sizeHint().width() + margin );
209 xoffset = width() + margin;
212 yoffset = -1 * ( myPopup->sizeHint().height() + margin );
216 yoffset = height() + margin;
219 int dw = QApplication::desktop()->width();
220 int dh = QApplication::desktop()->height();
221 x = QMIN( QMAX( x + xoffset, 0 ), dw );
222 y = QMIN( QMAX( y + yoffset, 0 ), dh );
224 myPopup->exec( QPoint( x, y ) );
227 bool QtxMenuButton::event( QEvent* e )
229 if ( e->type() == QEvent::MouseButtonPress ||
230 e->type() == QEvent::MouseButtonDblClick ||
231 e->type() == QEvent::MouseButtonRelease )
237 return QPushButton::event( e );
240 QSize QtxMenuButton::sizeHint() const
242 QSize sz = QPushButton::sizeHint();
243 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
244 sz = QSize( QMAX( sz.width(), myPopup->sizeHint().width() ), sz.height() );
249 QSize QtxMenuButton::minimumSizeHint() const
251 QSize sz = QPushButton::minimumSizeHint();
252 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
253 sz = QSize( QMAX( sz.width(), myPopup->sizeHint().width() ), sz.height() );
258 void QtxMenuButton::resizeEvent( QResizeEvent* re )
261 QPushButton::resizeEvent( re );
263 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
264 myPopup->setMinimumWidth( re ? re->size().width() : width() );
267 QPopupMenu* QtxMenuButton::popup() const
272 void QtxMenuButton::drawButtonLabel( QPainter* p )
274 QPushButton::drawButtonLabel( p );
276 QStyle::SFlags flags = QStyle::Style_Default;
278 flags |= QStyle::Style_Enabled;
280 flags |= QStyle::Style_HasFocus;
285 QRect r = style().subRect( QStyle::SR_PushButtonContents, this );
288 if ( myArrow && myPopup && myPopup->count() )
294 QRect ar( 0, 0, w, h );
295 if ( position() == Left || position() == Top )
296 r.moveBy( ar.width() + 2 * margin, 0 );
298 ar.moveBy( r.width() - ar.width() - 2 * margin, 0 );
300 r.setWidth( r.width() - ar.width() - 2 * margin );
302 ar.moveBy( margin, ( height() - h ) / 2 );
304 QPointArray arrow( 3 );
305 switch ( position() )
308 arrow.putPoints( 0, 3, ar.left(), ar.top() + ar.height() / 2, ar.right(), ar.top(), ar.right(), ar.bottom() );
311 arrow.putPoints( 0, 3, ar.left(), ar.top(), ar.left(), ar.bottom(), ar.right(), ar.top() + ar.height() / 2 );
314 arrow.putPoints( 0, 3, ar.left(), ar.bottom(), ar.right(), ar.bottom(), ar.left() + ar.width() / 2, ar.top() );
318 arrow.putPoints( 0, 3, ar.left(), ar.top(), ar.right(), ar.top(), ar.left() + ar.width() / 2, ar.bottom() );
322 p->setPen( colorGroup().text() );
323 p->setBrush( colorGroup().text() );
324 p->drawPolygon( arrow, true );
327 // style().drawControl( QStyle::CE_PushButtonLabel, p, this, r, colorGroup(), flags );