QGIS API Documentation 3.41.0-Master (88383c3d16f)
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 setupUi( this );
31 setupButtons( mButtonBox );
32
33 QgsMetadataSearchContext searchContext;
35
36 mSourceModel = new QgsLayerMetadataResultsModel( searchContext, this );
37 mProxyModel = new QgsLayerMetadataResultsProxyModel( this );
38 mProxyModel->setSourceModel( mSourceModel );
39 mMetadataTableView->setModel( mProxyModel );
40 mMetadataTableView->setSortingEnabled( true );
41 mMetadataTableView->sortByColumn( 0, Qt::SortOrder::AscendingOrder );
42 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Identifier, QHeaderView::ResizeMode::Stretch );
43 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Title, QHeaderView::ResizeMode::Stretch );
44 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Abstract, QHeaderView::ResizeMode::Stretch );
45 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::DataProviderName, QHeaderView::ResizeMode::ResizeToContents );
46 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::GeometryType, QHeaderView::ResizeMode::ResizeToContents );
47 mMetadataTableView->setSelectionBehavior( QAbstractItemView::SelectRows );
48
49 mExtentFilterComboBox->addItem( QString() );
50 mExtentFilterComboBox->addItem( QStringLiteral( "Map Canvas Extent" ) );
51 mExtentFilterComboBox->addItem( QStringLiteral( "Current Project Extent" ) );
52 mExtentFilterComboBox->setCurrentIndex( 0 );
53 mExtentFilterComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
54 mExtentFilterComboBox->adjustSize();
55
56 mGeometryTypeComboBox->addItem( QString(), QVariant() );
60 // Note: unknown geometry is mapped to null and missing from the combo
62 mGeometryTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "mIconRaster.svg" ) ), tr( "Raster" ), QVariant() );
63 mGeometryTypeComboBox->setCurrentIndex( 0 );
64 mGeometryTypeComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
65 mGeometryTypeComboBox->adjustSize();
66
67 auto updateLoadBtn = [=] {
68 if ( mIsLoading )
69 {
70 mAbortPushButton->setText( tr( "Abort" ) );
71 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mTaskCancel.svg" ) ) );
72 }
73 else
74 {
75 mAbortPushButton->setText( tr( "Refresh" ) );
76 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRefresh.svg" ) ) );
77 }
78 };
79
80 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, mProgressBar, &QProgressBar::setValue );
81 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, this, [=]( int progress ) {
82 if ( progress == 100 )
83 {
84 mIsLoading = false;
85 mReloadRequired = false;
86 mProgressBar->hide();
87 updateLoadBtn();
88 }
89 } );
90
91 connect( mAbortPushButton, &QPushButton::clicked, mSourceModel, [=]( bool ) {
92 if ( !mIsLoading )
93 {
94 mProgressBar->show();
95 mReloadRequired = true;
96 refresh();
97 }
98 else
99 {
100 mProgressBar->hide();
101 mSourceModel->cancel();
102 mIsLoading = false;
103 mReloadRequired = false;
104 }
105 updateLoadBtn();
106 } );
107
108 connect( mMetadataTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [=]( const QItemSelection &, const QItemSelection & ) {
109 emit enableButtons( mMetadataTableView->selectionModel()->hasSelection() );
110 } );
111
112 connect( mSearchFilterLineEdit, &QLineEdit::textEdited, mProxyModel, &QgsLayerMetadataResultsProxyModel::setFilterString );
113 connect( mSearchFilterLineEdit, &QgsFilterLineEdit::cleared, mProxyModel, [=] { mProxyModel->setFilterString( QString() ); } );
114 connect( mExtentFilterComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLayerMetadataSearchWidget::updateExtentFilter );
115
116 connect( mGeometryTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [=]( int index ) {
117 if ( index == 0 ) // reset all filters
118 {
119 mProxyModel->setFilterGeometryTypeEnabled( false );
120 mProxyModel->setFilterMapLayerTypeEnabled( false );
121 }
122 else
123 {
124 const QVariant geomTypeFilterValue( mGeometryTypeComboBox->currentData() );
125 if ( geomTypeFilterValue.isValid() ) // Vector layers
126 {
127 mProxyModel->setFilterGeometryTypeEnabled( true );
128 mProxyModel->setFilterGeometryType( geomTypeFilterValue.value<Qgis::GeometryType>() );
129 mProxyModel->setFilterMapLayerTypeEnabled( true );
131 }
132 else // Raster layers
133 {
134 mProxyModel->setFilterGeometryTypeEnabled( false );
135 mProxyModel->setFilterMapLayerTypeEnabled( true );
137 }
138 }
139 } );
140
141 connect( QgsProject::instance(), &QgsProject::layersAdded, this, [=]( const QList<QgsMapLayer *> & ) {
142 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
143 } );
144
145 connect( QgsProject::instance(), &QgsProject::layersRemoved, this, [=]( const QStringList & ) {
146 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
147 } );
148
149 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayerMetadataSearchWidget::showHelp );
150}
151
153{
154 if ( newMapCanvas && mapCanvas() != newMapCanvas )
155 {
156 connect( newMapCanvas, &QgsMapCanvas::extentsChanged, this, [=] {
157 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
158 } );
159 }
161}
162
164{
165 if ( index == 1 && mapCanvas() )
166 {
167 QgsCoordinateTransform ct( mapCanvas()->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsProject::instance()->transformContext() );
169 mProxyModel->setFilterExtent( ct.transformBoundingBox( mapCanvas()->extent() ) );
170 }
171 else if ( index == 2 )
172 {
176 mProxyModel->setFilterExtent( ct.transformBoundingBox( extent ) );
177 }
178 else
179 {
180 mProxyModel->setFilterExtent( QgsRectangle() );
181 }
182}
183
185{
186 // Lazy reload
187 mReloadRequired = true;
188 refreshInternal();
189}
190
192{
193 const QModelIndexList &selectedIndexes { mMetadataTableView->selectionModel()->selectedRows() };
194 if ( !selectedIndexes.isEmpty() )
195 {
196 for ( const auto &selectedIndex : std::as_const( selectedIndexes ) )
197 {
198 const QgsLayerMetadataProviderResult metadataResult { mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), static_cast<int>( QgsLayerMetadataResultsModel::CustomRole::Metadata ) ).value<QgsLayerMetadataProviderResult>() };
199
200 QString layerName = metadataResult.title();
201 if ( layerName.isEmpty() )
202 {
203 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( metadataResult.dataProviderName(), metadataResult.uri() );
204 if ( components.contains( QStringLiteral( "layerName" ) ) )
205 {
206 layerName = components.value( QStringLiteral( "layerName" ) ).toString();
207 }
208 else if ( components.contains( QStringLiteral( "table" ) ) )
209 {
210 layerName = components.value( QStringLiteral( "table" ) ).toString();
211 }
212 else
213 {
214 layerName = metadataResult.identifier();
215 }
216 }
217
218 switch ( metadataResult.layerType() )
219 {
221 {
223 emit addRasterLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
225 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
226 break;
227 }
229 {
231 emit addVectorLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
233 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
234 break;
235 }
237 {
239 emit addMeshLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
241 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
242 break;
243 }
244 default: // unsupported
245 {
246 // Ignore
247 break;
248 }
249 }
250 }
251 }
252}
253
255{
256 mSearchFilterLineEdit->clear();
257 mExtentFilterComboBox->setCurrentIndex( 0 );
258}
259
261{
262 QgsAbstractDataSourceWidget::showEvent( event );
263 mSearchFilterLineEdit->setText( mProxyModel->filterString() );
264 refreshInternal();
265}
266
267void QgsLayerMetadataSearchWidget::showHelp()
268{
269 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#the-layer-metadata-search-panel" ) );
270}
271
272void QgsLayerMetadataSearchWidget::refreshInternal()
273{
274 if ( mReloadRequired && isVisible() )
275 {
276 mIsLoading = true;
277 mSourceModel->reloadAsync();
278 }
279}
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:6702
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6701
QgsCoordinateTransformContext transformContext
Coordinate transform context.