@@ -47,6 +47,7 @@ class TestTransformer : public QgsPropertyTransformer
47
47
{
48
48
return new TestTransformer ( mMinValue , mMaxValue );
49
49
}
50
+ virtual QString toExpression ( const QString& ) const override { return QString (); }
50
51
51
52
private:
52
53
@@ -80,6 +81,7 @@ class TestQgsProperty : public QObject
80
81
void propertyTransformer (); // test for QgsPropertyTransformer
81
82
void sizeScaleTransformer (); // test for QgsSizeScaleTransformer
82
83
void colorRampTransformer (); // test for QgsColorRampTransformer
84
+ void asExpression (); // test converting property to expression
83
85
void propertyCollection (); // test for QgsPropertyCollection
84
86
void collectionStack (); // test for QgsPropertyCollectionStack
85
87
@@ -729,6 +731,19 @@ void TestQgsProperty::sizeScaleTransformer()
729
731
QCOMPARE ( t.size ( 100 ), 10.0 );
730
732
QVERIFY ( qgsDoubleNear ( t.size ( 150 ), 13.5355 , 0.001 ) );
731
733
QCOMPARE ( t.size ( 200 ), 20.0 );
734
+
735
+ // as expression
736
+ QgsSizeScaleTransformer t2 ( QgsSizeScaleTransformer::Linear,
737
+ 15 ,
738
+ 25 ,
739
+ 150 ,
740
+ 250 ,
741
+ -10 ,
742
+ 1.6 );
743
+ QCOMPARE ( t2.toExpression ( " 5+6" ), QStringLiteral ( " coalesce(scale_linear(5+6, 15, 25, 150, 250), -10)" ) );
744
+ t2.setType ( QgsSizeScaleTransformer::Exponential );
745
+ t2.setExponent ( 1.6 );
746
+ QCOMPARE ( t2.toExpression ( " 5+6" ), QStringLiteral ( " coalesce(scale_exp(5+6, 15, 25, 150, 250, 1.6), -10)" ) );
732
747
}
733
748
734
749
void TestQgsProperty::colorRampTransformer ()
@@ -765,6 +780,7 @@ void TestQgsProperty::colorRampTransformer()
765
780
25 ,
766
781
new QgsGradientColorRamp ( QColor ( 10 , 20 , 30 ), QColor ( 200 , 190 , 180 ) ),
767
782
QColor ( 100 , 150 , 200 ) );
783
+ t1.setRampName ( " rampname " );
768
784
769
785
QDomElement element = doc.createElement ( " xform" );
770
786
QVERIFY ( t1.writeXml ( element, doc ) );
@@ -773,6 +789,7 @@ void TestQgsProperty::colorRampTransformer()
773
789
QCOMPARE ( r1.minValue (), 15.0 );
774
790
QCOMPARE ( r1.maxValue (), 25.0 );
775
791
QCOMPARE ( r1.nullColor (), QColor ( 100 , 150 , 200 ) );
792
+ QCOMPARE ( r1.rampName (), QString ( " rampname " ) );
776
793
QVERIFY ( dynamic_cast < QgsGradientColorRamp* >( r1.colorRamp () ) );
777
794
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r1.colorRamp () )->color1 (), QColor ( 10 , 20 , 30 ) );
778
795
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r1.colorRamp () )->color2 (), QColor ( 200 , 190 , 180 ) );
@@ -782,6 +799,7 @@ void TestQgsProperty::colorRampTransformer()
782
799
QCOMPARE ( r2->minValue (), 15.0 );
783
800
QCOMPARE ( r2->maxValue (), 25.0 );
784
801
QCOMPARE ( r2->nullColor (), QColor ( 100 , 150 , 200 ) );
802
+ QCOMPARE ( r2->rampName (), QString ( " rampname " ) );
785
803
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r2->colorRamp () )->color1 (), QColor ( 10 , 20 , 30 ) );
786
804
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r2->colorRamp () )->color2 (), QColor ( 200 , 190 , 180 ) );
787
805
@@ -790,6 +808,7 @@ void TestQgsProperty::colorRampTransformer()
790
808
QCOMPARE ( r3.minValue (), 15.0 );
791
809
QCOMPARE ( r3.maxValue (), 25.0 );
792
810
QCOMPARE ( r3.nullColor (), QColor ( 100 , 150 , 200 ) );
811
+ QCOMPARE ( r3.rampName (), QString ( " rampname " ) );
793
812
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r3.colorRamp () )->color1 (), QColor ( 10 , 20 , 30 ) );
794
813
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r3.colorRamp () )->color2 (), QColor ( 200 , 190 , 180 ) );
795
814
@@ -799,6 +818,7 @@ void TestQgsProperty::colorRampTransformer()
799
818
QCOMPARE ( r4.minValue (), 15.0 );
800
819
QCOMPARE ( r4.maxValue (), 25.0 );
801
820
QCOMPARE ( r4.nullColor (), QColor ( 100 , 150 , 200 ) );
821
+ QCOMPARE ( r4.rampName (), QString ( " rampname " ) );
802
822
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r4.colorRamp () )->color1 (), QColor ( 10 , 20 , 30 ) );
803
823
QCOMPARE ( static_cast < QgsGradientColorRamp* >( r4.colorRamp () )->color2 (), QColor ( 200 , 190 , 180 ) );
804
824
@@ -814,13 +834,51 @@ void TestQgsProperty::colorRampTransformer()
814
834
QCOMPARE ( t.nullColor (), QColor ( 1 , 10 , 11 , 21 ) );
815
835
t.setColorRamp ( new QgsGradientColorRamp ( QColor ( 10 , 20 , 100 ), QColor ( 100 , 200 , 200 ) ) );
816
836
QCOMPARE ( static_cast < QgsGradientColorRamp* >( t.colorRamp () )->color1 (), QColor ( 10 , 20 , 100 ) );
837
+ t.setRampName ( " colorramp" );
838
+ QCOMPARE ( t.rampName (), QString ( " colorramp" ) );
817
839
818
840
// test colors
819
841
QCOMPARE ( t.color ( 50 ), QColor ( 10 , 20 , 100 ) ); // out of range
820
842
QCOMPARE ( t.color ( 100 ), QColor ( 10 , 20 , 100 ) );
821
843
QCOMPARE ( t.color ( 150 ), QColor ( 55 , 110 , 150 ) );
822
844
QCOMPARE ( t.color ( 200 ), QColor ( 100 , 200 , 200 ) );
823
845
QCOMPARE ( t.color ( 250 ), QColor ( 100 , 200 , 200 ) ); // out of range
846
+
847
+ // toExpression
848
+ QgsColorRampTransformer t5 ( 15 ,
849
+ 25 ,
850
+ new QgsGradientColorRamp ( QColor ( 10 , 20 , 30 ), QColor ( 200 , 190 , 180 ) ),
851
+ QColor ( 100 , 150 , 200 ) );
852
+ QCOMPARE ( t5.toExpression ( " 5+6" ), QStringLiteral ( " coalesce(ramp_color('custom ramp',scale_linear(5+6, 15, 25, 0, 1), '#6496c8')" ) );
853
+ t5.setRampName ( " my ramp" );
854
+ QCOMPARE ( t5.toExpression ( " 5+6" ), QStringLiteral ( " coalesce(ramp_color('my ramp',scale_linear(5+6, 15, 25, 0, 1), '#6496c8')" ) );
855
+ }
856
+
857
+ void TestQgsProperty::asExpression ()
858
+ {
859
+ // static property
860
+ QgsProperty p = QgsProperty::fromValue ( 5 );
861
+ QCOMPARE ( p.asExpression (), QStringLiteral ( " 5" ) );
862
+ p = QgsProperty::fromValue ( " value" );
863
+ QCOMPARE ( p.asExpression (), QStringLiteral ( " 'value'" ) );
864
+
865
+ // field based property
866
+ p = QgsProperty::fromField ( " a field" );
867
+ QCOMPARE ( p.asExpression (), QStringLiteral ( " \" a field\" " ) );
868
+
869
+ // expression based property
870
+ p = QgsProperty::fromExpression ( " 5 + 6" );
871
+ QCOMPARE ( p.asExpression (), QStringLiteral ( " 5 + 6" ) );
872
+
873
+ // with transformer
874
+ p.setTransformer ( new QgsSizeScaleTransformer ( QgsSizeScaleTransformer::Linear,
875
+ 15 ,
876
+ 25 ,
877
+ 150 ,
878
+ 250 ,
879
+ -10 ,
880
+ 1 ) );
881
+ QCOMPARE ( p.asExpression (), QStringLiteral ( " coalesce(scale_linear(5 + 6, 15, 25, 150, 250), -10)" ) );
824
882
}
825
883
826
884
void TestQgsProperty::propertyCollection ()
0 commit comments