mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Sort variables in variable editor
This commit is contained in:
parent
2d5c5e2aac
commit
390ea4e9ba
@ -125,9 +125,17 @@ class QgsExpressionContextScope
|
||||
|
||||
/** Returns a list of variable names contained within the scope.
|
||||
* @see functionNames()
|
||||
* @see filteredVariableNames()
|
||||
*/
|
||||
QStringList variableNames() const;
|
||||
|
||||
/** Returns a fitlered and sorted list of variable names contained within the scope.
|
||||
* Hidden variable names will be excluded, and the list will be sorted so that
|
||||
* read only variables are listed first.
|
||||
* @see variableNames()
|
||||
*/
|
||||
QStringList filteredVariableNames() const;
|
||||
|
||||
/** Tests whether the specified variable is read only and should not be editable
|
||||
* by users.
|
||||
* @param name variable name
|
||||
|
@ -112,6 +112,51 @@ QStringList QgsExpressionContextScope::variableNames() const
|
||||
return names;
|
||||
}
|
||||
|
||||
bool QgsExpressionContextScope::variableNameSort( const QString& a, const QString& b )
|
||||
{
|
||||
return QString::localeAwareCompare( a, b ) < 0;
|
||||
}
|
||||
|
||||
// not public API
|
||||
/// @cond
|
||||
class QgsExpressionContextVariableCompare
|
||||
{
|
||||
public:
|
||||
explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope& scope )
|
||||
: mScope( scope )
|
||||
{ }
|
||||
|
||||
bool operator()( const QString& a, const QString& b ) const
|
||||
{
|
||||
bool aReadOnly = mScope.isReadOnly( a );
|
||||
bool bReadOnly = mScope.isReadOnly( b );
|
||||
if ( aReadOnly != bReadOnly )
|
||||
return aReadOnly;
|
||||
return QString::localeAwareCompare( a, b ) < 0;
|
||||
}
|
||||
|
||||
private:
|
||||
const QgsExpressionContextScope& mScope;
|
||||
};
|
||||
/// @endcond
|
||||
|
||||
QStringList QgsExpressionContextScope::filteredVariableNames() const
|
||||
{
|
||||
QStringList allVariables = mVariables.keys();
|
||||
QStringList filtered;
|
||||
Q_FOREACH ( const QString& variable, allVariables )
|
||||
{
|
||||
if ( variable.startsWith( "_" ) )
|
||||
continue;
|
||||
|
||||
filtered << variable;
|
||||
}
|
||||
QgsExpressionContextVariableCompare cmp( *this );
|
||||
qSort( filtered.begin(), filtered.end(), cmp );
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
|
||||
{
|
||||
return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
|
||||
|
@ -155,9 +155,17 @@ class CORE_EXPORT QgsExpressionContextScope
|
||||
|
||||
/** Returns a list of variable names contained within the scope.
|
||||
* @see functionNames()
|
||||
* @see filteredVariableNames()
|
||||
*/
|
||||
QStringList variableNames() const;
|
||||
|
||||
/** Returns a fitlered and sorted list of variable names contained within the scope.
|
||||
* Hidden variable names will be excluded, and the list will be sorted so that
|
||||
* read only variables are listed first.
|
||||
* @see variableNames()
|
||||
*/
|
||||
QStringList filteredVariableNames() const;
|
||||
|
||||
/** Tests whether the specified variable is read only and should not be editable
|
||||
* by users.
|
||||
* @param name variable name
|
||||
@ -216,6 +224,7 @@ class CORE_EXPORT QgsExpressionContextScope
|
||||
QHash<QString, StaticVariable> mVariables;
|
||||
QHash<QString, QgsScopedExpressionFunction* > mFunctions;
|
||||
|
||||
bool variableNameSort( const QString &a, const QString &b );
|
||||
};
|
||||
|
||||
/** \ingroup core
|
||||
|
@ -366,11 +366,8 @@ void QgsVariableEditorTree::refreshScopeVariables( QgsExpressionContextScope* sc
|
||||
bool isCurrent = scopeIndex == mEditableScopeIndex;
|
||||
QTreeWidgetItem* scopeItem = mScopeToItem.value( scopeIndex );
|
||||
|
||||
Q_FOREACH ( const QString& name, scope->variableNames() )
|
||||
Q_FOREACH ( const QString& name, scope->filteredVariableNames() )
|
||||
{
|
||||
if ( name.startsWith( QChar( '_' ) ) )
|
||||
continue;
|
||||
|
||||
QTreeWidgetItem* item;
|
||||
if ( mVariableToItem.contains( qMakePair( scopeIndex, name ) ) )
|
||||
{
|
||||
|
@ -165,6 +165,10 @@ void TestQgsExpressionContext::contextScope()
|
||||
scope.setVariable( "readonly", "newvalue" );
|
||||
QVERIFY( scope.isReadOnly( "readonly" ) );
|
||||
|
||||
//test retrieving filtered variable names
|
||||
scope.setVariable( "_hidden_", "hidden" );
|
||||
QCOMPARE( scope.filteredVariableNames(), QStringList() << "readonly" << "notreadonly" << "test" );
|
||||
|
||||
//removal
|
||||
scope.setVariable( "toremove", 5 );
|
||||
QVERIFY( scope.hasVariable( "toremove" ) );
|
||||
|
Loading…
x
Reference in New Issue
Block a user