mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-26 00:02:08 -05:00
Remove all hardcoded source selects: rely on the registry
The data source manager dialog is now clean from hardcoded provider keys and there is a mean to add items to the dialog via plugins (C++, providers and Python).
This commit is contained in:
parent
eb0d57072f
commit
2d073d6faf
@ -22,8 +22,11 @@
|
||||
#include "qgsbrowserdockwidget.h"
|
||||
#include "qgssettings.h"
|
||||
#include "qgsproviderregistry.h"
|
||||
#include "qgssourceselectprovider.h"
|
||||
#include "qgssourceselectproviderregistry.h"
|
||||
#include "qgsabstractdatasourcewidget.h"
|
||||
#include "qgsmapcanvas.h"
|
||||
#include "qgsgui.h"
|
||||
|
||||
QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl ) :
|
||||
QgsOptionsDialogBase( QStringLiteral( "Data Source Manager" ), parent, fl ),
|
||||
@ -55,39 +58,18 @@ QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QWidget *parent, QgsMapC
|
||||
connect( mBrowserWidget, &QgsBrowserDockWidget::connectionsChanged, this, &QgsDataSourceManagerDialog::connectionsChanged );
|
||||
connect( this, &QgsDataSourceManagerDialog::updateProjectHome, mBrowserWidget, &QgsBrowserDockWidget::updateProjectHome );
|
||||
|
||||
// Add data provider dialogs
|
||||
|
||||
providerDialog( QStringLiteral( "ogr" ), tr( "Vector" ), QStringLiteral( "/mActionAddOgrLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "gdal" ), tr( "Raster" ), QStringLiteral( "/mActionAddRasterLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "delimitedtext" ), tr( "Delimited Text" ), QStringLiteral( "/mActionAddDelimitedTextLayer.svg" ) );
|
||||
|
||||
#ifdef HAVE_POSTGRESQL
|
||||
providerDialog( QStringLiteral( "postgres" ), tr( "PostgreSQL" ), QStringLiteral( "/mActionAddPostgisLayer.svg" ) );
|
||||
#endif
|
||||
|
||||
providerDialog( QStringLiteral( "spatialite" ), tr( "SpatiaLite" ), QStringLiteral( "/mActionAddSpatiaLiteLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "mssql" ), tr( "MSSQL" ), QStringLiteral( "/mActionAddMssqlLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "DB2" ), tr( "DB2" ), QStringLiteral( "/mActionAddDb2Layer.svg" ) );
|
||||
|
||||
#ifdef HAVE_ORACLE
|
||||
providerDialog( QStringLiteral( "oracle" ), tr( "Oracle" ), QStringLiteral( "/mActionAddOracleLayer.svg" ) );
|
||||
#endif
|
||||
|
||||
providerDialog( QStringLiteral( "virtual" ), tr( "Virtual Layer" ), QStringLiteral( "/mActionAddVirtualLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "wms" ), tr( "WMS" ), QStringLiteral( "/mActionAddWmsLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "wcs" ), tr( "WCS" ), QStringLiteral( "/mActionAddWcsLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "WFS" ), tr( "WFS" ), QStringLiteral( "/mActionAddWfsLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "arcgismapserver" ), tr( "ArcGIS Map Server" ), QStringLiteral( "/mActionAddAmsLayer.svg" ) );
|
||||
|
||||
providerDialog( QStringLiteral( "arcgisfeatureserver" ), tr( "ArcGIS Feature Server" ), QStringLiteral( "/mActionAddAfsLayer.svg" ) );
|
||||
// Add provider dialogs
|
||||
const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( ) ;
|
||||
for ( const auto provider : sourceSelectProviders )
|
||||
{
|
||||
QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
|
||||
if ( !dlg )
|
||||
{
|
||||
QMessageBox::warning( this, provider->name(), tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ) );
|
||||
continue;
|
||||
}
|
||||
addProviderDialog( dlg, provider->providerKey(), provider->text(), provider->icon( ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -140,31 +122,21 @@ void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStr
|
||||
}
|
||||
|
||||
|
||||
QgsAbstractDataSourceWidget *QgsDataSourceManagerDialog::providerDialog( const QString providerKey, const QString providerName, const QString icon, QString title )
|
||||
void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, QString toolTip )
|
||||
{
|
||||
QgsAbstractDataSourceWidget *dlg = dynamic_cast<QgsAbstractDataSourceWidget *>( QgsProviderRegistry::instance()->createSelectionWidget( providerKey, this, Qt::Widget, QgsProviderRegistry::WidgetMode::Embedded ) );
|
||||
if ( !dlg )
|
||||
mPageNames.append( providerKey );
|
||||
ui->mOptionsStackedWidget->addWidget( dlg );
|
||||
QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
|
||||
layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
|
||||
layerItem->setIcon( icon );
|
||||
// Set crs and extent from canvas
|
||||
if ( mMapCanvas )
|
||||
{
|
||||
QMessageBox::warning( this, providerName, tr( "Cannot get %1 select dialog from provider %2." ).arg( providerName, providerKey ) );
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
mPageNames.append( providerKey );
|
||||
ui->mOptionsStackedWidget->addWidget( dlg );
|
||||
QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
|
||||
layerItem->setToolTip( title.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : title );
|
||||
layerItem->setIcon( QgsApplication::getThemeIcon( icon ) );
|
||||
// Set crs and extent from canvas
|
||||
if ( mMapCanvas )
|
||||
{
|
||||
dlg->setMapCanvas( mMapCanvas );
|
||||
}
|
||||
connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
|
||||
connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
|
||||
makeConnections( dlg, providerKey );
|
||||
return dlg;
|
||||
dlg->setMapCanvas( mMapCanvas );
|
||||
}
|
||||
connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
|
||||
connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
|
||||
makeConnections( dlg, providerKey );
|
||||
}
|
||||
|
||||
void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
|
||||
|
@ -114,8 +114,7 @@ class GUI_EXPORT QgsDataSourceManagerDialog : public QgsOptionsDialogBase, priva
|
||||
void providerDialogsRefreshRequested();
|
||||
|
||||
private:
|
||||
// Return the dialog from the provider
|
||||
QgsAbstractDataSourceWidget *providerDialog( const QString providerKey, const QString providerName, const QString icon, QString title = QString() );
|
||||
void addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, QString toolTip = QString() );
|
||||
void makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey );
|
||||
Ui::QgsDataSourceManagerDialog *ui;
|
||||
QgsBrowserDockWidget *mBrowserWidget = nullptr;
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include "qgis_gui.h"
|
||||
#include "qgis.h"
|
||||
#include "qgsguiutils.h"
|
||||
#include "qgsproviderregistry.h"
|
||||
#include "qgsabstractdatasourcewidget.h"
|
||||
|
||||
class QString;
|
||||
@ -61,7 +63,7 @@ class GUI_EXPORT QgsSourceSelectProvider
|
||||
/** Create a new instance of QgsAbstractDataSourceWidget (or null).
|
||||
* Caller takes responsibility of deleting created.
|
||||
*/
|
||||
virtual QgsAbstractDataSourceWidget *createDataSourceWidget( QWidget *parent = nullptr ) const = 0 SIP_FACTORY;
|
||||
virtual QgsAbstractDataSourceWidget *createDataSourceWidget( QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::Widget, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::Embedded ) const = 0 SIP_FACTORY;
|
||||
|
||||
};
|
||||
|
||||
|
@ -47,11 +47,15 @@ void QgsSourceSelectProviderRegistry::addProvider( QgsSourceSelectProvider *prov
|
||||
} );
|
||||
}
|
||||
|
||||
void QgsSourceSelectProviderRegistry::removeProvider( QgsSourceSelectProvider *provider )
|
||||
bool QgsSourceSelectProviderRegistry::removeProvider( QgsSourceSelectProvider *provider )
|
||||
{
|
||||
int index = mProviders.indexOf( provider );
|
||||
if ( index >= 0 )
|
||||
{
|
||||
delete mProviders.takeAt( index );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QgsSourceSelectProvider *QgsSourceSelectProviderRegistry::providerByName( const QString &name )
|
||||
|
@ -49,16 +49,17 @@ class GUI_EXPORT QgsSourceSelectProviderRegistry
|
||||
//! Get list of available providers
|
||||
QList< QgsSourceSelectProvider *> providers();
|
||||
|
||||
//! Add a provider implementation. Takes ownership of the object.
|
||||
//! Add a \a provider implementation. Takes ownership of the object.
|
||||
void addProvider( QgsSourceSelectProvider *provider SIP_TRANSFER );
|
||||
|
||||
//! Remove provider implementation from the list (provider object is deleted)
|
||||
void removeProvider( QgsSourceSelectProvider *provider );
|
||||
//! Remove \a provider implementation from the list (\a provider object is deleted)
|
||||
//! \returns true if the provider was actually removed and deleted
|
||||
bool removeProvider( QgsSourceSelectProvider *provider SIP_TRANSFER );
|
||||
|
||||
//! Return a provider by name or nullptr if not found
|
||||
//! Return a provider by \a name or nullptr if not found
|
||||
QgsSourceSelectProvider *providerByName( const QString &name );
|
||||
|
||||
//! Return a (possibly empty) list of providers by data provider's key
|
||||
//! Return a (possibly empty) list of providers by data \a providerkey
|
||||
QList<QgsSourceSelectProvider *> providersByKey( const QString &providerKey );
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user