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"
|
|
|
|
|
|
|
|
#include "RimMainPlotCollection.h"
|
|
|
|
#include "RimProject.h"
|
|
|
|
#include "RimViewWindow.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>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMdiSubWindow>
|
|
|
|
|
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
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
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
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicSnapshotViewToFileFeature::saveToFile( const QImage& image, const QString& defaultFileBaseName )
|
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-09-06 03:40:57 -05:00
|
|
|
QString defaultAbsFileName = caf::Utils::constructFullFileName( startPath, defaultFileBaseName, ".png" );
|
|
|
|
QString fileName = QFileDialog::getSaveFileName( nullptr, tr( "Export to File" ), defaultAbsFileName );
|
|
|
|
if ( fileName.isEmpty() )
|
2017-08-03 09:22:02 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the directory to next time
|
2019-09-06 03:40:57 -05:00
|
|
|
app->setLastUsedDialogDirectory( "IMAGE_SNAPSHOT", QFileInfo( fileName ).absolutePath() );
|
2017-08-03 09:22:02 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
RicSnapshotViewToFileFeature::saveSnapshotAs( fileName, image );
|
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()
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return QIcon( ":/SnapShotSave.png" );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image = viewWindow->snapshotWindowContent();
|
2019-09-06 03:40:57 -05:00
|
|
|
saveToFile( image, 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
|
|
|
}
|