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 #include "QtxPopupMenu.h"
24 Class: QtxPopupMenu::TitleMenuItem [Internal]
25 Descr: Custom menu item for popup title.
28 class QtxPopupMenu::TitleMenuItem : public QCustomMenuItem
31 TitleMenuItem( const QString&, const QIconSet&, const int );
32 virtual ~TitleMenuItem();
34 virtual bool fullSpan() const;
35 virtual bool isSeparator() const;
36 virtual void setFont( const QFont& );
38 virtual void paint( QPainter*, const QColorGroup&, bool, bool, int, int, int, int );
39 virtual QSize sizeHint();
51 QtxPopupMenu::TitleMenuItem::TitleMenuItem( const QString& txt, const QIconSet& ico, const int align )
62 QtxPopupMenu::TitleMenuItem::~TitleMenuItem()
67 \return TRUE if this item wants to span the entire popup menu width
69 bool QtxPopupMenu::TitleMenuItem::fullSpan() const
75 \return TRUE if this item is just a separator
77 bool QtxPopupMenu::TitleMenuItem::isSeparator() const
84 \param font - new font
86 void QtxPopupMenu::TitleMenuItem::setFont( const QFont& font )
89 myFont.setBold( true );
95 void QtxPopupMenu::TitleMenuItem::paint( QPainter* p, const QColorGroup& cg,
96 bool act, bool enabled, int x, int y, int w, int h )
101 p->fillRect( x, y, w, h, cg.brush( QColorGroup::Dark ) );
103 p->setPen( cg.shadow() );
104 p->drawRect( x, y, w, h );
108 int iw = p->fontMetrics().width( myText ) + ( myIcon.isNull() ? 0 : myIcon.pixmap().width() + s );
109 int ih = QMAX( ( myIcon.isNull() ? 0 : myIcon.pixmap().height() ), p->fontMetrics().height() );
114 if ( myAlign & AlignLeft )
116 else if ( myAlign & AlignRight )
118 else if ( myAlign & AlignHCenter )
119 ix = x + ( w - iw ) / 2;
121 if ( myAlign & AlignTop )
123 else if ( myAlign & AlignBottom )
124 iy = y + ( h - ih - m );
125 else if ( myAlign & AlignVCenter )
126 iy = y + ( h - ih ) / 2;
128 if ( !myIcon.isNull() )
130 p->drawPixmap( ix, iy + ( ih - myIcon.pixmap().height() ) / 2, myIcon.pixmap() );
131 ix += myIcon.pixmap().width() + s;
134 p->setPen( cg.brightText() );
135 p->drawText( ix, iy + ( ih - p->fontMetrics().height() ) / 2 +
136 p->fontMetrics().ascent(), myText, 0, -1 );
142 \return the recommended size for item
144 QSize QtxPopupMenu::TitleMenuItem::sizeHint()
146 QFontMetrics fM( myFont );
150 int w = fM.width( myText ) + ( myIcon.isNull() ? 0 : myIcon.pixmap().width() + s );
151 int h = QMAX( ( myIcon.isNull() ? 0 : myIcon.pixmap().height() ), fM.height() ) + 2 * m;
153 return QSize( w, h );
159 QtxPopupMenu::QtxPopupMenu( QWidget* parent, const char* name )
160 : QPopupMenu( parent, name ),
162 myPolicy( TitleAuto ),
163 myAlign( AlignCenter )
170 QtxPopupMenu::~QtxPopupMenu()
175 \return popup menu title
177 QString QtxPopupMenu::titleText() const
183 \return popup menu icon
185 QIconSet QtxPopupMenu::titleIcon() const
191 \return popup menu title policy
193 int QtxPopupMenu::titlePolicy() const
199 \return popup menu title alignment
201 int QtxPopupMenu::titleAlignment() const
208 \param txt - new text
210 void QtxPopupMenu::setTitleText( const QString& txt )
222 \param icon - new icon
224 void QtxPopupMenu::setTitleIcon( const QIconSet& ico )
233 \param p - new policy
235 void QtxPopupMenu::setTitlePolicy( const int p )
246 Changes title alignment
247 \param a - new alignment
249 void QtxPopupMenu::setTitleAlignment( const int a )
262 void QtxPopupMenu::show()
272 void QtxPopupMenu::hide()
281 \param txt - item text
282 \param icon - item icon
283 \param align - item alignment
285 QtxPopupMenu::TitleMenuItem* QtxPopupMenu::createTitleItem( const QString& txt, const QIconSet& ico,
286 const int align ) const
288 return new TitleMenuItem( txt, ico, align );
292 Inserts title item to popup menu
294 void QtxPopupMenu::insertTitle()
296 if ( myId != -1 || titlePolicy() == TitleOff ||
297 ( titlePolicy() == TitleAuto && titleText().stripWhiteSpace().isEmpty() ) )
300 TitleMenuItem* item = createTitleItem( titleText(), titleIcon(), titleAlignment() );
302 myId = insertItem( item, -1, 0 );
303 setItemEnabled( myId, false );
307 Removes title item from popup menu
309 void QtxPopupMenu::removeTitle()
321 void QtxPopupMenu::updateTitle()