mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Implement pdf rendering (#5250)
* First PDF creation support * Reimplement info box * Set title and make overlay frame margins more unified * Remove a style sheet that was never meant to be applied to Project Tree * Update RiuDraggableOverlayFrame when changing content * Default page layout in Preferences * undo removal of elision * Remove friend class assignment in cafCategoryMapper * the required methods have been made public * Fix up after review * Remove spurious const on by-value return * Fix compile errors on Linux * Fix size adjustment of legends with plot resizing
This commit is contained in:
@@ -20,8 +20,10 @@
|
||||
|
||||
#include "RiaGuiApplication.h"
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaPlotWindowRedrawScheduler.h"
|
||||
|
||||
#include "RimMainPlotCollection.h"
|
||||
#include "RimPlotWindow.h"
|
||||
#include "RimProject.h"
|
||||
#include "RimViewWindow.h"
|
||||
#include "RiuPlotMainWindow.h"
|
||||
@@ -33,9 +35,13 @@
|
||||
#include <QAction>
|
||||
#include <QClipboard>
|
||||
#include <QDebug>
|
||||
#include <QDesktopWidget>
|
||||
#include <QFileDialog>
|
||||
#include <QFileInfo>
|
||||
#include <QMdiSubWindow>
|
||||
#include <QPageLayout>
|
||||
#include <QPainter>
|
||||
#include <QPdfWriter>
|
||||
|
||||
CAF_CMD_SOURCE_INIT( RicSnapshotViewToFileFeature, "RicSnapshotViewToFileFeature" );
|
||||
|
||||
@@ -72,7 +78,71 @@ void RicSnapshotViewToFileFeature::saveSnapshotAs( const QString& fileName, cons
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RicSnapshotViewToFileFeature::saveToFile( const QImage& image, const QString& defaultFileBaseName )
|
||||
void RicSnapshotViewToFileFeature::savePlotPDFReportAs( const QString& fileName, RimPlotWindow* plot )
|
||||
{
|
||||
RiaPlotWindowRedrawScheduler::instance()->performScheduledUpdatesAndReplots();
|
||||
QCoreApplication::processEvents();
|
||||
QFile pdfFile( fileName );
|
||||
if ( pdfFile.open( QIODevice::WriteOnly ) )
|
||||
{
|
||||
const int resolution = RiaGuiApplication::instance()->desktop()->logicalDpiX();
|
||||
QPdfWriter pdfPrinter( fileName );
|
||||
pdfPrinter.setPageLayout( plot->pageLayout() );
|
||||
pdfPrinter.setCreator( QCoreApplication::applicationName() );
|
||||
pdfPrinter.setResolution( resolution );
|
||||
QPainter painter( &pdfPrinter );
|
||||
QRect widgetRect = plot->viewWidget()->contentsRect();
|
||||
QRect fullPageRect = pdfPrinter.pageLayout().fullRectPixels( resolution );
|
||||
QRect paintRect = pdfPrinter.pageLayout().paintRectPixels( resolution );
|
||||
plot->viewWidget()->resize( paintRect.size() );
|
||||
plot->renderWindowContent( &painter );
|
||||
plot->viewWidget()->resize( widgetRect.size() );
|
||||
}
|
||||
else
|
||||
{
|
||||
RiaLogging::error( QString( "Could not write PDF to %1" ).arg( fileName ) );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RicSnapshotViewToFileFeature::saveViewWindowToFile( RimViewWindow* viewWindow,
|
||||
const QString& defaultFileBaseName /*= "image" */ )
|
||||
{
|
||||
RimPlotWindow* plotWindow = dynamic_cast<RimPlotWindow*>( viewWindow );
|
||||
|
||||
QString fileName = generateSaveFileName( defaultFileBaseName, plotWindow != nullptr );
|
||||
if ( !fileName.isEmpty() )
|
||||
{
|
||||
if ( plotWindow && fileName.endsWith( "PDF", Qt::CaseInsensitive ) )
|
||||
{
|
||||
savePlotPDFReportAs( fileName, plotWindow );
|
||||
}
|
||||
else
|
||||
{
|
||||
RicSnapshotViewToFileFeature::saveSnapshotAs( fileName, viewWindow->snapshotWindowContent() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RicSnapshotViewToFileFeature::saveImageToFile( const QImage& image, const QString& defaultFileBaseName )
|
||||
{
|
||||
QString fileName = generateSaveFileName( defaultFileBaseName, false );
|
||||
if ( !fileName.isEmpty() )
|
||||
{
|
||||
RicSnapshotViewToFileFeature::saveSnapshotAs( fileName, image );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RicSnapshotViewToFileFeature::generateSaveFileName( const QString& defaultFileBaseName /*= "image"*/,
|
||||
bool supportPDF /*= false */ )
|
||||
{
|
||||
RiaApplication* app = RiaApplication::instance();
|
||||
RimProject* proj = app->project();
|
||||
@@ -88,17 +158,30 @@ void RicSnapshotViewToFileFeature::saveToFile( const QImage& image, const QStrin
|
||||
startPath = app->lastUsedDialogDirectory( "IMAGE_SNAPSHOT" );
|
||||
}
|
||||
|
||||
QString defaultAbsFileName = caf::Utils::constructFullFileName( startPath, defaultFileBaseName, ".png" );
|
||||
QString fileName = QFileDialog::getSaveFileName( nullptr, tr( "Export to File" ), defaultAbsFileName );
|
||||
if ( fileName.isEmpty() )
|
||||
QStringList imageFileExtensions;
|
||||
imageFileExtensions << "*.png"
|
||||
<< "*.jpg"
|
||||
<< "*.bmp"
|
||||
<< "*.pbm"
|
||||
<< "*.pgm";
|
||||
QString fileExtensionFilter = QString( "Images (%1)" ).arg( imageFileExtensions.join( " " ) );
|
||||
|
||||
if ( supportPDF )
|
||||
{
|
||||
return;
|
||||
fileExtensionFilter += ";;PDF report( *.pdf )";
|
||||
}
|
||||
|
||||
// Remember the directory to next time
|
||||
app->setLastUsedDialogDirectory( "IMAGE_SNAPSHOT", QFileInfo( fileName ).absolutePath() );
|
||||
|
||||
RicSnapshotViewToFileFeature::saveSnapshotAs( fileName, image );
|
||||
QString defaultAbsFileName = caf::Utils::constructFullFileName( startPath, defaultFileBaseName, ".png" );
|
||||
QString fileName = QFileDialog::getSaveFileName( nullptr,
|
||||
tr( "Export to File" ),
|
||||
defaultAbsFileName,
|
||||
fileExtensionFilter );
|
||||
if ( !fileName.isEmpty() )
|
||||
{
|
||||
// Remember the directory to next time
|
||||
app->setLastUsedDialogDirectory( "IMAGE_SNAPSHOT", QFileInfo( fileName ).absolutePath() );
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -142,8 +225,7 @@ void RicSnapshotViewToFileFeature::onActionTriggered( bool isChecked )
|
||||
return;
|
||||
}
|
||||
|
||||
QImage image = viewWindow->snapshotWindowContent();
|
||||
saveToFile( image, RicSnapshotFilenameGenerator::generateSnapshotFileName( viewWindow ) );
|
||||
saveViewWindowToFile( viewWindow, RicSnapshotFilenameGenerator::generateSnapshotFileName( viewWindow ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "cafCmdFeature.h"
|
||||
|
||||
class RimPlotWindow;
|
||||
class RimViewWindow;
|
||||
class QImage;
|
||||
|
||||
@@ -31,9 +32,13 @@ class RicSnapshotViewToFileFeature : public caf::CmdFeature
|
||||
CAF_CMD_HEADER_INIT;
|
||||
|
||||
public:
|
||||
static void saveSnapshotAs( const QString& fileName, RimViewWindow* viewWindow );
|
||||
static void saveSnapshotAs( const QString& fileName, const QImage& image );
|
||||
static void saveToFile( const QImage& image, const QString& defaultFileBaseName = "image" );
|
||||
static void saveSnapshotAs( const QString& fileName, RimViewWindow* viewWindow );
|
||||
static void saveSnapshotAs( const QString& fileName, const QImage& image );
|
||||
static void savePlotPDFReportAs( const QString& fileName, RimPlotWindow* plotWindow );
|
||||
|
||||
static void saveViewWindowToFile( RimViewWindow* viewWindow, const QString& defaultFileBaseName = "image" );
|
||||
static void saveImageToFile( const QImage& image, const QString& defaultFileBaseName = "image" );
|
||||
static QString generateSaveFileName( const QString& defaultFileBaseName = "image", bool supportPDF = false );
|
||||
static QIcon icon();
|
||||
static QString text();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user