Skip to content

Commit

Permalink
Remove all hardcoded source selects: rely on the registry
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
elpaso committed Sep 4, 2017
1 parent eb0d570 commit 2d073d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 64 deletions.
82 changes: 27 additions & 55 deletions src/gui/qgsdatasourcemanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -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( ) );
}

}

Expand Down Expand Up @@ -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 )
{
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( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
layerItem->setIcon( icon );
// Set crs and extent from canvas
if ( mMapCanvas )
{
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 )
Expand Down
3 changes: 1 addition & 2 deletions src/gui/qgsdatasourcemanagerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgssourceselectprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "qgis_gui.h"
#include "qgis.h"
#include "qgsguiutils.h"
#include "qgsproviderregistry.h"
#include "qgsabstractdatasourcewidget.h"

class QString;
Expand Down Expand Up @@ -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;

};

Expand Down
6 changes: 5 additions & 1 deletion src/gui/qgssourceselectproviderregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
11 changes: 6 additions & 5 deletions src/gui/qgssourceselectproviderregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );


Expand Down

0 comments on commit 2d073d6

Please sign in to comment.