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
33 QtxToolTip::QtxToolTip( QWidget* parent )
34 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
37 setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
38 setBackgroundColor( QColor( 255, 255, 231 ) );
40 myWidgetRegion = QRect( -1, -1, -1, -1 );
42 setFrameShape( QFrame::Panel );
43 setFrameShadow( QFrame::Plain );
45 parent->setMouseTracking( true );
46 parent->installEventFilter( this );
47 installEventFilter( this );
49 mySleepTimer = new QTimer( this );
50 myWakeUpTimer = new QTimer( this );
51 connect( mySleepTimer, SIGNAL( timeout() ), this, SLOT( onSleepTimeOut() ) );
52 connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
54 myWakeUpDelayTime = 700;
55 myShowDelayTime = 5000;
58 QtxToolTip::~QtxToolTip()
62 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
64 if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
68 if ( e->type() == QEvent::Leave )
70 if ( isVisible() && ( o == this ) )
72 myWakeUpTimer->stop();
74 if ( e->type() == QEvent::MouseMove )
76 QMouseEvent* me = (QMouseEvent*)e;
77 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
78 if ( myWakeUpTimer->isActive() )
80 myWakeUpTimer->stop();
81 myWakeUpTimer->start( myWakeUpDelayTime, true );
85 if ( !myWidgetRegion.contains( thePos ) )
88 myWidgetRegion = QRect( -1, -1, -1, -1 );
93 if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
94 myWakeUpTimer->start( myWakeUpDelayTime, true );
97 if ( e->type() == QEvent::KeyPress )
101 if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
102 e->type() == QEvent::MouseButtonRelease ) )
109 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
111 QFontMetrics theFM = fontMetrics();
112 int theHeight = theFM.height();
113 int theWidth = theFM.width( text ) + 2;
114 showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
117 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
120 myWidgetRegion = aWidgetRegion;
121 setGeometry( aRegion );
122 if ( myShowDelayTime != 0 )
123 mySleepTimer->start( myShowDelayTime, true );
127 void QtxToolTip::hideTip()
130 myWidgetRegion = QRect( -1, -1, -1, -1 );
131 mySleepTimer->stop();
134 void QtxToolTip::maybeTip( const QPoint& pos )
137 QRect textRegion, theRegion( -1, -1, -1, -1 );
138 QFont theFnt = font();
140 emit maybeTip( pos, text, theFnt, textRegion, theRegion );
142 if ( theRegion.isValid() )
145 int margin = lineWidth() + indent();
146 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
147 QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
148 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
149 if ( tipRegion.left() < 0 )
150 tipRegion.moveBy( -1 * tipRegion.left(), 0 );
151 showTip( tipRegion, text, theRegion );
155 void QtxToolTip::onSleepTimeOut()
157 mySleepTimer->stop();
161 void QtxToolTip::onWakeUpTimeOut()
163 myWakeUpTimer->stop();
164 QPoint pos = QCursor::pos();
165 if ( parentWidget() )
166 pos = parentWidget()->mapFromGlobal( pos );
170 void QtxToolTip::mousePressEvent( QMouseEvent* e )
173 QWidget* reciever = parentWidget();
174 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
175 reciever->mapFromGlobal( e->globalPos() ),
176 e->button(), e->state() );
177 QApplication::sendEvent( reciever, me );
180 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
183 QWidget* reciever = parentWidget();
184 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
185 reciever->mapFromGlobal( e->globalPos() ),
186 e->button(), e->state() );
187 QApplication::sendEvent( reciever, me );
190 void QtxToolTip::setWakeUpDelayTime( int theTime )
193 myWakeUpDelayTime = theTime;
196 void QtxToolTip::setShowDelayTime( int theTime )
199 myShowDelayTime = theTime;
202 QTimer* QtxToolTip::sleepTimer() const
207 QTimer* QtxToolTip::wakeUpTimer() const
209 return myWakeUpTimer;