Back to index

salome-gui  6.5.0
QtxToolTip.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxToolTip.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxToolTip.h"
00027 
00028 #include <QFont>
00029 #include <QTimer>
00030 #include <QCursor>
00031 #include <QFontMetrics>
00032 #include <QApplication>
00033 #include <QPalette>
00034 #include <QMouseEvent>
00035 
00036 #define TOOLTIP_SHOW_DELAY 0500
00037 #define TOOLTIP_HIDE_DELAY 7000
00038 
00042 QtxToolTip::QtxToolTip( QWidget* parent )
00043 : QLabel( parent, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::Window )
00044 {
00045   setObjectName( "" );
00046   setIndent( 3 );
00047   setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
00048   QPalette palette;
00049   palette.setColor( backgroundRole(), QColor( 255, 255, 231 ) );
00050   setPalette( palette );
00051 
00052         myWidgetRegion = QRect( -1, -1, -1, -1 );
00053 
00054         setFrameShape( QFrame::Panel );
00055         setFrameShadow( QFrame::Plain );
00056 
00057   parent->setMouseTracking( true );
00058         parent->installEventFilter( this );
00059         installEventFilter( this );
00060 
00061         mySleepTimer = new QTimer( this );
00062         mySleepTimer->setSingleShot( true );
00063         myWakeUpTimer = new QTimer( this );
00064         myWakeUpTimer->setSingleShot( true );
00065         connect( mySleepTimer,  SIGNAL( timeout() ), this, SLOT( onSleepTimeOut()  ) );
00066         connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
00067 
00068   myWakeUpDelayTime = 700;
00069   myShowDelayTime = 5000;
00070 }
00071 
00075 QtxToolTip::~QtxToolTip()
00076 {
00077 }
00078 
00082 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
00083 {
00084         if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
00085         {
00086                 hideTip();
00087         }
00088         if ( e->type() == QEvent::Leave )
00089         {
00090                 if ( isVisible() && ( o == this ) )
00091                         hideTip();
00092                 myWakeUpTimer->stop();
00093         }
00094         if ( e->type() == QEvent::MouseMove )
00095         {
00096                 QMouseEvent* me = (QMouseEvent*)e;
00097                 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
00098                 if ( myWakeUpTimer->isActive() )
00099                 {
00100                         myWakeUpTimer->stop();
00101                         myWakeUpTimer->start( myWakeUpDelayTime );
00102                 }
00103                 if ( isVisible() )
00104                 {
00105                         if ( !myWidgetRegion.contains( thePos ) )
00106       {
00107                                 hideTip();
00108         myWidgetRegion = QRect( -1, -1, -1, -1 );
00109       }
00110                 }
00111                 else
00112                 {
00113                         if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
00114                                 myWakeUpTimer->start( myWakeUpDelayTime );
00115                 }
00116         }
00117         if ( e->type() == QEvent::KeyPress )
00118         {
00119                 hideTip();
00120         }
00121         if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
00122                           e->type() == QEvent::MouseButtonRelease ) )
00123         {
00124                 hideTip();
00125         }
00126         return false;
00127 }
00128 
00135 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
00136 {
00137         QFontMetrics theFM = fontMetrics();
00138         int theHeight = theFM.height();
00139         int theWidth = theFM.width( text ) + 2;
00140         showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
00141 }
00142 
00149 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
00150 {
00151         setText( text );
00152         myWidgetRegion = aWidgetRegion;
00153         setGeometry( aRegion );
00154         if ( myShowDelayTime != 0 )
00155                 mySleepTimer->start( myShowDelayTime );
00156         show();
00157 }
00158 
00162 void QtxToolTip::hideTip()
00163 {
00164         hide();
00165   myWidgetRegion = QRect( -1, -1, -1, -1 );
00166         mySleepTimer->stop();
00167 }
00168 
00174 void QtxToolTip::maybeTip( const QPoint& pos )
00175 {
00176         QString text;
00177         QRect textRegion, theRegion( -1, -1, -1, -1 );
00178         QFont theFnt = font();
00179 
00180     emit maybeTip( pos, text, theFnt, textRegion, theRegion );
00181 
00182     if ( theRegion.isValid() )
00183         {
00184                 setFont( theFnt );
00185                 int margin = lineWidth() + indent();
00186                 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
00187                                          QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
00188                 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
00189                 if ( tipRegion.left() < 0 )
00190                         tipRegion.translate( -1 * tipRegion.left(), 0 );
00191                 showTip( tipRegion, text, theRegion );
00192         }
00193 }
00194 
00198 void QtxToolTip::onSleepTimeOut()
00199 {
00200         mySleepTimer->stop();
00201         hideTip();
00202 }
00203 
00207 void QtxToolTip::onWakeUpTimeOut()
00208 {
00209         myWakeUpTimer->stop();
00210   QPoint pos = QCursor::pos();
00211   if ( parentWidget() )
00212     pos = parentWidget()->mapFromGlobal( pos );
00213   maybeTip( pos );
00214 }
00215 
00219 void QtxToolTip::mousePressEvent( QMouseEvent* e )
00220 {
00221         hideTip();
00222         QWidget* reciever = parentWidget();
00223         QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
00224                                                                            reciever->mapFromGlobal( e->globalPos() ),
00225                                                                            e->button(), e->buttons(), Qt::KeypadModifier );
00226         QApplication::sendEvent( reciever, me );
00227 }
00228 
00232 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
00233 {
00234         hideTip();
00235         QWidget* reciever = parentWidget();
00236         QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
00237                                                                            reciever->mapFromGlobal( e->globalPos() ),
00238                                                                            e->button(), e->buttons(), Qt::KeypadModifier );
00239         QApplication::sendEvent( reciever, me );
00240 }
00241 
00246 void QtxToolTip::setWakeUpDelayTime( int theTime )
00247 {
00248   if( !(theTime < 0) )
00249     myWakeUpDelayTime = theTime;
00250 }
00251 
00256 void QtxToolTip::setShowDelayTime( int theTime )
00257 {
00258   if( !(theTime < 0) )
00259     myShowDelayTime = theTime;
00260 }
00261 
00265 QTimer* QtxToolTip::sleepTimer() const
00266 {
00267   return mySleepTimer;
00268 }
00269 
00273 QTimer* QtxToolTip::wakeUpTimer() const
00274 {
00275   return myWakeUpTimer;
00276 }