28
28
#include " qgisapp.h"
29
29
#include " qgsapplication.h"
30
30
#include " qgslayertree.h"
31
+ #include " qgslayertreeutils.h"
31
32
#include " qgslayertreemodel.h"
32
33
#include " qgslayertreemodellegendnode.h"
33
34
#include " qgslegendrenderer.h"
@@ -119,6 +120,10 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend )
119
120
mItemTreeView ->setMenuProvider ( new QgsComposerLegendMenuProvider ( mItemTreeView , this ) );
120
121
connect ( legend, SIGNAL ( itemChanged () ), this , SLOT ( setGuiElements () ) );
121
122
mWrapCharLineEdit ->setText ( legend->wrapChar () );
123
+
124
+ // connect atlas state to the filter legend by atlas checkbox
125
+ connect ( &legend->composition ()->atlasComposition (), SIGNAL ( toggled ( bool ) ), this , SLOT ( updateFilterLegendByAtlasButton () ) );
126
+ connect ( &legend->composition ()->atlasComposition (), SIGNAL ( coverageLayerChanged ( QgsVectorLayer* ) ), this , SLOT ( updateFilterLegendByAtlasButton () ) );
122
127
}
123
128
124
129
setGuiElements ();
@@ -574,15 +579,22 @@ void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
574
579
575
580
mLegend ->setAutoUpdateModel ( state == Qt::Checked );
576
581
577
- mLegend ->update ();
582
+ mLegend ->updateItem ();
578
583
mLegend ->endCommand ();
579
584
580
585
// do not allow editing of model if auto update is on - we would modify project's layer tree
581
586
QList<QWidget*> widgets;
582
587
widgets << mMoveDownToolButton << mMoveUpToolButton << mRemoveToolButton << mAddToolButton
583
- << mEditPushButton << mCountToolButton << mUpdateAllPushButton << mAddGroupToolButton ;
588
+ << mEditPushButton << mCountToolButton << mUpdateAllPushButton << mAddGroupToolButton
589
+ << mExpressionFilterButton ;
584
590
Q_FOREACH ( QWidget* w, widgets )
585
591
w->setEnabled ( state != Qt::Checked );
592
+
593
+ if ( state == Qt::Unchecked )
594
+ {
595
+ // update widgets state based on current selection
596
+ selectedChanged ( QModelIndex (), QModelIndex () );
597
+ }
586
598
}
587
599
588
600
void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged ( int index )
@@ -616,7 +628,7 @@ void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged( int index )
616
628
{
617
629
mLegend ->beginCommand ( tr ( " Legend map changed" ) );
618
630
mLegend ->setComposerMap ( map );
619
- mLegend ->update ();
631
+ mLegend ->updateItem ();
620
632
mLegend ->endCommand ();
621
633
}
622
634
}
@@ -748,7 +760,7 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
748
760
}
749
761
750
762
mLegend ->adjustBoxSize ();
751
- mLegend ->update ();
763
+ mLegend ->updateItem ();
752
764
mLegend ->endCommand ();
753
765
}
754
766
@@ -811,7 +823,7 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
811
823
}
812
824
813
825
mLegend ->adjustBoxSize ();
814
- mLegend ->update ();
826
+ mLegend ->updateItem ();
815
827
mLegend ->endCommand ();
816
828
}
817
829
@@ -853,7 +865,7 @@ void QgsComposerLegendWidget::resetLayerNodeToDefaults()
853
865
854
866
mItemTreeView ->layerTreeModel ()->refreshLayerLegend ( nodeLayer );
855
867
856
- mLegend ->update ();
868
+ mLegend ->updateItem ();
857
869
mLegend ->adjustBoxSize ();
858
870
mLegend ->endCommand ();
859
871
}
@@ -879,16 +891,43 @@ void QgsComposerLegendWidget::on_mCountToolButton_clicked( bool checked )
879
891
880
892
mLegend ->beginCommand ( tr ( " Legend updated" ) );
881
893
currentNode->setCustomProperty ( " showFeatureCount" , checked ? 1 : 0 );
882
- mLegend ->update ();
894
+ mLegend ->updateItem ();
883
895
mLegend ->adjustBoxSize ();
884
896
mLegend ->endCommand ();
885
897
}
886
898
887
- void QgsComposerLegendWidget::on_mFilterByMapToolButton_clicked ( bool checked )
899
+ void QgsComposerLegendWidget::on_mFilterByMapToolButton_toggled ( bool checked )
888
900
{
889
901
mLegend ->beginCommand ( tr ( " Legend updated" ) );
890
902
mLegend ->setLegendFilterByMapEnabled ( checked );
891
- mLegend ->update ();
903
+ mLegend ->adjustBoxSize ();
904
+ mLegend ->endCommand ();
905
+ }
906
+
907
+ void QgsComposerLegendWidget::on_mExpressionFilterButton_toggled ( bool checked )
908
+ {
909
+ if ( !mLegend )
910
+ {
911
+ return ;
912
+ }
913
+
914
+ // get current item
915
+ QModelIndex currentIndex = mItemTreeView ->currentIndex ();
916
+ if ( !currentIndex.isValid () )
917
+ {
918
+ return ;
919
+ }
920
+
921
+ QgsLayerTreeNode* currentNode = mItemTreeView ->currentNode ();
922
+ if ( !QgsLayerTree::isLayer ( currentNode ) )
923
+ return ;
924
+
925
+ QgsLayerTreeUtils::setLegendFilterByExpression ( *qobject_cast<QgsLayerTreeLayer*>( currentNode ),
926
+ mExpressionFilterButton ->expressionText (),
927
+ checked );
928
+
929
+ mLegend ->beginCommand ( tr ( " Legend updated" ) );
930
+ mLegend ->updateItem ();
892
931
mLegend ->adjustBoxSize ();
893
932
mLegend ->endCommand ();
894
933
}
@@ -904,11 +943,24 @@ void QgsComposerLegendWidget::on_mAddGroupToolButton_clicked()
904
943
{
905
944
mLegend ->beginCommand ( tr ( " Legend group added" ) );
906
945
mLegend ->modelV2 ()->rootGroup ()->addGroup ( tr ( " Group" ) );
907
- mLegend ->update ();
946
+ mLegend ->updateItem ();
908
947
mLegend ->endCommand ();
909
948
}
910
949
}
911
950
951
+ void QgsComposerLegendWidget::on_mFilterLegendByAtlasCheckBox_toggled ( bool toggled )
952
+ {
953
+ if ( mLegend )
954
+ {
955
+ mLegend ->setLegendFilterOutAtlas ( toggled );
956
+ // force update of legend when in preview mode
957
+ if ( mLegend ->composition ()->atlasMode () == QgsComposition::PreviewAtlas )
958
+ {
959
+ mLegend ->composition ()->atlasComposition ().refreshFeature ();
960
+ }
961
+ }
962
+ }
963
+
912
964
void QgsComposerLegendWidget::updateLegend ()
913
965
{
914
966
if ( mLegend )
@@ -918,8 +970,7 @@ void QgsComposerLegendWidget::updateLegend()
918
970
// this will reset the model completely, loosing any changes
919
971
mLegend ->setAutoUpdateModel ( true );
920
972
mLegend ->setAutoUpdateModel ( false );
921
-
922
- mLegend ->update ();
973
+ mLegend ->updateItem ();
923
974
mLegend ->endCommand ();
924
975
}
925
976
}
@@ -997,11 +1048,16 @@ void QgsComposerLegendWidget::selectedChanged( const QModelIndex & current, cons
997
1048
Q_UNUSED ( previous );
998
1049
QgsDebugMsg ( " Entered" );
999
1050
1051
+ if ( mLegend && mLegend ->autoUpdateModel () )
1052
+ return ;
1053
+
1000
1054
mCountToolButton ->setChecked ( false );
1001
1055
mCountToolButton ->setEnabled ( false );
1002
1056
1003
- if ( mLegend && mLegend ->autoUpdateModel () )
1004
- return ;
1057
+ mExpressionFilterButton ->blockSignals ( true );
1058
+ mExpressionFilterButton ->setChecked ( false );
1059
+ mExpressionFilterButton ->setEnabled ( false );
1060
+ mExpressionFilterButton ->blockSignals ( false );
1005
1061
1006
1062
QgsLayerTreeNode* currentNode = mItemTreeView ->currentNode ();
1007
1063
if ( !QgsLayerTree::isLayer ( currentNode ) )
@@ -1014,6 +1070,15 @@ void QgsComposerLegendWidget::selectedChanged( const QModelIndex & current, cons
1014
1070
1015
1071
mCountToolButton ->setChecked ( currentNode->customProperty ( " showFeatureCount" , 0 ).toInt () );
1016
1072
mCountToolButton ->setEnabled ( true );
1073
+
1074
+ bool exprEnabled;
1075
+ QString expr = QgsLayerTreeUtils::legendFilterByExpression ( *qobject_cast<QgsLayerTreeLayer*>( currentNode ), &exprEnabled );
1076
+ mExpressionFilterButton ->blockSignals ( true );
1077
+ mExpressionFilterButton ->setExpressionText ( expr );
1078
+ mExpressionFilterButton ->setVectorLayer ( vl );
1079
+ mExpressionFilterButton ->setEnabled ( true );
1080
+ mExpressionFilterButton ->setChecked ( exprEnabled );
1081
+ mExpressionFilterButton ->blockSignals ( false );
1017
1082
}
1018
1083
1019
1084
void QgsComposerLegendWidget::setCurrentNodeStyleFromAction ()
@@ -1023,5 +1088,11 @@ void QgsComposerLegendWidget::setCurrentNodeStyleFromAction()
1023
1088
return ;
1024
1089
1025
1090
QgsLegendRenderer::setNodeLegendStyle ( mItemTreeView ->currentNode (), ( QgsComposerLegendStyle::Style ) a->data ().toInt () );
1026
- mLegend ->update ();
1091
+ mLegend ->updateItem ();
1092
+ }
1093
+
1094
+ void QgsComposerLegendWidget::updateFilterLegendByAtlasButton ()
1095
+ {
1096
+ const QgsAtlasComposition& atlas = mLegend ->composition ()->atlasComposition ();
1097
+ mFilterLegendByAtlasCheckBox ->setEnabled ( atlas.enabled () && atlas.coverageLayer () && atlas.coverageLayer ()->geometryType () == QGis::Polygon );
1027
1098
}
0 commit comments