39#include <QButtonGroup>
40#include <QDomDocument>
43#include <QImageReader>
44#include <QInputDialog>
47#include <QNetworkReply>
48#include <QNetworkRequest>
50#include <QRadioButton>
51#include <QRegularExpression>
56#include "moc_qgsowssourceselect.cpp"
58using namespace Qt::StringLiterals;
67 connect( mNewButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mNewButton_clicked );
68 connect( mEditButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mEditButton_clicked );
69 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mDeleteButton_clicked );
70 connect( mSaveButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSaveButton_clicked );
71 connect( mLoadButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLoadButton_clicked );
72 connect( mConnectButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mConnectButton_clicked );
73 connect( mChangeCRSButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mChangeCRSButton_clicked );
74 connect( mLayersTreeWidget, &QTreeWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged );
75 connect( mConnectionsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsOWSSourceSelect::mConnectionsComboBox_activated );
76 connect( mAddDefaultButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mAddDefaultButton_clicked );
77 connect( mTilesetsTableWidget, &QTableWidget::itemClicked,
this, &QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked );
78 connect( mLayerUpButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerUpButton_clicked );
79 connect( mLayerDownButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerDownButton_clicked );
83 setWindowTitle( tr(
"Add Layer(s) from a %1 Server" ).arg( service ) );
87 mCacheComboBox->addItem( tr(
"Always Cache" ), QNetworkRequest::AlwaysCache );
88 mCacheComboBox->addItem( tr(
"Prefer Cache" ), QNetworkRequest::PreferCache );
89 mCacheComboBox->addItem( tr(
"Prefer Network" ), QNetworkRequest::PreferNetwork );
90 mCacheComboBox->addItem( tr(
"Always Network" ), QNetworkRequest::AlwaysNetwork );
93 mCacheComboBox->setCurrentIndex( mCacheComboBox->findData( QNetworkRequest::PreferNetwork ) );
102 mSelectedCRS = currentRefSys.
authid();
107 mTabWidget->removeTab( mTabWidget->indexOf( mLayerOrderTab ) );
108 mTabWidget->removeTab( mTabWidget->indexOf( mTilesetsTab ) );
110 mFormatWidget->hide();
112 mCacheWidget->hide();
129 mSpatialExtentBox->setOutputCrs( crs );
134 mSpatialExtentBox->setCurrentExtent( canvas->
extent(), destinationCrs );
135 mSpatialExtentBox->setOutputExtentFromCurrent();
136 mSpatialExtentBox->setMapCanvas( canvas );
137 mSpatialExtentBox->setChecked(
false );
147 mLayersTreeWidget->clearSelection();
152 mFormatComboBox->clear();
153 mFormatComboBox->setEnabled(
false );
164 if ( mProviderFormats.isEmpty() )
167 for (
int i = 0; i < mProviderFormats.size(); i++ )
170 mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
184 QMap<QString, QString> formatsMap;
185 formatsMap.insert( u
"geotiff"_s, u
"tiff"_s );
186 formatsMap.insert( u
"gtiff"_s, u
"tiff"_s );
187 formatsMap.insert( u
"tiff"_s, u
"tiff"_s );
188 formatsMap.insert( u
"tif"_s, u
"tiff"_s );
189 formatsMap.insert( u
"gif"_s, u
"gif"_s );
190 formatsMap.insert( u
"jpeg"_s, u
"jpeg"_s );
191 formatsMap.insert( u
"jpg"_s, u
"jpeg"_s );
192 formatsMap.insert( u
"png"_s, u
"png"_s );
196 for (
int i = 0; i < layersFormats.size(); i++ )
198 const QString format = layersFormats.value( i );
200 const QString simpleFormat = format.toLower().remove( u
"image/"_s ).remove( QRegularExpression(
"_.*" ) );
202 const QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
205 QString label = format;
207 if ( mMimeLabelMap.contains( mimeFormat ) )
209 if ( format != mMimeLabelMap.value( mimeFormat ) )
212 label +=
" / " + mMimeLabelMap.value( mimeFormat );
215 if ( simpleFormat.contains(
"tif"_L1 ) )
217 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
230 mFormatComboBox->insertItem( i, label );
234 preferred = preferred >= 0 ? preferred : 0;
235 mFormatComboBox->setCurrentIndex( preferred );
237 mFormatComboBox->setEnabled(
true );
242 mTimeComboBox->clear();
249 mTimeComboBox->clear();
250 mTimeComboBox->setEnabled(
false );
255 mConnectionsComboBox->clear();
263 if (
string.compare(
"wms"_L1, Qt::CaseInsensitive ) == 0 )
265 else if (
string.compare(
"wfs"_L1, Qt::CaseInsensitive ) == 0 )
267 else if (
string.compare(
"wcs"_L1, Qt::CaseInsensitive ) == 0 )
273void QgsOWSSourceSelect::mNewButton_clicked()
287void QgsOWSSourceSelect::mEditButton_clicked()
301void QgsOWSSourceSelect::mDeleteButton_clicked()
303 const QString msg = tr(
"Are you sure you want to remove the %1 connection and all associated settings?" )
304 .arg( mConnectionsComboBox->currentText() );
305 const QMessageBox::StandardButton result = QMessageBox::question(
this, tr(
"Remove Connection" ), msg, QMessageBox::Yes | QMessageBox::No );
306 if ( result == QMessageBox::Yes )
309 mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );
315void QgsOWSSourceSelect::mSaveButton_clicked()
321void QgsOWSSourceSelect::mLoadButton_clicked()
323 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(), tr(
"XML files (*.xml *.XML)" ) );
324 if ( fileName.isEmpty() )
337 const QStringList &names,
338 QMap<int, QgsTreeWidgetItem *> &items,
339 int &layerAndStyleCount,
340 const QMap<int, int> &layerParents,
341 const QMap<int, QStringList> &layerParentNames
344 QgsDebugMsgLevel( u
"id = %1 layerAndStyleCount = %2 names = %3 "_s.arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ), 2 );
345 if ( items.contains(
id ) )
350 if ( layerParents.contains(
id ) )
353 const int parent = layerParents[id];
354 item =
new QgsTreeWidgetItem(
createItem( parent, layerParentNames[parent], items, layerAndStyleCount, layerParents, layerParentNames ) );
359 item->setText( 0, QString::number( ++layerAndStyleCount ) );
360 item->setText( 1, names[0].simplified() );
361 item->setText( 2, names[1].simplified() );
362 item->setText( 3, names[2].simplified() );
363 item->setToolTip( 3,
"<font color=black>" + names[2].simplified() +
"</font>" );
374void QgsOWSSourceSelect::mConnectButton_clicked()
376 mLayersTreeWidget->clear();
381 mConnName = mConnectionsComboBox->currentText();
386 QApplication::setOverrideCursor( Qt::WaitCursor );
391 QApplication::restoreOverrideCursor();
398void QgsOWSSourceSelect::mChangeCRSButton_clicked()
401 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
402 for ( QTreeWidgetItem *item : constSelectedItems )
404 const QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
405 if ( !layer.isEmpty() )
409 QgsProjectionSelectionDialog *mySelector =
new QgsProjectionSelectionDialog(
this );
415 mySelector->
setCrs( defaultCRS );
422 if ( !mySelector->exec() )
425 mSelectedCRS = mySelector->
crs().
authid();
426 mSpatialExtentBox->setOutputCrs( mySelector->
crs() );
431 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
439void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
447 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
449 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
451 if ( !mSelectedLayersCRSs.isEmpty() )
456 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
457 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
459 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
463 if ( it == mSelectedLayersCRSs.constBegin() )
471 if ( it == mSelectedLayersCRSs.constEnd() )
473 if ( mSelectedLayersCRSs.constFind(
QgsProject::instance()->crs().authid() ) != mSelectedLayersCRSs.constEnd() )
480 mSelectedCRS = defaultCRS;
485 mChangeCRSButton->setEnabled(
true );
492 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
493 mSelectedCRS.clear();
494 mSelectedCRSLabel->clear();
495 mChangeCRSButton->setEnabled(
false );
498void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
502 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
505 mTilesetsTableWidget->blockSignals(
true );
506 mTilesetsTableWidget->clearSelection();
509 QgsDebugMsgLevel( u
"selecting current row %1"_s.arg( mTilesetsTableWidget->currentRow() ), 2 );
510 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
517 mTilesetsTableWidget->blockSignals(
false );
540 const int cache = mCacheComboBox->currentData().toInt();
541 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
551 return mTimeComboBox->currentText();
558 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
560 if ( mConnectionsComboBox->currentIndex() < 0 )
562 if ( toSelect.isNull() )
563 mConnectionsComboBox->setCurrentIndex( 0 );
565 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
568 if ( mConnectionsComboBox->count() == 0 )
571 mConnectButton->setEnabled(
false );
572 mEditButton->setEnabled(
false );
573 mDeleteButton->setEnabled(
false );
574 mSaveButton->setEnabled(
false );
579 mConnectButton->setEnabled(
true );
580 mEditButton->setEnabled(
true );
581 mDeleteButton->setEnabled(
true );
582 mSaveButton->setEnabled(
true );
590 mStatusLabel->setText( message );
600 mv->setWindowTitle( title );
602 if ( format ==
"text/html"_L1 )
613void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
619void QgsOWSSourceSelect::mAddDefaultButton_clicked()
636 QMap<QString, QString> exampleServers;
637 exampleServers[u
"DM Solutions GMap"_s] = u
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap"_s;
638 exampleServers[u
"Lizardtech server"_s] = u
"http://wms.lizardtech.com/lizardtech/iserv/ows"_s;
644 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
645 for ( ; i != exampleServers.constEnd(); ++i )
649 if ( !keys.contains( i.key() ) )
651 const QString path = i.key();
652 settings.
setValue( path +
"/url", i.value() );
658 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have "
659 "been added to the server list. Note that if "
660 "you access the Internet via a web proxy, you will "
661 "need to set the proxy settings in the QGIS options dialog." )
665void QgsOWSSourceSelect::mLayerUpButton_clicked()
667 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
668 if ( selectionList.empty() )
672 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
673 if ( selectedIndex < 1 )
678 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
679 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
680 mLayerOrderTreeWidget->clearSelection();
681 selectedItem->setSelected(
true );
684void QgsOWSSourceSelect::mLayerDownButton_clicked()
686 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
687 if ( selectionList.empty() )
691 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
692 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
697 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
698 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
699 mLayerOrderTreeWidget->clearSelection();
700 selectedItem->setSelected(
true );
705 return QList<SupportedFormat>();
710 return QStringList();
715 return QStringList();
720 return QStringList();
723void QgsOWSSourceSelect::updateButtons()
static QString geographicCrsAuthId()
Geographic coordinate system auth:id string for a default geographic CRS (EPSG:4326).
Represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString userFriendlyIdentifier(Qgis::CrsIdentifierType type=Qgis::CrsIdentifierType::MediumString) const
Returns a user friendly identifier for the CRS.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
A generic message view for displaying QGIS messages.
void setMessageAsPlainText(const QString &msg)
void showMessage(bool blocking=true) override
display the message to the user and deletes itself
void setMessageAsHtml(const QString &msg)
@ FlagShowHttpSettings
Display the 'http' group.
ConnectionType
Available connection types for configuring in the dialog.
@ ConnectionWms
WMS connection.
@ ConnectionWfs
WFS connection.
@ ConnectionWcs
WCS connection.
virtual QStringList selectedLayersCrses()
Server CRS supported for currently selected layer item(s).
virtual QStringList selectedLayersTimes()
List of times (temporalDomain timePosition/timePeriod for currently selected layer item(s).
QString descriptionForAuthId(const QString &authId)
Returns a textual description for the authority id.
void refresh() override
Triggered when the provider's connections need to be refreshed.
QString selectedTime()
Returns currently selected time.
void clearCrs()
Clear CRSs.
void populateCrs()
Sets supported CRSs.
virtual void enableLayersForCrs(QTreeWidgetItem *item)
QString mService
Service name.
virtual QList< QgsOWSSourceSelect::SupportedFormat > providerFormats()
List of image formats (encodings) supported by provider.
QString connectionInfo()
Connection info (uri).
void setMapCanvas(QgsMapCanvas *mapCanvas) override
Sets the dialog map canvas.
QNetworkRequest::CacheLoadControl selectedCacheLoadControl()
Returns currently selected cache load control.
void populateFormats()
Populate supported formats.
QTableWidgetItem * mCurrentTileset
QString selectedCrs()
Returns currently selected Crs.
void populateTimes()
Populate times.
QgsOWSSourceSelect(const QString &service, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::Standalone)
Constructor.
virtual QStringList selectedLayersFormats()
List of formats supported for currently selected layer item(s).
void reset() override
Called when this source select widget is being shown in a "new and clean" dialog.
void setConnectionListPosition()
Sets the server connection combo box to that stored in the config file.
void clearTimes()
Clear times.
QgsTreeWidgetItem * createItem(int id, const QStringList &names, QMap< int, QgsTreeWidgetItem * > &items, int &layerAndStyleCount, const QMap< int, int > &layerParents, const QMap< int, QStringList > &layerParentNames)
create an item including possible parents
QString selectedFormat()
Returns currently selected format.
QString mConnName
Name for selected connection.
void showError(const QString &title, const QString &format, const QString &error)
show whatever error is exposed.
void populateConnectionList()
Populate the connection list combo box.
QString connName()
Connection name.
void addDefaultServers()
Add a few example servers to the list.
void showStatusMessage(const QString &message)
Sets status message to theMessage.
QgsDataSourceUri mUri
URI for selected connection.
QString mConnectionInfo
Connection info for selected connection.
void prepareExtent()
Prepares the spatial extent box with the general settings including original crs, destination crs and...
void clearFormats()
Clear previously set formats.
virtual void populateLayerList()
Populate the layer list.
QMap< QString, QString > mCrsNames
Connections management for OWS connections.
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateReferenceSystem crs
void showNoCrsForLayerMessage()
When called, the dialog will show a default "layer has no CRS set" message above the projection selec...
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the initial crs to show within the dialog.
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
void setOgcWmsCrsFilter(const QSet< QString > &crsFilter)
filters this dialog by the given CRSs
WidgetMode
Different ways a source select dialog can be used.
@ Manager
Used by data items for QgsDataItem::paramWidget().
Stores settings for use within QGIS.
QStringList childGroups(Qgis::SettingsOrigin origin=Qgis::SettingsOrigin::Any) const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsgLevel(str, level)
QgsNewHttpConnection::ConnectionType connectionTypeFromServiceString(const QString &string)