Skip to content

Commit

Permalink
add API to allow adding processing parameters to classification methods
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Dec 4, 2019
1 parent f341b34 commit b078370
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
41 changes: 39 additions & 2 deletions src/core/classification/qgsclassificationmethod.cpp
Expand Up @@ -39,7 +39,7 @@ QList<double> QgsClassificationMethod::rangesToBreaks( const QList<QgsClassifica
QgsClassificationMethod::QgsClassificationMethod( MethodProperties properties, int codeComplexity )
: mFlags( properties )
, mCodeComplexity( codeComplexity )
, mLabelFormat( QStringLiteral( "%1 - %2 " ) )
, mLabelFormat( QStringLiteral( "%1 - %2" ) )
{
}

Expand Down Expand Up @@ -161,6 +161,42 @@ QString QgsClassificationMethod::formatNumber( double value ) const
}
}

void QgsClassificationMethod::addParameter( QgsProcessingParameterDefinition *definition )
{
mParameters.append( definition );
}

QVariant QgsClassificationMethod::parameterValue( const QString &name ) const
{
const QgsProcessingParameterDefinition *def = parameterDefinition( name );
if ( def )
return mParameterValues.value( name, def->defaultValue() );
else
return mParameterValues.value( name );
}

const QgsProcessingParameterDefinition *QgsClassificationMethod::parameterDefinition( const QString &parameterName ) const
{
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->name() == parameterName )
return def;
}
return nullptr;
}

void QgsClassificationMethod::setParameterValues( const QVariantMap &values )
{
mParameterValues = values;
for ( const QString &paramName : mParameterValues.keys() ) // todo is this ok?
{
if ( !parameterDefinition( paramName ) )
{
QgsMessageLog::logMessage( name(), QObject::tr( "Parameter %1 does not exist in the method" ).arg( paramName ) );
}
}
}

QList<QgsClassificationRange> QgsClassificationMethod::classes( const QgsVectorLayer *layer, const QString &expression, int nclasses )
{
if ( expression.isEmpty() )
Expand All @@ -173,6 +209,7 @@ QList<QgsClassificationRange> QgsClassificationMethod::classes( const QgsVectorL
double minimum;
double maximum;


int fieldIndex = layer->fields().indexFromName( expression );

bool ok;
Expand All @@ -192,7 +229,7 @@ QList<QgsClassificationRange> QgsClassificationMethod::classes( const QgsVectorL
maximum = layer->maximumValue( fieldIndex ).toDouble();
}

// get the breaks
// get the breaks, minimum and maximum might be updated by implementation
QList<double> breaks = calculateBreaks( minimum, maximum, values, nclasses );
breaks.insert( 0, minimum );
// create classes
Expand Down
52 changes: 43 additions & 9 deletions src/core/classification/qgsclassificationmethod.h
Expand Up @@ -110,7 +110,7 @@ class CORE_EXPORT QgsClassificationMethod SIP_ABSTRACT
enum MethodProperty
{
NoFlag = 0, //!< No flag
ValuesNotRequired = 1 << 1, //!< Values are not required to calculate, min/max are enough
ValuesNotRequired = 1 << 1, //!< Deprecated since QGIS 3.12
SymmetricModeAvailable = 1 << 2, //!< This allows using symmetric classification
};
Q_DECLARE_FLAGS( MethodProperties, MethodProperty )
Expand Down Expand Up @@ -159,14 +159,15 @@ class CORE_EXPORT QgsClassificationMethod SIP_ABSTRACT
//! Reads extra information to apply it to the method
virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) {Q_UNUSED( element ); Q_UNUSED( context )}

// *******************
// non-virtual methods

/**
* Returns if the method requires values to calculate the classes
* If not, bounds are sufficient
*/
bool valuesRequired() const {return !mFlags.testFlag( ValuesNotRequired );}
virtual bool valuesRequired() const {return true;}


// *******************
// non-virtual methods

//! Code complexity as the exponent in Big O notation
int codeComplexity() const {return mCodeComplexity;}
Expand Down Expand Up @@ -270,6 +271,24 @@ class CORE_EXPORT QgsClassificationMethod SIP_ABSTRACT
*/
QString labelForRange( const QgsRendererRange &range, ClassPosition position = Inner ) const;

/**
* Get parameter from its id
* \since QGIS 3.12
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &parameterName ) const;

/**
* List of parameters
* \since QGIS 3.12
*/
const QgsProcessingParameterDefinitions parameterDefinitions() const {return mParameters;}

/**
* Set values of the additional parameters
* \since QGIS 3.12
*/
void setParameterValues( const QVariantMap &values );



static const int MAX_PRECISION;
Expand All @@ -283,20 +302,32 @@ class CORE_EXPORT QgsClassificationMethod SIP_ABSTRACT
//! Format the number according to label properties
QString formatNumber( double value ) const;

/**
* Add parameter
* \since QGIS 3.12
*/
void addParameter( QgsProcessingParameterDefinition *definition );

/**
* Get the parameter value
* \see setParameterValues
* \since QGIS 3.12
*/
QVariant parameterValue( const QString &name ) const;

private:

/**
* Calculate the breaks, should be reimplemented, values might be an empty list
* Calculate the breaks, should be reimplemented, values might be an empty list if they are not required
* If the symmetric mode is available, the implementation is responsible of applying the symmetry
* The maximum value is expected to be added at the end of the list, but not the minimum
*/
virtual QList<double> calculateBreaks( double minimum, double maximum,
virtual QList<double> calculateBreaks( double &minimum, double &maximum,
const QList<double> &values, int nclasses ) = 0;

//! This is called after calculating the breaks or restoring from XML, so it can rely on private variables
virtual QString valueToLabel( double value ) const {return formatNumber( value );}


//! Create a list of ranges from a list of classes
QList<QgsClassificationRange> breaksToClasses( const QList<double> &breaks ) const;

Expand All @@ -313,10 +344,13 @@ class CORE_EXPORT QgsClassificationMethod SIP_ABSTRACT
bool mLabelTrimTrailingZeroes = true;
QString mLabelFormat;


// values used to manage number formatting - precision and trailing zeroes
double mLabelNumberScale = 1.0;
QString mLabelNumberSuffix;

//! additional parameters
QgsProcessingParameterDefinitions mParameters;
QVariantMap mParameterValues;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsClassificationMethod::MethodProperties )
Expand Down

0 comments on commit b078370

Please sign in to comment.