2015-08-24 10:03:21 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- Ceetron Solutions AS
//
// 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 "RicSaveEclipseInputPropertyFeature.h"
2017-06-28 10:13:59 +02:00
#include "RiaApplication.h"
#include "RicExportFeatureImpl.h"
#include "RifEclipseInputFileTools.h"
#include "RimEclipseInputCase.h"
2015-08-24 10:03:21 +02:00
#include "RimEclipseInputProperty.h"
#include "RimEclipseInputPropertyCollection.h"
#include "RimExportInputPropertySettings.h"
2018-02-27 16:37:06 +01:00
#include "Riu3DMainWindowTools.h"
2015-08-24 10:03:21 +02:00
#include "cafPdmUiPropertyViewDialog.h"
2017-06-28 10:13:59 +02:00
#include "cafSelectionManager.h"
2015-08-24 10:03:21 +02:00
#include <QAction>
#include <QFileInfo>
2017-06-28 10:13:59 +02:00
#include <QMessageBox>
2015-08-24 10:03:21 +02:00
CAF_CMD_SOURCE_INIT ( RicSaveEclipseInputPropertyFeature , "RicSaveEclipseInputPropertyFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicSaveEclipseInputPropertyFeature :: isCommandEnabled ()
{
2018-02-18 18:56:43 +01:00
return selectedInputProperty () != nullptr ;
2015-08-24 10:03:21 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicSaveEclipseInputPropertyFeature :: onActionTriggered ( bool isChecked )
{
2017-04-20 10:38:58 +02:00
this -> disableModelChangeContribution ();
2015-08-24 10:03:21 +02:00
RimEclipseInputProperty * inputProperty = selectedInputProperty ();
if ( ! inputProperty ) return ;
{
bool isResolved = false ;
if ( inputProperty -> resolvedState == RimEclipseInputProperty :: RESOLVED || inputProperty -> resolvedState == RimEclipseInputProperty :: RESOLVED_NOT_SAVED )
{
isResolved = true ;
}
if ( ! isResolved )
{
2018-02-27 16:37:06 +01:00
QMessageBox :: warning ( Riu3DMainWindowTools :: mainWindowWidget (), "Export failure" , "Property is not resolved, and then it is not possible to export the property." );
2015-08-24 10:03:21 +02:00
return ;
}
}
RimExportInputSettings exportSettings ;
exportSettings . eclipseKeyword = inputProperty -> eclipseKeyword ;
// Find input reservoir for this property
2018-02-18 18:56:43 +01:00
RimEclipseInputCase * inputReservoir = nullptr ;
2015-08-24 10:03:21 +02:00
{
RimEclipseInputPropertyCollection * inputPropertyCollection = dynamic_cast < RimEclipseInputPropertyCollection *> ( inputProperty -> parentField () -> ownerObject ());
if ( ! inputPropertyCollection ) return ;
inputReservoir = dynamic_cast < RimEclipseInputCase *> ( inputPropertyCollection -> parentField () -> ownerObject ());
}
if ( ! inputReservoir ) return ;
{
RiaApplication * app = RiaApplication :: instance ();
2016-09-21 09:46:17 +02:00
QString projectFolder = app -> currentProjectPath ();
if ( projectFolder . isEmpty ())
2015-08-24 10:03:21 +02:00
{
projectFolder = inputReservoir -> locationOnDisc ();
}
QString outputFileName = projectFolder + "/" + inputProperty -> eclipseKeyword ;
exportSettings . fileName = outputFileName ;
}
2018-02-27 16:37:06 +01:00
caf :: PdmUiPropertyViewDialog propertyDialog ( Riu3DMainWindowTools :: mainWindowWidget (), & exportSettings , "Export Eclipse Property to Text File" , "" );
2017-06-28 10:13:59 +02:00
RicExportFeatureImpl :: configureForExport ( & propertyDialog );
2015-08-24 10:03:21 +02:00
if ( propertyDialog . exec () == QDialog :: Accepted )
{
2017-03-15 09:10:16 +01:00
bool isOk = RifEclipseInputFileTools :: writePropertyToTextFile ( exportSettings . fileName , inputReservoir -> eclipseCaseData (), 0 , inputProperty -> resultName , exportSettings . eclipseKeyword );
2015-08-24 10:03:21 +02:00
if ( isOk )
{
inputProperty -> fileName = exportSettings . fileName ;
inputProperty -> eclipseKeyword = exportSettings . eclipseKeyword ;
inputProperty -> resolvedState = RimEclipseInputProperty :: RESOLVED ;
inputProperty -> updateConnectedEditors ();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicSaveEclipseInputPropertyFeature :: setupActionLook ( QAction * actionToSetup )
{
2017-03-15 11:49:32 +01:00
actionToSetup -> setText ( "Export Property To File" );
2015-08-24 10:03:21 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimEclipseInputProperty * RicSaveEclipseInputPropertyFeature :: selectedInputProperty () const
{
std :: vector < RimEclipseInputProperty *> selection ;
caf :: SelectionManager :: instance () -> objectsByType ( & selection );
return selection . size () > 0 ? selection [ 0 ] : NULL ;
}