Skip to content
Permalink
Browse files

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

layers visible within the (linked) map only

Fixes #20186
  • Loading branch information
nyalldawson committed Oct 23, 2018
1 parent de0e74b commit 8cf64c8a51d7d2d9f6c60f2632ac4e20f27f595d
@@ -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 );
@@ -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 * >() );
}
@@ -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
@@ -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();
@@ -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;

@@ -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,20 +34,23 @@
</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">
<string>Search</string>
</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>

0 comments on commit 8cf64c8

Please sign in to comment.
You can’t perform that action at this time.