Skip to content
Permalink
Browse files

Sort variables in variable editor

  • Loading branch information
nyalldawson committed Sep 11, 2015
1 parent 2d5c5e2 commit 390ea4e9ba0895d2aea756c2a470db4eb4baed90
@@ -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" ) );

0 comments on commit 390ea4e

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