1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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/ or email : webmaster.salome@opencascade.com
21 // Author: Sergey TELKOV
28 #include <QPaintEvent>
29 #include <QTextDocument>
30 #include <QWidgetAction>
31 #include <QLinearGradient>
32 #include <QAbstractTextDocumentLayout>
36 \brief Popup menu title item.
40 class QtxMenu::Title : public QWidget
43 Title( QWidget* = 0 );
47 void setIcon( const QIcon& );
50 void setText( const QString& );
52 Qt::Alignment alignment() const;
53 void setAlignment( const Qt::Alignment );
55 virtual QSize sizeHint() const;
56 virtual QSize minimumSizeHint() const;
59 virtual void paintEvent( QPaintEvent* );
64 Qt::Alignment myAlignment;
69 \param parent parent widget
72 QtxMenu::Title::Title( QWidget* parent )
82 QtxMenu::Title::~Title()
87 \brief Get title icon.
88 \return title item icon
91 QIcon QtxMenu::Title::icon() const
97 \brief Set title icon.
98 \param ico title item icon
101 void QtxMenu::Title::setIcon( const QIcon& ico )
107 \brief Get title menu text.
108 \return menu text for the title item
111 QString QtxMenu::Title::text() const
117 \brief Set title menu text.
118 \param txt menu text to be used for the title item
121 void QtxMenu::Title::setText( const QString& txt )
127 \brief Get title alignment flags.
128 \return title alignment flags
131 Qt::Alignment QtxMenu::Title::alignment() const
137 \brief Set title alignment flags.
138 \param a title alignment flags
141 void QtxMenu::Title::setAlignment( const Qt::Alignment a )
147 \brief Get recommended size for the title item widget.
148 \return title item widget size
151 QSize QtxMenu::Title::sizeHint() const
155 doc.setHtml( text() );
157 QSize sz = icon().isNull() ? QSize( 0, 0 ) : icon().actualSize( QSize( 16, 16 ) );
158 sz.setWidth( 2 * m + sz.width() + (int)doc.size().width() );
159 sz.setHeight( 2 * m + qMax( sz.height(), (int)doc.size().height() ) );
164 \brief Get recommended minimum size for the title item widget.
165 \return title item widget minimum size
168 QSize QtxMenu::Title::minimumSizeHint() const
174 \brief Paint the title item widget.
175 \param e paint event (not used)
178 void QtxMenu::Title::paintEvent( QPaintEvent* /*e*/ )
182 QString txt = text();
183 Qt::Alignment align = alignment();
186 base.setTop( base.top() + 1 );
187 base.setLeft( base.left() + 1 );
188 base.setRight( base.right() -1 );
189 base.setBottom( base.bottom() - 1 );
194 QSize isz = ico.isNull() ? QSize( 0, 0 ) : ico.actualSize( QSize( 16, 16 ) );
195 QSize sz( (int)doc.size().width(), (int)doc.size().height() );
198 QAbstractTextDocumentLayout::PaintContext ctx;
199 ctx.palette.setColor( QPalette::Text, palette().color( QPalette::Light ) );
201 QLinearGradient linearGrad( base.topLeft(), base.topRight() );
202 linearGrad.setColorAt( 0, palette().color( QPalette::Highlight ) );
203 linearGrad.setColorAt( 1, palette().color( QPalette::Window ) );
205 p.fillRect( base, linearGrad );
207 QPoint start = base.topLeft() + QPoint( m, m );
208 if ( align & Qt::AlignLeft )
209 start.setX( base.left() + m );
210 else if ( align & Qt::AlignRight )
211 start.setX( base.right() - m - isz.width() - sz.width() );
212 else if ( align & Qt::AlignHCenter )
213 start.setX( base.left() + ( base.width() - isz.width() - sz.width() ) / 2 );
215 if ( align & Qt::AlignTop )
216 start.setY( base.top() + m );
217 else if ( align & Qt::AlignBottom )
218 start.setY( base.bottom() - m - qMax( isz.height(), - sz.height() ) );
219 else if ( align & Qt::AlignVCenter )
220 start.setY( base.top() + ( base.height() - qMax( isz.height(), sz.height() ) ) / 2 );
224 ico.paint( &p, QRect( start, isz ) );
225 start.setX( start.x() + isz.width() );
229 p.translate( start );
230 doc.documentLayout()->draw( &p, ctx );
236 \brief The class QtxMenu represents the popup menu with the title.
238 The title for the popup menu can be set via setTitleText() method.
239 In addition, title item can contain the icon, which can be set using
240 setTitleIcon() method. Current title text and icon can be retrieved with
241 titleText() and titleIcon() methods.
243 The title text alignment flags can be changed using setTitleAlignment()
244 method and retrieved with titleAlignment() method.
246 By default, QtxMenu::TitleAuto mode is used. In this mode, the title item
247 is shown only if it is not empty. To show title always (even empty), pass
248 QtxMenu::TitleOn to the setTitleMode() method. To hide the title, use
249 setTitleMode() method with QtxMenu::TitleOff parameter.
254 \param parent parent widget
256 QtxMenu::QtxMenu( QWidget* parent )
260 myTitle = new Title( this );
261 myAction = new QWidgetAction( this );
262 myAction->setDefaultWidget( myTitle );
273 \brief Get title menu text.
274 \return menu text for the title item
276 QString QtxMenu::titleText() const
278 return myTitle->text();
282 \brief Get title icon.
283 \return title item icon
285 QIcon QtxMenu::titleIcon() const
287 return myTitle->icon();
291 \brief Get title item display mode.
292 \return popup menu title display mode (QtxMenu::TitleMode)
294 QtxMenu::TitleMode QtxMenu::titleMode() const
300 \brief Get title alignment flags.
301 \return title alignment flags
303 Qt::Alignment QtxMenu::titleAlignment() const
305 return myTitle->alignment();
309 \brief Set title menu text.
310 \param txt menu text to be used for the title item
312 void QtxMenu::setTitleText( const QString& txt )
314 if ( titleText() == txt )
317 myTitle->setText( txt );
323 \brief Set title icon.
324 \param ico title item icon
326 void QtxMenu::setTitleIcon( const QIcon& ico )
328 myTitle->setIcon( ico );
334 \brief Set title item display mode.
335 \param m popup menu title display mode (QtxMenu::TitleMode)
337 void QtxMenu::setTitleMode( const QtxMenu::TitleMode m )
348 \brief Set title alignment flags.
349 \param a title alignment flags
351 void QtxMenu::setTitleAlignment( const Qt::Alignment a )
353 if ( titleAlignment() == a )
356 myTitle->setAlignment( a );
362 \brief Customize show/hide menu operation.
363 \param on new popup menu visibility state
365 void QtxMenu::setVisible( bool on )
370 QMenu::setVisible( on );
377 \brief Insert title item to the popup menu.
379 void QtxMenu::insertTitle()
381 if ( titleMode() == TitleOff || ( titleMode() == TitleAuto && titleText().trimmed().isEmpty() ) )
384 if ( actions().isEmpty() )
385 addAction( myAction );
387 insertAction( actions().first(), myAction );
391 \brief Remove title item from the popup menu.
393 void QtxMenu::removeTitle()
395 if ( actions().contains( myAction ) )
396 removeAction( myAction );
400 \brief Update title item.
402 void QtxMenu::updateTitle()
404 if ( !actions().contains( myAction ) )