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( 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();
127 mLayersTreeWidget->clearSelection();
132 mFormatComboBox->clear();
133 mFormatComboBox->setEnabled(
false );
145 if ( mProviderFormats.isEmpty() )
148 for (
int i = 0; i < mProviderFormats.size(); i++ )
151 mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
165 QMap<QString, QString> formatsMap;
166 formatsMap.insert( QStringLiteral(
"geotiff" ), QStringLiteral(
"tiff" ) );
167 formatsMap.insert( QStringLiteral(
"gtiff" ), QStringLiteral(
"tiff" ) );
168 formatsMap.insert( QStringLiteral(
"tiff" ), QStringLiteral(
"tiff" ) );
169 formatsMap.insert( QStringLiteral(
"tif" ), QStringLiteral(
"tiff" ) );
170 formatsMap.insert( QStringLiteral(
"gif" ), QStringLiteral(
"gif" ) );
171 formatsMap.insert( QStringLiteral(
"jpeg" ), QStringLiteral(
"jpeg" ) );
172 formatsMap.insert( QStringLiteral(
"jpg" ), QStringLiteral(
"jpeg" ) );
173 formatsMap.insert( QStringLiteral(
"png" ), QStringLiteral(
"png" ) );
177 for (
int i = 0; i < layersFormats.size(); i++ )
179 QString format = layersFormats.value( i );
181 QString simpleFormat = format.toLower().remove( QStringLiteral(
"image/" ) ).remove( QRegExp(
"_.*" ) );
182 QgsDebugMsg(
"server simpleFormat = " + simpleFormat );
183 QString mimeFormat =
"image/" + formatsMap.value( simpleFormat );
184 QgsDebugMsg(
"server mimeFormat = " + mimeFormat );
186 QString label = format;
188 if ( mMimeLabelMap.contains( mimeFormat ) )
190 if ( format != mMimeLabelMap.value( mimeFormat ) )
193 label +=
" / " + mMimeLabelMap.value( mimeFormat );
196 if ( simpleFormat.contains( QLatin1String(
"tif" ) ) )
198 if ( preferred < 0 || simpleFormat.startsWith(
'g' ) )
208 QgsDebugMsg( QStringLiteral(
"format %1 unknown" ).arg( format ) );
211 mFormatComboBox->insertItem( i, label );
215 preferred = preferred >= 0 ? preferred : 0;
216 mFormatComboBox->setCurrentIndex( preferred );
218 mFormatComboBox->setEnabled(
true );
223 mTimeComboBox->clear();
230 mTimeComboBox->clear();
231 mTimeComboBox->setEnabled(
false );
236 mConnectionsComboBox->clear();
244 if (
string.compare( QLatin1String(
"wms" ), Qt::CaseInsensitive ) == 0 )
246 else if (
string.compare( QLatin1String(
"wfs" ), Qt::CaseInsensitive ) == 0 )
248 else if (
string.compare( QLatin1String(
"wcs" ), Qt::CaseInsensitive ) == 0 )
254 void QgsOWSSourceSelect::mNewButton_clicked()
268 void QgsOWSSourceSelect::mEditButton_clicked()
282 void QgsOWSSourceSelect::mDeleteButton_clicked()
284 QString msg = tr(
"Are you sure you want to remove the %1 connection and all associated settings?" )
285 .arg( mConnectionsComboBox->currentText() );
286 QMessageBox::StandardButton result = QMessageBox::question(
this, tr(
"Delete Connection" ), msg, QMessageBox::Yes | QMessageBox::No );
287 if ( result == QMessageBox::Yes )
290 mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );
296 void QgsOWSSourceSelect::mSaveButton_clicked()
302 void QgsOWSSourceSelect::mLoadButton_clicked()
304 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Connections" ), QDir::homePath(),
305 tr(
"XML files (*.xml *.XML)" ) );
306 if ( fileName.isEmpty() )
319 const QStringList &names,
320 QMap<int, QgsTreeWidgetItem *> &items,
321 int &layerAndStyleCount,
322 const QMap<int, int> &layerParents,
323 const QMap<int, QStringList> &layerParentNames )
325 QgsDebugMsg( QStringLiteral(
"id = %1 layerAndStyleCount = %2 names = %3 " ).arg(
id ).arg( layerAndStyleCount ).arg( names.join(
"," ) ) );
326 if ( items.contains(
id ) )
331 if ( layerParents.contains(
id ) )
334 int parent = layerParents[ id ];
335 item =
new QgsTreeWidgetItem(
createItem( parent, layerParentNames[ parent ], items, layerAndStyleCount, layerParents, layerParentNames ) );
340 item->setText( 0, QString::number( ++layerAndStyleCount ) );
341 item->setText( 1, names[0].simplified() );
342 item->setText( 2, names[1].simplified() );
343 item->setText( 3, names[2].simplified() );
344 item->setToolTip( 3,
"<font color=black>" + names[2].simplified() +
"</font>" );
355 void QgsOWSSourceSelect::mConnectButton_clicked()
358 mLayersTreeWidget->clear();
363 mConnName = mConnectionsComboBox->currentText();
368 QApplication::setOverrideCursor( Qt::WaitCursor );
370 QgsDebugMsg( QStringLiteral(
"call populateLayerList" ) );
373 QApplication::restoreOverrideCursor();
380 void QgsOWSSourceSelect::mChangeCRSButton_clicked()
383 const auto constSelectedItems = mLayersTreeWidget->selectedItems();
384 for ( QTreeWidgetItem *item : constSelectedItems )
386 QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
387 if ( !layer.isEmpty() )
398 mySelector->
setCrs( defaultCRS );
401 if ( !mySelector->exec() )
404 mSelectedCRS = mySelector->
crs().
authid();
409 for (
int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
417 void QgsOWSSourceSelect::mLayersTreeWidget_itemSelectionChanged()
425 mCRSLabel->setText( tr(
"Coordinate Reference System (%n available)",
"crs count", mSelectedLayersCRSs.count() ) +
':' );
427 mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
429 if ( !mSelectedLayersCRSs.isEmpty() )
434 QSet<QString>::const_iterator it = mSelectedLayersCRSs.constBegin();
435 for ( ; it != mSelectedLayersCRSs.constEnd(); ++it )
437 if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
441 if ( it == mSelectedLayersCRSs.constBegin() )
449 if ( it == mSelectedLayersCRSs.constEnd() )
452 mSelectedCRS = defaultCRS;
455 mChangeCRSButton->setEnabled(
true );
462 mCRSLabel->setText( tr(
"Coordinate Reference System" ) +
':' );
463 mSelectedCRS.clear();
464 mSelectedCRSLabel->clear();
465 mChangeCRSButton->setEnabled(
false );
468 void QgsOWSSourceSelect::mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
472 QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
475 mTilesetsTableWidget->blockSignals(
true );
476 mTilesetsTableWidget->clearSelection();
479 QgsDebugMsg( QStringLiteral(
"selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
480 mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
487 mTilesetsTableWidget->blockSignals(
false );
511 int cache = mCacheComboBox->currentData().toInt();
512 return static_cast<QNetworkRequest::CacheLoadControl
>( cache );
522 return mTimeComboBox->currentText();
529 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
531 if ( mConnectionsComboBox->currentIndex() < 0 )
533 if ( toSelect.isNull() )
534 mConnectionsComboBox->setCurrentIndex( 0 );
536 mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
539 if ( mConnectionsComboBox->count() == 0 )
542 mConnectButton->setEnabled(
false );
543 mEditButton->setEnabled(
false );
544 mDeleteButton->setEnabled(
false );
545 mSaveButton->setEnabled(
false );
550 mConnectButton->setEnabled(
true );
551 mEditButton->setEnabled(
true );
552 mDeleteButton->setEnabled(
true );
553 mSaveButton->setEnabled(
true );
561 mStatusLabel->setText( message );
570 QgsMessageViewer *mv =
new QgsMessageViewer(
this );
571 mv->setWindowTitle( title );
573 if ( format == QLatin1String(
"text/html" ) )
575 mv->setMessageAsHtml( error );
579 mv->setMessageAsPlainText( tr(
"Could not understand the response:\n%1" ).arg( error ) );
581 mv->showMessage(
true );
584 void QgsOWSSourceSelect::mConnectionsComboBox_activated(
int )
590 void QgsOWSSourceSelect::mAddDefaultButton_clicked()
607 QMap<QString, QString> exampleServers;
608 exampleServers[QStringLiteral(
"DM Solutions GMap" )] = QStringLiteral(
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap" );
609 exampleServers[QStringLiteral(
"Lizardtech server" )] = QStringLiteral(
"http://wms.lizardtech.com/lizardtech/iserv/ows" );
615 QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
616 for ( ; i != exampleServers.constEnd(); ++i )
620 if ( !keys.contains( i.key() ) )
622 QString path = i.key();
623 settings.
setValue( path +
"/url", i.value() );
629 QMessageBox::information(
this, tr(
"Add WMS Servers" ),
"<p>" + tr(
"Several WMS servers have "
630 "been added to the server list. Note that if "
631 "you access the Internet via a web proxy, you will "
632 "need to set the proxy settings in the QGIS options dialog." ) +
"</p>" );
635 void QgsOWSSourceSelect::mLayerUpButton_clicked()
637 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
638 if ( selectionList.empty() )
642 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
643 if ( selectedIndex < 1 )
648 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
649 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
650 mLayerOrderTreeWidget->clearSelection();
651 selectedItem->setSelected(
true );
654 void QgsOWSSourceSelect::mLayerDownButton_clicked()
656 QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
657 if ( selectionList.empty() )
661 int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
662 if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
667 QTreeWidgetItem *selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
668 mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
669 mLayerOrderTreeWidget->clearSelection();
670 selectedItem->setSelected(
true );
675 return QList<SupportedFormat>();
680 return QStringList();
685 return QStringList();
690 return QStringList();
693 void QgsOWSSourceSelect::updateButtons()