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
125
125
126
126
/** Returns a list of variable names contained within the scope.
127
127
* @see functionNames()
128
+ * @see filteredVariableNames()
128
129
*/
129
130
QStringList variableNames() const;
130
131
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
+
131
139
/** Tests whether the specified variable is read only and should not be editable
132
140
* by users.
133
141
* @param name variable name
Original file line number Diff line number Diff line change @@ -112,6 +112,51 @@ QStringList QgsExpressionContextScope::variableNames() const
112
112
return names;
113
113
}
114
114
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
+
115
160
bool QgsExpressionContextScope::isReadOnly ( const QString &name ) const
116
161
{
117
162
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
155
155
156
156
/* * Returns a list of variable names contained within the scope.
157
157
* @see functionNames()
158
+ * @see filteredVariableNames()
158
159
*/
159
160
QStringList variableNames () const ;
160
161
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
+
161
169
/* * Tests whether the specified variable is read only and should not be editable
162
170
* by users.
163
171
* @param name variable name
@@ -216,6 +224,7 @@ class CORE_EXPORT QgsExpressionContextScope
216
224
QHash<QString, StaticVariable> mVariables ;
217
225
QHash<QString, QgsScopedExpressionFunction* > mFunctions ;
218
226
227
+ bool variableNameSort ( const QString &a, const QString &b );
219
228
};
220
229
221
230
/* * \ingroup core
Original file line number Diff line number Diff line change @@ -366,11 +366,8 @@ void QgsVariableEditorTree::refreshScopeVariables( QgsExpressionContextScope* sc
366
366
bool isCurrent = scopeIndex == mEditableScopeIndex ;
367
367
QTreeWidgetItem* scopeItem = mScopeToItem .value ( scopeIndex );
368
368
369
- Q_FOREACH ( const QString& name, scope->variableNames () )
369
+ Q_FOREACH ( const QString& name, scope->filteredVariableNames () )
370
370
{
371
- if ( name.startsWith ( QChar ( ' _' ) ) )
372
- continue ;
373
-
374
371
QTreeWidgetItem* item;
375
372
if ( mVariableToItem .contains ( qMakePair ( scopeIndex, name ) ) )
376
373
{
Original file line number Diff line number Diff line change @@ -165,6 +165,10 @@ void TestQgsExpressionContext::contextScope()
165
165
scope.setVariable ( " readonly" , " newvalue" );
166
166
QVERIFY ( scope.isReadOnly ( " readonly" ) );
167
167
168
+ // test retrieving filtered variable names
169
+ scope.setVariable ( " _hidden_" , " hidden" );
170
+ QCOMPARE ( scope.filteredVariableNames (), QStringList () << " readonly" << " notreadonly" << " test" );
171
+
168
172
// removal
169
173
scope.setVariable ( " toremove" , 5 );
170
174
QVERIFY ( scope.hasVariable ( " toremove" ) );
You can’t perform that action at this time.
0 commit comments