QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
17 #include <QStandardItem>
18 #include "qgsmaplayer.h"
19 #include "qgsmaplayermodel.h"
20 #include "qgsmaplayerproxymodel.h"
21 #include "qgssettings.h"
22 #include "qgsgui.h"
23 #include "qgshelp.h"
24 
26  : QDialog( parent )
27 {
28  setupUi( this );
30 
31  mFilterLineEdit->setShowClearButton( true );
32  mFilterLineEdit->setShowSearchIcon( true );
33 
34  mModel = new QgsMapLayerProxyModel( listMapLayers );
35  listMapLayers->setModel( mModel );
36  QModelIndex firstLayer = mModel->index( 0, 0 );
37  listMapLayers->selectionModel()->select( firstLayer, QItemSelectionModel::Select );
38 
39  connect( listMapLayers, &QListView::doubleClicked, this, &QgsLayoutLegendLayersDialog::accept );
40 
41  connect( mFilterLineEdit, &QLineEdit::textChanged, mModel, &QgsMapLayerProxyModel::setFilterString );
42  connect( mCheckBoxVisibleLayers, &QCheckBox::toggled, this, &QgsLayoutLegendLayersDialog::filterVisible );
43  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutLegendLayersDialog::showHelp );
44 
45  mFilterLineEdit->setFocus();
46 }
47 
48 void QgsLayoutLegendLayersDialog::setVisibleLayers( const QList<QgsMapLayer *> &layers )
49 {
50  mVisibleLayers = layers;
51 }
52 
53 QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
54 {
55  QList< QgsMapLayer * > layers;
56 
57  const QModelIndexList selection = listMapLayers->selectionModel()->selectedIndexes();
58  for ( const QModelIndex &index : selection )
59  {
60  const QModelIndex sourceIndex = mModel->mapToSource( index );
61  if ( !sourceIndex.isValid() )
62  {
63  continue;
64  }
65 
66  QgsMapLayer *layer = mModel->sourceLayerModel()->layerFromIndex( sourceIndex );
67  if ( layer )
68  layers << layer;
69  }
70  return layers;
71 }
72 
73 void QgsLayoutLegendLayersDialog::filterVisible( bool enabled )
74 {
75  if ( enabled )
76  mModel->setLayerAllowlist( mVisibleLayers );
77  else
78  mModel->setLayerAllowlist( QList< QgsMapLayer * >() );
79 }
80 
81 void QgsLayoutLegendLayersDialog::showHelp()
82 {
83  QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_legend.html#legend-items" ) );
84 }
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:156
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
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...
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
void setFilterString(const QString &filter)
Sets a filter string, such that only layers with names matching the specified string will be shown.
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:85