QGIS API Documentation 3.41.0-Master (af5edcb665c)
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;
226 if ( type == QLatin1String( "vsicurl" ) )
227 {
228 uri = url;
229 // If no protocol is provided in the URL, default to HTTP
230 if ( !uri.startsWith( "http://" ) && !uri.startsWith( "https://" ) && !uri.startsWith( "ftp://" ) )
231 {
232 uri.prepend( QStringLiteral( "http://" ) );
233 }
234 uri.prepend( QStringLiteral( "/vsicurl/" ) );
235 }
236 else if ( type == QLatin1String( "vsis3" )
237 || type == QLatin1String( "vsigs" )
238 || type == QLatin1String( "vsiaz" )
239 || type == QLatin1String( "vsiadls" )
240 || type == QLatin1String( "vsioss" )
241 || type == QLatin1String( "vsiswift" )
242 || type == QLatin1String( "vsihdfs" ) )
243 {
244 uri = url;
245 uri.prepend( QStringLiteral( "/%1/" ).arg( type ) );
246 }
247 // catching both GeoJSON and GeoJSONSeq
248 else if ( type.startsWith( QLatin1String( "GeoJSON" ) ) )
249 {
250 uri = url;
251 }
252 else if ( type == QLatin1String( "CouchDB" ) )
253 {
254 uri = QStringLiteral( "couchdb:%1" ).arg( url );
255 }
256 else if ( type == QLatin1String( "DODS/OPeNDAP" ) )
257 {
258 uri = QStringLiteral( "DODS:%1" ).arg( url );
259 }
260 else if ( type == QLatin1String( "WFS3" ) )
261 {
262 uri = QStringLiteral( "WFS3:%1" ).arg( url );
263 }
264 QgsDebugMsgLevel( "Connection type is=" + type + " and uri=" + uri, 2 );
265 // Update URI with authentication information
266 if ( !configId.isEmpty() )
267 {
268 if ( expandAuthConfig )
269 {
270 QStringList connectionItems;
271 connectionItems << uri;
272 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
273 {
274 uri = connectionItems.join( QString() );
275 }
276 }
277 else
278 {
279 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
280 }
281 }
282 else if ( !( username.isEmpty() || password.isEmpty() ) )
283 {
284 uri.replace( QLatin1String( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
285 }
286 return uri;
287}
288
289QWidget *QgsGdalGuiUtils::createWidgetForOption( const QgsGdalOption &option, QWidget *parent, bool includeDefaultChoices )
290{
291 switch ( option.type )
292 {
294 {
295 QComboBox *cb = new QComboBox( parent );
296 if ( includeDefaultChoices )
297 {
298 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
299 }
300 for ( const QString &val : std::as_const( option.options ) )
301 {
302 cb->addItem( val, val );
303 }
304 cb->setCurrentIndex( 0 );
305 cb->setToolTip( option.description );
306 return cb;
307 }
308
310 {
311 QComboBox *cb = new QComboBox( parent );
312 if ( includeDefaultChoices )
313 {
314 cb->addItem( QObject::tr( "<Default>" ), QgsVariantUtils::createNullVariant( QMetaType::Type::QString ) );
315 }
316 cb->addItem( QObject::tr( "Yes" ), "YES" );
317 cb->addItem( QObject::tr( "No" ), "NO" );
318 cb->setCurrentIndex( 0 );
319 cb->setToolTip( option.description );
320 return cb;
321 }
322
324 {
325 QgsFilterLineEdit *res = new QgsFilterLineEdit( parent );
326 res->setToolTip( option.description );
327 res->setShowClearButton( true );
328 if ( includeDefaultChoices )
329 {
330 res->setPlaceholderText( QObject::tr( "Default" ) );
331 }
332 return res;
333 }
334
336 {
337 QgsSpinBox *res = new QgsSpinBox( parent );
338 res->setToolTip( option.description );
339 if ( option.minimum.isValid() )
340 res->setMinimum( option.minimum.toInt() );
341 else
342 res->setMinimum( 0 );
343 if ( option.maximum.isValid() )
344 res->setMaximum( option.maximum.toInt() );
345 else
346 res->setMaximum( std::numeric_limits<int>::max() - 1 );
347 if ( includeDefaultChoices )
348 {
349 res->setMinimum( res->minimum() - 1 );
350 res->setClearValueMode( QgsSpinBox::ClearValueMode::MinimumValue, QObject::tr( "Default" ) );
351 }
352 else if ( option.defaultValue.isValid() )
353 {
354 res->setClearValue( option.defaultValue.toInt() );
355 }
356 res->clear();
357 return res;
358 }
359
361 {
362 QgsDoubleSpinBox *res = new QgsDoubleSpinBox( parent );
363 res->setToolTip( option.description );
364 if ( option.minimum.isValid() )
365 res->setMinimum( option.minimum.toDouble() );
366 else
367 res->setMinimum( 0 );
368 if ( option.maximum.isValid() )
369 res->setMaximum( option.maximum.toDouble() );
370 else
371 res->setMaximum( std::numeric_limits<double>::max() - 1 );
372 if ( option.defaultValue.isValid() )
373 res->setClearValue( option.defaultValue.toDouble() );
374 if ( includeDefaultChoices )
375 {
376 res->setMinimum( res->minimum() - 1 );
378 }
379 else if ( option.defaultValue.isValid() )
380 {
381 res->setClearValue( option.defaultValue.toDouble() );
382 }
383 res->clear();
384 return res;
385 }
386
388 break;
389 }
390 return nullptr;
391}
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:39