Skip to content

Commit 8cf64c8

Browse files
committed
[layouts] Add option to filter "add layer to legend" dialog to
layers visible within the (linked) map only Fixes #20186
1 parent de0e74b commit 8cf64c8

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

python/core/auto_generated/layout/qgslayoutitemmap.sip.in

+6
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ will be calculated. This can be expensive to calculate, so if they are not requi
470470
virtual void finalizeRestoreFromXml();
471471

472472

473+
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = 0 ) const;
474+
%Docstring
475+
Returns a list of the layers which will be rendered within this map item, considering
476+
any locked layers, linked map theme, and data defined settings.
477+
%End
478+
473479
protected:
474480

475481
virtual void draw( QgsLayoutItemRenderContext &context );

src/app/layout/qgslayoutlegendlayersdialog.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ QgsLayoutLegendLayersDialog::QgsLayoutLegendLayersDialog( QWidget *parent )
3838
connect( listMapLayers, &QListView::doubleClicked, this, &QgsLayoutLegendLayersDialog::accept );
3939

4040
connect( mFilterLineEdit, &QLineEdit::textChanged, mModel, &QgsMapLayerProxyModel::setFilterString );
41+
connect( mCheckBoxVisibleLayers, &QCheckBox::toggled, this, &QgsLayoutLegendLayersDialog::filterVisible );
4142
}
4243

4344
QgsLayoutLegendLayersDialog::~QgsLayoutLegendLayersDialog()
@@ -46,6 +47,11 @@ QgsLayoutLegendLayersDialog::~QgsLayoutLegendLayersDialog()
4647
settings.setValue( QStringLiteral( "Windows/LayoutLegendLayers/geometry" ), saveGeometry() );
4748
}
4849

50+
void QgsLayoutLegendLayersDialog::setVisibleLayers( const QList<QgsMapLayer *> &layers )
51+
{
52+
mVisibleLayers = layers;
53+
}
54+
4955
QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
5056
{
5157
QList< QgsMapLayer * > layers;
@@ -65,3 +71,11 @@ QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
6571
}
6672
return layers;
6773
}
74+
75+
void QgsLayoutLegendLayersDialog::filterVisible( bool enabled )
76+
{
77+
if ( enabled )
78+
mModel->setLayerWhitelist( mVisibleLayers );
79+
else
80+
mModel->setLayerWhitelist( QList< QgsMapLayer * >() );
81+
}

src/app/layout/qgslayoutlegendlayersdialog.h

+10
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ class QgsLayoutLegendLayersDialog: public QDialog, private Ui::QgsLayoutLegendLa
3232
QgsLayoutLegendLayersDialog( QWidget *parent = nullptr );
3333
~QgsLayoutLegendLayersDialog() override;
3434

35+
/**
36+
* Sets a list of visible \a layers, to use for filtering within the dialog.
37+
*/
38+
void setVisibleLayers( const QList<QgsMapLayer *> &layers );
39+
3540
QList< QgsMapLayer * > selectedLayers() const;
3641

42+
private slots:
43+
44+
void filterVisible( bool enabled );
45+
3746
private:
3847
QgsLayoutLegendLayersDialog() = delete;
3948

4049
QgsMapLayerProxyModel *mModel = nullptr;
50+
QList< QgsMapLayer * > mVisibleLayers;
4151
};
4252

4353
#endif //QGSLAYOUTLEGENDLAYERSDIALOG_H

src/app/layout/qgslayoutlegendwidget.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,19 @@ void QgsLayoutLegendWidget::mAddToolButton_clicked()
699699
return;
700700
}
701701

702+
QList< QgsMapLayer * > visibleLayers;
703+
if ( mLegend->linkedMap() )
704+
{
705+
visibleLayers = mLegend->linkedMap()->layersToRender();
706+
}
707+
if ( visibleLayers.isEmpty() )
708+
{
709+
// just use current canvas layers as visible layers
710+
visibleLayers = QgisApp::instance()->mapCanvas()->layers();
711+
}
712+
702713
QgsLayoutLegendLayersDialog addDialog( this );
714+
addDialog.setVisibleLayers( visibleLayers );
703715
if ( addDialog.exec() == QDialog::Accepted )
704716
{
705717
const QList<QgsMapLayer *> layers = addDialog.selectedLayers();

src/core/layout/qgslayoutitemmap.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
417417

418418
void finalizeRestoreFromXml() override;
419419

420+
/**
421+
* Returns a list of the layers which will be rendered within this map item, considering
422+
* any locked layers, linked map theme, and data defined settings.
423+
*/
424+
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
425+
420426
protected:
421427

422428
void draw( QgsLayoutItemRenderContext &context ) override;
@@ -619,9 +625,6 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
619625
//! Resets the item tooltip to reflect current map id
620626
void updateToolTip();
621627

622-
//! Returns a list of the layers to render for this map item
623-
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
624-
625628
//! Returns current layer style overrides for this map item
626629
QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
627630

src/ui/layout/qgslayoutlegendlayersdialogbase.ui

+18-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
<property name="spacing">
1818
<number>6</number>
1919
</property>
20-
<item row="2" column="0">
20+
<item row="1" column="0">
21+
<widget class="QListView" name="listMapLayers">
22+
<property name="selectionMode">
23+
<enum>QAbstractItemView::ExtendedSelection</enum>
24+
</property>
25+
</widget>
26+
</item>
27+
<item row="3" column="0">
2128
<widget class="QDialogButtonBox" name="buttonBox">
2229
<property name="orientation">
2330
<enum>Qt::Horizontal</enum>
@@ -27,20 +34,23 @@
2734
</property>
2835
</widget>
2936
</item>
30-
<item row="1" column="0">
31-
<widget class="QListView" name="listMapLayers">
32-
<property name="selectionMode">
33-
<enum>QAbstractItemView::ExtendedSelection</enum>
34-
</property>
35-
</widget>
36-
</item>
3737
<item row="0" column="0">
3838
<widget class="QgsFilterLineEdit" name="mFilterLineEdit">
3939
<property name="placeholderText">
4040
<string>Search</string>
4141
</property>
4242
</widget>
4343
</item>
44+
<item row="2" column="0">
45+
<widget class="QCheckBox" name="mCheckBoxVisibleLayers">
46+
<property name="toolTip">
47+
<string>If checked, only layers visible within the map will be listed</string>
48+
</property>
49+
<property name="text">
50+
<string>Show visible layers only</string>
51+
</property>
52+
</widget>
53+
</item>
4454
</layout>
4555
</widget>
4656
<customwidgets>

0 commit comments

Comments
 (0)