From ba08d6e1b798d07c9ccd34ced898774868f5929b Mon Sep 17 00:00:00 2001 From: asl Date: Tue, 22 Aug 2006 05:21:30 +0000 Subject: [PATCH] PAL13244 - tooltips over curve points --- src/Plot2d/Makefile.in | 9 ++-- src/Plot2d/Plot2d_Curve.cxx | 41 ++++++++++++--- src/Plot2d/Plot2d_Curve.h | 10 ++-- src/Plot2d/Plot2d_ToolTip.cxx | 93 +++++++++++++++++++++++++++++++++ src/Plot2d/Plot2d_ToolTip.h | 50 ++++++++++++++++++ src/Plot2d/Plot2d_ViewFrame.cxx | 3 ++ 6 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 src/Plot2d/Plot2d_ToolTip.cxx create mode 100644 src/Plot2d/Plot2d_ToolTip.h diff --git a/src/Plot2d/Makefile.in b/src/Plot2d/Makefile.in index 647c1b84d..73af7206c 100755 --- a/src/Plot2d/Makefile.in +++ b/src/Plot2d/Makefile.in @@ -39,7 +39,8 @@ EXPORT_HEADERS= Plot2d.h \ Plot2d_ViewManager.h \ Plot2d_ViewModel.h \ Plot2d_ViewWindow.h \ - Plot2d_SetupCurveDlg.h + Plot2d_SetupCurveDlg.h \ + Plot2d_ToolTip.h # .po files to transform in .qm PO_FILES = Plot2d_images.po \ @@ -55,7 +56,8 @@ LIB_SRC= Plot2d_Curve.cxx \ Plot2d_ViewManager.cxx \ Plot2d_ViewModel.cxx \ Plot2d_ViewWindow.cxx \ - Plot2d_SetupCurveDlg.cxx + Plot2d_SetupCurveDlg.cxx \ + Plot2d_ToolTip.cxx LIB_MOC = \ Plot2d_FitDataDlg.h \ @@ -64,7 +66,8 @@ LIB_MOC = \ Plot2d_ViewManager.h \ Plot2d_ViewModel.h \ Plot2d_ViewWindow.h \ - Plot2d_SetupCurveDlg.h + Plot2d_SetupCurveDlg.h \ + Plot2d_ToolTip.h RESOURCES_FILES = \ plot2d_clone.png \ diff --git a/src/Plot2d/Plot2d_Curve.cxx b/src/Plot2d/Plot2d_Curve.cxx index c86db8f32..bec633fdc 100755 --- a/src/Plot2d/Plot2d_Curve.cxx +++ b/src/Plot2d/Plot2d_Curve.cxx @@ -147,22 +147,24 @@ QString Plot2d_Curve::getVerUnits() const /*! Adds one point for curve. */ -void Plot2d_Curve::addPoint(double theX, double theY) +void Plot2d_Curve::addPoint(double theX, double theY, const QString& txt ) { Plot2d_Point aPoint; aPoint.x = theX; aPoint.y = theY; + aPoint.text = txt; myPoints.append(aPoint); } /*! Insert one point for curve on some position. */ -void Plot2d_Curve::insertPoint(int thePos, double theX, double theY) +void Plot2d_Curve::insertPoint(int thePos, double theX, double theY, const QString& txt) { Plot2d_Point aPoint; aPoint.x = theX; aPoint.y = theY; + aPoint.text = txt; QValueList::iterator aIt; int aCurrent = 0; @@ -211,10 +213,12 @@ pointList Plot2d_Curve::getPointList() const /*! Sets curve's data. */ -void Plot2d_Curve::setData( const double* hData, const double* vData, long size ) +void Plot2d_Curve::setData( const double* hData, const double* vData, long size, const QStringList& lst ) { clearAllPoints(); - for(long i = 0; i < size; i++) addPoint(hData[i], vData[i]); + QStringList::const_iterator anIt = lst.begin(), aLast = lst.end(); + for( long i = 0; i < size; i++, anIt++ ) + addPoint( hData[i], vData[i], anIt==aLast ? QString::null : *anIt ); } /*! @@ -364,7 +368,7 @@ double Plot2d_Curve::getMinX() const { QValueList::const_iterator aIt; double aMinX = 1e150; - int aCurrent = 0; + //int aCurrent = 0; for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) { if ( (*aIt).x < aMinX ) aMinX = (*aIt).x; @@ -379,10 +383,35 @@ double Plot2d_Curve::getMinY() const { QValueList::const_iterator aIt; double aMinY = 1e150; - int aCurrent = 0; + //int aCurrent = 0; for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) { if ( (*aIt).y < aMinY ) aMinY = (*aIt).y; } return aMinY; } + +/*! + Changes text assigned to point of curve + \param ind -- index of point + \param txt -- new text +*/ +void Plot2d_Curve::setText( const int ind, const QString& txt ) +{ + if( ind<0 || ind>=myPoints.count() ) + return; + + myPoints[ind].text = txt; +} + +/*! + \return text assigned to point + \param ind -- index of point +*/ +QString Plot2d_Curve::text( const int ind ) const +{ + if( ind<0 || ind>=myPoints.count() ) + return QString::null; + else + return myPoints[ind].text; +} diff --git a/src/Plot2d/Plot2d_Curve.h b/src/Plot2d/Plot2d_Curve.h index 1fa244aa4..bd3be97bd 100755 --- a/src/Plot2d/Plot2d_Curve.h +++ b/src/Plot2d/Plot2d_Curve.h @@ -30,6 +30,7 @@ typedef struct { double x; double y; + QString text; } Plot2d_Point; typedef QValueList pointList; @@ -57,16 +58,19 @@ public: QString getHorUnits() const; void setVerUnits( const QString& units ); QString getVerUnits() const; - void addPoint(double theX, double theY); - void insertPoint(int thePos, double theX, double theY); + void addPoint(double theX, double theY, const QString& = QString::null ); + void insertPoint(int thePos, double theX, double theY, const QString& = QString::null ); void deletePoint(int thePos); void clearAllPoints(); pointList getPointList() const; - void setData( const double* hData, const double* vData, long size ); + void setData( const double* hData, const double* vData, long size, const QStringList& = QStringList() ); double* horData() const; double* verData() const; + void setText( const int, const QString& ); + QString text( const int ) const; + int nbPoints() const; bool isEmpty() const; diff --git a/src/Plot2d/Plot2d_ToolTip.cxx b/src/Plot2d/Plot2d_ToolTip.cxx new file mode 100644 index 000000000..3cdb1ba8a --- /dev/null +++ b/src/Plot2d/Plot2d_ToolTip.cxx @@ -0,0 +1,93 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: Plot2d_ToolTip.cxx +// Author: Alexandre SOLOVYOV + +#include +#include +#include + +#include + +#include +#include + +const int maxDist = 3, tip_margin = 10; + + +Plot2d_ToolTip::Plot2d_ToolTip( Plot2d_ViewFrame* frame, Plot2d_Plot2d* plot ) +: QtxToolTip( plot->canvas() ), + myFrame( frame ), + myPlot( plot ) +{ + connect( this, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ), + this, SLOT( onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) ); +} + +Plot2d_ToolTip::~Plot2d_ToolTip() +{ +} + +void Plot2d_ToolTip::onToolTip( QPoint p, QString& str, QFont& f, QRect& txtRect, QRect& rect ) +{ + int curInd, pInd, dist; + double x, y; + curInd = myPlot->closestCurve( p.x(), p.y(), dist, x, y, pInd ); + + if( dist>maxDist ) + return; + + Plot2d_Curve* c = myFrame->getCurves().find( curInd ); + if( !c ) + return; + + str = c->text( pInd ); + if( !str ) + return; + + QFontMetrics m( f ); + QStringList lst = QStringList::split( "\n", str ); + QStringList::const_iterator anIt = lst.begin(), aLast = lst.end(); + int w = 0, h = 0; + for( ; anIt!=aLast; anIt++ ) + { + if( h>0 ) + h+= m.lineSpacing(); + + QRect r = m.boundingRect( *anIt ); + if( r.width()>w ) + w = r.width(); + h+=r.height(); + } + + txtRect = QRect( p.x(), p.y(), w, h ); + rect = txtRect; +} + +bool Plot2d_ToolTip::eventFilter( QObject* o, QEvent* e ) +{ + bool res = QtxToolTip::eventFilter( o, e ); + if( e && e->type() == QEvent::MouseMove ) + { + QMouseEvent* me = ( QMouseEvent* )e; + if( me->state()==0 ) + return true; + } + return res; +} diff --git a/src/Plot2d/Plot2d_ToolTip.h b/src/Plot2d/Plot2d_ToolTip.h new file mode 100644 index 000000000..a26c46a83 --- /dev/null +++ b/src/Plot2d/Plot2d_ToolTip.h @@ -0,0 +1,50 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: Plot2d_ToolTip.h +// Author: Alexandre SOLOVYOV + +#ifndef PLOT2D_TOOLTIP_H +#define PLOT2D_TOOLTIP_H + +#include +#include + +class Plot2d_ViewFrame; +class Plot2d_Plot2d; + +class PLOT2D_EXPORT Plot2d_ToolTip : public QtxToolTip +{ + Q_OBJECT + +public: + Plot2d_ToolTip( Plot2d_ViewFrame*, Plot2d_Plot2d* ); + virtual ~Plot2d_ToolTip(); + +public slots: + void onToolTip( QPoint, QString&, QFont&, QRect&, QRect& ); + +protected: + virtual bool eventFilter( QObject*, QEvent* ); + +private: + Plot2d_ViewFrame* myFrame; + Plot2d_Plot2d* myPlot; +}; + +#endif diff --git a/src/Plot2d/Plot2d_ViewFrame.cxx b/src/Plot2d/Plot2d_ViewFrame.cxx index 35b590480..00ba07afa 100755 --- a/src/Plot2d/Plot2d_ViewFrame.cxx +++ b/src/Plot2d/Plot2d_ViewFrame.cxx @@ -23,6 +23,7 @@ #include "Plot2d_FitDataDlg.h" #include "Plot2d_ViewWindow.h" #include "Plot2d_SetupViewDlg.h" +#include "Plot2d_ToolTip.h" #include "SUIT_Tools.h" #include "SUIT_Session.h" @@ -152,6 +153,8 @@ Plot2d_ViewFrame::Plot2d_ViewFrame( QWidget* parent, const QString& title ) /* Plot 2d View */ QVBoxLayout* aLayout = new QVBoxLayout( this ); myPlot = new Plot2d_Plot2d( this ); + new Plot2d_ToolTip( this, myPlot ); + aLayout->addWidget( myPlot ); // createActions(); -- 2.39.2