QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 <QString>
27
28#include "moc_qgslayermetadatasearchwidget.cpp"
29
30using namespace Qt::StringLiterals;
31
34{
35 setupUi( this );
36 setupButtons( mButtonBox );
37
38 QgsMetadataSearchContext searchContext;
40
41 mSourceModel = new QgsLayerMetadataResultsModel( searchContext, this );
42 mProxyModel = new QgsLayerMetadataResultsProxyModel( this );
43 mProxyModel->setSourceModel( mSourceModel );
44 mMetadataTableView->setModel( mProxyModel );
45 mMetadataTableView->setSortingEnabled( true );
46 mMetadataTableView->sortByColumn( 0, Qt::SortOrder::AscendingOrder );
47 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Identifier, QHeaderView::ResizeMode::Stretch );
48 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Title, QHeaderView::ResizeMode::Stretch );
49 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::Abstract, QHeaderView::ResizeMode::Stretch );
50 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::DataProviderName, QHeaderView::ResizeMode::ResizeToContents );
51 mMetadataTableView->horizontalHeader()->setSectionResizeMode( QgsLayerMetadataResultsModel::Sections::GeometryType, QHeaderView::ResizeMode::ResizeToContents );
52 mMetadataTableView->setSelectionBehavior( QAbstractItemView::SelectRows );
53
54 mExtentFilterComboBox->addItem( QString() );
55 mExtentFilterComboBox->addItem( u"Map Canvas Extent"_s );
56 mExtentFilterComboBox->addItem( u"Current Project Extent"_s );
57 mExtentFilterComboBox->setCurrentIndex( 0 );
58 mExtentFilterComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
59 mExtentFilterComboBox->adjustSize();
60
61 mGeometryTypeComboBox->addItem( QString(), QVariant() );
64 mGeometryTypeComboBox
66 // Note: unknown geometry is mapped to null and missing from the combo
68 mGeometryTypeComboBox->addItem( QgsApplication::getThemeIcon( u"mIconRaster.svg"_s ), tr( "Raster" ), QVariant() );
69 mGeometryTypeComboBox->setCurrentIndex( 0 );
70 mGeometryTypeComboBox->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToContents );
71 mGeometryTypeComboBox->adjustSize();
72
73 auto updateLoadBtn = [this] {
74 if ( mIsLoading )
75 {
76 mAbortPushButton->setText( tr( "Abort" ) );
77 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( u"mTaskCancel.svg"_s ) );
78 }
79 else
80 {
81 mAbortPushButton->setText( tr( "Refresh" ) );
82 mAbortPushButton->setIcon( QgsApplication::getThemeIcon( u"mActionRefresh.svg"_s ) );
83 }
84 };
85
86 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, mProgressBar, &QProgressBar::setValue );
87 connect( mSourceModel, &QgsLayerMetadataResultsModel::progressChanged, this, [this, updateLoadBtn]( int progress ) {
88 if ( progress == 100 )
89 {
90 mIsLoading = false;
91 mReloadRequired = false;
92 mProgressBar->hide();
93 updateLoadBtn();
94 }
95 } );
96
97 connect( mAbortPushButton, &QPushButton::clicked, mSourceModel, [this, updateLoadBtn]( bool ) {
98 if ( !mIsLoading )
99 {
100 mProgressBar->show();
101 mReloadRequired = true;
102 refresh();
103 }
104 else
105 {
106 mProgressBar->hide();
107 mSourceModel->cancel();
108 mIsLoading = false;
109 mReloadRequired = false;
110 }
111 updateLoadBtn();
112 } );
113
114 connect( mMetadataTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this]( const QItemSelection &, const QItemSelection & ) {
115 emit enableButtons( mMetadataTableView->selectionModel()->hasSelection() );
116 } );
117
118 connect( mSearchFilterLineEdit, &QLineEdit::textEdited, mProxyModel, &QgsLayerMetadataResultsProxyModel::setFilterString );
119 connect( mSearchFilterLineEdit, &QgsFilterLineEdit::cleared, mProxyModel, [this] { mProxyModel->setFilterString( QString() ); } );
120 connect( mExtentFilterComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLayerMetadataSearchWidget::updateExtentFilter );
121
122 connect( mGeometryTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
123 if ( index == 0 ) // reset all filters
124 {
125 mProxyModel->setFilterGeometryTypeEnabled( false );
126 mProxyModel->setFilterMapLayerTypeEnabled( false );
127 }
128 else
129 {
130 const QVariant geomTypeFilterValue( mGeometryTypeComboBox->currentData() );
131 if ( geomTypeFilterValue.isValid() ) // Vector layers
132 {
133 mProxyModel->setFilterGeometryTypeEnabled( true );
134 mProxyModel->setFilterGeometryType( geomTypeFilterValue.value<Qgis::GeometryType>() );
135 mProxyModel->setFilterMapLayerTypeEnabled( true );
136 mProxyModel->setFilterMapLayerType( Qgis::LayerType::Vector );
137 }
138 else // Raster layers
139 {
140 mProxyModel->setFilterGeometryTypeEnabled( false );
141 mProxyModel->setFilterMapLayerTypeEnabled( true );
142 mProxyModel->setFilterMapLayerType( Qgis::LayerType::Raster );
143 }
144 }
145 } );
146
147 connect( QgsProject::instance(), &QgsProject::layersAdded, this, [this]( const QList<QgsMapLayer *> & ) { updateExtentFilter( mExtentFilterComboBox->currentIndex() ); } );
148
149 connect( QgsProject::instance(), &QgsProject::layersRemoved, this, [this]( const QStringList & ) { updateExtentFilter( mExtentFilterComboBox->currentIndex() ); } );
150
151 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayerMetadataSearchWidget::showHelp );
152}
153
155{
156 if ( newMapCanvas && mapCanvas() != newMapCanvas )
157 {
158 connect( newMapCanvas, &QgsMapCanvas::extentsChanged, this, [this] { updateExtentFilter( mExtentFilterComboBox->currentIndex() ); } );
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 {
199 mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), static_cast<int>( QgsLayerMetadataResultsModel::CustomRole::Metadata ) ).value<QgsLayerMetadataProviderResult>()
200 };
201
202 QString layerName = metadataResult.title();
203 if ( layerName.isEmpty() )
204 {
205 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( metadataResult.dataProviderName(), metadataResult.uri() );
206 if ( components.contains( u"layerName"_s ) )
207 {
208 layerName = components.value( u"layerName"_s ).toString();
209 }
210 else if ( components.contains( u"table"_s ) )
211 {
212 layerName = components.value( u"table"_s ).toString();
213 }
214 else
215 {
216 layerName = metadataResult.identifier();
217 }
218 }
219
220 switch ( metadataResult.layerType() )
221 {
223 {
225 emit addRasterLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
227 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
228 break;
229 }
231 {
233 emit addVectorLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
235 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
236 break;
237 }
239 {
241 emit addMeshLayer( metadataResult.uri(), layerName, metadataResult.dataProviderName() );
243 emit addLayer( metadataResult.layerType(), metadataResult.uri(), layerName, metadataResult.dataProviderName() );
244 break;
245 }
246 default: // unsupported
247 {
248 // Ignore
249 break;
250 }
251 }
252 }
253 }
254}
255
257{
258 mSearchFilterLineEdit->clear();
259 mExtentFilterComboBox->setCurrentIndex( 0 );
260}
261
263{
264 QgsAbstractDataSourceWidget::showEvent( event );
265 mSearchFilterLineEdit->setText( mProxyModel->filterString() );
266 refreshInternal();
267}
268
269void QgsLayerMetadataSearchWidget::showHelp()
270{
271 QgsHelp::openHelp( u"managing_data_source/opening_data.html#the-layer-metadata-search-panel"_s );
272}
273
274void QgsLayerMetadataSearchWidget::refreshInternal()
275{
276 if ( mReloadRequired && isVisible() )
277 {
278 mIsLoading = true;
279 mSourceModel->reloadAsync();
280 }
281}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Null
No geometry.
Definition qgis.h:384
@ Vector
Vector layer.
Definition qgis.h:207
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:210
@ Raster
Raster layer.
Definition qgis.h:208
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:41
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:120
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:7504
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7503
QgsCoordinateTransformContext transformContext
Coordinate transform context.