42 #include <QButtonGroup> 43 #include <QFileDialog> 44 #include <QRadioButton> 45 #include <QDomDocument> 46 #include <QHeaderView> 47 #include <QImageReader> 48 #include <QInputDialog> 50 #include <QMessageBox> 54 #include <QNetworkRequest> 55 #include <QNetworkReply> 64 connect( mNewButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mNewButton_clicked );
65 connect( mEditButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mEditButton_clicked );
66 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mDeleteButton_clicked );
67 connect( mSaveButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSaveButton_clicked );
68 connect( mLoadButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLoadButton_clicked );
69 connect( mConnectButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mConnectButton_clicked );
70 connect( mChangeCRSButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mChangeCRSButton_clicked );
71 connect( mLayersTreeWidget, &QTreeWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged );
72 connect( mConnectionsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsOWSSourceSelect::mConnectionsComboBox_activated );
73 connect( mAddDefaultButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mAddDefaultButton_clicked );
74 connect( mSearchButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSearchButton_clicked );
75 connect( mSearchTableWidget, &QTableWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mSearchTableWidget_itemSelectionChanged );
76 connect( mTilesetsTableWidget, &QTableWidget::itemClicked,
this, &QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked );
77 connect( mLayerUpButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerUpButton_clicked );
78 connect( mLayerDownButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerDownButton_clicked );
82 setWindowTitle( tr(
"Add Layer(s) from a %1 Server" ).arg( service ) );
86 mTileWidthLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
87 mTileHeightLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
88 mFeatureCountLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
90 mCacheComboBox->addItem( tr(
"Always cache" ), QNetworkRequest::AlwaysCache );
91 mCacheComboBox->addItem( tr(
"Prefer cache" ), QNetworkRequest::PreferCache );
92 mCacheComboBox->addItem( tr(
"Prefer network" ), QNetworkRequest::PreferNetwork );
93 mCacheComboBox->addItem( tr(
"Always network" ), QNetworkRequest::AlwaysNetwork );
96 mCacheComboBox->setCurrentIndex( mCacheComboBox->findData( QNetworkRequest::PreferNetwork ) );
98 if (
widgetMode() != QgsProviderRegistry::WidgetMode::Manager )
105 mSelectedCRS = currentRefSys.
authid();
110 mTabWidget->removeTab( mTabWidget->indexOf( mLayerOrderTab ) );
111 mTabWidget->removeTab( mTabWidget->indexOf( mTilesetsTab ) );
113 mFormatWidget->hide();
115 mCacheWidget->hide();
129 mLayersTreeWidget->clearSelection();
134 mFormatComboBox->clear();
135 mFormatComboBox->setEnabled(
false );
147 if ( mProviderFormats.isEmpty() )
150 for (
int i = 0; i < mProviderFormats.size(); i++ )
153 mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
167 QMap<QString, QString> formatsMap;
168 formatsMap.insert( QStringLiteral(
"geotiff" ), QStringLiteral(
"tiff" ) );
169 formatsMap.insert( QStringLiteral(
"gtiff" ), QStringLiteral(
"tiff" ) );
170 formatsMap.insert( QStringLiteral(
"tiff" ), QStringLiteral(
"tiff" ) );
171 formatsMap.insert( QStringLiteral(
"tif" ), QStringLiteral(
"tiff" ) );
172 formatsMap.insert( QStringLiteral(
"gif" ), QStringLiteral(
"gif" ) );
173 formatsMap.insert( QStringLiteral(
"jpeg" ), QStringLiteral(
"jpeg" ) );
174 formatsMap.insert( QStringLiteral(
"jpg" ), QStringLiteral(
"jpeg" ) );
175 formatsMap.insert( QStringLiteral(
"png" ), QStringLiteral(
"png" ) );
179 for (
int i = 0; i < layersFormats.size(); i++ )
181 QString format = layersFormats.value( i );
183 QString simpleFormat = format.toLower().remove( QStringLiteral(
"image/" ) ).remove( QRegExp(
"_.*" ) );
184 QgsDebugMsg(
"server simpleFormat = " + simpleFormat );
185 QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
186 QgsDebugMsg(
"server mimeFormat = " + mimeFormat );
188 QString label = format;
190 if ( mMimeLabelMap.contains( mimeFormat ) )
192 if ( format != mMimeLabelMap.value( mimeFormat ) )
195 label +=
" / " + mMimeLabelMap.value( mimeFormat );
198 if ( simpleFormat.contains( QLatin1String(
"tif" ) ) )
200 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
210 QgsDebugMsg( QStringLiteral(
"format %1 unknown" ).arg( format ) );
213 mFormatComboBox->insertItem( i, label );
217 preferred = preferred >= 0 ? preferred : 0;
218 mFormatComboBox->setCurrentIndex( preferred );
220 mFormatComboBox->setEnabled(
true );
225 mTimeComboBox->clear();
232 mTimeComboBox->clear();
233 mTimeComboBox->setEnabled(
false );
238 mConnectionsComboBox->clear();
246 if (
string.compare( QLatin1String(
"wms" ), Qt::CaseInsensitive ) == 0 )
248 else if (
string.compare( QLatin1String(
"wfs" ), Qt::CaseInsensitive ) == 0 )
250 else if (
string.compare( QLatin1String(
"wcs" ), Qt::CaseInsensitive ) == 0 )
256 void QgsOWSSourceSelect::mNewButton_clicked()
270 void QgsOWSSourceSelect::mEditButton_clicked()
284 void QgsOWSSourceSelect::mDeleteButton_clicked()
286 QString msg = tr(
"Are you sure you want to remove the %1 connection and all associated settings?" )
287 .arg( mConnectionsComboBox->currentText() );
288 QMessageBox::StandardButton result = QMessageBox::question(
this, tr(
"Delete Connection" ), msg, QMessageBox::Yes | QMessageBox::No );
289 if ( result == QMessageBox::Yes )
292 mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );
298 void QgsOWSSourceSelect::mSaveButton_clicked()
304 void QgsOWSSourceSelect::mLoadButton_clicked()
306 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(),
307 tr(
"XML files (*.xml *.XML)" ) );
308 if ( fileName.isEmpty() )
321 const QStringList &names,
322 QMap<int, QgsTreeWidgetItem *> &items,
323 int &layerAndStyleCount,
324 const QMap<int, int> &layerParents,
325 const QMap<int, QStringList> &layerParentNames )
327 QgsDebugMsg( QStringLiteral(
"id = %1 layerAndStyleCount = %2 names = %3 " ).arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ) );
328 if ( items.contains(
id ) )
333 if ( layerParents.contains(
id ) )
336 int parent = layerParents[ id ];
337 item =
new QgsTreeWidgetItem(
createItem( parent, layerParentNames[ parent ], items, layerAndStyleCount, layerParents, layerParentNames ) );
342 item->setText( 0, QString::number( ++layerAndStyleCount ) );
343 item->setText( 1, names[0].simplified() );
344 item->setText( 2, names[1].simplified() );
345 item->setText( 3, names[2].simplified() );
346 item->setToolTip( 3,
"<font color=black>" + names[2].simplified() +
"</font>" );
357 void QgsOWSSourceSelect::mConnectButton_clicked()
360 mLayersTreeWidget->clear();
365 mConnName = mConnectionsComboBox->currentText();
370 QApplication::setOverrideCursor( Qt::WaitCursor );
372 QgsDebugMsg( QStringLiteral(
"call populateLayerList" ) );
375 QApplication::restoreOverrideCursor();
382 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
385 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
386 for ( QTreeWidgetItem *item : constSelectedItems )
388 QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
389 if ( !layer.isEmpty() )
400 mySelector->
setCrs( defaultCRS );
403 if ( !mySelector->exec() )
406 mSelectedCRS = mySelector->
crs().
authid();
411 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
419 void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
427 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
429 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
431 if ( !mSelectedLayersCRSs.isEmpty() )
436 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
437 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
439 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
443 if ( it == mSelectedLayersCRSs.constBegin() )
451 if ( it == mSelectedLayersCRSs.constEnd() )
454 mSelectedCRS = defaultCRS;
457 mChangeCRSButton->setEnabled(
true );
464 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
465 mSelectedCRS.clear();
466 mSelectedCRSLabel->clear();
467 mChangeCRSButton->setEnabled(
false );
470 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
474 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
477 mTilesetsTableWidget->blockSignals(
true );
478 mTilesetsTableWidget->clearSelection();
481 QgsDebugMsg( QStringLiteral(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
482 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
489 mTilesetsTableWidget->blockSignals(
false );
513 int cache = mCacheComboBox->currentData().toInt();
514 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
524 return mTimeComboBox->currentText();
531 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
533 if ( mConnectionsComboBox->currentIndex() < 0 )
535 if ( toSelect.isNull() )
536 mConnectionsComboBox->setCurrentIndex( 0 );
538 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
541 if ( mConnectionsComboBox->count() == 0 )
544 mConnectButton->setEnabled(
false );
545 mEditButton->setEnabled(
false );
546 mDeleteButton->setEnabled(
false );
547 mSaveButton->setEnabled(
false );
552 mConnectButton->setEnabled(
true );
553 mEditButton->setEnabled(
true );
554 mDeleteButton->setEnabled(
true );
555 mSaveButton->setEnabled(
true );
563 mStatusLabel->setText( message );
572 QgsMessageViewer *mv =
new QgsMessageViewer(
this );
573 mv->setWindowTitle( title );
575 if ( format == QLatin1String(
"text/html" ) )
577 mv->setMessageAsHtml( error );
581 mv->setMessageAsPlainText( tr(
"Could not understand the response:\n%1" ).arg( error ) );
583 mv->showMessage(
true );
586 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
592 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
609 QMap<QString, QString> exampleServers;
610 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
611 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
617 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
618 for ( ; i != exampleServers.constEnd(); ++i )
622 if ( !keys.contains( i.key() ) )
624 QString path = i.key();
625 settings.
setValue( path +
"/url", i.value() );
631 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have " 632 "been added to the server list. Note that if " 633 "you access the Internet via a web proxy, you will " 634 "need to set the proxy settings in the QGIS options dialog." ) +
"</p>" );
639 QDomElement title = item.firstChildElement( QStringLiteral(
"title" ) );
641 QDomElement description = item.firstChildElement( QStringLiteral(
"description" ) );
643 QDomElement link = item.firstChildElement( QStringLiteral(
"link" ) );
651 QTableWidgetItem *tableItem =
new QTableWidgetItem( el.text() );
653 tableItem->setToolTip( el.text() );
654 mSearchTableWidget->setItem( row, column, tableItem );
658 void QgsOWSSourceSelect::mSearchButton_clicked()
661 mSearchTableWidget->clearContents();
662 mSearchTableWidget->setRowCount( 0 );
665 mSearchAddButton->setEnabled(
false );
667 QApplication::setOverrideCursor( Qt::WaitCursor );
671 QString mySearchUrl = settings.
value( QStringLiteral(
"qgis/WMSSearchUrl" ),
"http://geopole.org/wms/search?search=%1&type=rss" ).toString();
672 QUrl url( mySearchUrl.arg( mSearchTermLineEdit->text() ) );
676 connect( r, &QNetworkReply::finished,
this, &QgsOWSSourceSelect::searchFinished );
679 void QgsOWSSourceSelect::searchFinished()
681 QApplication::restoreOverrideCursor();
683 QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
687 if ( r->error() == QNetworkReply::NoError )
690 QDomDocument doc( QStringLiteral(
"RSS" ) );
691 QByteArray res = r->readAll();
694 if ( doc.setContent( res, &error, &line, &column ) )
696 QDomNodeList list = doc.elementsByTagName( QStringLiteral(
"item" ) );
697 mSearchTableWidget->setRowCount( list.size() );
698 for (
int i = 0; i < list.size(); i++ )
700 if ( list.item( i ).isElement() )
702 QDomElement item = list.item( i ).toElement();
707 mSearchTableWidget->resizeColumnsToContents();
711 QgsDebugMsg( QStringLiteral(
"setContent failed" ) );
712 showStatusMessage( tr(
"parse error at row %1, column %2: %3" ).arg( line ).arg( column ).arg( error ) );
723 void QgsOWSSourceSelect::mSearchTableWidget_itemSelectionChanged()
725 mSearchAddButton->setEnabled( mSearchTableWidget->currentRow() != -1 );
728 void QgsOWSSourceSelect::mLayerUpButton_clicked()
730 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
731 if ( selectionList.empty() )
735 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
736 if ( selectedIndex < 1 )
741 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
742 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
743 mLayerOrderTreeWidget->clearSelection();
744 selectedItem->setSelected(
true );
747 void QgsOWSSourceSelect::mLayerDownButton_clicked()
749 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
750 if ( selectionList.empty() )
754 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
755 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
760 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
761 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
762 mLayerOrderTreeWidget->clearSelection();
763 selectedItem->setSelected(
true );
768 return QList<SupportedFormat>();
773 return QStringList();
778 return QStringList();
783 return QStringList();
786 void QgsOWSSourceSelect::updateButtons()
QStringList childGroups() 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.
WidgetMode
Different ways a source select dialog can be used.
QMap< QString, QString > mCrsNames
void populateCrs()
Sets supported CRSs.
virtual void populateLayerList()
Populate the layer list.
void clearTimes()
Clear times.
This class is a composition of two QSettings instances:
QString connectionInfo()
Connection info (uri)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsNewHttpConnection::ConnectionType connectionTypeFromServiceString(const QString &string)
void setMessage(const QString &message)
Sets a message to show in the dialog.
QgsOWSSourceSelect(const QString &service, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::None)
Constructor.
QString selectedFormat()
Returns currently selected format.
virtual QStringList selectedLayersCrses()
Server CRS supported for currently selected layer item(s)
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
QString descriptionForAuthId(const QString &authId)
Returns a textual description for the authority id.
static QgsGui * instance()
Returns a pointer to the singleton instance.
virtual QStringList selectedLayersFormats()
List of formats supported for currently selected layer item(s)
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
QNetworkRequest::CacheLoadControl selectedCacheLoadControl()
Returns currently selected cache load control.
A generic dialog to prompt the user for a Coordinate Reference System.
void populateConnectionList()
Populate the connection list combo box.
void addWmsListItem(const QDomElement &el, int row, int column)
virtual void enableLayersForCrs(QTreeWidgetItem *item)
void addDefaultServers()
Add a few example servers to the list.
QgsCoordinateReferenceSystem crs
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
void populateFormats()
Populate supported formats.
void clearCrs()
Clear CRSs.
virtual QStringList selectedLayersTimes()
List of times (temporalDomain timePosition/timePeriod 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 setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the initial crs to show within the dialog.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
void setOgcWmsCrsFilter(const QSet< QString > &crsFilter)
filters this dialog by the given CRSs
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
ConnectionType
Available connection types for configuring in the dialog.
QString connName()
Connection name.
void showStatusMessage(const QString &message)
Sets status message to theMessage.
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
QString mConnectionInfo
Connection info for selected connection.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void showError(const QString &title, const QString &format, const QString &error)
show whatever error is exposed.
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class represents a coordinate reference system (CRS).
virtual QList< QgsOWSSourceSelect::SupportedFormat > providerFormats()
List of image formats (encodings) supported by provider.
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...
void refresh() override
Triggered when the provider's connections need to be refreshed.
QString selectedTime()
Returns currently selected time.
void addWmsListRow(const QDomElement &item, int row)
void clearFormats()
Clear previously set formats.
QString mService
Service name.
QTableWidgetItem * mCurrentTileset
Dialog to allow the user to configure and save connection information for an HTTP Server for WMS...
void setConnectionListPosition()
Sets the server connection combo box to that stored in the config file.
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
QgsDataSourceUri mUri
URI for selected connection.
void populateTimes()
Populate times.
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
QString authid() const
Returns the authority identifier for the CRS.
QString mConnName
Name for selected connection.
QString selectedCrs()
Returns currently selected Crs.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.