QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsdatasourcemanagerdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatasourcemanagerdialog.cpp - datasource manager dialog
3 
4  ---------------------
5  begin : May 19, 2017
6  copyright : (C) 2017 by Alessandro Pasotti
7  email : apasotti at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include <QListWidgetItem>
18 
20 #include "ui_qgsdatasourcemanagerdialog.h"
21 #include "qgsbrowserdockwidget.h"
22 #include "qgssettings.h"
23 #include "qgsproviderregistry.h"
27 #include "qgsmapcanvas.h"
28 #include "qgsmessagelog.h"
29 #include "qgsgui.h"
30 
31 QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QgsBrowserModel *browserModel, QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl ) :
32  QgsOptionsDialogBase( QStringLiteral( "Data Source Manager" ), parent, fl ),
33  ui( new Ui::QgsDataSourceManagerDialog ),
34  mPreviousRow( -1 ),
35  mMapCanvas( canvas )
36 {
37 
38  ui->setupUi( this );
39  ui->verticalLayout_2->setSpacing( 6 );
40  ui->verticalLayout_2->setMargin( 0 );
41  ui->verticalLayout_2->setContentsMargins( 0, 0, 0, 0 );
42  // QgsOptionsDialogBase handles saving/restoring of geometry, splitter and current tab states,
43  // switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
44  // and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
45  initOptionsBase( true );
46 
47  // Bind list index to the stacked dialogs
48  connect( ui->mOptionsListWidget, &QListWidget::currentRowChanged, this, &QgsDataSourceManagerDialog::setCurrentPage );
49 
50  // BROWSER Add the browser widget to the first stacked widget page
51  mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), browserModel, this );
52  mBrowserWidget->setFeatures( QDockWidget::NoDockWidgetFeatures );
53  ui->mOptionsStackedWidget->addWidget( mBrowserWidget );
54  mPageNames.append( QStringLiteral( "browser" ) );
55  // Forward all browser signals
57  connect( mBrowserWidget, &QgsBrowserDockWidget::openFile, this, &QgsDataSourceManagerDialog::openFile );
60 
61  // Add provider dialogs
62  const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( );
63  for ( const auto &provider : sourceSelectProviders )
64  {
65  QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
66  if ( !dlg )
67  {
68  QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::Critical );
69  continue;
70  }
71  addProviderDialog( dlg, provider->providerKey(), provider->text(), provider->icon( ), provider->toolTip( ) );
72  }
73 
74 }
75 
77 {
78  delete ui;
79 }
80 
81 void QgsDataSourceManagerDialog::openPage( const QString &pageName )
82 {
83  int pageIdx = mPageNames.indexOf( pageName );
84  if ( pageIdx != -1 )
85  {
86  QTimer::singleShot( 0, this, [ = ] { setCurrentPage( pageIdx ); } );
87  }
88 }
89 
91 {
92  mPreviousRow = ui->mOptionsStackedWidget->currentIndex();
93  ui->mOptionsStackedWidget->setCurrentIndex( index );
94  setWindowTitle( tr( "Data Source Manager | %1" ).arg( ui->mOptionsListWidget->currentItem()->text() ) );
95 }
96 
98 {
99  int prevPage = mPreviousRow != -1 ? mPreviousRow : 0;
100  setCurrentPage( prevPage );
101 }
102 
104 {
105  mBrowserWidget->refresh();
107 }
108 
109 void QgsDataSourceManagerDialog::rasterLayerAdded( const QString &uri, const QString &baseName, const QString &providerKey )
110 {
111  emit addRasterLayer( uri, baseName, providerKey );
112 }
113 
114 void QgsDataSourceManagerDialog::vectorLayerAdded( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey )
115 {
116  emit addVectorLayer( vectorLayerPath, baseName, providerKey );
117 }
118 
119 void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
120 {
121  emit addVectorLayers( layerQStringList, enc, dataSourceType );
122 }
123 
124 
125 void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, const QString &toolTip )
126 {
127  mPageNames.append( providerKey );
128  ui->mOptionsStackedWidget->addWidget( dlg );
129  QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
130  layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
131  layerItem->setIcon( icon );
132  // Set crs and extent from canvas
133  if ( mMapCanvas )
134  {
135  dlg->setMapCanvas( mMapCanvas );
136  }
137  connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
138  connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
139  makeConnections( dlg, providerKey );
140 }
141 
142 void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
143 {
144  // DB
145  connect( dlg, SIGNAL( addDatabaseLayers( QStringList const &, QString const & ) ),
146  this, SIGNAL( addDatabaseLayers( QStringList const &, QString const & ) ) );
147  connect( dlg, SIGNAL( progress( int, int ) ),
148  this, SIGNAL( showProgress( int, int ) ) );
149  connect( dlg, SIGNAL( progressMessage( QString ) ),
150  this, SIGNAL( showStatusMessage( QString ) ) );
151  // Vector
152  connect( dlg, &QgsAbstractDataSourceWidget::addVectorLayer, this, [ = ]( const QString & vectorLayerPath, const QString & baseName, const QString & specifiedProvider )
153  {
154  QString key = specifiedProvider.isEmpty() ? providerKey : specifiedProvider;
155  this->vectorLayerAdded( vectorLayerPath, baseName, key );
156  }
157  );
160  connect( dlg, SIGNAL( connectionsChanged() ), this, SIGNAL( connectionsChanged() ) );
161  // Raster
162  connect( dlg, SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ),
163  this, SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ) );
164 
165  // Virtual
166  connect( dlg, SIGNAL( replaceVectorLayer( QString, QString, QString, QString ) ),
167  this, SIGNAL( replaceSelectedVectorLayer( QString, QString, QString, QString ) ) );
168  // Common
169  connect( dlg, SIGNAL( connectionsChanged() ), this, SIGNAL( connectionsChanged() ) );
170  connect( this, SIGNAL( providerDialogsRefreshRequested() ), dlg, SLOT( refresh() ) );
171 }
172 
174 {
175  ui->mOptionsStackedWidget->currentWidget()->show();
177 }
void rasterLayerAdded(QString const &uri, QString const &baseName, QString const &providerKey)
A raster layer was added: for signal forwarding to QgisApp TODO: use this with an internal source sel...
void connectionsChanged()
Emitted when a connection has changed inside the provider dialogs This signal is normally forwarded t...
void showEvent(QShowEvent *event) override
Abstract base Data Source Widget to create connections and add layers This class provides common func...
void showProgress(int progress, int totalSteps)
Emitted when the dialog is busy: for signal forwarding to QgisApp.
void refresh()
Refresh the browser view.
void initOptionsBase(bool restoreUi=true, const QString &title=QString())
Set up the base ui connections for vertical tabs.
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:52
The QgsDataSourceManagerDialog class embeds the browser panel and all the provider dialogs...
void addVectorLayer(const QString &vectorLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a vector layer was selected for addition: for signal forwarding to QgisApp.
void providerDialogsRefreshRequested()
One or more provider connections have changed and the dialogs should be refreshed.
void setPreviousPage()
Reset current page to previously selected page.
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled from the browser.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void openPage(const QString &pageName)
Open a given page in the dialog.
void updateProjectHome()
Update project home directory.
void setMapCanvas(const QgsMapCanvas *mapCanvas)
Store a pointer to the map canvas to retrieve extent and CRS Used to select an appropriate CRS and po...
A base dialog for options and properties dialogs that offers vertical tabs.
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
void showEvent(QShowEvent *e) override
void addVectorLayer(const QString &uri, const QString &layerName, const QString &providerKey=QString())
Emitted when a vector layer has been selected for addition.
void addRasterLayer()
Emitted when the user wants to select a raster layer: for signal forwarding to QgisApp.
void vectorLayerAdded(const QString &vectorLayerPath, const QString &baseName, const QString &providerKey)
A vector layer was added: for signal forwarding to QgisApp.
The QgsBrowserDockWidget class.
void vectorLayersAdded(const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType)
One or more vector layer were added: for signal forwarding to QgisApp.
void replaceSelectedVectorLayer(const QString &oldId, const QString &uri, const QString &layerName, const QString &provider)
Replace the selected layer by a vector layer defined by uri, layer name, data source uri...
void updateProjectHome()
Update project home directory.
void showStatusMessage(const QString &message)
Emitted when a status message needs to be shown: for signal forwarding to QgisApp.
void addVectorLayers(const QStringList &layerList, const QString &encoding, const QString &dataSourceType)
Emitted when one or more OGR supported layers are selected for addition.
A model for showing available data sources and other items in a structured tree.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsDataSourceManagerDialog(QgsBrowserModel *browserModel, QWidget *parent=nullptr, QgsMapCanvas *canvas=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
QgsDataSourceManagerDialog constructor.
void connectionsChanged()
Connections changed in the browser.
void refresh()
Refresh browser view model (and view)
void addDatabaseLayers(const QStringList &layerPathList, const QString &providerKey)
Emitted when a DB layer was selected for addition: for signal forwarding to QgisApp.
void addVectorLayers(const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType)
Emitted when a one or more layer were selected for addition: for signal forwarding to QgisApp...
void setCurrentPage(int index)
Sync current page with the leftbar list.