Skip to content

Commit

Permalink
Sort variables in variable editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 11, 2015
1 parent 2d5c5e2 commit 390ea4e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsexpressioncontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsexpressioncontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/gui/qgsvariableeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) )
{
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down

0 comments on commit 390ea4e

Please sign in to comment.