2017-09-18 03:47:59 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// 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 "RicImportObservedDataFeature.h"
# include "RiaApplication.h"
2017-09-18 07:16:35 -05:00
# include "RimObservedData.h"
2017-09-18 03:47:59 -05:00
# include "RimObservedDataCollection.h"
# include "RimOilField.h"
# include "RimProject.h"
# include "RimSummaryObservedDataFile.h"
2018-02-27 08:53:33 -06:00
# include "RiuPlotMainWindowTools.h"
2017-10-16 06:31:06 -05:00
2017-09-18 07:16:35 -05:00
# include "cafSelectionManager.h"
2017-09-18 03:47:59 -05:00
# include <QAction>
# include <QFileDialog>
2017-11-03 02:11:27 -05:00
# include <QMessageBox>
2017-09-18 03:47:59 -05:00
CAF_CMD_SOURCE_INIT ( RicImportObservedDataFeature , " RicImportObservedDataFeature " ) ;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicImportObservedDataFeature : : RicImportObservedDataFeature ( )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2017-09-18 07:16:35 -05:00
void RicImportObservedDataFeature : : selectObservedDataFileInDialog ( )
2017-09-18 03:47:59 -05:00
{
RiaApplication * app = RiaApplication : : instance ( ) ;
QString defaultDir = app - > lastUsedDialogDirectory ( " INPUT_FILES " ) ;
2017-12-14 07:26:07 -06:00
QStringList fileNames = QFileDialog : : getOpenFileNames ( nullptr , " Import Observed Time History Data " , defaultDir , " Observed Data (*.RSM *.txt *.csv);;All Files (*.*) " ) ;
2017-09-18 03:47:59 -05:00
if ( fileNames . isEmpty ( ) ) return ;
// Remember the path to next time
app - > setLastUsedDialogDirectory ( " INPUT_FILES " , QFileInfo ( fileNames . last ( ) ) . absolutePath ( ) ) ;
RimProject * proj = app - > project ( ) ;
RimObservedDataCollection * observedDataCollection = proj - > activeOilField ( ) ? proj - > activeOilField ( ) - > observedDataCollection ( ) : nullptr ;
if ( ! observedDataCollection ) return ;
2017-12-01 05:15:30 -06:00
RimObservedData * observedData = nullptr ;
2017-11-03 02:11:27 -05:00
2017-10-16 06:31:06 -05:00
for ( const QString & fileName : fileNames )
2017-09-18 03:47:59 -05:00
{
2017-12-01 05:15:30 -06:00
bool retryImport = false ;
do
2017-11-03 02:11:27 -05:00
{
2017-12-01 05:15:30 -06:00
QString errorText ;
if ( fileName . endsWith ( " .rsm " , Qt : : CaseInsensitive ) )
{
observedData = observedDataCollection - > createAndAddRsmObservedDataFromFile ( fileName , & errorText ) ;
retryImport = false ;
}
else if ( fileName . endsWith ( " .txt " , Qt : : CaseInsensitive ) | | fileName . endsWith ( " .csv " , Qt : : CaseInsensitive ) )
{
bool useSavedFieldValuesInDialog = retryImport ;
observedData = observedDataCollection - > createAndAddCvsObservedDataFromFile ( fileName , useSavedFieldValuesInDialog , & errorText ) ;
retryImport = ! errorText . isEmpty ( ) ;
}
else
{
errorText = " Not able to import file. Make sure '*.rsm' is used as extension if data is in RMS format or '*.txt' or '*.csv' if data is in CSV format. " ;
retryImport = false ;
}
if ( ! errorText . isEmpty ( ) )
{
QMessageBox msgBox ;
msgBox . setIcon ( QMessageBox : : Warning ) ;
msgBox . setText ( " Errors detected during import " ) ;
msgBox . setDetailedText ( errorText ) ;
msgBox . exec ( ) ;
}
} while ( retryImport ) ;
2017-11-03 02:11:27 -05:00
}
2018-02-27 08:53:33 -06:00
RiuPlotMainWindowTools : : showPlotMainWindow ( ) ;
RiuPlotMainWindowTools : : selectAsCurrentItem ( observedData ) ;
2017-09-18 03:47:59 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2017-09-18 07:16:35 -05:00
bool RicImportObservedDataFeature : : isCommandEnabled ( )
{
std : : vector < RimObservedDataCollection * > selectionObservedDataCollection ;
caf : : SelectionManager : : instance ( ) - > objectsByType ( & selectionObservedDataCollection ) ;
std : : vector < RimObservedData * > selectionObservedData ;
caf : : SelectionManager : : instance ( ) - > objectsByType ( & selectionObservedData ) ;
return ( selectionObservedDataCollection . size ( ) > 0 | | selectionObservedData . size ( ) > 0 ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicImportObservedDataFeature : : onActionTriggered ( bool isChecked )
{
RicImportObservedDataFeature : : selectObservedDataFileInDialog ( ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2017-09-18 03:47:59 -05:00
void RicImportObservedDataFeature : : setupActionLook ( QAction * actionToSetup )
{
2018-05-07 04:30:19 -05:00
actionToSetup - > setIcon ( QIcon ( " :/ObservedDataFile16x16.png " ) ) ;
2017-09-29 04:39:07 -05:00
actionToSetup - > setText ( " Import Observed Time History Data " ) ;
2017-09-18 03:47:59 -05:00
}