Back to index

salome-gui  6.5.0
QtxMenu.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxMenu.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxMenu.h"
00024 
00025 #include <QLabel>
00026 #include <QLayout>
00027 #include <QPainter>
00028 #include <QPaintEvent>
00029 #include <QTextDocument>
00030 #include <QWidgetAction>
00031 #include <QLinearGradient>
00032 #include <QAbstractTextDocumentLayout>
00033 
00040 class QtxMenu::Title : public QWidget
00041 {
00042 public:
00043   Title( QWidget* = 0 );
00044   virtual ~Title();
00045 
00046   QIcon            icon() const;
00047   void             setIcon( const QIcon& );
00048 
00049   QString          text() const;
00050   void             setText( const QString& );
00051 
00052   Qt::Alignment    alignment() const;
00053   void             setAlignment( const Qt::Alignment );
00054 
00055   virtual QSize    sizeHint() const;
00056   virtual QSize    minimumSizeHint() const;
00057 
00058 protected:
00059   virtual void     paintEvent( QPaintEvent* );
00060 
00061 private:
00062   QIcon            myIcon;
00063   QString          myText;
00064   Qt::Alignment    myAlignment;
00065 };
00066 
00072 QtxMenu::Title::Title( QWidget* parent )
00073 : QWidget( parent ),
00074   myAlignment( 0 )
00075 {
00076 }
00077 
00082 QtxMenu::Title::~Title()
00083 {
00084 }
00085 
00091 QIcon QtxMenu::Title::icon() const
00092 {
00093   return myIcon;
00094 }
00095 
00101 void QtxMenu::Title::setIcon( const QIcon& ico )
00102 {
00103   myIcon = ico;
00104 }
00105 
00111 QString QtxMenu::Title::text() const
00112 {
00113   return myText;
00114 }
00115 
00121 void QtxMenu::Title::setText( const QString& txt )
00122 {
00123   myText = txt;
00124 }
00125 
00131 Qt::Alignment QtxMenu::Title::alignment() const
00132 {
00133   return myAlignment;
00134 }
00135 
00141 void QtxMenu::Title::setAlignment( const Qt::Alignment a )
00142 {
00143   myAlignment = a;
00144 }
00145 
00151 QSize QtxMenu::Title::sizeHint() const
00152 {
00153   int m = 5;
00154   QTextDocument doc;
00155   doc.setHtml( text() );
00156 
00157   QSize sz = icon().isNull() ? QSize( 0, 0 ) : icon().actualSize( QSize( 16, 16 ) );
00158   sz.setWidth( 2 * m + sz.width() + (int)doc.size().width() );
00159   sz.setHeight( 2 * m + qMax( sz.height(), (int)doc.size().height() ) );
00160   return sz;
00161 }
00162 
00168 QSize QtxMenu::Title::minimumSizeHint() const
00169 {
00170   return sizeHint();
00171 }
00172 
00178 void QtxMenu::Title::paintEvent( QPaintEvent* /*e*/ )
00179 {
00180   int m = 5;
00181   QIcon ico = icon();
00182   QString txt = text();
00183   Qt::Alignment align = alignment();
00184 
00185   QRect base = rect();
00186   base.setTop( base.top() + 1 );
00187   base.setLeft( base.left() + 1 );
00188   base.setRight( base.right() -1 );
00189   base.setBottom( base.bottom() - 1 );
00190 
00191   QTextDocument doc;
00192   doc.setHtml( txt );
00193 
00194   QSize isz = ico.isNull() ? QSize( 0, 0 ) : ico.actualSize( QSize( 16, 16 ) );
00195   QSize sz( (int)doc.size().width(), (int)doc.size().height() );
00196 
00197   QPainter p( this );
00198   QAbstractTextDocumentLayout::PaintContext ctx;
00199   ctx.palette.setColor( QPalette::Text, palette().color( QPalette::Light ) );
00200 
00201   QLinearGradient linearGrad( base.topLeft(), base.topRight() );
00202   linearGrad.setColorAt( 0, palette().color( QPalette::Highlight ) );
00203   linearGrad.setColorAt( 1, palette().color( QPalette::Window ) );
00204 
00205   p.fillRect( base, linearGrad );
00206 
00207   QPoint start = base.topLeft() + QPoint( m, m );
00208   if ( align & Qt::AlignLeft )
00209     start.setX( base.left() + m );
00210   else if ( align & Qt::AlignRight )
00211     start.setX( base.right() - m - isz.width() - sz.width() );
00212   else if ( align & Qt::AlignHCenter )
00213     start.setX( base.left() + ( base.width() - isz.width() - sz.width() ) / 2 );
00214 
00215   if ( align & Qt::AlignTop )
00216     start.setY( base.top() + m );
00217   else if ( align & Qt::AlignBottom )
00218     start.setY( base.bottom() - m - qMax( isz.height(), - sz.height() ) );
00219   else if ( align & Qt::AlignVCenter )
00220     start.setY( base.top() + ( base.height() - qMax( isz.height(), sz.height() ) ) / 2 );
00221 
00222   if ( !ico.isNull() )
00223   {
00224     ico.paint( &p, QRect( start, isz ) );
00225     start.setX( start.x() + isz.width() );
00226   }
00227 
00228   p.save();
00229   p.translate( start );
00230   doc.documentLayout()->draw( &p, ctx );
00231   p.restore();
00232 }
00233 
00256 QtxMenu::QtxMenu( QWidget* parent )
00257 : QMenu( parent ),
00258   myMode( TitleAuto )
00259 {
00260   myTitle = new Title( this );
00261   myAction = new QWidgetAction( this );
00262   myAction->setDefaultWidget( myTitle );
00263 }
00264 
00268 QtxMenu::~QtxMenu()
00269 {
00270 }
00271 
00276 QString QtxMenu::titleText() const
00277 {
00278   return myTitle->text();
00279 }
00280 
00285 QIcon QtxMenu::titleIcon() const
00286 {
00287   return myTitle->icon();
00288 }
00289 
00294 QtxMenu::TitleMode QtxMenu::titleMode() const
00295 {
00296   return myMode;
00297 }
00298 
00303 Qt::Alignment QtxMenu::titleAlignment() const
00304 {
00305   return myTitle->alignment();
00306 }
00307 
00312 void QtxMenu::setTitleText( const QString& txt )
00313 {
00314   if ( titleText() == txt )
00315     return;
00316 
00317   myTitle->setText( txt );
00318 
00319   updateTitle();
00320 }
00321 
00326 void QtxMenu::setTitleIcon( const QIcon& ico )
00327 {
00328   myTitle->setIcon( ico );
00329 
00330   updateTitle();
00331 }
00332 
00337 void QtxMenu::setTitleMode( const QtxMenu::TitleMode m )
00338 {
00339   if ( myMode == m )
00340     return;
00341 
00342   myMode = m;
00343 
00344   updateTitle();
00345 }
00346 
00351 void QtxMenu::setTitleAlignment( const Qt::Alignment a )
00352 {
00353   if ( titleAlignment() == a )
00354     return;
00355 
00356   myTitle->setAlignment( a );
00357 
00358   updateTitle();
00359 }
00360 
00365 void QtxMenu::setVisible( bool on )
00366 {
00367   if ( on )
00368     insertTitle();
00369 
00370   QMenu::setVisible( on );
00371 
00372   if ( !on )
00373     removeTitle();
00374 }
00375 
00379 void QtxMenu::insertTitle()
00380 {
00381   if ( titleMode() == TitleOff || ( titleMode() == TitleAuto && titleText().trimmed().isEmpty() ) )
00382     return;
00383 
00384   if ( actions().isEmpty() )
00385     addAction( myAction );
00386   else
00387     insertAction( actions().first(), myAction );
00388 }
00389 
00393 void QtxMenu::removeTitle()
00394 {
00395   if ( actions().contains( myAction ) )
00396     removeAction( myAction );
00397 }
00398 
00402 void QtxMenu::updateTitle()
00403 {
00404   if ( !actions().contains( myAction ) )
00405     return;
00406 
00407   removeTitle();
00408   insertTitle();
00409 }