QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
25  : QDialog( parent )
26 {
27  setupUi( this );
29 
30  mFilterLineEdit->setShowClearButton( true );
31  mFilterLineEdit->setShowSearchIcon( true );
32 
33  mModel = new QgsMapLayerProxyModel( listMapLayers );
34  listMapLayers->setModel( mModel );
35  QModelIndex firstLayer = mModel->index( 0, 0 );
36  listMapLayers->selectionModel()->select( firstLayer, QItemSelectionModel::Select );
37 
38  connect( listMapLayers, &QListView::doubleClicked, this, &QgsLayoutLegendLayersDialog::accept );
39 
40  connect( mFilterLineEdit, &QLineEdit::textChanged, mModel, &QgsMapLayerProxyModel::setFilterString );
41  connect( mCheckBoxVisibleLayers, &QCheckBox::toggled, this, &QgsLayoutLegendLayersDialog::filterVisible );
42 }
43 
44 void QgsLayoutLegendLayersDialog::setVisibleLayers( const QList<QgsMapLayer *> &layers )
45 {
46  mVisibleLayers = layers;
47 }
48 
49 QList< QgsMapLayer *> QgsLayoutLegendLayersDialog::selectedLayers() const
50 {
51  QList< QgsMapLayer * > layers;
52 
53  const QModelIndexList selection = listMapLayers->selectionModel()->selectedIndexes();
54  for ( const QModelIndex &index : selection )
55  {
56  const QModelIndex sourceIndex = mModel->mapToSource( index );
57  if ( !sourceIndex.isValid() )
58  {
59  continue;
60  }
61 
62  QgsMapLayer *layer = mModel->sourceLayerModel()->layerFromIndex( sourceIndex );
63  if ( layer )
64  layers << layer;
65  }
66  return layers;
67 }
68 
69 void QgsLayoutLegendLayersDialog::filterVisible( bool enabled )
70 {
71  if ( enabled )
72  mModel->setLayerWhitelist( mVisibleLayers );
73  else
74  mModel->setLayerWhitelist( QList< QgsMapLayer * >() );
75 }
QgsLayoutLegendLayersDialog(QWidget *parent=nullptr)
constructor
QgsMapLayer * layerFromIndex(const QModelIndex &index) const
Returns the map layer corresponding to the specified index.
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QList< QgsMapLayer *> selectedLayers() const
Returns the list of selected layers.
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 setVisibleLayers(const QList< QgsMapLayer *> &layers)
Sets a list of visible layers, to use for filtering within the dialog.
void setLayerWhitelist(const QList< QgsMapLayer *> &layers)
Sets a whitelist of layers to include within the model.
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:133
void setFilterString(const QString &filter)
Sets a filter string, such that only layers with names matching the specified string will be shown...