mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-16 00:03:12 -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.
|
/** Returns a list of variable names contained within the scope.
|
||||||
* @see functionNames()
|
* @see functionNames()
|
||||||
|
* @see filteredVariableNames()
|
||||||
*/
|
*/
|
||||||
QStringList variableNames() const;
|
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
|
/** Tests whether the specified variable is read only and should not be editable
|
||||||
* by users.
|
* by users.
|
||||||
* @param name variable name
|
* @param name variable name
|
||||||
|
@ -112,6 +112,51 @@ QStringList QgsExpressionContextScope::variableNames() const
|
|||||||
return names;
|
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
|
bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
|
||||||
{
|
{
|
||||||
return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
|
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.
|
/** Returns a list of variable names contained within the scope.
|
||||||
* @see functionNames()
|
* @see functionNames()
|
||||||
|
* @see filteredVariableNames()
|
||||||
*/
|
*/
|
||||||
QStringList variableNames() const;
|
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
|
/** Tests whether the specified variable is read only and should not be editable
|
||||||
* by users.
|
* by users.
|
||||||
* @param name variable name
|
* @param name variable name
|
||||||
@ -216,6 +224,7 @@ class CORE_EXPORT QgsExpressionContextScope
|
|||||||
QHash<QString, StaticVariable> mVariables;
|
QHash<QString, StaticVariable> mVariables;
|
||||||
QHash<QString, QgsScopedExpressionFunction* > mFunctions;
|
QHash<QString, QgsScopedExpressionFunction* > mFunctions;
|
||||||
|
|
||||||
|
bool variableNameSort( const QString &a, const QString &b );
|
||||||
};
|
};
|
||||||
|
|
||||||
/** \ingroup core
|
/** \ingroup core
|
||||||
|
@ -366,11 +366,8 @@ void QgsVariableEditorTree::refreshScopeVariables( QgsExpressionContextScope* sc
|
|||||||
bool isCurrent = scopeIndex == mEditableScopeIndex;
|
bool isCurrent = scopeIndex == mEditableScopeIndex;
|
||||||
QTreeWidgetItem* scopeItem = mScopeToItem.value( scopeIndex );
|
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;
|
QTreeWidgetItem* item;
|
||||||
if ( mVariableToItem.contains( qMakePair( scopeIndex, name ) ) )
|
if ( mVariableToItem.contains( qMakePair( scopeIndex, name ) ) )
|
||||||
{
|
{
|
||||||
|
@ -165,6 +165,10 @@ void TestQgsExpressionContext::contextScope()
|
|||||||
scope.setVariable( "readonly", "newvalue" );
|
scope.setVariable( "readonly", "newvalue" );
|
||||||
QVERIFY( scope.isReadOnly( "readonly" ) );
|
QVERIFY( scope.isReadOnly( "readonly" ) );
|
||||||
|
|
||||||
|
//test retrieving filtered variable names
|
||||||
|
scope.setVariable( "_hidden_", "hidden" );
|
||||||
|
QCOMPARE( scope.filteredVariableNames(), QStringList() << "readonly" << "notreadonly" << "test" );
|
||||||
|
|
||||||
//removal
|
//removal
|
||||||
scope.setVariable( "toremove", 5 );
|
scope.setVariable( "toremove", 5 );
|
||||||
QVERIFY( scope.hasVariable( "toremove" ) );
|
QVERIFY( scope.hasVariable( "toremove" ) );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user