Salome HOME
Copyright update 2022
[modules/gui.git] / src / GLViewer / GLViewer_ToolTip.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "GLViewer_Context.h"
24 #include "GLViewer_ToolTip.h"
25 #include "GLViewer_Viewer2d.h"
26 #include "GLViewer_ViewPort2d.h"
27 #include "GLViewer_ViewFrame.h"
28
29 #include <QLabel>
30 #include <QTimer>
31 #include <QBitmap>
32 #include <QApplication>
33 #include <QToolTip>
34 #include <QMouseEvent>
35
36 /*!
37   constructor
38 */
39 GLViewer_ObjectTip::GLViewer_ObjectTip( GLViewer_ViewPort2d* theParent )
40 :QObject(),
41  myPoint( -1, -1 )
42 {
43   mypViewPort = theParent;
44   mypLabel = new QLabel( "Test", NULL, 
45                          Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool | Qt::X11BypassWindowManagerHint );
46   mypLabel->setObjectName("ObjectTipText");
47   mypLabel->setMargin( 1 );
48   mypLabel->setFrameStyle( QFrame::Plain | QFrame::Box );
49   mypLabel->setLineWidth( 1 );
50   mypLabel->setAlignment( Qt::AlignAbsolute | Qt::AlignTop );
51   mypLabel->setIndent( 0 );
52   mypLabel->ensurePolished();
53   
54   mypTimer = new QTimer( this );
55
56   connect( mypTimer, SIGNAL( timeout() ), this, SLOT( showTip() ) );
57 }
58
59 /*!
60   destructor
61 */
62 GLViewer_ObjectTip::~GLViewer_ObjectTip()
63
64   if( mypLabel )
65     delete mypLabel;
66 }
67
68
69 /*!
70   It is called when there is a possibility that a tool tip should be shown
71   \param p - position of tooltip
72 */
73 bool GLViewer_ObjectTip::maybeTip( const QPoint& /*p*/ )
74 {
75   GLViewer_Context* aContext = ((GLViewer_Viewer2d*)mypViewPort->getViewFrame()->getViewer())->getGLContext();
76   GLViewer_Object* anObj = aContext->getCurrentObject();
77   if( anObj )
78   {
79     setText( anObj->getName() );
80     return true;
81   }
82
83   return false;
84 }
85
86 /*!
87   Custom event filter
88 */
89 bool GLViewer_ObjectTip::eventFilter( QObject* theObj, QEvent* e )
90 {
91   hideTipAndSleep();
92   switch( e->type() )
93   {
94     case QEvent::MouseMove:
95     {
96       QWidget* aWidget = (QWidget*) theObj;
97       if( aWidget == mypViewPort->getGLWidget() )
98       {
99         wakeup();
100         QMouseEvent* m = (QMouseEvent *)e;
101
102         myPoint.setX( m->x() );
103         myPoint.setY( m->y() );
104       }
105     }
106     default:
107       break;
108   }
109   return false;
110 }
111
112 /*!
113   Hides tooltip and stops timer
114 */
115 void GLViewer_ObjectTip::hideTipAndSleep()
116 {
117   myPoint.setX(-1);
118   myPoint.setY(-1);
119
120   if( mypLabel )
121   {
122     mypLabel->hide();
123   }
124   mypTimer->stop();
125 }
126
127 /*!
128   Shows tooltip
129 */
130 void GLViewer_ObjectTip::showTip()
131 {
132   if( maybeTip( myPoint ) )
133   {
134     
135     mypLabel->setText( myText );
136     mypLabel->adjustSize( );
137     
138     QPoint pos = mypViewPort->getGLWidget()->mapToGlobal( myPoint );
139     
140     int cur_height = 24;
141     QCursor* aCursor = QApplication::overrideCursor();
142     if( aCursor )
143     {
144       const QBitmap* aBitmap = aCursor->bitmap();
145       if( aBitmap )
146         cur_height = aBitmap->height();
147     }
148     mypLabel->setGeometry( pos.x(), pos.y() + cur_height, mypLabel->width(), mypLabel->height() );
149     mypLabel->setPalette( QToolTip::palette() );
150
151     mypLabel->show();
152
153   }
154 }
155
156 /*!
157   Restarts timer
158 */
159 void GLViewer_ObjectTip::wakeup( int theTime )
160 {
161   if( mypTimer->isActive() )
162     mypTimer->stop();
163   mypTimer->start( theTime );
164 }