mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-22 00:14:55 -05:00
Added new logic for handling hiding of expression variables, this will help to have a clear way of not showing variables that are not needed anymore but they can break past QGIS projects if they are removed.
This commit is contained in:
parent
ebd1e527e1
commit
df3b3d122e
@ -404,6 +404,63 @@ Writes scope variables to an XML ``element``.
|
||||
.. versionadded:: 3.6
|
||||
%End
|
||||
|
||||
|
||||
QStringList hiddenVariables() const;
|
||||
%Docstring
|
||||
Returns the list of variables hidden within the scope.
|
||||
|
||||
.. seealso:: :py:func:`setHiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`addHiddenVariable`
|
||||
|
||||
.. seealso:: :py:func:`removeHiddenVariable`
|
||||
|
||||
.. versionadded:: 3.28
|
||||
%End
|
||||
|
||||
void setHiddenVariables( const QStringList &hiddenVariables );
|
||||
%Docstring
|
||||
|
||||
Sets the list of variables intended to be hidden.
|
||||
|
||||
.. seealso:: :py:func:`hiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`addHiddenVariable`
|
||||
|
||||
.. seealso:: :py:func:`removeHiddenVariable`
|
||||
|
||||
.. versionadded:: 3.28
|
||||
%End
|
||||
|
||||
|
||||
void addHiddenVariable( const QString &hiddenVariable );
|
||||
%Docstring
|
||||
|
||||
Adds the passed variable to a list of hidden variables.
|
||||
|
||||
.. seealso:: :py:func:`hiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`setHiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`removeHiddenVariable`
|
||||
|
||||
.. versionadded:: 3.28
|
||||
%End
|
||||
|
||||
void removeHiddenVariable( const QString &hiddenVariable );
|
||||
%Docstring
|
||||
|
||||
Removes the passed variable from a list of hidden variables.
|
||||
|
||||
.. seealso:: :py:func:`hiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`setHiddenVariables`
|
||||
|
||||
.. seealso:: :py:func:`addHiddenVariable`
|
||||
|
||||
.. versionadded:: 3.28
|
||||
%End
|
||||
|
||||
};
|
||||
|
||||
class QgsExpressionContext
|
||||
|
||||
@ -47,6 +47,7 @@ QgsExpressionContextScope::QgsExpressionContextScope( const QgsExpressionContext
|
||||
, mFeature( other.mFeature )
|
||||
, mHasGeometry( other.mHasGeometry )
|
||||
, mGeometry( other.mGeometry )
|
||||
, mHiddenVariables( other.mHiddenVariables )
|
||||
{
|
||||
QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
|
||||
for ( ; it != other.mFunctions.constEnd(); ++it )
|
||||
@ -63,6 +64,7 @@ QgsExpressionContextScope &QgsExpressionContextScope::operator=( const QgsExpres
|
||||
mFeature = other.mFeature;
|
||||
mHasGeometry = other.mHasGeometry;
|
||||
mGeometry = other.mGeometry;
|
||||
mHiddenVariables = other.mHiddenVariables;
|
||||
|
||||
qDeleteAll( mFunctions );
|
||||
mFunctions.clear();
|
||||
@ -120,6 +122,29 @@ QStringList QgsExpressionContextScope::variableNames() const
|
||||
return names;
|
||||
}
|
||||
|
||||
QStringList QgsExpressionContextScope::hiddenVariables() const
|
||||
{
|
||||
return mHiddenVariables;
|
||||
}
|
||||
|
||||
void QgsExpressionContextScope::setHiddenVariables( const QStringList &hiddenVariables )
|
||||
{
|
||||
mHiddenVariables = hiddenVariables;
|
||||
}
|
||||
|
||||
void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
|
||||
{
|
||||
if ( !mHiddenVariables.contains( hiddenVariable ) )
|
||||
mHiddenVariables << hiddenVariable;
|
||||
}
|
||||
|
||||
void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
|
||||
{
|
||||
if ( mHiddenVariables.contains( hiddenVariable ) )
|
||||
mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
|
||||
}
|
||||
|
||||
|
||||
/// @cond PRIVATE
|
||||
class QgsExpressionContextVariableCompare
|
||||
{
|
||||
@ -208,8 +233,14 @@ void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsRe
|
||||
{
|
||||
const QDomElement variableElement = variablesNodeList.at( i ).toElement();
|
||||
const QString key = variableElement.attribute( QStringLiteral( "name" ) );
|
||||
const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
|
||||
setVariable( key, value );
|
||||
if ( variableElement.tagName() == QStringLiteral( "Variable" ) )
|
||||
{
|
||||
const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
|
||||
setVariable( key, value );
|
||||
}
|
||||
else
|
||||
addHiddenVariable( key );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,6 +254,13 @@ bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &do
|
||||
varElem.appendChild( valueElem );
|
||||
element.appendChild( varElem );
|
||||
}
|
||||
|
||||
for ( QString hiddenVariable : mHiddenVariables )
|
||||
{
|
||||
QDomElement varElem = document.createElement( QStringLiteral( "HiddenVariable" ) );
|
||||
varElem.setAttribute( QStringLiteral( "name" ), hiddenVariable );
|
||||
element.appendChild( varElem );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -421,10 +459,20 @@ QStringList QgsExpressionContext::filteredVariableNames() const
|
||||
QStringList allVariables = variableNames();
|
||||
QStringList filtered;
|
||||
const auto constAllVariables = allVariables;
|
||||
|
||||
QStringList hiddenVariables;
|
||||
|
||||
for ( const QgsExpressionContextScope *scope : mStack )
|
||||
{
|
||||
const QStringList scopeHiddenVariables = scope->hiddenVariables();
|
||||
for ( const QString &name : scopeHiddenVariables )
|
||||
hiddenVariables << name ;
|
||||
}
|
||||
|
||||
for ( const QString &variable : constAllVariables )
|
||||
{
|
||||
if ( variable.startsWith( '_' ) ||
|
||||
variable.compare( QStringLiteral( "frame_timestep_unit" ) ) == 0 )
|
||||
hiddenVariables.contains( variable ) )
|
||||
continue;
|
||||
|
||||
filtered << variable;
|
||||
|
||||
@ -380,6 +380,51 @@ class CORE_EXPORT QgsExpressionContextScope
|
||||
*/
|
||||
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the list of variables hidden within the scope.
|
||||
*
|
||||
* \see setHiddenVariables()
|
||||
* \see addHiddenVariable()
|
||||
* \see removeHiddenVariable()
|
||||
* \since QGIS 3.28
|
||||
*/
|
||||
QStringList hiddenVariables() const;
|
||||
|
||||
/**
|
||||
*
|
||||
* Sets the list of variables intended to be hidden.
|
||||
*
|
||||
* \see hiddenVariables()
|
||||
* \see addHiddenVariable()
|
||||
* \see removeHiddenVariable()
|
||||
* \since QGIS 3.28
|
||||
*/
|
||||
void setHiddenVariables( const QStringList &hiddenVariables );
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Adds the passed variable to a list of hidden variables.
|
||||
*
|
||||
* \see hiddenVariables()
|
||||
* \see setHiddenVariables()
|
||||
* \see removeHiddenVariable()
|
||||
* \since QGIS 3.28
|
||||
*/
|
||||
void addHiddenVariable( const QString &hiddenVariable );
|
||||
|
||||
/**
|
||||
*
|
||||
* Removes the passed variable from a list of hidden variables.
|
||||
*
|
||||
* \see hiddenVariables()
|
||||
* \see setHiddenVariables()
|
||||
* \see addHiddenVariable()
|
||||
* \since QGIS 3.28
|
||||
*/
|
||||
void removeHiddenVariable( const QString &hiddenVariable );
|
||||
|
||||
private:
|
||||
QString mName;
|
||||
QHash<QString, StaticVariable> mVariables;
|
||||
@ -388,6 +433,7 @@ class CORE_EXPORT QgsExpressionContextScope
|
||||
QgsFeature mFeature;
|
||||
bool mHasGeometry = false;
|
||||
QgsGeometry mGeometry;
|
||||
QStringList mHiddenVariables;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -82,6 +82,9 @@ QgsExpressionContextScope *QgsTemporalNavigationObject::createExpressionContextS
|
||||
scope->setVariable( QStringLiteral( "animation_start_time" ), mTemporalExtents.begin(), true );
|
||||
scope->setVariable( QStringLiteral( "animation_end_time" ), mTemporalExtents.end(), true );
|
||||
scope->setVariable( QStringLiteral( "animation_interval" ), mTemporalExtents.end() - mTemporalExtents.begin(), true );
|
||||
|
||||
scope->addHiddenVariable( QStringLiteral( "frame_timestep_unit" ) );
|
||||
|
||||
return scope.release();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user