Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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/ or email : webmaster.salome@opencascade.com
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 /*!
34   Constructor
35 */
36 QtxToolTip::QtxToolTip( QWidget* parent )
37 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WX11BypassWM | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
38 {
39   setIndent( 3 );
40         setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
41         setBackgroundColor( QColor( 255, 255, 231 ) );
42
43         myWidgetRegion = QRect( -1, -1, -1, -1 );
44
45         setFrameShape( QFrame::Panel );
46         setFrameShadow( QFrame::Plain );
47
48   parent->setMouseTracking( true );
49         parent->installEventFilter( this );
50         installEventFilter( this );
51
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() ) );
56
57   myWakeUpDelayTime = 700;
58   myShowDelayTime = 5000;
59 }
60
61 /*!
62   Destructor
63 */
64 QtxToolTip::~QtxToolTip()
65 {
66 }
67
68 /*!
69   Custom event filter
70 */
71 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
72 {
73         if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
74         {
75                 hideTip();
76         }
77         if ( e->type() == QEvent::Leave )
78         {
79                 if ( isVisible() && ( o == this ) )
80                         hideTip();
81                 myWakeUpTimer->stop();
82         }
83         if ( e->type() == QEvent::MouseMove )
84         {
85                 QMouseEvent* me = (QMouseEvent*)e;
86                 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
87                 if ( myWakeUpTimer->isActive() )
88                 {
89                         myWakeUpTimer->stop();
90                         myWakeUpTimer->start( myWakeUpDelayTime, true );
91                 }
92                 if ( isVisible() )
93                 {
94                         if ( !myWidgetRegion.contains( thePos ) )
95       {
96                                 hideTip();
97         myWidgetRegion = QRect( -1, -1, -1, -1 );
98       }
99                 }
100                 else
101                 {
102                         if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
103                                 myWakeUpTimer->start( myWakeUpDelayTime, true );
104                 }
105         }
106         if ( e->type() == QEvent::KeyPress )
107         {
108                 hideTip();
109         }
110         if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
111                           e->type() == QEvent::MouseButtonRelease ) )
112         {
113                 hideTip();
114         }
115         return false;
116 }
117
118 /*!
119   Shows tool tip
120   \param aPos - position
121   \param text - tooltip text
122   \param aWidgetRegion - rectangle
123 */
124 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
125 {
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 );
130 }
131
132 /*!
133   Shows tool tip
134   \param aRegion - tooltip region
135   \param text - tooltip text
136   \param aWidgetRegion - widget rectangle
137 */
138 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
139 {
140         setText( text );
141         myWidgetRegion = aWidgetRegion;
142         setGeometry( aRegion );
143         if ( myShowDelayTime != 0 )
144                 mySleepTimer->start( myShowDelayTime, true );
145         show();
146 }
147
148 /*!
149   Hides tooltip
150 */
151 void QtxToolTip::hideTip()
152 {
153         hide();
154   myWidgetRegion = QRect( -1, -1, -1, -1 );
155         mySleepTimer->stop();
156 }
157
158 /*!
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
162 */
163 void QtxToolTip::maybeTip( const QPoint& pos )
164 {
165         QString text;
166         QRect textRegion, theRegion( -1, -1, -1, -1 );
167         QFont theFnt = font();
168
169     emit maybeTip( pos, text, theFnt, textRegion, theRegion );
170
171     if ( theRegion.isValid() )
172         {
173                 setFont( theFnt );
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 );
181         }
182 }
183
184 /*!
185   SLOT: called when sleep time is out
186 */
187 void QtxToolTip::onSleepTimeOut()
188 {
189         mySleepTimer->stop();
190         hideTip();
191 }
192
193 /*!
194   SLOT: called when wake time is out
195 */
196 void QtxToolTip::onWakeUpTimeOut()
197 {
198         myWakeUpTimer->stop();
199   QPoint pos = QCursor::pos();
200   if ( parentWidget() )
201     pos = parentWidget()->mapFromGlobal( pos );
202   maybeTip( pos );
203 }
204
205 /*!
206   Custom mouse press event handler
207 */
208 void QtxToolTip::mousePressEvent( QMouseEvent* e )
209 {
210         hideTip();
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 );
216 }
217
218 /*!
219   Custom mouse double click event handler
220 */
221 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
222 {
223         hideTip();
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 );
229 }
230
231 /*!
232   Sets wake delay time
233   \param theTime
234 */
235 void QtxToolTip::setWakeUpDelayTime( int theTime )
236 {
237   if( !(theTime < 0) )
238     myWakeUpDelayTime = theTime;
239 }
240
241 /*!
242   Sets show delay time
243   \param theTime
244 */
245 void QtxToolTip::setShowDelayTime( int theTime )
246 {
247   if( !(theTime < 0) )
248     myShowDelayTime = theTime;
249 }
250
251 /*!
252   \return timer measuring time of sleeping
253 */
254 QTimer* QtxToolTip::sleepTimer() const
255 {
256   return mySleepTimer;
257 }
258
259 /*!
260   \return timer measuring time of waking up
261 */
262 QTimer* QtxToolTip::wakeUpTimer() const
263 {
264   return myWakeUpTimer;
265 }