QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsapplication.h"
19#include "qgshelp.h"
20#include "qgsiconutils.h"
23#include "qgsmapcanvas.h"
25
26#include "moc_qgslayermetadatasearchwidget.cpp"
27
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 = [this] {
69 if ( mIsLoading )
70 {
71 mAbortPushButton->setText( tr( "Abort" ) );
72 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mTaskCancel.svg" ) ) );
73 }
74 else
75 {
76 mAbortPushButton->setText( tr( "Refresh" ) );
77 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRefresh.svg" ) ) );
78 }
79 };
80
81 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, mProgressBar, &QProgressBar::setValue );
82 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, this, [this, updateLoadBtn]( int progress ) {
83 if ( progress == 100 )
84 {
85 mIsLoading = false;
86 mReloadRequired = false;
87 mProgressBar->hide();
88 updateLoadBtn();
89 }
90 } );
91
92 connect( mAbortPushButton, &QPushButton::clicked, mSourceModel, [this, updateLoadBtn]( bool ) {
93 if ( !mIsLoading )
94 {
95 mProgressBar->show();
96 mReloadRequired = true;
97 refresh();
98 }
99 else
100 {
101 mProgressBar->hide();
102 mSourceModel->cancel();
103 mIsLoading = false;
104 mReloadRequired = false;
105 }
106 updateLoadBtn();
107 } );
108
109 connect( mMetadataTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this]( const QItemSelection &, const QItemSelection & ) {
110 emit enableButtons( mMetadataTableView->selectionModel()->hasSelection() );
111 } );
112
113 connect( mSearchFilterLineEdit, &QLineEdit::textEdited, mProxyModel, &QgsLayerMetadataResultsProxyModel::setFilterString );
114 connect( mSearchFilterLineEdit, &QgsFilterLineEdit::cleared, mProxyModel, [this] { mProxyModel->setFilterString( QString() ); } );
115 connect( mExtentFilterComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLayerMetadataSearchWidget::updateExtentFilter );
116
117 connect( mGeometryTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
118 if ( index == 0 ) // reset all filters
119 {
120 mProxyModel->setFilterGeometryTypeEnabled( false );
121 mProxyModel->setFilterMapLayerTypeEnabled( false );
122 }
123 else
124 {
125 const QVariant geomTypeFilterValue( mGeometryTypeComboBox->currentData() );
126 if ( geomTypeFilterValue.isValid() ) // Vector layers
127 {
128 mProxyModel->setFilterGeometryTypeEnabled( true );
129 mProxyModel->setFilterGeometryType( geomTypeFilterValue.value<Qgis::GeometryType>() );
130 mProxyModel->setFilterMapLayerTypeEnabled( true );
131 mProxyModel->setFilterMapLayerType( Qgis::LayerType::Vector );
132 }
133 else // Raster layers
134 {
135 mProxyModel->setFilterGeometryTypeEnabled( false );
136 mProxyModel->setFilterMapLayerTypeEnabled( true );
137 mProxyModel->setFilterMapLayerType( Qgis::LayerType::Raster );
138 }
139 }
140 } );
141
142 connect( QgsProject::instance(), &QgsProject::layersAdded, this, [this]( const QList<QgsMapLayer *> & ) {
143 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
144 } );
145
146 connect( QgsProject::instance(), &QgsProject::layersRemoved, this, [this]( const QStringList & ) {
147 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
148 } );
149
150 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayerMetadataSearchWidget::showHelp );
151}
152
154{
155 if ( newMapCanvas && mapCanvas() != newMapCanvas )
156 {
157 connect( newMapCanvas, &QgsMapCanvas::extentsChanged, this, [this] {
158 updateExtentFilter( mExtentFilterComboBox->currentIndex() );
159 } );
160 }
162}
163
165{
166 if ( index == 1 && mapCanvas() )
167 {
168 QgsCoordinateTransform ct( mapCanvas()->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsProject::instance()->transformContext() );
170 mProxyModel->setFilterExtent( ct.transformBoundingBox( mapCanvas()->extent() ) );
171 }
172 else if ( index == 2 )
173 {
177 mProxyModel->setFilterExtent( ct.transformBoundingBox( extent ) );
178 }
179 else
180 {
181 mProxyModel->setFilterExtent( QgsRectangle() );
182 }
183}
184
186{
187 // Lazy reload
188 mReloadRequired = true;
189 refreshInternal();
190}
191
193{
194 const QModelIndexList &selectedIndexes { mMetadataTableView->selectionModel()->selectedRows() };
195 if ( !selectedIndexes.isEmpty() )
196 {
197 for ( const auto &selectedIndex : std::as_const( selectedIndexes ) )
198 {
199 const QgsLayerMetadataProviderResult metadataResult { mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), static_cast<int>( QgsLayerMetadataResultsModel::CustomRole::Metadata ) ).value<QgsLayerMetadataProviderResult>() };
200
201 QString layerName = metadataResult.title();
202 if ( layerName.isEmpty() )
203 {
204 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( metadataResult.dataProviderName(), metadataResult.uri() );
205 if ( components.contains( QStringLiteral( "layerName" ) ) )
206 {
207 layerName = components.value( QStringLiteral( "layerName" ) ).toString();
208 }
209 else if ( components.contains( QStringLiteral( "table" ) ) )
210 {
211 layerName = components.value( QStringLiteral( "table" ) ).toString();
212 }
213 else
214 {
215 layerName = metadataResult.identifier();
216 }
217 }
218
219 switch ( metadataResult.layerType() )
220 {
222 {
224 emit addRasterLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
226 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
227 break;
228 }
230 {
232 emit addVectorLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
234 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
235 break;
236 }
238 {
240 emit addMeshLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
242 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
243 break;
244 }
245 default: // unsupported
246 {
247 // Ignore
248 break;
249 }
250 }
251 }
252 }
253}
254
256{
257 mSearchFilterLineEdit->clear();
258 mExtentFilterComboBox->setCurrentIndex( 0 );
259}
260
262{
263 QgsAbstractDataSourceWidget::showEvent( event );
264 mSearchFilterLineEdit->setText( mProxyModel->filterString() );
265 refreshInternal();
266}
267
268void QgsLayerMetadataSearchWidget::showHelp()
269{
270 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#the-layer-metadata-search-panel" ) );
271}
272
273void QgsLayerMetadataSearchWidget::refreshInternal()
274{
275 if ( mReloadRequired && isVisible() )
276 {
277 mIsLoading = true;
278 mSourceModel->reloadAsync();
279 }
280}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Null
No geometry.
Definition qgis.h:363
@ Vector
Vector layer.
Definition qgis.h:191
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
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.
QgsAbstractDataSourceWidget(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::Standalone)
Constructor.
Q_DECL_DEPRECATED void addMeshLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer has been selected for addition.
QgsProviderRegistry::WidgetMode widgetMode() const
Returns the widget mode.
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.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
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.
Handles coordinate transforms between two 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:38
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.
const QString & dataProviderName() const
Returns the data provider name.
const QString & uri() const
Returns the layer data source URI.
Qgis::LayerType layerType() const
Returns the layer type.
A proxy model for QgsLayerMetadataResultsModel, handling text and extent filtering.
void setFilterString(const QString &filterString)
Sets the text filter to filterString.
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:116
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 Q_INVOKABLE QString geometryDisplayString(Qgis::GeometryType type)
Returns a display string for a geometry type.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
QgsCoordinateTransformContext transformContext
Coordinate transform context.