mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Refactor out findClosestPlotItem
This commit is contained in:
parent
ae23e2f252
commit
ee5c058634
@ -955,14 +955,20 @@ void RiuQwtPlotWidget::updateOverlayFrameLayout()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItemInSelection /*= false*/ )
|
||||
void RiuQwtPlotWidget::findClosestPlotItem( const QPoint& pos,
|
||||
QwtPlotItem** closestItem,
|
||||
int* closestCurvePoint,
|
||||
double* distanceFromClick ) const
|
||||
{
|
||||
QwtPlotItem* closestItem = nullptr;
|
||||
double distMin = DBL_MAX;
|
||||
int closestCurvePoint = -1;
|
||||
CAF_ASSERT( closestItem && closestCurvePoint && distanceFromClick );
|
||||
|
||||
// Force empty defaults
|
||||
*closestItem = nullptr;
|
||||
*closestCurvePoint = -1;
|
||||
*distanceFromClick = DBL_MAX;
|
||||
|
||||
const QwtPlotItemList& itmList = itemList();
|
||||
for ( QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); it++ )
|
||||
@ -972,11 +978,11 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem
|
||||
QwtPlotCurve* candidateCurve = static_cast<QwtPlotCurve*>( *it );
|
||||
double dist = DBL_MAX;
|
||||
int curvePoint = candidateCurve->closestPoint( pos, &dist );
|
||||
if ( dist < distMin )
|
||||
if ( dist < *distanceFromClick )
|
||||
{
|
||||
closestItem = candidateCurve;
|
||||
distMin = dist;
|
||||
closestCurvePoint = curvePoint;
|
||||
*closestItem = candidateCurve;
|
||||
*distanceFromClick = dist;
|
||||
*closestCurvePoint = curvePoint;
|
||||
}
|
||||
}
|
||||
else if ( ( *it )->rtti() == QwtPlotItem::Rtti_PlotShape )
|
||||
@ -985,8 +991,8 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem
|
||||
QPointF scalePos( invTransform( xBottom, pos.x() ), invTransform( yLeft, pos.y() ) );
|
||||
if ( shapeItem->shape().boundingRect().contains( scalePos ) )
|
||||
{
|
||||
closestItem = *it;
|
||||
distMin = 0.0;
|
||||
*closestItem = *it;
|
||||
*distanceFromClick = 0.0;
|
||||
}
|
||||
}
|
||||
else if ( ( *it )->rtti() == QwtPlotItem::Rtti_PlotBarChart )
|
||||
@ -1000,23 +1006,35 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem
|
||||
QPointF samplePoint = barChart->sample( (int)i );
|
||||
double dist = horizontal ? std::abs( samplePoint.x() - scalePos.y() )
|
||||
: std::abs( samplePoint.x() - scalePos.x() );
|
||||
if ( dist < distMin )
|
||||
if ( dist < *distanceFromClick )
|
||||
{
|
||||
closestItem = *it;
|
||||
closestCurvePoint = (int)i;
|
||||
distMin = dist;
|
||||
*closestItem = *it;
|
||||
*closestCurvePoint = (int)i;
|
||||
*distanceFromClick = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItemInSelection /*= false*/ )
|
||||
{
|
||||
QwtPlotItem* closestItem = nullptr;
|
||||
double distanceFromClick = DBL_MAX;
|
||||
int closestCurvePoint = -1;
|
||||
|
||||
findClosestPlotItem( pos, &closestItem, &closestCurvePoint, &distanceFromClick );
|
||||
|
||||
RiuPlotMainWindowTools::showPlotMainWindow();
|
||||
resetPlotItemHighlighting();
|
||||
if ( closestItem && distMin < 20 )
|
||||
if ( closestItem && distanceFromClick < 20 )
|
||||
{
|
||||
// TODO: highlight all selected curves
|
||||
highlightPlotItem( closestItem );
|
||||
emit plotItemSelected( closestItem, toggleItemInSelection, distMin < 10 ? closestCurvePoint : -1 );
|
||||
emit plotItemSelected( closestItem, toggleItemInSelection, distanceFromClick < 10 ? closestCurvePoint : -1 );
|
||||
|
||||
scheduleReplot();
|
||||
}
|
||||
|
@ -162,6 +162,11 @@ protected:
|
||||
virtual void endZoomOperations();
|
||||
|
||||
private:
|
||||
void findClosestPlotItem( const QPoint& pos,
|
||||
QwtPlotItem** closestItem,
|
||||
int* closestCurvePoint,
|
||||
double* distanceFromClick ) const;
|
||||
|
||||
void selectClosestPlotItem( const QPoint& pos, bool toggleItemInSelection = false );
|
||||
static int defaultMinimumWidth();
|
||||
void replot() override;
|
||||
|
Loading…
Reference in New Issue
Block a user