File tree Expand file tree Collapse file tree 5 files changed +67
-4
lines changed Expand file tree Collapse file tree 5 files changed +67
-4
lines changed Original file line number Diff line number Diff line change @@ -125,9 +125,17 @@ class QgsExpressionContextScope
125125
126126 /** Returns a list of variable names contained within the scope.
127127 * @see functionNames()
128+ * @see filteredVariableNames()
128129 */
129130 QStringList variableNames() const;
130131
132+ /** Returns a fitlered and sorted list of variable names contained within the scope.
133+ * Hidden variable names will be excluded, and the list will be sorted so that
134+ * read only variables are listed first.
135+ * @see variableNames()
136+ */
137+ QStringList filteredVariableNames() const;
138+
131139 /** Tests whether the specified variable is read only and should not be editable
132140 * by users.
133141 * @param name variable name
Original file line number Diff line number Diff line change @@ -112,6 +112,51 @@ QStringList QgsExpressionContextScope::variableNames() const
112112 return names;
113113}
114114
115+ bool QgsExpressionContextScope::variableNameSort ( const QString& a, const QString& b )
116+ {
117+ return QString::localeAwareCompare ( a, b ) < 0 ;
118+ }
119+
120+ // not public API
121+ // / @cond
122+ class QgsExpressionContextVariableCompare
123+ {
124+ public:
125+ explicit QgsExpressionContextVariableCompare ( const QgsExpressionContextScope& scope )
126+ : mScope( scope )
127+ { }
128+
129+ bool operator ()( const QString& a, const QString& b ) const
130+ {
131+ bool aReadOnly = mScope .isReadOnly ( a );
132+ bool bReadOnly = mScope .isReadOnly ( b );
133+ if ( aReadOnly != bReadOnly )
134+ return aReadOnly;
135+ return QString::localeAwareCompare ( a, b ) < 0 ;
136+ }
137+
138+ private:
139+ const QgsExpressionContextScope& mScope ;
140+ };
141+ // / @endcond
142+
143+ QStringList QgsExpressionContextScope::filteredVariableNames () const
144+ {
145+ QStringList allVariables = mVariables .keys ();
146+ QStringList filtered;
147+ Q_FOREACH ( const QString& variable, allVariables )
148+ {
149+ if ( variable.startsWith ( " _" ) )
150+ continue ;
151+
152+ filtered << variable;
153+ }
154+ QgsExpressionContextVariableCompare cmp ( *this );
155+ qSort ( filtered.begin (), filtered.end (), cmp );
156+
157+ return filtered;
158+ }
159+
115160bool QgsExpressionContextScope::isReadOnly ( const QString &name ) const
116161{
117162 return hasVariable ( name ) ? mVariables .value ( name ).readOnly : false ;
Original file line number Diff line number Diff line change @@ -155,9 +155,17 @@ class CORE_EXPORT QgsExpressionContextScope
155155
156156 /* * Returns a list of variable names contained within the scope.
157157 * @see functionNames()
158+ * @see filteredVariableNames()
158159 */
159160 QStringList variableNames () const ;
160161
162+ /* * Returns a fitlered and sorted list of variable names contained within the scope.
163+ * Hidden variable names will be excluded, and the list will be sorted so that
164+ * read only variables are listed first.
165+ * @see variableNames()
166+ */
167+ QStringList filteredVariableNames () const ;
168+
161169 /* * Tests whether the specified variable is read only and should not be editable
162170 * by users.
163171 * @param name variable name
@@ -216,6 +224,7 @@ class CORE_EXPORT QgsExpressionContextScope
216224 QHash<QString, StaticVariable> mVariables ;
217225 QHash<QString, QgsScopedExpressionFunction* > mFunctions ;
218226
227+ bool variableNameSort ( const QString &a, const QString &b );
219228};
220229
221230/* * \ingroup core
Original file line number Diff line number Diff line change @@ -366,11 +366,8 @@ void QgsVariableEditorTree::refreshScopeVariables( QgsExpressionContextScope* sc
366366 bool isCurrent = scopeIndex == mEditableScopeIndex ;
367367 QTreeWidgetItem* scopeItem = mScopeToItem .value ( scopeIndex );
368368
369- Q_FOREACH ( const QString& name, scope->variableNames () )
369+ Q_FOREACH ( const QString& name, scope->filteredVariableNames () )
370370 {
371- if ( name.startsWith ( QChar ( ' _' ) ) )
372- continue ;
373-
374371 QTreeWidgetItem* item;
375372 if ( mVariableToItem .contains ( qMakePair ( scopeIndex, name ) ) )
376373 {
Original file line number Diff line number Diff line change @@ -165,6 +165,10 @@ void TestQgsExpressionContext::contextScope()
165165 scope.setVariable ( " readonly" , " newvalue" );
166166 QVERIFY ( scope.isReadOnly ( " readonly" ) );
167167
168+ // test retrieving filtered variable names
169+ scope.setVariable ( " _hidden_" , " hidden" );
170+ QCOMPARE ( scope.filteredVariableNames (), QStringList () << " readonly" << " notreadonly" << " test" );
171+
168172 // removal
169173 scope.setVariable ( " toremove" , 5 );
170174 QVERIFY ( scope.hasVariable ( " toremove" ) );
You can’t perform that action at this time.
0 commit comments