[layouts] Add option to filter "add layer to legend" dialog to

layers visible within the (linked) map only

Fixes #20186
This commit is contained in:
Nyall Dawson 2018-10-23 10:56:55 +10:00
parent de0e74be59
commit 8cf64c8a51
6 changed files with 66 additions and 11 deletions

View File

@ -470,6 +470,12 @@ will be calculated. This can be expensive to calculate, so if they are not requi
virtual void finalizeRestoreFromXml();
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = 0 ) const;
%Docstring
Returns a list of the layers which will be rendered within this map item, considering
any locked layers, linked map theme, and data defined settings.
%End
protected:
virtual void draw( QgsLayoutItemRenderContext &context );

View File

@ -38,6 +38,7 @@ QgsLayoutLegendLayersDialog::QgsLayoutLegendLayersDialog( QWidget *parent )
connect( listMapLayers, &QListView::doubleClicked, this, &QgsLayoutLegendLayersDialog::accept );
connect( mFilterLineEdit, &QLineEdit::textChanged, mModel, &QgsMapLayerProxyModel::setFilterString );
connect( mCheckBoxVisibleLayers, &QCheckBox::toggled, this, &QgsLayoutLegendLayersDialog::filterVisible );
}
QgsLayoutLegendLayersDialog::~QgsLayoutLegendLayersDialog()
@ -46,6 +47,11 @@ QgsLayoutLegendLayersDialog::~QgsLayoutLegendLayersDialog()
settings.setValue( QStringLiteral( "Windows/LayoutLegendLayers/geometry" ), saveGeometry() );
}
void QgsLayoutLegendLayersDialog::setVisibleLayers( const QList<QgsMapLayer *> &layers )
{
mVisibleLayers = layers;
}
QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
{
QList< QgsMapLayer * > layers;
@ -65,3 +71,11 @@ QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
}
return layers;
}
void QgsLayoutLegendLayersDialog::filterVisible( bool enabled )
{
if ( enabled )
mModel->setLayerWhitelist( mVisibleLayers );
else
mModel->setLayerWhitelist( QList< QgsMapLayer * >() );
}

View File

@ -32,12 +32,22 @@ class QgsLayoutLegendLayersDialog: public QDialog, private Ui::QgsLayoutLegendLa
QgsLayoutLegendLayersDialog( QWidget *parent = nullptr );
~QgsLayoutLegendLayersDialog() override;
/**
* Sets a list of visible \a layers, to use for filtering within the dialog.
*/
void setVisibleLayers( const QList<QgsMapLayer *> &layers );
QList< QgsMapLayer * > selectedLayers() const;
private slots:
void filterVisible( bool enabled );
private:
QgsLayoutLegendLayersDialog() = delete;
QgsMapLayerProxyModel *mModel = nullptr;
QList< QgsMapLayer * > mVisibleLayers;
};
#endif //QGSLAYOUTLEGENDLAYERSDIALOG_H

View File

@ -699,7 +699,19 @@ void QgsLayoutLegendWidget::mAddToolButton_clicked()
return;
}
QList< QgsMapLayer * > visibleLayers;
if ( mLegend->linkedMap() )
{
visibleLayers = mLegend->linkedMap()->layersToRender();
}
if ( visibleLayers.isEmpty() )
{
// just use current canvas layers as visible layers
visibleLayers = QgisApp::instance()->mapCanvas()->layers();
}
QgsLayoutLegendLayersDialog addDialog( this );
addDialog.setVisibleLayers( visibleLayers );
if ( addDialog.exec() == QDialog::Accepted )
{
const QList<QgsMapLayer *> layers = addDialog.selectedLayers();

View File

@ -417,6 +417,12 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
void finalizeRestoreFromXml() override;
/**
* Returns a list of the layers which will be rendered within this map item, considering
* any locked layers, linked map theme, and data defined settings.
*/
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
protected:
void draw( QgsLayoutItemRenderContext &context ) override;
@ -619,9 +625,6 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
//! Resets the item tooltip to reflect current map id
void updateToolTip();
//! Returns a list of the layers to render for this map item
QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
//! Returns current layer style overrides for this map item
QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;

View File

@ -17,7 +17,14 @@
<property name="spacing">
<number>6</number>
</property>
<item row="2" column="0">
<item row="1" column="0">
<widget class="QListView" name="listMapLayers">
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -27,13 +34,6 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QListView" name="listMapLayers">
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QgsFilterLineEdit" name="mFilterLineEdit">
<property name="placeholderText">
@ -41,6 +41,16 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="mCheckBoxVisibleLayers">
<property name="toolTip">
<string>If checked, only layers visible within the map will be listed</string>
</property>
<property name="text">
<string>Show visible layers only</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>