QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgslayoutlegendlayersdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutlegendlayersdialog.cpp
3 -------------------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16#include "moc_qgslayoutlegendlayersdialog.cpp"
17
18#include <QStandardItem>
19#include "qgsmaplayer.h"
20#include "qgsmaplayermodel.h"
22#include "qgssettings.h"
23#include "qgsgui.h"
24#include "qgshelp.h"
25
27 : QDialog( parent )
28{
29 setupUi( this );
31
32 mFilterLineEdit->setShowClearButton( true );
33 mFilterLineEdit->setShowSearchIcon( true );
34
35 mModel = new QgsMapLayerProxyModel( listMapLayers );
36 listMapLayers->setModel( mModel );
37 const QModelIndex firstLayer = mModel->index( 0, 0 );
38 listMapLayers->selectionModel()->select( firstLayer, QItemSelectionModel::Select );
39
40 connect( listMapLayers, &QListView::doubleClicked, this, &QgsLayoutLegendLayersDialog::accept );
41
42 connect( mFilterLineEdit, &QLineEdit::textChanged, mModel, &QgsMapLayerProxyModel::setFilterString );
43 connect( mCheckBoxVisibleLayers, &QCheckBox::toggled, this, &QgsLayoutLegendLayersDialog::filterVisible );
44 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutLegendLayersDialog::showHelp );
45
46 mFilterLineEdit->setFocus();
47}
48
49void QgsLayoutLegendLayersDialog::setVisibleLayers( const QList<QgsMapLayer *> &layers )
50{
51 mVisibleLayers = layers;
52}
53
55{
56 QList< QgsMapLayer * > layers;
57
58 const QModelIndexList selection = listMapLayers->selectionModel()->selectedIndexes();
59 for ( const QModelIndex &index : selection )
60 {
61 const QModelIndex sourceIndex = mModel->mapToSource( index );
62 if ( !sourceIndex.isValid() )
63 {
64 continue;
65 }
66
67 QgsMapLayer *layer = mModel->sourceLayerModel()->layerFromIndex( sourceIndex );
68 if ( layer )
69 layers << layer;
70 }
71 return layers;
72}
73
74void QgsLayoutLegendLayersDialog::filterVisible( bool enabled )
75{
76 if ( enabled )
77 mModel->setLayerAllowlist( mVisibleLayers );
78 else
79 mModel->setLayerAllowlist( QList< QgsMapLayer * >() );
80}
81
82void QgsLayoutLegendLayersDialog::showHelp()
83{
84 QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_legend.html#legend-items" ) );
85}
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:209
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
void setVisibleLayers(const QList< QgsMapLayer * > &layers)
Sets a list of visible layers, to use for filtering within the dialog.
QList< QgsMapLayer * > selectedLayers() const
Returns the list of selected layers.
QgsLayoutLegendLayersDialog(QWidget *parent=nullptr)
constructor
QgsMapLayer * layerFromIndex(const QModelIndex &index) const
Returns the map layer corresponding to the specified index.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
void setFilterString(const QString &filter)
Sets a filter string, such that only layers with names matching the specified string will be shown.
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
void setLayerAllowlist(const QList< QgsMapLayer * > &layers)
Sets an allowlist of layers to include within the model.
Base class for all map layer types.
Definition qgsmaplayer.h:76