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