QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsgdalguiutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgdalguiutils.cpp
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
18#include "qgsgdalguiutils.h"
19#include "qgslogger.h"
20#include "qgsapplication.h"
21#include "qgsauthmanager.h"
22#include "qgsgdalutils.h"
23#include "qgsspinbox.h"
24#include "qgsdoublespinbox.h"
25#include "qgsfilterlineedit.h"
26
27#include <QComboBox>
28
29QString QgsGdalGuiUtils::createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig )
30{
31 QString uri;
32
33 // If an auth configuration is set, override username and password
34 // Note that only Basic auth (username/password) is for now supported for OGR connections
35 if ( ! configId.isEmpty() )
36 {
37 // Blank credentials: we are using authcfg!
38 username = QString();
39 password = QString();
40 // append authcfg is at the end, because we want to append the authcfg as last argument
41 }
42
43 //todo:add default ports for all kind of databases
44 if ( connectionType == QLatin1String( "ESRI Personal GeoDatabase" ) )
45 {
46 uri = "PGeo:" + database;
47 }
48 else if ( connectionType == QLatin1String( "ESRI ArcSDE" ) )
49 {
50 if ( port.isEmpty() )
51 port = QStringLiteral( "5151" );
52
53 uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + username + ',' + password;
54 }
55 else if ( connectionType == QLatin1String( "Informix DataBlade" ) )
56 {
57 //not tested
58 uri = "IDB:dbname=" + database;
59
60 if ( !host.isEmpty() )
61 uri += QStringLiteral( " server=%1" ).arg( host );
62
63 if ( !username.isEmpty() )
64 {
65 uri += QStringLiteral( " user=%1" ).arg( username );
66
67 if ( !password.isEmpty() )
68 uri += QStringLiteral( " pass=%1" ).arg( password );
69 }
70 }
71 else if ( connectionType == QLatin1String( "Ingres" ) )
72 {
73 //not tested
74 uri = "@driver=ingres,dbname=" + database;
75 if ( !username.isEmpty() )
76 {
77 uri += QStringLiteral( ",userid=%1" ).arg( username );
78
79 if ( !password.isEmpty() )
80 uri += QStringLiteral( ",password=%1" ).arg( password );
81 }
82 }
83 else if ( connectionType == QLatin1String( "MySQL" ) )
84 {
85 uri = "MySQL:" + database;
86
87 if ( !host.isEmpty() )
88 {
89 uri += QStringLiteral( ",host=%1" ).arg( host );
90
91 if ( !port.isEmpty() )
92 uri += QStringLiteral( ",port=%1" ).arg( port );
93 }
94
95 if ( !username.isEmpty() )
96 {
97 uri += QStringLiteral( ",user=%1" ).arg( username );
98
99 if ( !password.isEmpty() )
100 uri += QStringLiteral( ",password=%1" ).arg( password );
101 }
102 }
103 else if ( connectionType == QLatin1String( "MSSQL" ) )
104 {
105 uri = QStringLiteral( "MSSQL:" );
106
107 if ( !host.isEmpty() )
108 {
109 uri += QStringLiteral( ";server=%1" ).arg( host );
110
111 if ( !port.isEmpty() )
112 uri += QStringLiteral( ",%1" ).arg( port );
113 }
114
115 if ( !username.isEmpty() )
116 {
117 uri += QStringLiteral( ";uid=%1" ).arg( username );
118
119 if ( !password.isEmpty() )
120 uri += QStringLiteral( ";pwd=%1" ).arg( password );
121 }
122 else
123 uri += QLatin1String( ";trusted_connection=yes" );
124
125 if ( !database.isEmpty() )
126 uri += QStringLiteral( ";database=%1" ).arg( database );
127 }
128 else if ( connectionType == QLatin1String( "Oracle Spatial" ) )
129 {
130 uri = "OCI:" + username;
131
132 if ( ( !username.isEmpty() && !password.isEmpty() ) ||
133 ( username.isEmpty() && password.isEmpty() ) )
134 {
135 uri += '/';
136 if ( !password.isEmpty() )
137 uri += password;
138 }
139
140 if ( !host.isEmpty() || !database.isEmpty() )
141 {
142 uri += '@';
143
144 if ( !host.isEmpty() )
145 {
146 uri += host;
147 if ( !port.isEmpty() )
148 uri += ':' + port;
149 }
150
151 if ( !database.isEmpty() )
152 {
153 if ( !host.isEmpty() )
154 uri += '/';
155 uri += database;
156 }
157 }
158 }
159 else if ( connectionType == QLatin1String( "ODBC" ) )
160 {
161 if ( !username.isEmpty() )
162 {
163 if ( password.isEmpty() )
164 {
165 uri = "ODBC:" + username + '@' + database;
166 }
167 else
168 {
169 uri = "ODBC:" + username + '/' + password + '@' + database;
170 }
171
172 }
173 else
174 {
175 uri = "ODBC:" + database;
176 }
177 }
178 else if ( connectionType == QLatin1String( "OGDI Vectors" ) )
179 {
180 }
181 else if ( connectionType == QLatin1String( "PostgreSQL" ) )
182 {
183 uri = "PG:dbname='" + database + '\'';
184
185 if ( !host.isEmpty() )
186 {
187 uri += QStringLiteral( " host='%1'" ).arg( host );
188
189 if ( !port.isEmpty() )
190 uri += QStringLiteral( " port='%1'" ).arg( port );
191 }
192
193 if ( !username.isEmpty() )
194 {
195 uri += QStringLiteral( " user='%1'" ).arg( username );
196
197 if ( !password.isEmpty() )
198 uri += QStringLiteral( " password='%1'" ).arg( password );
199 }
200
201 uri += ' ';
202 }
203 // Append authentication configuration to the URI
204 if ( !( configId.isEmpty() ) )
205 {
206 if ( ! expandAuthConfig )
207 {
208 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
209 }
210 else
211 {
212 QStringList connectionItems;
213 connectionItems << uri;
214 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
215 {
216 uri = connectionItems.join( QString() );
217 }
218 }
219 }
220 QgsDebugMsgLevel( "Connection type is=" + connectionType + " and uri=" + uri, 2 );
221 return uri;
222}
223
224
225QString QgsGdalGuiUtils::createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig )
226{
227 QString uri;
228 if ( type == QLatin1String( "vsicurl" ) )
229 {
230 uri = url;
231 // If no protocol is provided in the URL, default to HTTP
232 if ( !uri.startsWith( "http://" ) && !uri.startsWith( "https://" ) && !uri.startsWith( "ftp://" ) )
233 {
234 uri.prepend( QStringLiteral( "http://" ) );
235 }
236 uri.prepend( QStringLiteral( "/vsicurl/" ) );
237 }
238 else if ( type == QLatin1String( "vsis3" )
239 || type == QLatin1String( "vsigs" )
240 || type == QLatin1String( "vsiaz" )
241 || type == QLatin1String( "vsiadls" )
242 || type == QLatin1String( "vsioss" )
243 || type == QLatin1String( "vsiswift" )
244 || type == QLatin1String( "vsihdfs" )
245 )
246 {
247 uri = url;
248 uri.prepend( QStringLiteral( "/%1/" ).arg( type ) );
249 }
250 // catching both GeoJSON and GeoJSONSeq
251 else if ( type.startsWith( QLatin1String( "GeoJSON" ) ) )
252 {
253 uri = url;
254 }
255 else if ( type == QLatin1String( "CouchDB" ) )
256 {
257 uri = QStringLiteral( "couchdb:%1" ).arg( url );
258 }
259 else if ( type == QLatin1String( "DODS/OPeNDAP" ) )
260 {
261 uri = QStringLiteral( "DODS:%1" ).arg( url );
262 }
263 else if ( type == QLatin1String( "WFS3" ) )
264 {
265 uri = QStringLiteral( "WFS3:%1" ).arg( url );
266 }
267 QgsDebugMsgLevel( "Connection type is=" + type + " and uri=" + uri, 2 );
268 // Update URI with authentication information
269 if ( ! configId.isEmpty() )
270 {
271 if ( expandAuthConfig )
272 {
273 QStringList connectionItems;
274 connectionItems << uri;
275 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
276 {
277 uri = connectionItems.join( QString() );
278 }
279 }
280 else
281 {
282 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
283 }
284 }
285 else if ( !( username.isEmpty() || password.isEmpty( ) ) )
286 {
287 uri.replace( QLatin1String( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
288 }
289 return uri;
290}
291
292QWidget *QgsGdalGuiUtils::createWidgetForOption( const QgsGdalOption &option, QWidget *parent, bool includeDefaultChoices )
293{
294 switch ( option.type )
295 {
297 {
298 QComboBox *cb = new QComboBox( parent );
299 if ( includeDefaultChoices )
300 {
301 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
302 }
303 for ( const QString &val : std::as_const( option.options ) )
304 {
305 cb->addItem( val, val );
306 }
307 cb->setCurrentIndex( 0 );
308 cb->setToolTip( option.description );
309 return cb;
310 }
311
313 {
314 QComboBox *cb = new QComboBox( parent );
315 if ( includeDefaultChoices )
316 {
317 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
318 }
319 cb->addItem( QObject::tr( "Yes" ), "YES" );
320 cb->addItem( QObject::tr( "No" ), "NO" );
321 cb->setCurrentIndex( 0 );
322 cb->setToolTip( option.description );
323 return cb;
324 }
325
327 {
328 QgsFilterLineEdit *res = new QgsFilterLineEdit( parent );
329 res->setToolTip( option.description );
330 res->setShowClearButton( true );
331 if ( includeDefaultChoices )
332 {
333 res->setPlaceholderText( QObject::tr( "Default" ) );
334 }
335 return res;
336 }
337
339 {
340 QgsSpinBox *res = new QgsSpinBox( parent );
341 res->setToolTip( option.description );
342 if ( option.minimum.isValid() )
343 res->setMinimum( option.minimum.toInt() );
344 else
345 res->setMinimum( 0 );
346 if ( option.maximum.isValid() )
347 res->setMaximum( option.maximum.toInt() );
348 else
349 res->setMaximum( std::numeric_limits< int>::max() - 1 );
350 if ( includeDefaultChoices )
351 {
352 res->setMinimum( res->minimum() - 1 );
354 QObject::tr( "Default" ) );
355 }
356 else if ( option.defaultValue.isValid() )
357 {
358 res->setClearValue( option.defaultValue.toInt() );
359 }
360 res->clear();
361 return res;
362 }
363
365 {
366 QgsDoubleSpinBox *res = new QgsDoubleSpinBox( parent );
367 res->setToolTip( option.description );
368 if ( option.minimum.isValid() )
369 res->setMinimum( option.minimum.toDouble() );
370 else
371 res->setMinimum( 0 );
372 if ( option.maximum.isValid() )
373 res->setMaximum( option.maximum.toDouble() );
374 else
375 res->setMaximum( std::numeric_limits< double>::max() - 1 );
376 if ( option.defaultValue.isValid() )
377 res->setClearValue( option.defaultValue.toDouble() );
378 if ( includeDefaultChoices )
379 {
380 res->setMinimum( res->minimum() - 1 );
382 QObject::tr( "Default" ) );
383 }
384 else if ( option.defaultValue.isValid() )
385 {
386 res->setClearValue( option.defaultValue.toDouble() );
387 }
388 res->clear();
389 return res;
390 }
391
393 break;
394 }
395 return nullptr;
396}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
void setClearValueMode(ClearValueMode mode, const QString &clearValueText=QString())
Defines if the clear value should be the minimum or maximum values of the widget or a custom value.
void clear() override
Sets the current value to the value defined by the clear value.
@ MinimumValue
Reset value to minimum()
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
static QString createProtocolURI(const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig=false)
Create protocol uri from connection parameters.
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 QWidget * createWidgetForOption(const QgsGdalOption &option, QWidget *parent=nullptr, bool includeDefaultChoices=false)
Creates a new widget for configuration a GDAL option.
Encapsulates the definition of a GDAL configuration option.
QVariant defaultValue
Default value.
QVariant maximum
Maximum acceptable value.
QStringList options
Available choices, for Select options.
QVariant minimum
Minimum acceptable value.
@ Int
Integer option.
@ Boolean
Boolean option.
@ Invalid
Invalid option.
@ Text
Text option.
@ Double
Double option.
@ Select
Selection option.
QString description
Option description.
Type type
Option type.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
Definition qgsspinbox.h:43
@ MinimumValue
Reset value to minimum()
Definition qgsspinbox.h:64
void setClearValueMode(ClearValueMode mode, const QString &clearValueText=QString())
Defines if the clear value should be the minimum or maximum values of the widget or a custom value.
void setClearValue(int customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
void clear() override
Sets the current value to the value defined by the clear value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39