/
qgsexpressioncontext.cpp
938 lines (767 loc) · 31.2 KB
/
qgsexpressioncontext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/***************************************************************************
qgsexpressioncontext.cpp
------------------------
Date : April 2015
Copyright : (C) 2015 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsexpressioncontext.h"
#include "qgslogger.h"
#include "qgsexpression.h"
#include "qgsfields.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include "qgssymbollayerutils.h"
#include "qgsgeometry.h"
#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgsatlascomposition.h"
#include "qgsapplication.h"
#include "qgsmapsettings.h"
#include <QSettings>
#include <QDir>
const QString QgsExpressionContext::EXPR_FIELDS( QStringLiteral( "_fields_" ) );
const QString QgsExpressionContext::EXPR_FEATURE( QStringLiteral( "_feature_" ) );
const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( QStringLiteral( "value" ) );
const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( QStringLiteral( "symbol_color" ) );
const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( QStringLiteral( "symbol_angle" ) );
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( QStringLiteral( "geometry_part_count" ) );
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( QStringLiteral( "geometry_part_num" ) );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( QStringLiteral( "geometry_point_count" ) );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( QStringLiteral( "geometry_point_num" ) );
const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( QStringLiteral( "cluster_size" ) );
const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( QStringLiteral( "cluster_color" ) );
//
// QgsExpressionContextScope
//
QgsExpressionContextScope::QgsExpressionContextScope( const QString& name )
: mName( name )
{
}
QgsExpressionContextScope::QgsExpressionContextScope( const QgsExpressionContextScope& other )
: mName( other.mName )
, mVariables( other.mVariables )
{
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
for ( ; it != other.mFunctions.constEnd(); ++it )
{
mFunctions.insert( it.key(), it.value()->clone() );
}
}
QgsExpressionContextScope& QgsExpressionContextScope::operator=( const QgsExpressionContextScope & other )
{
mName = other.mName;
mVariables = other.mVariables;
qDeleteAll( mFunctions );
mFunctions.clear();
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
for ( ; it != other.mFunctions.constEnd(); ++it )
{
mFunctions.insert( it.key(), it.value()->clone() );
}
return *this;
}
QgsExpressionContextScope::~QgsExpressionContextScope()
{
qDeleteAll( mFunctions );
}
void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value )
{
if ( mVariables.contains( name ) )
{
StaticVariable existing = mVariables.value( name );
existing.value = value;
addVariable( existing );
}
else
{
addVariable( QgsExpressionContextScope::StaticVariable( name, value ) );
}
}
void QgsExpressionContextScope::addVariable( const QgsExpressionContextScope::StaticVariable &variable )
{
mVariables.insert( variable.name, variable );
}
bool QgsExpressionContextScope::removeVariable( const QString &name )
{
return mVariables.remove( name ) > 0;
}
bool QgsExpressionContextScope::hasVariable( const QString &name ) const
{
return mVariables.contains( name );
}
QVariant QgsExpressionContextScope::variable( const QString &name ) const
{
return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
}
QStringList QgsExpressionContextScope::variableNames() const
{
QStringList names = mVariables.keys();
return names;
}
bool QgsExpressionContextScope::variableNameSort( const QString& a, const QString& b )
{
return QString::localeAwareCompare( a, b ) < 0;
}
/// @cond PRIVATE
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;
}
bool QgsExpressionContextScope::hasFunction( const QString& name ) const
{
return mFunctions.contains( name );
}
QgsExpression::Function* QgsExpressionContextScope::function( const QString& name ) const
{
return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
}
QStringList QgsExpressionContextScope::functionNames() const
{
return mFunctions.keys();
}
void QgsExpressionContextScope::addFunction( const QString& name, QgsScopedExpressionFunction* function )
{
mFunctions.insert( name, function );
}
void QgsExpressionContextScope::setFeature( const QgsFeature &feature )
{
addVariable( StaticVariable( QgsExpressionContext::EXPR_FEATURE, QVariant::fromValue( feature ), true ) );
}
void QgsExpressionContextScope::setFields( const QgsFields &fields )
{
addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
}
//
// QgsExpressionContext
//
QgsExpressionContext::QgsExpressionContext( const QgsExpressionContext& other )
{
Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
{
mStack << new QgsExpressionContextScope( *scope );
}
mHighlightedVariables = other.mHighlightedVariables;
mCachedValues = other.mCachedValues;
}
QgsExpressionContext& QgsExpressionContext::operator=( QgsExpressionContext && other )
{
if ( this != &other )
{
qDeleteAll( mStack );
// move the stack over
mStack = other.mStack;
other.mStack.clear();
mHighlightedVariables = other.mHighlightedVariables;
mCachedValues = other.mCachedValues;
}
return *this;
}
QgsExpressionContext& QgsExpressionContext::operator=( const QgsExpressionContext & other )
{
qDeleteAll( mStack );
mStack.clear();
Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
{
mStack << new QgsExpressionContextScope( *scope );
}
mHighlightedVariables = other.mHighlightedVariables;
mCachedValues = other.mCachedValues;
return *this;
}
QgsExpressionContext::~QgsExpressionContext()
{
qDeleteAll( mStack );
mStack.clear();
}
bool QgsExpressionContext::hasVariable( const QString& name ) const
{
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
if ( scope->hasVariable( name ) )
return true;
}
return false;
}
QVariant QgsExpressionContext::variable( const QString& name ) const
{
const QgsExpressionContextScope* scope = activeScopeForVariable( name );
return scope ? scope->variable( name ) : QVariant();
}
bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
{
return mHighlightedVariables.contains( name );
}
void QgsExpressionContext::setHighlightedVariables( const QStringList& variableNames )
{
mHighlightedVariables = variableNames;
}
const QgsExpressionContextScope* QgsExpressionContext::activeScopeForVariable( const QString& name ) const
{
//iterate through stack backwards, so that higher priority variables take precedence
QList< QgsExpressionContextScope* >::const_iterator it = mStack.constEnd();
while ( it != mStack.constBegin() )
{
--it;
if (( *it )->hasVariable( name ) )
return ( *it );
}
return nullptr;
}
QgsExpressionContextScope* QgsExpressionContext::activeScopeForVariable( const QString& name )
{
//iterate through stack backwards, so that higher priority variables take precedence
QList< QgsExpressionContextScope* >::const_iterator it = mStack.constEnd();
while ( it != mStack.constBegin() )
{
--it;
if (( *it )->hasVariable( name ) )
return ( *it );
}
return nullptr;
}
QgsExpressionContextScope* QgsExpressionContext::scope( int index )
{
if ( index < 0 || index >= mStack.count() )
return nullptr;
return mStack.at( index );
}
QgsExpressionContextScope *QgsExpressionContext::lastScope()
{
if ( mStack.count() < 1 )
return nullptr;
return mStack.last();
}
int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const
{
if ( !scope )
return -1;
return mStack.indexOf( scope );
}
int QgsExpressionContext::indexOfScope( const QString& scopeName ) const
{
int index = 0;
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
if ( scope->name() == scopeName )
return index;
index++;
}
return -1;
}
QStringList QgsExpressionContext::variableNames() const
{
QStringList names;
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
names << scope->variableNames();
}
return names.toSet().toList();
}
QStringList QgsExpressionContext::filteredVariableNames() const
{
QStringList allVariables = variableNames();
QStringList filtered;
Q_FOREACH ( const QString& variable, allVariables )
{
if ( variable.startsWith( '_' ) )
continue;
filtered << variable;
}
filtered.sort();
return filtered;
}
bool QgsExpressionContext::isReadOnly( const QString& name ) const
{
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
if ( scope->isReadOnly( name ) )
return true;
}
return false;
}
bool QgsExpressionContext::hasFunction( const QString &name ) const
{
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
if ( scope->hasFunction( name ) )
return true;
}
return false;
}
QStringList QgsExpressionContext::functionNames() const
{
QStringList result;
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
{
result << scope->functionNames();
}
result = result.toSet().toList();
result.sort();
return result;
}
QgsExpression::Function *QgsExpressionContext::function( const QString &name ) const
{
//iterate through stack backwards, so that higher priority variables take precedence
QList< QgsExpressionContextScope* >::const_iterator it = mStack.constEnd();
while ( it != mStack.constBegin() )
{
--it;
if (( *it )->hasFunction( name ) )
return ( *it )->function( name );
}
return nullptr;
}
int QgsExpressionContext::scopeCount() const
{
return mStack.count();
}
void QgsExpressionContext::appendScope( QgsExpressionContextScope* scope )
{
mStack.append( scope );
}
QgsExpressionContextScope* QgsExpressionContext::popScope()
{
if ( !mStack.isEmpty() )
return mStack.takeLast();
return nullptr;
}
QgsExpressionContext& QgsExpressionContext::operator<<( QgsExpressionContextScope* scope )
{
mStack.append( scope );
return *this;
}
void QgsExpressionContext::setFeature( const QgsFeature &feature )
{
if ( mStack.isEmpty() )
mStack.append( new QgsExpressionContextScope() );
mStack.last()->setFeature( feature );
}
QgsFeature QgsExpressionContext::feature() const
{
return qvariant_cast<QgsFeature>( variable( QgsExpressionContext::EXPR_FEATURE ) );
}
void QgsExpressionContext::setFields( const QgsFields &fields )
{
if ( mStack.isEmpty() )
mStack.append( new QgsExpressionContextScope() );
mStack.last()->setFields( fields );
}
QgsFields QgsExpressionContext::fields() const
{
return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
}
void QgsExpressionContext::setOriginalValueVariable( const QVariant &value )
{
if ( mStack.isEmpty() )
mStack.append( new QgsExpressionContextScope() );
mStack.last()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_ORIGINAL_VALUE,
value, true ) );
}
void QgsExpressionContext::setCachedValue( const QString& key, const QVariant& value ) const
{
mCachedValues.insert( key, value );
}
bool QgsExpressionContext::hasCachedValue( const QString& key ) const
{
return mCachedValues.contains( key );
}
QVariant QgsExpressionContext::cachedValue( const QString& key ) const
{
return mCachedValues.value( key, QVariant() );
}
void QgsExpressionContext::clearCachedValues() const
{
mCachedValues.clear();
}
//
// QgsExpressionContextUtils
//
QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );
QVariantMap customVariables = QgsApplication::customVariables();
for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
{
scope->setVariable( it.key(), it.value() );
}
//add some extra global variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version" ), Qgis::QGIS_VERSION, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version_no" ), Qgis::QGIS_VERSION_INT, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_release_name" ), Qgis::QGIS_RELEASE_NAME, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_platform" ), QgsApplication::platform(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_os_name" ), QgsApplication::osName(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_account_name" ), QgsApplication::userLoginName(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_full_name" ), QgsApplication::userFullName(), true ) );
return scope;
}
void QgsExpressionContextUtils::setGlobalVariable( const QString& name, const QVariant& value )
{
QgsApplication::setCustomVariable( name, value );
}
void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
{
QgsApplication::setCustomVariables( variables );
}
/// @cond PRIVATE
class GetNamedProjectColor : public QgsScopedExpressionFunction
{
public:
GetNamedProjectColor()
: QgsScopedExpressionFunction( QStringLiteral( "project_color" ), 1, QStringLiteral( "Color" ) )
{
//build up color list from project. Do this in advance for speed
QStringList colorStrings = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) );
QStringList colorLabels = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) );
//generate list from custom colors
int colorIndex = 0;
for ( QStringList::iterator it = colorStrings.begin();
it != colorStrings.end(); ++it )
{
QColor color = QgsSymbolLayerUtils::decodeColor( *it );
QString label;
if ( colorLabels.length() > colorIndex )
{
label = colorLabels.at( colorIndex );
}
mColors.insert( label.toLower(), color );
colorIndex++;
}
}
virtual QVariant func( const QVariantList& values, const QgsExpressionContext*, QgsExpression* ) override
{
QString colorName = values.at( 0 ).toString().toLower();
if ( mColors.contains( colorName ) )
{
return QStringLiteral( "%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
}
else
return QVariant();
}
QgsScopedExpressionFunction* clone() const override
{
return new GetNamedProjectColor();
}
private:
QHash< QString, QColor > mColors;
};
///@endcond
QgsExpressionContextScope* QgsExpressionContextUtils::projectScope()
{
QgsProject* project = QgsProject::instance();
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Project" ) );
const QVariantMap vars = QgsProject::instance()->customVariables();
QVariantMap::const_iterator it = vars.constBegin();
for ( ; it != vars.constEnd(); ++it )
{
scope->setVariable( it.key(), it.value() );
}
//add other known project variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_title" ), project->title(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_path" ), project->fileInfo().filePath(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_folder" ), project->fileInfo().dir().path(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), project->fileInfo().fileName(), true ) );
QgsCoordinateReferenceSystem projectCrs = project->crs();
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true ) );
scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor() );
return scope;
}
void QgsExpressionContextUtils::setProjectVariable( const QString& name, const QVariant& value )
{
QgsProject* project = QgsProject::instance();
QVariantMap vars = project->customVariables();
vars.insert( name, value );
project->setCustomVariables( vars );
}
void QgsExpressionContextUtils::setProjectVariables( const QVariantMap& variables )
{
QgsProject::instance()->setCustomVariables( variables );
}
QgsExpressionContextScope* QgsExpressionContextUtils::layerScope( const QgsMapLayer* layer )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );
if ( !layer )
return scope;
//add variables defined in layer properties
QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
int varIndex = 0;
Q_FOREACH ( const QString& variableName, variableNames )
{
if ( varIndex >= variableValues.length() )
{
break;
}
QVariant varValue = variableValues.at( varIndex );
varIndex++;
scope->setVariable( variableName, varValue );
}
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QPointer<QgsMapLayer> >( QPointer<QgsMapLayer>( const_cast<QgsMapLayer*>( layer ) ) ), true ) );
const QgsVectorLayer* vLayer = dynamic_cast< const QgsVectorLayer* >( layer );
if ( vLayer )
{
scope->setFields( vLayer->fields() );
}
//TODO - add functions. Possibilities include:
//is_selected
//field summary stats
return scope;
}
void QgsExpressionContextUtils::setLayerVariable( QgsMapLayer* layer, const QString& name, const QVariant& value )
{
if ( !layer )
return;
//write variable to layer
QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
variableNames << name;
variableValues << value.toString();
layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer* layer, const QVariantMap& variables )
{
if ( !layer )
return;
QStringList variableNames;
QStringList variableValues;
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value().toString();
}
layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const QgsMapSettings& mapSettings )
{
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsComposerMap::createExpressionContext()
// (rationale is described in QgsComposerMap::createExpressionContext() )
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
//add known map settings context variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
QgsGeometry centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
return scope;
}
QgsExpressionContextScope* QgsExpressionContextUtils::updateSymbolScope( const QgsSymbol* symbol, QgsExpressionContextScope* symbolScope )
{
if ( !symbolScope )
return nullptr;
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbol ? symbol->color() : QColor(), true ) );
double angle = 0.0;
const QgsMarkerSymbol* markerSymbol = dynamic_cast< const QgsMarkerSymbol* >( symbol );
if ( markerSymbol )
{
angle = markerSymbol->angle();
}
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_ANGLE, angle, true ) );
return symbolScope;
}
QgsExpressionContextScope *QgsExpressionContextUtils::compositionScope( const QgsComposition *composition )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) );
if ( !composition )
return scope;
//add variables defined in composition properties
QStringList variableNames = composition->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = composition->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
int varIndex = 0;
Q_FOREACH ( const QString& variableName, variableNames )
{
if ( varIndex >= variableValues.length() )
{
break;
}
QVariant varValue = variableValues.at( varIndex );
varIndex++;
scope->setVariable( variableName, varValue );
}
//add known composition context variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_numpages" ), composition->numPages(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), composition->paperHeight(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), composition->paperWidth(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), composition->printResolution(), true ) );
return scope;
}
void QgsExpressionContextUtils::setCompositionVariable( QgsComposition* composition, const QString& name, const QVariant& value )
{
if ( !composition )
return;
//write variable to composition
QStringList variableNames = composition->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = composition->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
variableNames << name;
variableValues << value.toString();
composition->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
composition->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
void QgsExpressionContextUtils::setCompositionVariables( QgsComposition* composition, const QVariantMap& variables )
{
if ( !composition )
return;
QStringList variableNames;
QStringList variableValues;
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value().toString();
}
composition->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
composition->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
QgsExpressionContextScope* QgsExpressionContextUtils::atlasScope( const QgsAtlasComposition* atlas )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) );
if ( !atlas )
{
//add some dummy atlas variables. This is done so that as in certain contexts we want to show
//users that these variables are available even if they have no current value
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), QString(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( QgsFeature() ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), 0, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( QgsGeometry() ), true ) );
return scope;
}
//add known atlas variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_totalfeatures" ), atlas->numFeatures(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featurenumber" ), atlas->currentFeatureNumber() + 1, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_filename" ), atlas->currentFilename(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), atlas->currentPageName(), true ) );
if ( atlas->enabled() && atlas->coverageLayer() )
{
scope->setFields( atlas->coverageLayer()->fields() );
}
if ( atlas->enabled() )
{
QgsFeature atlasFeature = atlas->feature();
scope->setFeature( atlasFeature );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), atlasFeature.id(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
}
return scope;
}
QgsExpressionContextScope *QgsExpressionContextUtils::composerItemScope( const QgsComposerItem *composerItem )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composer Item" ) );
if ( !composerItem )
return scope;
//add variables defined in composer item properties
QStringList variableNames = composerItem->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = composerItem->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
int varIndex = 0;
Q_FOREACH ( const QString& variableName, variableNames )
{
if ( varIndex >= variableValues.length() )
{
break;
}
QVariant varValue = variableValues.at( varIndex );
varIndex++;
scope->setVariable( variableName, varValue );
}
//add known composer item context variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_id" ), composerItem->id(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_uuid" ), composerItem->uuid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_page" ), composerItem->page(), true ) );
return scope;
}
void QgsExpressionContextUtils::setComposerItemVariable( QgsComposerItem* composerItem, const QString& name, const QVariant& value )
{
if ( !composerItem )
return;
//write variable to composer item
QStringList variableNames = composerItem->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = composerItem->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
variableNames << name;
variableValues << value.toString();
composerItem->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
composerItem->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
void QgsExpressionContextUtils::setComposerItemVariables( QgsComposerItem* composerItem, const QVariantMap& variables )
{
if ( !composerItem )
return;
QStringList variableNames;
QStringList variableValues;
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value().toString();
}
composerItem->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
composerItem->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}
QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope();
scope->setFeature( feature );
scope->setFields( fields );
return QgsExpressionContext() << scope;
}
void QgsExpressionContextUtils::registerContextFunctions()
{
QgsExpression::registerFunction( new GetNamedProjectColor() );
}
bool QgsScopedExpressionFunction::usesGeometry( const QgsExpression::NodeFunction* node ) const
{
Q_UNUSED( node )
return mUsesGeometry;
}
QSet<QString> QgsScopedExpressionFunction::referencedColumns( const QgsExpression::NodeFunction* node ) const
{
Q_UNUSED( node )
return mReferencedColumns;
}