#12217 Add support for import of Preferences from a text file

Allow export and import of Preferences from the Preferences dialog
Add command line parameter --preferences mypreferences.xml
This commit is contained in:
Magne Sjaastad
2025-03-12 15:32:44 +01:00
parent 45f182b5c4
commit cc84b68873
7 changed files with 142 additions and 4 deletions
@@ -532,7 +532,10 @@ bool RiaApplication::loadProject( const QString& projectFileName, ProjectLoadAct
// VL check regarding specific order mentioned in comment above...
m_preferences->lastUsedProjectFileName = fullPathProjectFileName;
m_preferences->writePreferencesToApplicationStore();
if ( m_preferencesFileName.isEmpty() )
{
m_preferences->writePreferencesToApplicationStore();
}
for ( size_t oilFieldIdx = 0; oilFieldIdx < m_project->oilFields().size(); oilFieldIdx++ )
{
@@ -857,7 +860,10 @@ bool RiaApplication::saveProjectAs( const QString& fileName, gsl::not_null<QStri
}
m_preferences->lastUsedProjectFileName = fileName;
m_preferences->writePreferencesToApplicationStore();
if ( m_preferencesFileName.isEmpty() )
{
m_preferences->writePreferencesToApplicationStore();
}
onProjectSaved();
@@ -1324,7 +1330,10 @@ void RiaApplication::applyPreferences()
caf::ProgressInfoStatic::setEnabled( RiaPreferencesSystem::current()->showProgressBar() );
m_preferences->writePreferencesToApplicationStore();
if ( m_preferencesFileName.isEmpty() )
{
m_preferences->writePreferencesToApplicationStore();
}
}
//--------------------------------------------------------------------------------------------------
@@ -1484,7 +1493,17 @@ cvf::Font* RiaApplication::defaultWellLabelFont()
void RiaApplication::initialize()
{
m_preferences = std::make_unique<RiaPreferences>();
caf::PdmSettings::readFieldsFromApplicationStore( m_preferences.get() );
if ( !m_preferencesFileName.isEmpty() )
{
// The text file with preference values has priority above reading from user settings
m_preferences->importPreferenceValuesFromFile( m_preferencesFileName );
}
else
{
caf::PdmSettings::readFieldsFromApplicationStore( m_preferences.get() );
}
m_preferences->initAfterReadRecursively();
applyPreferences();
@@ -249,6 +249,8 @@ protected:
QString m_commandLineHelpText;
QMap<QString, QVariant> m_sessionCache; // Session cache used to store username/passwords per session
QString m_preferencesFileName;
bool m_runningWorkerProcess;
private:
@@ -159,6 +159,14 @@ RiaApplication::ApplicationStatus RiaConsoleApplication::handleArguments( gsl::n
return RiaApplication::ApplicationStatus::EXIT_COMPLETED;
}
if ( cvf::Option o = progOpt->option( "preferences" ) )
{
CVF_ASSERT( o.valueCount() == 1 );
m_preferencesFileName = cvfqt::Utils::toQString( o.value( 0 ) );
RiaApplication::initialize();
onProjectClosed();
}
if ( cvf::Option o = progOpt->option( "startdir" ) )
{
CVF_ASSERT( o.valueCount() == 1 );
@@ -495,6 +495,14 @@ RiaApplication::ApplicationStatus RiaGuiApplication::handleArguments( gsl::not_n
return RiaApplication::ApplicationStatus::EXIT_COMPLETED;
}
if ( cvf::Option o = progOpt->option( "preferences" ) )
{
CVF_ASSERT( o.valueCount() == 1 );
m_preferencesFileName = cvfqt::Utils::toQString( o.value( 0 ) );
RiaApplication::initialize();
onProjectClosed();
}
if ( cvf::Option o = progOpt->option( "regressiontest" ) )
{
CVF_ASSERT( o.valueCount() == 1 );
@@ -37,6 +37,7 @@
#include "OsduCommands//RicDeleteOsduTokenFeature.h"
#include "Sumo/RicDeleteSumoTokenFeature.h"
#include "RiuFileDialogTools.h"
#include "RiuGuiTheme.h"
#include "cafPdmFieldCvfColor.h"
@@ -287,6 +288,12 @@ RiaPreferences::RiaPreferences()
CAF_PDM_InitField( &m_deleteSumoToken, "deleteSumoToken", false, "" );
caf::PdmUiPushButtonEditor::configureEditorLabelHidden( &m_deleteSumoToken );
m_deleteSumoToken.xmlCapability()->disableIO();
CAF_PDM_InitFieldNoDefault( &m_importPreferences, "importPreferences", "Import From File" );
caf::PdmUiPushButtonEditor::configureEditorLabelHidden( &m_importPreferences );
CAF_PDM_InitFieldNoDefault( &m_exportPreferences, "exportPreferences", "Export To File" );
caf::PdmUiPushButtonEditor::configureEditorLabelHidden( &m_exportPreferences );
}
//--------------------------------------------------------------------------------------------------
@@ -360,6 +367,20 @@ void RiaPreferences::defineEditorAttribute( const caf::PdmFieldHandle* field, QS
pbAttribute->m_buttonText = "Delete Token";
}
}
else if ( field == &m_importPreferences )
{
if ( auto* pbAttribute = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute ) )
{
pbAttribute->m_buttonText = "Import Preferences";
}
}
else if ( field == &m_exportPreferences )
{
if ( auto* pbAttribute = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute ) )
{
pbAttribute->m_buttonText = "Export Preferences";
}
}
}
//--------------------------------------------------------------------------------------------------
@@ -401,8 +422,14 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
m_loggerFlushInterval.uiCapability()->setUiReadOnly( !m_loggerFilename().first );
caf::PdmUiGroup* otherGroup = uiOrdering.addNewGroup( "Other" );
otherGroup->setCollapsedByDefault();
otherGroup->add( &holoLensDisableCertificateVerification );
otherGroup->add( &m_useUndoRedo );
caf::PdmUiGroup* importExportGroup = uiOrdering.addNewGroup( "Import and Export" );
importExportGroup->setCollapsedByDefault();
importExportGroup->add( &m_importPreferences, { .newRow = false, .totalColumnSpan = 1 } );
importExportGroup->add( &m_exportPreferences, { .newRow = false, .totalColumnSpan = 1 } );
}
else if ( uiConfigName == RiaPreferences::tabNameGrid() )
{
@@ -581,6 +608,20 @@ void RiaPreferences::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
RicDeleteSumoTokenFeature::deleteUserToken();
m_deleteSumoToken = false;
}
else if ( changedField == &m_exportPreferences )
{
QString filePath = RiuFileDialogTools::getSaveFileName( nullptr, "Export Preferences", "", "XML files (*.xml);;All files(*.*)" );
exportPreferenceValuesToFile( filePath );
m_exportPreferences = false;
}
else if ( changedField == &m_importPreferences )
{
QString filePath = RiuFileDialogTools::getOpenFileName( nullptr, "Import Preferences", "", "XML files (*.xml);;All files(*.*)" );
importPreferenceValuesFromFile( filePath );
m_importPreferences = false;
}
else
{
m_summaryPreferences->fieldChangedByUi( changedField, oldValue, newValue );
@@ -1081,3 +1122,54 @@ RiaPreferencesGrid* RiaPreferences::gridPreferences() const
{
return m_gridPreferences();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPreferences::importPreferenceValuesFromFile( const QString& fileName )
{
if ( fileName.isEmpty() ) return;
QFile file( fileName );
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QTextStream in( &file );
QString fileContent = in.readAll();
file.close();
if ( !fileContent.isEmpty() )
{
this->readObjectFromXmlString( fileContent, caf::PdmDefaultObjectFactory::instance() );
}
auto txt = "Imported preferences from " + fileName;
RiaLogging::info( txt );
}
else
{
auto txt = QString( "Unable to open file: " ) + fileName;
RiaLogging::error( txt );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPreferences::exportPreferenceValuesToFile( const QString& fileName )
{
QFile file( fileName );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QTextStream out( &file );
auto content = this->writeObjectToXmlString();
out << content;
file.close();
QString txt = "Exported preferences to " + fileName;
RiaLogging::info( txt );
}
else
{
auto txt = QString( "Unable to open file: " ) + fileName;
RiaLogging::error( txt );
}
}
@@ -132,6 +132,9 @@ public:
RiaPreferencesSumo* sumoPreferences() const;
RiaPreferencesGrid* gridPreferences() const;
void importPreferenceValuesFromFile( const QString& fileName );
void exportPreferenceValuesToFile( const QString& fileName );
public:
caf::PdmField<bool> enableGrpcServer;
caf::PdmField<int> defaultGrpcPortNumber;
@@ -251,5 +254,9 @@ private:
caf::PdmField<bool> m_showInfoBox;
caf::PdmField<bool> m_showGridBox;
// Load and save preferences
caf::PdmField<bool> m_exportPreferences;
caf::PdmField<bool> m_importPreferences;
QStringList m_tabNames;
};
@@ -56,6 +56,8 @@ bool RiaArgumentParser::parseArguments( cvf::ProgramOptions* progOpt )
progOpt->registerOption( "help", "", "Displays help text and exits." );
progOpt->registerOption( "?", "", "Displays help text and exits." );
progOpt->registerOption( "preferences", "<filename>", "Import Preferences from <filename>.", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "project", "<filename>", "Open project file <filename>.", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "last", "", "Open last used project." );
progOpt->registerOption( "case",