1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxToolTip.cxx
23 // Author: Sergey TELKOV
25 #include "QtxToolTip.h"
30 #include <QFontMetrics>
31 #include <QApplication>
33 #include <QMouseEvent>
35 #define TOOLTIP_SHOW_DELAY 0500
36 #define TOOLTIP_HIDE_DELAY 7000
41 QtxToolTip::QtxToolTip( QWidget* parent )
42 : QLabel( parent, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::Window )
46 setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
48 palette.setColor( backgroundRole(), QColor( 255, 255, 231 ) );
49 setPalette( palette );
51 myWidgetRegion = QRect( -1, -1, -1, -1 );
53 setFrameShape( QFrame::Panel );
54 setFrameShadow( QFrame::Plain );
56 parent->setMouseTracking( true );
57 parent->installEventFilter( this );
58 installEventFilter( this );
60 mySleepTimer = new QTimer( this );
61 mySleepTimer->setSingleShot( true );
62 myWakeUpTimer = new QTimer( this );
63 myWakeUpTimer->setSingleShot( true );
64 connect( mySleepTimer, SIGNAL( timeout() ), this, SLOT( onSleepTimeOut() ) );
65 connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
67 myWakeUpDelayTime = 700;
68 myShowDelayTime = 5000;
74 QtxToolTip::~QtxToolTip()
81 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
83 if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
87 if ( e->type() == QEvent::Leave )
89 if ( isVisible() && ( o == this ) )
91 myWakeUpTimer->stop();
93 if ( e->type() == QEvent::MouseMove )
95 QMouseEvent* me = (QMouseEvent*)e;
96 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
97 if ( myWakeUpTimer->isActive() )
99 myWakeUpTimer->stop();
100 myWakeUpTimer->start( myWakeUpDelayTime );
104 if ( !myWidgetRegion.contains( thePos ) )
107 myWidgetRegion = QRect( -1, -1, -1, -1 );
112 if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
113 myWakeUpTimer->start( myWakeUpDelayTime );
116 if ( e->type() == QEvent::KeyPress )
120 if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
121 e->type() == QEvent::MouseButtonRelease ) )
130 \param aPos - position
131 \param text - tooltip text
132 \param aWidgetRegion - rectangle
134 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
136 QFontMetrics theFM = fontMetrics();
137 int theHeight = theFM.height();
138 int theWidth = theFM.width( text ) + 2;
139 showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
144 \param aRegion - tooltip region
145 \param text - tooltip text
146 \param aWidgetRegion - widget rectangle
148 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
151 myWidgetRegion = aWidgetRegion;
152 setGeometry( aRegion );
153 if ( myShowDelayTime != 0 )
154 mySleepTimer->start( myShowDelayTime );
161 void QtxToolTip::hideTip()
164 myWidgetRegion = QRect( -1, -1, -1, -1 );
165 mySleepTimer->stop();
169 It is called when there is a possibility that a tool tip should be shown and
170 must decide whether there is a tool tip for the point p in the widget that this QToolTip object relates to
171 \param pos - position
173 void QtxToolTip::maybeTip( const QPoint& pos )
176 QRect textRegion, theRegion( -1, -1, -1, -1 );
177 QFont theFnt = font();
179 emit maybeTip( pos, text, theFnt, textRegion, theRegion );
181 if ( theRegion.isValid() )
184 int margin = lineWidth() + indent();
185 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
186 QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
187 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
188 if ( tipRegion.left() < 0 )
189 tipRegion.translate( -1 * tipRegion.left(), 0 );
190 showTip( tipRegion, text, theRegion );
195 SLOT: called when sleep time is out
197 void QtxToolTip::onSleepTimeOut()
199 mySleepTimer->stop();
204 SLOT: called when wake time is out
206 void QtxToolTip::onWakeUpTimeOut()
208 myWakeUpTimer->stop();
209 QPoint pos = QCursor::pos();
210 if ( parentWidget() )
211 pos = parentWidget()->mapFromGlobal( pos );
216 Custom mouse press event handler
218 void QtxToolTip::mousePressEvent( QMouseEvent* e )
221 QWidget* reciever = parentWidget();
222 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
223 reciever->mapFromGlobal( e->globalPos() ),
224 e->button(), e->buttons(), Qt::KeypadModifier );
225 QApplication::sendEvent( reciever, me );
229 Custom mouse double click event handler
231 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
234 QWidget* reciever = parentWidget();
235 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
236 reciever->mapFromGlobal( e->globalPos() ),
237 e->button(), e->buttons(), Qt::KeypadModifier );
238 QApplication::sendEvent( reciever, me );
245 void QtxToolTip::setWakeUpDelayTime( int theTime )
248 myWakeUpDelayTime = theTime;
255 void QtxToolTip::setShowDelayTime( int theTime )
258 myShowDelayTime = theTime;
262 \return timer measuring time of sleeping
264 QTimer* QtxToolTip::sleepTimer() const
270 \return timer measuring time of waking up
272 QTimer* QtxToolTip::wakeUpTimer() const
274 return myWakeUpTimer;