2017-08-03 09:22:02 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 Statoil ASA
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-08-03 09:22:02 -05: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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-08-03 09:22:02 -05: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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2017-08-03 09:22:02 -05:00
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RicSnapshotViewToFileFeature.h"
|
|
|
|
|
2019-05-06 03:36:05 -05:00
|
|
|
#include "RiaGuiApplication.h"
|
2017-08-03 09:22:02 -05:00
|
|
|
#include "RiaLogging.h"
|
2019-12-18 05:25:19 -06:00
|
|
|
#include "RiaPlotWindowRedrawScheduler.h"
|
2017-08-03 09:22:02 -05:00
|
|
|
|
|
|
|
#include "RimMainPlotCollection.h"
|
2020-01-16 05:32:40 -06:00
|
|
|
#include "RimMultiPlot.h"
|
2019-12-18 05:25:19 -06:00
|
|
|
#include "RimPlotWindow.h"
|
2017-08-03 09:22:02 -05:00
|
|
|
#include "RimProject.h"
|
|
|
|
#include "RimViewWindow.h"
|
2020-08-21 11:56:44 -05:00
|
|
|
|
|
|
|
#include "RiuFileDialogTools.h"
|
2020-10-01 03:35:57 -05:00
|
|
|
#include "RiuGuiTheme.h"
|
2018-04-26 23:28:08 -05:00
|
|
|
#include "RiuPlotMainWindow.h"
|
2017-08-03 09:22:02 -05:00
|
|
|
|
2017-08-04 04:25:17 -05:00
|
|
|
#include "RicSnapshotFilenameGenerator.h"
|
|
|
|
|
2017-08-03 09:22:02 -05:00
|
|
|
#include "cafUtils.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QDebug>
|
2019-12-18 05:25:19 -06:00
|
|
|
#include <QDesktopWidget>
|
2017-08-03 09:22:02 -05:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMdiSubWindow>
|
2019-12-18 05:25:19 -06:00
|
|
|
#include <QPageLayout>
|
|
|
|
#include <QPdfWriter>
|
2017-08-03 09:22:02 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
CAF_CMD_SOURCE_INIT( RicSnapshotViewToFileFeature, "RicSnapshotViewToFileFeature" );
|
2017-08-03 09:22:02 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-08-03 09:22:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicSnapshotViewToFileFeature::saveSnapshotAs( const QString& fileName, RimViewWindow* viewWindow )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2020-01-21 05:36:22 -06:00
|
|
|
RimPlotWindow* plotWindow = dynamic_cast<RimPlotWindow*>( viewWindow );
|
|
|
|
if ( plotWindow && fileName.endsWith( ".pdf" ) )
|
|
|
|
{
|
2020-02-17 04:11:11 -06:00
|
|
|
savePlotPdfReportAs( fileName, plotWindow );
|
2020-01-21 05:36:22 -06:00
|
|
|
}
|
|
|
|
else if ( viewWindow )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
|
|
|
QImage image = viewWindow->snapshotWindowContent();
|
2019-09-06 03:40:57 -05:00
|
|
|
saveSnapshotAs( fileName, image );
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-08-03 09:22:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicSnapshotViewToFileFeature::saveSnapshotAs( const QString& fileName, const QImage& image )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( !image.isNull() )
|
2017-11-29 04:45:01 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( image.save( fileName ) )
|
2017-11-29 04:45:01 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
RiaLogging::info( QString( "Exported snapshot image to %1" ).arg( fileName ) );
|
2017-11-29 04:45:01 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
RiaLogging::error( QString( "Error when trying to export snapshot image to %1" ).arg( fileName ) );
|
2017-11-29 04:45:01 -06:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-08-03 09:22:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-02-17 04:11:11 -06:00
|
|
|
void RicSnapshotViewToFileFeature::savePlotPdfReportAs( const QString& fileName, RimPlotWindow* plot )
|
2019-12-18 05:25:19 -06:00
|
|
|
{
|
2020-10-01 03:35:57 -05:00
|
|
|
auto currentTheme = RiuGuiTheme::currentGuiTheme();
|
|
|
|
|
|
|
|
RiuGuiTheme::updateGuiTheme( RiaDefines::ThemeEnum::LIGHT );
|
2019-12-18 05:25:19 -06:00
|
|
|
RiaPlotWindowRedrawScheduler::instance()->performScheduledUpdatesAndReplots();
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QFile pdfFile( fileName );
|
|
|
|
if ( pdfFile.open( QIODevice::WriteOnly ) )
|
|
|
|
{
|
2020-01-07 02:26:39 -06:00
|
|
|
int resolution = RiaGuiApplication::applicationResolution();
|
|
|
|
int pageWidth = plot->pageLayout().fullRectPixels( resolution ).width();
|
|
|
|
int widgetWidth = plot->viewWidget()->width();
|
|
|
|
int deltaWidth = widgetWidth - pageWidth;
|
2020-01-06 02:06:10 -06:00
|
|
|
|
2020-01-07 02:26:39 -06:00
|
|
|
while ( std::abs( deltaWidth ) > 1 )
|
|
|
|
{
|
|
|
|
int newResolution = resolution + deltaWidth / std::abs( deltaWidth );
|
|
|
|
pageWidth = plot->pageLayout().fullRectPixels( resolution ).width();
|
|
|
|
int newDeltaWidth = widgetWidth - pageWidth;
|
|
|
|
if ( std::abs( newDeltaWidth ) > std::abs( deltaWidth ) ) break;
|
2020-01-06 02:06:10 -06:00
|
|
|
|
2020-01-07 02:26:39 -06:00
|
|
|
resolution = newResolution;
|
|
|
|
deltaWidth = newDeltaWidth;
|
|
|
|
}
|
2019-12-18 05:25:19 -06:00
|
|
|
QPdfWriter pdfPrinter( fileName );
|
|
|
|
pdfPrinter.setPageLayout( plot->pageLayout() );
|
|
|
|
pdfPrinter.setCreator( QCoreApplication::applicationName() );
|
|
|
|
pdfPrinter.setResolution( resolution );
|
2019-12-19 04:37:40 -06:00
|
|
|
QRect widgetRect = plot->viewWidget()->contentsRect();
|
2020-01-08 01:53:06 -06:00
|
|
|
|
2020-01-16 05:32:40 -06:00
|
|
|
RimMultiPlot* multiPlot = dynamic_cast<RimMultiPlot*>( plot );
|
2020-01-08 01:53:06 -06:00
|
|
|
if ( multiPlot && multiPlot->previewModeEnabled() )
|
2020-01-06 06:19:20 -06:00
|
|
|
{
|
|
|
|
QRect pageRect = pdfPrinter.pageLayout().fullRectPixels( resolution );
|
|
|
|
plot->viewWidget()->resize( pageRect.size() );
|
|
|
|
plot->renderWindowContent( &pdfPrinter );
|
|
|
|
plot->viewWidget()->resize( widgetRect.size() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QRect pageRect = pdfPrinter.pageLayout().paintRectPixels( resolution );
|
|
|
|
plot->viewWidget()->resize( pageRect.size() );
|
|
|
|
plot->renderWindowContent( &pdfPrinter );
|
|
|
|
plot->viewWidget()->resize( widgetRect.size() );
|
|
|
|
}
|
2019-12-18 05:25:19 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RiaLogging::error( QString( "Could not write PDF to %1" ).arg( fileName ) );
|
|
|
|
}
|
2020-10-01 03:35:57 -05:00
|
|
|
RiuGuiTheme::updateGuiTheme( currentTheme );
|
2019-12-18 05:25:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
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 ) )
|
|
|
|
{
|
2020-02-17 04:11:11 -06:00
|
|
|
savePlotPdfReportAs( fileName, plotWindow );
|
2019-12-18 05:25:19 -06:00
|
|
|
}
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-02-17 04:11:11 -06:00
|
|
|
QString RicSnapshotViewToFileFeature::generateSaveFileName( const QString& defaultFileBaseName,
|
|
|
|
bool supportPDF,
|
|
|
|
const QString& defaultExtension )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
RiaApplication* app = RiaApplication::instance();
|
|
|
|
RimProject* proj = app->project();
|
2017-08-03 09:22:02 -05:00
|
|
|
|
|
|
|
QString startPath;
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( !proj->fileName().isEmpty() )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QFileInfo fi( proj->fileName() );
|
2017-08-03 09:22:02 -05:00
|
|
|
startPath = fi.absolutePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
startPath = app->lastUsedDialogDirectory( "IMAGE_SNAPSHOT" );
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:25:19 -06:00
|
|
|
QStringList imageFileExtensions;
|
|
|
|
imageFileExtensions << "*.png"
|
|
|
|
<< "*.jpg"
|
|
|
|
<< "*.bmp"
|
|
|
|
<< "*.pbm"
|
|
|
|
<< "*.pgm";
|
|
|
|
QString fileExtensionFilter = QString( "Images (%1)" ).arg( imageFileExtensions.join( " " ) );
|
|
|
|
|
2020-02-17 04:11:11 -06:00
|
|
|
QString pdfFilter = "PDF report( *.pdf )";
|
2019-12-18 05:25:19 -06:00
|
|
|
if ( supportPDF )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2020-02-17 04:11:11 -06:00
|
|
|
fileExtensionFilter += QString( ";;%1" ).arg( pdfFilter );
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 04:11:11 -06:00
|
|
|
QString defaultAbsFileName =
|
|
|
|
caf::Utils::constructFullFileName( startPath, defaultFileBaseName, "." + defaultExtension );
|
|
|
|
|
|
|
|
QString selectedExtension;
|
2021-03-25 08:46:28 -05:00
|
|
|
if ( supportPDF && defaultExtension.compare( "pdf", Qt::CaseInsensitive ) == 0 )
|
2020-02-17 04:11:11 -06:00
|
|
|
{
|
|
|
|
selectedExtension = pdfFilter;
|
|
|
|
}
|
2020-08-21 11:56:44 -05:00
|
|
|
QString fileName = RiuFileDialogTools::getSaveFileName( nullptr,
|
|
|
|
tr( "Export to File" ),
|
|
|
|
defaultAbsFileName,
|
|
|
|
fileExtensionFilter,
|
|
|
|
&selectedExtension );
|
2019-12-18 05:25:19 -06:00
|
|
|
if ( !fileName.isEmpty() )
|
|
|
|
{
|
|
|
|
// Remember the directory to next time
|
|
|
|
app->setLastUsedDialogDirectory( "IMAGE_SNAPSHOT", QFileInfo( fileName ).absolutePath() );
|
|
|
|
}
|
|
|
|
return fileName;
|
2017-11-29 04:45:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-11-29 04:45:01 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QIcon RicSnapshotViewToFileFeature::icon()
|
|
|
|
{
|
2020-10-01 10:25:19 -05:00
|
|
|
return QIcon( ":/SnapShotSave.svg" );
|
2017-11-29 04:45:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-11-29 04:45:01 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RicSnapshotViewToFileFeature::text()
|
|
|
|
{
|
|
|
|
return "Snapshot To File";
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-11-29 04:45:01 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RicSnapshotViewToFileFeature::isCommandEnabled()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-11-29 04:45:01 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicSnapshotViewToFileFeature::onActionTriggered( bool isChecked )
|
2017-11-29 04:45:01 -06:00
|
|
|
{
|
|
|
|
// Get active view window before displaying the file selection dialog
|
|
|
|
// If this is done after the file save dialog is displayed (and closed)
|
2018-10-10 03:17:07 -05:00
|
|
|
// app->activeViewWindow() returns nullptr on Linux
|
2018-09-14 04:23:57 -05:00
|
|
|
|
2019-05-06 03:36:05 -05:00
|
|
|
RimViewWindow* viewWindow = RiaGuiApplication::activeViewWindow();
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( !viewWindow )
|
2017-11-29 04:45:01 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
RiaLogging::error( "No view window is available, nothing to do" );
|
|
|
|
|
2017-11-29 04:45:01 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:25:19 -06:00
|
|
|
saveViewWindowToFile( viewWindow, RicSnapshotFilenameGenerator::generateSnapshotFileName( viewWindow ) );
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-08-03 09:22:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicSnapshotViewToFileFeature::setupActionLook( QAction* actionToSetup )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
actionToSetup->setText( text() );
|
|
|
|
actionToSetup->setIcon( icon() );
|
2017-08-03 09:22:02 -05:00
|
|
|
}
|