1 // File: QtxToolTip.cxx
2 // Author: Sergey TELKOV
4 #include "QtxToolTip.h"
9 #include <qfontmetrics.h>
10 #include <qapplication.h>
12 #define TOOLTIP_SHOW_DELAY 0500
13 #define TOOLTIP_HIDE_DELAY 7000
15 QtxToolTip::QtxToolTip( QWidget* parent )
16 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
19 setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
20 setBackgroundColor( QColor( 255, 255, 231 ) );
22 myWidgetRegion = QRect( -1, -1, -1, -1 );
24 setFrameShape( QFrame::Panel );
25 setFrameShadow( QFrame::Plain );
27 parent->setMouseTracking( true );
28 parent->installEventFilter( this );
29 installEventFilter( this );
31 mySleepTimer = new QTimer( this );
32 myWakeUpTimer = new QTimer( this );
33 connect( mySleepTimer, SIGNAL( timeout() ), this, SLOT( onSleepTimeOut() ) );
34 connect( myWakeUpTimer, SIGNAL( timeout() ), this, SLOT( onWakeUpTimeOut() ) );
36 myWakeUpDelayTime = 700;
37 myShowDelayTime = 5000;
40 QtxToolTip::~QtxToolTip()
44 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
46 if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
50 if ( e->type() == QEvent::Leave )
52 if ( isVisible() && ( o == this ) )
54 myWakeUpTimer->stop();
56 if ( e->type() == QEvent::MouseMove )
58 QMouseEvent* me = (QMouseEvent*)e;
59 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
60 if ( myWakeUpTimer->isActive() )
62 myWakeUpTimer->stop();
63 myWakeUpTimer->start( myWakeUpDelayTime, true );
67 if ( !myWidgetRegion.contains( thePos ) )
70 myWidgetRegion = QRect( -1, -1, -1, -1 );
75 if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
76 myWakeUpTimer->start( myWakeUpDelayTime, true );
79 if ( e->type() == QEvent::KeyPress )
83 if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
84 e->type() == QEvent::MouseButtonRelease ) )
91 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
93 QFontMetrics theFM = fontMetrics();
94 int theHeight = theFM.height();
95 int theWidth = theFM.width( text ) + 2;
96 showTip( QRect( QPoint( aPos.x(), aPos.y() + 10 ), QSize( theWidth, theHeight ) ), text, aWidgetRegion );
99 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
102 myWidgetRegion = aWidgetRegion;
103 setGeometry( aRegion );
104 if ( myShowDelayTime != 0 )
105 mySleepTimer->start( myShowDelayTime, true );
109 void QtxToolTip::hideTip()
112 myWidgetRegion = QRect( -1, -1, -1, -1 );
113 mySleepTimer->stop();
116 void QtxToolTip::maybeTip( const QPoint& pos )
119 QRect textRegion, theRegion( -1, -1, -1, -1 );
120 QFont theFnt = font();
122 emit maybeTip( pos, text, theFnt, textRegion, theRegion );
124 if ( theRegion.isValid() )
127 int margin = lineWidth() + indent();
128 QRect dspRegion( QPoint( textRegion.x() - margin, textRegion.y() ),
129 QSize( textRegion.width() + 2 * margin, textRegion.height() ) );
130 QRect tipRegion( parentWidget()->mapToGlobal( dspRegion.topLeft() ), dspRegion.size() );
131 if ( tipRegion.left() < 0 )
132 tipRegion.moveBy( -1 * tipRegion.left(), 0 );
133 showTip( tipRegion, text, theRegion );
137 void QtxToolTip::onSleepTimeOut()
139 mySleepTimer->stop();
143 void QtxToolTip::onWakeUpTimeOut()
145 myWakeUpTimer->stop();
146 QPoint pos = QCursor::pos();
147 if ( parentWidget() )
148 pos = parentWidget()->mapFromGlobal( pos );
152 void QtxToolTip::mousePressEvent( QMouseEvent* e )
155 QWidget* reciever = parentWidget();
156 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonPress,
157 reciever->mapFromGlobal( e->globalPos() ),
158 e->button(), e->state() );
159 QApplication::sendEvent( reciever, me );
162 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
165 QWidget* reciever = parentWidget();
166 QMouseEvent* me = new QMouseEvent( QEvent::MouseButtonDblClick,
167 reciever->mapFromGlobal( e->globalPos() ),
168 e->button(), e->state() );
169 QApplication::sendEvent( reciever, me );
172 void QtxToolTip::setWakeUpDelayTime( int theTime )
175 myWakeUpDelayTime = theTime;
178 void QtxToolTip::setShowDelayTime( int theTime )
181 myShowDelayTime = theTime;
184 QTimer* QtxToolTip::sleepTimer() const
189 QTimer* QtxToolTip::wakeUpTimer() const
191 return myWakeUpTimer;