Skip to content

Commit ffaafa6

Browse files
committed
Rework QgsGeoNodeNewConnection to inherit from QgsNewHttpConnection
Avoids a lot of duplicate code. Note that while this refactoring was done to allow WFS and WMS settings to be simultaneously visible, I've removed the settings from the GeoNode connection for now. Looking into this they were being stored, but not used at all when loading the layers from the GeoNode instance.
1 parent 90237fa commit ffaafa6

6 files changed

+125
-600
lines changed

python/gui/qgsnewhttpconnection.sip

+31
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,42 @@ class QgsNewHttpConnection : QDialog
5555
and appearance.
5656
%End
5757

58+
QString name() const;
59+
%Docstring
60+
Returns the current connection name.
61+
.. versionadded:: 3.0
62+
:rtype: str
63+
%End
64+
65+
QString url() const;
66+
%Docstring
67+
Returns the current connection url.
68+
.. versionadded:: 3.0
69+
:rtype: str
70+
%End
71+
5872
public slots:
5973

6074
virtual void accept();
6175

6276

77+
protected:
78+
79+
virtual bool validate();
80+
%Docstring
81+
Returns true if dialog settings are valid, or false if current
82+
settings are not valid and the dialog should not be acceptable.
83+
.. versionadded:: 3.0
84+
:rtype: bool
85+
%End
86+
87+
QPushButton *testConnectButton();
88+
%Docstring
89+
Returns the "test connection" button.
90+
.. versionadded:: 3.0
91+
:rtype: QPushButton
92+
%End
93+
6394
};
6495

6596
QFlags<QgsNewHttpConnection::ConnectionType> operator|(QgsNewHttpConnection::ConnectionType f1, QFlags<QgsNewHttpConnection::ConnectionType> f2);

src/app/geocms/geonode/qgsgeonodenewconnection.cpp

+19-227
Original file line numberDiff line numberDiff line change
@@ -16,243 +16,22 @@
1616
***************************************************************************/
1717

1818
#include <QMessageBox>
19-
#include <QUrl>
20-
#include "qgslogger.h"
21-
2219
#include "qgsgeonodenewconnection.h"
23-
#include "qgsauthmanager.h"
24-
#include "qgsdatasourceuri.h"
2520
#include "qgsgeonodeconnection.h"
26-
#include "qgssettings.h"
2721
#include "qgsgeonoderequest.h"
2822

2923
QgsGeoNodeNewConnection::QgsGeoNodeNewConnection( QWidget *parent, const QString &connName, Qt::WindowFlags fl )
30-
: QDialog( parent, fl )
31-
, mOriginalConnName( connName )
32-
, mAuthConfigSelect( nullptr )
24+
: QgsNewHttpConnection( parent, 0, QgsGeoNodeConnectionUtils::pathGeoNodeConnection(), connName, QgsNewHttpConnection::FlagShowTestConnection, fl )
3325
{
34-
setupUi( this );
35-
36-
mBaseKey = QgsGeoNodeConnectionUtils::pathGeoNodeConnection();
37-
mCredentialsBaseKey = QgsGeoNodeConnectionUtils::pathGeoNodeConnection();
38-
39-
mAuthConfigSelect = new QgsAuthConfigSelect( this );
40-
tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
41-
42-
cmbDpiMode->clear();
43-
cmbDpiMode->addItem( tr( "all" ) );
44-
cmbDpiMode->addItem( tr( "off" ) );
45-
cmbDpiMode->addItem( tr( "QGIS" ) );
46-
cmbDpiMode->addItem( tr( "UMN" ) );
47-
cmbDpiMode->addItem( tr( "GeoServer" ) );
48-
49-
cmbVersion->clear();
50-
cmbVersion->addItem( tr( "Auto-detect" ) );
51-
cmbVersion->addItem( tr( "1.0" ) );
52-
cmbVersion->addItem( tr( "1.1" ) );
53-
cmbVersion->addItem( tr( "2.0" ) );
54-
55-
if ( !connName.isEmpty() )
56-
{
57-
// populate the dialog with the information stored for the connection
58-
// populate the fields with the stored setting parameters
59-
QgsSettings settings;
60-
61-
QString key = mBaseKey + '/' + connName;
62-
QString credentialsKey = mCredentialsBaseKey + '/' + connName;
63-
txtName->setText( connName );
64-
txtUrl->setText( settings.value( key + "/url", "", QgsSettings::Providers ).toString() );
65-
66-
cbxIgnoreGetMapURI->setChecked( settings.value( key + "/wms/ignoreGetMapURI", false, QgsSettings::Providers ).toBool() );
67-
cbxWfsIgnoreAxisOrientation->setChecked( settings.value( key + "/wfs/ignoreAxisOrientation", false, QgsSettings::Providers ).toBool() );
68-
cbxWmsIgnoreAxisOrientation->setChecked( settings.value( key + "/wms/ignoreAxisOrientation", false, QgsSettings::Providers ).toBool() );
69-
cbxWfsInvertAxisOrientation->setChecked( settings.value( key + "/wfs/invertAxisOrientation", false, QgsSettings::Providers ).toBool() );
70-
cbxWmsInvertAxisOrientation->setChecked( settings.value( key + "/wms/invertAxisOrientation", false, QgsSettings::Providers ).toBool() );
71-
cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/wms/ignoreGetFeatureInfoURI", false, QgsSettings::Providers ).toBool() );
72-
cbxSmoothPixmapTransform->setChecked( settings.value( key + "/wms/smoothPixmapTransform", false, QgsSettings::Providers ).toBool() );
73-
74-
int dpiIdx;
75-
switch ( settings.value( key + "/dpiMode", 7, QgsSettings::Providers ).toInt() )
76-
{
77-
case 0: // off
78-
dpiIdx = 1;
79-
break;
80-
case 1: // QGIS
81-
dpiIdx = 2;
82-
break;
83-
case 2: // UMN
84-
dpiIdx = 3;
85-
break;
86-
case 4: // GeoServer
87-
dpiIdx = 4;
88-
break;
89-
default: // other => all
90-
dpiIdx = 0;
91-
break;
92-
}
93-
cmbDpiMode->setCurrentIndex( dpiIdx );
94-
95-
QString version = settings.value( key + "/version", QLatin1String( "1.0.0" ), QgsSettings::Providers ).toString();
96-
int versionIdx = 0; // AUTO
97-
if ( version == QLatin1String( "1.0.0" ) )
98-
versionIdx = 1;
99-
else if ( version == QLatin1String( "1.1.0" ) )
100-
versionIdx = 2;
101-
else if ( version == QLatin1String( "2.0.0" ) )
102-
versionIdx = 3;
103-
cmbVersion->setCurrentIndex( versionIdx );
104-
105-
txtReferer->setText( settings.value( key + "/referer", "", QgsSettings::Providers ).toString() );
106-
txtMaxNumFeatures->setText( settings.value( key + "/maxnumfeatures", QgsSettings::Providers ).toString() );
107-
108-
txtUserName->setText( settings.value( credentialsKey + "/username", "", QgsSettings::Providers ).toString() );
109-
txtPassword->setText( settings.value( credentialsKey + "/password", "", QgsSettings::Providers ).toString() );
110-
111-
QString authcfg = settings.value( credentialsKey + "/authcfg", "", QgsSettings::Providers ).toString();
112-
mAuthConfigSelect->setConfigId( authcfg );
113-
if ( !authcfg.isEmpty() )
114-
{
115-
tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
116-
}
117-
}
26+
setWindowTitle( tr( "Create a New GeoNode Connection" ) );
11827

119-
// Adjust height
120-
int w = width();
121-
adjustSize();
122-
resize( w, height() );
123-
124-
buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
125-
connect( txtName, &QLineEdit::textChanged, this, &QgsGeoNodeNewConnection::okButtonBehavior );
126-
connect( txtUrl, &QLineEdit::textChanged, this, &QgsGeoNodeNewConnection::okButtonBehavior );
127-
connect( btnConnect, &QPushButton::clicked, this, &QgsGeoNodeNewConnection::testConnection );
128-
}
129-
130-
QString QgsGeoNodeNewConnection::name() const
131-
{
132-
return txtName->text();
133-
}
134-
135-
void QgsGeoNodeNewConnection::accept()
136-
{
137-
QgsSettings settings;
138-
QString key = mBaseKey + '/' + txtName->text();
139-
QString credentialsKey = mCredentialsBaseKey + '/' + txtName->text();
140-
141-
// warn if entry was renamed to an existing connection
142-
if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
143-
settings.contains( key + "/url", QgsSettings::Providers ) &&
144-
QMessageBox::question( this,
145-
tr( "Save connection" ),
146-
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
147-
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
148-
{
149-
return;
150-
}
151-
152-
if ( !txtPassword->text().isEmpty() &&
153-
QMessageBox::question( this,
154-
tr( "Saving passwords" ),
155-
trUtf8( "WARNING: You have entered a password. It will be stored in unsecured plain text in your project files and your home directory (Unix-like OS) or user profile (Windows). If you want to avoid this, press Cancel and either:\n\na) Don't provide a password in the connection settings — it will be requested interactively when needed;\nb) Use the Configuration tab to add your credentials in an HTTP Basic Authentication method and store them in an encrypted database." ),
156-
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
157-
{
158-
return;
159-
}
160-
161-
// on rename delete original entry first
162-
if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
163-
{
164-
// Manually add Section here
165-
settings.remove( "providers/" + mBaseKey + '/' + mOriginalConnName );
166-
settings.remove( "providers/qgis//" + mCredentialsBaseKey + '/' + mOriginalConnName );
167-
settings.sync();
168-
}
169-
170-
if ( !txtUrl->text().contains( "://" ) &&
171-
QMessageBox::information(
172-
this,
173-
tr( "Invalid URL" ),
174-
tr( "Your URL doesn't contains protocol (e.g. http or https). Please add the protocol." ) ) == QMessageBox::Ok )
175-
{
176-
return;
177-
}
178-
QUrl url( txtUrl->text() );
179-
180-
settings.setValue( key + "/url", url.toString(), QgsSettings::Providers );
181-
182-
settings.setValue( key + "/wfs/ignoreAxisOrientation", cbxWfsIgnoreAxisOrientation->isChecked(), QgsSettings::Providers );
183-
settings.setValue( key + "/wms/ignoreAxisOrientation", cbxWmsIgnoreAxisOrientation->isChecked(), QgsSettings::Providers );
184-
settings.setValue( key + "/wfs/invertAxisOrientation", cbxWfsInvertAxisOrientation->isChecked(), QgsSettings::Providers );
185-
settings.setValue( key + "/wms/invertAxisOrientation", cbxWmsInvertAxisOrientation->isChecked(), QgsSettings::Providers );
186-
187-
settings.setValue( key + "/wms/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked(), QgsSettings::Providers );
188-
settings.setValue( key + "/wms/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked(), QgsSettings::Providers );
189-
settings.setValue( key + "/wms/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked(), QgsSettings::Providers );
190-
191-
int dpiMode = 0;
192-
switch ( cmbDpiMode->currentIndex() )
193-
{
194-
case 0: // all => QGIS|UMN|GeoServer
195-
dpiMode = 7;
196-
break;
197-
case 1: // off
198-
dpiMode = 0;
199-
break;
200-
case 2: // QGIS
201-
dpiMode = 1;
202-
break;
203-
case 3: // UMN
204-
dpiMode = 2;
205-
break;
206-
case 4: // GeoServer
207-
dpiMode = 4;
208-
break;
209-
}
210-
211-
settings.setValue( key + "/wms/dpiMode", dpiMode, QgsSettings::Providers );
212-
settings.setValue( key + "/wms/referer", txtReferer->text(), QgsSettings::Providers );
213-
214-
QString version = QStringLiteral( "auto" );
215-
switch ( cmbVersion->currentIndex() )
216-
{
217-
case 0:
218-
version = QStringLiteral( "auto" );
219-
break;
220-
case 1:
221-
version = QStringLiteral( "1.0.0" );
222-
break;
223-
case 2:
224-
version = QStringLiteral( "1.1.0" );
225-
break;
226-
case 3:
227-
version = QStringLiteral( "2.0.0" );
228-
break;
229-
}
230-
231-
settings.setValue( key + "/wfs/version", version, QgsSettings::Providers );
232-
settings.setValue( key + "/wfs/maxnumfeatures", txtMaxNumFeatures->text(), QgsSettings::Providers );
233-
234-
settings.setValue( credentialsKey + "/username", txtUserName->text(), QgsSettings::Providers );
235-
settings.setValue( credentialsKey + "/password", txtPassword->text(), QgsSettings::Providers );
236-
237-
settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId(), QgsSettings::Providers );
238-
239-
settings.setValue( mBaseKey + "/selected", txtName->text(), QgsSettings::Providers );
240-
241-
QDialog::accept();
242-
}
243-
244-
void QgsGeoNodeNewConnection::okButtonBehavior( const QString &text )
245-
{
246-
Q_UNUSED( text );
247-
buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
248-
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !txtName->text().isEmpty() && !txtUrl->text().isEmpty() );
28+
connect( testConnectButton(), &QPushButton::clicked, this, &QgsGeoNodeNewConnection::testConnection );
24929
}
25030

25131
void QgsGeoNodeNewConnection::testConnection()
25232
{
25333
QApplication::setOverrideCursor( Qt::BusyCursor );
254-
QString url = txtUrl->text();
255-
QgsGeoNodeRequest geonodeRequest( url, true );
34+
QgsGeoNodeRequest geonodeRequest( url(), true );
25635

25736
QList<QgsGeoNodeRequest::ServiceLayerDetail> layers = geonodeRequest.fetchLayersBlocking();
25837
QApplication::restoreOverrideCursor();
@@ -261,12 +40,25 @@ void QgsGeoNodeNewConnection::testConnection()
26140
{
26241
QMessageBox::information( this,
26342
tr( "Test connection" ),
264-
tr( "\nConnection to %1 was successful, \n\n%1 is a valid geonode instance.\n\n" ).arg( txtUrl->text() ) );
43+
tr( "\nConnection to %1 was successful, \n\n%1 is a valid geonode instance.\n\n" ).arg( url() ) );
26544
}
26645
else
26746
{
26847
QMessageBox::information( this,
26948
tr( "Test connection" ),
270-
tr( "\nConnection failed, \n\nplease check whether %1 is a valid geonode instance.\n\n" ).arg( txtUrl->text() ) );
49+
tr( "\nConnection failed, \n\nplease check whether %1 is a valid geonode instance.\n\n" ).arg( url() ) );
50+
}
51+
}
52+
53+
bool QgsGeoNodeNewConnection::validate()
54+
{
55+
if ( !url().contains( "://" ) )
56+
{
57+
QMessageBox::warning(
58+
this,
59+
tr( "Invalid URL" ),
60+
tr( "Your URL doesn't contain a protocol (e.g. http or https). Please add the protocol." ) );
61+
return false;
27162
}
63+
return QgsNewHttpConnection::validate();
27264
}

src/app/geocms/geonode/qgsgeonodenewconnection.h

+7-14
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,25 @@
1818
#ifndef QGSGEONODENEWCONNECTION_H
1919
#define QGSGEONODENEWCONNECTION_H
2020

21-
#include "ui_qgsnewgeonodeconnectionbase.h"
22-
#include "qgsguiutils.h"
23-
#include "qgsauthconfigselect.h"
21+
#include "qgsnewhttpconnection.h"
2422

25-
class QgsGeoNodeNewConnection : public QDialog, private Ui::QgsNewGeoNodeConnectionBase
23+
class QgsGeoNodeNewConnection : public QgsNewHttpConnection
2624
{
2725
Q_OBJECT
2826

2927
public:
3028
//! Constructor
3129
QgsGeoNodeNewConnection( QWidget *parent = nullptr, const QString &connName = QString::null, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
3230

33-
//! Returns the new connection name
34-
QString name() const;
35-
3631
public slots:
37-
void accept() override;
38-
void okButtonBehavior( const QString & );
32+
3933
//! Test the connection using the parameters supplied
4034
void testConnection();
4135

42-
private:
43-
QString mBaseKey;
44-
QString mCredentialsBaseKey;
45-
QString mOriginalConnName; //store initial name to delete entry in case of rename
46-
QgsAuthConfigSelect *mAuthConfigSelect = nullptr;
36+
protected:
37+
38+
bool validate() override;
39+
4740
};
4841

4942
#endif //QGSGEONODENEWCONNECTION_H

0 commit comments

Comments
 (0)