Skip to content
Permalink
Browse files

Reuse QgsXyzSourceWidget in QgsXyzConnectionDialog

  • Loading branch information
nyalldawson committed Dec 25, 2020
1 parent 16f9083 commit 4610d9579aa1682524095e198061aee81138bfa4
@@ -16,6 +16,7 @@
#include "qgsxyzconnectiondialog.h"
#include "qgsxyzconnection.h"
#include "qgsgui.h"
#include "qgsxyzsourcewidget.h"

#include <QMessageBox>

@@ -25,69 +26,57 @@ QgsXyzConnectionDialog::QgsXyzConnectionDialog( QWidget *parent )
setupUi( this );
QgsGui::enableAutoGeometryRestore( this );

// Behavior for min and max zoom checkbox
connect( mCheckBoxZMin, &QCheckBox::toggled, mSpinZMin, &QSpinBox::setEnabled );
connect( mCheckBoxZMax, &QCheckBox::toggled, mSpinZMax, &QSpinBox::setEnabled );
mSpinZMax->setClearValue( 18 );
mSourceWidget = new QgsXyzSourceWidget();
QHBoxLayout *hlayout = new QHBoxLayout();
hlayout->addWidget( mSourceWidget );
mConnectionGroupBox->setLayout( hlayout );

buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
connect( mEditName, &QLineEdit::textChanged, this, &QgsXyzConnectionDialog::updateOkButtonState );
connect( mEditUrl, &QLineEdit::textChanged, this, &QgsXyzConnectionDialog::updateOkButtonState );
connect( mSourceWidget, &QgsXyzSourceWidget::validChanged, this, &QgsXyzConnectionDialog::updateOkButtonState );
}

void QgsXyzConnectionDialog::setConnection( const QgsXyzConnection &conn )
{
mEditName->setText( conn.name );
mEditUrl->setText( conn.url );
mCheckBoxZMin->setChecked( conn.zMin != -1 );
mSpinZMin->setValue( conn.zMin != -1 ? conn.zMin : 0 );
mCheckBoxZMax->setChecked( conn.zMax != -1 );
mSpinZMax->setValue( conn.zMax != -1 ? conn.zMax : 18 );
mAuthSettings->setUsername( conn.username );
mAuthSettings->setPassword( conn.password );
mEditReferer->setText( conn.referer );
int index = 0; // default is "unknown"
if ( conn.tilePixelRatio == 2. )
index = 2; // high-res
else if ( conn.tilePixelRatio == 1. )
index = 1; // normal-res
mComboTileResolution->setCurrentIndex( index );
mAuthSettings->setConfigId( conn.authCfg );
mSourceWidget->setUrl( conn.url );
mSourceWidget->setZMin( conn.zMin );
mSourceWidget->setZMax( conn.zMax );
mSourceWidget->setUsername( conn.username );
mSourceWidget->setPassword( conn.password );
mSourceWidget->setReferer( conn.referer );
mSourceWidget->setTilePixelRatio( conn.tilePixelRatio );
mSourceWidget->setAuthCfg( conn.authCfg );
}

QgsXyzConnection QgsXyzConnectionDialog::connection() const
{
QgsXyzConnection conn;
conn.name = mEditName->text();
conn.url = mEditUrl->text();
if ( mCheckBoxZMin->isChecked() )
conn.zMin = mSpinZMin->value();
if ( mCheckBoxZMax->isChecked() )
conn.zMax = mSpinZMax->value();
conn.username = mAuthSettings->username();
conn.password = mAuthSettings->password();
conn.referer = mEditReferer->text();
if ( mComboTileResolution->currentIndex() == 1 )
conn.tilePixelRatio = 1.; // normal-res
else if ( mComboTileResolution->currentIndex() == 2 )
conn.tilePixelRatio = 2.; // high-res
else
conn.tilePixelRatio = 0; // unknown
conn.authCfg = mAuthSettings->configId( );
conn.url = mSourceWidget->url();
conn.zMin = mSourceWidget->zMin();
conn.zMax = mSourceWidget->zMax();
conn.username = mSourceWidget->username();
conn.password = mSourceWidget->password();
conn.referer = mSourceWidget->referer();
conn.tilePixelRatio = mSourceWidget->tilePixelRatio();
conn.authCfg = mSourceWidget->authcfg( );
return conn;
}

void QgsXyzConnectionDialog::updateOkButtonState()
{
bool enabled = !mEditName->text().isEmpty() && !mEditUrl->text().isEmpty();
bool enabled = !mEditName->text().isEmpty() && !mSourceWidget->url().isEmpty();
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
}

void QgsXyzConnectionDialog::accept()
{
if ( mCheckBoxZMin->isChecked() && mCheckBoxZMax->isChecked() && mSpinZMax->value() < mSpinZMin->value() )
if ( mSourceWidget->zMin() != -1
&& mSourceWidget->zMax() != -1
&& mSourceWidget->zMax() < mSourceWidget->zMin() )
{
QMessageBox::warning( this, tr( "Connection Properties" ), tr( "The maximum zoom level (%1) cannot be lower than the minimum zoom level (%2)." ).arg( mSpinZMax->value() ).arg( mSpinZMin->value() ) );
QMessageBox::warning( this, tr( "Connection Properties" ), tr( "The maximum zoom level (%1) cannot be lower than the minimum zoom level (%2)." ).arg( mSourceWidget->zMax() ).arg( mSourceWidget->zMin() ) );
return;
}
QDialog::accept();
@@ -23,6 +23,7 @@

struct QgsXyzConnection;

class QgsXyzSourceWidget;

class QgsXyzConnectionDialog : public QDialog, public Ui::QgsXyzConnectionDialog
{
@@ -43,6 +44,8 @@ class QgsXyzConnectionDialog : public QDialog, public Ui::QgsXyzConnectionDialog
private:
QString mBaseKey;
QString mCredentialsBaseKey;

QgsXyzSourceWidget *mSourceWidget = nullptr;
};

#endif // QGSXYZCONNECTIONDIALOG_H
@@ -24,6 +24,7 @@

#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>

QgsXyzSourceSelect::QgsXyzSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
: QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
@@ -99,6 +99,98 @@ QString QgsXyzSourceWidget::sourceUri() const
return QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "wms" ), parts );
}

void QgsXyzSourceWidget::setUrl( const QString &url )
{
mEditUrl->setText( url );
}

QString QgsXyzSourceWidget::url() const
{
return mEditUrl->text();
}

void QgsXyzSourceWidget::setZMin( int zMin )
{
mCheckBoxZMin->setChecked( zMin != -1 );
mSpinZMin->setValue( zMin != -1 ? zMin : 0 );
}

int QgsXyzSourceWidget::zMin() const
{
return mCheckBoxZMin->isChecked() ? mSpinZMin->value() : -1;
}

void QgsXyzSourceWidget::setZMax( int zMax )
{
mCheckBoxZMax->setChecked( zMax != -1 );
mSpinZMax->setValue( zMax != -1 ? zMax : 0 );
}

int QgsXyzSourceWidget::zMax() const
{
return mCheckBoxZMax->isChecked() ? mSpinZMax->value() : -1;
}

void QgsXyzSourceWidget::setUsername( const QString &username )
{
mAuthSettings->setUsername( username );
}

void QgsXyzSourceWidget::setPassword( const QString &password )
{
mAuthSettings->setPassword( password );
}

void QgsXyzSourceWidget::setAuthCfg( const QString &id )
{
mAuthSettings->setConfigId( id );
}

QString QgsXyzSourceWidget::username() const
{
return mAuthSettings->username();
}

QString QgsXyzSourceWidget::password() const
{
return mAuthSettings->password();
}

QString QgsXyzSourceWidget::authcfg() const
{
return mAuthSettings->configId();
}

void QgsXyzSourceWidget::setReferer( const QString &referer )
{
mEditReferer->setText( referer );
}

QString QgsXyzSourceWidget::referer() const
{
return mEditReferer->text();
}

void QgsXyzSourceWidget::setTilePixelRatio( int ratio )
{
int index = 0; // default is "unknown"
if ( ratio == 2. )
index = 2; // high-res
else if ( ratio == 1. )
index = 1; // normal-res
mComboTileResolution->setCurrentIndex( ratio );
}

int QgsXyzSourceWidget::tilePixelRatio() const
{
if ( mComboTileResolution->currentIndex() == 1 )
return 1.; // normal-res
else if ( mComboTileResolution->currentIndex() == 2 )
return 2.; // high-res
else
return 0; // unknown
}

void QgsXyzSourceWidget::validate()
{
const bool valid = !mEditUrl->text().isEmpty();
@@ -31,6 +31,29 @@ class QgsXyzSourceWidget : public QgsProviderSourceWidget, private Ui::QgsXyzSou
void setSourceUri( const QString &uri ) override;
QString sourceUri() const override;

void setUrl( const QString &url );
QString url() const;

void setZMin( int zMin );
int zMin() const;

void setZMax( int zMax );
int zMax() const;

void setUsername( const QString &username );
void setPassword( const QString &password );
void setAuthCfg( const QString &id );

QString username() const;
QString password() const;
QString authcfg() const;

void setReferer( const QString &referer );
QString referer() const;

void setTilePixelRatio( int ratio );
int tilePixelRatio() const;

private slots:

void validate();

0 comments on commit 4610d95

Please sign in to comment.
You can’t perform that action at this time.