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