Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / Plot2d / Plot2d_ToolTip.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File:      Plot2d_ToolTip.cxx
20 // Author:    Alexandre SOLOVYOV
21
22 #include <Plot2d_ToolTip.h>
23 #include <Plot2d_ViewFrame.h>
24 #include <Plot2d_Curve.h>
25
26 #include <qfontmetrics.h>
27
28 #include <qwt_plot.h>
29 #include <qwt_plot_canvas.h>
30
31 const int maxDist = 3, tip_margin = 10;
32
33
34 Plot2d_ToolTip::Plot2d_ToolTip( Plot2d_ViewFrame* frame, Plot2d_Plot2d* plot )
35 : QtxToolTip( plot->canvas() ),
36   myFrame( frame ),
37   myPlot( plot )
38 {
39   connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
40            this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
41 }
42
43 Plot2d_ToolTip::~Plot2d_ToolTip()
44 {
45 }
46
47 void Plot2d_ToolTip::onToolTip( QPoint p, QString& str, QFont& f, QRect& txtRect, QRect& rect )
48 {
49   int curInd, pInd, dist;
50   double x, y;
51   curInd = myPlot->closestCurve( p.x(), p.y(), dist, x, y, pInd );
52
53   if( dist>maxDist )
54     return;
55   
56   Plot2d_Curve* c = myFrame->getCurves().find( curInd );
57   if( !c )
58     return;
59
60   str = c->text( pInd );
61   if( !str )
62     return;
63
64   QFontMetrics m( f );
65   QStringList lst = QStringList::split( "\n", str );
66   QStringList::const_iterator anIt = lst.begin(), aLast = lst.end();
67   int w = 0, h = 0;
68   for( ; anIt!=aLast; anIt++ )
69   {
70     if( h>0 )
71       h+= m.lineSpacing();
72
73     QRect r = m.boundingRect( *anIt );
74     if( r.width()>w )
75       w = r.width();
76     h+=r.height();
77   }
78
79   txtRect = QRect( p.x(), p.y(), w, h );
80   rect = txtRect;
81 }
82
83 bool Plot2d_ToolTip::eventFilter( QObject* o, QEvent* e )
84 {
85   bool res = QtxToolTip::eventFilter( o, e );
86   if( e && e->type() == QEvent::MouseMove )
87   {
88     QMouseEvent* me = ( QMouseEvent* )e;
89     if( me->state()==0 )
90       return true;
91   }
92   return res;
93 }