Skip to content
Permalink
Browse files
Show project items when dropping a gpkg onto QGIS alongside layers
  • Loading branch information
nyalldawson committed Jul 15, 2021
1 parent 1f31a38 commit 96be786dd289153beaf5e2fd7b1730ec4293110a
Showing with 35 additions and 5 deletions.
  1. +24 −4 src/app/qgisapp.cpp
  2. +1 −1 src/app/qgisapp.h
  3. +8 −0 src/app/qgsprovidersublayersdialog.cpp
  4. +2 −0 src/app/qgsprovidersublayersdialog.h
@@ -5965,8 +5965,11 @@ bool QgisApp::askUserForZipItemLayers( const QString &path, const QList< QgsMapL
return true;
}

QgisApp::SublayerHandling QgisApp::shouldAskUserForSublayers( const QList<QgsProviderSublayerDetails> &layers ) const
QgisApp::SublayerHandling QgisApp::shouldAskUserForSublayers( const QList<QgsProviderSublayerDetails> &layers, bool hasNonLayerItems ) const
{
if ( hasNonLayerItems )
return SublayerHandling::AskUser;

QgsSettings settings;
const Qgis::SublayerPromptMode promptLayers = settings.enumValue( QStringLiteral( "qgis/promptForSublayers" ), Qgis::SublayerPromptMode::AlwaysAsk );

@@ -7353,23 +7356,40 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )
}
}

QList< QgsProviderSublayerModel::NonLayerItem > nonLayerItems;
if ( QgsProjectStorage *ps = QgsApplication::projectStorageRegistry()->projectStorageFromUri( fileName ) )
{
const QStringList projects = ps->listProjects( fileName );
for ( const QString &project : projects )
{
QgsProviderSublayerModel::NonLayerItem projectItem;
projectItem.setType( QStringLiteral( "project" ) );
projectItem.setName( project );
projectItem.setUri( fileName );
projectItem.setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconQgsProjectFile.svg" ) ) );
nonLayerItems << projectItem;
}
}

// query sublayers
QList< QgsProviderSublayerDetails > sublayers = QgsProviderRegistry::instance()->querySublayers( fileName );
if ( !sublayers.empty() )

if ( !sublayers.empty() || !nonLayerItems.empty() )
{
const bool detailsAreIncomplete = QgsProviderUtils::sublayerDetailsAreIncomplete( sublayers, true );
const bool singleSublayerOnly = sublayers.size() == 1;
QString groupName;

if ( allowInteractive && ( !singleSublayerOnly || detailsAreIncomplete ) )
if ( allowInteractive && ( !singleSublayerOnly || detailsAreIncomplete || !nonLayerItems.empty() ) )
{
// ask user for sublayers (unless user settings dictate otherwise!)
switch ( shouldAskUserForSublayers( sublayers ) )
switch ( shouldAskUserForSublayers( sublayers, !nonLayerItems.empty() ) )
{
case SublayerHandling::AskUser:
{
// prompt user for sublayers
QgsProviderSublayersDialog dlg( fileName, fileName, sublayers, {}, this );
dlg.setNonLayerItems( nonLayerItems );

if ( dlg.exec() )
sublayers = dlg.selectedLayers();
@@ -2080,7 +2080,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
LoadAll,
AbortLoading
};
SublayerHandling shouldAskUserForSublayers( const QList< QgsProviderSublayerDetails > &layers ) const;
SublayerHandling shouldAskUserForSublayers( const QList< QgsProviderSublayerDetails > &layers, bool hasNonLayerItems = false ) const;

QList< QgsMapLayer * > addSublayers( const QList< QgsProviderSublayerDetails> &layers, const QString &baseName, const QString &groupName );

@@ -181,6 +181,14 @@ QgsProviderSublayersDialog::QgsProviderSublayersDialog( const QString &uri, cons
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
}

void QgsProviderSublayersDialog::setNonLayerItems( const QList<QgsProviderSublayerModel::NonLayerItem> &items )
{
for ( const QgsProviderSublayerModel::NonLayerItem &item : items )
{
mModel->addNonLayerItem( item );
}
}

QgsProviderSublayersDialog::~QgsProviderSublayersDialog()
{
QgsSettings settings;
@@ -54,6 +54,8 @@ class QgsProviderSublayersDialog : public QDialog, private Ui::QgsProviderSublay
QWidget *parent SIP_TRANSFERTHIS = nullptr,
Qt::WindowFlags fl = Qt::WindowFlags() );

void setNonLayerItems( const QList< QgsProviderSublayerModel::NonLayerItem > &items );

~QgsProviderSublayersDialog() override;

QList< QgsProviderSublayerDetails > selectedLayers() const;

0 comments on commit 96be786

Please sign in to comment.