Work in progress: Support capturing snapshots with a given size.

This commit is contained in:
Jon Jenssen authored and JJ committed 2026-06-17 13:55:32 +02:00
1 parent c2811b1db8
commit 361b4bc3db
9 files changed
+96 -1

No files matched your search

@@ -46,7 +46,7 @@ void RicSnapshotViewToPdfFeature::onActionTriggered( bool isChecked )
RimViewWindow* viewWindow = RiaGuiApplication::activeViewWindow();
if ( !viewWindow )
{
RiaLogging::error( "No view window is available, nothing to do" );
RiaLogging::error( "No plot window is available, nothing to do" );
return;
}
@@ -513,6 +513,31 @@ QImage Rim3dView::snapshotWindowContent()
return QImage();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage Rim3dView::captureSnapshot( int width, int height )
{
QImage image;
if ( m_viewer )
{
QSize orgSize = m_viewer->layoutWidget()->size();
// adjust for possible display DPI scaling
const auto ratio = m_viewer->displayScalingRatio();
m_viewer->layoutWidget()->setFixedSize( (int)( 1.0 * width / ratio ), (int)( 1.0 * height / ratio ) );
image = m_viewer->snapshotImage();
// reset fixed size and restore original size
m_viewer->layoutWidget()->setMinimumSize( 0, 0 );
m_viewer->layoutWidget()->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
m_viewer->layoutWidget()->resize( orgSize );
}
return image;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -147,6 +147,7 @@ public:
cvf::ref<caf::DisplayCoordTransform> displayCoordTransform() const override;
virtual std::vector<RimLegendConfig*> legendConfigs() const = 0;
QImage captureSnapshot( int width, int height ) override;
QImage snapshotWindowContent() override;
void zoomAll() override;
void forceShowWindowOn();
@@ -35,6 +35,7 @@
#include "cafPdmUiToolButtonEditor.h"
#include <QPaintDevice>
#include <QPainter>
#include <QRegularExpression>
#include <cvfAssert.h>
@@ -639,6 +640,25 @@ QImage RimMultiPlot::snapshotWindowContent()
return image;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage RimMultiPlot::captureSnapshot( int width, int height )
{
QImage image;
if ( m_viewer.isNull() ) return image;
QSize orgViewSize = m_viewer->size();
image = captureImage( m_viewer->bookWidget(), width, height );
m_viewer->resize( orgViewSize );
doUpdateLayout();
return image;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -117,6 +117,7 @@ public:
protected:
QImage snapshotWindowContent() override;
QImage captureSnapshot( int width, int height ) override;
QWidget* createViewWidget( QWidget* mainWindowParent ) override;
void deleteViewWidget() override;
@@ -38,6 +38,7 @@
#include <QDebug>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QWidget>
@@ -222,6 +223,39 @@ QImage RimViewWindow::snapshotWindowContent()
return image;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage RimViewWindow::captureSnapshot( int width, int height )
{
return captureImage( viewWidget(), width, height );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage RimViewWindow::captureImage( QWidget* widget, int width, int height )
{
if ( !widget ) return QImage();
QSize orgSize = widget->size();
widget->setFixedSize( width, height );
QPixmap pix( width, height );
pix.fill( Qt::transparent );
QPainter painter( &pix );
widget->render( &painter );
// reset fixed size and restore original size
widget->setMinimumSize( 0, 0 );
widget->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
widget->resize( orgSize );
return pix.toImage();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -66,6 +66,8 @@ public:
ads::CDockWidget* dockWidget();
ads::CDockWidget* createDockWidget();
virtual QImage captureSnapshot( int width, int height );
virtual QImage snapshotWindowContent();
virtual void zoomAll() = 0;
@@ -99,6 +101,8 @@ protected:
void defineObjectEditorAttribute( QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;
QImage captureImage( QWidget* widget, int width, int height );
private:
friend class RimProject;
@@ -797,3 +797,11 @@ void RiuMultiPlotBook::adjustBookFrame()
{
m_book->adjustSize();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* RiuMultiPlotBook::bookWidget()
{
return m_book;
}
@@ -98,6 +98,8 @@ public:
// The code used to be called from RiuMultiPlotBook::showEvent(), but this caused a crash when a dock widget was hidden and shown again.
void forcePerformUpdate();
QWidget* bookWidget();
protected:
void contextMenuEvent( QContextMenuEvent* ) override;