Skip to content

Commit

Permalink
QgsProjectLayerGroupDialog: accept an existing project
Browse files Browse the repository at this point in the history
in an overloaded constructor.

Fixes #40552
  • Loading branch information
elpaso authored and github-actions[bot] committed Feb 19, 2021
1 parent bdef9fb commit 50d7ee2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/app/qgsprojectlayergroupdialog.cpp
Expand Up @@ -81,6 +81,33 @@ QgsProjectLayerGroupDialog::QgsProjectLayerGroupDialog( QWidget *parent, const Q
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectLayerGroupDialog::showHelp );
}

QgsProjectLayerGroupDialog::QgsProjectLayerGroupDialog( const QgsProject *project, QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{

// Preconditions
Q_ASSERT( project );
Q_ASSERT( project->layerTreeRoot() );

setupUi( this );
QgsGui::enableAutoGeometryRestore( this );

mRootGroup = project->layerTreeRoot()->clone();
QgsEmbeddedLayerTreeModel *model = new QgsEmbeddedLayerTreeModel( mRootGroup, this );
mTreeView->setModel( model );

mProjectFileWidget->hide();
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( true );

removeEmbeddedNodes( mRootGroup );

connect( mTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsProjectLayerGroupDialog::onTreeViewSelectionChanged );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectLayerGroupDialog::mButtonBox_accepted );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectLayerGroupDialog::showHelp );

}

QgsProjectLayerGroupDialog::~QgsProjectLayerGroupDialog()
{
delete mRootGroup;
Expand Down
23 changes: 22 additions & 1 deletion src/app/qgsprojectlayergroupdialog.h
Expand Up @@ -43,13 +43,34 @@ class QgsEmbeddedLayerTreeModel : public QgsLayerTreeModel
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
};

//! A dialog to select layers and groups from a qgs project
/**
* A dialog to select layers and groups from a QGIS project.
*
* The dialog can be used in two different ways by using the corresponding
* constructor:
*
* - use a QGIS project selected from the file system
* - use an already loaded QGIS project object passed in the constructor
*
* In the second case, the file selection dialog is hidden.
*/
class APP_EXPORT QgsProjectLayerGroupDialog: public QDialog, private Ui::QgsProjectLayerGroupDialogBase
{
Q_OBJECT
public:

//! Constructor. If a project file is given, the groups/layers are displayed directly and the file selection hidden
QgsProjectLayerGroupDialog( QWidget *parent = nullptr, const QString &projectFile = QString(), Qt::WindowFlags f = Qt::WindowFlags() );

/**
* Constructs a QgsProjectLayerGroupDialog from an existing \a project and an optional \a parent.
*
* \warning The project must not be NULL and it must contain a valid layer tree root.
* \since QGIS 3.18
*/
QgsProjectLayerGroupDialog( const QgsProject *project, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags() );


~QgsProjectLayerGroupDialog() override;

QStringList selectedGroups() const;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsprojectproperties.cpp
Expand Up @@ -1910,7 +1910,7 @@ void QgsProjectProperties::mRemoveWMSPrintLayoutButton_clicked()

void QgsProjectProperties::mAddLayerRestrictionButton_clicked()
{
QgsProjectLayerGroupDialog d( this, QgsProject::instance()->fileName() );
QgsProjectLayerGroupDialog d( QgsProject::instance(), this );
d.setWindowTitle( tr( "Select Restricted Layers and Groups" ) );
if ( d.exec() == QDialog::Accepted )
{
Expand Down

0 comments on commit 50d7ee2

Please sign in to comment.