Salome HOME
Merge relevant changes from V8_0_0_BR branch
[modules/gui.git] / src / GLViewer / GLViewer_ToolTip.cxx
1 // Copyright (C) 2007-2015  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 //  Author : OPEN CASCADE
24 // File:      GLViewer_ToolTip.xx
25 // Created:   March, 2005
26 //#include "GLViewerAfx.h"
27 //
28 #include "GLViewer_Context.h"
29 #include "GLViewer_ToolTip.h"
30 #include "GLViewer_Viewer2d.h"
31 #include "GLViewer_ViewPort2d.h"
32 #include "GLViewer_ViewFrame.h"
33
34 #include <QLabel>
35 #include <QTimer>
36 #include <QBitmap>
37 #include <QApplication>
38 #include <QToolTip>
39 #include <QMouseEvent>
40
41 /*!
42   constructor
43 */
44 GLViewer_ObjectTip::GLViewer_ObjectTip( GLViewer_ViewPort2d* theParent )
45 :QObject(),
46  myText(),
47  myPoint( -1, -1 )
48 {
49   mypViewPort = theParent;
50   //mypLabel = NULL;
51   mypLabel = new QLabel( "Test", NULL, 
52                          Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool | Qt::X11BypassWindowManagerHint );
53   mypLabel->setObjectName("ObjectTipText");
54   mypLabel->setMargin( 1 );
55   //mypLabel->setAutoMask( false );
56   mypLabel->setFrameStyle( QFrame::Plain | QFrame::Box );
57   mypLabel->setLineWidth( 1 );
58   mypLabel->setAlignment( Qt::AlignAbsolute | Qt::AlignTop );
59   mypLabel->setIndent( 0 );
60   mypLabel->ensurePolished();
61   
62   //mypLabel->setPalette( QToolTip::palette() );
63
64   mypTimer = new QTimer( this );
65
66   connect( mypTimer, SIGNAL( timeout() ), this, SLOT( showTip() ) );
67 }
68
69 /*!
70   destructor
71 */
72 GLViewer_ObjectTip::~GLViewer_ObjectTip()
73
74 //  delete mypRect;
75   if( mypLabel )
76     delete mypLabel;
77
78   //if( mypPoint )
79   //  delete mypPoint;
80
81   //if( mypTimer )
82   //  delete mypTimer;
83 }
84
85
86 /*!
87   It is called when there is a possibility that a tool tip should be shown
88   \param p - position of tooltip
89 */
90 bool GLViewer_ObjectTip::maybeTip( const QPoint &p )
91 {
92
93
94   GLViewer_Context* aContext = ((GLViewer_Viewer2d*)mypViewPort->getViewFrame()->getViewer())->getGLContext();
95
96   /*if( !aContext->currentObjectIsChanged() )
97     return false;
98   else
99     return true;
100   if( myPoint.x() == -1 && myPoint.y() == -1 || aContext->currentObjectIsChanged())
101   {
102     myPoint = p;
103   }
104   else/if( abs(myPoint.y() - p.y()) < 16 )
105   {
106     return;
107   }
108   else // > 16
109   {
110     myPoint = p;
111   }
112 */  
113   GLViewer_Object* anObj = aContext->getCurrentObject();
114   if( anObj )
115   {
116     setText( anObj->getName() );
117     return true;
118   }
119
120   return false;
121   /*if( anObj )
122   {
123     //GLViewer_Rect* aRect = anObj->getRect();
124     //QRect aWinRect = mypViewPort->GLV2win( *aRect );
125     tip( QRect( p.x(), p.y(), 1, 1 ), anObj->getName() );
126     //QFontMetrics aFM( font() );    
127      //showTip( aWinRect, anObj->getName(), QRect( 0, 0, aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 ) );
128     //tip( aWinRect, anObj->getName(), aWinRect( aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 )  );
129   }
130 //  else
131 //    clear();
132     
133   //tip( QRect( 0, 0, mypViewPort->getGLWidget()->width(),mypViewPort->getGLWidget()->height() ) , "test Tool tip" );
134   */
135 }
136
137 /*!
138   Custom event filter
139 */
140 bool GLViewer_ObjectTip::eventFilter( QObject* theObj, QEvent* e )
141 {
142   hideTipAndSleep();
143   switch( e->type() )
144   {
145     /*case QEvent::MouseButtonPress:
146     case QEvent::MouseButtonRelease:
147     case QEvent::MouseButtonDblClick:
148     case QEvent::KeyPress:
149     case QEvent::KeyRelease:
150             // input - turn off tool tip mode
151             hideTipAndSleep();
152             break;*/
153     case QEvent::MouseMove:
154       {
155         //hideTipAndSleep();
156         /*if( mypTimer->isActive() )
157         {
158           mypTimer->Stop();
159           wakeUp();
160         }*/
161         QWidget* aWidget = (QWidget*) theObj;
162         if( aWidget == mypViewPort->getGLWidget() )
163         {
164           wakeup();
165           QMouseEvent* m = (QMouseEvent *)e;
166           //if( !mypPoint )
167           //  mypPoint = new QPoint();
168
169           myPoint.setX( m->x() );
170           myPoint.setY( m->y() );
171         }
172       }
173   }
174   return false;
175 }
176
177 /*!
178   Hides tooltip and stops timer
179 */
180 void GLViewer_ObjectTip::hideTipAndSleep()
181 {
182   //if( mypPoint )
183   //  delete mypPoint;
184   myPoint.setX(-1);
185   myPoint.setY(-1);
186
187   if( mypLabel )
188   {
189     mypLabel->hide();
190     //delete mypLabel;
191   }
192   mypTimer->stop();
193 }
194
195 /*!
196   Shows tooltip
197 */
198 void GLViewer_ObjectTip::showTip()
199 {
200   if( maybeTip( myPoint ) )
201   {
202     
203     mypLabel->setText( myText );
204     mypLabel->adjustSize( );
205     
206     QPoint pos = mypViewPort->getGLWidget()->mapToGlobal( myPoint );
207     
208     //mypLabel->show();
209     int cur_height = 24;
210     QCursor* aCursor = QApplication::overrideCursor();
211     if( aCursor )
212     {
213       const QBitmap* aBitmap = aCursor->bitmap();
214       if( aBitmap )
215         cur_height = aBitmap->height();
216     }
217     mypLabel->setGeometry( pos.x(), pos.y() + cur_height, mypLabel->width(), mypLabel->height() );
218     mypLabel->setPalette( QToolTip::palette() );
219
220     mypLabel->show();
221
222   }
223 }
224
225 /*!
226   Restarts timer
227 */
228 void GLViewer_ObjectTip::wakeup( int theTime )
229 {
230   if( mypTimer->isActive() )
231     mypTimer->stop();
232   mypTimer->start( theTime );
233 }