Skip to content

Commit 57cb687

Browse files
elpasonyalldawson
authored andcommittedFeb 19, 2021
QgsProjectLayerGroupDialog: accept an existing project
in an overloaded constructor. Fixes #40552
1 parent bdef9fb commit 57cb687

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed
 

‎src/app/qgsprojectlayergroupdialog.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ QgsProjectLayerGroupDialog::QgsProjectLayerGroupDialog( QWidget *parent, const Q
8181
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectLayerGroupDialog::showHelp );
8282
}
8383

84+
QgsProjectLayerGroupDialog::QgsProjectLayerGroupDialog( const QgsProject *project, QWidget *parent, Qt::WindowFlags f )
85+
: QDialog( parent, f )
86+
{
87+
88+
// Preconditions
89+
Q_ASSERT( project );
90+
Q_ASSERT( project->layerTreeRoot() );
91+
92+
setupUi( this );
93+
QgsGui::enableAutoGeometryRestore( this );
94+
95+
mRootGroup = project->layerTreeRoot()->clone();
96+
QgsEmbeddedLayerTreeModel *model = new QgsEmbeddedLayerTreeModel( mRootGroup, this );
97+
mTreeView->setModel( model );
98+
99+
mProjectFileWidget->hide();
100+
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
101+
102+
removeEmbeddedNodes( mRootGroup );
103+
104+
connect( mTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsProjectLayerGroupDialog::onTreeViewSelectionChanged );
105+
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectLayerGroupDialog::mButtonBox_accepted );
106+
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
107+
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectLayerGroupDialog::showHelp );
108+
109+
}
110+
84111
QgsProjectLayerGroupDialog::~QgsProjectLayerGroupDialog()
85112
{
86113
delete mRootGroup;

‎src/app/qgsprojectlayergroupdialog.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,34 @@ class QgsEmbeddedLayerTreeModel : public QgsLayerTreeModel
4343
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
4444
};
4545

46-
//! A dialog to select layers and groups from a qgs project
46+
/**
47+
* A dialog to select layers and groups from a QGIS project.
48+
*
49+
* The dialog can be used in two different ways by using the corresponding
50+
* constructor:
51+
*
52+
* - use a QGIS project selected from the file system
53+
* - use an already loaded QGIS project object passed in the constructor
54+
*
55+
* In the second case, the file selection dialog is hidden.
56+
*/
4757
class APP_EXPORT QgsProjectLayerGroupDialog: public QDialog, private Ui::QgsProjectLayerGroupDialogBase
4858
{
4959
Q_OBJECT
5060
public:
61+
5162
//! Constructor. If a project file is given, the groups/layers are displayed directly and the file selection hidden
5263
QgsProjectLayerGroupDialog( QWidget *parent = nullptr, const QString &projectFile = QString(), Qt::WindowFlags f = Qt::WindowFlags() );
64+
65+
/**
66+
* Constructs a QgsProjectLayerGroupDialog from an existing \a project and an optional \a parent.
67+
*
68+
* \warning The project must not be NULL and it must contain a valid layer tree root.
69+
* \since QGIS 3.18
70+
*/
71+
QgsProjectLayerGroupDialog( const QgsProject *project, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags() );
72+
73+
5374
~QgsProjectLayerGroupDialog() override;
5475

5576
QStringList selectedGroups() const;

‎src/app/qgsprojectproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ void QgsProjectProperties::mRemoveWMSPrintLayoutButton_clicked()
19101910

19111911
void QgsProjectProperties::mAddLayerRestrictionButton_clicked()
19121912
{
1913-
QgsProjectLayerGroupDialog d( this, QgsProject::instance()->fileName() );
1913+
QgsProjectLayerGroupDialog d( QgsProject::instance(), this );
19141914
d.setWindowTitle( tr( "Select Restricted Layers and Groups" ) );
19151915
if ( d.exec() == QDialog::Accepted )
19161916
{

0 commit comments

Comments
 (0)
Please sign in to comment.