Skip to content

Commit

Permalink
[layouts] Add option to filter "add layer to legend" dialog to
Browse files Browse the repository at this point in the history
layers visible within the (linked) map only

Fixes #20186
  • Loading branch information
nyalldawson committed Oct 23, 2018
1 parent de0e74b commit 8cf64c8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
6 changes: 6 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemmap.sip.in
Expand Up @@ -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 );
Expand Down
14 changes: 14 additions & 0 deletions src/app/layout/qgslayoutlegendlayersdialog.cpp
Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -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 * >() );
}
10 changes: 10 additions & 0 deletions src/app/layout/qgslayoutlegendlayersdialog.h
Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/app/layout/qgslayoutlegendwidget.cpp
Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions src/core/layout/qgslayoutitemmap.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
26 changes: 18 additions & 8 deletions src/ui/layout/qgslayoutlegendlayersdialogbase.ui
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 8cf64c8

Please sign in to comment.