]> SALOME platform Git repositories - modules/gui.git/blob - src/Plot2d/Plot2d_ToolTip.cxx
Salome HOME
ba44643f7dae1edafb1e1767081c05f6199f4966
[modules/gui.git] / src / Plot2d / Plot2d_ToolTip.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:      Plot2d_ToolTip.cxx
23 // Author:    Alexandre SOLOVYOV
24 //
25 #include "Plot2d_ToolTip.h"
26 #include "Plot2d_ViewFrame.h"
27 #include "Plot2d_Curve.h"
28
29 #include <QFontMetrics>
30 #include <QEvent>
31 #include <QMouseEvent>
32
33 #include <qwt_plot.h>
34 #include <qwt_plot_canvas.h>
35
36 const int maxDist = 3, tip_margin = 10;
37
38
39 Plot2d_ToolTip::Plot2d_ToolTip( Plot2d_ViewFrame* frame, Plot2d_Plot2d* plot )
40 : QtxToolTip( plot->canvas() ),
41   myFrame( frame ),
42   myPlot( plot )
43 {
44   connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
45            this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
46 }
47
48 Plot2d_ToolTip::~Plot2d_ToolTip()
49 {
50 }
51
52 void Plot2d_ToolTip::onToolTip( QPoint p, QString& str, QFont& f, QRect& txtRect, QRect& rect )
53 {
54   int pInd;
55   double dist;
56
57   Plot2d_Curve* c = myPlot->getClosestCurve( p, dist, pInd );
58   if( !c || dist>maxDist )
59     return;
60
61   str = c->text( pInd );
62   if( str.isEmpty() )
63     return;
64
65   QFontMetrics m( f );
66   QStringList lst = str.split( "\n", QString::SkipEmptyParts );
67   QStringList::const_iterator anIt = lst.begin(), aLast = lst.end();
68   int w = 0, h = 0;
69   for( ; anIt!=aLast; anIt++ )
70   {
71     if( h>0 )
72       h+= m.lineSpacing();
73
74     QRect r = m.boundingRect( *anIt );
75     if( r.width()>w )
76       w = r.width();
77     h+=r.height();
78   }
79
80   txtRect = QRect( p.x(), p.y(), w, h );
81   rect = txtRect;
82 }
83
84 bool Plot2d_ToolTip::eventFilter( QObject* o, QEvent* e )
85 {
86   bool res = QtxToolTip::eventFilter( o, e );
87   if( e && e->type() == QEvent::MouseMove )
88   {
89     QMouseEvent* me = ( QMouseEvent* )e;
90     if( me->modifiers()==0 )
91       return true;
92   }
93   return res;
94 }