Move number formatting setting to CAF to enable use in UI editors

* Move RiaNumberFormat to cafPdmUiNumberFormat in caf framework
* Add unit test
This commit is contained in:
Magne Sjaastad authored and GitHub committed 2026-02-16 11:32:04 +01:00
1 parent 6cdcc4493a
commit 9aa5a3d222
26 files changed
+481 -197

No files matched your search

@@ -29,7 +29,6 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaStimPlanModelDefines.h
${CMAKE_CURRENT_LIST_DIR}/RiaThermalFractureDefines.h
${CMAKE_CURRENT_LIST_DIR}/RiaResultNames.h
${CMAKE_CURRENT_LIST_DIR}/RiaNumberFormat.h
${CMAKE_CURRENT_LIST_DIR}/RiaRftDefines.h
${CMAKE_CURRENT_LIST_DIR}/RiaDateTimeDefines.h
${CMAKE_CURRENT_LIST_DIR}/RiaPlotCollectionScheduler.h
@@ -75,7 +74,6 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaStimPlanModelDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaThermalFractureDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaResultNames.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaNumberFormat.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRftDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaDateTimeDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaPlotCollectionScheduler.cpp
@@ -1,69 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Equinor 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 "RiaNumberFormat.h"
#include "cafAppEnum.h"
template <>
void caf::AppEnum<RiaNumberFormat::NumberFormatType>::setUp()
{
addItem( RiaNumberFormat::NumberFormatType::AUTO, "AUTO", "Automatic" );
addItem( RiaNumberFormat::NumberFormatType::FIXED, "FIXED", "Fixed, decimal" );
addItem( RiaNumberFormat::NumberFormatType::SCIENTIFIC, "SCIENTIFIC", "Scientific notation" );
setDefault( RiaNumberFormat::NumberFormatType::FIXED );
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaNumberFormat::valueToText( double value, RiaNumberFormat::NumberFormatType numberFormat, int precision )
{
QString valueString;
switch ( numberFormat )
{
case RiaNumberFormat::NumberFormatType::FIXED:
valueString = QString::number( value, 'f', precision );
break;
case RiaNumberFormat::NumberFormatType::SCIENTIFIC:
valueString = QString::number( value, 'e', precision );
break;
default:
valueString = QString::number( value );
break;
}
return valueString;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaNumberFormat::sprintfFormat( RiaNumberFormat::NumberFormatType numberFormat, int precision )
{
switch ( numberFormat )
{
case RiaNumberFormat::NumberFormatType::FIXED:
return QString( "%.%1f" ).arg( precision );
case RiaNumberFormat::NumberFormatType::SCIENTIFIC:
return QString( "%.%1e" ).arg( precision );
default:
return QString( "%.%1g" ).arg( precision );
}
}
@@ -1,38 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Equinor 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QString>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiaNumberFormat
{
public:
enum class NumberFormatType
{
AUTO,
SCIENTIFIC,
FIXED
};
static QString valueToText( double value, RiaNumberFormat::NumberFormatType numberFormat, int precision );
static QString sprintfFormat( RiaNumberFormat::NumberFormatType numberFormat, int precision );
};
@@ -134,12 +134,12 @@ void RivContourMapProjectionPartMgr::appendContourLinesToModel( const cvf::Camer
cvf::ModelBasicList* model,
const caf::DisplayCoordTransform* displayCoordTransform,
const std::vector<RigContourPolygonsTools::ContourPolygons>& contourLinePolygons,
const RigContourMapGrid& contourMapGrid,
cvf::ScalarMapper* mapper,
bool showContourLines,
bool showContourLabels,
RiaNumberFormat::NumberFormatType numberFormat,
int precision )
const RigContourMapGrid& contourMapGrid,
cvf::ScalarMapper* mapper,
bool showContourLines,
bool showContourLabels,
caf::NumberFormatType numberFormat,
int precision )
{
if ( showContourLines )
{
@@ -409,7 +409,7 @@ std::vector<cvf::ref<cvf::Drawable>>
const std::vector<RigContourPolygonsTools::ContourPolygons>& contourLinePolygons,
const RigContourMapGrid& contourMapGrid,
const cvf::ScalarMapper* scalarMapper,
RiaNumberFormat::NumberFormatType numberFormat,
caf::NumberFormatType numberFormat,
int precision ) const
{
CVF_ASSERT( camera && displayCoordTransform && labelBBoxes );
@@ -438,7 +438,7 @@ std::vector<cvf::ref<cvf::Drawable>>
{
if ( contourLinePolygons[i][j].vertices.empty() ) continue;
QString qLabelText = RiaNumberFormat::valueToText( contourLinePolygons[i][j].value, numberFormat, precision );
QString qLabelText = caf::PdmUiNumberFormat::valueToText( contourLinePolygons[i][j].value, numberFormat, precision );
cvf::String labelText = cvfqt::Utils::toString( qLabelText );
size_t nVertices = contourLinePolygons[i][j].vertices.size();
@@ -20,11 +20,10 @@
#include "ContourMap/RigContourPolygonsTools.h"
#include "RiaNumberFormat.h"
#include "cafDisplayCoordTransform.h"
#include "cafPdmObject.h"
#include "cafPdmPointer.h"
#include "cafPdmUiNumberFormat.h"
#include "cvfDrawableGeo.h"
#include "cvfDrawableText.h"
@@ -63,7 +62,7 @@ public:
cvf::ScalarMapper* mapper,
bool showContourLines,
bool showContourLabels,
RiaNumberFormat::NumberFormatType numberFormat,
caf::NumberFormatType numberFormat,
int precision );
void appendPickPointVisToModel( cvf::ModelBasicList* model,
@@ -92,10 +91,10 @@ private:
const caf::DisplayCoordTransform* displayCoordTransform,
std::vector<std::vector<cvf::BoundingBox>>* labelBBoxes,
const std::vector<RigContourPolygonsTools::ContourPolygons>& contourLinePolygons,
const RigContourMapGrid& contourMapGrid,
const cvf::ScalarMapper* scalarMapper,
RiaNumberFormat::NumberFormatType numberFormat,
int precision ) const;
const RigContourMapGrid& contourMapGrid,
const cvf::ScalarMapper* scalarMapper,
caf::NumberFormatType numberFormat,
int precision ) const;
cvf::ref<cvf::DrawableGeo> createPickPointVisDrawable( const caf::DisplayCoordTransform* displayCoordTransform,
const cvf::Vec2d& pickPoint,
@@ -21,7 +21,6 @@
#include "RiaApplication.h"
#include "RiaDefines.h"
#include "RiaLogging.h"
#include "RiaNumberFormat.h"
#include "RiaPreferences.h"
#include "RiaWeightedGeometricMeanCalculator.h"
#include "RiaWeightedHarmonicMeanCalculator.h"
@@ -45,6 +44,7 @@
#include "FractureCommands/RicNewStimPlanFractureTemplateFeature.h"
#include "cafAppEnum.h"
#include "cafPdmUiNumberFormat.h"
#include "cafPdmUiTextEditor.h"
#include "cafPdmUiToolButtonEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"
@@ -1276,12 +1276,12 @@ QString RimEnsembleFractureStatistics::generateStatisticsTable(
text += "</thead>";
text += "<tbody>";
auto emptyTextOnInf = []( double value, RiaNumberFormat::NumberFormatType numberFormat, int precision )
auto emptyTextOnInf = []( double value, caf::NumberFormatType numberFormat, int precision )
{
if ( std::isinf( value ) )
return QString( "" );
else
return RiaNumberFormat::valueToText( value, numberFormat, precision );
return caf::PdmUiNumberFormat::valueToText( value, numberFormat, precision );
};
for ( auto propertyType : propertyTypes )
@@ -23,7 +23,6 @@
#include "RiaEclipseUnitTools.h"
#include "RiaFractureDefines.h"
#include "RiaLogging.h"
#include "RiaNumberFormat.h"
#include "RiaWeightedGeometricMeanCalculator.h"
#include "RiaWeightedMeanCalculator.h"
@@ -52,6 +51,7 @@
#include "cafPdmObjectScriptingCapability.h"
#include "cafPdmUiDoubleSliderEditor.h"
#include "cafPdmUiFilePathEditor.h"
#include "cafPdmUiNumberFormat.h"
#include "cafPdmUiTextEditor.h"
#include "cvfMath.h"
@@ -818,7 +818,7 @@ void RimStimPlanFractureTemplate::appendTextIfValidValue( QString&
{
QString name = caf::AppEnum<RigEnsembleFractureStatisticsCalculator::PropertyType>::uiText( propertyType );
auto [numberFormat, precision] = RigEnsembleFractureStatisticsCalculator::numberFormatForProperty( propertyType );
body += QString( "%1: %2<br>" ).arg( name ).arg( RiaNumberFormat::valueToText( value, numberFormat, precision ) );
body += QString( "%1: %2<br>" ).arg( name ).arg( caf::PdmUiNumberFormat::valueToText( value, numberFormat, precision ) );
}
}
@@ -22,7 +22,6 @@
#include "RiaApplication.h"
#include "RiaColorTables.h"
#include "RiaNumberFormat.h"
#include "RiaNumericalTools.h"
#include "RiaPreferences.h"
#include "RiaResultNames.h"
@@ -153,7 +152,7 @@ RimRegularLegendConfig::RimRegularLegendConfig()
m_significantDigitsInData = m_precision;
CAF_PDM_InitField( &m_tickNumberFormat,
"TickNumberFormat",
caf::AppEnum<RiaNumberFormat::NumberFormatType>( RiaNumberFormat::NumberFormatType::FIXED ),
caf::AppEnum<caf::NumberFormatType>( caf::NumberFormatType::FIXED ),
"Number format" );
CAF_PDM_InitField( &m_colorRangeMode_OBSOLETE, "ColorRangeMode", ColorRangeEnum( ColorRangesType::UNDEFINED ), "Colors" );
@@ -533,12 +532,12 @@ void RimRegularLegendConfig::updateLegend()
decadesInRange = cvf::Math::ceil( decadesInRange );
// Using Fixed format
RiaNumberFormat::NumberFormatType nft = m_tickNumberFormat();
caf::NumberFormatType nft = m_tickNumberFormat();
m_scalarMapperLegend->setTickFormat( (caf::OverlayScalarMapperLegend::NumberFormat)nft );
// Set the fixed number of digits after the decimal point to the number needed to show all the significant digits.
int numDecimalDigits = m_precision();
if ( nft != RiaNumberFormat::NumberFormatType::SCIENTIFIC )
if ( nft != caf::NumberFormatType::SCIENTIFIC )
{
numDecimalDigits -= static_cast<int>( decadesInRange );
}
@@ -572,7 +571,7 @@ void RimRegularLegendConfig::updateLegend()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimRegularLegendConfig::setTickNumberFormat( RiaNumberFormat::NumberFormatType numberFormat )
void RimRegularLegendConfig::setTickNumberFormat( caf::NumberFormatType numberFormat )
{
m_tickNumberFormat = numberFormat;
}
@@ -1136,13 +1135,13 @@ void RimRegularLegendConfig::updateFonts()
//--------------------------------------------------------------------------------------------------
QString RimRegularLegendConfig::valueToText( double value ) const
{
return RiaNumberFormat::valueToText( value, m_tickNumberFormat(), m_significantDigitsInData );
return caf::PdmUiNumberFormat::valueToText( value, m_tickNumberFormat(), m_significantDigitsInData );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaNumberFormat::NumberFormatType RimRegularLegendConfig::tickNumberFormat() const
caf::NumberFormatType RimRegularLegendConfig::tickNumberFormat() const
{
return m_tickNumberFormat();
}
@@ -1164,7 +1163,7 @@ void RimRegularLegendConfig::setDefaultConfigForResultName( int caseId, const QS
RimRegularLegendConfig::MappingType mappingType = MappingType::LINEAR_CONTINUOUS;
RimLegendConfig::RangeModeType rangeType = RimLegendConfig::RangeModeType::AUTOMATIC_ALLTIMESTEPS;
RiaNumberFormat::NumberFormatType numberFormat = RiaNumberFormat::NumberFormatType::FIXED;
caf::NumberFormatType numberFormat = caf::NumberFormatType::FIXED;
if ( useLog )
{
@@ -1173,7 +1172,7 @@ void RimRegularLegendConfig::setDefaultConfigForResultName( int caseId, const QS
else
mappingType = RimRegularLegendConfig::MappingType::LOG10_CONTINUOUS;
numberFormat = RiaNumberFormat::NumberFormatType::AUTO;
numberFormat = caf::NumberFormatType::AUTO;
rangeType = RimLegendConfig::RangeModeType::USER_DEFINED;
}
@@ -1203,7 +1202,7 @@ void RimRegularLegendConfig::setDefaultConfigForResultName( int caseId, const QS
{
mappingType = MappingType::LINEAR_DISCRETE;
rangeType = RimLegendConfig::RangeModeType::AUTOMATIC_ALLTIMESTEPS;
numberFormat = RiaNumberFormat::NumberFormatType::FIXED;
numberFormat = caf::NumberFormatType::FIXED;
}
resetUserDefinedValues();
@@ -20,8 +20,6 @@
#pragma once
#include "RiaNumberFormat.h"
#include "RimLegendConfig.h"
#include "RimLegendConfigChangeType.h"
@@ -29,6 +27,7 @@
#include "cvfObject.h"
#include "cafPdmPtrField.h"
#include "cafPdmUiNumberFormat.h"
#include "cafSignal.h"
#include <tuple>
@@ -128,9 +127,9 @@ public:
void setMappingMode( MappingType mappingType );
MappingType mappingMode() { return m_mappingMode(); }
void setTickNumberFormat( RiaNumberFormat::NumberFormatType numberFormat );
void setTickNumberFormat( caf::NumberFormatType numberFormat );
RiaNumberFormat::NumberFormatType tickNumberFormat() const;
caf::NumberFormatType tickNumberFormat() const;
int significantDigitsInData() const;
@@ -229,17 +228,17 @@ private:
cvf::Color3ubArray m_categoryColors;
// Fields
caf::PdmField<bool> m_showLegend;
caf::PdmField<int> m_numLevels;
caf::PdmField<int> m_precision;
caf::PdmField<caf::AppEnum<RiaNumberFormat::NumberFormatType>> m_tickNumberFormat;
caf::PdmField<RangeModeEnum> m_rangeMode;
caf::PdmField<double> m_userDefinedMaxValue;
caf::PdmField<double> m_userDefinedMinValue;
caf::PdmField<caf::AppEnum<ColorRangesType>> m_colorRangeMode_OBSOLETE;
caf::PdmField<caf::AppEnum<MappingType>> m_mappingMode;
caf::PdmField<caf::AppEnum<CategoryColorModeType>> m_categoryColorMode;
caf::PdmField<bool> m_centerLegendAroundZero;
caf::PdmField<bool> m_showLegend;
caf::PdmField<int> m_numLevels;
caf::PdmField<int> m_precision;
caf::PdmField<caf::AppEnum<caf::NumberFormatType>> m_tickNumberFormat;
caf::PdmField<RangeModeEnum> m_rangeMode;
caf::PdmField<double> m_userDefinedMaxValue;
caf::PdmField<double> m_userDefinedMinValue;
caf::PdmField<caf::AppEnum<ColorRangesType>> m_colorRangeMode_OBSOLETE;
caf::PdmField<caf::AppEnum<MappingType>> m_mappingMode;
caf::PdmField<caf::AppEnum<CategoryColorModeType>> m_categoryColorMode;
caf::PdmField<bool> m_centerLegendAroundZero;
caf::PdmPtrField<RimColorLegend*> m_colorLegend;
caf::PdmField<bool> m_selectColorLegendButton;
@@ -64,6 +64,7 @@
#include "cafPdmUiCheckBoxEditor.h"
#include "cafPdmUiComboBoxEditor.h"
#include "cafPdmUiLabelEditor.h"
#include "cafPdmUiNumberFormat.h"
#include "cafPdmUiPushButtonEditor.h"
#include "cafPdmUiTreeOrdering.h"
#include "cafPdmUiTreeSelectionEditor.h"
@@ -1879,7 +1880,7 @@ void RimSummaryMultiPlot::updateReadOutLines( double qwtTimeValue, double yValue
anno->setValue( yValue );
auto scaledValue = yValue / annotationAxis->scaleFactor();
auto valueText = RiaNumberFormat::valueToText( scaledValue, RiaNumberFormat::NumberFormatType::FIXED, 2 );
auto valueText = caf::PdmUiNumberFormat::valueToText( scaledValue, caf::NumberFormatType::FIXED, 2 );
anno->setName( valueText );
@@ -19,7 +19,6 @@
#include "RimSummaryPlotAxisFormatter.h"
#include "RiaDefines.h"
#include "RiaNumberFormat.h"
#include "Summary/RiaSummaryCurveDefinition.h"
#include "RifSummaryReaderInterface.h"
@@ -20,7 +20,6 @@
#include "RiaDefines.h"
#include "RiaEclipseUnitTools.h"
#include "RiaNumberFormat.h"
#include "RiaWeightedMeanCalculator.h"
#include "RigFractureCell.h"
@@ -434,10 +433,10 @@ std::vector<RigEnsembleFractureStatisticsCalculator::PropertyType> RigEnsembleFr
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<RiaNumberFormat::NumberFormatType, int> RigEnsembleFractureStatisticsCalculator::numberFormatForProperty( PropertyType propertyType )
std::pair<caf::NumberFormatType, int> RigEnsembleFractureStatisticsCalculator::numberFormatForProperty( PropertyType propertyType )
{
if ( propertyType == PropertyType::WIDTH )
return std::make_pair( RiaNumberFormat::NumberFormatType::FIXED, 4 );
return std::make_pair( caf::NumberFormatType::FIXED, 4 );
else
return std::make_pair( RiaNumberFormat::NumberFormatType::FIXED, 1 );
return std::make_pair( caf::NumberFormatType::FIXED, 1 );
}
@@ -20,9 +20,10 @@
#include "RiaDefines.h"
#include "RiaNumberFormat.h"
#include "RigHistogramData.h"
#include "cafPdmUiNumberFormat.h"
#include "cvfObject.h"
#include <functional>
@@ -64,7 +65,7 @@ public:
static std::vector<RigEnsembleFractureStatisticsCalculator::PropertyType> propertyTypes();
static std::pair<RiaNumberFormat::NumberFormatType, int> numberFormatForProperty( PropertyType propertyType );
static std::pair<caf::NumberFormatType, int> numberFormatForProperty( PropertyType propertyType );
private:
static std::vector<double> calculateGridStatistics( const std::vector<cvf::ref<RigStimPlanFractureDefinition>>& fractureDefinitions,
@@ -19,8 +19,6 @@
#include "RiuFemTimeHistoryResultAccessor.h"
#include "RiaNumberFormat.h"
#include "RigFemClosestResultIndexCalculator.h"
#include "RigFemPart.h"
#include "RigFemPartCollection.h"
@@ -31,6 +29,8 @@
#include "RiuGeoMechXfTensorResultAccessor.h"
#include "cafPdmUiNumberFormat.h"
#include <cmath> // Needed for HUGE_VAL on Linux
//--------------------------------------------------------------------------------------------------
@@ -101,9 +101,9 @@ QString RiuFemTimeHistoryResultAccessor::geometrySelectionText() const
cvf::Vec3d domainCoord = m_intersectionPointInDomain;
text += QString( ", ijk[%1, %2, %3] " ).arg( i ).arg( j ).arg( k );
auto xTxt = RiaNumberFormat::valueToText( domainCoord.x(), RiaNumberFormat::NumberFormatType::FIXED, 2 );
auto yTxt = RiaNumberFormat::valueToText( domainCoord.y(), RiaNumberFormat::NumberFormatType::FIXED, 2 );
auto zTxt = RiaNumberFormat::valueToText( -domainCoord.z(), RiaNumberFormat::NumberFormatType::FIXED, 2 );
auto xTxt = caf::PdmUiNumberFormat::valueToText( domainCoord.x(), caf::NumberFormatType::FIXED, 2 );
auto yTxt = caf::PdmUiNumberFormat::valueToText( domainCoord.y(), caf::NumberFormatType::FIXED, 2 );
auto zTxt = caf::PdmUiNumberFormat::valueToText( -domainCoord.z(), caf::NumberFormatType::FIXED, 2 );
QString formattedText = QString( "Intersection point : [E: %1, N: %2, Depth: %3]" ).arg( xTxt ).arg( yTxt ).arg( zTxt );
@@ -37,7 +37,7 @@ RiuScalarMapperLegendFrame::RiuScalarMapperLegendFrame( QWidget* parent, const Q
: RiuAbstractLegendFrame( parent, title )
, m_scalarMapper( scalarMapper )
, m_tickNumberPrecision( 4 )
, m_numberFormat( RiaNumberFormat::NumberFormatType::AUTO )
, m_numberFormat( caf::NumberFormatType::AUTO )
{
updateTickValues();
}
@@ -60,7 +60,7 @@ void RiuScalarMapperLegendFrame::setTickPrecision( int precision )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuScalarMapperLegendFrame::setTickFormat( NumberFormat format )
void RiuScalarMapperLegendFrame::setTickFormat( caf::NumberFormatType format )
{
m_numberFormat = format;
}
@@ -145,7 +145,7 @@ void RiuScalarMapperLegendFrame::layoutInfo( LayoutInfo* layout ) const
QString RiuScalarMapperLegendFrame::label( int index ) const
{
double tickValue = m_tickValues[index];
return RiaNumberFormat::valueToText( tickValue, m_numberFormat, m_tickNumberPrecision );
return caf::PdmUiNumberFormat::valueToText( tickValue, m_numberFormat, m_tickNumberPrecision );
}
//--------------------------------------------------------------------------------------------------
@@ -17,10 +17,10 @@
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RiaNumberFormat.h"
#include "RiuAbstractLegendFrame.h"
#include "cafPdmUiNumberFormat.h"
#include "cvfObject.h"
#include "cvfRect.h"
#include "cvfScalarMapper.h"
@@ -37,15 +37,12 @@ class RiuScalarMapperLegendFrame : public RiuAbstractLegendFrame
{
Q_OBJECT
public:
using NumberFormat = RiaNumberFormat::NumberFormatType;
public:
RiuScalarMapperLegendFrame( QWidget* parent, const QString& title, cvf::ScalarMapper* scalarMapper );
~RiuScalarMapperLegendFrame() override;
void setTickPrecision( int precision );
void setTickFormat( NumberFormat format );
void setTickFormat( caf::NumberFormatType format );
void updateTickValues();
void setScalarMapper( cvf::ScalarMapper* scalarMapper );
@@ -61,5 +58,5 @@ private:
cvf::cref<cvf::ScalarMapper> m_scalarMapper;
std::vector<double> m_tickValues;
int m_tickNumberPrecision;
NumberFormat m_numberFormat;
caf::NumberFormatType m_numberFormat;
};
@@ -67,6 +67,8 @@ set(PROJECT_FILES
cafPdmUiLabelEditor.cpp
cafPdmUiCheckBoxAndTextEditor.h
cafPdmUiCheckBoxAndTextEditor.cpp
cafPdmUiNumberFormat.h
cafPdmUiNumberFormat.cpp
cafPdmUiValueRangeEditor.h
cafPdmUiValueRangeEditor.cpp
# object editors
@@ -41,6 +41,7 @@
#include "cafPdmUiDefaultObjectEditor.h"
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiLineEditor.h"
#include "cafPdmUiNumberFormat.h"
#include <QHBoxLayout>
#include <QLabel>
@@ -62,6 +63,24 @@ void PdmUiCheckBoxAndTextEditor::configureAndUpdateUi( const QString& uiConfigNa
m_lineEdit->setToolTip( uiField()->uiToolTip( uiConfigName ) );
// Read display format from map-based attributes
int decimals = -1;
int numberFormat = static_cast<int>( caf::NumberFormatType::AUTO );
if ( auto uiItem = uiField() )
{
if ( auto val = uiItem->attribute<int>( Keys::DECIMALS, uiConfigName ) )
{
decimals = val.value();
}
if ( auto val = uiItem->attribute<int>( Keys::NUMBER_FORMAT, uiConfigName ) )
{
numberFormat = val.value();
}
uiItem->validateAttributes( "PdmUiCheckBoxAndTextEditor", SUPPORTED_ATTRIBUTES, uiConfigName );
}
bool isChecked = false;
QString textString;
@@ -72,8 +91,19 @@ void PdmUiCheckBoxAndTextEditor::configureAndUpdateUi( const QString& uiConfigNa
QList<QVariant> lst = variantValue.toList();
if ( lst.size() == 2 )
{
isChecked = lst[0].toBool();
textString = lst[1].toString();
isChecked = lst[0].toBool();
bool valueOk = false;
double value = lst[1].toDouble( &valueOk );
if ( valueOk && decimals >= 0 )
{
auto fmt = static_cast<NumberFormatType>( numberFormat );
textString = PdmUiNumberFormat::valueToText( value, fmt, decimals );
}
else
{
textString = lst[1].toString();
}
}
}
@@ -45,6 +45,8 @@
#include <QString>
#include <QWidget>
#include <set>
namespace caf
{
//==================================================================================================
@@ -59,6 +61,16 @@ public:
PdmUiCheckBoxAndTextEditor() {}
~PdmUiCheckBoxAndTextEditor() override {}
// Attribute key constants for compile-time safety and discoverability
struct Keys
{
static inline const QString DECIMALS = QStringLiteral( "decimals" );
static inline const QString NUMBER_FORMAT = QStringLiteral( "numberFormat" );
};
// Set of all supported attributes for validation
inline static const std::set<QString> SUPPORTED_ATTRIBUTES = { Keys::DECIMALS, Keys::NUMBER_FORMAT };
protected:
QWidget* createEditorWidget( QWidget* parent ) override;
void configureAndUpdateUi( const QString& uiConfigName ) override;
@@ -36,6 +36,8 @@
#include "cafPdmUiDoubleValueEditor.h"
#include "cafPdmUiNumberFormat.h"
#include "cafFactory.h"
#include "cafPdmField.h"
#include "cafPdmLogging.h"
@@ -97,7 +99,7 @@ void PdmUiDoubleValueEditor::configureAndUpdateUi( const QString& uiConfigName )
if ( auto val = uiItem->attribute<int>( Keys::NUMBER_FORMAT, uiConfigName ) )
{
m_attributes.m_numberFormat = static_cast<PdmUiDoubleValueEditorAttribute::NumberFormat>( val.value() );
m_attributes.m_numberFormat = static_cast<NumberFormatType>( val.value() );
}
// Validate: warn about unsupported attributes
@@ -109,12 +111,7 @@ void PdmUiDoubleValueEditor::configureAndUpdateUi( const QString& uiConfigName )
QString textValue;
if ( valueOk )
{
if ( m_attributes.m_numberFormat == PdmUiDoubleValueEditorAttribute::NumberFormat::FIXED )
textValue = QString::number( value, 'f', m_attributes.m_decimals );
else if ( m_attributes.m_numberFormat == PdmUiDoubleValueEditorAttribute::NumberFormat::SCIENTIFIC )
textValue = QString::number( value, 'e', m_attributes.m_decimals );
else
textValue = QString::number( value, 'g', m_attributes.m_decimals );
textValue = PdmUiNumberFormat::valueToText( value, m_attributes.m_numberFormat, m_attributes.m_decimals );
}
else
{
@@ -37,6 +37,7 @@
#pragma once
#include "cafPdmUiFieldLabelEditorHandle.h"
#include "cafPdmUiNumberFormat.h"
#include <QDoubleValidator>
#include <QGroupBox>
@@ -54,22 +55,16 @@ namespace caf
class PdmUiDoubleValueEditorAttribute : public PdmUiEditorAttribute
{
public:
enum class NumberFormat
{
FIXED,
SCIENTIFIC,
AUTOMATIC
};
PdmUiDoubleValueEditorAttribute()
{
m_decimals = 6;
m_numberFormat = NumberFormat::AUTOMATIC;
m_numberFormat = NumberFormatType::AUTO;
}
void setFixedWithTwoDecimals()
{
m_decimals = 2;
m_numberFormat = NumberFormat::FIXED;
m_numberFormat = NumberFormatType::FIXED;
}
// Convenience function to set the number format to fixed with two decimals
@@ -83,7 +78,7 @@ public:
public:
int m_decimals;
NumberFormat m_numberFormat;
NumberFormatType m_numberFormat;
QPointer<QDoubleValidator> m_validator;
};
@@ -0,0 +1,47 @@
#include "cafPdmUiNumberFormat.h"
#include "cafAppEnum.h"
template <>
void caf::AppEnum<caf::NumberFormatType>::setUp()
{
addItem( caf::NumberFormatType::AUTO, "AUTO", "Automatic" );
addItem( caf::NumberFormatType::FIXED, "FIXED", "Fixed, decimal" );
addItem( caf::NumberFormatType::SCIENTIFIC, "SCIENTIFIC", "Scientific notation" );
setDefault( caf::NumberFormatType::FIXED );
};
namespace caf
{
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmUiNumberFormat::valueToText( double value, NumberFormatType numberFormat, int precision )
{
switch ( numberFormat )
{
case NumberFormatType::FIXED:
return QString::number( value, 'f', precision );
case NumberFormatType::SCIENTIFIC:
return QString::number( value, 'e', precision );
default:
return QString::number( value );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmUiNumberFormat::sprintfFormat( NumberFormatType numberFormat, int precision )
{
switch ( numberFormat )
{
case NumberFormatType::FIXED:
return QString( "%.%1f" ).arg( precision );
case NumberFormatType::SCIENTIFIC:
return QString( "%.%1e" ).arg( precision );
default:
return QString( "%.%1g" ).arg( precision );
}
}
} // namespace caf
@@ -0,0 +1,20 @@
#pragma once
#include <QString>
namespace caf
{
enum class NumberFormatType
{
AUTO,
SCIENTIFIC,
FIXED
};
class PdmUiNumberFormat
{
public:
static QString valueToText( double value, NumberFormatType numberFormat, int precision );
static QString sprintfFormat( NumberFormatType numberFormat, int precision );
};
} // namespace caf
@@ -13,8 +13,10 @@ qt_standard_project_setup()
include_directories(${CMAKE_CURRENT_SOURCE_DIR} # required for gtest-all.cpp
)
set(PROJECT_FILES cafUserInterface_UnitTests.cpp cafPdmUiTreeViewModelTest.cpp
cafPdmUiTreeSelectionQModelTest.cpp gtest/gtest-all.cpp
set(PROJECT_FILES
cafUserInterface_UnitTests.cpp cafPdmUiTreeViewModelTest.cpp
cafPdmUiTreeSelectionQModelTest.cpp cafPdmUiNumberFormatTest.cpp
cafPdmUiCheckBoxAndTextEditorTest.cpp gtest/gtest-all.cpp
)
qt_add_executable(${PROJECT_NAME} ${PROJECT_FILES})
@@ -0,0 +1,243 @@
#include "gtest/gtest.h"
#include "cafInternalPdmValueFieldSpecializations.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "cafPdmUiCheckBoxAndTextEditor.h"
#include "cafPdmUiNumberFormat.h"
#include <QCheckBox>
#include <QLineEdit>
#include <utility>
using namespace caf;
class CheckBoxAndTextTestObj : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
CheckBoxAndTextTestObj()
{
CAF_PDM_InitObject( "CheckBoxAndTextTestObj" );
auto defaultPair = std::make_pair( false, QString( "" ) );
CAF_PDM_InitField( &m_checkableText, "CheckableText", defaultPair, "Checkable Text" );
auto doublePair = std::make_pair( true, 3.14 );
CAF_PDM_InitField( &m_checkableDouble, "CheckableDouble", doublePair, "Checkable Double" );
}
caf::PdmField<std::pair<bool, QString>> m_checkableText;
caf::PdmField<std::pair<bool, double>> m_checkableDouble;
};
CAF_PDM_SOURCE_INIT( CheckBoxAndTextTestObj, "CheckBoxAndTextTestObj" );
//--------------------------------------------------------------------------------------------------
/// Test default field values for pair<bool, QString>
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, DefaultValues )
{
CheckBoxAndTextTestObj obj;
EXPECT_EQ( obj.m_checkableText().first, false );
EXPECT_EQ( obj.m_checkableText().second.toStdString(), "" );
EXPECT_EQ( obj.m_checkableDouble().first, true );
EXPECT_DOUBLE_EQ( obj.m_checkableDouble().second, 3.14 );
}
//--------------------------------------------------------------------------------------------------
/// Test setting and getting pair<bool, QString> field values
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, SetAndGetPairValues )
{
CheckBoxAndTextTestObj obj;
obj.m_checkableText = std::make_pair( true, QString( "hello" ) );
EXPECT_EQ( obj.m_checkableText().first, true );
EXPECT_EQ( obj.m_checkableText().second.toStdString(), "hello" );
obj.m_checkableText = std::make_pair( false, QString( "world" ) );
EXPECT_EQ( obj.m_checkableText().first, false );
EXPECT_EQ( obj.m_checkableText().second.toStdString(), "world" );
}
//--------------------------------------------------------------------------------------------------
/// Test QVariant conversion round-trip for pair<bool, QString>
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, QVariantRoundTrip )
{
auto original = std::make_pair( true, QString( "test value" ) );
QVariant variant = PdmValueFieldSpecialization<std::pair<bool, QString>>::convert( original );
EXPECT_TRUE( variant.canConvert<QList<QVariant>>() );
QList<QVariant> lst = variant.toList();
EXPECT_EQ( lst.size(), 2 );
EXPECT_EQ( lst[0].toBool(), true );
EXPECT_EQ( lst[1].toString().toStdString(), "test value" );
std::pair<bool, QString> restored;
PdmValueFieldSpecialization<std::pair<bool, QString>>::setFromVariant( variant, restored );
EXPECT_EQ( restored.first, original.first );
EXPECT_EQ( restored.second, original.second );
}
//--------------------------------------------------------------------------------------------------
/// Test QVariant conversion for unchecked state
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, QVariantUncheckedState )
{
auto original = std::make_pair( false, QString( "disabled text" ) );
QVariant variant = PdmValueFieldSpecialization<std::pair<bool, QString>>::convert( original );
QList<QVariant> lst = variant.toList();
EXPECT_EQ( lst[0].toBool(), false );
EXPECT_EQ( lst[1].toString().toStdString(), "disabled text" );
}
//--------------------------------------------------------------------------------------------------
/// Test number formatting used by the editor for double values
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, NumberFormattingForDoubleValues )
{
auto pair = std::make_pair( true, 1234.5678 );
QVariant variant = PdmValueFieldSpecialization<std::pair<bool, double>>::convert( pair );
QList<QVariant> lst = variant.toList();
EXPECT_EQ( lst.size(), 2 );
bool valueOk = false;
double value = lst[1].toDouble( &valueOk );
EXPECT_TRUE( valueOk );
// Test the formatting path used in configureAndUpdateUi
QString fixedText = PdmUiNumberFormat::valueToText( value, NumberFormatType::FIXED, 2 );
EXPECT_EQ( fixedText.toStdString(), "1234.57" );
QString scientificText = PdmUiNumberFormat::valueToText( value, NumberFormatType::SCIENTIFIC, 3 );
EXPECT_EQ( scientificText.toStdString(), "1.235e+03" );
}
//--------------------------------------------------------------------------------------------------
/// Test QVariant round-trip for pair<bool, double>
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, QVariantRoundTripDouble )
{
auto original = std::make_pair( true, 42.5 );
QVariant variant = PdmValueFieldSpecialization<std::pair<bool, double>>::convert( original );
std::pair<bool, double> restored;
PdmValueFieldSpecialization<std::pair<bool, double>>::setFromVariant( variant, restored );
EXPECT_EQ( restored.first, original.first );
EXPECT_DOUBLE_EQ( restored.second, original.second );
}
//--------------------------------------------------------------------------------------------------
/// Test field uiValue returns correct QVariant list
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, FieldUiValue )
{
CheckBoxAndTextTestObj obj;
obj.m_checkableText = std::make_pair( true, QString( "ui test" ) );
QVariant uiValue = obj.m_checkableText.uiCapability()->uiValue();
EXPECT_TRUE( uiValue.canConvert<QList<QVariant>>() );
QList<QVariant> lst = uiValue.toList();
EXPECT_EQ( lst.size(), 2 );
EXPECT_EQ( lst[0].toBool(), true );
EXPECT_EQ( lst[1].toString().toStdString(), "ui test" );
}
//--------------------------------------------------------------------------------------------------
/// Test configureAndUpdateUi with checked string field
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, ConfigureAndUpdateUi_CheckedStringField )
{
CheckBoxAndTextTestObj obj;
obj.m_checkableText = std::make_pair( true, QString( "my text" ) );
QWidget parentWidget;
PdmUiCheckBoxAndTextEditor editor;
editor.setUiField( obj.m_checkableText.uiCapability() );
editor.createWidgets( &parentWidget );
editor.updateUi();
auto* editorWidget = editor.editorWidget();
auto* checkBox = editorWidget->findChild<QCheckBox*>();
auto* lineEdit = editorWidget->findChild<QLineEdit*>();
ASSERT_NE( checkBox, nullptr );
ASSERT_NE( lineEdit, nullptr );
EXPECT_TRUE( checkBox->isChecked() );
EXPECT_EQ( lineEdit->text().toStdString(), "my text" );
EXPECT_TRUE( lineEdit->isEnabled() );
}
//--------------------------------------------------------------------------------------------------
/// Test configureAndUpdateUi with unchecked field disables line edit
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, ConfigureAndUpdateUi_UncheckedDisablesLineEdit )
{
CheckBoxAndTextTestObj obj;
obj.m_checkableText = std::make_pair( false, QString( "disabled text" ) );
QWidget parentWidget;
PdmUiCheckBoxAndTextEditor editor;
editor.setUiField( obj.m_checkableText.uiCapability() );
editor.createWidgets( &parentWidget );
editor.updateUi();
auto* editorWidget = editor.editorWidget();
auto* checkBox = editorWidget->findChild<QCheckBox*>();
auto* lineEdit = editorWidget->findChild<QLineEdit*>();
ASSERT_NE( checkBox, nullptr );
ASSERT_NE( lineEdit, nullptr );
EXPECT_FALSE( checkBox->isChecked() );
EXPECT_EQ( lineEdit->text().toStdString(), "disabled text" );
EXPECT_FALSE( lineEdit->isEnabled() );
}
//--------------------------------------------------------------------------------------------------
/// Test configureAndUpdateUi with double field and number formatting attributes
//--------------------------------------------------------------------------------------------------
TEST( PdmUiCheckBoxAndTextEditorTest, ConfigureAndUpdateUi_DoubleWithNumberFormat )
{
CheckBoxAndTextTestObj obj;
obj.m_checkableDouble = std::make_pair( true, 1234.5678 );
obj.m_checkableDouble.uiCapability()->setAttribute( PdmUiCheckBoxAndTextEditor::Keys::DECIMALS, 2 );
obj.m_checkableDouble.uiCapability()->setAttribute( PdmUiCheckBoxAndTextEditor::Keys::NUMBER_FORMAT,
static_cast<int>( NumberFormatType::FIXED ) );
QWidget parentWidget;
PdmUiCheckBoxAndTextEditor editor;
editor.setUiField( obj.m_checkableDouble.uiCapability() );
editor.createWidgets( &parentWidget );
editor.updateUi();
auto* editorWidget = editor.editorWidget();
auto* checkBox = editorWidget->findChild<QCheckBox*>();
auto* lineEdit = editorWidget->findChild<QLineEdit*>();
ASSERT_NE( checkBox, nullptr );
ASSERT_NE( lineEdit, nullptr );
EXPECT_TRUE( checkBox->isChecked() );
EXPECT_EQ( lineEdit->text().toStdString(), "1234.57" );
EXPECT_TRUE( lineEdit->isEnabled() );
}
@@ -0,0 +1,51 @@
#include "gtest/gtest.h"
#include "cafPdmUiNumberFormat.h"
using namespace caf;
TEST( PdmUiNumberFormatTest, ValueToTextFixed )
{
QString result = PdmUiNumberFormat::valueToText( 3.14159, NumberFormatType::FIXED, 2 );
EXPECT_EQ( result.toStdString(), "3.14" );
result = PdmUiNumberFormat::valueToText( 0.0, NumberFormatType::FIXED, 3 );
EXPECT_EQ( result.toStdString(), "0.000" );
result = PdmUiNumberFormat::valueToText( -1.5, NumberFormatType::FIXED, 1 );
EXPECT_EQ( result.toStdString(), "-1.5" );
}
TEST( PdmUiNumberFormatTest, ValueToTextScientific )
{
QString result = PdmUiNumberFormat::valueToText( 1000.0, NumberFormatType::SCIENTIFIC, 2 );
EXPECT_EQ( result.toStdString(), "1.00e+03" );
result = PdmUiNumberFormat::valueToText( 0.001, NumberFormatType::SCIENTIFIC, 1 );
EXPECT_EQ( result.toStdString(), "1.0e-03" );
}
TEST( PdmUiNumberFormatTest, ValueToTextAuto )
{
QString result = PdmUiNumberFormat::valueToText( 42.0, NumberFormatType::AUTO, 6 );
EXPECT_EQ( result.toStdString(), "42" );
}
TEST( PdmUiNumberFormatTest, SprintfFormatFixed )
{
QString result = PdmUiNumberFormat::sprintfFormat( NumberFormatType::FIXED, 3 );
EXPECT_EQ( result.toStdString(), "%.3f" );
}
TEST( PdmUiNumberFormatTest, SprintfFormatScientific )
{
QString result = PdmUiNumberFormat::sprintfFormat( NumberFormatType::SCIENTIFIC, 2 );
EXPECT_EQ( result.toStdString(), "%.2e" );
}
TEST( PdmUiNumberFormatTest, SprintfFormatAuto )
{
QString result = PdmUiNumberFormat::sprintfFormat( NumberFormatType::AUTO, 4 );
EXPECT_EQ( result.toStdString(), "%.4g" );
}