17 #include <QMessageBox> 18 #include <QRegExpValidator> 28 #include <cpl_error.h> 32 : QDialog( parent, fl )
33 , mOriginalConnName( connName )
36 connect( btnConnect, &QPushButton::clicked,
this, &QgsNewOgrConnection::btnConnect_clicked );
42 restoreGeometry( settings.value( QStringLiteral(
"Windows/OGRDatabaseConnection/geometry" ) ).toByteArray() );
46 for (
int i = 0; i < dbDrivers.count(); i++ )
48 QString dbDrive = dbDrivers.at( i );
49 cmbDatabaseTypes->addItem( dbDrive.split(
',' ).at( 0 ) );
51 txtName->setEnabled(
true );
52 cmbDatabaseTypes->setEnabled(
true );
53 if ( !connName.isEmpty() )
57 QString key =
'/' + connType +
"/connections/" + connName;
58 txtHost->setText( settings.value( key +
"/host" ).toString() );
59 txtDatabase->setText( settings.value( key +
"/database" ).toString() );
60 QString port = settings.value( key +
"/port" ).toString();
61 txtPort->setText( port );
62 if ( settings.value( key +
"/store_username" ).toString() == QLatin1String(
"true" ) )
64 mAuthSettingsDatabase->setUsername( settings.value( key +
"/username" ).toString() );
65 mAuthSettingsDatabase->setStoreUsernameChecked(
true );
67 if ( settings.value( key +
"/store_password" ).toString() == QLatin1String(
"true" ) )
69 mAuthSettingsDatabase->setPassword( settings.value( key +
"/password" ).toString() );
70 mAuthSettingsDatabase->setStorePasswordChecked(
true );
72 mAuthSettingsDatabase->setConfigId( settings.value( key +
"/configid" ).toString() );
73 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
74 txtName->setText( connName );
75 txtName->setEnabled(
false );
76 cmbDatabaseTypes->setEnabled(
false );
78 txtName->setValidator(
new QRegExpValidator( QRegExp(
"[^\\/]+" ), txtName ) );
79 mAuthSettingsDatabase->setDataprovider( QStringLiteral(
"ogr" ) );
80 mAuthSettingsDatabase->showStoreCheckboxes(
true );
96 mAuthSettingsDatabase->configId(),
97 mAuthSettingsDatabase->username(),
98 mAuthSettingsDatabase->password(),
103 OGRSFDriverH pahDriver;
105 poDS = OGROpen( uri.toUtf8().constData(),
false, &pahDriver );
108 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
112 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection to %1 was successful." ).arg( uri ) );
113 OGRReleaseDataSource( poDS );
119 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/opening_data.html#creating-a-stored-connection" ) );
126 QString baseKey =
'/' + cmbDatabaseTypes->currentText() +
"/connections/";
127 settings.
setValue( baseKey +
"selected", txtName->text() );
130 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
131 settings.
contains( baseKey + txtName->text() +
"/host" ) &&
132 QMessageBox::question(
this,
133 tr(
"Save Connection" ),
134 tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
135 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
141 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
143 settings.
remove( baseKey + mOriginalConnName );
146 baseKey += txtName->text();
147 settings.
setValue( baseKey +
"/host", txtHost->text() );
148 settings.
setValue( baseKey +
"/database", txtDatabase->text() );
149 settings.
setValue( baseKey +
"/port", txtPort->text() );
150 settings.
setValue( baseKey +
"/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
151 settings.
setValue( baseKey +
"/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
152 settings.
setValue( baseKey +
"/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ?
"true" :
"false" );
153 settings.
setValue( baseKey +
"/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ?
"true" :
"false" );
154 settings.
setValue( baseKey +
"/configid", mAuthSettingsDatabase->configId() );
159 void QgsNewOgrConnection::btnConnect_clicked()
This class is a composition of two QSettings instances:
~QgsNewOgrConnection() override
#define Q_NOWARN_DEPRECATED_PUSH
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
void testConnection()
Tests the connection using the parameters supplied.
QgsNewOgrConnection(QWidget *parent=nullptr, const QString &connType=QString(), const QString &connName=QString(), Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
#define Q_NOWARN_DEPRECATED_POP
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void accept() override
Autoconnected SLOTS *.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.