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>
56 #include <QRegularExpression>
65 connect( mNewButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mNewButton_clicked );
66 connect( mEditButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mEditButton_clicked );
67 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mDeleteButton_clicked );
68 connect( mSaveButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSaveButton_clicked );
69 connect( mLoadButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLoadButton_clicked );
70 connect( mConnectButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mConnectButton_clicked );
71 connect( mChangeCRSButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mChangeCRSButton_clicked );
72 connect( mLayersTreeWidget, &QTreeWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged );
73 connect( mConnectionsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsOWSSourceSelect::mConnectionsComboBox_activated );
74 connect( mAddDefaultButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mAddDefaultButton_clicked );
75 connect( mTilesetsTableWidget, &QTableWidget::itemClicked,
this, &QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked );
76 connect( mLayerUpButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerUpButton_clicked );
77 connect( mLayerDownButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerDownButton_clicked );
81 setWindowTitle( tr(
"Add Layer(s) from a %1 Server" ).arg( service ) );
85 mTileWidthLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
86 mTileHeightLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
87 mFeatureCountLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
89 mCacheComboBox->addItem( tr(
"Always Cache" ), QNetworkRequest::AlwaysCache );
90 mCacheComboBox->addItem( tr(
"Prefer Cache" ), QNetworkRequest::PreferCache );
91 mCacheComboBox->addItem( tr(
"Prefer Network" ), QNetworkRequest::PreferNetwork );
92 mCacheComboBox->addItem( tr(
"Always Network" ), QNetworkRequest::AlwaysNetwork );
95 mCacheComboBox->setCurrentIndex( mCacheComboBox->findData( QNetworkRequest::PreferNetwork ) );
97 if (
widgetMode() != QgsProviderRegistry::WidgetMode::Manager )
104 mSelectedCRS = currentRefSys.
authid();
109 mTabWidget->removeTab( mTabWidget->indexOf( mLayerOrderTab ) );
110 mTabWidget->removeTab( mTabWidget->indexOf( mTilesetsTab ) );
112 mFormatWidget->hide();
114 mCacheWidget->hide();
128 mLayersTreeWidget->clearSelection();
133 mFormatComboBox->clear();
134 mFormatComboBox->setEnabled(
false );
146 if ( mProviderFormats.isEmpty() )
149 for (
int i = 0; i < mProviderFormats.size(); i++ )
152 mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
166 QMap<QString, QString> formatsMap;
167 formatsMap.insert( QStringLiteral(
"geotiff" ), QStringLiteral(
"tiff" ) );
168 formatsMap.insert( QStringLiteral(
"gtiff" ), QStringLiteral(
"tiff" ) );
169 formatsMap.insert( QStringLiteral(
"tiff" ), QStringLiteral(
"tiff" ) );
170 formatsMap.insert( QStringLiteral(
"tif" ), QStringLiteral(
"tiff" ) );
171 formatsMap.insert( QStringLiteral(
"gif" ), QStringLiteral(
"gif" ) );
172 formatsMap.insert( QStringLiteral(
"jpeg" ), QStringLiteral(
"jpeg" ) );
173 formatsMap.insert( QStringLiteral(
"jpg" ), QStringLiteral(
"jpeg" ) );
174 formatsMap.insert( QStringLiteral(
"png" ), QStringLiteral(
"png" ) );
178 for (
int i = 0; i < layersFormats.size(); i++ )
180 const QString format = layersFormats.value( i );
182 const QString simpleFormat = format.toLower().remove( QStringLiteral(
"image/" ) ).remove( QRegularExpression(
"_.*" ) );
183 QgsDebugMsg(
"server simpleFormat = " + simpleFormat );
184 const QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
185 QgsDebugMsg(
"server mimeFormat = " + mimeFormat );
187 QString label = format;
189 if ( mMimeLabelMap.contains( mimeFormat ) )
191 if ( format != mMimeLabelMap.value( mimeFormat ) )
194 label +=
" / " + mMimeLabelMap.value( mimeFormat );
197 if ( simpleFormat.contains( QLatin1String(
"tif" ) ) )
199 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
209 QgsDebugMsg( QStringLiteral(
"format %1 unknown" ).arg( format ) );
212 mFormatComboBox->insertItem( i, label );
216 preferred = preferred >= 0 ? preferred : 0;
217 mFormatComboBox->setCurrentIndex( preferred );
219 mFormatComboBox->setEnabled(
true );
224 mTimeComboBox->clear();
231 mTimeComboBox->clear();
232 mTimeComboBox->setEnabled(
false );
237 mConnectionsComboBox->clear();
245 if (
string.compare( QLatin1String(
"wms" ), Qt::CaseInsensitive ) == 0 )
247 else if (
string.compare( QLatin1String(
"wfs" ), Qt::CaseInsensitive ) == 0 )
249 else if (
string.compare( QLatin1String(
"wcs" ), Qt::CaseInsensitive ) == 0 )
255 void QgsOWSSourceSelect::mNewButton_clicked()
269 void QgsOWSSourceSelect::mEditButton_clicked()
283 void QgsOWSSourceSelect::mDeleteButton_clicked()
285 const QString msg = tr(
"Are you sure you want to remove the %1 connection and all associated settings?" )
286 .arg( mConnectionsComboBox->currentText() );
287 const QMessageBox::StandardButton result = QMessageBox::question(
this, tr(
"Remove Connection" ), msg, QMessageBox::Yes | QMessageBox::No );
288 if ( result == QMessageBox::Yes )
291 mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );
297 void QgsOWSSourceSelect::mSaveButton_clicked()
303 void QgsOWSSourceSelect::mLoadButton_clicked()
305 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(),
306 tr(
"XML files (*.xml *.XML)" ) );
307 if ( fileName.isEmpty() )
320 const QStringList &names,
321 QMap<int, QgsTreeWidgetItem *> &items,
322 int &layerAndStyleCount,
323 const QMap<int, int> &layerParents,
324 const QMap<int, QStringList> &layerParentNames )
326 QgsDebugMsg( QStringLiteral(
"id = %1 layerAndStyleCount = %2 names = %3 " ).arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ) );
327 if ( items.contains(
id ) )
332 if ( layerParents.contains(
id ) )
335 const int parent = layerParents[ id ];
336 item =
new QgsTreeWidgetItem(
createItem( parent, layerParentNames[ parent ], items, layerAndStyleCount, layerParents, layerParentNames ) );
341 item->setText( 0, QString::number( ++layerAndStyleCount ) );
342 item->setText( 1, names[0].simplified() );
343 item->setText( 2, names[1].simplified() );
344 item->setText( 3, names[2].simplified() );
345 item->setToolTip( 3,
"<font color=black>" + names[2].simplified() +
"</font>" );
356 void QgsOWSSourceSelect::mConnectButton_clicked()
359 mLayersTreeWidget->clear();
364 mConnName = mConnectionsComboBox->currentText();
369 QApplication::setOverrideCursor( Qt::WaitCursor );
371 QgsDebugMsg( QStringLiteral(
"call populateLayerList" ) );
374 QApplication::restoreOverrideCursor();
381 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
384 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
385 for ( QTreeWidgetItem *item : constSelectedItems )
387 const QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
388 if ( !layer.isEmpty() )
398 mySelector->
setCrs( defaultCRS );
405 if ( !mySelector->exec() )
408 mSelectedCRS = mySelector->
crs().
authid();
413 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
421 void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
429 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
431 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
433 if ( !mSelectedLayersCRSs.isEmpty() )
438 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
439 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
441 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
445 if ( it == mSelectedLayersCRSs.constBegin() )
453 if ( it == mSelectedLayersCRSs.constEnd() )
462 mSelectedCRS = defaultCRS;
466 mChangeCRSButton->setEnabled(
true );
473 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
474 mSelectedCRS.clear();
475 mSelectedCRSLabel->clear();
476 mChangeCRSButton->setEnabled(
false );
479 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
483 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
486 mTilesetsTableWidget->blockSignals(
true );
487 mTilesetsTableWidget->clearSelection();
490 QgsDebugMsg( QStringLiteral(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
491 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
498 mTilesetsTableWidget->blockSignals(
false );
522 const int cache = mCacheComboBox->currentData().toInt();
523 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
533 return mTimeComboBox->currentText();
540 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
542 if ( mConnectionsComboBox->currentIndex() < 0 )
544 if ( toSelect.isNull() )
545 mConnectionsComboBox->setCurrentIndex( 0 );
547 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
550 if ( mConnectionsComboBox->count() == 0 )
553 mConnectButton->setEnabled(
false );
554 mEditButton->setEnabled(
false );
555 mDeleteButton->setEnabled(
false );
556 mSaveButton->setEnabled(
false );
561 mConnectButton->setEnabled(
true );
562 mEditButton->setEnabled(
true );
563 mDeleteButton->setEnabled(
true );
564 mSaveButton->setEnabled(
true );
572 mStatusLabel->setText( message );
582 mv->setWindowTitle( title );
584 if ( format == QLatin1String(
"text/html" ) )
595 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
601 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
618 QMap<QString, QString> exampleServers;
619 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
620 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
626 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
627 for ( ; i != exampleServers.constEnd(); ++i )
631 if ( !keys.contains( i.key() ) )
633 const QString path = i.key();
634 settings.
setValue( path +
"/url", i.value() );
640 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have "
641 "been added to the server list. Note that if "
642 "you access the Internet via a web proxy, you will "
643 "need to set the proxy settings in the QGIS options dialog." ) +
"</p>" );
646 void QgsOWSSourceSelect::mLayerUpButton_clicked()
648 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
649 if ( selectionList.empty() )
653 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
654 if ( selectedIndex < 1 )
659 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
660 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
661 mLayerOrderTreeWidget->clearSelection();
662 selectedItem->setSelected(
true );
665 void QgsOWSSourceSelect::mLayerDownButton_clicked()
667 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
668 if ( selectionList.empty() )
672 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
673 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
678 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
679 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
680 mLayerOrderTreeWidget->clearSelection();
681 selectedItem->setSelected(
true );
686 return QList<SupportedFormat>();
691 return QStringList();
696 return QStringList();
701 return QStringList();
704 void QgsOWSSourceSelect::updateButtons()
This class 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(IdentifierType type=MediumString) const
Returns a user friendly identifier for the CRS.
QString authid() const
Returns the authority identifier for the CRS.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
static QgsGui * instance()
Returns a pointer to the singleton instance.
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...
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)
Dialog to allow the user to configure and save connection information for an HTTP Server for WMS,...
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.
QgsOWSSourceSelect(const QString &service, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::None)
Constructor.
QString connectionInfo()
Connection info (uri)
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.
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 clearFormats()
Clear previously set formats.
virtual void populateLayerList()
Populate the layer list.
QMap< QString, QString > mCrsNames
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
A generic dialog to prompt the user for a Coordinate Reference System.
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.
This class is a composition of two QSettings instances:
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.
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.
CONSTLATIN1STRING geoEpsgCrsAuthId()
Geographic coord sys from EPSG authority.
QgsNewHttpConnection::ConnectionType connectionTypeFromServiceString(const QString &string)