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