Back to index

salome-gui  6.5.0
GLViewer_ToolTip.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  Author : OPEN CASCADE
00024 // File:      GLViewer_ToolTip.xx
00025 // Created:   March, 2005
00026 //#include "GLViewerAfx.h"
00027 //
00028 #include "GLViewer_Context.h"
00029 #include "GLViewer_ToolTip.h"
00030 #include "GLViewer_Viewer2d.h"
00031 #include "GLViewer_ViewPort2d.h"
00032 #include "GLViewer_ViewFrame.h"
00033 
00034 #include <QLabel>
00035 #include <QTimer>
00036 #include <QBitmap>
00037 #include <QApplication>
00038 #include <QToolTip>
00039 #include <QMouseEvent>
00040 
00044 GLViewer_ObjectTip::GLViewer_ObjectTip( GLViewer_ViewPort2d* theParent )
00045 :QObject(),
00046  myText(),
00047  myPoint( -1, -1 )
00048 {
00049   mypViewPort = theParent;
00050   //mypLabel = NULL;
00051   mypLabel = new QLabel( "Test", NULL, 
00052                          Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool | Qt::X11BypassWindowManagerHint );
00053   mypLabel->setObjectName("ObjectTipText");
00054   mypLabel->setMargin( 1 );
00055   //mypLabel->setAutoMask( FALSE );
00056   mypLabel->setFrameStyle( QFrame::Plain | QFrame::Box );
00057   mypLabel->setLineWidth( 1 );
00058   mypLabel->setAlignment( Qt::AlignAbsolute | Qt::AlignTop );
00059   mypLabel->setIndent( 0 );
00060   mypLabel->ensurePolished();
00061   
00062   //mypLabel->setPalette( QToolTip::palette() );
00063 
00064   mypTimer = new QTimer( this );
00065 
00066   connect( mypTimer, SIGNAL( timeout() ), this, SLOT( showTip() ) );
00067 }
00068 
00072 GLViewer_ObjectTip::~GLViewer_ObjectTip()
00073 { 
00074 //  delete mypRect;
00075   if( mypLabel )
00076     delete mypLabel;
00077 
00078   //if( mypPoint )
00079   //  delete mypPoint;
00080 
00081   //if( mypTimer )
00082   //  delete mypTimer;
00083 }
00084 
00085 
00090 bool GLViewer_ObjectTip::maybeTip( const QPoint &p )
00091 {
00092 
00093 
00094   GLViewer_Context* aContext = ((GLViewer_Viewer2d*)mypViewPort->getViewFrame()->getViewer())->getGLContext();
00095 
00096   /*if( !aContext->currentObjectIsChanged() )
00097     return false;
00098   else
00099     return true;
00100   if( myPoint.x() == -1 && myPoint.y() == -1 || aContext->currentObjectIsChanged())
00101   {
00102     myPoint = p;
00103   }
00104   else/if( abs(myPoint.y() - p.y()) < 16 )
00105   {
00106     return;
00107   }
00108   else // > 16
00109   {
00110     myPoint = p;
00111   }
00112 */  
00113   GLViewer_Object* anObj = aContext->getCurrentObject();
00114   if( anObj )
00115   {
00116     setText( anObj->getName() );
00117     return true;
00118   }
00119 
00120   return false;
00121   /*if( anObj )
00122   {
00123     //GLViewer_Rect* aRect = anObj->getRect();
00124     //QRect aWinRect = mypViewPort->GLV2win( *aRect );
00125     tip( QRect( p.x(), p.y(), 1, 1 ), anObj->getName() );
00126     //QFontMetrics aFM( font() );    
00127      //showTip( aWinRect, anObj->getName(), QRect( 0, 0, aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 ) );
00128     //tip( aWinRect, anObj->getName(), aWinRect( aFM.width( anObj->getName() + "  " ), aFM.height()*1.5 )  );
00129   }
00130 //  else
00131 //    clear();
00132     
00133   //tip( QRect( 0, 0, mypViewPort->getGLWidget()->width(),mypViewPort->getGLWidget()->height() ) , "test Tool tip" );
00134   */
00135 }
00136 
00140 bool GLViewer_ObjectTip::eventFilter( QObject* theObj, QEvent* e )
00141 {
00142   hideTipAndSleep();
00143   switch( e->type() )
00144   {
00145     /*case QEvent::MouseButtonPress:
00146     case QEvent::MouseButtonRelease:
00147     case QEvent::MouseButtonDblClick:
00148     case QEvent::KeyPress:
00149     case QEvent::KeyRelease:
00150             // input - turn off tool tip mode
00151             hideTipAndSleep();
00152             break;*/
00153     case QEvent::MouseMove:
00154       {
00155         //hideTipAndSleep();
00156         /*if( mypTimer->isActive() )
00157         {
00158           mypTimer->Stop();
00159           wakeUp();
00160         }*/
00161         QWidget* aWidget = (QWidget*) theObj;
00162         if( aWidget == mypViewPort->getGLWidget() )
00163         {
00164           wakeup();
00165           QMouseEvent* m = (QMouseEvent *)e;
00166           //if( !mypPoint )
00167           //  mypPoint = new QPoint();
00168 
00169           myPoint.setX( m->x() );
00170           myPoint.setY( m->y() );
00171         }
00172       }
00173   }
00174   return false;
00175 }
00176 
00180 void GLViewer_ObjectTip::hideTipAndSleep()
00181 {
00182   //if( mypPoint )
00183   //  delete mypPoint;
00184   myPoint.setX(-1);
00185   myPoint.setY(-1);
00186 
00187   if( mypLabel )
00188   {
00189     mypLabel->hide();
00190     //delete mypLabel;
00191   }
00192   mypTimer->stop();
00193 }
00194 
00198 void GLViewer_ObjectTip::showTip()
00199 {
00200   if( maybeTip( myPoint ) )
00201   {
00202     
00203     mypLabel->setText( myText );
00204     mypLabel->adjustSize( );
00205     
00206     QPoint pos = mypViewPort->getGLWidget()->mapToGlobal( myPoint );
00207     
00208     //mypLabel->show();
00209     int cur_height = 24;
00210     QCursor* aCursor = QApplication::overrideCursor();
00211     if( aCursor )
00212     {
00213       const QBitmap* aBitmap = aCursor->bitmap();
00214       if( aBitmap )
00215         cur_height = aBitmap->height();
00216     }
00217     mypLabel->setGeometry( pos.x(), pos.y() + cur_height, mypLabel->width(), mypLabel->height() );
00218     mypLabel->setPalette( QToolTip::palette() );
00219 
00220     mypLabel->show();
00221 
00222   }
00223 }
00224 
00228 void GLViewer_ObjectTip::wakeup( int theTime )
00229 {
00230   if( mypTimer->isActive() )
00231     mypTimer->stop();
00232   mypTimer->start( theTime );
00233 }