1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
23 // File: QtxToolTip.cxx
24 // Author: Sergey TELKOV
26 #include "QtxToolTip.h"
31 #include <QFontMetrics>
32 #include <QApplication>
34 #include <QMouseEvent>
36 #define TOOLTIP_SHOW_DELAY 0500
37 #define TOOLTIP_HIDE_DELAY 7000
42 QtxToolTip::QtxToolTip( QWidget* parent )
43 : QLabel( parent, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::Window )
47 setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
49 palette.setColor( backgroundRole(), QColor( 255, 255, 231 ) );
50 setPalette( palette );
52 myWidgetRegion = QRect( -1, -1, -1, -1 );
54 setFrameShape( QFrame::Panel );
55 setFrameShadow( QFrame::Plain );
57 parent->setMouseTracking( true );
58 parent->installEventFilter( this );
59 installEventFilter( this );
61 mySleepTimer = new QTimer( this );
62 mySleepTimer->setSingleShot( true );
63 myWakeUpTimer = new QTimer( this );
64 myWakeUpTimer->setSingleShot( true );
65 connect( mySleepTimer, SIGNAL( timeout() ), this, SLOT( onSleepTimeOut() ) );
66 connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
68 myWakeUpDelayTime = 700;
69 myShowDelayTime = 5000;
75 QtxToolTip::~QtxToolTip()
82 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
84 if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
88 if ( e->type() == QEvent::Leave )
90 if ( isVisible() && ( o == this ) )
92 myWakeUpTimer->stop();
94 if ( e->type() == QEvent::MouseMove )
96 QMouseEvent* me = (QMouseEvent*)e;
97 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
98 if ( myWakeUpTimer->isActive() )
100 myWakeUpTimer->stop();
101 myWakeUpTimer->start( myWakeUpDelayTime );
105 if ( !myWidgetRegion.contains( thePos ) )
108 myWidgetRegion = QRect( -1, -1, -1, -1 );
113 if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
114 myWakeUpTimer->start( myWakeUpDelayTime );
117 if ( e->type() == QEvent::KeyPress )
121 if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
122 e->type() == QEvent::MouseButtonRelease ) )
131 \param aPos - position
132 \param text - tooltip text
133 \param aWidgetRegion - rectangle
135 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
137 QFontMetrics theFM = fontMetrics();
138 int theHeight = theFM.height();
139 int theWidth = theFM.width( text ) + 2;
140 showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
145 \param aRegion - tooltip region
146 \param text - tooltip text
147 \param aWidgetRegion - widget rectangle
149 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
152 myWidgetRegion = aWidgetRegion;
153 setGeometry( aRegion );
154 if ( myShowDelayTime != 0 )
155 mySleepTimer->start( myShowDelayTime );
162 void QtxToolTip::hideTip()
165 myWidgetRegion = QRect( -1, -1, -1, -1 );
166 mySleepTimer->stop();
170 It is called when there is a possibility that a tool tip should be shown and
171 must decide whether there is a tool tip for the point p in the widget that this QToolTip object relates to
172 \param pos - position
174 void QtxToolTip::maybeTip( const QPoint& pos )
177 QRect textRegion, theRegion( -1, -1, -1, -1 );
178 QFont theFnt = font();
180 emit maybeTip( pos, text, theFnt, textRegion, theRegion );
182 if ( theRegion.isValid() )
185 int margin = lineWidth() + indent();
186 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
187 QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
188 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
189 if ( tipRegion.left() < 0 )
190 tipRegion.translate( -1 * tipRegion.left(), 0 );
191 showTip( tipRegion, text, theRegion );
196 SLOT: called when sleep time is out
198 void QtxToolTip::onSleepTimeOut()
200 mySleepTimer->stop();
205 SLOT: called when wake time is out
207 void QtxToolTip::onWakeUpTimeOut()
209 myWakeUpTimer->stop();
210 QPoint pos = QCursor::pos();
211 if ( parentWidget() )
212 pos = parentWidget()->mapFromGlobal( pos );
217 Custom mouse press event handler
219 void QtxToolTip::mousePressEvent( QMouseEvent* e )
222 QWidget* reciever = parentWidget();
223 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
224 reciever->mapFromGlobal( e->globalPos() ),
225 e->button(), e->buttons(), Qt::KeypadModifier );
226 QApplication::sendEvent( reciever, me );
230 Custom mouse double click event handler
232 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
235 QWidget* reciever = parentWidget();
236 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
237 reciever->mapFromGlobal( e->globalPos() ),
238 e->button(), e->buttons(), Qt::KeypadModifier );
239 QApplication::sendEvent( reciever, me );
246 void QtxToolTip::setWakeUpDelayTime( int theTime )
249 myWakeUpDelayTime = theTime;
256 void QtxToolTip::setShowDelayTime( int theTime )
259 myShowDelayTime = theTime;
263 \return timer measuring time of sleeping
265 QTimer* QtxToolTip::sleepTimer() const
271 \return timer measuring time of waking up
273 QTimer* QtxToolTip::wakeUpTimer() const
275 return myWakeUpTimer;