18#include <QRegularExpressionValidator>
19#include <QRegularExpression>
34 : QDialog( parent, fl )
35 , mOriginalConnName( connName )
40 connect( btnConnect, &QPushButton::clicked,
this, &QgsNewOgrConnection::btnConnect_clicked );
45 buttonBox->button( QDialogButtonBox::Ok )->setDisabled(
true );
46 connect( txtName, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
47 connect( txtHost, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
48 connect( txtDatabase, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
49 connect( txtPort, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
55 for (
int i = 0; i < dbDrivers.count(); i++ )
57 const QString dbDriver = dbDrivers.at( i );
58 if ( !dbDriver.isEmpty() )
59 cmbDatabaseTypes->addItem( dbDriver.split(
',' ).at( 0 ) );
61 txtName->setEnabled(
true );
62 cmbDatabaseTypes->setEnabled(
true );
64 if ( !connType.isEmpty() )
66 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
69 if ( !connName.isEmpty() )
73 const QString key =
'/' + connType +
"/connections/" + connName;
74 txtHost->setText( settings.
value( key +
"/host" ).toString() );
75 txtDatabase->setText( settings.
value( key +
"/database" ).toString() );
76 const QString port = settings.
value( key +
"/port" ).toString();
77 txtPort->setText( port );
78 if ( settings.
value( key +
"/store_username" ).toString() == QLatin1String(
"true" ) )
80 mAuthSettingsDatabase->setUsername( settings.
value( key +
"/username" ).toString() );
81 mAuthSettingsDatabase->setStoreUsernameChecked(
true );
83 if ( settings.
value( key +
"/store_password" ).toString() == QLatin1String(
"true" ) )
85 mAuthSettingsDatabase->setPassword( settings.
value( key +
"/password" ).toString() );
86 mAuthSettingsDatabase->setStorePasswordChecked(
true );
88 mAuthSettingsDatabase->setConfigId( settings.
value( key +
"/configid" ).toString() );
89 txtName->setText( connName );
90 txtName->setEnabled(
false );
91 cmbDatabaseTypes->setEnabled(
false );
93 txtName->setValidator(
new QRegularExpressionValidator( QRegularExpression(
"[^\\/]+" ), txtName ) );
94 mAuthSettingsDatabase->setDataprovider( QStringLiteral(
"ogr" ) );
95 mAuthSettingsDatabase->showStoreCheckboxes(
true );
105 mAuthSettingsDatabase->configId(),
106 mAuthSettingsDatabase->username(),
107 mAuthSettingsDatabase->password(),
112 OGRSFDriverH pahDriver;
114 poDS = OGROpen( uri.toUtf8().constData(),
false, &pahDriver );
117 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
121 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection to %1 was successful." ).arg( uri ) );
122 OGRReleaseDataSource( poDS );
128 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/opening_data.html#creating-a-stored-connection" ) );
131void QgsNewOgrConnection::updateOkButtonState()
133 const bool enabled = !txtName->text().isEmpty();
134 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
142 QString baseKey =
'/' + cmbDatabaseTypes->currentText() +
"/connections/";
143 settings.
setValue( baseKey +
"selected", txtName->text() );
146 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
147 settings.
contains( baseKey + txtName->text() +
"/host" ) &&
148 QMessageBox::question(
this,
149 tr(
"Save Connection" ),
150 tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
151 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
157 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
159 settings.
remove( baseKey + mOriginalConnName );
162 baseKey += txtName->text();
163 settings.
setValue( baseKey +
"/host", txtHost->text() );
164 settings.
setValue( baseKey +
"/database", txtDatabase->text() );
165 settings.
setValue( baseKey +
"/port", txtPort->text() );
166 settings.
setValue( baseKey +
"/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
167 settings.
setValue( baseKey +
"/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
168 settings.
setValue( baseKey +
"/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ?
"true" :
"false" );
169 settings.
setValue( baseKey +
"/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ?
"true" :
"false" );
170 settings.
setValue( baseKey +
"/configid", mAuthSettingsDatabase->configId() );
175void QgsNewOgrConnection::btnConnect_clicked()
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
void testConnection()
Tests the connection using the parameters supplied.
void accept() override
Autoconnected SLOTS.
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
QgsNewOgrConnection(QWidget *parent=nullptr, const QString &connType=QString(), const QString &connName=QString(), Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString databaseDrivers() const
Returns a string containing the available database drivers.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.