Fix clazy, cppcheck warnings in expression builder widgets

This commit is contained in:
Nyall Dawson 2021-08-17 09:43:40 +10:00
parent 2f7efed0b1
commit 06b3b1650a
6 changed files with 62 additions and 53 deletions

View File

@ -42,21 +42,21 @@ Create a new expression builder widget with an optional parent.
%End %End
~QgsExpressionBuilderWidget(); ~QgsExpressionBuilderWidget();
void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
%Docstring %Docstring
Initialize without any layer Initialize without any layer
.. versionadded:: 3.14 .. versionadded:: 3.14
%End %End
void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
%Docstring %Docstring
Initialize with a layer Initialize with a layer
.. versionadded:: 3.14 .. versionadded:: 3.14
%End %End
void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
%Docstring %Docstring
Initialize with given fields without any layer Initialize with given fields without any layer
@ -200,7 +200,7 @@ Loads the user expressions.
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %End
void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ) /Deprecated/; void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) /Deprecated/;
%Docstring %Docstring
Stores the user ``expression`` with given ``label`` and ``helpText``. Stores the user ``expression`` with given ``label`` and ``helpText``.

View File

@ -89,10 +89,18 @@ level down
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const; virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
void setFilterString( const QString &string );
%Docstring
Sets the search filter ``string``.
.. versionadded:: 3.22
%End
protected: protected:
virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const; virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
}; };
class QgsExpressionTreeView : QTreeView class QgsExpressionTreeView : QTreeView
@ -213,7 +221,7 @@ Adds the current expression to the given ``collection``.
By default it is saved to the collection "generic". By default it is saved to the collection "generic".
%End %End
void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ); void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
%Docstring %Docstring
Stores the user ``expression`` with given ``label`` and ``helpText``. Stores the user ``expression`` with given ``label`` and ``helpText``.
%End %End

View File

@ -251,7 +251,7 @@ QgsExpressionBuilderWidget::~QgsExpressionBuilderWidget()
delete mExpressionTreeMenuProvider; delete mExpressionTreeMenuProvider;
} }
void QgsExpressionBuilderWidget::init( const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags ) void QgsExpressionBuilderWidget::init( const QgsExpressionContext &context, const QString &recentCollection, QgsExpressionBuilderWidget::Flags flags )
{ {
setExpressionContext( context ); setExpressionContext( context );
@ -262,13 +262,13 @@ void QgsExpressionBuilderWidget::init( const QgsExpressionContext &context, cons
mExpressionTreeView->loadUserExpressions(); mExpressionTreeView->loadUserExpressions();
} }
void QgsExpressionBuilderWidget::initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags ) void QgsExpressionBuilderWidget::initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context, const QString &recentCollection, QgsExpressionBuilderWidget::Flags flags )
{ {
init( context, recentCollection, flags ); init( context, recentCollection, flags );
setLayer( layer ); setLayer( layer );
} }
void QgsExpressionBuilderWidget::initWithFields( const QgsFields &fields, const QgsExpressionContext &context, const QString &recentCollection, const Flags &flags ) void QgsExpressionBuilderWidget::initWithFields( const QgsFields &fields, const QgsExpressionContext &context, const QString &recentCollection, QgsExpressionBuilderWidget::Flags flags )
{ {
init( context, recentCollection, flags ); init( context, recentCollection, flags );
mExpressionTreeView->loadFieldNames( fields ); mExpressionTreeView->loadFieldNames( fields );
@ -402,10 +402,10 @@ void QgsExpressionBuilderWidget::updateFunctionFileList( const QString &path )
if ( cmbFileNames->count() == 0 ) if ( cmbFileNames->count() == 0 )
{ {
// Create default sample entry. // Create default sample entry.
newFunctionFile( "default" ); newFunctionFile( QStringLiteral( "default" ) );
txtPython->setText( QStringLiteral( "'''\n#Sample custom function file\n" txtPython->setText( QStringLiteral( "'''\n#Sample custom function file\n"
"#(uncomment to use and customize or Add button to create a new file) \n%1 \n '''" ).arg( txtPython->text() ) ); "#(uncomment to use and customize or Add button to create a new file) \n%1 \n '''" ).arg( txtPython->text() ) );
saveFunctionFile( "default" ); saveFunctionFile( QStringLiteral( "default" ) );
} }
} }
@ -598,7 +598,7 @@ void QgsExpressionBuilderWidget::loadUserExpressions( )
mExpressionTreeView->loadUserExpressions(); mExpressionTreeView->loadUserExpressions();
} }
void QgsExpressionBuilderWidget::saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ) void QgsExpressionBuilderWidget::saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText )
{ {
mExpressionTreeView->saveToUserExpressions( label, expression, helpText ); mExpressionTreeView->saveToUserExpressions( label, expression, helpText );
} }
@ -685,7 +685,7 @@ void QgsExpressionBuilderWidget::showEvent( QShowEvent *e )
txtExpressionString->setFocus(); txtExpressionString->setFocus();
} }
void QgsExpressionBuilderWidget::createErrorMarkers( QList<QgsExpression::ParserError> errors ) void QgsExpressionBuilderWidget::createErrorMarkers( const QList<QgsExpression::ParserError> &errors )
{ {
clearErrors(); clearErrors();
for ( const QgsExpression::ParserError &error : errors ) for ( const QgsExpression::ParserError &error : errors )
@ -769,7 +769,8 @@ void QgsExpressionBuilderWidget::createMarkers( const QgsExpressionNode *inNode
case QgsExpressionNode::NodeType::ntCondition: case QgsExpressionNode::NodeType::ntCondition:
{ {
const QgsExpressionNodeCondition *node = static_cast<const QgsExpressionNodeCondition *>( inNode ); const QgsExpressionNodeCondition *node = static_cast<const QgsExpressionNodeCondition *>( inNode );
for ( QgsExpressionNodeCondition::WhenThen *cond : node->conditions() ) const QList<QgsExpressionNodeCondition::WhenThen *> conditions = node->conditions();
for ( QgsExpressionNodeCondition::WhenThen *cond : conditions )
{ {
createMarkers( cond->whenExp() ); createMarkers( cond->whenExp() );
createMarkers( cond->thenExp() ); createMarkers( cond->thenExp() );
@ -1123,7 +1124,7 @@ QMenu *QgsExpressionBuilderWidget::ExpressionTreeMenuProvider::createContextMenu
if ( formatterCanProvideAvailableValues( layer, item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString() ) ) if ( formatterCanProvideAvailableValues( layer, item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString() ) )
{ {
menu->addAction( tr( "Load First 10 Unique Used Values" ), mExpressionBuilderWidget, SLOT( loadSampleUsedValues() ) ); menu->addAction( tr( "Load First 10 Unique Used Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadSampleUsedValues );
menu->addAction( tr( "Load All Unique Used Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadAllUsedValues ); menu->addAction( tr( "Load All Unique Used Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadAllUsedValues );
} }
} }

View File

@ -69,19 +69,19 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* Initialize without any layer * Initialize without any layer
* \since QGIS 3.14 * \since QGIS 3.14
*/ */
void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
/** /**
* Initialize with a layer * Initialize with a layer
* \since QGIS 3.14 * \since QGIS 3.14
*/ */
void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
/** /**
* Initialize with given fields without any layer * Initialize with given fields without any layer
* \since QGIS 3.14 * \since QGIS 3.14
*/ */
void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll ); void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
/** /**
* Sets layer in order to get the fields and values * Sets layer in order to get the fields and values
@ -186,7 +186,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* \deprecated since QGIS 3.14 use expressionTree()->saveToUserExpressions() instead * \deprecated since QGIS 3.14 use expressionTree()->saveToUserExpressions() instead
* \since QGIS 3.12 * \since QGIS 3.12
*/ */
Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ) SIP_DEPRECATED; Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
/** /**
* Removes the expression \a label from the user stored expressions. * Removes the expression \a label from the user stored expressions.
@ -407,7 +407,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
int FUNCTION_MARKER_ID = 25; int FUNCTION_MARKER_ID = 25;
void createErrorMarkers( QList<QgsExpression::ParserError> errors ); void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
void createMarkers( const QgsExpressionNode *node ); void createMarkers( const QgsExpressionNode *node );
void clearFunctionMarkers(); void clearFunctionMarkers();
void clearErrors(); void clearErrors();

View File

@ -327,7 +327,7 @@ void QgsExpressionTreeView::registerItem( const QString &group,
const QString &label, const QString &label,
const QString &expressionText, const QString &expressionText,
const QString &helpText, const QString &helpText,
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, QIcon icon, const QStringList &tags, const QString &name ) QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, const QIcon &icon, const QStringList &tags, const QString &name )
{ {
QgsExpressionItem *item = new QgsExpressionItem( label, expressionText, helpText, type ); QgsExpressionItem *item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole ); item->setData( label, Qt::UserRole );
@ -505,7 +505,7 @@ void QgsExpressionTreeView::saveToRecent( const QString &expressionText, const Q
loadRecent( collection ); loadRecent( collection );
} }
void QgsExpressionTreeView::saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ) void QgsExpressionTreeView::saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText )
{ {
QgsSettings settings; QgsSettings settings;
const QString location = QStringLiteral( "user" ); const QString location = QStringLiteral( "user" );
@ -542,7 +542,6 @@ void QgsExpressionTreeView::loadUserExpressions( )
QgsSettings settings; QgsSettings settings;
const QString location = QStringLiteral( "user" ); const QString location = QStringLiteral( "user" );
settings.beginGroup( location, QgsSettings::Section::Expressions ); settings.beginGroup( location, QgsSettings::Section::Expressions );
QString label;
QString helpText; QString helpText;
QString expression; QString expression;
int i = 0; int i = 0;
@ -598,7 +597,7 @@ QJsonDocument QgsExpressionTreeView::exportUserExpressions()
settings.endGroup(); settings.endGroup();
} }
exportObject["expressions"] = exportList; exportObject[QStringLiteral( "expressions" )] = exportList;
QJsonDocument exportJson = QJsonDocument( exportObject ); QJsonDocument exportJson = QJsonDocument( exportObject );
return exportJson; return exportJson;
@ -613,14 +612,14 @@ void QgsExpressionTreeView::loadExpressionsFromJson( const QJsonDocument &expres
QJsonObject expressionsObject = expressionsDocument.object(); QJsonObject expressionsObject = expressionsDocument.object();
// validate json for manadatory fields // validate json for manadatory fields
if ( ! expressionsObject["qgis_version"].isString() if ( ! expressionsObject[QStringLiteral( "qgis_version" )].isString()
|| ! expressionsObject["exported_at"].isString() || ! expressionsObject[QStringLiteral( "exported_at" )].isString()
|| ! expressionsObject["author"].isString() || ! expressionsObject[QStringLiteral( "author" )].isString()
|| ! expressionsObject["expressions"].isArray() ) || ! expressionsObject[QStringLiteral( "expressions" )].isArray() )
return; return;
// validate versions // validate versions
QVersionNumber qgisJsonVersion = QVersionNumber::fromString( expressionsObject["qgis_version"].toString() ); QVersionNumber qgisJsonVersion = QVersionNumber::fromString( expressionsObject[QStringLiteral( "qgis_version" )].toString() );
QVersionNumber qgisVersion = QVersionNumber::fromString( Qgis::version() ); QVersionNumber qgisVersion = QVersionNumber::fromString( Qgis::version() );
// if the expressions are from newer version of QGIS, we ask the user to confirm // if the expressions are from newer version of QGIS, we ask the user to confirm
@ -654,7 +653,8 @@ void QgsExpressionTreeView::loadExpressionsFromJson( const QJsonDocument &expres
settings.beginGroup( QStringLiteral( "user" ), QgsSettings::Section::Expressions ); settings.beginGroup( QStringLiteral( "user" ), QgsSettings::Section::Expressions );
mUserExpressionLabels = settings.childGroups(); mUserExpressionLabels = settings.childGroups();
for ( const QJsonValue && expressionValue : expressionsObject["expressions"].toArray() ) const QJsonArray expressions = expressionsObject[QStringLiteral( "expressions" )].toArray();
for ( const QJsonValue && expressionValue : expressions )
{ {
// validate the type of the array element, can be anything // validate the type of the array element, can be anything
if ( ! expressionValue.isObject() ) if ( ! expressionValue.isObject() )
@ -667,43 +667,43 @@ void QgsExpressionTreeView::loadExpressionsFromJson( const QJsonDocument &expres
QJsonObject expressionObj = expressionValue.toObject(); QJsonObject expressionObj = expressionValue.toObject();
// make sure the required keys are the correct types // make sure the required keys are the correct types
if ( ! expressionObj["name"].isString() if ( ! expressionObj[QStringLiteral( "name" )].isString()
|| ! expressionObj["type"].isString() || ! expressionObj[QStringLiteral( "type" )].isString()
|| ! expressionObj["expression"].isString() || ! expressionObj[QStringLiteral( "expression" )].isString()
|| ! expressionObj["group"].isString() || ! expressionObj[QStringLiteral( "group" )].isString()
|| ! expressionObj["description"].isString() ) || ! expressionObj[QStringLiteral( "description" )].isString() )
{ {
// try to stringify and put an indicator what happened. Try to stringify the name, if fails, go with the expression. // try to stringify and put an indicator what happened. Try to stringify the name, if fails, go with the expression.
if ( ! expressionObj["name"].toString().isEmpty() ) if ( ! expressionObj[QStringLiteral( "name" )].toString().isEmpty() )
skippedExpressionLabels.append( expressionObj["name"].toString() ); skippedExpressionLabels.append( expressionObj[QStringLiteral( "name" )].toString() );
else else
skippedExpressionLabels.append( expressionObj["expression"].toString() ); skippedExpressionLabels.append( expressionObj[QStringLiteral( "expression" )].toString() );
continue; continue;
} }
// we want to import only items of type expression for now // we want to import only items of type expression for now
if ( expressionObj["type"].toString() != QLatin1String( "expression" ) ) if ( expressionObj[QStringLiteral( "type" )].toString() != QLatin1String( "expression" ) )
{ {
skippedExpressionLabels.append( expressionObj["name"].toString() ); skippedExpressionLabels.append( expressionObj[QStringLiteral( "name" )].toString() );
continue; continue;
} }
// we want to import only items of type expression for now // we want to import only items of type expression for now
if ( expressionObj["group"].toString() != QLatin1String( "user" ) ) if ( expressionObj[QStringLiteral( "group" )].toString() != QLatin1String( "user" ) )
{ {
skippedExpressionLabels.append( expressionObj["name"].toString() ); skippedExpressionLabels.append( expressionObj[QStringLiteral( "name" )].toString() );
continue; continue;
} }
const QString label = expressionObj["name"].toString(); const QString label = expressionObj[QStringLiteral( "name" )].toString();
const QString expression = expressionObj["expression"].toString(); const QString expression = expressionObj[QStringLiteral( "expression" )].toString();
const QString helpText = expressionObj["description"].toString(); const QString helpText = expressionObj[QStringLiteral( "description" )].toString();
// make sure they have valid name // make sure they have valid name
if ( label.contains( "\\" ) || label.contains( '/' ) ) if ( label.contains( QLatin1String( "\\" ) ) || label.contains( '/' ) )
{ {
skippedExpressionLabels.append( expressionObj["name"].toString() ); skippedExpressionLabels.append( expressionObj[QStringLiteral( "name" )].toString() );
continue; continue;
} }
@ -736,13 +736,14 @@ void QgsExpressionTreeView::loadExpressionsFromJson( const QJsonDocument &expres
if ( ! skippedExpressionLabels.isEmpty() ) if ( ! skippedExpressionLabels.isEmpty() )
{ {
QStringList skippedExpressionLabelsQuoted; QStringList skippedExpressionLabelsQuoted;
skippedExpressionLabelsQuoted.reserve( skippedExpressionLabels.size() );
for ( const QString &skippedExpressionLabel : skippedExpressionLabels ) for ( const QString &skippedExpressionLabel : skippedExpressionLabels )
skippedExpressionLabelsQuoted.append( QStringLiteral( "'%1'" ).arg( skippedExpressionLabel ) ); skippedExpressionLabelsQuoted.append( QStringLiteral( "'%1'" ).arg( skippedExpressionLabel ) );
QMessageBox::information( this, QMessageBox::information( this,
tr( "Skipped Expression Imports" ), tr( "Skipped Expression Imports" ),
QStringLiteral( "%1\n%2" ).arg( tr( "The following expressions have been skipped:" ), QStringLiteral( "%1\n%2" ).arg( tr( "The following expressions have been skipped:" ),
skippedExpressionLabelsQuoted.join( ", " ) ) ); skippedExpressionLabelsQuoted.join( QStringLiteral( ", " ) ) ) );
} }
} }
@ -750,10 +751,9 @@ const QList<QgsExpressionItem *> QgsExpressionTreeView::findExpressions( const Q
{ {
QList<QgsExpressionItem *> result; QList<QgsExpressionItem *> result;
const QList<QStandardItem *> found { mModel->findItems( label, Qt::MatchFlag::MatchRecursive ) }; const QList<QStandardItem *> found { mModel->findItems( label, Qt::MatchFlag::MatchRecursive ) };
for ( const auto &item : std::as_const( found ) ) result.reserve( found.size() );
{ std::transform( found.begin(), found.end(), std::back_inserter( result ),
result.push_back( static_cast<QgsExpressionItem *>( item ) ); []( QStandardItem * item ) -> QgsExpressionItem* { return static_cast<QgsExpressionItem *>( item ); } );
}
return result; return result;
} }

View File

@ -248,7 +248,7 @@ class GUI_EXPORT QgsExpressionTreeView : public QTreeView
/** /**
* Stores the user \a expression with given \a label and \a helpText. * Stores the user \a expression with given \a label and \a helpText.
*/ */
void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ); void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
/** /**
* Removes the expression \a label from the user stored expressions. * Removes the expression \a label from the user stored expressions.
@ -322,7 +322,7 @@ class GUI_EXPORT QgsExpressionTreeView : public QTreeView
const QString &helpText = QString(), const QString &helpText = QString(),
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode, QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1, bool highlightedItem = false, int sortOrder = 1,
QIcon icon = QIcon(), const QIcon &icon = QIcon(),
const QStringList &tags = QStringList(), const QStringList &tags = QStringList(),
const QString &name = QString() ); const QString &name = QString() );