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();
121 restoreGeometry( settings.
value( QStringLiteral(
"Windows/WMSSourceSelect/geometry" ) ).toByteArray() );
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( QString(
"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( QStringLiteral(
"wms" ), Qt::CaseInsensitive ) == 0 )
252 else if (
string.compare( QStringLiteral(
"wfs" ), Qt::CaseInsensitive ) == 0 )
254 else if (
string.compare( QStringLiteral(
"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( QString(
"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 );
379 QApplication::restoreOverrideCursor();
386 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
389 Q_FOREACH ( QTreeWidgetItem *item, mLayersTreeWidget->selectedItems() )
391 QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
392 if ( !layer.isEmpty() )
403 mySelector->
setCrs( defaultCRS );
406 if ( !mySelector->exec() )
409 mSelectedCRS = mySelector->
crs().
authid();
414 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
422 void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
430 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
432 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
434 if ( !mSelectedLayersCRSs.isEmpty() )
439 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
440 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
442 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
446 if ( it == mSelectedLayersCRSs.constBegin() )
454 if ( it == mSelectedLayersCRSs.constEnd() )
457 mSelectedCRS = defaultCRS;
460 mChangeCRSButton->setEnabled(
true );
467 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
468 mSelectedCRS.clear();
469 mSelectedCRSLabel->clear();
470 mChangeCRSButton->setEnabled(
false );
473 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
477 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
480 mTilesetsTableWidget->blockSignals(
true );
481 mTilesetsTableWidget->clearSelection();
484 QgsDebugMsg( QString(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
485 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
492 mTilesetsTableWidget->blockSignals(
false );
516 int cache = mCacheComboBox->currentData().toInt();
517 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
527 return mTimeComboBox->currentText();
534 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
536 if ( mConnectionsComboBox->currentIndex() < 0 )
538 if ( toSelect.isNull() )
539 mConnectionsComboBox->setCurrentIndex( 0 );
541 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
544 if ( mConnectionsComboBox->count() == 0 )
547 mConnectButton->setEnabled(
false );
548 mEditButton->setEnabled(
false );
549 mDeleteButton->setEnabled(
false );
550 mSaveButton->setEnabled(
false );
555 mConnectButton->setEnabled(
true );
556 mEditButton->setEnabled(
true );
557 mDeleteButton->setEnabled(
true );
558 mSaveButton->setEnabled(
true );
566 mStatusLabel->setText( message );
576 mv->setWindowTitle( title );
578 if ( format == QLatin1String(
"text/html" ) )
589 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
595 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
612 QMap<QString, QString> exampleServers;
613 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
614 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
620 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
621 for ( ; i != exampleServers.constEnd(); ++i )
625 if ( !keys.contains( i.key() ) )
627 QString path = i.key();
628 settings.
setValue( path +
"/url", i.value() );
634 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have " 635 "been added to the server list. Note that if " 636 "you access the Internet via a web proxy, you will " 637 "need to set the proxy settings in the QGIS options dialog." ) +
"</p>" );
642 QDomElement title = item.firstChildElement( QStringLiteral(
"title" ) );
644 QDomElement description = item.firstChildElement( QStringLiteral(
"description" ) );
646 QDomElement link = item.firstChildElement( QStringLiteral(
"link" ) );
654 QTableWidgetItem *tableItem =
new QTableWidgetItem( el.text() );
656 tableItem->setToolTip( el.text() );
657 mSearchTableWidget->setItem( row, column, tableItem );
661 void QgsOWSSourceSelect::mSearchButton_clicked()
664 mSearchTableWidget->clearContents();
665 mSearchTableWidget->setRowCount( 0 );
668 mSearchAddButton->setEnabled(
false );
670 QApplication::setOverrideCursor( Qt::WaitCursor );
674 QString mySearchUrl = settings.
value( QStringLiteral(
"qgis/WMSSearchUrl" ),
"http://geopole.org/wms/search?search=%1&type=rss" ).toString();
675 QUrl url( mySearchUrl.arg( mSearchTermLineEdit->text() ) );
679 connect( r, &QNetworkReply::finished,
this, &QgsOWSSourceSelect::searchFinished );
682 void QgsOWSSourceSelect::searchFinished()
684 QApplication::restoreOverrideCursor();
686 QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
690 if ( r->error() == QNetworkReply::NoError )
693 QDomDocument doc( QStringLiteral(
"RSS" ) );
694 QByteArray res = r->readAll();
697 if ( doc.setContent( res, &error, &line, &column ) )
699 QDomNodeList list = doc.elementsByTagName( QStringLiteral(
"item" ) );
700 mSearchTableWidget->setRowCount( list.size() );
701 for (
int i = 0; i < list.size(); i++ )
703 if ( list.item( i ).isElement() )
705 QDomElement item = list.item( i ).toElement();
710 mSearchTableWidget->resizeColumnsToContents();
715 showStatusMessage( tr(
"parse error at row %1, column %2: %3" ).arg( line ).arg( column ).arg( error ) );
726 void QgsOWSSourceSelect::mSearchTableWidget_itemSelectionChanged()
728 mSearchAddButton->setEnabled( mSearchTableWidget->currentRow() != -1 );
731 void QgsOWSSourceSelect::mLayerUpButton_clicked()
733 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
734 if ( selectionList.empty() )
738 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
739 if ( selectedIndex < 1 )
744 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
745 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
746 mLayerOrderTreeWidget->clearSelection();
747 selectedItem->setSelected(
true );
750 void QgsOWSSourceSelect::mLayerDownButton_clicked()
752 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
753 if ( selectionList.empty() )
757 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
758 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
763 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
764 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
765 mLayerOrderTreeWidget->clearSelection();
766 selectedItem->setSelected(
true );
771 return QList<SupportedFormat>();
776 return QStringList();
781 return QStringList();
786 return QStringList();
789 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 complete uri.
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.