/*
    Generate an floating point number given a value, using formatting defined within this class.
    The value must be a single variant.
    This is used when formatting a single value, or for each value in an array of values.
*/
double QEFloatingFormatting::formatFloatingNonArray( const QVariant &value ) {

    // Determine the format from the variant type.
    // Only the types used to store ca data are used. any other type is considered a failure.
    switch( value.type() ) {
        case QVariant::Double :
        {
            return value.toDouble(); // No conversion requried. Stored in variant as required type
        }
        case QVariant::LongLong :
        {
            return formatFromInteger( value );
        }
        case QVariant::ULongLong :
        {
            return formatFromUnsignedInteger( value );
        }
        case QVariant::String :
        {
            return formatFromString( value );
        }
        default :
        {
            return formatFailure( QString( "Bug in QEFloatingFormatting::formatFloating(). The QVariant type was not expected" ) );
        }
    }
}
/*
    Generate a string given an element value, using formatting defined within this class.
*/
QString QEStringFormatting::formatElementString( const QVariant& value ) {
    // Examine the value and note the matching format
    determineDbFormat( value );

    // Initialise
    bool errorMessage = false;      // Set if an error message is the result
    outStr.clear();

    // Set the precision
    if( useDbPrecision )
        stream.setRealNumberPrecision( dbPrecision );
    else
        stream.setRealNumberPrecision( precision );

    // Format the value as requested
    switch( format )
    {
        // Determine the format from the variant type.
        // Only the types used to store ca data are used. any other type is
        // considered a failure.
        case FORMAT_DEFAULT :
        {
            bool haveEnumeratedString = false;  // Successfully converted the value to an enumerated string
            // If a list of enumerated strings is available, attempt to get an enumerated string
            if( dbEnumerations.size() )
            {
                // Ensure the input value can be used as an index into the list of enumerated strings
                bool convertOk;
                long lValue = value.toLongLong( &convertOk );
                if( convertOk && lValue >= 0 )
                {
                    // Get the appropriate enumerated string
                    if( lValue < dbEnumerations.size() )
                    {
                        outStr = dbEnumerations[lValue];
                        haveEnumeratedString = true;
                    }
                    // NOTE: STAT field hard-coded values now set up in QCaObject.cpp - extra values appended to dbEnumerations.
                }
            }

            // If no list of enumerated strings was available, or a string could not be selected,
            // convert the value based on it's type.
            if( !haveEnumeratedString )
            {
                switch( dbFormat )
                {
                case FORMAT_FLOATING:
                    formatFromFloating( value );
                    break;

                case FORMAT_INTEGER:
                    formatFromInteger( value );
                    break;

                case FORMAT_UNSIGNEDINTEGER:
                    formatFromUnsignedInteger( value );
                    break;

                case FORMAT_STRING:
                    formatFromString( value );
                    break;

                default:
                    formatFailure( QString( "Bug in QEStringFormatting::formatString(). The QVariant type was not expected" ) );
                    errorMessage = true;
                    break;
                }
            }
            break;
        }

        // Format as requested, ignoring the database type
        case FORMAT_FLOATING:
            formatFromFloating( value );
            break;

         case FORMAT_INTEGER:
            formatFromInteger( value );
            break;

        case FORMAT_UNSIGNEDINTEGER:
            formatFromUnsignedInteger( value );
            break;

        case FORMAT_LOCAL_ENUMERATE:
            formatFromEnumeration( value );
            break;

        case FORMAT_TIME:
            formatFromTime( value );
            break;

        case FORMAT_STRING:
            formatFromString( value );
            break;

        // Don't know how to format.
        // This is a code error. All cases in QEStringFormatting::formats should be catered for
        default:
            formatFailure( QString( "Bug in QEStringFormatting::format(). The format type was not expected" ) );
            errorMessage = true;
            break;
    }

    return outStr;
}