Example #1
0
Settings::Settings(const QString& filename, CfgInitializerFnType initializer)
   : m_cfgFilename(StdDir::getSysConfDir()+"/"+filename), 
     m_settings(createQSettings())
{
   QFile cfgFilename(m_cfgFilename);
   if(!cfgFilename.exists()){
      if(nullptr != initializer){
         initializer(*this);
      }
   }
}
Example #2
0
void Settings::load()
{
    const char * KEY = "SpeedCrunch";

    QSettings * settings = createQSettings( KEY );
    if ( ! settings )
        return;

    QString key = QString::fromLatin1( KEY ) + QLatin1String("/General/");

    // angle mode special case
    QString angleUnitStr;
    angleUnitStr = settings->value( key + QLatin1String("AngleMode"), "r" ).toString();
    if ( angleUnitStr != QLatin1String("r") && angleUnitStr != QLatin1String("d") )
        angleUnit = 'r';
    else
        angleUnit = angleUnitStr.at( 0 ).toAscii();

    // radix character special case
    QString radixCharStr;
    radixCharStr = settings->value( key + QLatin1String("RadixCharacter"), 0 ).toString();
    setRadixCharacter( radixCharStr.at(0).toAscii() );

    autoAns = settings->value( key + QLatin1String("AutoAns"), true ).toBool();
    autoCalc = settings->value( key + QLatin1String("AutoCalc"), true ).toBool();
    autoCompletion = settings->value( key + QLatin1String("AutoCompletion"), true ).toBool();
    historySave = settings->value( key + QLatin1String("HistorySave"), true ).toBool();
    leaveLastExpression = settings->value( key + QLatin1String("LeaveLastExpression"), false ).toBool();
    language = settings->value( key + QLatin1String("Language"), "C" ).toString();
    variableSave = settings->value( key + QLatin1String("VariableSave"), true ).toBool();
    syntaxHighlighting = settings->value( key + QLatin1String("SyntaxHighlighting"),
                                          true ).toBool();
    systemTrayIconVisible = settings->value( key + QLatin1String("SystemTrayIconVisible"),
                                             false ).toBool();
    autoResultToClipboard = settings->value( key + QLatin1String("AutoResultToClipboard"), false ).toBool();

    key = KEY + QLatin1String("/Format/");

    // format special case
    QString formatStr;
    formatStr = settings->value( key + QLatin1String("Type"), 'f' ).toString();
    if ( formatStr != "g" && formatStr != "f" && formatStr != "e" && formatStr != "n"
         && formatStr != "h" && formatStr != "o" && formatStr != "b" )
    {
        resultFormat = 'f';
    }
    else
        resultFormat = formatStr.at( 0 ).toAscii();

    resultPrecision = settings->value( key + QLatin1String("Precision"), -1 ).toInt();

    if ( resultPrecision > DECPRECISION )
        resultPrecision = DECPRECISION;

    key = KEY + QLatin1String("/Layout/");
    windowOnfullScreen = settings->value( key + QLatin1String("WindowOnFullScreen"),
                                          false ).toBool();
    historyDockVisible = settings->value( key + QLatin1String("HistoryDockVisible"),
                                          false ).toBool();
    keypadVisible = settings->value( key + QLatin1String("KeypadVisible"), true ).toBool();
    menuBarVisible = settings->value( key + QLatin1String("MenuBarVisible"), true ).toBool();
    statusBarVisible = settings->value( key + QLatin1String("StatusBarVisible"), false ).toBool();
    functionsDockVisible = settings->value( key + QLatin1String("FunctionsDockVisible"),
                                            false ).toBool();
    variablesDockVisible = settings->value( key + QLatin1String("VariablesDockVisible"),
                                            false ).toBool();
    formulaBookDockVisible = settings->value( key + QLatin1String("FormulaBookDockVisible"),
                                           false ).toBool();
    constantsDockVisible = settings->value( key + QLatin1String("ConstantsDockVisible"),
                                            false ).toBool();
    windowAlwaysOnTop = settings->value( key + QLatin1String("WindowAlwaysOnTop"),
                                         false ).toBool();

    windowPosition = settings->value( key + QLatin1String("WindowPosition"), QPoint() ).toPoint();
    windowSize = settings->value( key + QLatin1String("WindowSize"), QSize(400, 300) ).toSize();
    windowState = settings->value( key + QLatin1String("State") ).toByteArray();

    key = KEY + QLatin1String("/Display/");
    displayFont = settings->value( key + QLatin1String("DisplayFont"), QFont().toString() ).toString();
    colorScheme = settings->value( key + QLatin1String("ColorScheme"), 0 ).toInt();

    // load history
    key = KEY + QLatin1String("/History/");
    history.clear();
    int count = settings->value( key + QLatin1String("Count"), 0 ).toInt();
    for ( int i = 0; i < count; ++i ) {
        QString keyname = QString( key + QLatin1String("Expression%1") ).arg( i );
        QString str = settings->value( keyname ).toString();
        if ( ! str.isEmpty() ) {
            QString expr;
            for ( int c = 0; c < str.length(); c++ )
                if ( str.at(c) >= 32 )
                    expr.append( str.at(c) );
            history.append( expr );
        }
    }

    // load results
    historyResults.clear();
    for ( int i = 0; i < count; ++i ) {
        QString keyname = QString( key + QLatin1String("Expression%1Result") ).arg( i );
        QString str = settings->value( keyname ).toString();
        if ( ! str.isEmpty() )
            historyResults.append( str );
    }
    if ( history.count() != historyResults.count() ) {
        // avoid application crash because of new features with old settings files
        history.clear();
        historyResults.clear();
    }

    // load variables
    key = KEY + QLatin1String("/Variables/");
    variables.clear();
    settings->beginGroup( key );
    QStringList names = settings->childKeys();
    settings->endGroup();
    for ( int k = 0; k < names.count(); ++k ) {
        QString name = names.at( k );
        QString keyname = key + name;
        QString value = settings->value( keyname ).toString();
        // treat upper case escape code
        for ( int c = 0; c < name.length(); ++c ) {
            if ( name.at(c) == '_' ) {
                name.remove( c, 1 );
                if ( name.at(c) != '_' )
                    name[c] = name.at( c ).toUpper();
            }
        }
        // load
        if ( ! value.isEmpty()
             && name != QLatin1String("pi")
             && name != QLatin1String("phi")
             && name != QLatin1String("e") )
            variables.append( QString::fromLatin1("%1=%2").arg(name).arg(value) );
    }

    delete settings;
}
Example #3
0
void Settings::save()
{
    const QString KEY = QString::fromLatin1( "SpeedCrunch" );

    QSettings * settings = createQSettings( KEY );
    if ( ! settings )
        return;

    int k, i; // used on loops
    QString key = KEY + QLatin1String( "/General/" );

    settings->setValue( key + QLatin1String("HistorySave"), historySave );
    settings->setValue( key + QLatin1String("LeaveLastExpression"), leaveLastExpression );
    settings->setValue( key + QLatin1String("VariableSave"), variableSave );
    settings->setValue( key + QLatin1String("AutoCompletion"), autoCompletion );
    settings->setValue( key + QLatin1String("AutoAns"), autoAns );
    settings->setValue( key + QLatin1String("AutoCalc"), autoCalc );
    settings->setValue( key + QLatin1String("SystemTrayIconVisible"), systemTrayIconVisible );
    settings->setValue( key + QLatin1String("SyntaxHighlighting"), syntaxHighlighting );
    settings->setValue( key + QLatin1String("AutoResultToClipboard"), autoResultToClipboard );
    settings->setValue( key + QLatin1String("Language"), language );

    settings->setValue( key + QLatin1String("AngleMode"), QString(QChar(angleUnit)) );

    char c = 'C';
    if ( s_radixCharacter != 0 )
        c = s_radixCharacter;
    settings->setValue( key + QLatin1String("RadixCharacter"), QString(QChar(c)) );

    key = KEY + QLatin1String("/Format/");

    settings->setValue( key + QLatin1String("Type"), QString(QChar(resultFormat)) );
    settings->setValue( key + QLatin1String("Precision"), resultPrecision );

    key = KEY + QLatin1String("/Layout/");

    settings->setValue( key + QLatin1String("FormulaBookDockVisible"), formulaBookDockVisible );
    settings->setValue( key + QLatin1String("ConstantsDockVisible"), constantsDockVisible );
    settings->setValue( key + QLatin1String("FunctionsDockVisible"), functionsDockVisible );
    settings->setValue( key + QLatin1String("HistoryDockVisible"), historyDockVisible );
    settings->setValue( key + QLatin1String("WindowOnFullScreen"), windowOnfullScreen );
    settings->setValue( key + QLatin1String("KeypadVisible"), keypadVisible );
    settings->setValue( key + QLatin1String("MenuBarVisible"), menuBarVisible );
    settings->setValue( key + QLatin1String("StatusBarVisible"), statusBarVisible );
    settings->setValue( key + QLatin1String("VariablesDockVisible"), variablesDockVisible );
    settings->setValue( key + QLatin1String("WindowPosition"), windowPosition );
    settings->setValue( key + QLatin1String("WindowSize"), windowSize );
    settings->setValue( key + QLatin1String("WindowAlwaysOnTop"), windowAlwaysOnTop );
    settings->setValue( key + QLatin1String("State"), windowState );

    key = KEY + QLatin1String("/Display/");

    settings->setValue( key + QLatin1String("DisplayFont"), displayFont );
    settings->setValue( key + QLatin1String("ColorScheme"), colorScheme );

    // save history
    if ( historySave ) {
        key = KEY + QLatin1String("/History/");
        QStringList realHistory = history;
        QStringList realHistoryResults = historyResults;

        if ( history.count() > 100 ) {
            realHistory.clear();
            realHistoryResults.clear();
            unsigned start = history.count() - 100;
            for ( int j = start; j < history.count(); j++ ) {
                realHistory.append( history.at(j) );
                realHistoryResults.append( historyResults.at(j) );
            }
        }

        settings->beginGroup( key );
        QStringList hkeys = settings->childKeys();
        settings->endGroup();

        for ( k = 0; k < hkeys.count(); k++ ) {
            settings->remove( key + "Expression" + QString::number(k) );
            settings->remove( key + "Expression" + QString::number(k) + "Result" );
        }

        settings->setValue( key + QLatin1String("/Count/"), (int)history.count() );
        for ( i = 0; i < realHistory.count(); i++ ) {
            settings->setValue( (key + "Expression" + QString::number(i)), realHistory.at(i) );
            settings->setValue( (key + "Expression" + QString::number(i) + "Result"), realHistoryResults.at(i) );
        }
    }

    // save variables
    if ( variableSave ) {
        key = KEY + QLatin1String( "/Variables/" );
        settings->beginGroup( key );
        QStringList vkeys = settings->childKeys();
        settings->endGroup();

        for ( k = 0; k < vkeys.count(); ++k )
            //settings->remove( (key + QLatin1String("%1")).arg(vkeys.at(k)) );
            settings->remove( key + vkeys.at(k) );

        for ( i = 0; i < variables.count(); i++ ) {
            QStringList s = variables[i].split( '=' );
            if ( s.count() == 2
                 && s.at(0) != QLatin1String("pi")
                 && s.at(0) != QLatin1String("phi")
                 && s.at(0) != QLatin1String("e") )
            {
                QString name = "";
                QString value = s.at( 1 );
                int length = s.at( 0 ).length();
                for ( int c = 0; c < length; ++c ) {
                    if ( s.at(0).at(c).isUpper() || s.at(0).at(c) == '_' ) {
                        name += '_';
                        name += s.at( 0 ).at( c ).toLower();
                    } else
                        name += s.at( 0 ).at( c );
                }
                //settings->setValue( QString(key + "%1").arg(name), value );
                settings->setValue( key + name, value );
            }
        }
    }

    delete settings;
}