1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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/
19 // File: QtxToolTip.cxx
20 // Author: Sergey TELKOV
22 #include "QtxToolTip.h"
27 #include <qfontmetrics.h>
28 #include <qapplication.h>
30 #define TOOLTIP_SHOW_DELAY 0500
31 #define TOOLTIP_HIDE_DELAY 7000
36 QtxToolTip::QtxToolTip( QWidget* parent )
37 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WX11BypassWM | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
40 setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
41 setBackgroundColor( QColor( 255, 255, 231 ) );
43 myWidgetRegion = QRect( -1, -1, -1, -1 );
45 setFrameShape( QFrame::Panel );
46 setFrameShadow( QFrame::Plain );
48 parent->setMouseTracking( true );
49 parent->installEventFilter( this );
50 installEventFilter( this );
52 mySleepTimer = new QTimer( this );
53 myWakeUpTimer = new QTimer( this );
54 connect( mySleepTimer, SIGNAL( timeout() ), this, SLOT( onSleepTimeOut() ) );
55 connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
57 myWakeUpDelayTime = 700;
58 myShowDelayTime = 5000;
64 QtxToolTip::~QtxToolTip()
71 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
73 if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
77 if ( e->type() == QEvent::Leave )
79 if ( isVisible() && ( o == this ) )
81 myWakeUpTimer->stop();
83 if ( e->type() == QEvent::MouseMove )
85 QMouseEvent* me = (QMouseEvent*)e;
86 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
87 if ( myWakeUpTimer->isActive() )
89 myWakeUpTimer->stop();
90 myWakeUpTimer->start( myWakeUpDelayTime, true );
94 if ( !myWidgetRegion.contains( thePos ) )
97 myWidgetRegion = QRect( -1, -1, -1, -1 );
102 if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
103 myWakeUpTimer->start( myWakeUpDelayTime, true );
106 if ( e->type() == QEvent::KeyPress )
110 if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
111 e->type() == QEvent::MouseButtonRelease ) )
120 \param aPos - position
121 \param text - tooltip text
122 \param aWidgetRegion - rectangle
124 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
126 QFontMetrics theFM = fontMetrics();
127 int theHeight = theFM.height();
128 int theWidth = theFM.width( text ) + 2;
129 showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
134 \param aRegion - tooltip region
135 \param text - tooltip text
136 \param aWidgetRegion - widget rectangle
138 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
141 myWidgetRegion = aWidgetRegion;
142 setGeometry( aRegion );
143 if ( myShowDelayTime != 0 )
144 mySleepTimer->start( myShowDelayTime, true );
151 void QtxToolTip::hideTip()
154 myWidgetRegion = QRect( -1, -1, -1, -1 );
155 mySleepTimer->stop();
159 It is called when there is a possibility that a tool tip should be shown and
160 must decide whether there is a tool tip for the point p in the widget that this QToolTip object relates to
161 \param pos - position
163 void QtxToolTip::maybeTip( const QPoint& pos )
166 QRect textRegion, theRegion( -1, -1, -1, -1 );
167 QFont theFnt = font();
169 emit maybeTip( pos, text, theFnt, textRegion, theRegion );
171 if ( theRegion.isValid() )
174 int margin = lineWidth() + indent();
175 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
176 QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
177 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
178 if ( tipRegion.left() < 0 )
179 tipRegion.moveBy( -1 * tipRegion.left(), 0 );
180 showTip( tipRegion, text, theRegion );
185 SLOT: called when sleep time is out
187 void QtxToolTip::onSleepTimeOut()
189 mySleepTimer->stop();
194 SLOT: called when wake time is out
196 void QtxToolTip::onWakeUpTimeOut()
198 myWakeUpTimer->stop();
199 QPoint pos = QCursor::pos();
200 if ( parentWidget() )
201 pos = parentWidget()->mapFromGlobal( pos );
206 Custom mouse press event handler
208 void QtxToolTip::mousePressEvent( QMouseEvent* e )
211 QWidget* reciever = parentWidget();
212 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
213 reciever->mapFromGlobal( e->globalPos() ),
214 e->button(), e->state() );
215 QApplication::sendEvent( reciever, me );
219 Custom mouse double click event handler
221 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
224 QWidget* reciever = parentWidget();
225 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
226 reciever->mapFromGlobal( e->globalPos() ),
227 e->button(), e->state() );
228 QApplication::sendEvent( reciever, me );
235 void QtxToolTip::setWakeUpDelayTime( int theTime )
238 myWakeUpDelayTime = theTime;
245 void QtxToolTip::setShowDelayTime( int theTime )
248 myShowDelayTime = theTime;
252 \return timer measuring time of sleeping
254 QTimer* QtxToolTip::sleepTimer() const
260 \return timer measuring time of waking up
262 QTimer* QtxToolTip::wakeUpTimer() const
264 return myWakeUpTimer;