17 #include <QMessageBox>
18 #include <QRegExpValidator>
25 #include "qgssettings.h"
29 #include <cpl_error.h>
33 : QDialog( parent, fl )
34 , mOriginalConnName( connName )
39 connect( btnConnect, &QPushButton::clicked,
this, &QgsNewOgrConnection::btnConnect_clicked );
44 buttonBox->button( QDialogButtonBox::Ok )->setDisabled(
true );
45 connect( txtName, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
46 connect( txtHost, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
47 connect( txtDatabase, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
48 connect( txtPort, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
54 for (
int i = 0; i < dbDrivers.count(); i++ )
56 QString dbDrive = dbDrivers.at( i );
57 cmbDatabaseTypes->addItem( dbDrive.split(
',' ).at( 0 ) );
59 txtName->setEnabled(
true );
60 cmbDatabaseTypes->setEnabled(
true );
61 if ( !connName.isEmpty() )
65 QString key =
'/' + connType +
"/connections/" + connName;
66 txtHost->setText( settings.value( key +
"/host" ).toString() );
67 txtDatabase->setText( settings.value( key +
"/database" ).toString() );
68 QString port = settings.value( key +
"/port" ).toString();
69 txtPort->setText( port );
70 if ( settings.value( key +
"/store_username" ).toString() == QLatin1String(
"true" ) )
72 mAuthSettingsDatabase->setUsername( settings.value( key +
"/username" ).toString() );
73 mAuthSettingsDatabase->setStoreUsernameChecked(
true );
75 if ( settings.value( key +
"/store_password" ).toString() == QLatin1String(
"true" ) )
77 mAuthSettingsDatabase->setPassword( settings.value( key +
"/password" ).toString() );
78 mAuthSettingsDatabase->setStorePasswordChecked(
true );
80 mAuthSettingsDatabase->setConfigId( settings.value( key +
"/configid" ).toString() );
81 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
82 txtName->setText( connName );
83 txtName->setEnabled(
false );
84 cmbDatabaseTypes->setEnabled(
false );
86 txtName->setValidator(
new QRegExpValidator( QRegExp(
"[^\\/]+" ), txtName ) );
87 mAuthSettingsDatabase->setDataprovider( QStringLiteral(
"ogr" ) );
88 mAuthSettingsDatabase->showStoreCheckboxes(
true );
98 mAuthSettingsDatabase->configId(),
99 mAuthSettingsDatabase->username(),
100 mAuthSettingsDatabase->password(),
105 OGRSFDriverH pahDriver;
107 poDS = OGROpen( uri.toUtf8().constData(),
false, &pahDriver );
110 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
114 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection to %1 was successful." ).arg( uri ) );
115 OGRReleaseDataSource( poDS );
121 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/opening_data.html#creating-a-stored-connection" ) );
124 void QgsNewOgrConnection::updateOkButtonState()
126 bool enabled = !txtName->text().isEmpty();
127 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
134 QgsSettings settings;
135 QString baseKey =
'/' + cmbDatabaseTypes->currentText() +
"/connections/";
136 settings.setValue( baseKey +
"selected", txtName->text() );
139 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
140 settings.contains( baseKey + txtName->text() +
"/host" ) &&
141 QMessageBox::question(
this,
142 tr(
"Save Connection" ),
143 tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
144 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
150 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
152 settings.remove( baseKey + mOriginalConnName );
155 baseKey += txtName->text();
156 settings.setValue( baseKey +
"/host", txtHost->text() );
157 settings.setValue( baseKey +
"/database", txtDatabase->text() );
158 settings.setValue( baseKey +
"/port", txtPort->text() );
159 settings.setValue( baseKey +
"/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
160 settings.setValue( baseKey +
"/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
161 settings.setValue( baseKey +
"/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ?
"true" :
"false" );
162 settings.setValue( baseKey +
"/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ?
"true" :
"false" );
163 settings.setValue( baseKey +
"/configid", mAuthSettingsDatabase->configId() );
168 void 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.
#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.