QGIS API Documentation 3.43.0-Master (ebb4087afc0)
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() ) || ( username.isEmpty() && password.isEmpty() ) )
133 {
134 uri += '/';
135 if ( !password.isEmpty() )
136 uri += password;
137 }
138
139 if ( !host.isEmpty() || !database.isEmpty() )
140 {
141 uri += '@';
142
143 if ( !host.isEmpty() )
144 {
145 uri += host;
146 if ( !port.isEmpty() )
147 uri += ':' + port;
148 }
149
150 if ( !database.isEmpty() )
151 {
152 if ( !host.isEmpty() )
153 uri += '/';
154 uri += database;
155 }
156 }
157 }
158 else if ( connectionType == QLatin1String( "ODBC" ) )
159 {
160 if ( !username.isEmpty() )
161 {
162 if ( password.isEmpty() )
163 {
164 uri = "ODBC:" + username + '@' + database;
165 }
166 else
167 {
168 uri = "ODBC:" + username + '/' + password + '@' + database;
169 }
170 }
171 else
172 {
173 uri = "ODBC:" + database;
174 }
175 }
176 else if ( connectionType == QLatin1String( "OGDI Vectors" ) )
177 {
178 }
179 else if ( connectionType == QLatin1String( "PostgreSQL" ) )
180 {
181 uri = "PG:dbname='" + database + '\'';
182
183 if ( !host.isEmpty() )
184 {
185 uri += QStringLiteral( " host='%1'" ).arg( host );
186
187 if ( !port.isEmpty() )
188 uri += QStringLiteral( " port='%1'" ).arg( port );
189 }
190
191 if ( !username.isEmpty() )
192 {
193 uri += QStringLiteral( " user='%1'" ).arg( username );
194
195 if ( !password.isEmpty() )
196 uri += QStringLiteral( " password='%1'" ).arg( password );
197 }
198
199 uri += ' ';
200 }
201 // Append authentication configuration to the URI
202 if ( !( configId.isEmpty() ) )
203 {
204 if ( !expandAuthConfig )
205 {
206 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
207 }
208 else
209 {
210 QStringList connectionItems;
211 connectionItems << uri;
212 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
213 {
214 uri = connectionItems.join( QString() );
215 }
216 }
217 }
218 QgsDebugMsgLevel( "Connection type is=" + connectionType + " and uri=" + uri, 2 );
219 return uri;
220}
221
222
223QString QgsGdalGuiUtils::createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig )
224{
225 QString uri = url;
226 QString prefix;
227 if ( type == QLatin1String( "vsicurl" ) )
228 {
229 prefix = QStringLiteral( "/vsicurl/" );
230 if ( !uri.startsWith( prefix ) )
231 {
232 // If no protocol is provided in the URL, default to HTTP
233 if ( !uri.startsWith( QLatin1String( "http://" ) ) && !uri.startsWith( QLatin1String( "https://" ) ) && !uri.startsWith( QLatin1String( "ftp://" ) ) )
234 {
235 uri.prepend( QStringLiteral( "http://" ) );
236 }
237 uri.prepend( prefix );
238 }
239 }
240 else if ( type == QLatin1String( "vsis3" )
241 || type == QLatin1String( "vsigs" )
242 || type == QLatin1String( "vsiaz" )
243 || type == QLatin1String( "vsiadls" )
244 || type == QLatin1String( "vsioss" )
245 || type == QLatin1String( "vsiswift" )
246 || type == QLatin1String( "vsihdfs" ) )
247 {
248 prefix = QStringLiteral( "/%1/" ).arg( type );
249 if ( !uri.startsWith( prefix ) )
250 {
251 uri.prepend( prefix );
252 }
253 }
254 // catching both GeoJSON and GeoJSONSeq
255 else if ( type.startsWith( QLatin1String( "GeoJSON" ) ) )
256 {
257 // no change needed for now
258 }
259 else if ( type == QLatin1String( "CouchDB" ) )
260 {
261 prefix = QStringLiteral( "couchdb:" );
262 if ( !uri.startsWith( prefix ) )
263 {
264 uri.prepend( prefix );
265 }
266 }
267 else if ( type == QLatin1String( "DODS/OPeNDAP" ) )
268 {
269 prefix = QStringLiteral( "DODS:" );
270 if ( !uri.startsWith( prefix ) )
271 {
272 uri.prepend( prefix );
273 }
274 }
275 else if ( type == QLatin1String( "WFS3" ) )
276 {
277 prefix = QStringLiteral( "WFS3:" );
278 if ( !uri.startsWith( prefix ) )
279 {
280 uri.prepend( prefix );
281 }
282 }
283 QgsDebugMsgLevel( "Connection type is=" + type + " and uri=" + uri, 2 );
284 // Update URI with authentication information
285 if ( !configId.isEmpty() )
286 {
287 if ( expandAuthConfig )
288 {
289 QStringList connectionItems;
290 connectionItems << uri;
291 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
292 {
293 uri = connectionItems.join( QString() );
294 }
295 }
296 else
297 {
298 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
299 }
300 }
301 else if ( !( username.isEmpty() || password.isEmpty() ) )
302 {
303 uri.replace( QLatin1String( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
304 }
305 return uri;
306}
307
308QWidget *QgsGdalGuiUtils::createWidgetForOption( const QgsGdalOption &option, QWidget *parent, bool includeDefaultChoices )
309{
310 switch ( option.type )
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 for ( const QString &val : std::as_const( option.options ) )
320 {
321 cb->addItem( val, val );
322 }
323 cb->setCurrentIndex( 0 );
324 cb->setToolTip( option.description );
325 return cb;
326 }
327
329 {
330 QComboBox *cb = new QComboBox( parent );
331 if ( includeDefaultChoices )
332 {
333 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
334 }
335 cb->addItem( QObject::tr( "Yes" ), "YES" );
336 cb->addItem( QObject::tr( "No" ), "NO" );
337 cb->setCurrentIndex( 0 );
338 cb->setToolTip( option.description );
339 return cb;
340 }
341
343 {
344 QgsFilterLineEdit *res = new QgsFilterLineEdit( parent );
345 res->setToolTip( option.description );
346 res->setShowClearButton( true );
347 if ( includeDefaultChoices )
348 {
349 res->setPlaceholderText( QObject::tr( "Default" ) );
350 }
351 return res;
352 }
353
355 {
356 QgsSpinBox *res = new QgsSpinBox( parent );
357 res->setToolTip( option.description );
358 if ( option.minimum.isValid() )
359 res->setMinimum( option.minimum.toInt() );
360 else
361 res->setMinimum( 0 );
362 if ( option.maximum.isValid() )
363 res->setMaximum( option.maximum.toInt() );
364 else
365 res->setMaximum( std::numeric_limits<int>::max() - 1 );
366 if ( includeDefaultChoices )
367 {
368 res->setMinimum( res->minimum() - 1 );
369 res->setClearValueMode( QgsSpinBox::ClearValueMode::MinimumValue, QObject::tr( "Default" ) );
370 }
371 else if ( option.defaultValue.isValid() )
372 {
373 res->setClearValue( option.defaultValue.toInt() );
374 }
375 res->clear();
376 return res;
377 }
378
380 {
381 QgsDoubleSpinBox *res = new QgsDoubleSpinBox( parent );
382 res->setToolTip( option.description );
383 if ( option.minimum.isValid() )
384 res->setMinimum( option.minimum.toDouble() );
385 else
386 res->setMinimum( 0 );
387 if ( option.maximum.isValid() )
388 res->setMaximum( option.maximum.toDouble() );
389 else
390 res->setMaximum( std::numeric_limits<double>::max() - 1 );
391 if ( option.defaultValue.isValid() )
392 res->setClearValue( option.defaultValue.toDouble() );
393 if ( includeDefaultChoices )
394 {
395 res->setMinimum( res->minimum() - 1 );
397 }
398 else if ( option.defaultValue.isValid() )
399 {
400 res->setClearValue( option.defaultValue.toDouble() );
401 }
402 res->clear();
403 return res;
404 }
405
407 break;
408 }
409 return nullptr;
410}
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:62
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:41