ResInsight/ApplicationCode/UserInterface/RiuWellLogTrack.cpp

400 lines
15 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////////////////////////
2017-02-13 01:37:31 -06:00
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- Ceetron Solutions AS
//
2017-02-13 01:37:31 -06:00
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2017-02-13 01:37:31 -06:00
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
2017-02-13 01:37:31 -06:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiuWellLogTrack.h"
#include "RiaApplication.h"
#include "RimWellLogCurve.h"
2017-02-13 01:37:31 -06:00
#include "RimWellLogPlot.h"
#include "RimWellLogTrack.h"
#include "RiuPlotMainWindowTools.h"
#include "RiuQwtCurvePointTracker.h"
#include "RiuQwtPlotTools.h"
2017-02-13 01:37:31 -06:00
2018-06-28 01:23:16 -05:00
#include "RiuQwtLinearScaleEngine.h"
2017-02-13 01:37:31 -06:00
#include "qwt_legend.h"
#include "qwt_plot_curve.h"
#include "qwt_plot_grid.h"
#include "qwt_plot_layout.h"
#include "qwt_plot_marker.h"
#include "qwt_plot_picker.h"
#include "qwt_scale_draw.h"
#include "qwt_symbol.h"
#include "qwt_text.h"
#include <QFont>
#include <QMouseEvent>
#include <QScrollArea>
#include <QWheelEvent>
#include <cfloat>
2017-02-13 01:37:31 -06:00
#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1
#define RIU_SCROLLWHEEL_PANFACTOR 0.1
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
RiuWellLogTrack::RiuWellLogTrack( RimWellLogTrack* plotTrackDefinition, QWidget* parent )
: QwtPlot( parent )
2017-02-13 01:37:31 -06:00
{
Q_ASSERT( plotTrackDefinition );
2017-02-13 01:37:31 -06:00
m_plotTrackDefinition = plotTrackDefinition;
2017-02-13 01:37:31 -06:00
setDefaults();
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
RiuWellLogTrack::~RiuWellLogTrack() {}
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setDefaults()
{
RiuQwtPlotTools::setCommonPlotBehaviour( this );
2017-02-13 01:37:31 -06:00
enableAxis( QwtPlot::xTop, true );
enableAxis( QwtPlot::yLeft, true );
enableAxis( QwtPlot::xBottom, false );
enableAxis( QwtPlot::yRight, false );
2017-02-13 01:37:31 -06:00
axisScaleEngine( QwtPlot::yLeft )->setAttribute( QwtScaleEngine::Inverted, true );
2017-02-13 01:37:31 -06:00
// Align the canvas with the actual min and max values of the curves
axisScaleEngine( QwtPlot::xTop )->setAttribute( QwtScaleEngine::Floating, true );
axisScaleEngine( QwtPlot::yLeft )->setAttribute( QwtScaleEngine::Floating, true );
setAxisScale( QwtPlot::yLeft, 1000, 0 );
setXRange( 0, 100 );
axisScaleDraw( QwtPlot::xTop )->setMinimumExtent( axisExtent( QwtPlot::xTop ) );
setMinimumWidth( defaultMinimumWidth() );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setDepthZoom( double minDepth, double maxDepth )
2017-02-13 01:37:31 -06:00
{
// Note: Y-axis is inverted
setAxisScale( QwtPlot::yLeft, maxDepth, minDepth );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setXRange( double min, double max, QwtPlot::Axis axis )
2017-02-13 01:37:31 -06:00
{
setAxisScale( axis, min, max );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setDepthTitle( const QString& title )
2017-02-13 01:37:31 -06:00
{
QwtText axisTitleY = axisTitle( QwtPlot::yLeft );
if ( title != axisTitleY.text() )
2018-07-06 02:17:16 -05:00
{
axisTitleY.setText( title );
setAxisTitle( QwtPlot::yLeft, axisTitleY );
setMinimumWidth( defaultMinimumWidth() + axisExtent( QwtPlot::yLeft ) );
2018-07-06 02:17:16 -05:00
}
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setXTitle( const QString& title )
2017-02-13 01:37:31 -06:00
{
QwtText axisTitleX = axisTitle( QwtPlot::xTop );
if ( title != axisTitleX.text() )
2018-07-06 02:17:16 -05:00
{
axisTitleX.setText( title );
setAxisTitle( QwtPlot::xTop, axisTitleX );
2018-07-06 02:17:16 -05:00
}
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
bool RiuWellLogTrack::eventFilter( QObject* watched, QEvent* event )
2017-02-13 01:37:31 -06:00
{
if ( watched == canvas() )
2017-02-13 01:37:31 -06:00
{
QWheelEvent* wheelEvent = dynamic_cast<QWheelEvent*>( event );
if ( wheelEvent )
2017-02-13 01:37:31 -06:00
{
if ( !m_plotTrackDefinition )
2017-02-13 01:37:31 -06:00
{
return QwtPlot::eventFilter( watched, event );
2017-02-13 01:37:31 -06:00
}
RimWellLogPlot* plotDefinition;
m_plotTrackDefinition->firstAncestorOrThisOfType( plotDefinition );
if ( !plotDefinition )
2017-02-13 01:37:31 -06:00
{
return QwtPlot::eventFilter( watched, event );
2017-02-13 01:37:31 -06:00
}
if ( wheelEvent->modifiers() & Qt::ControlModifier )
2017-02-13 01:37:31 -06:00
{
QwtScaleMap scaleMap = canvasMap( QwtPlot::yLeft );
double zoomCenter = scaleMap.invTransform( wheelEvent->pos().y() );
2017-02-13 01:37:31 -06:00
if ( wheelEvent->delta() > 0 )
2017-02-13 01:37:31 -06:00
{
plotDefinition->setDepthZoomByFactorAndCenter( RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter );
2017-02-13 01:37:31 -06:00
}
else
{
plotDefinition->setDepthZoomByFactorAndCenter( 1.0 / RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter );
2017-02-13 01:37:31 -06:00
}
}
else
{
plotDefinition->panDepth( wheelEvent->delta() < 0 ? RIU_SCROLLWHEEL_PANFACTOR
: -RIU_SCROLLWHEEL_PANFACTOR );
2017-02-13 01:37:31 -06:00
}
event->accept();
return true;
}
else
{
QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>( event );
if ( mouseEvent )
2017-02-13 01:37:31 -06:00
{
if ( mouseEvent->button() == Qt::LeftButton && mouseEvent->type() == QMouseEvent::MouseButtonRelease )
2017-02-13 01:37:31 -06:00
{
selectClosestCurve( mouseEvent->pos() );
2017-02-13 01:37:31 -06:00
}
}
}
}
return QwtPlot::eventFilter( watched, event );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::selectClosestCurve( const QPoint& pos )
2017-02-13 01:37:31 -06:00
{
2018-02-18 11:56:43 -06:00
QwtPlotCurve* closestCurve = nullptr;
double distMin = DBL_MAX;
2017-02-13 01:37:31 -06:00
const QwtPlotItemList& itmList = itemList();
for ( QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); it++ )
2017-02-13 01:37:31 -06:00
{
if ( ( *it )->rtti() == QwtPlotItem::Rtti_PlotCurve )
2017-02-13 01:37:31 -06:00
{
QwtPlotCurve* candidateCurve = static_cast<QwtPlotCurve*>( *it );
double dist = DBL_MAX;
candidateCurve->closestPoint( pos, &dist );
if ( dist < distMin )
2017-02-13 01:37:31 -06:00
{
closestCurve = candidateCurve;
distMin = dist;
2017-02-13 01:37:31 -06:00
}
}
}
if ( closestCurve && distMin < 20 )
2017-02-13 01:37:31 -06:00
{
RimWellLogCurve* selectedCurve = m_plotTrackDefinition->curveDefinitionFromCurve( closestCurve );
if ( selectedCurve )
2017-02-13 01:37:31 -06:00
{
RiuPlotMainWindowTools::showPlotMainWindow();
RiuPlotMainWindowTools::selectAsCurrentItem( selectedCurve );
return;
2017-02-13 01:37:31 -06:00
}
}
RiuPlotMainWindowTools::showPlotMainWindow();
RiuPlotMainWindowTools::selectAsCurrentItem( m_plotTrackDefinition );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiuWellLogTrack::defaultMinimumWidth()
{
return 80;
}
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
QSize RiuWellLogTrack::sizeHint() const
{
return QSize( 0, 0 );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
QSize RiuWellLogTrack::minimumSizeHint() const
{
return QSize( 0, 0 );
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
2017-02-13 01:37:31 -06:00
//--------------------------------------------------------------------------------------------------
bool RiuWellLogTrack::isRimTrackVisible()
{
if ( m_plotTrackDefinition )
2017-02-13 01:37:31 -06:00
{
return m_plotTrackDefinition->isVisible();
}
return false;
2017-02-13 01:37:31 -06:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2019-10-03 08:45:20 -05:00
void RiuWellLogTrack::enableDepthAxisLabelsAndTicks( bool enable )
{
this->axisScaleDraw( QwtPlot::yLeft )->enableComponent( QwtAbstractScaleDraw::Ticks, enable );
this->axisScaleDraw( QwtPlot::yLeft )->enableComponent( QwtAbstractScaleDraw::Labels, enable );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiuWellLogTrack::widthScaleFactor() const
{
if ( m_plotTrackDefinition )
{
return m_plotTrackDefinition->widthScaleFactor();
}
return 1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::enableXGridLines( bool majorGridLines, bool minorGridLines )
{
QwtPlotItemList plotItems = this->itemList( QwtPlotItem::Rtti_PlotGrid );
for ( QwtPlotItem* plotItem : plotItems )
{
QwtPlotGrid* grid = static_cast<QwtPlotGrid*>( plotItem );
grid->setXAxis( QwtPlot::xTop );
grid->enableX( majorGridLines );
grid->enableXMin( minorGridLines );
grid->setMajorPen( Qt::lightGray, 1.0, Qt::SolidLine );
grid->setMinorPen( Qt::lightGray, 1.0, Qt::DashLine );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::enableDepthGridLines( bool majorGridLines, bool minorGridLines )
{
QwtPlotItemList plotItems = this->itemList( QwtPlotItem::Rtti_PlotGrid );
for ( QwtPlotItem* plotItem : plotItems )
{
QwtPlotGrid* grid = static_cast<QwtPlotGrid*>( plotItem );
grid->setYAxis( QwtPlot::yLeft );
grid->enableY( majorGridLines );
grid->enableYMin( minorGridLines );
grid->setMajorPen( Qt::lightGray, 1.0, Qt::SolidLine );
grid->setMinorPen( Qt::lightGray, 1.0, Qt::DashLine );
}
}
2018-06-28 01:23:16 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setMajorAndMinorTickIntervals( double majorTickInterval, double minorTickInterval )
2018-06-28 01:23:16 -05:00
{
RiuQwtLinearScaleEngine* scaleEngine = dynamic_cast<RiuQwtLinearScaleEngine*>(
this->axisScaleEngine( QwtPlot::xTop ) );
if ( scaleEngine )
2018-06-28 01:23:16 -05:00
{
QwtInterval currentRange = this->axisInterval( QwtPlot::xTop );
QwtScaleDiv scaleDiv = scaleEngine->divideScaleWithExplicitIntervals( currentRange.minValue(),
currentRange.maxValue(),
majorTickInterval,
minorTickInterval );
this->setAxisScaleDiv( QwtPlot::xTop, scaleDiv );
2018-06-28 01:23:16 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogTrack::setAutoTickIntervalCounts( int maxMajorTickIntervalCount, int maxMinorTickIntervalCount )
2018-06-28 01:23:16 -05:00
{
this->setAxisMaxMajor( QwtPlot::xTop, maxMajorTickIntervalCount );
this->setAxisMaxMinor( QwtPlot::xTop, maxMinorTickIntervalCount );
// Reapply axis limits to force Qwt to use the tick settings.
QwtInterval currentRange = this->axisInterval( QwtPlot::xTop );
this->setXRange( currentRange.minValue(), currentRange.maxValue() );
2018-06-28 01:23:16 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiuWellLogTrack::getCurrentMajorTickInterval() const
{
QwtScaleDiv scaleDiv = this->axisScaleDiv( QwtPlot::xTop );
QList<double> majorTicks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
if ( majorTicks.size() < 2 ) return 0.0;
2018-06-28 01:23:16 -05:00
return majorTicks.at( 1 ) - majorTicks.at( 0 );
2018-06-28 01:23:16 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiuWellLogTrack::getCurrentMinorTickInterval() const
{
QwtScaleDiv scaleDiv = this->axisScaleDiv( QwtPlot::xTop );
QList<double> minorTicks = scaleDiv.ticks( QwtScaleDiv::MinorTick );
if ( minorTicks.size() < 2 ) return 0.0;
2018-06-28 01:23:16 -05:00
return minorTicks.at( 1 ) - minorTicks.at( 0 );
2018-06-28 01:23:16 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiuWellLogTrack::axisExtent( QwtPlot::Axis axis ) const
{
QFont tickLabelFont = axisFont( axis );
int lineExtent = static_cast<int>( std::ceil( axisScaleDraw( axis )->extent( tickLabelFont ) ) );
if ( !axisTitle( axis ).text().isEmpty() )
{
QFont titleFont = axisTitle( axis ).font();
lineExtent += QFontMetrics( titleFont ).height();
}
return lineExtent;
}