Sort variables in variable editor

This commit is contained in:
Nyall Dawson 2015-09-11 17:11:48 +10:00
parent 2d5c5e2aac
commit 390ea4e9ba
5 changed files with 67 additions and 4 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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 ) ) )
{

View File

@ -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" ) );