40 #include <QButtonGroup>
41 #include <QFileDialog>
42 #include <QRadioButton>
43 #include <QDomDocument>
44 #include <QHeaderView>
45 #include <QImageReader>
46 #include <QInputDialog>
48 #include <QMessageBox>
52 #include <QNetworkRequest>
53 #include <QNetworkReply>
54 #include <QRegularExpression>
63 connect( mNewButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mNewButton_clicked );
64 connect( mEditButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mEditButton_clicked );
65 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mDeleteButton_clicked );
66 connect( mSaveButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mSaveButton_clicked );
67 connect( mLoadButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLoadButton_clicked );
68 connect( mConnectButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mConnectButton_clicked );
69 connect( mChangeCRSButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mChangeCRSButton_clicked );
70 connect( mLayersTreeWidget, &QTreeWidget::itemSelectionChanged,
this, &QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged );
71 connect( mConnectionsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsOWSSourceSelect::mConnectionsComboBox_activated );
72 connect( mAddDefaultButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mAddDefaultButton_clicked );
73 connect( mTilesetsTableWidget, &QTableWidget::itemClicked,
this, &QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked );
74 connect( mLayerUpButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerUpButton_clicked );
75 connect( mLayerDownButton, &QPushButton::clicked,
this, &QgsOWSSourceSelect::mLayerDownButton_clicked );
79 setWindowTitle( tr(
"Add Layer(s) from a %1 Server" ).arg( service ) );
83 mTileWidthLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
84 mTileHeightLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
85 mFeatureCountLineEdit->setValidator(
new QIntValidator( 0, 9999,
this ) );
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 ) );
95 if (
widgetMode() != QgsProviderRegistry::WidgetMode::Manager )
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( destinationCrs.
bounds(), destinationCrs );
135 mSpatialExtentBox->setOutputExtentFromCurrent();
136 mSpatialExtentBox->setMapCanvas( canvas );
146 mLayersTreeWidget->clearSelection();
151 mFormatComboBox->clear();
152 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( QStringLiteral(
"geotiff" ), QStringLiteral(
"tiff" ) );
186 formatsMap.insert( QStringLiteral(
"gtiff" ), QStringLiteral(
"tiff" ) );
187 formatsMap.insert( QStringLiteral(
"tiff" ), QStringLiteral(
"tiff" ) );
188 formatsMap.insert( QStringLiteral(
"tif" ), QStringLiteral(
"tiff" ) );
189 formatsMap.insert( QStringLiteral(
"gif" ), QStringLiteral(
"gif" ) );
190 formatsMap.insert( QStringLiteral(
"jpeg" ), QStringLiteral(
"jpeg" ) );
191 formatsMap.insert( QStringLiteral(
"jpg" ), QStringLiteral(
"jpeg" ) );
192 formatsMap.insert( QStringLiteral(
"png" ), QStringLiteral(
"png" ) );
196 for (
int i = 0; i < layersFormats.size(); i++ )
198 const QString format = layersFormats.value( i );
200 const QString simpleFormat = format.toLower().remove( QStringLiteral(
"image/" ) ).remove( QRegularExpression(
"_.*" ) );
201 QgsDebugMsg(
"server simpleFormat = " + simpleFormat );
202 const QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
203 QgsDebugMsg(
"server mimeFormat = " + mimeFormat );
205 QString label = format;
207 if ( mMimeLabelMap.contains( mimeFormat ) )
209 if ( format != mMimeLabelMap.value( mimeFormat ) )
212 label +=
" / " + mMimeLabelMap.value( mimeFormat );
215 if ( simpleFormat.contains( QLatin1String(
"tif" ) ) )
217 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
227 QgsDebugMsg( QStringLiteral(
"format %1 unknown" ).arg( format ) );
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( QLatin1String(
"wms" ), Qt::CaseInsensitive ) == 0 )
265 else if (
string.compare( QLatin1String(
"wfs" ), Qt::CaseInsensitive ) == 0 )
267 else if (
string.compare( QLatin1String(
"wcs" ), Qt::CaseInsensitive ) == 0 )
273 void QgsOWSSourceSelect::mNewButton_clicked()
287 void QgsOWSSourceSelect::mEditButton_clicked()
301 void 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() );
315 void QgsOWSSourceSelect::mSaveButton_clicked()
321 void QgsOWSSourceSelect::mLoadButton_clicked()
323 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(),
324 tr(
"XML files (*.xml *.XML)" ) );
325 if ( fileName.isEmpty() )
338 const QStringList &names,
339 QMap<int, QgsTreeWidgetItem *> &items,
340 int &layerAndStyleCount,
341 const QMap<int, int> &layerParents,
342 const QMap<int, QStringList> &layerParentNames )
344 QgsDebugMsg( QStringLiteral(
"id = %1 layerAndStyleCount = %2 names = %3 " ).arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ) );
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>" );
374 void QgsOWSSourceSelect::mConnectButton_clicked()
377 mLayersTreeWidget->clear();
382 mConnName = mConnectionsComboBox->currentText();
387 QApplication::setOverrideCursor( Qt::WaitCursor );
389 QgsDebugMsg( QStringLiteral(
"call populateLayerList" ) );
392 QApplication::restoreOverrideCursor();
399 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
402 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
403 for ( QTreeWidgetItem *item : constSelectedItems )
405 const QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
406 if ( !layer.isEmpty() )
416 mySelector->
setCrs( defaultCRS );
423 if ( !mySelector->exec() )
426 mSelectedCRS = mySelector->
crs().
authid();
431 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
439 void 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() )
480 mSelectedCRS = defaultCRS;
484 mChangeCRSButton->setEnabled(
true );
491 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
492 mSelectedCRS.clear();
493 mSelectedCRSLabel->clear();
494 mChangeCRSButton->setEnabled(
false );
497 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
501 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
504 mTilesetsTableWidget->blockSignals(
true );
505 mTilesetsTableWidget->clearSelection();
508 QgsDebugMsg( QStringLiteral(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
509 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
516 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 );
599 QgsMessageViewer *mv =
new QgsMessageViewer(
this );
600 mv->setWindowTitle( title );
602 if ( format == QLatin1String(
"text/html" ) )
604 mv->setMessageAsHtml( error );
608 mv->setMessageAsPlainText( tr(
"Could not understand the response:\n%1" ).arg( error ) );
610 mv->showMessage(
true );
613 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
619 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
636 QMap<QString, QString> exampleServers;
637 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
638 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
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." ) +
"</p>" );
664 void QgsOWSSourceSelect::mLayerUpButton_clicked()
666 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
667 if ( selectionList.empty() )
671 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
672 if ( selectedIndex < 1 )
677 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
678 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
679 mLayerOrderTreeWidget->clearSelection();
680 selectedItem->setSelected(
true );
683 void QgsOWSSourceSelect::mLayerDownButton_clicked()
685 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
686 if ( selectionList.empty() )
690 const int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
691 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
696 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
697 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
698 mLayerOrderTreeWidget->clearSelection();
699 selectedItem->setSelected(
true );
704 return QList<SupportedFormat>();
709 return QStringList();
714 return QStringList();
719 return QStringList();
722 void QgsOWSSourceSelect::updateButtons()