QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsnewogrconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnewogrconnection.cpp - description
3 -------------------
4 begin : Mon Jan 2 2009
5 copyright : (C) 2009 by Godofredo Contreras Nava
6 email : frdcn at hotmail.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#include <QMessageBox>
18#include <QRegularExpressionValidator>
19#include <QRegularExpression>
20
21#include "qgsnewogrconnection.h"
22#include "qgslogger.h"
23#include "qgsproviderregistry.h"
24#include "qgsgdalguiutils.h"
25#include "qgsapplication.h"
26#include "qgssettings.h"
27#include "qgsgui.h"
28
29#include <ogr_api.h>
30#include <cpl_error.h>
31#include "qgshelp.h"
32
33QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connType, const QString &connName, Qt::WindowFlags fl )
34 : QDialog( parent, fl )
35 , mOriginalConnName( connName )
36{
37 setupUi( this );
39
40 connect( btnConnect, &QPushButton::clicked, this, &QgsNewOgrConnection::btnConnect_clicked );
42 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewOgrConnection::showHelp );
44
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 );
50
51 const QgsSettings settings;
52
53 //add database drivers
54 const QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
55 for ( int i = 0; i < dbDrivers.count(); i++ )
56 {
57 const QString dbDriver = dbDrivers.at( i );
58 if ( !dbDriver.isEmpty() )
59 cmbDatabaseTypes->addItem( dbDriver.split( ',' ).at( 0 ) );
60 }
61 txtName->setEnabled( true );
62 cmbDatabaseTypes->setEnabled( true );
63
64 if ( !connType.isEmpty() )
65 {
66 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
67 }
68
69 if ( !connName.isEmpty() )
70 {
71 // populate the dialog with the information stored for the connection
72 // populate the fields with the stored setting parameters
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" ) )
79 {
80 mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
81 mAuthSettingsDatabase->setStoreUsernameChecked( true );
82 }
83 if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
84 {
85 mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
86 mAuthSettingsDatabase->setStorePasswordChecked( true );
87 }
88 mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
89 txtName->setText( connName );
90 txtName->setEnabled( false );
91 cmbDatabaseTypes->setEnabled( false );
92 }
93 txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );
94 mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
95 mAuthSettingsDatabase->showStoreCheckboxes( true );
96}
97
99{
100 QString uri = QgsGdalGuiUtils::createDatabaseURI( cmbDatabaseTypes->currentText(),
101 txtHost->text(),
102 txtDatabase->text(),
103 txtPort->text(),
104 mAuthSettingsDatabase->configId(),
105 mAuthSettingsDatabase->username(),
106 mAuthSettingsDatabase->password(),
107 true );
108 QgsDebugMsgLevel( "Connecting using uri = " + uri, 2 );
109 OGRRegisterAll();
110 OGRDataSourceH poDS;
111 OGRSFDriverH pahDriver;
112 CPLErrorReset();
113 poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
114 if ( !poDS )
115 {
116 QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
117 }
118 else
119 {
120 QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
121 OGRReleaseDataSource( poDS );
122 }
123}
124
126{
127 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
128}
129
130void QgsNewOgrConnection::updateOkButtonState()
131{
132 const bool enabled = !txtName->text().isEmpty();
133 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
134}
135
136
139{
140 QgsSettings settings;
141 QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
142 settings.setValue( baseKey + "selected", txtName->text() );
143
144 // warn if entry was renamed to an existing connection
145 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
146 settings.contains( baseKey + txtName->text() + "/host" ) &&
147 QMessageBox::question( this,
148 tr( "Save Connection" ),
149 tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
150 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
151 {
152 return;
153 }
154
155 // on rename delete original entry first
156 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
157 {
158 settings.remove( baseKey + mOriginalConnName );
159 }
160
161 baseKey += txtName->text();
162 settings.setValue( baseKey + "/host", txtHost->text() );
163 settings.setValue( baseKey + "/database", txtDatabase->text() );
164 settings.setValue( baseKey + "/port", txtPort->text() );
165 settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
166 settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
167 settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
168 settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
169 settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
170
171 QDialog::accept();
172}
173
174void QgsNewOgrConnection::btnConnect_clicked()
175{
177}
178
static QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig=false)
Create database uri from connection parameters.
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...
Definition qgsgui.cpp:208
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
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:
Definition qgssettings.h:64
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
Definition qgis.h:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39