41 #include <QButtonGroup> 42 #include <QFileDialog> 43 #include <QRadioButton> 44 #include <QDomDocument> 45 #include <QHeaderView> 46 #include <QImageReader> 47 #include <QInputDialog> 49 #include <QMessageBox> 53 #include <QNetworkRequest> 54 #include <QNetworkReply> 62 connect( mNewButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mNewButton_clicked );
63 connect( mEditButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mEditButton_clicked );
64 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mDeleteButton_clicked );
65 connect( mSaveButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSaveButton_clicked );
66 connect( mLoadButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLoadButton_clicked );
67 connect( mConnectButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mConnectButton_clicked );
68 connect( mChangeCRSButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mChangeCRSButton_clicked );
69 connect( mLayersTreeWidget, &QTreeWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged );
70 connect( mConnectionsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsOWSSourceSelect::mConnectionsComboBox_activated );
71 connect( mAddDefaultButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mAddDefaultButton_clicked );
72 connect( mSearchButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSearchButton_clicked );
73 connect( mSearchTableWidget, &QTableWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mSearchTableWidget_itemSelectionChanged );
74 connect( mTilesetsTableWidget, &QTableWidget::itemClicked,
this, &QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked );
75 connect( mLayerUpButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerUpButton_clicked );
76 connect( mLayerDownButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerDownButton_clicked );
80 setWindowTitle( tr(
"Add Layer(s) from a %1 Server" ).arg( service ) );
84 mTileWidthLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
85 mTileHeightLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
86 mFeatureCountLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
88 mCacheComboBox->addItem( tr(
"Always cache" ), QNetworkRequest::AlwaysCache );
89 mCacheComboBox->addItem( tr(
"Prefer cache" ), QNetworkRequest::PreferCache );
90 mCacheComboBox->addItem( tr(
"Prefer network" ), QNetworkRequest::PreferNetwork );
91 mCacheComboBox->addItem( tr(
"Always network" ), QNetworkRequest::AlwaysNetwork );
94 mCacheComboBox->setCurrentIndex( mCacheComboBox->findData( QNetworkRequest::PreferNetwork ) );
96 if (
widgetMode() != QgsProviderRegistry::WidgetMode::Manager )
103 mSelectedCRS = currentRefSys.
authid();
108 mTabWidget->removeTab( mTabWidget->indexOf( mLayerOrderTab ) );
109 mTabWidget->removeTab( mTabWidget->indexOf( mTilesetsTab ) );
111 mFormatWidget->hide();
113 mCacheWidget->hide();
120 QgsDebugMsg( QStringLiteral(
"restoring geometry" ) );
121 restoreGeometry( settings.
value( QStringLiteral(
"Windows/WMSSourceSelect/geometry" ) ).toByteArray() );
127 QgsDebugMsg( QStringLiteral(
"saving geometry" ) );
138 mFormatComboBox->clear();
139 mFormatComboBox->setEnabled(
false );
151 if ( mProviderFormats.isEmpty() )
154 for (
int i = 0; i < mProviderFormats.size(); i++ )
157 mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
171 QMap<QString, QString> formatsMap;
172 formatsMap.insert( QStringLiteral(
"geotiff" ), QStringLiteral(
"tiff" ) );
173 formatsMap.insert( QStringLiteral(
"gtiff" ), QStringLiteral(
"tiff" ) );
174 formatsMap.insert( QStringLiteral(
"tiff" ), QStringLiteral(
"tiff" ) );
175 formatsMap.insert( QStringLiteral(
"tif" ), QStringLiteral(
"tiff" ) );
176 formatsMap.insert( QStringLiteral(
"gif" ), QStringLiteral(
"gif" ) );
177 formatsMap.insert( QStringLiteral(
"jpeg" ), QStringLiteral(
"jpeg" ) );
178 formatsMap.insert( QStringLiteral(
"jpg" ), QStringLiteral(
"jpeg" ) );
179 formatsMap.insert( QStringLiteral(
"png" ), QStringLiteral(
"png" ) );
183 for (
int i = 0; i < layersFormats.size(); i++ )
185 QString format = layersFormats.value( i );
187 QString simpleFormat = format.toLower().remove( QStringLiteral(
"image/" ) ).remove( QRegExp(
"_.*" ) );
188 QgsDebugMsg(
"server simpleFormat = " + simpleFormat );
189 QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
190 QgsDebugMsg(
"server mimeFormat = " + mimeFormat );
192 QString label = format;
194 if ( mMimeLabelMap.contains( mimeFormat ) )
196 if ( format != mMimeLabelMap.value( mimeFormat ) )
199 label +=
" / " + mMimeLabelMap.value( mimeFormat );
202 if ( simpleFormat.contains( QLatin1String(
"tif" ) ) )
204 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
214 QgsDebugMsg( QStringLiteral(
"format %1 unknown" ).arg( format ) );
217 mFormatComboBox->insertItem( i, label );
221 preferred = preferred >= 0 ? preferred : 0;
222 mFormatComboBox->setCurrentIndex( preferred );
224 mFormatComboBox->setEnabled(
true );
229 mTimeComboBox->clear();
236 mTimeComboBox->clear();
237 mTimeComboBox->setEnabled(
false );
242 mConnectionsComboBox->clear();
250 if (
string.compare( QLatin1String(
"wms" ), Qt::CaseInsensitive ) == 0 )
252 else if (
string.compare( QLatin1String(
"wfs" ), Qt::CaseInsensitive ) == 0 )
254 else if (
string.compare( QLatin1String(
"wcs" ), Qt::CaseInsensitive ) == 0 )
260 void QgsOWSSourceSelect::mNewButton_clicked()
274 void QgsOWSSourceSelect::mEditButton_clicked()
288 void QgsOWSSourceSelect::mDeleteButton_clicked()
290 QString msg = tr(
"Are you sure you want to remove the %1 connection and all associated settings?" )
291 .arg( mConnectionsComboBox->currentText() );
292 QMessageBox::StandardButton result = QMessageBox::question(
this, tr(
"Delete Connection" ), msg, QMessageBox::Yes | QMessageBox::No );
293 if ( result == QMessageBox::Yes )
296 mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );
302 void QgsOWSSourceSelect::mSaveButton_clicked()
308 void QgsOWSSourceSelect::mLoadButton_clicked()
310 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(),
311 tr(
"XML files (*.xml *.XML)" ) );
312 if ( fileName.isEmpty() )
325 const QStringList &names,
326 QMap<int, QgsTreeWidgetItem *> &items,
327 int &layerAndStyleCount,
328 const QMap<int, int> &layerParents,
329 const QMap<int, QStringList> &layerParentNames )
331 QgsDebugMsg( QStringLiteral(
"id = %1 layerAndStyleCount = %2 names = %3 " ).arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ) );
332 if ( items.contains(
id ) )
337 if ( layerParents.contains(
id ) )
340 int parent = layerParents[ id ];
341 item =
new QgsTreeWidgetItem(
createItem( parent, layerParentNames[ parent ], items, layerAndStyleCount, layerParents, layerParentNames ) );
346 item->setText( 0, QString::number( ++layerAndStyleCount ) );
347 item->setText( 1, names[0].simplified() );
348 item->setText( 2, names[1].simplified() );
349 item->setText( 3, names[2].simplified() );
350 item->setToolTip( 3,
"<font color=black>" + names[2].simplified() +
"</font>" );
361 void QgsOWSSourceSelect::mConnectButton_clicked()
364 mLayersTreeWidget->clear();
369 mConnName = mConnectionsComboBox->currentText();
374 QApplication::setOverrideCursor( Qt::WaitCursor );
376 QgsDebugMsg( QStringLiteral(
"call populateLayerList" ) );
379 QApplication::restoreOverrideCursor();
386 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
389 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
390 for ( QTreeWidgetItem *item : constSelectedItems )
392 QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
393 if ( !layer.isEmpty() )
404 mySelector->
setCrs( defaultCRS );
407 if ( !mySelector->exec() )
410 mSelectedCRS = mySelector->
crs().
authid();
415 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
423 void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
431 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
433 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
435 if ( !mSelectedLayersCRSs.isEmpty() )
440 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
441 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
443 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
447 if ( it == mSelectedLayersCRSs.constBegin() )
455 if ( it == mSelectedLayersCRSs.constEnd() )
458 mSelectedCRS = defaultCRS;
461 mChangeCRSButton->setEnabled(
true );
468 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
469 mSelectedCRS.clear();
470 mSelectedCRSLabel->clear();
471 mChangeCRSButton->setEnabled(
false );
474 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
478 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
481 mTilesetsTableWidget->blockSignals(
true );
482 mTilesetsTableWidget->clearSelection();
485 QgsDebugMsg( QStringLiteral(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
486 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
493 mTilesetsTableWidget->blockSignals(
false );
517 int cache = mCacheComboBox->currentData().toInt();
518 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
528 return mTimeComboBox->currentText();
535 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
537 if ( mConnectionsComboBox->currentIndex() < 0 )
539 if ( toSelect.isNull() )
540 mConnectionsComboBox->setCurrentIndex( 0 );
542 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
545 if ( mConnectionsComboBox->count() == 0 )
548 mConnectButton->setEnabled(
false );
549 mEditButton->setEnabled(
false );
550 mDeleteButton->setEnabled(
false );
551 mSaveButton->setEnabled(
false );
556 mConnectButton->setEnabled(
true );
557 mEditButton->setEnabled(
true );
558 mDeleteButton->setEnabled(
true );
559 mSaveButton->setEnabled(
true );
567 mStatusLabel->setText( message );
577 mv->setWindowTitle( title );
579 if ( format == QLatin1String(
"text/html" ) )
590 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
596 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
613 QMap<QString, QString> exampleServers;
614 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
615 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
621 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
622 for ( ; i != exampleServers.constEnd(); ++i )
626 if ( !keys.contains( i.key() ) )
628 QString path = i.key();
629 settings.
setValue( path +
"/url", i.value() );
635 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have " 636 "been added to the server list. Note that if " 637 "you access the Internet via a web proxy, you will " 638 "need to set the proxy settings in the QGIS options dialog." ) +
"</p>" );
643 QDomElement title = item.firstChildElement( QStringLiteral(
"title" ) );
645 QDomElement description = item.firstChildElement( QStringLiteral(
"description" ) );
647 QDomElement link = item.firstChildElement( QStringLiteral(
"link" ) );
655 QTableWidgetItem *tableItem =
new QTableWidgetItem( el.text() );
657 tableItem->setToolTip( el.text() );
658 mSearchTableWidget->setItem( row, column, tableItem );
662 void QgsOWSSourceSelect::mSearchButton_clicked()
665 mSearchTableWidget->clearContents();
666 mSearchTableWidget->setRowCount( 0 );
669 mSearchAddButton->setEnabled(
false );
671 QApplication::setOverrideCursor( Qt::WaitCursor );
675 QString mySearchUrl = settings.
value( QStringLiteral(
"qgis/WMSSearchUrl" ),
"http://geopole.org/wms/search?search=%1&type=rss" ).toString();
676 QUrl url( mySearchUrl.arg( mSearchTermLineEdit->text() ) );
680 connect( r, &QNetworkReply::finished,
this, &QgsOWSSourceSelect::searchFinished );
683 void QgsOWSSourceSelect::searchFinished()
685 QApplication::restoreOverrideCursor();
687 QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
691 if ( r->error() == QNetworkReply::NoError )
694 QDomDocument doc( QStringLiteral(
"RSS" ) );
695 QByteArray res = r->readAll();
698 if ( doc.setContent( res, &error, &line, &column ) )
700 QDomNodeList list = doc.elementsByTagName( QStringLiteral(
"item" ) );
701 mSearchTableWidget->setRowCount( list.size() );
702 for (
int i = 0; i < list.size(); i++ )
704 if ( list.item( i ).isElement() )
706 QDomElement item = list.item( i ).toElement();
711 mSearchTableWidget->resizeColumnsToContents();
715 QgsDebugMsg( QStringLiteral(
"setContent failed" ) );
716 showStatusMessage( tr(
"parse error at row %1, column %2: %3" ).arg( line ).arg( column ).arg( error ) );
727 void QgsOWSSourceSelect::mSearchTableWidget_itemSelectionChanged()
729 mSearchAddButton->setEnabled( mSearchTableWidget->currentRow() != -1 );
732 void QgsOWSSourceSelect::mLayerUpButton_clicked()
734 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
735 if ( selectionList.empty() )
739 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
740 if ( selectedIndex < 1 )
745 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
746 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
747 mLayerOrderTreeWidget->clearSelection();
748 selectedItem->setSelected(
true );
751 void QgsOWSSourceSelect::mLayerDownButton_clicked()
753 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
754 if ( selectionList.empty() )
758 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
759 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
764 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
765 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
766 mLayerOrderTreeWidget->clearSelection();
767 selectedItem->setSelected(
true );
772 return QList<SupportedFormat>();
777 return QStringList();
782 return QStringList();
787 return QStringList();
790 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 (embedded is for the data source manager dialog) ...
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.
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.
~QgsOWSSourceSelect() override
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
A generic dialog to prompt the user for a Coordinate Reference System.
void setMessageAsHtml(const QString &msg)
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
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
void setMessageAsPlainText(const QString &msg)
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 showMessage(bool blocking=true) override
display the message to the user and deletes itself
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).
A generic message view for displaying QGIS messages.
virtual QList< QgsOWSSourceSelect::SupportedFormat > providerFormats()
List of image formats (encodings) supported by provider.
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.