Skip to content

Commit

Permalink
[xyz] In data source manager, allow users to directly add an XYZ
Browse files Browse the repository at this point in the history
tile layer without having to create a connection first

This permits users to easily add ad-hoc XYZ layers to their map
without polluting their lists of saved connections with entries
for these.
  • Loading branch information
nyalldawson committed Jan 27, 2023
1 parent 94cf996 commit e55bbca
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/providers/wms/qgsxyzconnectiondialog.h
Expand Up @@ -35,6 +35,8 @@ class QgsXyzConnectionDialog : public QDialog, public Ui::QgsXyzConnectionDialog

QgsXyzConnection connection() const;

QgsXyzSourceWidget *sourceWidget() { return mSourceWidget; }

void accept() override;

private slots:
Expand Down
59 changes: 52 additions & 7 deletions src/providers/wms/qgsxyzsourceselect.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsxyzconnection.h"
#include "qgsxyzconnectiondialog.h"
#include "qgsowsconnection.h"
#include "qgsxyzsourcewidget.h"

#include <QFileDialog>
#include <QMessageBox>
Expand All @@ -35,6 +36,23 @@ QgsXyzSourceSelect::QgsXyzSourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs
setWindowTitle( tr( "Add XYZ Layer" ) );
mConnectionsGroupBox->setTitle( tr( "XYZ Connections" ) );

mSourceWidget = new QgsXyzSourceWidget();
QHBoxLayout *hlayout = new QHBoxLayout();
hlayout->setContentsMargins( 0, 0, 0, 0 );
hlayout->addWidget( mSourceWidget );
mSourceContainerWidget->setLayout( hlayout );

connect( mSourceWidget, &QgsProviderSourceWidget::validChanged, this, &QgsXyzSourceSelect::enableButtons );
connect( mSourceWidget, &QgsProviderSourceWidget::changed, this, [this]
{
if ( mBlockChanges )
return;

mBlockChanges++;
cmbConnections->setCurrentIndex( cmbConnections->findData( QStringLiteral( "~~custom~~" ) ) );
mBlockChanges--;
} );

QgsGui::enableAutoGeometryRestore( this );

connect( btnNew, &QPushButton::clicked, this, &QgsXyzSourceSelect::btnNew_clicked );
Expand All @@ -50,10 +68,19 @@ QgsXyzSourceSelect::QgsXyzSourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs

void QgsXyzSourceSelect::btnNew_clicked()
{
const bool isCustom = cmbConnections->currentData().toString() == QLatin1String( "~~custom~~" );

QgsXyzConnectionDialog nc( this );
if ( isCustom )
{
// when creating a new connection, default to the current connection parameters
nc.sourceWidget()->setSourceUri( mSourceWidget->sourceUri() );
}
if ( nc.exec() )
{
QgsXyzConnectionUtils::addConnection( nc.connection() );

QgsXyzConnectionSettings::sTreeXyzConnections->setSelectedItem( nc.connection().name );
populateConnectionList();
emit connectionsChanged();
}
Expand Down Expand Up @@ -106,20 +133,19 @@ void QgsXyzSourceSelect::btnLoad_clicked()

void QgsXyzSourceSelect::addButtonClicked()
{
emit addRasterLayer( QgsXyzConnectionUtils::connection( cmbConnections->currentText() ).encodedUri(), cmbConnections->currentText(), QStringLiteral( "wms" ) );
const bool isCustom = cmbConnections->currentData().toString() == QLatin1String( "~~custom~~" );
emit addRasterLayer( mSourceWidget->sourceUri(), isCustom ? tr( "XYZ Layer" ) : cmbConnections->currentText(), QStringLiteral( "wms" ) );
}

void QgsXyzSourceSelect::populateConnectionList()
{
cmbConnections->blockSignals( true );
cmbConnections->clear();
cmbConnections->addItem( tr( "Custom" ), QStringLiteral( "~~custom~~" ) );
cmbConnections->addItems( QgsXyzConnectionUtils::connectionList() );
cmbConnections->blockSignals( false );

btnEdit->setDisabled( cmbConnections->count() == 0 );
btnDelete->setDisabled( cmbConnections->count() == 0 );
btnSave->setDisabled( cmbConnections->count() == 0 );
cmbConnections->setDisabled( cmbConnections->count() == 0 );
btnSave->setDisabled( cmbConnections->count() == 1 );

setConnectionListPosition();
}
Expand All @@ -138,13 +164,32 @@ void QgsXyzSourceSelect::setConnectionListPosition()
cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
}

emit enableButtons( !cmbConnections->currentText().isEmpty() );
const bool isCustom = cmbConnections->currentData().toString() == QLatin1String( "~~custom~~" );
btnEdit->setDisabled( isCustom );
btnDelete->setDisabled( isCustom );
}

void QgsXyzSourceSelect::cmbConnections_currentTextChanged( const QString &text )
{
QgsXyzConnectionSettings::sTreeXyzConnections->setSelectedItem( text );
emit enableButtons( !text.isEmpty() );

const bool isCustom = cmbConnections->currentData().toString() == QLatin1String( "~~custom~~" );
btnEdit->setDisabled( isCustom );
btnDelete->setDisabled( isCustom );

if ( !mBlockChanges )
{
mBlockChanges++;
if ( isCustom )
{
mSourceWidget->setSourceUri( QString() );
}
else
{
mSourceWidget->setSourceUri( QgsXyzConnectionUtils::connection( cmbConnections->currentText() ).encodedUri() );
}
mBlockChanges--;
}
}

void QgsXyzSourceSelect::showHelp()
Expand Down
5 changes: 5 additions & 0 deletions src/providers/wms/qgsxyzsourceselect.h
Expand Up @@ -21,6 +21,8 @@
#include "qgsabstractdatasourcewidget.h"
#include "ui_qgstilesourceselectbase.h"

class QgsXyzSourceWidget;

/*!
* \brief Dialog to create connections to XYZ servers.
*
Expand Down Expand Up @@ -60,6 +62,9 @@ class QgsXyzSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsTi
void populateConnectionList();
void setConnectionListPosition();
void showHelp();

QgsXyzSourceWidget *mSourceWidget = nullptr;
int mBlockChanges = 0;
};

#endif // QGSXYZSOURCESELECT_H
2 changes: 0 additions & 2 deletions src/providers/wms/qgsxyzsourcewidget.cpp
Expand Up @@ -19,8 +19,6 @@
#include "qgswmssourceselect.h"
#include "qgsproviderregistry.h"

#include "qgswmsprovider.h"

QgsXyzSourceWidget::QgsXyzSourceWidget( QWidget *parent )
: QgsProviderSourceWidget( parent )
{
Expand Down
28 changes: 20 additions & 8 deletions src/ui/qgstilesourceselectbase.ui
Expand Up @@ -7,10 +7,20 @@
<x>0</x>
<y>0</y>
<width>558</width>
<height>166</height>
<height>259</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Help</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QGroupBox" name="mConnectionsGroupBox">
<property name="title">
Expand Down Expand Up @@ -95,14 +105,16 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Help</set>
<item row="1" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Connection Details</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="mSourceContainerWidget" native="true"/>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
Expand Down

0 comments on commit e55bbca

Please sign in to comment.