Skip to content

Commit

Permalink
Handle variable precision in range dlg
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 22, 2021
1 parent 6a22e1f commit d98c99e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsludialog.sip.in
Expand Up @@ -35,6 +35,7 @@ Returns the upper value.
.. versionadded:: 3.21
%End
void setUpperValue( const QString &val );

};

/************************************************************************
Expand Down
31 changes: 29 additions & 2 deletions src/gui/qgsludialog.cpp
Expand Up @@ -22,6 +22,9 @@ QgsLUDialog::QgsLUDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );

connect( mLowerEdit, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, [ this ]( double value ) { setDecimalPlaces( mLowerEdit, value ); } );
connect( mUpperEdit, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, [ this ]( double value ) { setDecimalPlaces( mUpperEdit, value ); } );
}

QString QgsLUDialog::lowerValue() const
Expand All @@ -46,10 +49,34 @@ double QgsLUDialog::upperValueDouble() const

void QgsLUDialog::setLowerValue( const QString &val )
{
mLowerEdit->setValue( QLocale().toDouble( val ) );
bool ok;
const double value { QLocale().toDouble( val, &ok )};
mLowerEdit->setValue( value );
if ( ok )
{
setDecimalPlaces( mLowerEdit, value );
}
}

void QgsLUDialog::setUpperValue( const QString &val )
{
mUpperEdit->setValue( QLocale().toDouble( val ) );
bool ok;
const double value { QLocale().toDouble( val, &ok )};
mUpperEdit->setValue( value );
if ( ok )
{
setDecimalPlaces( mUpperEdit, value );
}
}

void QgsLUDialog::setDecimalPlaces( QgsDoubleSpinBox *widget, double value ) const
{
const QString strVal { QVariant( value ).toString() };
const int dotPosition( strVal.indexOf( '.' ) );
int decimals {2};
if ( dotPosition >= 0 )
{
decimals = std::max<int>( 2, strVal.length() - dotPosition - 1 );
widget->setDecimals( decimals );
}
}
4 changes: 4 additions & 0 deletions src/gui/qgsludialog.h
Expand Up @@ -48,6 +48,10 @@ class GUI_EXPORT QgsLUDialog: public QDialog, private Ui::QgsLUDialogBase
*/
double upperValueDouble() const;
void setUpperValue( const QString &val );

private:

void setDecimalPlaces( QgsDoubleSpinBox *widget, double value ) const;
};

#endif
20 changes: 19 additions & 1 deletion src/gui/symbology/qgscategorizedsymbolrendererwidget.cpp
Expand Up @@ -508,7 +508,20 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
case QVariant::Type::Double:
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 12 );
bool ok;
const QVariant value = index.data( QgsCategorizedSymbolRendererWidget::CustomRoles::ValueRole );
int decimals {2};
if ( value.toDouble( &ok ); ok )
{
const QString strVal { value.toString() };
const int dotPosition( strVal.indexOf( '.' ) );
if ( dotPosition >= 0 )
{
decimals = std::max<int>( 2, strVal.length() - dotPosition - 1 );
}
}
editor->setDecimals( decimals );
editor->setClearValue( 0 );
editor->setMaximum( std::numeric_limits<double>::max() );
editor->setMinimum( std::numeric_limits<double>::lowest() );
break;
Expand All @@ -517,6 +530,7 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 0 );
editor->setClearValue( 0 );
editor->setMaximum( std::numeric_limits<int>::max() );
editor->setMinimum( std::numeric_limits<int>::min() );
break;
Expand All @@ -525,6 +539,7 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 0 );
editor->setClearValue( 0 );
editor->setMaximum( std::numeric_limits<char>::max() );
editor->setMinimum( std::numeric_limits<char>::min() );
break;
Expand All @@ -533,6 +548,7 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 0 );
editor->setClearValue( 0 );
editor->setMaximum( std::numeric_limits<unsigned int>::max() );
editor->setMinimum( 0 );
break;
Expand All @@ -541,6 +557,7 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 0 );
editor->setClearValue( 0 );
editor->setMaximum( static_cast<double>( std::numeric_limits<qlonglong>::max() ) );
editor->setMinimum( std::numeric_limits<qlonglong>::min() );
break;
Expand All @@ -549,6 +566,7 @@ QWidget *QgsCategorizedRendererViewItemDelegate::createEditor( QWidget *parent,
{
editor = new QgsDoubleSpinBox( parent );
editor->setDecimals( 0 );
editor->setClearValue( 0 );
editor->setMaximum( static_cast<double>( std::numeric_limits<unsigned long long>::max() ) );
editor->setMinimum( 0 );
break;
Expand Down

0 comments on commit d98c99e

Please sign in to comment.