Skip to content
Permalink
Browse files

Use switches for enums, combobox data instead of index positions

Both are more future-proof
  • Loading branch information
nyalldawson committed Sep 14, 2018
1 parent 8f90791 commit 0f15735945786067bd570564e44c4de5e92499b4
@@ -918,36 +918,46 @@ void QgsGraduatedSymbolRenderer::updateClasses( QgsVectorLayer *vlayer, Mode mod
QList<double> breaks;
QList<double> labels;

if ( mode == EqualInterval )
switch ( mode )
{
breaks = QgsGraduatedSymbolRenderer::calcEqualIntervalBreaks( minimum, maximum, nclasses, mUseSymmetricMode, symmetryPoint, astride );
}
else if ( mode == Pretty )
{
breaks = QgsSymbolLayerUtils::prettyBreaks( minimum, maximum, nclasses );
setListForCboPrettyBreaks( _breaksAsStrings( breaks ) );
case EqualInterval:
{
breaks = QgsGraduatedSymbolRenderer::calcEqualIntervalBreaks( minimum, maximum, nclasses, mUseSymmetricMode, symmetryPoint, astride );
break;
}

if ( useSymmetricMode )
QgsGraduatedSymbolRenderer::makeBreaksSymmetric( breaks, symmetryPoint, astride );
}
else if ( mode == Quantile || mode == Jenks || mode == StdDev )
{
// get values from layer
if ( !valuesLoaded )
case Pretty:
{
values = QgsVectorLayerUtils::getDoubleValues( vlayer, mAttrName, ok );
breaks = QgsSymbolLayerUtils::prettyBreaks( minimum, maximum, nclasses );
setListForCboPrettyBreaks( _breaksAsStrings( breaks ) );

if ( useSymmetricMode )
QgsGraduatedSymbolRenderer::makeBreaksSymmetric( breaks, symmetryPoint, astride );
break;
}
// calculate the breaks
if ( mode == Quantile )
breaks = _calcQuantileBreaks( values, nclasses );
else if ( mode == Jenks )
breaks = _calcJenksBreaks( values, nclasses, minimum, maximum );
else if ( mode == StdDev )
breaks = _calcStdDevBreaks( values, nclasses, labels, mUseSymmetricMode, symmetryPoint, astride );
}
else
{
Q_ASSERT( false );

case Quantile:
case Jenks:
case StdDev:
{
// get values from layer
if ( !valuesLoaded )
{
values = QgsVectorLayerUtils::getDoubleValues( vlayer, mAttrName, ok );
}
// calculate the breaks
if ( mode == Quantile )
breaks = _calcQuantileBreaks( values, nclasses );
else if ( mode == Jenks )
breaks = _calcJenksBreaks( values, nclasses, minimum, maximum );
else if ( mode == StdDev )
breaks = _calcStdDevBreaks( values, nclasses, labels, mUseSymmetricMode, symmetryPoint, astride );
break;
}

case Custom:
Q_ASSERT( false );
break;
}

double lower, upper = minimum;
@@ -1188,16 +1198,26 @@ QDomElement QgsGraduatedSymbolRenderer::save( QDomDocument &doc, const QgsReadWr

// save mode
QString modeString;
if ( mMode == EqualInterval )
modeString = QStringLiteral( "equal" );
else if ( mMode == Quantile )
modeString = QStringLiteral( "quantile" );
else if ( mMode == Jenks )
modeString = QStringLiteral( "jenks" );
else if ( mMode == StdDev )
modeString = QStringLiteral( "stddev" );
else if ( mMode == Pretty )
modeString = QStringLiteral( "pretty" );
switch ( mMode )
{
case EqualInterval:
modeString = QStringLiteral( "equal" );
break;
case Quantile:
modeString = QStringLiteral( "quantile" );
break;
case Jenks:
modeString = QStringLiteral( "jenks" );
break;
case StdDev:
modeString = QStringLiteral( "stddev" );
break;
case Pretty:
modeString = QStringLiteral( "pretty" );
break;
case Custom:
break;
}
if ( !modeString.isEmpty() )
{
QDomElement modeElem = doc.createElement( QStringLiteral( "mode" ) );
@@ -449,6 +449,13 @@ QgsGraduatedSymbolRendererWidget::QgsGraduatedSymbolRendererWidget( QgsVectorLay

// setup user interface
setupUi( this );

cboGraduatedMode->addItem( tr( "Equal Interval" ), QgsGraduatedSymbolRenderer::EqualInterval );
cboGraduatedMode->addItem( tr( "Quantile (Equal Count)" ), QgsGraduatedSymbolRenderer::Quantile );
cboGraduatedMode->addItem( tr( "Natural Breaks (Jenks)" ), QgsGraduatedSymbolRenderer::Jenks );
cboGraduatedMode->addItem( tr( "Standard Deviation" ), QgsGraduatedSymbolRenderer::StdDev );
cboGraduatedMode->addItem( tr( "Pretty Breaks" ), QgsGraduatedSymbolRenderer::Pretty );

connect( methodComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::methodComboBox_currentIndexChanged );
this->layout()->setContentsMargins( 0, 0, 0, 0 );

@@ -607,38 +614,50 @@ void QgsGraduatedSymbolRendererWidget::updateUiFromRenderer( bool updateCount )
spinSymmetryPointForOtherMethods->setShowClearButton( false );

// update UI from the graduated renderer (update combo boxes, view)
if ( mRenderer->mode() < cboGraduatedMode->count() )
if ( cboGraduatedMode->findData( mRenderer->mode() ) >= 0 )
{
cboGraduatedMode->setCurrentIndex( mRenderer->mode() );
cboGraduatedMode->setCurrentIndex( cboGraduatedMode->findData( mRenderer->mode() ) );
}

// symmetric classification
if ( cboGraduatedMode->currentIndex() == 0 || cboGraduatedMode->currentIndex() == 3 ) // EqualInterval or StdDev
{
cbxClassifySymmetric->setVisible( true );
cbxAstride->setVisible( true );
cboSymmetryPointForPretty->setVisible( false );
spinSymmetryPointForOtherMethods->setVisible( true );
spinSymmetryPointForOtherMethods->setValue( mRenderer->symmetryPoint() );
}
else if ( cboGraduatedMode->currentIndex() == 4 ) // Pretty
{
cbxClassifySymmetric->setVisible( true );
cbxAstride->setVisible( true );
spinSymmetryPointForOtherMethods->setVisible( false );
cboSymmetryPointForPretty->setVisible( true );
cboSymmetryPointForPretty->clear();
cboSymmetryPointForPretty->addItems( mRenderer->listForCboPrettyBreaks() );
// replace the combobox on the good old value
cboSymmetryPointForPretty->setCurrentText( QString::number( mRenderer->symmetryPoint(), 'f', 2 ) );
}
else // quantile or Jenks
const QgsGraduatedSymbolRenderer::Mode cboMode = static_cast< QgsGraduatedSymbolRenderer::Mode >( cboGraduatedMode->currentData().toInt() );
switch ( cboMode )
{
cbxClassifySymmetric->setVisible( false );
cbxAstride->setVisible( false );
cboSymmetryPointForPretty->setVisible( false );
spinSymmetryPointForOtherMethods->setVisible( false );
spinSymmetryPointForOtherMethods->setValue( mRenderer->symmetryPoint() );
case QgsGraduatedSymbolRenderer::EqualInterval:
case QgsGraduatedSymbolRenderer::StdDev:
{
cbxClassifySymmetric->setVisible( true );
cbxAstride->setVisible( true );
cboSymmetryPointForPretty->setVisible( false );
spinSymmetryPointForOtherMethods->setVisible( true );
spinSymmetryPointForOtherMethods->setValue( mRenderer->symmetryPoint() );
break;
}

case QgsGraduatedSymbolRenderer::Pretty:
{
cbxClassifySymmetric->setVisible( true );
cbxAstride->setVisible( true );
spinSymmetryPointForOtherMethods->setVisible( false );
cboSymmetryPointForPretty->setVisible( true );
cboSymmetryPointForPretty->clear();
cboSymmetryPointForPretty->addItems( mRenderer->listForCboPrettyBreaks() );
// replace the combobox on the good old value
cboSymmetryPointForPretty->setCurrentText( QString::number( mRenderer->symmetryPoint(), 'f', 2 ) );
break;
}

case QgsGraduatedSymbolRenderer::Quantile:
case QgsGraduatedSymbolRenderer::Jenks:
case QgsGraduatedSymbolRenderer::Custom:
{
cbxClassifySymmetric->setVisible( false );
cbxAstride->setVisible( false );
cboSymmetryPointForPretty->setVisible( false );
spinSymmetryPointForOtherMethods->setVisible( false );
spinSymmetryPointForOtherMethods->setValue( mRenderer->symmetryPoint() );
break;
}
}

if ( mRenderer->useSymmetricMode() )
@@ -873,53 +892,68 @@ void QgsGraduatedSymbolRendererWidget::classifyGraduated()

double symmetryPoint = spinSymmetryPointForOtherMethods->value();

if ( cboGraduatedMode->currentIndex() == 0 ) // EqualInterval
const QgsGraduatedSymbolRenderer::Mode cboMode = static_cast< QgsGraduatedSymbolRenderer::Mode >( cboGraduatedMode->currentData().toInt() );
switch ( cboMode )
{
mode = QgsGraduatedSymbolRenderer::EqualInterval;
// knowing that spinSymmetryPointForOtherMethods->value() is automatically put at minimum when out of min-max
// using "(maximum-minimum)/100)" to avoid direct comparison of doubles
if ( spinSymmetryPointForOtherMethods->value() < ( minimum + ( maximum - minimum ) / 100. ) || spinSymmetryPointForOtherMethods->value() > ( maximum - ( maximum - minimum ) / 100. ) )
spinSymmetryPointForOtherMethods->setValue( minimum + ( maximum - minimum ) / 2. );
case QgsGraduatedSymbolRenderer::EqualInterval:
{
mode = QgsGraduatedSymbolRenderer::EqualInterval;
// knowing that spinSymmetryPointForOtherMethods->value() is automatically put at minimum when out of min-max
// using "(maximum-minimum)/100)" to avoid direct comparison of doubles
if ( spinSymmetryPointForOtherMethods->value() < ( minimum + ( maximum - minimum ) / 100. ) || spinSymmetryPointForOtherMethods->value() > ( maximum - ( maximum - minimum ) / 100. ) )
spinSymmetryPointForOtherMethods->setValue( minimum + ( maximum - minimum ) / 2. );

if ( cbxClassifySymmetric->isChecked() )
{
useSymmetricMode = true;
symmetryPoint = spinSymmetryPointForOtherMethods->value();
astride = cbxAstride->isChecked();
}
break;
}

if ( cbxClassifySymmetric->isChecked() )
case QgsGraduatedSymbolRenderer::Jenks:
{
useSymmetricMode = true;
symmetryPoint = spinSymmetryPointForOtherMethods->value();
astride = cbxAstride->isChecked();
mode = QgsGraduatedSymbolRenderer::Jenks;
break;
}
}
else if ( cboGraduatedMode->currentIndex() == 2 ) // Jenks
{
mode = QgsGraduatedSymbolRenderer::Jenks;
}
else if ( cboGraduatedMode->currentIndex() == 3 ) // StdDev
{
mode = QgsGraduatedSymbolRenderer::StdDev;
// knowing that spinSymmetryPointForOtherMethods->value() is automatically put at minimum when out of min-max
// using "(maximum-minimum)/100)" to avoid direct comparison of doubles
if ( spinSymmetryPointForOtherMethods->value() < ( minimum + ( maximum - minimum ) / 100. ) || spinSymmetryPointForOtherMethods->value() > ( maximum - ( maximum - minimum ) / 100. ) )
spinSymmetryPointForOtherMethods->setValue( minimum + ( maximum - minimum ) / 2. );

if ( cbxClassifySymmetric->isChecked() )
case QgsGraduatedSymbolRenderer::StdDev:
{
useSymmetricMode = true;
symmetryPoint = spinSymmetryPointForOtherMethods->value();
astride = cbxAstride->isChecked();
mode = QgsGraduatedSymbolRenderer::StdDev;
// knowing that spinSymmetryPointForOtherMethods->value() is automatically put at minimum when out of min-max
// using "(maximum-minimum)/100)" to avoid direct comparison of doubles
if ( spinSymmetryPointForOtherMethods->value() < ( minimum + ( maximum - minimum ) / 100. ) || spinSymmetryPointForOtherMethods->value() > ( maximum - ( maximum - minimum ) / 100. ) )
spinSymmetryPointForOtherMethods->setValue( minimum + ( maximum - minimum ) / 2. );

if ( cbxClassifySymmetric->isChecked() )
{
useSymmetricMode = true;
symmetryPoint = spinSymmetryPointForOtherMethods->value();
astride = cbxAstride->isChecked();
}
break;
}
}
else if ( cboGraduatedMode->currentIndex() == 4 ) // Pretty
{
mode = QgsGraduatedSymbolRenderer::Pretty;
if ( cbxClassifySymmetric->isChecked() )

case QgsGraduatedSymbolRenderer::Pretty:
{
useSymmetricMode = true;
astride = cbxAstride->isChecked();
symmetryPoint = cboSymmetryPointForPretty->currentText().toDouble(); //selected number
mode = QgsGraduatedSymbolRenderer::Pretty;
if ( cbxClassifySymmetric->isChecked() )
{
useSymmetricMode = true;
astride = cbxAstride->isChecked();
symmetryPoint = cboSymmetryPointForPretty->currentText().toDouble(); //selected number
}
break;
}

case QgsGraduatedSymbolRenderer::Quantile:
case QgsGraduatedSymbolRenderer::Custom:
{
// default should be quantile for now
mode = QgsGraduatedSymbolRenderer::Quantile; // Quantile
break;
}
}
else // default should be quantile for now
{
mode = QgsGraduatedSymbolRenderer::Quantile; // Quantile
}

// Jenks is n^2 complexity, warn for big dataset (more than 50k records)
@@ -319,31 +319,6 @@ Negative rounds to powers of 10</string>
<property name="editable">
<bool>false</bool>
</property>
<item>
<property name="text">
<string>Equal Interval</string>
</property>
</item>
<item>
<property name="text">
<string>Quantile (Equal Count)</string>
</property>
</item>
<item>
<property name="text">
<string>Natural Breaks (Jenks)</string>
</property>
</item>
<item>
<property name="text">
<string>Standard Deviation</string>
</property>
</item>
<item>
<property name="text">
<string>Pretty Breaks</string>
</property>
</item>
</widget>
</item>
<item>

0 comments on commit 0f15735

Please sign in to comment.
You can’t perform that action at this time.