Salome HOME
5141e127dda713ab37f1e6ea248238d595b2ac13
[modules/gui.git] / src / Qtx / QtxToolTip.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 // File:      QtxToolTip.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxToolTip.h"
23
24 #include <qfont.h>
25 #include <qtimer.h>
26 #include <qcursor.h>
27 #include <qfontmetrics.h>
28 #include <qapplication.h>
29
30 #define TOOLTIP_SHOW_DELAY 0500
31 #define TOOLTIP_HIDE_DELAY 7000
32
33 QtxToolTip::QtxToolTip( QWidget* parent )
34 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WX11BypassWM | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
35 {
36   setIndent( 3 );
37         setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
38         setBackgroundColor( QColor( 255, 255, 231 ) );
39
40         myWidgetRegion = QRect( -1, -1, -1, -1 );
41
42         setFrameShape( QFrame::Panel );
43         setFrameShadow( QFrame::Plain );
44
45   parent->setMouseTracking( true );
46         parent->installEventFilter( this );
47         installEventFilter( this );
48
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() ) );
53
54   myWakeUpDelayTime = 700;
55   myShowDelayTime = 5000;
56 }
57
58 QtxToolTip::~QtxToolTip()
59 {
60 }
61
62 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
63 {
64         if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
65         {
66                 hideTip();
67         }
68         if ( e->type() == QEvent::Leave )
69         {
70                 if ( isVisible() && ( o == this ) )
71                         hideTip();
72                 myWakeUpTimer->stop();
73         }
74         if ( e->type() == QEvent::MouseMove )
75         {
76                 QMouseEvent* me = (QMouseEvent*)e;
77                 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
78                 if ( myWakeUpTimer->isActive() )
79                 {
80                         myWakeUpTimer->stop();
81                         myWakeUpTimer->start( myWakeUpDelayTime, true );
82                 }
83                 if ( isVisible() )
84                 {
85                         if ( !myWidgetRegion.contains( thePos ) )
86       {
87                                 hideTip();
88         myWidgetRegion = QRect( -1, -1, -1, -1 );
89       }
90                 }
91                 else
92                 {
93                         if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
94                                 myWakeUpTimer->start( myWakeUpDelayTime, true );
95                 }
96         }
97         if ( e->type() == QEvent::KeyPress )
98         {
99                 hideTip();
100         }
101         if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
102                           e->type() == QEvent::MouseButtonRelease ) )
103         {
104                 hideTip();
105         }
106         return false;
107 }
108
109 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
110 {
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 );
115 }
116
117 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
118 {
119         setText( text );
120         myWidgetRegion = aWidgetRegion;
121         setGeometry( aRegion );
122         if ( myShowDelayTime != 0 )
123                 mySleepTimer->start( myShowDelayTime, true );
124         show();
125 }
126
127 void QtxToolTip::hideTip()
128 {
129         hide();
130   myWidgetRegion = QRect( -1, -1, -1, -1 );
131         mySleepTimer->stop();
132 }
133
134 void QtxToolTip::maybeTip( const QPoint& pos )
135 {
136         QString text;
137         QRect textRegion, theRegion( -1, -1, -1, -1 );
138         QFont theFnt = font();
139
140     emit maybeTip( pos, text, theFnt, textRegion, theRegion );
141
142     if ( theRegion.isValid() )
143         {
144                 setFont( theFnt );
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 );
152         }
153 }
154
155 void QtxToolTip::onSleepTimeOut()
156 {
157         mySleepTimer->stop();
158         hideTip();
159 }
160
161 void QtxToolTip::onWakeUpTimeOut()
162 {
163         myWakeUpTimer->stop();
164   QPoint pos = QCursor::pos();
165   if ( parentWidget() )
166     pos = parentWidget()->mapFromGlobal( pos );
167   maybeTip( pos );
168 }
169
170 void QtxToolTip::mousePressEvent( QMouseEvent* e )
171 {
172         hideTip();
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 );
178 }
179
180 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
181 {
182         hideTip();
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 );
188 }
189
190 void QtxToolTip::setWakeUpDelayTime( int theTime )
191 {
192   if( !(theTime < 0) )
193     myWakeUpDelayTime = theTime;
194 }
195
196 void QtxToolTip::setShowDelayTime( int theTime )
197 {
198   if( !(theTime < 0) )
199     myShowDelayTime = theTime;
200 }
201
202 QTimer* QtxToolTip::sleepTimer() const
203 {
204   return mySleepTimer;
205 }
206
207 QTimer* QtxToolTip::wakeUpTimer() const
208 {
209   return myWakeUpTimer;
210 }