Skip to content
Permalink
Browse files

Add a provider source widget for gdal file based layers, and allow

changing layer file source from raster layer properties
  • Loading branch information
nyalldawson committed Dec 25, 2020
1 parent d5c3b4b commit 639f63e56244719d38b98dd7b87369084e1d6cd7
@@ -346,6 +346,7 @@ set(QGIS_GUI_SRCS
providers/qgspointcloudproviderguimetadata.cpp
providers/qgspointcloudsourceselect.cpp

providers/gdal/qgsgdalfilesourcewidget.cpp
providers/gdal/qgsgdalsourceselect.cpp
providers/gdal/qgsgdalguiprovider.cpp

@@ -1134,8 +1135,10 @@ set(QGIS_GUI_HDRS
providers/qgspointcloudsourceselect.h
providers/qgspointcloudproviderguimetadata.h

providers/gdal/qgsgdalfilesourcewidget.h
providers/gdal/qgsgdalguiprovider.h
providers/gdal/qgsgdalsourceselect.h

providers/ogr/qgsgeopackageitemguiprovider.h
providers/ogr/qgsgeopackageprojectstoragedialog.h
providers/ogr/qgsgeopackageprojectstorageguiprovider.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgsgdalfilesourcewidget.cpp
--------------------------------------
Date : December 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsgdalfilesourcewidget.h"
///@cond PRIVATE

#include "qgsproviderregistry.h"
#include "ogr/qgsogrhelperfunctions.h"

#include <gdal.h>

QgsGdalFileSourceWidget::QgsGdalFileSourceWidget( QWidget *parent )
: QgsProviderSourceWidget( parent )
{
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );

mFileWidget = new QgsFileWidget();
mFileWidget->setDialogTitle( tr( "Open GDAL Supported Raster Dataset(s)" ) );
mFileWidget->setFilter( QgsProviderRegistry::instance()->fileRasterFilters() );
mFileWidget->setStorageMode( QgsFileWidget::GetFile );
mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
layout->addWidget( mFileWidget );

setLayout( layout );
}

void QgsGdalFileSourceWidget::setSourceUri( const QString &uri )
{
mSourceParts = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );

mFileWidget->setFilePath( mSourceParts.value( QStringLiteral( "path" ) ).toString() );
}

QString QgsGdalFileSourceWidget::sourceUri() const
{
QVariantMap parts = mSourceParts;
parts.insert( QStringLiteral( "path" ), mFileWidget->filePath() );
return QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts );
}


///@endcond
@@ -0,0 +1,45 @@
/***************************************************************************
qgsgdalfilesourcewidget.h
--------------------------------------
Date : December 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGGDALFILESOURCEWIDGET_H
#define QGGDALFILESOURCEWIDGET_H

#include "ui_qgsgdalsourceselectbase.h"
#include "qgsprovidersourcewidget.h"
#include "qgis_gui.h"
#include "qgis_sip.h"

///@cond PRIVATE
#define SIP_NO_FILE

class QgsGdalFileSourceWidget : public QgsProviderSourceWidget
{
Q_OBJECT

public:
QgsGdalFileSourceWidget( QWidget *parent = nullptr );

void setSourceUri( const QString &uri ) override;
QString sourceUri() const override;

private:
QgsFileWidget *mFileWidget = nullptr;

QVariantMap mSourceParts;
};

///@endcond
#endif // QGGDALFILESOURCEWIDGET_H
@@ -33,6 +33,8 @@
#include "qgsdataitem.h"
#include "qgsdataitemguiprovider.h"
#include "qgsgdaldataitems.h"
#include "qgsmaplayer.h"
#include "qgsgdalfilesourcewidget.h"

static QString PROVIDER_KEY = QStringLiteral( "gdal" );

@@ -194,7 +196,48 @@ class QgsGdalRasterSourceSelectProvider : public QgsSourceSelectProvider
}
};

//
// QgsGdalSourceWidgetProvider
//

QgsGdalSourceWidgetProvider::QgsGdalSourceWidgetProvider()
{

}

QString QgsGdalSourceWidgetProvider::providerKey() const
{
return QStringLiteral( "gdal" );
}

bool QgsGdalSourceWidgetProvider::canHandleLayer( QgsMapLayer *layer ) const
{
if ( layer->providerType() != QLatin1String( "gdal" ) )
return false;

const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), layer->source() );
if ( parts.value( QStringLiteral( "path" ) ).toString().isEmpty() )
return false;

return true;
}

QgsProviderSourceWidget *QgsGdalSourceWidgetProvider::createWidget( QgsMapLayer *layer, QWidget *parent )
{
if ( layer->providerType() != QLatin1String( "gdal" ) )
return nullptr;

const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), layer->source() );
if ( parts.value( QStringLiteral( "path" ) ).toString().isEmpty() )
return nullptr;

return new QgsGdalFileSourceWidget( parent );
}


//
// QgsGdalGuiProviderMetadata
//
QgsGdalGuiProviderMetadata::QgsGdalGuiProviderMetadata():
QgsProviderGuiMetadata( PROVIDER_KEY )
{
@@ -214,4 +257,11 @@ QList<QgsDataItemGuiProvider *> QgsGdalGuiProviderMetadata::dataItemGuiProviders
return providers;
}

QList<QgsProviderSourceWidgetProvider *> QgsGdalGuiProviderMetadata::sourceWidgetProviders()
{
QList<QgsProviderSourceWidgetProvider *> providers;
providers << new QgsGdalSourceWidgetProvider();
return providers;
}

///@endcond
@@ -25,6 +25,7 @@ class QgsProviderMetadata;
#include "qgsgdalsourceselect.h"
#include "qgsdataitemguiprovider.h"
#include "qgsproviderguiregistry.h"
#include "qgsprovidersourcewidgetprovider.h"
#include <QObject>
#include <QPointer>

@@ -49,12 +50,22 @@ class QgsGdalItemGuiProvider : public QObject, public QgsDataItemGuiProvider
void onDeleteLayer( QgsDataItemGuiContext context );
};

class QgsGdalSourceWidgetProvider : public QgsProviderSourceWidgetProvider
{
public:
QgsGdalSourceWidgetProvider();
QString providerKey() const override;
bool canHandleLayer( QgsMapLayer *layer ) const override;
QgsProviderSourceWidget *createWidget( QgsMapLayer *layer, QWidget *parent = nullptr ) override;
};

class QgsGdalGuiProviderMetadata: public QgsProviderGuiMetadata
{
public:
QgsGdalGuiProviderMetadata();
QList<QgsSourceSelectProvider *> sourceSelectProviders() override;
QList<QgsDataItemGuiProvider *> dataItemGuiProviders() override;
QList<QgsProviderSourceWidgetProvider *> sourceWidgetProviders() override;
};

///@endcond
@@ -34,6 +34,8 @@
#include "qgsmultibandcolorrendererwidget.h"
#include "qgsnative.h"
#include "qgspalettedrendererwidget.h"
#include "qgsprovidersourcewidgetproviderregistry.h"
#include "qgsprovidersourcewidget.h"
#include "qgsproject.h"
#include "qgsrasterbandstats.h"
#include "qgsrastercontourrendererwidget.h"
@@ -129,6 +131,8 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv
if ( mRasterLayer && mRasterLayer->temporalProperties() )
connect( mRasterLayer->temporalProperties(), &QgsRasterLayerTemporalProperties::changed, this, &QgsRasterLayerProperties::temporalPropertiesChange );

mSourceGroupBox->hide();

mBtnStyle = new QPushButton( tr( "Style" ) );
QMenu *menuStyle = new QMenu( this );
menuStyle->addAction( tr( "Load Style…" ), this, &QgsRasterLayerProperties::loadStyle_clicked );
@@ -714,19 +718,25 @@ void QgsRasterLayerProperties::setRendererWidget( const QString &rendererName )
}
}

/**
\note moved from ctor
Previously this dialog was created anew with each right-click pop-up menu
invocation. Changed so that the dialog always exists after first
invocation, and is just re-synchronized with its layer's state when
re-shown.
*/
void QgsRasterLayerProperties::sync()
{
QgsSettings myQSettings;

if ( !mSourceWidget )
{
mSourceWidget = QgsGui::sourceWidgetProviderRegistry()->createWidget( mRasterLayer );
if ( mSourceWidget )
{
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget( mSourceWidget );
mSourceGroupBox->setLayout( layout );
mSourceGroupBox->show();
}
}

if ( mSourceWidget )
mSourceWidget->setSourceUri( mRasterLayer->source() );

const QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
if ( !provider )
return;
@@ -918,6 +928,14 @@ void QgsRasterLayerProperties::sync()

void QgsRasterLayerProperties::apply()
{
if ( mSourceWidget )
{
const QString newSource = mSourceWidget->sourceUri();
if ( newSource != mRasterLayer->source() )
{
mRasterLayer->setDataSource( newSource, mRasterLayer->name(), mRasterLayer->providerType(), QgsDataProvider::ProviderOptions() );
}
}

// Do nothing on "bad" layers
if ( !mRasterLayer->isValid() )
@@ -40,6 +40,7 @@ class QgsRasterRendererWidget;
class QgsRasterHistogramWidget;
class QgsRasterLayerTemporalPropertiesWidget;
class QgsWebView;
class QgsProviderSourceWidget;


/**
@@ -295,6 +296,8 @@ class GUI_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private

QgsResamplingUtils mResamplingUtils;

QgsProviderSourceWidget *mSourceWidget = nullptr;

friend class QgsAppScreenShots;
};
#endif

0 comments on commit 639f63e

Please sign in to comment.
You can’t perform that action at this time.