QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgslayermetadatasearchwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayermetadatasearchwidget.cpp - QgsLayerMetadataSearchWidget
3
4 ---------------------
5 begin : 1.9.2022
6 copyright : (C) 2022 by Alessandro Pasotti
7 email : elpaso 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 ***************************************************************************/
17#include "moc_qgslayermetadatasearchwidget.cpp"
20#include "qgsapplication.h"
21#include "qgsmapcanvas.h"
23#include "qgsiconutils.h"
24#include "qgshelp.h"
25
26
28 : QgsAbstractDataSourceWidget( parent, fl, widgetMode )
29{
30
31 setupUi( this );
32 setupButtons( mButtonBox );
33
34 QgsMetadataSearchContext searchContext;
36
37 mSourceModel = new QgsLayerMetadataResultsModel( searchContext, this );
38 mProxyModel = new QgsLayerMetadataResultsProxyModel( this );
39 mProxyModel->setSourceModel( mSourceModel );
40 mMetadataTableView->setModel( mProxyModel );
41 mMetadataTableView->setSortingEnabled( true );
42 mMetadataTableView->sortByColumn( 0, Qt::SortOrder::AscendingOrder );
43 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Identifier, QHeaderView::ResizeMode::Stretch );
44 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Title, QHeaderView::ResizeMode::Stretch );
45 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Abstract, QHeaderView::ResizeMode::Stretch );
46 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::DataProviderName, QHeaderView::ResizeMode::ResizeToContents );
47 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::GeometryType, QHeaderView::ResizeMode::ResizeToContents );
48 mMetadataTableView->setSelectionBehavior( QAbstractItemView::SelectRows );
49
50 mExtentFilterComboBox->addItem( QString( ) );
51 mExtentFilterComboBox->addItem( QStringLiteral( "Map Canvas Extent" ) );
52 mExtentFilterComboBox->addItem( QStringLiteral( "Current Project Extent" ) );
53 mExtentFilterComboBox->setCurrentIndex( 0 );
54 mExtentFilterComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
55 mExtentFilterComboBox->adjustSize();
56
57 mGeometryTypeComboBox->addItem( QString( ), QVariant() );
61 // Note: unknown geometry is mapped to null and missing from the combo
63 mGeometryTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "mIconRaster.svg" ) ), tr( "Raster" ), QVariant() );
64 mGeometryTypeComboBox->setCurrentIndex( 0 );
65 mGeometryTypeComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
66 mGeometryTypeComboBox->adjustSize();
67
68 auto updateLoadBtn = [ = ]
69 {
70 if ( mIsLoading )
71 {
72 mAbortPushButton->setText( tr( "Abort" ) );
73 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mTaskCancel.svg" ) ) );
74 }
75 else
76 {
77 mAbortPushButton->setText( tr( "Refresh" ) );
78 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRefresh.svg" ) ) );
79 }
80 };
81
82 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, mProgressBar, &QProgressBar::setValue );
83 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, this, [ = ]( int progress )
84 {
85 if ( progress == 100 )
86 {
87 mIsLoading = false;
88 mProgressBar->hide();
89 updateLoadBtn();
90 }
91 } );
92
93 connect( mAbortPushButton, &QPushButton::clicked, mSourceModel, [ = ]( bool )
94 {
95 if ( ! mIsLoading )
96 {
97 mIsLoading = true;
98 mProgressBar->show();
99 mSourceModel->reloadAsync( );
100 }
101 else
102 {
103 mProgressBar->hide();
104 mSourceModel->cancel();
105 mIsLoading = false;
106 }
107 updateLoadBtn();
108 } );
109
110 connect( mMetadataTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [ = ]( const QItemSelection &, const QItemSelection & )
111 {
112 emit enableButtons( mMetadataTableView->selectionModel()->hasSelection() );
113 } );
114
115 connect( mSearchFilterLineEdit, &QLineEdit::textEdited, mProxyModel, &QgsLayerMetadataResultsProxyModel::setFilterString );
116 connect( mSearchFilterLineEdit, &QgsFilterLineEdit::cleared, mProxyModel, [ = ] { mProxyModel->setFilterString( QString() ); } );
117 connect( mExtentFilterComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLayerMetadataSearchWidget::updateExtentFilter );
118
119 connect( mGeometryTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
120 {
121 if ( index == 0 ) // reset all filters
122 {
123 mProxyModel->setFilterGeometryTypeEnabled( false );
124 mProxyModel->setFilterMapLayerTypeEnabled( false );
125 }
126 else
127 {
128 const QVariant geomTypeFilterValue( mGeometryTypeComboBox->currentData() );
129 if ( geomTypeFilterValue.isValid() ) // Vector layers
130 {
131 mProxyModel->setFilterGeometryTypeEnabled( true );
132 mProxyModel->setFilterGeometryType( geomTypeFilterValue.value<Qgis::GeometryType>( ) );
133 mProxyModel->setFilterMapLayerTypeEnabled( true );
135 }
136 else // Raster layers
137 {
138 mProxyModel->setFilterGeometryTypeEnabled( false );
139 mProxyModel->setFilterMapLayerTypeEnabled( true );
141 }
142 }
143
144 } );
145
146 connect( QgsProject::instance(), &QgsProject::layersAdded, this, [ = ]( const QList<QgsMapLayer *> & )
147 {
148 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
149 } );
150
151 connect( QgsProject::instance(), &QgsProject::layersRemoved, this, [ = ]( const QStringList & )
152 {
153 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
154 } );
155
156 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayerMetadataSearchWidget::showHelp );
157
158 // Start loading metadata in the model
159 mSourceModel->reloadAsync();
160 mIsLoading = true;
161
162}
163
165{
166 if ( newMapCanvas && mapCanvas() != newMapCanvas )
167 {
168 connect( newMapCanvas, &QgsMapCanvas::extentsChanged, this, [ = ]
169 {
170 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
171 } );
172 }
174}
175
177{
178 if ( index == 1 && mapCanvas() )
179 {
180 QgsCoordinateTransform ct( mapCanvas()->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsProject::instance()->transformContext() );
182 mProxyModel->setFilterExtent( ct.transformBoundingBox( mapCanvas()->extent() ) );
183 }
184 else if ( index == 2 )
185 {
189 mProxyModel->setFilterExtent( ct.transformBoundingBox( extent ) );
190 }
191 else
192 {
193 mProxyModel->setFilterExtent( QgsRectangle( ) );
194 }
195}
196
198{
199 mSourceModel->reloadAsync();
200}
201
203{
204 const QModelIndexList &selectedIndexes { mMetadataTableView->selectionModel()->selectedRows() };
205 if ( ! selectedIndexes.isEmpty() )
206 {
207 for ( const auto &selectedIndex : std::as_const( selectedIndexes ) )
208 {
209 const QgsLayerMetadataProviderResult metadataResult { mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), static_cast< int >( QgsLayerMetadataResultsModel::CustomRole::Metadata ) ).value<QgsLayerMetadataProviderResult>() };
210
211 QString layerName = metadataResult.title();
212 if ( layerName.isEmpty() )
213 {
214 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( metadataResult.dataProviderName(), metadataResult.uri() );
215 if ( components.contains( QStringLiteral( "layerName" ) ) )
216 {
217 layerName = components.value( QStringLiteral( "layerName" ) ).toString();
218 }
219 else if ( components.contains( QStringLiteral( "table" ) ) )
220 {
221 layerName = components.value( QStringLiteral( "table" ) ).toString();
222 }
223 else
224 {
225 layerName = metadataResult.identifier();
226 }
227 }
228
229 switch ( metadataResult.layerType() )
230 {
232 {
234 emit addRasterLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
236 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
237 break;
238 }
240 {
242 emit addVectorLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
244 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
245 break;
246 }
248 {
250 emit addMeshLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
252 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
253 break;
254 }
255 default: // unsupported
256 {
257 // Ignore
258 break;
259 }
260 }
261 }
262 }
263}
264
266{
267 mSearchFilterLineEdit->clear();
268 mExtentFilterComboBox->setCurrentIndex( 0 );
269}
270
272{
273 QgsAbstractDataSourceWidget::showEvent( event );
274 mSearchFilterLineEdit->setText( mProxyModel->filterString( ) );
275}
276
277void QgsLayerMetadataSearchWidget::showHelp()
278{
279 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#the-layer-metadata-search-panel" ) );
280}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Polygon
Polygons.
@ Null
No geometry.
@ Vector
Vector layer.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
Abstract base Data Source Widget to create connections and add layers This class provides common func...
Q_DECL_DEPRECATED void progress(int, int)
Emitted when a progress dialog is shown by the provider dialog.
virtual QgsMapCanvas * mapCanvas()
Returns the dialog map canvas.
void setupButtons(QDialogButtonBox *buttonBox)
Connect the ok and apply/add buttons to the slots.
Q_DECL_DEPRECATED void addRasterLayer(const QString &rasterLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a raster layer has been selected for addition.
virtual void setMapCanvas(QgsMapCanvas *mapCanvas)
Sets the dialog map canvas.
Q_DECL_DEPRECATED void addMeshLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer has been selected for addition.
Q_DECL_DEPRECATED void addVectorLayer(const QString &uri, const QString &layerName, const QString &providerKey=QString())
Emitted when a vector layer has been selected for addition.
void addLayer(Qgis::LayerType type, const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a layer has been selected for addition.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Class for doing transforms between two map coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
void cleared()
Emitted when the widget is cleared.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
static QIcon iconForGeometryType(Qgis::GeometryType typeGroup)
Returns the icon for a vector layer whose geometry typeGroup is provided.
Result record of layer metadata provider search.
The QgsLayerMetadataResultsProxyModel class is a proxy model for QgsLayerMetadataResultsModel,...
void setFilterGeometryType(Qgis::GeometryType geometryType)
Sets the geometry type filter to geometryType.
void setFilterString(const QString &filterString)
Sets the text filter to filterString.
void setFilterExtent(const QgsRectangle &extent)
Sets the extent filter to extent.
void setFilterMapLayerTypeEnabled(bool enabled)
Sets the map layer type filter status to enabled.
void setFilterMapLayerType(const Qgis::LayerType mapLayerType)
Sets the map layer type filter to mapLayerType.
void setFilterGeometryTypeEnabled(bool enabled)
Sets the geometry type filter status to enabled.
const QString filterString() const
Returns the filter string.
void setMapCanvas(QgsMapCanvas *mapCanvas) override
Sets the dialog map canvas.
void updateExtentFilter(int index)
Updates the extent filter based on the combo box current item index.
QgsLayerMetadataSearchWidget(QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::Standalone)
Creates a new QgsLayerMetadataSearchWidget.
void showEvent(QShowEvent *event) override
Map canvas is a class for displaying all GIS data types on a canvas.
void extentsChanged()
Emitted when the extents of the map change.
QgsReferencedRectangle fullExtent() const
Returns the full extent of the project, which represents the maximal limits of the project.
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the registry.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
Definition qgsproject.h:113
const QgsProjectViewSettings * viewSettings() const
Returns the project's view settings, which contains settings and properties relating to how a QgsProj...
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
WidgetMode
Different ways a source select dialog can be used.
A rectangle specified with double values.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsRectangle with associated coordinate reference system.
static QString geometryDisplayString(Qgis::GeometryType type)
Returns a display string for a geometry type.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
QgsCoordinateTransformContext transformContext
Coordinate transform context.