QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
34 const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig
35)
36{
37 QString uri;
38
39 // If an auth configuration is set, override username and password
40 // Note that only Basic auth (username/password) is for now supported for OGR connections
41 if ( !configId.isEmpty() )
42 {
43 // Blank credentials: we are using authcfg!
44 username = QString();
45 password = QString();
46 // append authcfg is at the end, because we want to append the authcfg as last argument
47 }
48
49 //todo:add default ports for all kind of databases
50 if ( connectionType == "ESRI Personal GeoDatabase"_L1 )
51 {
52 uri = "PGeo:" + database;
53 }
54 else if ( connectionType == "ESRI ArcSDE"_L1 )
55 {
56 if ( port.isEmpty() )
57 port = u"5151"_s;
58
59 uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + username + ',' + password;
60 }
61 else if ( connectionType == "Informix DataBlade"_L1 )
62 {
63 //not tested
64 uri = "IDB:dbname=" + database;
65
66 if ( !host.isEmpty() )
67 uri += u" server=%1"_s.arg( host );
68
69 if ( !username.isEmpty() )
70 {
71 uri += u" user=%1"_s.arg( username );
72
73 if ( !password.isEmpty() )
74 uri += u" pass=%1"_s.arg( password );
75 }
76 }
77 else if ( connectionType == "Ingres"_L1 )
78 {
79 //not tested
80 uri = "@driver=ingres,dbname=" + database;
81 if ( !username.isEmpty() )
82 {
83 uri += u",userid=%1"_s.arg( username );
84
85 if ( !password.isEmpty() )
86 uri += u",password=%1"_s.arg( password );
87 }
88 }
89 else if ( connectionType == "MySQL"_L1 )
90 {
91 uri = "MySQL:" + database;
92
93 if ( !host.isEmpty() )
94 {
95 uri += u",host=%1"_s.arg( host );
96
97 if ( !port.isEmpty() )
98 uri += u",port=%1"_s.arg( port );
99 }
100
101 if ( !username.isEmpty() )
102 {
103 uri += u",user=%1"_s.arg( username );
104
105 if ( !password.isEmpty() )
106 uri += u",password=%1"_s.arg( password );
107 }
108 }
109 else if ( connectionType == "MSSQL"_L1 )
110 {
111 uri = u"MSSQL:"_s;
112
113 if ( !host.isEmpty() )
114 {
115 uri += u";server=%1"_s.arg( host );
116
117 if ( !port.isEmpty() )
118 uri += u",%1"_s.arg( port );
119 }
120
121 if ( !username.isEmpty() )
122 {
123 uri += u";uid=%1"_s.arg( username );
124
125 if ( !password.isEmpty() )
126 uri += u";pwd=%1"_s.arg( password );
127 }
128 else
129 uri += ";trusted_connection=yes"_L1;
130
131 if ( !database.isEmpty() )
132 uri += u";database=%1"_s.arg( database );
133 }
134 else if ( connectionType == "Oracle Spatial"_L1 )
135 {
136 uri = "OCI:" + username;
137
138 if ( ( !username.isEmpty() && !password.isEmpty() ) || ( username.isEmpty() && password.isEmpty() ) )
139 {
140 uri += '/';
141 if ( !password.isEmpty() )
142 uri += password;
143 }
144
145 if ( !host.isEmpty() || !database.isEmpty() )
146 {
147 uri += '@';
148
149 if ( !host.isEmpty() )
150 {
151 uri += host;
152 if ( !port.isEmpty() )
153 uri += ':' + port;
154 }
155
156 if ( !database.isEmpty() )
157 {
158 if ( !host.isEmpty() )
159 uri += '/';
160 uri += database;
161 }
162 }
163 }
164 else if ( connectionType == "ODBC"_L1 )
165 {
166 if ( !username.isEmpty() )
167 {
168 if ( password.isEmpty() )
169 {
170 uri = "ODBC:" + username + '@' + database;
171 }
172 else
173 {
174 uri = "ODBC:" + username + '/' + password + '@' + database;
175 }
176 }
177 else
178 {
179 uri = "ODBC:" + database;
180 }
181 }
182 else if ( connectionType == "OGDI Vectors"_L1 )
183 {
184 }
185 else if ( connectionType == "PostgreSQL"_L1 )
186 {
187 uri = "PG:dbname='" + database + '\'';
188
189 if ( !host.isEmpty() )
190 {
191 uri += u" host='%1'"_s.arg( host );
192
193 if ( !port.isEmpty() )
194 uri += u" port='%1'"_s.arg( port );
195 }
196
197 if ( !username.isEmpty() )
198 {
199 uri += u" user='%1'"_s.arg( username );
200
201 if ( !password.isEmpty() )
202 uri += u" password='%1'"_s.arg( password );
203 }
204
205 uri += ' ';
206 }
207 // Append authentication configuration to the URI
208 if ( !( configId.isEmpty() ) )
209 {
210 if ( !expandAuthConfig )
211 {
212 uri += u" authcfg='%1'"_s.arg( configId );
213 }
214 else
215 {
216 QStringList connectionItems;
217 connectionItems << uri;
218 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, u"ogr"_s ) )
219 {
220 uri = connectionItems.join( QString() );
221 }
222 }
223 }
224 QgsDebugMsgLevel( "Connection type is=" + connectionType + " and uri=" + uri, 2 );
225 return uri;
226}
227
228
229QString QgsGdalGuiUtils::createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig )
230{
231 QString uri = url;
232 QString prefix;
233 if ( type == "vsicurl"_L1 )
234 {
235 prefix = u"/vsicurl/"_s;
236 if ( !uri.startsWith( prefix ) )
237 {
238 // If no protocol is provided in the URL, default to HTTP
239 if ( !uri.startsWith( "http://"_L1 ) && !uri.startsWith( "https://"_L1 ) && !uri.startsWith( "ftp://"_L1 ) )
240 {
241 uri.prepend( u"http://"_s );
242 }
243 uri.prepend( prefix );
244 }
245 }
246 else if ( type == "vsis3"_L1 || type == "vsigs"_L1 || type == "vsiaz"_L1 || type == "vsiadls"_L1 || type == "vsioss"_L1 || type == "vsiswift"_L1 || type == "vsihdfs"_L1 )
247 {
248 prefix = u"/%1/"_s.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( "GeoJSON"_L1 ) )
256 {
257 // no change needed for now
258 }
259 else if ( type == "CouchDB"_L1 )
260 {
261 prefix = u"couchdb:"_s;
262 if ( !uri.startsWith( prefix ) )
263 {
264 uri.prepend( prefix );
265 }
266 }
267 else if ( type == "DODS/OPeNDAP"_L1 )
268 {
269 prefix = u"DODS:"_s;
270 if ( !uri.startsWith( prefix ) )
271 {
272 uri.prepend( prefix );
273 }
274 }
275 else if ( type == "WFS3"_L1 )
276 {
277 prefix = u"WFS3:"_s;
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, u"ogr"_s ) )
292 {
293 uri = connectionItems.join( QString() );
294 }
295 }
296 else
297 {
298 uri += u" authcfg='%1'"_s.arg( configId );
299 }
300 }
301 else if ( !( username.isEmpty() || password.isEmpty() ) )
302 {
303 uri.replace( "://"_L1, u"://%1:%2@"_s.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.
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