Skip to content

Commit

Permalink
Add multi axis zoom class
Browse files Browse the repository at this point in the history
Add class used to zoom all axis in a plot based on a screen rectangle.
  • Loading branch information
magnesj committed Sep 15, 2023
1 parent 6f6f773 commit 92e1393
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 37 deletions.
3 changes: 3 additions & 0 deletions ApplicationLibCode/UserInterface/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiuMatrixPlotWidget.h
${CMAKE_CURRENT_LIST_DIR}/RiuMenuBarBuildTools.h
${CMAKE_CURRENT_LIST_DIR}/RiuQwtPlotRectAnnotation.h
${CMAKE_CURRENT_LIST_DIR}/RiuQwtPlotZoomerMultiAxes.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand Down Expand Up @@ -219,6 +220,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiuMatrixPlotWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/RiuMenuBarBuildTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiuQwtPlotRectAnnotation.cpp
${CMAKE_CURRENT_LIST_DIR}/RiuQwtPlotZoomerMultiAxes.cpp
)

if(RESINSIGHT_USE_QT_CHARTS)
Expand Down Expand Up @@ -311,6 +313,7 @@ list(
${CMAKE_CURRENT_LIST_DIR}/RiuTextContentFrame.h
${CMAKE_CURRENT_LIST_DIR}/RiuQwtLegendOverlayContentFrame.h
${CMAKE_CURRENT_LIST_DIR}/RiuMatrixPlotWidget.h
${CMAKE_CURRENT_LIST_DIR}/RiuQwtPlotZoomerMultiAxes.h
)

list(APPEND QT_UI_FILES)
Expand Down
20 changes: 6 additions & 14 deletions ApplicationLibCode/UserInterface/RiuGridCrossQwtPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ RiuGridCrossQwtPlot::RiuGridCrossQwtPlot( RimGridCrossPlot* plot, QWidget* paren
: RiuQwtPlotWidget( plot, parent )
{
// LeftButton for the zooming
m_zoomerLeft = new RiuQwtPlotZoomer( qwtPlot()->canvas() );
m_zoomerLeft->setTrackerMode( QwtPicker::AlwaysOff );
m_zoomerLeft->initMousePattern( 1 );

// Attach a zoomer for the right axis
m_zoomerRight = new RiuQwtPlotZoomer( qwtPlot()->canvas() );
m_zoomerRight->setAxes( QwtAxis::XTop, QwtAxis::YRight );
m_zoomerRight->setTrackerMode( QwtPicker::AlwaysOff );
m_zoomerRight->initMousePattern( 1 );
m_plotZoomer = new RiuQwtPlotZoomer( qwtPlot()->canvas() );
m_plotZoomer->setTrackerMode( QwtPicker::AlwaysOff );
m_plotZoomer->initMousePattern( 1 );

// MidButton for the panning
QwtPlotPanner* panner = new QwtPlotPanner( qwtPlot()->canvas() );
Expand All @@ -82,8 +76,7 @@ RiuGridCrossQwtPlot::RiuGridCrossQwtPlot( RimGridCrossPlot* plot, QWidget* paren
auto wheelZoomer = new RiuQwtPlotWheelZoomer( qwtPlot() );

connect( wheelZoomer, SIGNAL( zoomUpdated() ), SLOT( onZoomedSlot() ) );
connect( m_zoomerLeft, SIGNAL( zoomed( const QRectF& ) ), SLOT( onZoomedSlot() ) );
connect( m_zoomerRight, SIGNAL( zoomed( const QRectF& ) ), SLOT( onZoomedSlot() ) );
connect( m_plotZoomer, SIGNAL( zoomed() ), SLOT( onZoomedSlot() ) );
connect( panner, SIGNAL( panned( int, int ) ), SLOT( onZoomedSlot() ) );
connect( this,
SIGNAL( plotItemSelected( std::shared_ptr<RiuPlotItem>, bool, int ) ),
Expand Down Expand Up @@ -254,16 +247,15 @@ bool RiuGridCrossQwtPlot::curveText( const QwtPlotCurve* curve, QString* curveTi
//--------------------------------------------------------------------------------------------------
bool RiuGridCrossQwtPlot::isZoomerActive() const
{
return m_zoomerLeft->isActiveAndValid() || m_zoomerRight->isActiveAndValid();
return m_plotZoomer->isActiveAndValid();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuGridCrossQwtPlot::endZoomOperations()
{
m_zoomerLeft->endZoomOperation();
m_zoomerRight->endZoomOperation();
m_plotZoomer->endZoomOperation();
}

//--------------------------------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions ApplicationLibCode/UserInterface/RiuGridCrossQwtPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,5 @@ private slots:
std::unique_ptr<RiuPlotAnnotationTool> m_annotationTool;
QwtPlotMarker* m_selectedPointMarker;

QPointer<RiuQwtPlotZoomer> m_zoomerLeft;
QPointer<RiuQwtPlotZoomer> m_zoomerRight;
QPointer<RiuQwtPlotZoomer> m_plotZoomer;
};
9 changes: 4 additions & 5 deletions ApplicationLibCode/UserInterface/RiuQwtPlotZoomer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
#pragma once

#include "RiuGuiTheme.h"
#include "RiuQwtPlotZoomerMultiAxes.h"

#include "qwt_plot_zoomer.h"

#include <QMouseEvent>

class RiuQwtPlotZoomer : public QwtPlotZoomer
class RiuQwtPlotZoomer : public RiuQwtPlotZoomerMultiAxes
{
public:
RiuQwtPlotZoomer( QWidget* canvas, bool doReplot = true )
: QwtPlotZoomer( canvas, doReplot )
: RiuQwtPlotZoomerMultiAxes( canvas, doReplot )
{
auto color = RiuGuiTheme::getColorByVariableName( "markerColor" );

Expand All @@ -46,8 +47,6 @@ class RiuQwtPlotZoomer : public QwtPlotZoomer
void endZoomOperation() { reset(); }

protected:
QSizeF minZoomSize() const override { return QwtPlotZoomer::minZoomSize() / 10.0e6; }

bool accept( QPolygon& pa ) const override
{
if ( pa.count() < 2 ) return false;
Expand All @@ -59,6 +58,6 @@ class RiuQwtPlotZoomer : public QwtPlotZoomer
const int minSize = 10;
if ( rect.width() < minSize && rect.height() < minSize ) return false;

return QwtPlotZoomer::accept( pa );
return RiuQwtPlotZoomerMultiAxes::accept( pa );
}
};
110 changes: 110 additions & 0 deletions ApplicationLibCode/UserInterface/RiuQwtPlotZoomerMultiAxes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// 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.
//
// 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>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RiuQwtPlotZoomerMultiAxes.h"

#include "qwt_picker_machine.h"
#include "qwt_plot.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"

//--------------------------------------------------------------------------------------------------
/// Create a zoomer for a plot canvas. All axes will be scaled based on the rectangle in screen coordinates.
//--------------------------------------------------------------------------------------------------
RiuQwtPlotZoomerMultiAxes::RiuQwtPlotZoomerMultiAxes( QWidget* canvas, bool doReplot )
: QwtPlotPicker( canvas )
{
if ( canvas )
{
setTrackerMode( ActiveOnly );
setRubberBand( RectRubberBand );
setStateMachine( new QwtPickerDragRectMachine() );

if ( doReplot && plot() ) plot()->replot();
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiuQwtPlotZoomerMultiAxes::end( bool ok )
{
ok = QwtPlotPicker::end( ok );
if ( !ok ) return false;

QwtPlot* plot = RiuQwtPlotZoomerMultiAxes::plot();
if ( !plot ) return false;

const QPolygon& pa = selection();
if ( pa.count() < 2 ) return false;

QRect rect = QRect( pa.first(), pa.last() );
rect = rect.normalized();

zoomFromScreenCoords( rect );

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuQwtPlotZoomerMultiAxes::zoomFromScreenCoords( const QRectF& screenCoords )
{
QwtPlot* plot = RiuQwtPlotZoomerMultiAxes::plot();
if ( !plot ) return;

const bool doReplot = plot->autoReplot();
plot->setAutoReplot( false );

for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
{
const int axesCount = plot->axesCount( axisPos );
for ( int i = 0; i < axesCount; i++ )
{
const QwtAxisId axisId( axisPos, i );

// Get the scale map for the axis used to convert between screen and domain coordinates
const QwtScaleMap map = plot->canvasMap( axisId );

if ( axisId.isXAxis() )
{
const double screenX1 = screenCoords.left();
const double screenX2 = screenCoords.right();
const double domainX1 = map.invTransform( screenX1 );
const double domainX2 = map.invTransform( screenX2 );

plot->setAxisScale( axisId, domainX1, domainX2 );
}
else
{
const double screenY1 = screenCoords.bottom();
const double screenY2 = screenCoords.top();
const double domainY1 = map.invTransform( screenY1 );
const double domainY2 = map.invTransform( screenY2 );

plot->setAxisScale( axisId, domainY1, domainY2 );
}
}
}

plot->setAutoReplot( doReplot );
plot->replot();

emit zoomed();
}
40 changes: 40 additions & 0 deletions ApplicationLibCode/UserInterface/RiuQwtPlotZoomerMultiAxes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// 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.
//
// 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>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "qwt_plot_picker.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiuQwtPlotZoomerMultiAxes : public QwtPlotPicker
{
Q_OBJECT
public:
explicit RiuQwtPlotZoomerMultiAxes( QWidget*, bool doReplot = true );

Q_SIGNALS:
void zoomed();

protected:
bool end( bool ok = true ) override;

private:
void zoomFromScreenCoords( const QRectF& screenCoords );
};
20 changes: 6 additions & 14 deletions ApplicationLibCode/UserInterface/RiuSummaryQwtPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,9 @@ RiuSummaryQwtPlot::RiuSummaryQwtPlot( RimSummaryPlot* plot, QWidget* parent /*=
connect( m_plotWidget, SIGNAL( customContextMenuRequested( QPoint ) ), this, SLOT( showContextMenu( QPoint ) ) );

// LeftButton for the zooming
m_zoomerLeft = new RiuQwtPlotZoomer( m_plotWidget->qwtPlot()->canvas() );
m_zoomerLeft->setTrackerMode( QwtPicker::AlwaysOff );
m_zoomerLeft->initMousePattern( 1 );

// Attach a zoomer for the right axis
m_zoomerRight = new RiuQwtPlotZoomer( m_plotWidget->qwtPlot()->canvas() );
m_zoomerRight->setAxes( QwtAxis::XTop, QwtAxis::YRight );
m_zoomerRight->setTrackerMode( QwtPicker::AlwaysOff );
m_zoomerRight->initMousePattern( 1 );
m_plotZoomer = new RiuQwtPlotZoomer( m_plotWidget->qwtPlot()->canvas() );
m_plotZoomer->setTrackerMode( QwtPicker::AlwaysOff );
m_plotZoomer->initMousePattern( 1 );

// MidButton for the panning
QwtPlotPanner* panner = new QwtPlotPanner( m_plotWidget->qwtPlot()->canvas() );
Expand All @@ -98,8 +92,7 @@ RiuSummaryQwtPlot::RiuSummaryQwtPlot( RimSummaryPlot* plot, QWidget* parent /*=
m_wheelZoomer = new RiuQwtPlotWheelZoomer( m_plotWidget->qwtPlot() );

connect( m_wheelZoomer, SIGNAL( zoomUpdated() ), SLOT( onZoomedSlot() ) );
connect( m_zoomerLeft, SIGNAL( zoomed( const QRectF& ) ), SLOT( onZoomedSlot() ) );
connect( m_zoomerRight, SIGNAL( zoomed( const QRectF& ) ), SLOT( onZoomedSlot() ) );
connect( m_plotZoomer, SIGNAL( zoomed() ), SLOT( onZoomedSlot() ) );
connect( panner, SIGNAL( panned( int, int ) ), SLOT( onZoomedSlot() ) );

setDefaults();
Expand Down Expand Up @@ -194,16 +187,15 @@ void RiuSummaryQwtPlot::setDefaults()
//--------------------------------------------------------------------------------------------------
bool RiuSummaryQwtPlot::isZoomerActive() const
{
return m_zoomerLeft->isActiveAndValid() || m_zoomerRight->isActiveAndValid();
return m_plotZoomer->isActiveAndValid();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuSummaryQwtPlot::endZoomOperations()
{
m_zoomerLeft->endZoomOperation();
m_zoomerRight->endZoomOperation();
m_plotZoomer->endZoomOperation();
}

//--------------------------------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions ApplicationLibCode/UserInterface/RiuSummaryQwtPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private slots:
std::unique_ptr<RiuPlotAnnotationTool> m_annotationTool;
QPointer<RiuQwtPlotWidget> m_plotWidget;

QPointer<RiuQwtPlotZoomer> m_zoomerLeft;
QPointer<RiuQwtPlotZoomer> m_zoomerRight;
QPointer<RiuQwtPlotZoomer> m_plotZoomer;
QPointer<RiuQwtPlotWheelZoomer> m_wheelZoomer;
};

0 comments on commit 92e1393

Please sign in to comment.