[Developers] Storing user-specific settings

Bert Bril Bert.Bril at opendtect.org
Fri Apr 24 13:44:41 CEST 2009


Hi all,


In many places there are things that are specific per user. Colors,
default values, and so forth.

For these defaults per user, there is the 'Settings' object
(Basic/settings.h). It is basically an IOPar which is automatically
loaded and stored in the right place with the right file name.

For OpendTect-base things there is the instance 'Settings::common()',
which for example has:
SEG-Y.Examine.Number of traces: 1500
The value is read when the SEG-Y 'wizard' starts:

    int nrex = 100;
    Settings::common().get( sKeySettNrTrcExamine, nrex );

When the user checks the 'Save as default' box the value will be saved:

    const int nrex = nrTrcExamine();
    Settings::common().set( sKeySettNrTrcExamine, nrex );
    Settings::common().write();

The Settings::common() instance is meant for OpendTect-base. If you want
to store/retrieve deaults for your specific plugin, all you have to do
is ask for an instance. If there was already a file, it will be read.
Otherwise, a new one is created. Thus you'd have:

    Setings::fetch( "MeMySelfAndI" ).get( "The Value", val );

and

    Settings& mysetts = Setings::fetch( "MeMySelfAndI" );
    mysetts.set( "The Value", val );
    mysetts.write();

A nice example is when you want to display a certain message once ever
per user. Like what the actual meaning of something is. Displaying it
again would be annoying. That could be done with:

    static const char* skey = "Inversion.Complicated Message Done";
    Settings& setts = Settings::fetch( "FooBarCompany" );
    if ( !setts.isTrue(skey) )
    {
        setts.setYN( skey, true );
        setts.write();
        if ( !uiMSG().askGoOn( "This is awfully complicated!"
                               "\n\nDo you want to continue?" ) )
            return false;
    }




-- Bert

-- 
-- Bert Bril / OpendTect developer at dGB
-- mailto:Bert.Bril at opendtect.org , http://opendtect.org





More information about the Developers mailing list