Salome HOME
updated copyright message
[modules/gui.git] / src / Plot2d / Plot2d_ToolTip.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
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 )
40 : QtxToolTip( frame->getPlotCanvas() ),
41   myFrame( frame )
42 {
43   connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
44            this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
45 }
46
47 Plot2d_ToolTip::~Plot2d_ToolTip()
48 {
49 }
50
51 void Plot2d_ToolTip::onToolTip( QPoint p, QString& str, QFont& f, QRect& txtRect, QRect& rect )
52 {
53   int pInd;
54   double dist;
55
56   Plot2d_Curve* c = myFrame->getClosestCurve( p, dist, pInd );
57   if( !c || dist>maxDist )
58     return;
59
60   str = c->text( pInd );
61   if( str.isEmpty() )
62     return;
63
64   QFontMetrics m( f );
65   QStringList lst = str.split( "\n", QString::SkipEmptyParts );
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->modifiers()==0 )
90       return true;
91   }
92   return res;
93 }