Salome HOME
0521ff19f05e210b101ff3d16b3fbaaa5cbcfb4f
[modules/gui.git] / src / GLViewer / GLViewer_ToolTip.cxx
1 //  Copyright (C) 2005 OPEN CASCADE
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 // File:      GLViewer_ToolTip.xx
23 // Created:   March, 2005
24
25 //#include "GLViewerAfx.h"
26 #include "GLViewer_Context.h"
27 #include "GLViewer_ToolTip.h"
28 #include "GLViewer_Viewer2d.h"
29 #include "GLViewer_ViewPort2d.h"
30
31 #include <qlabel.h>
32 #include <qtimer.h>
33 #include <qbitmap.h>
34 #include <qapplication.h>
35 #include <qtooltip.h>
36
37 /***************************************************************************
38 **  Class:   GLViewer_ToolTip
39 **  Descr:   ToolTip of GLViewer_Objects
40 **  Module:  GLViewer
41 **  Created: UI team, 25.03.05
42 ****************************************************************************/
43
44 //--------------------------------------------------------------------------
45 //Function: GLViewer_ToolTip()
46 //Description: constructor
47 //--------------------------------------------------------------------------
48 GLViewer_ObjectTip::GLViewer_ObjectTip( GLViewer_ViewPort2d* theParent )
49 :QObject(),
50  myText(),
51  myPoint( -1, -1 )
52 {
53   mypViewPort = theParent;
54   //mypLabel = NULL;
55   mypLabel = new QLabel( "Test", NULL, "ObjectTipText",
56              WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM );
57         mypLabel->setMargin( 1 );
58         mypLabel->setAutoMask( FALSE );
59   mypLabel->setFrameStyle( QFrame::Plain | QFrame::Box );
60   mypLabel->setLineWidth( 1 );
61   mypLabel->setAlignment( AlignAuto | AlignTop );
62   mypLabel->setIndent( 0 );
63   mypLabel->polish();
64   
65   //mypLabel->setPalette( QToolTip::palette() );
66
67   mypTimer = new QTimer( this );
68
69   connect( mypTimer, SIGNAL( timeout() ), this, SLOT( showTip() ) );
70 }
71
72 //--------------------------------------------------------------------------
73 //Function: GLViewer_ToolTip()
74 //Description: destructor
75 //--------------------------------------------------------------------------
76 GLViewer_ObjectTip::~GLViewer_ObjectTip()
77
78 //  delete mypRect;
79   if( mypLabel )
80     delete mypLabel;
81
82   //if( mypPoint )
83   //  delete mypPoint;
84
85   //if( mypTimer )
86   //  delete mypTimer;
87 }
88
89
90 //--------------------------------------------------------------------------
91 //Function: GLViewer_ToolTip()
92 //Description: destructor
93 //--------------------------------------------------------------------------
94 bool GLViewer_ObjectTip::maybeTip( const QPoint &p )
95 {
96
97
98   GLViewer_Context* aContext = ((GLViewer_Viewer2d*)mypViewPort->getViewFrame()->getViewer())->getGLContext();
99
100   /*if( !aContext->currentObjectIsChanged() )
101     return false;
102   else
103     return true;
104   if( myPoint.x() == -1 && myPoint.y() == -1 || aContext->currentObjectIsChanged())
105   {
106     myPoint = p;
107   }
108   else/if( abs(myPoint.y() - p.y()) < 16 )
109   {
110     return;
111   }
112   else // > 16
113   {
114     myPoint = p;
115   }
116 */  
117   GLViewer_Object* anObj = aContext->getCurrentObject();
118   if( anObj )
119   {
120     setText( anObj->getName() );
121     return true;
122   }
123
124   return false;
125   /*if( anObj )
126   {
127     //GLViewer_Rect* aRect = anObj->getRect();
128     //QRect aWinRect = mypViewPort->GLV2win( *aRect );
129     tip( QRect( p.x(), p.y(), 1, 1 ), anObj->getName() );
130     //QFontMetrics aFM( font() );    
131      //showTip( aWinRect, anObj->getName(), QRect( 0, 0, aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 ) );
132     //tip( aWinRect, anObj->getName(), aWinRect( aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 )  );
133   }
134 //  else
135 //    clear();
136     
137   //tip( QRect( 0, 0, mypViewPort->getGLWidget()->width(),mypViewPort->getGLWidget()->height() ) , "test Tool tip" );
138   */
139 }
140
141 bool GLViewer_ObjectTip::eventFilter( QObject* theObj, QEvent* e )
142 {
143   hideTipAndSleep();
144   switch( e->type() )
145   {
146     /*case QEvent::MouseButtonPress:
147     case QEvent::MouseButtonRelease:
148     case QEvent::MouseButtonDblClick:
149     case QEvent::KeyPress:
150     case QEvent::KeyRelease:
151             // input - turn off tool tip mode
152             hideTipAndSleep();
153             break;*/
154     case QEvent::MouseMove:
155       {
156         //hideTipAndSleep();
157         /*if( mypTimer->isActive() )
158         {
159           mypTimer->Stop();
160           wakeUp();
161         }*/
162         QWidget* aWidget = (QWidget*) theObj;
163         if( aWidget == mypViewPort->getGLWidget() )
164         {
165           wakeup();
166           QMouseEvent* m = (QMouseEvent *)e;
167           //if( !mypPoint )
168           //  mypPoint = new QPoint();
169
170           myPoint.setX( m->x() );
171           myPoint.setY( m->y() );
172         }
173       }
174   }
175   return false;
176 }
177
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 void GLViewer_ObjectTip::showTip()
195 {
196   if( maybeTip( myPoint ) )
197   {
198     
199     mypLabel->setText( myText );
200     mypLabel->adjustSize( );
201     
202     QPoint pos = mypViewPort->getGLWidget()->mapToGlobal( myPoint );
203     
204     //mypLabel->show();
205     int cur_height = 24;
206     QCursor* aCursor = QApplication::overrideCursor();
207     if( aCursor )
208     {
209       const QBitmap* aBitmap = aCursor->bitmap();
210       if( aBitmap )
211         cur_height = aBitmap->height();
212     }
213     mypLabel->setGeometry( pos.x(), pos.y() + cur_height, mypLabel->width(), mypLabel->height() );
214     mypLabel->setPalette( QToolTip::palette() );
215
216     mypLabel->show();
217
218   }
219 }
220
221 void GLViewer_ObjectTip::wakeup( int theTime )
222 {
223   if( mypTimer->isActive() )
224     mypTimer->stop();
225   mypTimer->start( theTime );
226 }