QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include "qgsapplication.h"
21#include "qgsauthmanager.h"
22#include "qgsdoublespinbox.h"
23#include "qgsfilterlineedit.h"
24#include "qgsgdalutils.h"
25#include "qgslogger.h"
26#include "qgsspinbox.h"
27
28#include <QComboBox>
29
30QString QgsGdalGuiUtils::createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig )
31{
32 QString uri;
33
34 // If an auth configuration is set, override username and password
35 // Note that only Basic auth (username/password) is for now supported for OGR connections
36 if ( !configId.isEmpty() )
37 {
38 // Blank credentials: we are using authcfg!
39 username = QString();
40 password = QString();
41 // append authcfg is at the end, because we want to append the authcfg as last argument
42 }
43
44 //todo:add default ports for all kind of databases
45 if ( connectionType == QLatin1String( "ESRI Personal GeoDatabase" ) )
46 {
47 uri = "PGeo:" + database;
48 }
49 else if ( connectionType == QLatin1String( "ESRI ArcSDE" ) )
50 {
51 if ( port.isEmpty() )
52 port = QStringLiteral( "5151" );
53
54 uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + username + ',' + password;
55 }
56 else if ( connectionType == QLatin1String( "Informix DataBlade" ) )
57 {
58 //not tested
59 uri = "IDB:dbname=" + database;
60
61 if ( !host.isEmpty() )
62 uri += QStringLiteral( " server=%1" ).arg( host );
63
64 if ( !username.isEmpty() )
65 {
66 uri += QStringLiteral( " user=%1" ).arg( username );
67
68 if ( !password.isEmpty() )
69 uri += QStringLiteral( " pass=%1" ).arg( password );
70 }
71 }
72 else if ( connectionType == QLatin1String( "Ingres" ) )
73 {
74 //not tested
75 uri = "@driver=ingres,dbname=" + database;
76 if ( !username.isEmpty() )
77 {
78 uri += QStringLiteral( ",userid=%1" ).arg( username );
79
80 if ( !password.isEmpty() )
81 uri += QStringLiteral( ",password=%1" ).arg( password );
82 }
83 }
84 else if ( connectionType == QLatin1String( "MySQL" ) )
85 {
86 uri = "MySQL:" + database;
87
88 if ( !host.isEmpty() )
89 {
90 uri += QStringLiteral( ",host=%1" ).arg( host );
91
92 if ( !port.isEmpty() )
93 uri += QStringLiteral( ",port=%1" ).arg( port );
94 }
95
96 if ( !username.isEmpty() )
97 {
98 uri += QStringLiteral( ",user=%1" ).arg( username );
99
100 if ( !password.isEmpty() )
101 uri += QStringLiteral( ",password=%1" ).arg( password );
102 }
103 }
104 else if ( connectionType == QLatin1String( "MSSQL" ) )
105 {
106 uri = QStringLiteral( "MSSQL:" );
107
108 if ( !host.isEmpty() )
109 {
110 uri += QStringLiteral( ";server=%1" ).arg( host );
111
112 if ( !port.isEmpty() )
113 uri += QStringLiteral( ",%1" ).arg( port );
114 }
115
116 if ( !username.isEmpty() )
117 {
118 uri += QStringLiteral( ";uid=%1" ).arg( username );
119
120 if ( !password.isEmpty() )
121 uri += QStringLiteral( ";pwd=%1" ).arg( password );
122 }
123 else
124 uri += QLatin1String( ";trusted_connection=yes" );
125
126 if ( !database.isEmpty() )
127 uri += QStringLiteral( ";database=%1" ).arg( database );
128 }
129 else if ( connectionType == QLatin1String( "Oracle Spatial" ) )
130 {
131 uri = "OCI:" + username;
132
133 if ( ( !username.isEmpty() && !password.isEmpty() ) || ( 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 else
173 {
174 uri = "ODBC:" + database;
175 }
176 }
177 else if ( connectionType == QLatin1String( "OGDI Vectors" ) )
178 {
179 }
180 else if ( connectionType == QLatin1String( "PostgreSQL" ) )
181 {
182 uri = "PG:dbname='" + database + '\'';
183
184 if ( !host.isEmpty() )
185 {
186 uri += QStringLiteral( " host='%1'" ).arg( host );
187
188 if ( !port.isEmpty() )
189 uri += QStringLiteral( " port='%1'" ).arg( port );
190 }
191
192 if ( !username.isEmpty() )
193 {
194 uri += QStringLiteral( " user='%1'" ).arg( username );
195
196 if ( !password.isEmpty() )
197 uri += QStringLiteral( " password='%1'" ).arg( password );
198 }
199
200 uri += ' ';
201 }
202 // Append authentication configuration to the URI
203 if ( !( configId.isEmpty() ) )
204 {
205 if ( !expandAuthConfig )
206 {
207 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
208 }
209 else
210 {
211 QStringList connectionItems;
212 connectionItems << uri;
213 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
214 {
215 uri = connectionItems.join( QString() );
216 }
217 }
218 }
219 QgsDebugMsgLevel( "Connection type is=" + connectionType + " and uri=" + uri, 2 );
220 return uri;
221}
222
223
224QString QgsGdalGuiUtils::createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig )
225{
226 QString uri = url;
227 QString prefix;
228 if ( type == QLatin1String( "vsicurl" ) )
229 {
230 prefix = QStringLiteral( "/vsicurl/" );
231 if ( !uri.startsWith( prefix ) )
232 {
233 // If no protocol is provided in the URL, default to HTTP
234 if ( !uri.startsWith( QLatin1String( "http://" ) ) && !uri.startsWith( QLatin1String( "https://" ) ) && !uri.startsWith( QLatin1String( "ftp://" ) ) )
235 {
236 uri.prepend( QStringLiteral( "http://" ) );
237 }
238 uri.prepend( prefix );
239 }
240 }
241 else if ( type == QLatin1String( "vsis3" )
242 || type == QLatin1String( "vsigs" )
243 || type == QLatin1String( "vsiaz" )
244 || type == QLatin1String( "vsiadls" )
245 || type == QLatin1String( "vsioss" )
246 || type == QLatin1String( "vsiswift" )
247 || type == QLatin1String( "vsihdfs" ) )
248 {
249 prefix = QStringLiteral( "/%1/" ).arg( type );
250 if ( !uri.startsWith( prefix ) )
251 {
252 uri.prepend( prefix );
253 }
254 }
255 // catching both GeoJSON and GeoJSONSeq
256 else if ( type.startsWith( QLatin1String( "GeoJSON" ) ) )
257 {
258 // no change needed for now
259 }
260 else if ( type == QLatin1String( "CouchDB" ) )
261 {
262 prefix = QStringLiteral( "couchdb:" );
263 if ( !uri.startsWith( prefix ) )
264 {
265 uri.prepend( prefix );
266 }
267 }
268 else if ( type == QLatin1String( "DODS/OPeNDAP" ) )
269 {
270 prefix = QStringLiteral( "DODS:" );
271 if ( !uri.startsWith( prefix ) )
272 {
273 uri.prepend( prefix );
274 }
275 }
276 else if ( type == QLatin1String( "WFS3" ) )
277 {
278 prefix = QStringLiteral( "WFS3:" );
279 if ( !uri.startsWith( prefix ) )
280 {
281 uri.prepend( prefix );
282 }
283 }
284 QgsDebugMsgLevel( "Connection type is=" + type + " and uri=" + uri, 2 );
285 // Update URI with authentication information
286 if ( !configId.isEmpty() )
287 {
288 if ( expandAuthConfig )
289 {
290 QStringList connectionItems;
291 connectionItems << uri;
292 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
293 {
294 uri = connectionItems.join( QString() );
295 }
296 }
297 else
298 {
299 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
300 }
301 }
302 else if ( !( username.isEmpty() || password.isEmpty() ) )
303 {
304 uri.replace( QLatin1String( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
305 }
306 return uri;
307}
308
309QWidget *QgsGdalGuiUtils::createWidgetForOption( const QgsGdalOption &option, QWidget *parent, bool includeDefaultChoices )
310{
311 switch ( option.type )
312 {
314 {
315 QComboBox *cb = new QComboBox( parent );
316 if ( includeDefaultChoices )
317 {
318 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
319 }
320 for ( const QString &val : std::as_const( option.options ) )
321 {
322 cb->addItem( val, val );
323 }
324 cb->setCurrentIndex( 0 );
325 cb->setToolTip( option.description );
326 return cb;
327 }
328
330 {
331 QComboBox *cb = new QComboBox( parent );
332 if ( includeDefaultChoices )
333 {
334 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
335 }
336 cb->addItem( QObject::tr( "Yes" ), "YES" );
337 cb->addItem( QObject::tr( "No" ), "NO" );
338 cb->setCurrentIndex( 0 );
339 cb->setToolTip( option.description );
340 return cb;
341 }
342
344 {
345 QgsFilterLineEdit *res = new QgsFilterLineEdit( parent );
346 res->setToolTip( option.description );
347 res->setShowClearButton( true );
348 if ( includeDefaultChoices )
349 {
350 res->setPlaceholderText( QObject::tr( "Default" ) );
351 }
352 return res;
353 }
354
356 {
357 QgsSpinBox *res = new QgsSpinBox( parent );
358 res->setToolTip( option.description );
359 if ( option.minimum.isValid() )
360 res->setMinimum( option.minimum.toInt() );
361 else
362 res->setMinimum( 0 );
363 if ( option.maximum.isValid() )
364 res->setMaximum( option.maximum.toInt() );
365 else
366 res->setMaximum( std::numeric_limits<int>::max() - 1 );
367 if ( includeDefaultChoices )
368 {
369 res->setMinimum( res->minimum() - 1 );
370 res->setClearValueMode( QgsSpinBox::ClearValueMode::MinimumValue, QObject::tr( "Default" ) );
371 }
372 else if ( option.defaultValue.isValid() )
373 {
374 res->setClearValue( option.defaultValue.toInt() );
375 }
376 res->clear();
377 return res;
378 }
379
381 {
382 QgsDoubleSpinBox *res = new QgsDoubleSpinBox( parent );
383 res->setToolTip( option.description );
384 if ( option.minimum.isValid() )
385 res->setMinimum( option.minimum.toDouble() );
386 else
387 res->setMinimum( 0 );
388 if ( option.maximum.isValid() )
389 res->setMaximum( option.maximum.toDouble() );
390 else
391 res->setMaximum( std::numeric_limits<double>::max() - 1 );
392 if ( option.defaultValue.isValid() )
393 res->setClearValue( option.defaultValue.toDouble() );
394 if ( includeDefaultChoices )
395 {
396 res->setMinimum( res->minimum() - 1 );
398 }
399 else if ( option.defaultValue.isValid() )
400 {
401 res->setClearValue( option.defaultValue.toDouble() );
402 }
403 res->clear();
404 return res;
405 }
406
408 break;
409 }
410 return nullptr;
411}
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.
A spin box with a clear button that will set the value to the defined clear value.
Definition qgsspinbox.h:45
@ 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:61