Back to index

salome-gui  6.5.0
Plot2d_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 //  File   : Plot2d_ToolTip.cxx
00023 //  Author : Alexandre SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
00024 
00025 #include "Plot2d_ToolTip.h"
00026 #include "Plot2d_ViewFrame.h"
00027 #include "Plot2d_Curve.h"
00028 
00029 #include <QFontMetrics>
00030 #include <QEvent>
00031 #include <QMouseEvent>
00032 
00033 #include <qwt_plot.h>
00034 #include <qwt_plot_canvas.h>
00035 
00036 const int maxDist = 3, tip_margin = 10;
00037 
00038 
00039 Plot2d_ToolTip::Plot2d_ToolTip( Plot2d_ViewFrame* frame )
00040 : QtxToolTip( frame->getPlotCanvas() ),
00041   myFrame( frame )
00042 {
00043   connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
00044            this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
00045 }
00046 
00047 Plot2d_ToolTip::~Plot2d_ToolTip()
00048 {
00049 }
00050 
00051 void Plot2d_ToolTip::onToolTip( QPoint p, QString& str, QFont& f, QRect& txtRect, QRect& rect )
00052 {
00053   int pInd;
00054   double dist;
00055 
00056   Plot2d_Curve* c = myFrame->getClosestCurve( p, dist, pInd );
00057   if( !c || dist>maxDist )
00058     return;
00059 
00060   str = c->text( pInd );
00061   if( str.isEmpty() )
00062     return;
00063 
00064   QFontMetrics m( f );
00065   QStringList lst = str.split( "\n", QString::SkipEmptyParts );
00066   QStringList::const_iterator anIt = lst.begin(), aLast = lst.end();
00067   int w = 0, h = 0;
00068   for( ; anIt!=aLast; anIt++ )
00069   {
00070     if( h>0 )
00071       h+= m.lineSpacing();
00072 
00073     QRect r = m.boundingRect( *anIt );
00074     if( r.width()>w )
00075       w = r.width();
00076     h+=r.height();
00077   }
00078 
00079   txtRect = QRect( p.x(), p.y(), w, h );
00080   rect = txtRect;
00081 }
00082 
00083 bool Plot2d_ToolTip::eventFilter( QObject* o, QEvent* e )
00084 {
00085   bool res = QtxToolTip::eventFilter( o, e );
00086   if( e && e->type() == QEvent::MouseMove )
00087   {
00088     QMouseEvent* me = ( QMouseEvent* )e;
00089     if( me->modifiers()==0 )
00090       return true;
00091   }
00092   return res;
00093 }