Salome HOME
IPAL9182: linking to libSPlot2d.so added
[modules/gui.git] / src / Qtx / QtxToolTip.cxx
1 // File:      QtxToolTip.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxToolTip.h"
5
6 #include <qfont.h>
7 #include <qtimer.h>
8 #include <qcursor.h>
9 #include <qfontmetrics.h>
10 #include <qapplication.h>
11
12 #define TOOLTIP_SHOW_DELAY 0500
13 #define TOOLTIP_HIDE_DELAY 7000
14
15 QtxToolTip::QtxToolTip( QWidget* parent )
16 : QLabel( parent, "", WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop | WType_TopLevel )
17 {
18   setIndent( 3 );
19         setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
20         setBackgroundColor( QColor( 255, 255, 231 ) );
21
22         myWidgetRegion = QRect( -1, -1, -1, -1 );
23
24         setFrameShape( QFrame::Panel );
25         setFrameShadow( QFrame::Plain );
26
27   parent->setMouseTracking( true );
28         parent->installEventFilter( this );
29         installEventFilter( this );
30
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() ) );
35
36   myWakeUpDelayTime = 700;
37   myShowDelayTime = 5000;
38 }
39
40 QtxToolTip::~QtxToolTip()
41 {
42 }
43
44 bool QtxToolTip::eventFilter( QObject* o, QEvent* e )
45 {
46         if ( ( e->type() == QEvent::Destroy ) || ( e->type() == QEvent::Close ) || ( e->type() == QEvent::Hide ) )
47         {
48                 hideTip();
49         }
50         if ( e->type() == QEvent::Leave )
51         {
52                 if ( isVisible() && ( o == this ) )
53                         hideTip();
54                 myWakeUpTimer->stop();
55         }
56         if ( e->type() == QEvent::MouseMove )
57         {
58                 QMouseEvent* me = (QMouseEvent*)e;
59                 QPoint thePos = parentWidget()->mapFromGlobal( me->globalPos() );
60                 if ( myWakeUpTimer->isActive() )
61                 {
62                         myWakeUpTimer->stop();
63                         myWakeUpTimer->start( myWakeUpDelayTime, true );
64                 }
65                 if ( isVisible() )
66                 {
67                         if ( !myWidgetRegion.contains( thePos ) )
68       {
69                                 hideTip();
70         myWidgetRegion = QRect( -1, -1, -1, -1 );
71       }
72                 }
73                 else
74                 {
75                         if ( !myWidgetRegion.isValid() || myWidgetRegion.contains( thePos ) )
76                                 myWakeUpTimer->start( myWakeUpDelayTime, true );
77                 }
78         }
79         if ( e->type() == QEvent::KeyPress )
80         {
81                 hideTip();
82         }
83         if ( o == parent() && ( e->type() == QEvent::MouseButtonPress ||
84                           e->type() == QEvent::MouseButtonRelease ) )
85         {
86                 hideTip();
87         }
88         return false;
89 }
90
91 void QtxToolTip::showTip( const QPoint& aPos, const QString& text, const QRect& aWidgetRegion )
92 {
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 );
97 }
98
99 void QtxToolTip::showTip( const QRect& aRegion, const QString& text, const QRect& aWidgetRegion )
100 {
101         setText( text );
102         myWidgetRegion = aWidgetRegion;
103         setGeometry( aRegion );
104         if ( myShowDelayTime != 0 )
105                 mySleepTimer->start( myShowDelayTime, true );
106         show();
107 }
108
109 void QtxToolTip::hideTip()
110 {
111         hide();
112   myWidgetRegion = QRect( -1, -1, -1, -1 );
113         mySleepTimer->stop();
114 }
115
116 void QtxToolTip::maybeTip( const QPoint& pos )
117 {
118         QString text;
119         QRect textRegion, theRegion( -1, -1, -1, -1 );
120         QFont theFnt = font();
121
122     emit maybeTip( pos, text, theFnt, textRegion, theRegion );
123
124     if ( theRegion.isValid() )
125         {
126                 setFont( theFnt );
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 );
134         }
135 }
136
137 void QtxToolTip::onSleepTimeOut()
138 {
139         mySleepTimer->stop();
140         hideTip();
141 }
142
143 void QtxToolTip::onWakeUpTimeOut()
144 {
145         myWakeUpTimer->stop();
146   QPoint pos = QCursor::pos();
147   if ( parentWidget() )
148     pos = parentWidget()->mapFromGlobal( pos );
149   maybeTip( pos );
150 }
151
152 void QtxToolTip::mousePressEvent( QMouseEvent* e )
153 {
154         hideTip();
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 );
160 }
161
162 void QtxToolTip::mouseDoubleClickEvent( QMouseEvent* e )
163 {
164         hideTip();
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 );
170 }
171
172 void QtxToolTip::setWakeUpDelayTime( int theTime )
173 {
174   if( !(theTime < 0) )
175     myWakeUpDelayTime = theTime;
176 }
177
178 void QtxToolTip::setShowDelayTime( int theTime )
179 {
180   if( !(theTime < 0) )
181     myShowDelayTime = theTime;
182 }
183
184 QTimer* QtxToolTip::sleepTimer() const
185 {
186   return mySleepTimer;
187 }
188
189 QTimer* QtxToolTip::wakeUpTimer() const
190 {
191   return myWakeUpTimer;
192 }