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