Salome HOME
updated copyright message
[modules/gui.git] / src / OCCViewer / OCCViewer_ToolTip.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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
20 // File:      OCCViewer_ToolTip.cxx
21 // Author:    Alexandre SOLOVYOV
22 //
23 #include "OCCViewer_ToolTip.h"
24 #include "OCCViewer_ViewWindow.h"
25 #include "OCCViewer_ViewPort3d.h"
26 #include "OCCViewer_ViewModel.h"
27
28 #include <SUIT_ViewManager.h>
29
30 /*!
31   Constructor
32
33   \param wnd - window where the tooltip should be assigned
34 */
35 OCCViewer_ToolTip::OCCViewer_ToolTip( OCCViewer_ViewWindow* wnd )
36 : QtxToolTip( wnd->getViewPort() ),
37   myWnd( wnd )
38 {
39   connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
40            this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
41 }
42
43 /*!
44   Destructor
45  */
46 OCCViewer_ToolTip::~OCCViewer_ToolTip()
47 {
48 }
49
50 /*!
51   \return font of the tooltip
52  */
53 QFont OCCViewer_ToolTip::font() const
54 {
55   return myFont;
56 }
57
58 /*!
59   Change font of the tooltip
60
61   \param f - new font
62  */
63 void OCCViewer_ToolTip::setFont( const QFont& f )
64 {
65   myFont = f;
66 }
67
68 /*!
69   Tooltip handler
70
71   \param p - current point
72   \param str - returned tooltip text
73   \param f - returned tooltip font
74   \param txtRect - returned tooltip text rectangle
75   \param rect - returned tooltip rectangle
76  */
77 void OCCViewer_ToolTip::onToolTip( QPoint p, QString& str, QFont& /*f*/, QRect& txtRect, QRect& rect )
78 {
79   OCCViewer_Viewer* v = dynamic_cast<OCCViewer_Viewer*>( myWnd->getViewManager()->getViewModel() );
80   Handle( AIS_InteractiveContext ) aCont = v->getAISContext();
81   if( aCont.IsNull() )
82     return;
83
84   QString txt;
85   Handle( SelectMgr_EntityOwner ) owner = aCont->DetectedOwner();
86   if( !owner.IsNull() )
87     emit toolTipFor( owner, txt );
88
89   Handle(AIS_InteractiveObject) obj = aCont->DetectedInteractive();
90   if( txt.isEmpty() && !obj.IsNull() )
91     emit toolTipFor( obj, txt );
92
93   if( txt.isEmpty() )
94     return;
95
96   str = txt;
97   QFontMetrics m( myFont );
98   int w = m.width( str ), h = m.height();
99
100   txtRect = QRect( p.x()+4, p.y()-h, w, h );
101   rect = txtRect;
102 }