QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsapplication.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsapplication.cpp - Accessors for application-wide data
3 --------------------------------------
4 Date : 02-Jan-2006
5 Copyright : (C) 2006 by Tom Elwertowski
6 Email : telwertowski at users dot sourceforge dot net
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsapplication.h"
17#include "qgsauthmanager.h"
20#include "qgsexception.h"
21#include "qgsgeometry.h"
24#include "qgslayout.h"
26#include "qgslogger.h"
27#include "qgsproject.h"
30#include "qgsnetworkreply.h"
31#include "qgsproviderregistry.h"
32#include "qgsexpression.h"
34#include "qgsruntimeprofiler.h"
35#include "qgstaskmanager.h"
39#include "qgssvgcache.h"
40#include "qgsimagecache.h"
41#include "qgssourcecache.h"
47#include "qgsrendererregistry.h"
51#include "qgssymbollayerutils.h"
52#include "qgscalloutsregistry.h"
55#include "qgsmessagelog.h"
57#include "qgssettings.h"
60#include "qgsunittypes.h"
61#include "qgsuserprofile.h"
65#include "qgs3dsymbolregistry.h"
67#include "qgssqliteutils.h"
68#include "qgsstyle.h"
69#include "qgsprojutils.h"
71#include "qgsnewsfeedparser.h"
72#include "qgsbookmarkmanager.h"
73#include "qgsstylemodel.h"
76#include "qgsmeshlayer.h"
77#include "qgsfeaturestore.h"
78#include "qgslocator.h"
79#include "qgsreadwritelocker.h"
81#include "qgsdbquerylog.h"
82#include "qgsfontmanager.h"
84#include "qgscolorrampimpl.h"
85#include "qgsinterval.h"
86#include "qgsgpsconnection.h"
87#include "qgssensorregistry.h"
88
93
97
98#include <QDir>
99#include <QFile>
100#include <QFileInfo>
101#include <QFileOpenEvent>
102#include <QMessageBox>
103#include <QPalette>
104#include <QProcess>
105#include <QProcessEnvironment>
106#include <QIcon>
107#include <QPixmap>
108#include <QThreadPool>
109#include <QLocale>
110#include <QStyle>
111#include <QLibraryInfo>
112#include <QStandardPaths>
113#include <QRegularExpression>
114#include <QTextStream>
115#include <QScreen>
116#include <QAuthenticator>
117#include <QRecursiveMutex>
118
120
122
124
126
128
129#ifndef Q_OS_WIN
130#include <netinet/in.h>
131#include <pwd.h>
132#else
133#include <winsock.h>
134#include <windows.h>
135#include <lmcons.h>
136#define SECURITY_WIN32
137#include <security.h>
138#ifdef _MSC_VER
139#pragma comment( lib, "Secur32.lib" )
140#endif
141#endif
142
143#include "qgsconfig.h"
144
145#include <gdal.h>
146#include <ogr_api.h>
147#include <cpl_conv.h> // for setting gdal options
148#include <sqlite3.h>
149#include <mutex>
150
151#include <proj.h>
152
153#if defined(Q_OS_LINUX)
154#include <sys/sysinfo.h>
155#endif
156
157#define CONN_POOL_MAX_CONCURRENT_CONNS 4
158
159QObject *ABISYM( QgsApplication::mFileOpenEventReceiver ) = nullptr;
160bool ABISYM( QgsApplication::mInitialized ) = false;
161bool ABISYM( QgsApplication::mRunningFromBuildDir ) = false;
162const char *QgsApplication::QGIS_ORGANIZATION_NAME = "QGIS";
163const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
164const char *QgsApplication::QGIS_APPLICATION_NAME = "QGIS3";
165QgsApplication::ApplicationMembers *QgsApplication::sApplicationMembers = nullptr;
166QgsAuthManager *QgsApplication::sAuthManager = nullptr;
167int ABISYM( QgsApplication::sMaxThreads ) = -1;
168
169Q_GLOBAL_STATIC( QStringList, sFileOpenEventList )
170Q_GLOBAL_STATIC( QString, sPrefixPath )
171Q_GLOBAL_STATIC( QString, sPluginPath )
172Q_GLOBAL_STATIC( QString, sPkgDataPath )
173Q_GLOBAL_STATIC( QString, sLibraryPath )
174Q_GLOBAL_STATIC( QString, sLibexecPath )
175Q_GLOBAL_STATIC( QString, sQmlImportPath )
176Q_GLOBAL_STATIC( QString, sThemeName )
177Q_GLOBAL_STATIC( QString, sProfilePath )
178
179Q_GLOBAL_STATIC( QStringList, sDefaultSvgPaths )
180Q_GLOBAL_STATIC( QgsStringMap, sSystemEnvVars )
181Q_GLOBAL_STATIC( QString, sConfigPath )
182
183Q_GLOBAL_STATIC( QString, sBuildSourcePath )
184#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
185Q_GLOBAL_STATIC( QString, sCfgIntDir )
186#endif
187Q_GLOBAL_STATIC( QString, sBuildOutputPath )
188Q_GLOBAL_STATIC( QStringList, sGdalSkipList )
189Q_GLOBAL_STATIC( QStringList, sDeferredSkippedGdalDrivers )
190Q_GLOBAL_STATIC( QString, sAuthDbDirPath )
191
192Q_GLOBAL_STATIC( QString, sUserName )
193Q_GLOBAL_STATIC( QString, sUserFullName )
194Q_GLOBAL_STATIC_WITH_ARGS( QString, sPlatformName, ( "external" ) )
195Q_GLOBAL_STATIC( QString, sApplicationFullName )
196Q_GLOBAL_STATIC( QString, sTranslation )
197
198Q_GLOBAL_STATIC( QTemporaryDir, sIconCacheDir )
199
200QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder, const QString &platformName )
201 : QApplication( argc, argv, GUIenabled )
202{
203 *sPlatformName() = platformName;
204
206
207 // Delay application members initialization in desktop app (In desktop app, profile folder is not known at this point)
208 if ( platformName != QLatin1String( "desktop" ) )
209 {
210 mApplicationMembers = new ApplicationMembers();
211 mApplicationMembers->mSettingsRegistryCore->migrateOldSettings();
212 }
213 else
214 {
215 *sProfilePath() = profileFolder;
216 }
217
218}
219
220void QgsApplication::init( QString profileFolder )
221{
222 // Initialize application members in desktop app (at this point, profile folder is known)
223 if ( platform() == QLatin1String( "desktop" ) )
224 {
225 instance()->mApplicationMembers = new ApplicationMembers();
226 instance()->mApplicationMembers->mSettingsRegistryCore->migrateOldSettings();
227 }
228
229 if ( profileFolder.isEmpty() )
230 {
231 if ( getenv( "QGIS_CUSTOM_CONFIG_PATH" ) )
232 {
233 profileFolder = getenv( "QGIS_CUSTOM_CONFIG_PATH" );
234 }
235 else
236 {
237 profileFolder = QStandardPaths::standardLocations( QStandardPaths::AppDataLocation ).value( 0 );
238 }
239 // This will normally get here for custom scripts that use QgsApplication.
240 // This doesn't get this hit for QGIS Desktop because we setup the profile via main
241 QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( profileFolder );
242 QgsUserProfileManager manager( rootProfileFolder );
243 QgsUserProfile *profile = manager.getProfile();
244 profileFolder = profile->folder();
245 delete profile;
246 }
247
248 *sProfilePath() = profileFolder;
249
250 static std::once_flag sMetaTypesRegistered;
251 std::call_once( sMetaTypesRegistered, []
252 {
253 qRegisterMetaType<QgsGeometry::Error>( "QgsGeometry::Error" );
254 qRegisterMetaType<QgsDatabaseQueryLogEntry>( "QgsDatabaseQueryLogEntry" );
255 qRegisterMetaType<QgsProcessingFeatureSourceDefinition>( "QgsProcessingFeatureSourceDefinition" );
256 qRegisterMetaType<QgsProcessingOutputLayerDefinition>( "QgsProcessingOutputLayerDefinition" );
257 qRegisterMetaType<Qgis::LayoutUnit>( "Qgis::LayoutUnit" );
258 qRegisterMetaType<QgsUnsetAttributeValue>( "QgsUnsetAttributeValue" );
259 qRegisterMetaType<QgsFeatureId>( "QgsFeatureId" );
260 qRegisterMetaType<QgsFields>( "QgsFields" );
261 qRegisterMetaType<QgsFeatureIds>( "QgsFeatureIds" );
262 qRegisterMetaType<QgsProperty>( "QgsProperty" );
263 qRegisterMetaType<QgsFeatureStoreList>( "QgsFeatureStoreList" );
264 qRegisterMetaType<Qgis::MessageLevel>( "Qgis::MessageLevel" );
265 qRegisterMetaType<Qgis::BrowserItemState>( "Qgis::BrowserItemState" );
266 qRegisterMetaType<Qgis::GpsFixStatus>( "Qgis::GpsFixStatus" );
267 qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
268 qRegisterMetaType<QgsReferencedPointXY>( "QgsReferencedPointXY" );
269 qRegisterMetaType<QgsReferencedGeometry>( "QgsReferencedGeometry" );
270 qRegisterMetaType<QgsLayoutRenderContext::Flags>( "QgsLayoutRenderContext::Flags" );
271 qRegisterMetaType<QgsStyle::StyleEntity>( "QgsStyle::StyleEntity" );
272 qRegisterMetaType<QgsCoordinateReferenceSystem>( "QgsCoordinateReferenceSystem" );
273 qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" );
274 qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" );
275 qRegisterMetaType<QgsNetworkReplyContent>( "QgsNetworkReplyContent" );
276 qRegisterMetaType<QgsFeature>( "QgsFeature" );
277 qRegisterMetaType<QgsGeometry>( "QgsGeometry" );
278 qRegisterMetaType<QgsInterval>( "QgsInterval" );
279 qRegisterMetaType<QgsRectangle>( "QgsRectangle" );
280 qRegisterMetaType<QgsPointXY>( "QgsPointXY" );
281 qRegisterMetaType<QgsPoint>( "QgsPoint" );
282 qRegisterMetaType<QgsDatumTransform::GridDetails>( "QgsDatumTransform::GridDetails" );
283 qRegisterMetaType<QgsDatumTransform::TransformDetails>( "QgsDatumTransform::TransformDetails" );
284 qRegisterMetaType<QgsNewsFeedParser::Entry>( "QgsNewsFeedParser::Entry" );
285 qRegisterMetaType<QgsRectangle>( "QgsRectangle" );
286 qRegisterMetaType<QgsLocatorResult>( "QgsLocatorResult" );
287 qRegisterMetaType<QgsGradientColorRamp>( "QgsGradientColorRamp" );
288 qRegisterMetaType<QgsProcessingModelChildParameterSource>( "QgsProcessingModelChildParameterSource" );
289#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
290 // Qt6 documentation says these are not needed anymore (https://www.qt.io/blog/whats-new-in-qmetatype-qvariant) #spellok
291 // TODO: when tests can run against Qt6 builds, check for any regressions
292 qRegisterMetaTypeStreamOperators<QgsProcessingModelChildParameterSource>( "QgsProcessingModelChildParameterSource" );
293#endif
294 qRegisterMetaType<QgsRemappingSinkDefinition>( "QgsRemappingSinkDefinition" );
295 qRegisterMetaType<QgsProcessingModelChildDependency>( "QgsProcessingModelChildDependency" );
296 qRegisterMetaType<QgsTextFormat>( "QgsTextFormat" );
297#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
298 QMetaType::registerComparators<QgsProcessingModelChildDependency>();
299 QMetaType::registerEqualsComparator<QgsProcessingFeatureSourceDefinition>();
300 QMetaType::registerEqualsComparator<QgsProperty>();
301 QMetaType::registerEqualsComparator<QgsDateTimeRange>();
302 QMetaType::registerEqualsComparator<QgsDateRange>();
303 QMetaType::registerEqualsComparator<QgsUnsetAttributeValue>();
304#endif
305 qRegisterMetaType<QPainter::CompositionMode>( "QPainter::CompositionMode" );
306 qRegisterMetaType<QgsDateTimeRange>( "QgsDateTimeRange" );
307 qRegisterMetaType<QList<QgsMapLayer *>>( "QList<QgsMapLayer*>" );
308 qRegisterMetaType<QMap<QNetworkRequest::Attribute, QVariant>>( "QMap<QNetworkRequest::Attribute,QVariant>" );
309 qRegisterMetaType<QMap<QNetworkRequest::KnownHeaders, QVariant>>( "QMap<QNetworkRequest::KnownHeaders,QVariant>" );
310 qRegisterMetaType<QList<QNetworkReply::RawHeaderPair>>( "QList<QNetworkReply::RawHeaderPair>" );
311 qRegisterMetaType< QAuthenticator * >( "QAuthenticator*" );
312 qRegisterMetaType< QgsGpsInformation >( "QgsGpsInformation" );
313 } );
314
315 ( void ) resolvePkgPath();
316
317 if ( ABISYM( mRunningFromBuildDir ) )
318 {
319 // we run from source directory - not installed to destination (specified prefix)
320 *sPrefixPath() = QString(); // set invalid path
321#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
322 setPluginPath( *sBuildOutputPath() + '/' + QString( QGIS_PLUGIN_SUBDIR ) + '/' + *sCfgIntDir() );
323#else
324 setPluginPath( *sBuildOutputPath() + '/' + QStringLiteral( QGIS_PLUGIN_SUBDIR ) );
325#endif
326 setPkgDataPath( *sBuildOutputPath() + QStringLiteral( "/data" ) ); // in buildDir/data - used for: doc, resources, svg
327 *sLibraryPath() = *sBuildOutputPath() + '/' + QGIS_LIB_SUBDIR + '/';
328#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
329 *sLibexecPath() = *sBuildOutputPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/' + *sCfgIntDir() + '/';
330#else
331 *sLibexecPath() = *sBuildOutputPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/';
332#endif
333#if defined( HAVE_QUICK )
334 *sQmlImportPath() = *sBuildOutputPath() + '/' + QGIS_QML_SUBDIR + '/';
335#endif
336 }
337 else
338 {
339 char *prefixPath = getenv( "QGIS_PREFIX_PATH" );
340 if ( !prefixPath )
341 {
342 if ( sPrefixPath()->isNull() )
343 {
344#if defined(Q_OS_MACX) || defined(Q_OS_WIN)
345 setPrefixPath( applicationDirPath(), true );
346#elif defined(ANDROID)
347 // this is "/data/data/org.qgis.qgis" in android
348 QDir myDir( QDir::homePath() );
349 myDir.cdUp();
350 QString myPrefix = myDir.absolutePath();
351 setPrefixPath( myPrefix, true );
352#else
353 QDir myDir( applicationDirPath() );
354 // Fix for server which is one level deeper in /usr/lib/cgi-bin
355 if ( applicationDirPath().contains( QStringLiteral( "cgi-bin" ) ) )
356 {
357 myDir.cdUp();
358 }
359 myDir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr
360 QString myPrefix = myDir.absolutePath();
361 setPrefixPath( myPrefix, true );
362#endif
363 }
364 }
365 else
366 {
367 setPrefixPath( prefixPath, true );
368 }
369 }
370
371 *sConfigPath() = profileFolder + '/'; // make sure trailing slash is included
372 *sDefaultSvgPaths() << qgisSettingsDirPath() + QStringLiteral( "svg/" );
373
374 *sAuthDbDirPath() = qgisSettingsDirPath();
375 if ( getenv( "QGIS_AUTH_DB_DIR_PATH" ) )
376 {
377 setAuthDatabaseDirPath( getenv( "QGIS_AUTH_DB_DIR_PATH" ) );
378 }
379
380 // force use of OpenGL renderer for Qt3d.
381 qputenv( "QT3D_RENDERER", "opengl" );
382
383 // store system environment variables passed to application, before they are adjusted
384 QMap<QString, QString> systemEnvVarMap;
385 QString passfile( QStringLiteral( "QGIS_AUTH_PASSWORD_FILE" ) ); // QString, for comparison
386
387 const auto systemEnvironment = QProcessEnvironment::systemEnvironment().toStringList();
388 for ( const QString &varStr : systemEnvironment )
389 {
390 int pos = varStr.indexOf( QLatin1Char( '=' ) );
391 if ( pos == -1 )
392 continue;
393 QString varStrName = varStr.left( pos );
394 QString varStrValue = varStr.mid( pos + 1 );
395 if ( varStrName != passfile )
396 {
397 systemEnvVarMap.insert( varStrName, varStrValue );
398 }
399 }
400 *sSystemEnvVars() = systemEnvVarMap;
401
402 // append local user-writable folder as a proj search path
403 QStringList currentProjSearchPaths = QgsProjUtils::searchPaths();
404 currentProjSearchPaths.append( qgisSettingsDirPath() + QStringLiteral( "proj" ) );
405#ifdef Q_OS_MACX
406 // append bundled proj lib for MacOS
407 QString projLib( QDir::cleanPath( pkgDataPath().append( "/proj" ) ) );
408 if ( QFile::exists( projLib ) )
409 {
410 currentProjSearchPaths.append( projLib );
411 }
412#endif // Q_OS_MACX
413
414 char **newPaths = new char *[currentProjSearchPaths.length()];
415 for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
416 {
417 newPaths[i] = CPLStrdup( currentProjSearchPaths.at( i ).toUtf8().constData() );
418 }
419 proj_context_set_search_paths( nullptr, currentProjSearchPaths.count(), newPaths );
420 for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
421 {
422 CPLFree( newPaths[i] );
423 }
424 delete [] newPaths;
425
426 // allow Qt to search for Qt plugins (e.g. sqldrivers) in our plugin directory
427 QCoreApplication::addLibraryPath( pluginPath() );
428
429 {
430 QgsScopedRuntimeProfile profile( tr( "Load user fonts" ) );
432 }
433
434 // set max. thread count to -1
435 // this should be read from QgsSettings but we don't know where they are at this point
436 // so we read actual value in main.cpp
437 ABISYM( sMaxThreads ) = -1;
438
439 {
440 QgsScopedRuntimeProfile profile( tr( "Load color schemes" ) );
443 }
444
445 {
446 QgsScopedRuntimeProfile profile( tr( "Load bookmarks" ) );
448 }
449
450 // trigger creation of default style
451 QgsStyle *defaultStyle = QgsStyle::defaultStyle();
452 if ( !members()->mStyleModel )
453 members()->mStyleModel = new QgsStyleModel( defaultStyle );
454
455 ABISYM( mInitialized ) = true;
456}
457
458
459void QgsApplication::installTranslators()
460{
461 if ( *sTranslation() != QLatin1String( "C" ) )
462 {
463 mQgisTranslator = new QTranslator( this );
464 if ( mQgisTranslator->load( QStringLiteral( "qgis_" ) + *sTranslation(), i18nPath() ) )
465 {
466 installTranslator( mQgisTranslator );
467 }
468 else
469 {
470 QgsDebugMsgLevel( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), *sTranslation() ), 2 );
471 }
472
473 /* Translation file for Qt.
474 * The strings from the QMenuBar context section are used by Qt/Mac to shift
475 * the About, Preferences and Quit items to the Mac Application menu.
476 * These items must be translated identically in both qt_ and qgis_ files.
477 */
478 QString qtTranslationsPath = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
479#ifdef __MINGW32__
480 QString prefix = QDir( QString( "%1/../" ).arg( QApplication::applicationDirPath() ) ).absolutePath();
481 qtTranslationsPath = prefix + qtTranslationsPath.mid( QLibraryInfo::location( QLibraryInfo::PrefixPath ).length() );
482#endif
483
484 mQtTranslator = new QTranslator( this );
485 if ( mQtTranslator->load( QStringLiteral( "qt_" ) + *sTranslation(), qtTranslationsPath ) )
486 {
487 installTranslator( mQtTranslator );
488 }
489 else
490 {
491 QgsDebugMsgLevel( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
492 }
493
494 mQtBaseTranslator = new QTranslator( this );
495 if ( mQtBaseTranslator->load( QStringLiteral( "qtbase_" ) + *sTranslation(), qtTranslationsPath ) )
496 {
497 installTranslator( mQtBaseTranslator );
498 }
499 else
500 {
501 QgsDebugMsgLevel( QStringLiteral( "loading of qtbase translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
502 }
503 }
504}
505
507{
508 if ( mApplicationMembers )
509 mApplicationMembers->mSettingsRegistryCore->backwardCompatibility();
510
511 delete mDataItemProviderRegistry;
512 delete mApplicationMembers;
513 delete mQgisTranslator;
514 delete mQtTranslator;
515 delete mQtBaseTranslator;
516
517 // we do this here as well as in exitQgis() -- it's safe to call as often as we want,
518 // and there's just a *chance* that someone hasn't properly called exitQgis prior to
519 // this destructor...
520 invalidateCaches();
521}
522
523void QgsApplication::invalidateCaches()
524{
525 // invalidate coordinate cache while the PROJ context held by the thread-locale
526 // QgsProjContextStore object is still alive. Otherwise if this later object
527 // is destroyed before the static variables of the cache, we might use freed memory.
531}
532
534{
535 return qobject_cast<QgsApplication *>( QCoreApplication::instance() );
536}
537
538bool QgsApplication::event( QEvent *event )
539{
540 bool done = false;
541 if ( event->type() == QEvent::FileOpen )
542 {
543 // handle FileOpen event (double clicking a file icon in Mac OS X Finder)
544 if ( ABISYM( mFileOpenEventReceiver ) )
545 {
546 // Forward event to main window.
547 done = notify( ABISYM( mFileOpenEventReceiver ), event );
548 }
549 else
550 {
551 // Store filename because receiver has not registered yet.
552 // If QGIS has been launched by double clicking a file icon, FileOpen will be
553 // the first event; the main window is not yet ready to handle the event.
554 sFileOpenEventList()->append( static_cast<QFileOpenEvent *>( event )->file() );
555 done = true;
556 }
557 }
558 else
559 {
560 // pass other events to base class
561 done = QApplication::event( event );
562 }
563 return done;
564}
565
566bool QgsApplication::notify( QObject *receiver, QEvent *event )
567{
568 bool done = false;
569 // Crashes in customization (especially on Mac), if we're not in the main/UI thread, see #5597
570 if ( thread() == receiver->thread() )
571 emit preNotify( receiver, event, &done );
572
573 if ( done )
574 return true;
575
576 // Send event to receiver and catch unhandled exceptions
577 done = true;
578 try
579 {
580 done = QApplication::notify( receiver, event );
581 }
582 catch ( QgsException &e )
583 {
584 qCritical() << "Caught unhandled QgsException: " << e.what();
585 if ( qApp->thread() == QThread::currentThread() )
586 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
587 }
588 catch ( std::exception &e )
589 {
590 qCritical() << "Caught unhandled std::exception: " << e.what();
591 if ( qApp->thread() == QThread::currentThread() )
592 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
593 }
594 catch ( ... )
595 {
596 qCritical() << "Caught unhandled unknown exception";
597 if ( qApp->thread() == QThread::currentThread() )
598 QMessageBox::critical( activeWindow(), tr( "Exception" ), tr( "unknown exception" ) );
599 }
600
601 return done;
602}
603
605{
606 return QgsRuntimeProfiler::threadLocalInstance();
607}
608
610{
611 // Set receiver for FileOpen events
612 ABISYM( mFileOpenEventReceiver ) = receiver;
613 // Propagate any events collected before the receiver has registered.
614 if ( sFileOpenEventList()->count() > 0 )
615 {
616 const QStringList fileOpenEventList = *sFileOpenEventList();
617 for ( const QString &file : fileOpenEventList )
618 {
619 QFileOpenEvent foe( file );
620 QgsApplication::sendEvent( ABISYM( mFileOpenEventReceiver ), &foe );
621 }
622 sFileOpenEventList()->clear();
623 }
624}
625
626void QgsApplication::setPrefixPath( const QString &prefixPath, bool useDefaultPaths )
627{
628 *sPrefixPath() = prefixPath;
629#if defined(Q_OS_WIN)
630 if ( sPrefixPath()->endsWith( "/bin" ) )
631 {
632 sPrefixPath()->chop( 4 );
633 }
634#endif
635 if ( useDefaultPaths && !ABISYM( mRunningFromBuildDir ) )
636 {
637 setPluginPath( *sPrefixPath() + '/' + QStringLiteral( QGIS_PLUGIN_SUBDIR ) );
638 setPkgDataPath( *sPrefixPath() + '/' + QStringLiteral( QGIS_DATA_SUBDIR ) );
639 }
640 *sLibraryPath() = *sPrefixPath() + '/' + QGIS_LIB_SUBDIR + '/';
641 *sLibexecPath() = *sPrefixPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/';
642#if defined( HAVE_QUICK )
643 *sQmlImportPath() = *sPrefixPath() + '/' + QGIS_QML_SUBDIR + '/';
644#endif
645}
646
647void QgsApplication::setPluginPath( const QString &pluginPath )
648{
649 *sPluginPath() = pluginPath;
650}
651
652void QgsApplication::setPkgDataPath( const QString &pkgDataPath )
653{
654 *sPkgDataPath() = pkgDataPath;
655
656 QString mySvgPath = pkgDataPath + QStringLiteral( "/svg/" );
657
658 // avoid duplicate entries
659 if ( !sDefaultSvgPaths()->contains( mySvgPath ) )
660 *sDefaultSvgPaths() << mySvgPath;
661}
662
663void QgsApplication::setDefaultSvgPaths( const QStringList &pathList )
664{
665 *sDefaultSvgPaths() = pathList;
666}
667
668void QgsApplication::setAuthDatabaseDirPath( const QString &authDbDirPath )
669{
670 QFileInfo fi( authDbDirPath );
671 if ( fi.exists() && fi.isDir() && fi.isWritable() )
672 {
673 *sAuthDbDirPath() = fi.canonicalFilePath() + QDir::separator();
674 }
675}
676
678{
679#if 0
680 if ( ABISYM( mRunningFromBuildDir ) )
681 {
682 static bool sOnce = true;
683 if ( sOnce )
684 {
685 QgsMessageLogNotifyBlocker blockNotifications;
686 ( void ) blockNotifications;
687 qWarning( "!!! prefix path was requested, but it is not valid - we do not run from installed path !!!" );
688 }
689 sOnce = false;
690 }
691#endif
692
693 return *sPrefixPath();
694}
696{
697 return *sPluginPath();
698}
699
701{
702 if ( sPkgDataPath()->isNull() )
703 return resolvePkgPath();
704 else
705 return *sPkgDataPath();
706}
707
709{
710 return QStringLiteral( ":/images/themes/default/" );
711}
713{
714 QString usersThemes = userThemesFolder() + QDir::separator() + themeName() + QDir::separator() + "icons/";
715 QDir dir( usersThemes );
716 if ( dir.exists() )
717 {
718 return usersThemes;
719 }
720 else
721 {
722 QString defaultThemes = defaultThemesFolder() + QDir::separator() + themeName() + QDir::separator() + "icons/";
723 return defaultThemes;
724 }
725}
726
728{
729 return iconsPath() + QStringLiteral( "qgis-icon-60x60.png" );
730}
731
733{
734 return ABISYM( sMaxThreads );
735}
736
737QString QgsApplication::iconPath( const QString &iconFile )
738{
739 // try active theme
740 QString path = activeThemePath();
741 if ( QFile::exists( path + iconFile ) )
742 return path + iconFile;
743
744 // use default theme
745 return defaultThemePath() + iconFile;
746}
747
748QIcon QgsApplication::getThemeIcon( const QString &name, const QColor &fillColor, const QColor &strokeColor )
749{
750 const QString cacheKey = ( name.startsWith( '/' ) ? name.mid( 1 ) : name )
751 + ( fillColor.isValid() ? QStringLiteral( "_%1" ).arg( fillColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() )
752 + ( strokeColor.isValid() ? QStringLiteral( "_%1" ).arg( strokeColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() );
753 QgsApplication *app = instance();
754 if ( app && app->mIconCache.contains( cacheKey ) )
755 return app->mIconCache.value( cacheKey );
756
757 QIcon icon;
758 const bool colorBased = fillColor.isValid() || strokeColor.isValid();
759
760 auto iconFromColoredSvg = [ = ]( const QString & path ) -> QIcon
761 {
762 // sizes are unused here!
763 const QByteArray svgContent = QgsApplication::svgCache()->svgContent( path, 16, fillColor, strokeColor, 1, 1 );
764
765 const QString iconPath = sIconCacheDir()->filePath( cacheKey + QStringLiteral( ".svg" ) );
766 QFile f( iconPath );
767 if ( f.open( QFile::WriteOnly | QFile::Truncate ) )
768 {
769 f.write( svgContent );
770 f.close();
771 }
772 else
773 {
774 QgsDebugError( QStringLiteral( "Could not create colorized icon svg at %1" ).arg( iconPath ) );
775 return QIcon();
776 }
777
778 return QIcon( f.fileName() );
779 };
780
781 QString preferredPath = activeThemePath() + QDir::separator() + name;
782 QString defaultPath = defaultThemePath() + QDir::separator() + name;
783 if ( QFile::exists( preferredPath ) )
784 {
785 if ( colorBased )
786 {
787 icon = iconFromColoredSvg( preferredPath );
788 }
789 else
790 {
791 icon = QIcon( preferredPath );
792 }
793 }
794 else if ( QFile::exists( defaultPath ) )
795 {
796 //could still return an empty icon if it
797 //doesn't exist in the default theme either!
798 if ( colorBased )
799 {
800 icon = iconFromColoredSvg( defaultPath );
801 }
802 else
803 {
804 icon = QIcon( defaultPath );
805 }
806 }
807 else
808 {
809 icon = QIcon();
810 }
811
812 if ( app )
813 app->mIconCache.insert( cacheKey, icon );
814 return icon;
815}
816
818{
819 QgsApplication *app = instance();
820 if ( app && app->mCursorCache.contains( cursor ) )
821 return app->mCursorCache.value( cursor );
822
823 // All calculations are done on 32x32 icons
824 // Defaults to center, individual cursors may override
825 int activeX = 16;
826 int activeY = 16;
827
828 QString name;
829 switch ( cursor )
830 {
831 case ZoomIn:
832 name = QStringLiteral( "mZoomIn.svg" );
833 activeX = 13;
834 activeY = 13;
835 break;
836 case ZoomOut:
837 name = QStringLiteral( "mZoomOut.svg" );
838 activeX = 13;
839 activeY = 13;
840 break;
841 case Identify:
842 activeX = 3;
843 activeY = 6;
844 name = QStringLiteral( "mIdentify.svg" );
845 break;
846 case CrossHair:
847 name = QStringLiteral( "mCrossHair.svg" );
848 break;
849 case CapturePoint:
850 name = QStringLiteral( "mCapturePoint.svg" );
851 break;
852 case Select:
853 name = QStringLiteral( "mSelect.svg" );
854 activeX = 6;
855 activeY = 6;
856 break;
857 case Sampler:
858 activeX = 5;
859 activeY = 5;
860 name = QStringLiteral( "mSampler.svg" );
861 break;
862 // No default
863 }
864 // It should never get here!
865 Q_ASSERT( ! name.isEmpty( ) );
866
867 QIcon icon = getThemeIcon( QStringLiteral( "cursors" ) + QDir::separator() + name );
868 QCursor cursorIcon;
869 // Check if an icon exists for this cursor (the O.S. default cursor will be used if it does not)
870 if ( ! icon.isNull( ) )
871 {
872 // Apply scaling
873 float scale = Qgis::UI_SCALE_FACTOR * QgsApplication::fontMetrics().height() / 32.0;
874 cursorIcon = QCursor( icon.pixmap( std::ceil( scale * 32 ), std::ceil( scale * 32 ) ), std::ceil( scale * activeX ), std::ceil( scale * activeY ) );
875 }
876 if ( app )
877 app->mCursorCache.insert( cursor, cursorIcon );
878 return cursorIcon;
879}
880
881// TODO: add some caching mechanism ?
882QPixmap QgsApplication::getThemePixmap( const QString &name, const QColor &foreColor, const QColor &backColor, const int size )
883{
884 const QString preferredPath = activeThemePath() + QDir::separator() + name;
885 const QString defaultPath = defaultThemePath() + QDir::separator() + name;
886 const QString path = QFile::exists( preferredPath ) ? preferredPath : defaultPath;
887 if ( foreColor.isValid() || backColor.isValid() )
888 {
889 bool fitsInCache = false;
890 const QImage image = svgCache()->svgAsImage( path, size, backColor, foreColor, 1, 1, fitsInCache );
891 return QPixmap::fromImage( image );
892 }
893
894 return QPixmap( path );
895}
896
897void QgsApplication::setThemeName( const QString &themeName )
898{
899 *sThemeName() = themeName;
900}
901
903{
904 static QString appPath;
905 if ( appPath.isNull() )
906 {
907 if ( QCoreApplication::instance() )
908 {
909 appPath = applicationDirPath();
910 }
911 else
912 {
913 qWarning( "Application path not initialized" );
914 }
915 }
916
917 if ( !appPath.isNull() || getenv( "QGIS_PREFIX_PATH" ) )
918 {
919 QString prefix = getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : appPath;
920
921 // check if QGIS is run from build directory (not the install directory)
922 QFile f;
923 // "/../../.." is for Mac bundled app in build directory
924 static const QStringList paths { QStringList() << QString() << QStringLiteral( "/.." ) << QStringLiteral( "/bin" ) << QStringLiteral( "/../../.." ) };
925 for ( const QString &path : paths )
926 {
927 f.setFileName( prefix + path + "/qgisbuildpath.txt" );
928 if ( f.exists() )
929 break;
930 }
931 if ( f.exists() && f.open( QIODevice::ReadOnly ) )
932 {
933 ABISYM( mRunningFromBuildDir ) = true;
934 *sBuildSourcePath() = f.readLine().trimmed();
935 *sBuildOutputPath() = f.readLine().trimmed();
936 QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 );
937 QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( sBuildSourcePath()->toUtf8().constData() ), 4 );
938 QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( sBuildOutputPath()->toUtf8().constData() ), 4 );
939#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
940#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
941 *sCfgIntDir() = prefix.split( '/', QString::SkipEmptyParts ).last();
942#else
943 *sCfgIntDir() = prefix.split( '/', Qt::SkipEmptyParts ).last();
944#endif
945 qDebug( "- cfg: %s", sCfgIntDir()->toUtf8().constData() );
946#endif
947 }
948 }
949
950 QString prefixPath;
951 if ( getenv( "QGIS_PREFIX_PATH" ) )
952 prefixPath = getenv( "QGIS_PREFIX_PATH" );
953 else
954 {
955#if defined(ANDROID)
956 // this is "/data/data/org.qgis.qgis" in android
957 QDir dir( QDir::homePath() );
958 dir.cdUp();
959 prefixPath = dir.absolutePath();
960#else
961
962#if defined(Q_OS_MACX)
963 prefixPath = appPath;
964#elif defined(Q_OS_WIN)
965 prefixPath = appPath;
966 if ( prefixPath.endsWith( "/bin" ) )
967 prefixPath.chop( 4 );
968#else
969 QDir dir( appPath );
970 // Fix for server which is one level deeper in /usr/lib/cgi-bin
971 if ( appPath.contains( QStringLiteral( "cgi-bin" ) ) )
972 {
973 dir.cdUp();
974 }
975 dir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr
976 prefixPath = dir.absolutePath();
977#endif
978#endif
979 }
980
981 if ( ABISYM( mRunningFromBuildDir ) )
982 return *sBuildOutputPath() + QStringLiteral( "/data" );
983 else
984 return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR );
985}
986
988{
989 return *sThemeName();
990}
991
992void QgsApplication::setUITheme( const QString &themeName )
993{
994 // Loop all style sheets, find matching name, load it.
995 QHash<QString, QString> themes = QgsApplication::uiThemes();
996 if ( themeName == QLatin1String( "default" ) || !themes.contains( themeName ) )
997 {
998 setThemeName( QStringLiteral( "default" ) );
999 qApp->setStyleSheet( QString() );
1000 return;
1001 }
1002
1003 QString path = themes.value( themeName );
1004 QString stylesheetname = path + "/style.qss";
1005
1006 QFile file( stylesheetname );
1007 QFile variablesfile( path + "/variables.qss" );
1008
1009 QFileInfo variableInfo( variablesfile );
1010
1011 if ( !file.open( QIODevice::ReadOnly ) || ( variableInfo.exists() && !variablesfile.open( QIODevice::ReadOnly ) ) )
1012 {
1013 return;
1014 }
1015
1016 QString styledata = file.readAll();
1017 styledata.replace( QLatin1String( "@theme_path" ), path );
1018
1019 if ( variableInfo.exists() )
1020 {
1021 QTextStream in( &variablesfile );
1022 while ( !in.atEnd() )
1023 {
1024 QString line = in.readLine();
1025 // This is a variable
1026 if ( line.startsWith( '@' ) )
1027 {
1028 int index = line.indexOf( ':' );
1029 QString name = line.mid( 0, index );
1030 QString value = line.mid( index + 1, line.length() );
1031 styledata.replace( name, value );
1032 }
1033 }
1034 variablesfile.close();
1035 }
1036 file.close();
1037
1038 if ( Qgis::UI_SCALE_FACTOR != 1.0 )
1039 {
1040 // apply OS-specific UI scale factor to stylesheet's em values
1041 int index = 0;
1042 const static QRegularExpression regex( QStringLiteral( "(?<=[\\s:])([0-9\\.]+)(?=em)" ) );
1043 QRegularExpressionMatch match = regex.match( styledata, index );
1044 while ( match.hasMatch() )
1045 {
1046 index = match.capturedStart();
1047 styledata.remove( index, match.captured( 0 ).length() );
1048 QString number = QString::number( match.captured( 0 ).toDouble() * Qgis::UI_SCALE_FACTOR );
1049 styledata.insert( index, number );
1050 index += number.length();
1051 match = regex.match( styledata, index );
1052 }
1053 }
1054
1055 qApp->setStyleSheet( styledata );
1056
1057 QFile palettefile( path + "/palette.txt" );
1058 QFileInfo paletteInfo( palettefile );
1059 if ( paletteInfo.exists() && palettefile.open( QIODevice::ReadOnly ) )
1060 {
1061 QPalette pal = qApp->palette();
1062 QTextStream in( &palettefile );
1063 while ( !in.atEnd() )
1064 {
1065 QString line = in.readLine();
1066 QStringList parts = line.split( ':' );
1067 if ( parts.count() == 2 )
1068 {
1069 int role = parts.at( 0 ).trimmed().toInt();
1070 QColor color = QgsSymbolLayerUtils::decodeColor( parts.at( 1 ).trimmed() );
1071 pal.setColor( static_cast< QPalette::ColorRole >( role ), color );
1072 }
1073 }
1074 palettefile.close();
1075 qApp->setPalette( pal );
1076 }
1077
1079}
1080
1081QHash<QString, QString> QgsApplication::uiThemes()
1082{
1083 QStringList paths = QStringList() << userThemesFolder() << defaultThemesFolder();
1084 QHash<QString, QString> mapping;
1085 mapping.insert( QStringLiteral( "default" ), QString() );
1086 const auto constPaths = paths;
1087 for ( const QString &path : constPaths )
1088 {
1089 QDir folder( path );
1090 QFileInfoList styleFiles = folder.entryInfoList( QDir::Dirs | QDir::NoDotAndDotDot );
1091 const auto constStyleFiles = styleFiles;
1092 for ( const QFileInfo &info : constStyleFiles )
1093 {
1094 QFileInfo styleFile( info.absoluteFilePath() + "/style.qss" );
1095 if ( !styleFile.exists() )
1096 continue;
1097
1098 QString name = info.baseName();
1099 QString path = info.absoluteFilePath();
1100 mapping.insert( name, path );
1101 }
1102 }
1103 return mapping;
1104}
1105
1107{
1108 return pkgDataPath() + QStringLiteral( "/doc/AUTHORS" );
1109}
1110
1112{
1113 return pkgDataPath() + QStringLiteral( "/doc/CONTRIBUTORS" );
1114}
1116{
1117 return pkgDataPath() + QStringLiteral( "/doc/developersmap.html" );
1118}
1119
1121{
1122 return pkgDataPath() + QStringLiteral( "/doc/SPONSORS" );
1123}
1124
1126{
1127 return pkgDataPath() + QStringLiteral( "/doc/DONORS" );
1128}
1129
1131{
1132 return pkgDataPath() + QStringLiteral( "/doc/TRANSLATORS" );
1133}
1134
1136{
1137 return pkgDataPath() + QStringLiteral( "/doc/LICENSE" );
1138}
1139
1141{
1142 if ( ABISYM( mRunningFromBuildDir ) )
1143 return *sBuildOutputPath() + QStringLiteral( "/i18n/" );
1144 else
1145 return pkgDataPath() + QStringLiteral( "/i18n/" );
1146}
1147
1149{
1150 return pkgDataPath() + QStringLiteral( "/resources/metadata-ISO/" );
1151}
1152
1154{
1155 return pkgDataPath() + QStringLiteral( "/resources/qgis.db" );
1156}
1157
1159{
1160 return *sConfigPath();
1161}
1162
1164{
1165 return qgisSettingsDirPath() + QStringLiteral( "qgis.db" );
1166}
1167
1169{
1170 return *sAuthDbDirPath() + QStringLiteral( "qgis-auth.db" );
1171}
1172
1174{
1175 return QStringLiteral( ":/images/splash/" );
1176}
1177
1179{
1180 return pkgDataPath() + QStringLiteral( "/images/icons/" );
1181}
1182
1184{
1185 if ( ABISYM( mRunningFromBuildDir ) )
1186 {
1187 QString tempCopy = QDir::tempPath() + "/srs6.db";
1188
1189 if ( !QFile( tempCopy ).exists() )
1190 {
1191 QFile f( buildSourcePath() + "/resources/srs6.db" );
1192 if ( !f.copy( tempCopy ) )
1193 {
1194 qFatal( "Could not create temporary copy" );
1195 }
1196 }
1197
1198 return tempCopy;
1199 }
1200 else
1201 {
1202 return pkgDataPath() + QStringLiteral( "/resources/srs.db" );
1203 }
1204}
1205
1206void QgsApplication::setSvgPaths( const QStringList &svgPaths )
1207{
1209 members()->mSvgPathCacheValid = false;
1210}
1211
1213{
1214 static QReadWriteLock lock;
1215
1217
1218 if ( members()->mSvgPathCacheValid )
1219 {
1220 return members()->mSvgPathCache;
1221 }
1222 else
1223 {
1225 //local directories to search when looking for an SVG with a given basename
1226 //defined by user in options dialog
1227 const QStringList pathList = settingsSearchPathsForSVG->value();
1228
1229 // maintain user set order while stripping duplicates
1230 QStringList paths;
1231 for ( const QString &path : pathList )
1232 {
1233 if ( !paths.contains( path ) )
1234 paths.append( path );
1235 }
1236 for ( const QString &path : std::as_const( *sDefaultSvgPaths() ) )
1237 {
1238 if ( !paths.contains( path ) )
1239 paths.append( path );
1240 }
1241 members()->mSvgPathCache = paths;
1242
1243 return paths;
1244 }
1245}
1246
1248{
1249 //local directories to search when looking for an template with a given basename
1250 //defined by user in options dialog
1252}
1253
1254QMap<QString, QString> QgsApplication::systemEnvVars()
1255{
1256 return *sSystemEnvVars();
1257}
1258
1260{
1261 return qgisSettingsDirPath() + QStringLiteral( "symbology-style.db" );
1262}
1263
1265{
1266 const thread_local QRegularExpression regexp( QRegularExpression::anchoredPattern( QStringLiteral( "^[A-Za-z][A-Za-z0-9\\._-]*" ) ) );
1267 return regexp;
1268}
1269
1271{
1272 if ( !sUserName()->isEmpty() )
1273 return *sUserName();
1274
1275#ifdef _MSC_VER
1276 TCHAR name [ UNLEN + 1 ];
1277 DWORD size = UNLEN + 1;
1278
1279 if ( GetUserName( ( TCHAR * )name, &size ) )
1280 {
1281#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1282 *sUserName() = QString::fromLocal8Bit( name );
1283#else
1284 *sUserName() = QString::fromWCharArray( name );
1285#endif
1286 }
1287
1288
1289#elif QT_CONFIG(process)
1290 QProcess process;
1291
1292 process.start( QStringLiteral( "whoami" ), QStringList() );
1293 process.waitForFinished();
1294 *sUserName() = process.readAllStandardOutput().trimmed();
1295#endif
1296
1297 if ( !sUserName()->isEmpty() )
1298 return *sUserName();
1299
1300 //backup plan - use environment variables
1301 *sUserName() = qgetenv( "USER" );
1302 if ( !sUserName()->isEmpty() )
1303 return *sUserName();
1304
1305 //last resort
1306 *sUserName() = qgetenv( "USERNAME" );
1307 return *sUserName();
1308}
1309
1311{
1312 if ( !sUserFullName()->isEmpty() )
1313 return *sUserFullName();
1314
1315#ifdef _MSC_VER
1316 TCHAR name [ UNLEN + 1 ];
1317 DWORD size = UNLEN + 1;
1318
1319 //note - this only works for accounts connected to domain
1320 if ( GetUserNameEx( NameDisplay, ( TCHAR * )name, &size ) )
1321 {
1322#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1323 *sUserFullName() = QString::fromLocal8Bit( name );
1324#else
1325 *sUserFullName() = QString::fromWCharArray( name );
1326#endif
1327 }
1328
1329 //fall back to login name
1330 if ( sUserFullName()->isEmpty() )
1331 *sUserFullName() = userLoginName();
1332#elif defined(Q_OS_ANDROID) || defined(__MINGW32__)
1333 *sUserFullName() = QStringLiteral( "Not available" );
1334#else
1335 struct passwd *p = getpwuid( getuid() );
1336
1337 if ( p )
1338 {
1339 QString gecosName = QString( p->pw_gecos );
1340 *sUserFullName() = gecosName.left( gecosName.indexOf( ',', 0 ) );
1341 }
1342
1343#endif
1344
1345 return *sUserFullName();
1346}
1347
1349{
1350#if defined(Q_OS_ANDROID)
1351 return QLatin1String( "android" );
1352#elif defined(Q_OS_MAC)
1353 return QLatin1String( "osx" );
1354#elif defined(Q_OS_WIN)
1355 return QLatin1String( "windows" );
1356#elif defined(Q_OS_LINUX)
1357 return QStringLiteral( "linux" );
1358#elif defined(Q_OS_FREEBSD)
1359 return QStringLiteral( "freebsd" );
1360#elif defined(Q_OS_OPENBSD)
1361 return QStringLiteral( "openbsd" );
1362#elif defined(Q_OS_NETBSD)
1363 return QStringLiteral( "netbsd" );
1364#elif defined(Q_OS_UNIX)
1365 return QLatin1String( "unix" );
1366#else
1367 return QLatin1String( "unknown" );
1368#endif
1369}
1370
1372{
1373#if defined(Q_OS_ANDROID)
1374 return -1;
1375#elif defined(Q_OS_MAC)
1376 return -1;
1377#elif defined(Q_OS_WIN)
1378 MEMORYSTATUSEX memoryStatus;
1379 ZeroMemory( &memoryStatus, sizeof( MEMORYSTATUSEX ) );
1380 memoryStatus.dwLength = sizeof( MEMORYSTATUSEX );
1381 if ( GlobalMemoryStatusEx( &memoryStatus ) )
1382 {
1383 return memoryStatus.ullTotalPhys / ( 1024 * 1024 );
1384 }
1385 else
1386 {
1387 return -1;
1388 }
1389#elif defined(Q_OS_LINUX)
1390 constexpr int megabyte = 1024 * 1024;
1391 struct sysinfo si;
1392 sysinfo( &si );
1393 return si.totalram / megabyte;
1394#elif defined(Q_OS_FREEBSD)
1395 return -1;
1396#elif defined(Q_OS_OPENBSD)
1397 return -1;
1398#elif defined(Q_OS_NETBSD)
1399 return -1;
1400#elif defined(Q_OS_UNIX)
1401 return -1;
1402#else
1403 return -1;
1404#endif
1405}
1406
1408{
1409 return *sPlatformName();
1410}
1411
1413{
1414 if ( !sApplicationFullName()->isEmpty() )
1415 return *sApplicationFullName();
1416
1417 //use environment variables
1418 *sApplicationFullName() = qgetenv( "QGIS_APPLICATION_FULL_NAME" );
1419 if ( !sApplicationFullName()->isEmpty() )
1420 return *sApplicationFullName();
1421
1422 //last resort
1423 QgsSettings settings;
1424 *sApplicationFullName() = settings.value(
1425 QStringLiteral( "/qgis/application_full_name" ),
1426 QStringLiteral( "%1 %2" ).arg( applicationName(), platform() )
1427 ).toString();
1428 return *sApplicationFullName();
1429}
1430
1432{
1434 {
1436 // don't differentiate en_US and en_GB
1437 if ( locale.startsWith( QLatin1String( "en" ), Qt::CaseInsensitive ) )
1438 {
1439 return locale.left( 2 );
1440 }
1441
1442 return locale;
1443 }
1444 else
1445 {
1446 return QLocale().name().left( 2 );
1447 }
1448}
1449
1450void QgsApplication::setLocale( const QLocale &locale )
1451{
1452 QLocale::setDefault( locale );
1453 emit instance()->localeChanged();
1454}
1455
1457{
1458 return qgisSettingsDirPath() + QStringLiteral( "/themes" );
1459}
1460
1462{
1463 return pkgDataPath() + QStringLiteral( "/resources/symbology-style.xml" );
1464}
1465
1467{
1468 return pkgDataPath() + QStringLiteral( "/resources/themes" );
1469}
1470
1472{
1473 return pkgDataPath() + QStringLiteral( "/resources/server/" );
1474}
1475
1477{
1478 return *sLibraryPath();
1479}
1480
1482{
1483 return *sLibexecPath();
1484}
1485
1487{
1488 return *sQmlImportPath();
1489}
1490
1492{
1493 return ( htonl( 1 ) == 1 ) ? XDR : NDR;
1494}
1495
1497{
1498 if ( !ABISYM( mInitialized ) && QgsApplication::instance() )
1499 {
1500 init( *sProfilePath() );
1501 }
1502
1503 // set the provider plugin path (this creates provider registry)
1505
1506 // create data item provider registry
1508
1509 // create project instance if doesn't exist
1511
1512 // Initialize authentication manager and connect to database
1514
1515 // Make sure we have a NAM created on the main thread.
1516 // Note that this might call QgsApplication::authManager to
1517 // setup the proxy configuration that's why it needs to be
1518 // called after the QgsAuthManager instance has been created
1520
1521}
1522
1524{
1525 if ( auto *lInstance = instance() )
1526 {
1527 if ( !lInstance->mAuthManager )
1528 {
1529 lInstance->mAuthManager = QgsAuthManager::instance();
1530 }
1531 return lInstance->mAuthManager;
1532 }
1533 else
1534 {
1535 // no QgsApplication instance
1536 if ( !sAuthManager )
1537 sAuthManager = QgsAuthManager::instance();
1538 return sAuthManager;
1539 }
1540}
1541
1542
1544{
1545 // make sure all threads are done before exiting
1546 QThreadPool::globalInstance()->waitForDone();
1547
1548 // don't create to delete
1549 if ( auto *lInstance = instance() )
1550 delete lInstance->mAuthManager;
1551 else
1552 delete sAuthManager;
1553
1554 //Ensure that all remaining deleteLater QObjects are actually deleted before we exit.
1555 QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
1556
1557 //delete all registered functions from expression engine (see above comment)
1559
1560 // avoid creating instance just to delete it!
1561 if ( QgsProject::sProject )
1562 delete QgsProject::instance();
1563
1564 //Ensure that providers/layers which called deleteLater on objects as part of their cleanup
1565 //result in fully deleted objects before we do the provider registry cleanup.
1566 //E.g. the QgsOgrConnPool instance has deleteLater calls when unrefing layers, so clearing
1567 //the project above has not yet fully cleaned up OGR objects, which we MUST do before
1568 //cleaning up the provider
1569 QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
1570
1571 // avoid creating instance just to delete it!
1572 if ( QgsProviderRegistry::exists() )
1574
1575 invalidateCaches();
1576
1578
1579 // tear-down GDAL/OGR
1580 OGRCleanupAll();
1581 GDALDestroyDriverManager();
1582}
1583
1585{
1586 QString myEnvironmentVar( getenv( "QGIS_PREFIX_PATH" ) );
1587 QString myState = tr( "Application state:\n"
1588 "QGIS_PREFIX_PATH env var:\t\t%1\n"
1589 "Prefix:\t\t%2\n"
1590 "Plugin Path:\t\t%3\n"
1591 "Package Data Path:\t%4\n"
1592 "Active Theme Name:\t%5\n"
1593 "Active Theme Path:\t%6\n"
1594 "Default Theme Path:\t%7\n"
1595 "SVG Search Paths:\t%8\n"
1596 "User DB Path:\t%9\n"
1597 "Auth DB Path:\t%10\n" )
1598 .arg( myEnvironmentVar,
1599 prefixPath(),
1600 pluginPath(),
1601 pkgDataPath(),
1602 themeName(),
1605 svgPaths().join( tr( "\n\t\t", "match indentation of application state" ) ),
1607 .arg( qgisAuthDatabaseFilePath() );
1608 return myState;
1609}
1610
1612{
1613 //
1614 // Make the style sheet desktop preferences aware by using qapplication
1615 // palette as a basis for colors where appropriate
1616 //
1617 // QColor myColor1 = palette().highlight().color();
1618 QColor myColor1( Qt::lightGray );
1619 QColor myColor2 = myColor1;
1620 myColor2 = myColor2.lighter( 110 ); //10% lighter
1621 QString myStyle;
1622 myStyle = QStringLiteral( ".overview{"
1623 " font: 1.82em;"
1624 " font-weight: bold;"
1625 "}"
1626 "body{"
1627 " background: white;"
1628 " color: black;"
1629 " font-family: 'Lato', 'Open Sans', 'Lucida Grande', 'Segoe UI', 'Arial', sans-serif;"
1630 " width: 100%;"
1631 "}"
1632 "h1{ background-color: #F6F6F6;"
1633 " color: #589632; " // from http://qgis.org/en/site/getinvolved/styleguide.html
1634 " font-size: x-large; "
1635 " font-weight: normal;"
1636 " background: none;"
1637 " padding: 0.75em 0 0;"
1638 " margin: 0;"
1639 " line-height: 3em;"
1640 "}"
1641 "h2{ background-color: #F6F6F6;"
1642 " color: #589632; " // from http://qgis.org/en/site/getinvolved/styleguide.html
1643 " font-size: medium; "
1644 " font-weight: normal;"
1645 " background: none;"
1646 " padding: 0.75em 0 0;"
1647 " margin: 0;"
1648 " line-height: 1.1em;"
1649 "}"
1650 "h3{ background-color: #F6F6F6;"
1651 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1652 " font-weight: bold;"
1653 " font-size: large;"
1654 " text-align: left;"
1655 " border-bottom: 5px solid #DCEB5C;"
1656 "}"
1657 "h4{ background-color: #F6F6F6;"
1658 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1659 " font-weight: bold;"
1660 " font-size: medium;"
1661 " text-align: left;"
1662 "}"
1663 "h5{ background-color: #F6F6F6;"
1664 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1665 " font-weight: bold;"
1666 " font-size: small;"
1667 " text-align: left;"
1668 "}"
1669 "a{ color: #729FCF;"
1670 " font-family: arial,sans-serif;"
1671 "}"
1672 "label{ background-color: #FFFFCC;"
1673 " border: 1px solid black;"
1674 " margin: 1px;"
1675 " padding: 0px 3px; "
1676 " font-size: small;"
1677 "}"
1678 "th .strong {"
1679 " font-weight: bold;"
1680 "}"
1681 "hr {"
1682 " border: 0;"
1683 " height: 0;"
1684 " border-top: 1px solid black;"
1685 "}"
1686 ".list-view .highlight {"
1687 " text-align: left;"
1688 " border: 0px;"
1689 " width: 20%;"
1690 " padding-right: 15px;"
1691 " padding-left: 20px;"
1692 " font-weight: bold;"
1693 "}"
1694 ".tabular-view .odd-row {"
1695 " background-color: #f9f9f9;"
1696 "}"
1697 ".section {"
1698 " font-weight: bold;"
1699 " padding-top:25px;"
1700 "}" );
1701
1702 // We have some subtle differences between Qt based style and QWebKit style
1703 switch ( styleSheetType )
1704 {
1705 case StyleSheetType::Qt:
1706 myStyle += QStringLiteral(
1707 ".tabular-view{ "
1708 " border-collapse: collapse;"
1709 " width: 95%;"
1710 "}"
1711 ".tabular-view th, .tabular-view td { "
1712 " border:1px solid black;"
1713 "}" );
1714 break;
1715
1716 case StyleSheetType::WebBrowser:
1717 myStyle += QStringLiteral(
1718 "body { "
1719 " margin: auto;"
1720 " width: 97%;"
1721 "}"
1722 "table.tabular-view, table.list-view { "
1723 " border-collapse: collapse;"
1724 " table-layout:fixed;"
1725 " width: 100% !important;"
1726 " font-size: 90%;"
1727 "}"
1728 // Override
1729 "h1 { "
1730 " line-height: inherit;"
1731 "}"
1732 "td, th {"
1733 " word-wrap: break-word; "
1734 " vertical-align: top;"
1735 "}"
1736 // Set first column width
1737 ".list-view th:first-child, .list-view td:first-child {"
1738 " width: 20%;"
1739 "}"
1740 ".list-view.highlight { "
1741 " padding-left: inherit; "
1742 "}"
1743 // Set first column width for inner tables
1744 ".tabular-view th:first-child, .tabular-view td:first-child { "
1745 " width: 20%; "
1746 "}"
1747 // Makes titles bg stand up
1748 ".tabular-view th.strong { "
1749 " background-color: #eee; "
1750 "}"
1751 // Give some visual appearance to those ugly nested tables
1752 ".tabular-view th, .tabular-view td { "
1753 " border: 1px solid #eee;"
1754 "}"
1755 );
1756 break;
1757 }
1758
1759 return myStyle;
1760}
1761
1763{
1764 if ( 0 >= OGRGetDriverCount() )
1765 {
1766 OGRRegisterAll();
1767 }
1768}
1769
1770QString QgsApplication::absolutePathToRelativePath( const QString &aPath, const QString &targetPath )
1771{
1772 QString aPathUrl = aPath;
1773 QString tPathUrl = targetPath;
1774#if defined( Q_OS_WIN )
1775 const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
1776
1777 aPathUrl.replace( '\\', '/' );
1778 if ( aPathUrl.startsWith( "//" ) )
1779 {
1780 // keep UNC prefix
1781 aPathUrl = "\\\\" + aPathUrl.mid( 2 );
1782 }
1783
1784 tPathUrl.replace( '\\', '/' );
1785 if ( tPathUrl.startsWith( "//" ) )
1786 {
1787 // keep UNC prefix
1788 tPathUrl = "\\\\" + tPathUrl.mid( 2 );
1789 }
1790#else
1791 const Qt::CaseSensitivity cs = Qt::CaseSensitive;
1792#endif
1793
1794#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
1795 QStringList targetElems = tPathUrl.split( '/', QString::SkipEmptyParts );
1796 QStringList aPathElems = aPathUrl.split( '/', QString::SkipEmptyParts );
1797#else
1798 QStringList targetElems = tPathUrl.split( '/', Qt::SkipEmptyParts );
1799 QStringList aPathElems = aPathUrl.split( '/', Qt::SkipEmptyParts );
1800#endif
1801
1802 targetElems.removeAll( QStringLiteral( "." ) );
1803 aPathElems.removeAll( QStringLiteral( "." ) );
1804
1805 // remove common part
1806 int n = 0;
1807 while ( !aPathElems.isEmpty() &&
1808 !targetElems.isEmpty() &&
1809 aPathElems[0].compare( targetElems[0], cs ) == 0 )
1810 {
1811 aPathElems.removeFirst();
1812 targetElems.removeFirst();
1813 n++;
1814 }
1815
1816 if ( n == 0 )
1817 {
1818 // no common parts; might not even be a file
1819 return aPathUrl;
1820 }
1821
1822 if ( !targetElems.isEmpty() )
1823 {
1824 // go up to the common directory
1825 for ( int i = 0; i < targetElems.size(); i++ )
1826 {
1827 aPathElems.insert( 0, QStringLiteral( ".." ) );
1828 }
1829 }
1830 else
1831 {
1832 // let it start with . nevertheless,
1833 // so relative path always start with either ./ or ../
1834 aPathElems.insert( 0, QStringLiteral( "." ) );
1835 }
1836
1837 return aPathElems.join( QLatin1Char( '/' ) );
1838}
1839
1840QString QgsApplication::relativePathToAbsolutePath( const QString &rpath, const QString &targetPath )
1841{
1842 // relative path should always start with ./ or ../
1843 if ( !rpath.startsWith( QLatin1String( "./" ) ) && !rpath.startsWith( QLatin1String( "../" ) ) )
1844 {
1845 return rpath;
1846 }
1847
1848 QString rPathUrl = rpath;
1849 QString targetPathUrl = targetPath;
1850
1851#if defined(Q_OS_WIN)
1852 rPathUrl.replace( '\\', '/' );
1853 targetPathUrl.replace( '\\', '/' );
1854
1855 bool uncPath = targetPathUrl.startsWith( "//" );
1856#endif
1857
1858#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
1859 QStringList srcElems = rPathUrl.split( '/', QString::SkipEmptyParts );
1860 QStringList targetElems = targetPathUrl.split( '/', QString::SkipEmptyParts );
1861#else
1862 QStringList srcElems = rPathUrl.split( '/', Qt::SkipEmptyParts );
1863 QStringList targetElems = targetPathUrl.split( '/', Qt::SkipEmptyParts );
1864#endif
1865
1866#if defined(Q_OS_WIN)
1867 if ( uncPath )
1868 {
1869 targetElems.insert( 0, "" );
1870 targetElems.insert( 0, "" );
1871 }
1872#endif
1873
1874 // append source path elements
1875 targetElems << srcElems;
1876 targetElems.removeAll( QStringLiteral( "." ) );
1877
1878 // resolve ..
1879 int pos;
1880 while ( ( pos = targetElems.indexOf( QLatin1String( ".." ) ) ) > 0 )
1881 {
1882 // remove preceding element and ..
1883 targetElems.removeAt( pos - 1 );
1884 targetElems.removeAt( pos - 1 );
1885 }
1886
1887#if !defined(Q_OS_WIN)
1888 // make path absolute
1889 targetElems.prepend( QString() );
1890#endif
1891
1892 return targetElems.join( QLatin1Char( '/' ) );
1893}
1894
1896{
1897 return *sBuildSourcePath();
1898}
1899
1901{
1902 return *sBuildOutputPath();
1903}
1904
1905#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
1906QString QgsApplication::cfgIntDir()
1907{
1908 return *sCfgIntDir();
1909}
1910#endif
1911
1912void QgsApplication::skipGdalDriver( const QString &driver )
1913{
1914 if ( sGdalSkipList()->contains( driver ) || driver.isEmpty() )
1915 {
1916 return;
1917 }
1918 *sGdalSkipList() << driver;
1920}
1921
1922void QgsApplication::restoreGdalDriver( const QString &driver )
1923{
1924 if ( !sGdalSkipList()->contains( driver ) )
1925 {
1926 return;
1927 }
1928 int myPos = sGdalSkipList()->indexOf( driver );
1929 if ( myPos >= 0 )
1930 {
1931 sGdalSkipList()->removeAt( myPos );
1932 }
1934}
1935
1937{
1938 return *sGdalSkipList();
1939}
1940
1941void QgsApplication::setSkippedGdalDrivers( const QStringList &skippedGdalDrivers,
1942 const QStringList &deferredSkippedGdalDrivers )
1943{
1944 *sGdalSkipList() = skippedGdalDrivers;
1945 *sDeferredSkippedGdalDrivers() = deferredSkippedGdalDrivers;
1946
1947 QgsSettings settings;
1948 settings.setValue( QStringLiteral( "gdal/skipDrivers" ), skippedGdalDrivers.join( QLatin1Char( ',' ) ) );
1949
1951}
1952
1954{
1955 QgsSettings settings;
1956 QString joinedList, delimiter;
1957 if ( settings.contains( QStringLiteral( "gdal/skipDrivers" ) ) )
1958 {
1959 joinedList = settings.value( QStringLiteral( "gdal/skipDrivers" ), QString() ).toString();
1960 delimiter = QStringLiteral( "," );
1961 }
1962 else
1963 {
1964 joinedList = settings.value( QStringLiteral( "gdal/skipList" ), QString() ).toString();
1965 delimiter = QStringLiteral( " " );
1966 }
1967 QStringList myList;
1968 if ( !joinedList.isEmpty() )
1969 {
1970 myList = joinedList.split( delimiter );
1971 }
1972 *sGdalSkipList() = myList;
1974}
1975
1977{
1978 return *sDeferredSkippedGdalDrivers();
1979}
1980
1982{
1983 sGdalSkipList()->removeDuplicates();
1984 QStringList realDisabledDriverList;
1985 for ( const auto &driverName : *sGdalSkipList() )
1986 {
1987 if ( !sDeferredSkippedGdalDrivers()->contains( driverName ) )
1988 realDisabledDriverList << driverName;
1989 }
1990 QString myDriverList = realDisabledDriverList.join( ',' );
1991 QgsDebugMsgLevel( QStringLiteral( "Gdal Skipped driver list set to:" ), 2 );
1992 QgsDebugMsgLevel( myDriverList, 2 );
1993 CPLSetConfigOption( "GDAL_SKIP", myDriverList.toUtf8() );
1994 GDALAllRegister(); //to update driver list and skip missing ones
1995}
1996
1998{
1999 QString folder = userThemesFolder();
2000 QDir myDir( folder );
2001 if ( !myDir.exists() )
2002 {
2003 myDir.mkpath( folder );
2004 }
2005
2006 return true;
2007}
2008
2009void QgsApplication::copyPath( const QString &src, const QString &dst )
2010{
2011 QDir dir( src );
2012 if ( ! dir.exists() )
2013 return;
2014
2015 const auto subDirectories = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot );
2016 for ( const QString &d : subDirectories )
2017 {
2018 QString dst_path = dst + QDir::separator() + d;
2019 dir.mkpath( dst_path );
2020 copyPath( src + QDir::separator() + d, dst_path );
2021 }
2022
2023 const auto files = dir.entryList( QDir::Files );
2024 for ( const QString &f : files )
2025 {
2026 QFile::copy( src + QDir::separator() + f, dst + QDir::separator() + f );
2027 }
2028}
2029
2031{
2032 //read values from QgsSettings
2033 QgsSettings settings;
2034
2035 QVariantMap variables;
2036
2037 //check if settings contains any variables
2038 settings.beginGroup( "variables" );
2039 QStringList childKeys = settings.childKeys();
2040 for ( QStringList::const_iterator it = childKeys.constBegin(); it != childKeys.constEnd(); ++it )
2041 {
2042 QString name = *it;
2043 variables.insert( name, settings.value( name ) );
2044 }
2045
2046 return variables;
2047}
2048
2049void QgsApplication::setCustomVariables( const QVariantMap &variables )
2050{
2051 QgsSettings settings;
2052
2053 QVariantMap::const_iterator it = variables.constBegin();
2054 settings.beginGroup( "variables" );
2055 settings.remove( "" );
2056 for ( ; it != variables.constEnd(); ++it )
2057 {
2058 settings.setValue( it.key(), it.value() );
2059 }
2060
2062}
2063
2064void QgsApplication::setCustomVariable( const QString &name, const QVariant &value )
2065{
2066 // save variable to settings
2067 QgsSettings settings;
2068
2069 settings.setValue( QStringLiteral( "variables/" ) + name, value );
2070
2072}
2073
2074int QgsApplication::scaleIconSize( int standardSize, bool applyDevicePixelRatio )
2075{
2076 QFontMetrics fm( ( QFont() ) );
2077 const double scale = 1.1 * standardSize / 24;
2078 int scaledIconSize = static_cast< int >( std::floor( std::max( Qgis::UI_SCALE_FACTOR * fm.height() * scale, static_cast< double >( standardSize ) ) ) );
2079 if ( applyDevicePixelRatio )
2080 {
2081 if ( QWidget *activeWindow = QApplication::activeWindow() )
2082 scaledIconSize *= ( activeWindow->screen() ? QApplication::activeWindow()->screen()->devicePixelRatio() : 1 );
2083 }
2084 return scaledIconSize;
2085}
2086
2088{
2090}
2091
2092void QgsApplication::setTranslation( const QString &translation )
2093{
2094 *sTranslation() = translation;
2095 if ( auto app = QgsApplication::instance() )
2096 {
2097 app->installTranslators();
2098 }
2099}
2100
2102{
2103 return *sTranslation();
2104}
2105
2107{
2108 emit requestForTranslatableObjects( translationContext );
2109}
2110
2112{
2113 ApplicationMembers *appMembers = members();
2114 if ( appMembers->mNullRepresentation.isNull() )
2115 {
2116 appMembers->mNullRepresentation = QgsSettings().value( QStringLiteral( "qgis/nullValue" ), QStringLiteral( "NULL" ) ).toString();
2117 }
2118 return appMembers->mNullRepresentation;
2119}
2120
2121void QgsApplication::setNullRepresentation( const QString &nullRepresentation )
2122{
2123 ApplicationMembers *appMembers = members();
2124 if ( !appMembers || appMembers->mNullRepresentation == nullRepresentation )
2125 return;
2126
2127 appMembers->mNullRepresentation = nullRepresentation;
2128 QgsSettings().setValue( QStringLiteral( "qgis/nullValue" ), nullRepresentation );
2129
2130 QgsApplication *app = instance();
2131 if ( app )
2132 emit app->nullRepresentationChanged();
2133}
2134
2136{
2137 return members()->mActionScopeRegistry;
2138}
2139
2140bool QgsApplication::createDatabase( QString *errorMessage )
2141{
2142 // set a working directory up for gdal to write .aux.xml files into
2143 // for cases where the raster dir is read only to the user
2144 // if the env var is already set it will be used preferentially
2145 QString myPamPath = qgisSettingsDirPath() + QStringLiteral( "gdal_pam/" );
2146 QDir myDir( myPamPath );
2147 if ( !myDir.exists() )
2148 {
2149 myDir.mkpath( myPamPath ); //fail silently
2150 }
2151
2152#if defined(Q_OS_WIN)
2153 CPLSetConfigOption( "GDAL_PAM_PROXY_DIR", myPamPath.toUtf8() );
2154#else
2155 //under other OS's we use an environment var so the user can
2156 //override the path if he likes
2157 int myChangeFlag = 0; //whether we want to force the env var to change
2158 setenv( "GDAL_PAM_PROXY_DIR", myPamPath.toUtf8(), myChangeFlag );
2159#endif
2160
2161 // Check qgis.db and make private copy if necessary
2162 QFile qgisPrivateDbFile( QgsApplication::qgisUserDatabaseFilePath() );
2163
2164 // first we look for ~/.qgis/qgis.db
2165 if ( !qgisPrivateDbFile.exists() )
2166 {
2167 // if it doesn't exist we copy it in from the global resources dir
2168 QString qgisMasterDbFileName = QgsApplication::qgisMasterDatabaseFilePath();
2169 QFile masterFile( qgisMasterDbFileName );
2170
2171 // Must be sure there is destination directory ~/.qgis
2172 QDir().mkpath( QgsApplication::qgisSettingsDirPath() );
2173
2174 //now copy the master file into the users .qgis dir
2175 bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );
2176
2177 if ( !isDbFileCopied )
2178 {
2179 if ( errorMessage )
2180 {
2181 *errorMessage = tr( "[ERROR] Can not make qgis.db private copy" );
2182 }
2183 return false;
2184 }
2185
2186 QFile::Permissions perms = QFile( qgisPrivateDbFile.fileName() ).permissions();
2187 if ( !( perms & QFile::WriteOwner ) )
2188 {
2189 if ( !qgisPrivateDbFile.setPermissions( perms | QFile::WriteOwner ) )
2190 {
2191 if ( errorMessage )
2192 {
2193 *errorMessage = tr( "Can not make '%1' user writable" ).arg( qgisPrivateDbFile.fileName() );
2194 }
2195 return false;
2196 }
2197 }
2198 }
2199 else
2200 {
2201 // migrate if necessary
2203 if ( database.open( QgsApplication::qgisUserDatabaseFilePath() ) != SQLITE_OK )
2204 {
2205 if ( errorMessage )
2206 {
2207 *errorMessage = tr( "Could not open qgis.db" );
2208 }
2209 return false;
2210 }
2211
2212 char *errmsg = nullptr;
2213 int res = sqlite3_exec( database.get(), "SELECT srs_id FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2214 if ( res != SQLITE_OK )
2215 {
2216 sqlite3_free( errmsg );
2217
2218 // qgis.db is missing tbl_srs, create it
2219 if ( sqlite3_exec( database.get(),
2220 "DROP INDEX IF EXISTS idx_srsauthid;"
2221 "CREATE TABLE tbl_srs ("
2222 "srs_id INTEGER PRIMARY KEY,"
2223 "description text NOT NULL,"
2224 "projection_acronym text NOT NULL,"
2225 "ellipsoid_acronym NOT NULL,"
2226 "parameters text NOT NULL,"
2227 "srid integer,"
2228 "auth_name varchar,"
2229 "auth_id varchar,"
2230 "is_geo integer NOT NULL,"
2231 "deprecated boolean,"
2232 "wkt text);"
2233 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2234 {
2235 if ( errorMessage )
2236 {
2237 *errorMessage = tr( "Creation of missing tbl_srs in the private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2238 }
2239 sqlite3_free( errmsg );
2240 return false;
2241 }
2242 }
2243 else
2244 {
2245 // test if wkt column exists in database
2246 res = sqlite3_exec( database.get(), "SELECT wkt FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2247 if ( res != SQLITE_OK )
2248 {
2249 // need to add wkt column
2250 sqlite3_free( errmsg );
2251 if ( sqlite3_exec( database.get(),
2252 "DROP INDEX IF EXISTS idx_srsauthid;"
2253 "DROP TABLE IF EXISTS tbl_srs_bak;"
2254 "ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
2255 "CREATE TABLE tbl_srs ("
2256 "srs_id INTEGER PRIMARY KEY,"
2257 "description text NOT NULL,"
2258 "projection_acronym text NOT NULL,"
2259 "ellipsoid_acronym NOT NULL,"
2260 "parameters text NOT NULL,"
2261 "srid integer,"
2262 "auth_name varchar,"
2263 "auth_id varchar,"
2264 "is_geo integer NOT NULL,"
2265 "deprecated boolean,"
2266 "wkt text);"
2267 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
2268 "INSERT INTO tbl_srs(srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) SELECT srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,'','',is_geo,0 FROM tbl_srs_bak;"
2269 "DROP TABLE tbl_srs_bak", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2270 {
2271 if ( errorMessage )
2272 {
2273 *errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2274 }
2275 sqlite3_free( errmsg );
2276 return false;
2277 }
2278 }
2279 }
2280
2281 res = sqlite3_exec( database.get(), "SELECT acronym FROM tbl_projection LIMIT 0", nullptr, nullptr, &errmsg );
2282 if ( res != SQLITE_OK )
2283 {
2284 sqlite3_free( errmsg );
2285
2286 // qgis.db is missing tbl_projection, create it
2287 if ( sqlite3_exec( database.get(),
2288 "CREATE TABLE tbl_projection ("
2289 "acronym varchar(20) NOT NULL PRIMARY KEY,"
2290 "name varchar(255) NOT NULL default '',"
2291 "notes varchar(255) NOT NULL default '',"
2292 "parameters varchar(255) NOT NULL default ''"
2293 ")", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2294 {
2295 if ( errorMessage )
2296 {
2297 *errorMessage = tr( "Creation of missing tbl_projection in the private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2298 }
2299 sqlite3_free( errmsg );
2300 return false;
2301 }
2302 }
2303
2304 res = sqlite3_exec( database.get(), "SELECT epsg FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2305 if ( res == SQLITE_OK )
2306 {
2307 // epsg column exists => need migration
2308 if ( sqlite3_exec( database.get(),
2309 "DROP INDEX IF EXISTS idx_srsauthid;"
2310 "DROP TABLE IF EXISTS tbl_srs_bak;"
2311 "ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
2312 "CREATE TABLE tbl_srs ("
2313 "srs_id INTEGER PRIMARY KEY,"
2314 "description text NOT NULL,"
2315 "projection_acronym text NOT NULL,"
2316 "ellipsoid_acronym NOT NULL,"
2317 "parameters text NOT NULL,"
2318 "srid integer,"
2319 "auth_name varchar,"
2320 "auth_id varchar,"
2321 "is_geo integer NOT NULL,"
2322 "deprecated boolean,"
2323 "wkt text);"
2324 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
2325 "INSERT INTO tbl_srs(srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) SELECT srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,'','',is_geo,0 FROM tbl_srs_bak;"
2326 "DROP TABLE tbl_srs_bak", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2327 {
2328 if ( errorMessage )
2329 {
2330 *errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2331 }
2332 sqlite3_free( errmsg );
2333 return false;
2334 }
2335 }
2336 else
2337 {
2338 sqlite3_free( errmsg );
2339 }
2340
2341 if ( sqlite3_exec( database.get(), "DROP VIEW vw_srs", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2342 {
2343 QgsDebugError( QStringLiteral( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
2344 }
2345
2346 if ( sqlite3_exec( database.get(),
2347 "CREATE VIEW vw_srs AS"
2348 " SELECT"
2349 " a.description AS description"
2350 ",a.srs_id AS srs_id"
2351 ",a.is_geo AS is_geo"
2352 ",coalesce(b.name,a.projection_acronym) AS name"
2353 ",a.parameters AS parameters"
2354 ",a.auth_name AS auth_name"
2355 ",a.auth_id AS auth_id"
2356 ",a.deprecated AS deprecated"
2357 " FROM tbl_srs a"
2358 " LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
2359 " ORDER BY coalesce(b.name,a.projection_acronym),a.description", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2360 {
2361 if ( errorMessage )
2362 {
2363 *errorMessage = tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2364 }
2365 sqlite3_free( errmsg );
2366 return false;
2367 }
2368 }
2369 return true;
2370}
2371
2372void QgsApplication::setMaxThreads( int maxThreads )
2373{
2374 QgsDebugMsgLevel( QStringLiteral( "maxThreads: %1" ).arg( maxThreads ), 2 );
2375
2376 // make sure value is between 1 and #cores, if not set to -1 (use #cores)
2377 if ( maxThreads < 1 || maxThreads > QThread::idealThreadCount() )
2378 maxThreads = -1;
2379
2380 // force at least 2 threads -- anything less risks deadlocks within Qt itself (e.g in QImage internal mutexes)
2381 if ( maxThreads > 0 && maxThreads < 2 )
2382 maxThreads = 2;
2383
2384 // save value
2385 ABISYM( sMaxThreads ) = maxThreads;
2386
2387 // if -1 use #cores
2388 if ( maxThreads == -1 )
2389 maxThreads = QThread::idealThreadCount();
2390
2391 // set max thread count in QThreadPool
2392 QThreadPool::globalInstance()->setMaxThreadCount( maxThreads );
2393 QgsDebugMsgLevel( QStringLiteral( "set QThreadPool max thread count to %1" ).arg( QThreadPool::globalInstance()->maxThreadCount() ), 2 );
2394}
2395
2397{
2398 return members()->mTaskManager;
2399}
2400
2402{
2403 return members()->mSettingsRegistryCore;
2404}
2405
2407{
2408 return members()->mColorSchemeRegistry;
2409}
2410
2412{
2413 return members()->mPaintEffectRegistry;
2414}
2415
2417{
2418 return members()->mRendererRegistry;
2419}
2420
2422{
2423 return members()->mRasterRendererRegistry;
2424}
2425
2427{
2428 return members()->mPointCloudRendererRegistry;
2429}
2430
2432{
2433 if ( auto *lInstance = instance() )
2434 {
2435 if ( !instance()->mDataItemProviderRegistry )
2436 {
2437 lInstance->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
2438 }
2439 return lInstance->mDataItemProviderRegistry;
2440 }
2441 else
2442 {
2443 // no QgsApplication instance
2444 static QgsDataItemProviderRegistry *sDataItemProviderRegistry = nullptr;
2445 if ( !sDataItemProviderRegistry )
2446 sDataItemProviderRegistry = new QgsDataItemProviderRegistry();
2447 return sDataItemProviderRegistry;
2448 }
2449}
2450
2452{
2453 return members()->mCrsRegistry;
2454}
2455
2457{
2458 return members()->mSvgCache;
2459}
2460
2462{
2463 return members()->mImageCache;
2464}
2465
2467{
2468 return members()->mSourceCache;
2469}
2470
2472{
2473 return members()->mNetworkContentFetcherRegistry;
2474}
2475
2477{
2478 return members()->mValidityCheckRegistry;
2479}
2480
2482{
2483 return members()->mSymbolLayerRegistry;
2484}
2485
2487{
2488 return members()->mCalloutRegistry;
2489}
2490
2492{
2493 return members()->mLayoutItemRegistry;
2494}
2495
2497{
2498 return members()->mAnnotationItemRegistry;
2499}
2500
2502{
2503 return members()->mSensorRegistry;
2504}
2505
2507{
2508 return members()->mGpsConnectionRegistry;
2509}
2510
2512{
2513 return members()->mGpsBabelFormatRegistry;
2514}
2515
2517{
2518 return members()->mPluginLayerRegistry;
2519}
2520
2522{
2523 return members()->mClassificationMethodRegistry;
2524}
2525
2527{
2528 return members()->mBookmarkManager;
2529}
2530
2532{
2533 return members()->mTileDownloadManager;
2534}
2535
2537{
2538 return members()->mRecentStyleHandler;
2539}
2540
2542{
2543 return members()->mQueryLogger;
2544}
2545
2547{
2548 return members()->mStyleModel;
2549}
2550
2552{
2553 return members()->mFontManager;
2554}
2555
2557{
2558 return members()->mMessageLog;
2559}
2560
2562{
2563 return members()->mProcessingRegistry;
2564}
2565
2567{
2568 return members()->mConnectionRegistry;
2569}
2570
2572{
2573 return members()->mLayerMetadataProviderRegistry;
2574}
2575
2577{
2578 return members()->mPageSizeRegistry;
2579}
2580
2582{
2583 return members()->mAnnotationRegistry;
2584}
2585
2587{
2588 return members()->mNumericFormatRegistry;
2589}
2590
2592{
2593 return members()->mFieldFormatterRegistry;
2594}
2595
2597{
2598 return members()->m3DRendererRegistry;
2599}
2600
2602{
2603 return members()->m3DSymbolRegistry;
2604}
2605
2607{
2608 return members()->mScaleBarRendererRegistry;
2609}
2610
2612{
2613 return members()->mProjectStorageRegistry;
2614}
2615
2617{
2618 return members()->mExternalStorageRegistry;
2619}
2620
2622{
2623 return members()->mLocalizedDataPathRegistry;
2624}
2625
2626QgsApplication::ApplicationMembers::ApplicationMembers()
2627{
2628 // don't use initializer lists or scoped pointers - as more objects are added here we
2629 // will need to be careful with the order of creation/destruction
2630 mSettingsRegistryCore = new QgsSettingsRegistryCore();
2631 mLocalizedDataPathRegistry = new QgsLocalizedDataPathRegistry();
2632 mMessageLog = new QgsMessageLog();
2633 QgsRuntimeProfiler *profiler = QgsRuntimeProfiler::threadLocalInstance();
2634
2635 {
2636 profiler->start( tr( "Create query logger" ) );
2637 mQueryLogger = new QgsDatabaseQueryLog();
2638 profiler->end();
2639 }
2640 {
2641 profiler->start( tr( "Setup coordinate reference system registry" ) );
2642 mCrsRegistry = new QgsCoordinateReferenceSystemRegistry();
2643 profiler->end();
2644 }
2645 {
2646 profiler->start( tr( "Create connection registry" ) );
2647 mConnectionRegistry = new QgsConnectionRegistry();
2648 profiler->end();
2649 }
2650 {
2651 profiler->start( tr( "Create project storage registry" ) );
2652 mProjectStorageRegistry = new QgsProjectStorageRegistry();
2653 profiler->end();
2654 }
2655 {
2656 profiler->start( tr( "Create layer metadata provider registry" ) );
2657 mLayerMetadataProviderRegistry = new QgsLayerMetadataProviderRegistry();
2658 profiler->end();
2659 }
2660 {
2661 profiler->start( tr( "Create font manager" ) );
2662 mFontManager = new QgsFontManager();
2663 profiler->end();
2664 }
2665 {
2666 profiler->start( tr( "Setup task manager" ) );
2667 mTaskManager = new QgsTaskManager();
2668 profiler->end();
2669 }
2670 {
2671 profiler->start( tr( "Setup action scope registry" ) );
2672 mActionScopeRegistry = new QgsActionScopeRegistry();
2673 profiler->end();
2674 }
2675 {
2676 profiler->start( tr( "Setup numeric formats" ) );
2677 mNumericFormatRegistry = new QgsNumericFormatRegistry();
2678 profiler->end();
2679 }
2680 {
2681 profiler->start( tr( "Setup field formats" ) );
2682 mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
2683 profiler->end();
2684 }
2685 {
2686 profiler->start( tr( "Setup SVG cache" ) );
2687 mSvgCache = new QgsSvgCache();
2688 profiler->end();
2689 }
2690 {
2691 profiler->start( tr( "Setup image cache" ) );
2692 mImageCache = new QgsImageCache();
2693 profiler->end();
2694 }
2695 {
2696 profiler->start( tr( "Setup source cache" ) );
2697 mSourceCache = new QgsSourceCache();
2698 profiler->end();
2699 }
2700 {
2701 profiler->start( tr( "Setup color scheme registry" ) );
2702 mColorSchemeRegistry = new QgsColorSchemeRegistry();
2703 profiler->end();
2704 }
2705 {
2706 profiler->start( tr( "Setup paint effect" ) );
2707 mPaintEffectRegistry = new QgsPaintEffectRegistry();
2708 profiler->end();
2709 }
2710 {
2711 profiler->start( tr( "Setup symbol layer registry" ) );
2712 mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
2713 profiler->end();
2714 }
2715 {
2716 profiler->start( tr( "Recent style handler" ) );
2717 mRecentStyleHandler = new QgsRecentStyleHandler();
2718 profiler->end();
2719 }
2720 {
2721 profiler->start( tr( "Setup callout registry" ) );
2722 mCalloutRegistry = new QgsCalloutRegistry();
2723 profiler->end();
2724 }
2725 {
2726 profiler->start( tr( "Setup renderer registry" ) );
2727 mRendererRegistry = new QgsRendererRegistry();
2728 profiler->end();
2729 }
2730 {
2731 profiler->start( tr( "Setup raster renderer registry" ) );
2732 mRasterRendererRegistry = new QgsRasterRendererRegistry();
2733 profiler->end();
2734 }
2735 {
2736 profiler->start( tr( "Setup point cloud renderer registry" ) );
2737 mPointCloudRendererRegistry = new QgsPointCloudRendererRegistry();
2738 profiler->end();
2739 }
2740 {
2741 profiler->start( tr( "Setup GPS registry" ) );
2742 mGpsConnectionRegistry = new QgsGpsConnectionRegistry();
2743 profiler->end();
2744 }
2745 {
2746 profiler->start( tr( "Setup GPSBabel format registry" ) );
2747 mGpsBabelFormatRegistry = new QgsBabelFormatRegistry();
2748 profiler->end();
2749 }
2750 {
2751 profiler->start( tr( "Setup plugin layer registry" ) );
2752 mPluginLayerRegistry = new QgsPluginLayerRegistry();
2753 profiler->end();
2754 }
2755 {
2756 profiler->start( tr( "Setup Processing registry" ) );
2757 mProcessingRegistry = new QgsProcessingRegistry();
2758 profiler->end();
2759 }
2760 mPageSizeRegistry = new QgsPageSizeRegistry();
2761 {
2762 profiler->start( tr( "Setup layout item registry" ) );
2763 mLayoutItemRegistry = new QgsLayoutItemRegistry();
2764 mLayoutItemRegistry->populate();
2765 profiler->end();
2766 }
2767 {
2768 profiler->start( tr( "Setup annotation registry" ) );
2769 mAnnotationRegistry = new QgsAnnotationRegistry();
2770 profiler->end();
2771 }
2772 {
2773 profiler->start( tr( "Setup annotation item registry" ) );
2774 mAnnotationItemRegistry = new QgsAnnotationItemRegistry();
2775 mAnnotationItemRegistry->populate();
2776 profiler->end();
2777 }
2778 {
2779 profiler->start( tr( "Setup sensor registry" ) );
2780 mSensorRegistry = new QgsSensorRegistry();
2781 mSensorRegistry->populate();
2782 profiler->end();
2783 }
2784 {
2785 profiler->start( tr( "Setup 3D symbol registry" ) );
2786 m3DSymbolRegistry = new Qgs3DSymbolRegistry();
2787 profiler->end();
2788 }
2789 {
2790 profiler->start( tr( "Setup 3D renderer registry" ) );
2791 m3DRendererRegistry = new Qgs3DRendererRegistry();
2792 profiler->end();
2793 }
2794 {
2795 profiler->start( tr( "Setup external storage registry" ) );
2796 mExternalStorageRegistry = new QgsExternalStorageRegistry();
2797 profiler->end();
2798 }
2799 {
2800 profiler->start( tr( "Setup network content cache" ) );
2801 mNetworkContentFetcherRegistry = new QgsNetworkContentFetcherRegistry();
2802 profiler->end();
2803 }
2804 {
2805 profiler->start( tr( "Setup layout check registry" ) );
2806 mValidityCheckRegistry = new QgsValidityCheckRegistry();
2807 profiler->end();
2808 }
2809 {
2810 profiler->start( tr( "Setup classification registry" ) );
2811 mClassificationMethodRegistry = new QgsClassificationMethodRegistry();
2812 profiler->end();
2813 }
2814 {
2815 profiler->start( tr( "Setup bookmark manager" ) );
2816 mBookmarkManager = new QgsBookmarkManager( nullptr );
2817 profiler->end();
2818 }
2819 {
2820 profiler->start( tr( "Setup tile download manager" ) );
2821 mTileDownloadManager = new QgsTileDownloadManager();
2822 profiler->end();
2823 }
2824 {
2825 profiler->start( tr( "Setup scalebar registry" ) );
2826 mScaleBarRendererRegistry = new QgsScaleBarRendererRegistry();
2827 profiler->end();
2828 }
2829}
2830
2831QgsApplication::ApplicationMembers::~ApplicationMembers()
2832{
2833 delete mStyleModel;
2834 delete mTileDownloadManager;
2835 delete mScaleBarRendererRegistry;
2836 delete mValidityCheckRegistry;
2837 delete mActionScopeRegistry;
2838 delete m3DRendererRegistry;
2839 delete m3DSymbolRegistry;
2840 delete mAnnotationRegistry;
2841 delete mColorSchemeRegistry;
2842 delete mFieldFormatterRegistry;
2843 delete mGpsConnectionRegistry;
2844 delete mGpsBabelFormatRegistry;
2845 delete mMessageLog;
2846 delete mPaintEffectRegistry;
2847 delete mPluginLayerRegistry;
2848 delete mProcessingRegistry;
2849 delete mPageSizeRegistry;
2850 delete mAnnotationItemRegistry;
2851 delete mSensorRegistry;
2852 delete mLayoutItemRegistry;
2853 delete mPointCloudRendererRegistry;
2854 delete mRasterRendererRegistry;
2855 delete mRendererRegistry;
2856 delete mSvgCache;
2857 delete mImageCache;
2858 delete mSourceCache;
2859 delete mCalloutRegistry;
2860 delete mRecentStyleHandler;
2861 delete mSymbolLayerRegistry;
2862 delete mExternalStorageRegistry;
2863 delete mTaskManager;
2864 delete mNetworkContentFetcherRegistry;
2865 delete mClassificationMethodRegistry;
2866 delete mNumericFormatRegistry;
2867 delete mBookmarkManager;
2868 delete mConnectionRegistry;
2869 delete mProjectStorageRegistry;
2870 delete mLayerMetadataProviderRegistry;
2871 delete mFontManager;
2872 delete mLocalizedDataPathRegistry;
2873 delete mCrsRegistry;
2874 delete mQueryLogger;
2875 delete mSettingsRegistryCore;
2876}
2877
2878QgsApplication::ApplicationMembers *QgsApplication::members()
2879{
2880 if ( auto *lInstance = instance() )
2881 {
2882 return lInstance->mApplicationMembers;
2883 }
2884 else
2885 {
2886 static QRecursiveMutex sMemberMutex;
2887 QMutexLocker lock( &sMemberMutex );
2888 if ( !sApplicationMembers )
2889 sApplicationMembers = new ApplicationMembers();
2890 return sApplicationMembers;
2891 }
2892}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:3749
Keeps track of available 3D renderers.
Registry of available 3D symbol classes.
The action scope registry is an application wide registry that contains a list of available action sc...
Registry of available annotation item types.
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static QString resolvePkgPath()
Calculate the application pkg path.
static int scaleIconSize(int standardSize, bool applyDevicePixelRatio=false)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
static void restoreGdalDriver(const QString &driver)
Sets the GDAL_SKIP environment variable to exclude the specified driver and then calls GDALDriverMana...
static void setCustomVariables(const QVariantMap &customVariables)
Custom expression variables for this application.
QString translation() const
Returns the current application translation locale code.
static QString i18nPath()
Returns the path to the translation directory.
static QgsAnnotationItemRegistry * annotationItemRegistry()
Returns the application's annotation item registry, used for annotation item types.
static QString osName()
Returns a string name of the operating system QGIS is running on.
static void registerOgrDrivers()
Register OGR drivers ensuring this only happens once.
static QString sponsorsFilePath()
Returns the path to the sponsors file.
static QgsRecentStyleHandler * recentStyleHandler()
Returns the handler for recently used style items.
endian_t
Constants for endian-ness.
static QString qgisMasterDatabaseFilePath()
Returns the path to the master qgis.db file.
static void skipGdalDriver(const QString &driver)
Sets the GDAL_SKIP environment variable to include the specified driver and then calls GDALDriverMana...
static QString defaultThemePath()
Returns the path to the default theme directory.
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
static QgsValidityCheckRegistry * validityCheckRegistry()
Returns the application's validity check registry, used for managing validity checks.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
static QString userStylePath()
Returns the path to user's style.
static QString platform()
Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external...
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
static QgsLayerMetadataProviderRegistry * layerMetadataProviderRegistry()
Returns registry of available layer metadata provider implementations.
static QgsConnectionRegistry * connectionRegistry()
Returns the application's connection registry, used for managing saved data provider connections.
static void exitQgis()
deletes provider registry and map layer registry
static void setPluginPath(const QString &pluginPath)
Alters plugin path - used by 3rd party apps.
static const QgsSettingsEntryStringList * settingsSearchPathsForSVG
Settings entry search path for SVG.
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static QVariantMap customVariables()
Custom expression variables for this application.
static QgsPointCloudRendererRegistry * pointCloudRendererRegistry()
Returns the application's point cloud renderer registry, used for managing point cloud layer 2D rende...
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
static QgsSensorRegistry * sensorRegistry()
Returns the application's sensor registry, used for sensor types.
static QString pluginPath()
Returns the path to the application plugin directory.
static void setUITheme(const QString &themeName)
Set the current UI theme used to style the interface.
static bool createDatabase(QString *errorMessage=nullptr)
initialize qgis.db
static const QgsSettingsEntryBool * settingsLocaleOverrideFlag
Settings entry locale override flag.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
static int systemMemorySizeMb()
Returns the size of the system memory (RAM) in megabytes.
static void setLocale(const QLocale &locale)
Sets the QGIS locale - used mainly by 3rd party apps and tests.
static void init(QString profileFolder=QString())
This method initializes paths etc for QGIS.
static void setThemeName(const QString &themeName)
Set the active theme to the specified theme.
void customVariablesChanged()
Emitted whenever a custom global variable changes.
static QString buildSourcePath()
Returns path to the source directory. Valid only when running from build directory.
static QString buildOutputPath()
Returns path to the build output directory. Valid only when running from build directory.
bool notify(QObject *receiver, QEvent *event) override
Catch exceptions when sending event to receiver.
static int maxThreads()
Gets maximum concurrent thread count.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
static QString reportStyleSheet(QgsApplication::StyleSheetType styleSheetType=QgsApplication::StyleSheetType::Qt)
Returns a css style sheet for reports, the styleSheetType argument determines what type of stylesheet...
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QgsScaleBarRendererRegistry * scaleBarRendererRegistry()
Gets the registry of available scalebar renderers.
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
static void setFileOpenEventReceiver(QObject *receiver)
Sets the FileOpen event receiver.
static QgsSymbolLayerRegistry * symbolLayerRegistry()
Returns the application's symbol layer registry, used for managing symbol layers.
static QgsRasterRendererRegistry * rasterRendererRegistry()
Returns the application's raster renderer registry, used for managing raster layer renderers.
static void applyGdalSkippedDrivers()
Apply the skipped drivers list to gdal.
static void setMaxThreads(int maxThreads)
Set maximum concurrent thread count.
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
static QgsNetworkContentFetcherRegistry * networkContentFetcherRegistry()
Returns the application's network content registry used for fetching temporary files during QGIS sess...
static QgsProjectStorageRegistry * projectStorageRegistry()
Returns registry of available project storage implementations.
static QString licenceFilePath()
Returns the path to the licence file.
static QString libexecPath()
Returns the path with utility executables (help viewer, crssync, ...)
static QStringList skippedGdalDrivers()
Returns the list of gdal drivers that should be skipped (based on GDAL_SKIP environment variable)
StyleSheetType
The StyleSheetType enum represents the stylesheet type that a widget supports.
static QString translatorsFilePath()
Returns the path to the sponsors file.
static const QgsSettingsEntryString * settingsLocaleGlobalLocale
Settings entry locale global locale.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static void setNullRepresentation(const QString &nullRepresentation)
This string is used to represent the value NULL throughout QGIS.
static QString applicationFullName()
Returns the QGIS application full name.
static QgsGpsConnectionRegistry * gpsConnectionRegistry()
Returns the application's GPS connection registry, used for managing GPS connections.
static QString locale()
Returns the QGIS locale.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QStringList svgPaths()
Returns the paths to svg directories.
static void initQgis()
loads providers
static QString showSettings()
Convenience function to get a summary of the paths used in this application instance useful for debug...
bool event(QEvent *event) override
Watch for QFileOpenEvent.
static void setPkgDataPath(const QString &pkgDataPath)
Alters pkg data path - used by 3rd party apps.
static QString absolutePathToRelativePath(const QString &apath, const QString &targetPath)
Converts absolute path to path relative to target.
static const QgsSettingsEntryString * settingsLocaleUserLocale
Settings entry locale user locale.
static QgsRuntimeProfiler * profiler()
Returns the application runtime profiler.
~QgsApplication() override
static QgsLocalizedDataPathRegistry * localizedDataPathRegistry()
Returns the registry of data repositories These are used as paths for basemaps, logos,...
static const char * QGIS_APPLICATION_NAME
static QgsTileDownloadManager * tileDownloadManager()
Returns the application's tile download manager, used for download of map tiles when rendering.
static const char * QGIS_ORGANIZATION_DOMAIN
static QMap< QString, QString > systemEnvVars()
Returns the system environment variables passed to application.
static void setAuthDatabaseDirPath(const QString &authDbDirPath)
Alters authentication data base directory path - used by 3rd party apps.
static QString prefixPath()
Returns the path to the application prefix directory.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
static QgsFontManager * fontManager()
Returns the application font manager, which manages available fonts and font installation for the QGI...
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static QgsDatabaseQueryLog * databaseQueryLog()
Returns the database query log.
static QgsMessageLog * messageLog()
Returns the application's message log.
void preNotify(QObject *receiver, QEvent *event, bool *done)
static bool createThemeFolder()
Create the users theme folder.
static QString metadataPath()
Returns the path to the metadata directory.
void localeChanged()
Emitted when project locale has been changed.
static QgsActionScopeRegistry * actionScopeRegistry()
Returns the action scope registry.
static QgsCoordinateReferenceSystemRegistry * coordinateReferenceSystemRegistry()
Returns the application's coordinate reference system (CRS) registry, which handles known CRS definit...
static const char * QGIS_ORGANIZATION_NAME
static QString contributorsFilePath()
Returns the path to the contributors file.
void collectTranslatableObjects(QgsTranslationContext *translationContext)
Emits the signal to collect all the strings of .qgs to be included in ts file.
static QgsSourceCache * sourceCache()
Returns the application's source cache, used for caching embedded and remote source strings as local ...
static QRegularExpression shortNameRegularExpression()
Returns the short name regular expression for line edit validator.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
static QgsAnnotationRegistry * annotationRegistry()
Returns the application's annotation registry, used for managing annotation types.
static QgsPluginLayerRegistry * pluginLayerRegistry()
Returns the application's plugin layer registry, used for managing plugin layer types.
static QgsClassificationMethodRegistry * classificationMethodRegistry()
Returns the application's classification methods registry, used in graduated renderer.
static QStringList deferredSkippedGdalDrivers()
Returns the list of gdal drivers that have been disabled in the current session, and thus,...
static QString defaultStylePath()
Returns the path to default style (works as a starting point).
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static QString qmlImportPath()
Returns the path where QML components are installed for QGIS Quick library.
Cursor
The Cursor enum defines constants for QGIS custom cursors.
@ ZoomOut
Zoom out.
@ CrossHair
Precisely identify a point on the canvas.
@ Identify
Identify: obtain information about the object.
@ Select
Select a rectangle.
@ CapturePoint
Select and capture a point or a feature.
@ Sampler
Color/Value picker.
@ ZoomIn
Zoom in.
static QString qgisAuthDatabaseFilePath()
Returns the path to the user authentication database file: qgis-auth.db.
static QString authorsFilePath()
Returns the path to the authors file.
static QgsBookmarkManager * bookmarkManager()
Returns the application's bookmark manager, used for storing installation-wide bookmarks.
static QString qgisUserDatabaseFilePath()
Returns the path to the user qgis.db file.
static QgsFieldFormatterRegistry * fieldFormatterRegistry()
Gets the registry of available field formatters.
static QString activeThemePath()
Returns the path to the currently active theme directory.
static QString defaultThemesFolder()
Returns the path to default themes folder from install (works as a starting point).
static void setSkippedGdalDrivers(const QStringList &skippedGdalDrivers, const QStringList &deferredSkippedGdalDrivers)
Sets the list of gdal drivers that should be disabled (skippedGdalDrivers), but excludes for now the ...
static QgsRendererRegistry * rendererRegistry()
Returns the application's renderer registry, used for managing vector layer renderers.
static void setTranslation(const QString &translation)
Set translation locale code.
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
static void setPrefixPath(const QString &prefixPath, bool useDefaultPaths=false)
Alters prefix path - used by 3rd party apps.
static QgsStyleModel * defaultStyleModel()
Returns a shared QgsStyleModel containing the default style library (see QgsStyle::defaultStyle()).
static QString relativePathToAbsolutePath(const QString &rpath, const QString &targetPath)
Converts path relative to target to an absolute path.
static void setSvgPaths(const QStringList &svgPaths)
Sets the paths to svg directories and invalidates the svg path list cache.
static QString developersMapFilePath()
Returns the path to the developers map file.
static QgsBabelFormatRegistry * gpsBabelFormatRegistry()
Returns the application's GPSBabel format registry, used for managing GPSBabel formats.
static endian_t endian()
Returns whether this machine uses big or little endian.
int maxConcurrentConnectionsPerPool() const
The maximum number of concurrent connections per connections pool.
static void setCustomVariable(const QString &name, const QVariant &value)
Set a single custom expression variable.
void requestForTranslatableObjects(QgsTranslationContext *translationContext)
Emitted when project strings which require translation are being collected for inclusion in a ....
static QString iconsPath()
Returns the path to the icons image directory.
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
static QgsExternalStorageRegistry * externalStorageRegistry()
Returns registry of available external storage implementations.
static QHash< QString, QString > uiThemes()
All themes found in ~/.qgis3/themes folder.
static QString splashPath()
Returns the path to the splash screen image directory.
static QString donorsFilePath()
Returns the path to the donors file.
static QString themeName()
Set the active theme to the specified theme.
void nullRepresentationChanged()
This string is used to represent the value NULL throughout QGIS.
static QString srsDatabaseFilePath()
Returns the path to the srs.db file.
static QString userThemesFolder()
Returns the path to user's themes folder.
static void registerGdalDriversFromSettings()
Register gdal drivers, excluding the ones mentioned in "gdal/skipList" setting.
static Qgs3DRendererRegistry * renderer3DRegistry()
Returns registry of available 3D renderers.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static void setDefaultSvgPaths(const QStringList &pathList)
Alters default svg paths - used by 3rd party apps.
static QString libraryPath()
Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries.
static QStringList layoutTemplatePaths()
Returns the paths to layout template directories.
static const QgsSettingsEntryBool * settingsLocaleShowGroupSeparator
Settings entry locale show group separator.
static QString userFullName()
Returns the user's operating system login account full display name.
static Q_DECL_DEPRECATED QgsSettingsRegistryCore * settingsRegistryCore()
Returns the application's settings registry, used for managing application settings.
static QString serverResourcesPath()
Returns the path to the server resources directory.
static QString appIconPath()
Gets application icon.
static QString userLoginName()
Returns the user's operating system login account name.
Singleton offering an interface to manage the authentication configuration database and to utilize co...
bool init(const QString &pluginPath=QString(), const QString &authDatabasePath=QString())
init initialize QCA, prioritize qca-ossl plugin and optionally set up the authentication database
static QgsAuthManager * instance()
Enforce singleton pattern.
A registry for QgsAbstractBabelFormat GPSBabel formats.
Manages storage of a set of bookmarks.
void initialize(const QString &filePath)
Initializes the bookmark manager.
Registry of available callout classes.
This class manages all known classification methods.
Registry of color schemes.
void addDefaultSchemes()
Adds all default color schemes to this color scheme.
void initStyleScheme()
Initializes the default random style color scheme for the user.
A registry for saved data provider connections, allowing retrieval of saved connections by name and p...
A registry for known coordinate reference system (CRS) definitions, including any user-defined CRSes.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used to initialize QgsCoordinateReferenceSystem objects.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used to initialize QgsCoordinateTransform objects.
This class keeps a list of data item providers that may add items to the browser tree.
Handles logging of database queries.
static void applyLocaleChange()
Adjusts the date time display formats according to locale.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used.
Defines a QGIS exception class.
Definition: qgsexception.h:35
QString what() const
Definition: qgsexception.h:49
static void cleanRegisteredFunctions()
Deletes all registered functions whose ownership have been transferred to the expression engine.
Registry of external storage backends used by QgsExternalResourceWidget.
The QgsFieldFormatterRegistry manages registered classes of QgsFieldFormatter.
Manages available fonts and font installation for a QGIS instance.
void installUserFonts()
Installs user fonts from the profile/fonts directory as application fonts.
A class to register / unregister existing GPS connections such that the information is available to a...
A cache for images derived from raster files.
Registry of layer metadata provider backends.
Registry of available layout item types.
static const QgsSettingsEntryStringList * settingsSearchPathForTemplates
Settings entry search path for templates.
Definition: qgslayout.h:662
A registry class to hold localized data paths which can be used for basemaps, logos,...
Temporarily blocks the application QgsMessageLog (see QgsApplication::messageLog()) from emitting the...
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:40
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
Registry for temporary fetched files.
The QgsNumericFormatRegistry manages registered classes of QgsNumericFormat.
A registry for known page sizes.
Registry of available paint effects.
A registry of plugin layers types.
Registry of 2D renderers for point clouds.
Registry for various processing components, including providers, algorithms and various parameters an...
static QStringList searchPaths()
Returns the current list of Proj file search paths.
Registry of storage backends that QgsProject may use.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:484
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Registry for raster renderers.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
@ Write
Lock for write.
@ Read
Lock for read.
void changeMode(Mode mode)
Change the mode of the lock to mode.
Handles and tracks style items recently used in the QGIS GUI.
Registry of renderers.
Provides a method of recording run time profiles of operations, allowing easy recording of their over...
void end(const QString &group="startup")
End the current profile event.
void start(const QString &name, const QString &group="startup")
Start a profile event with the given name.
The QgsScaleBarRendererRegistry manages registered scalebar renderers.
Scoped object for logging of the runtime for a single operation or group of operations.
Registry of available sensor types.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
A boolean settings entry.
A string list settings entry.
A string settings entry.
QgsSettingsRegistryCore is used for settings introspection and collects all QgsSettingsEntry instance...
static QgsSettingsTreeNode * sTreeLocale
static QgsSettingsTreeNode * sTreeSvg
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:89
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
A cache for source strings that returns a local file path containing the source content.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
static void cleanDefaultStyle()
Deletes the default style. Only to be used by QgsApplication::exitQgis()
Definition: qgsstyle.cpp:176
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
A cache for images / pictures derived from SVG files.
Definition: qgssvgcache.h:123
QImage svgAsImage(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Returns an SVG drawing as a QImage.
QByteArray svgContent(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >(), bool *isMissingImage=nullptr)
Gets the SVG content corresponding to the given path.
Registry of available symbol layer classes.
static QColor decodeColor(const QString &str)
Task manager for managing a set of long-running QgsTask tasks.
Tile download manager handles downloads of map tiles for the purpose of map rendering.
Used for the collecting of strings from projects for translation and creation of ts files.
User profile manager is used to manager list, and manage user profiles on the users machine.
QgsUserProfile * getProfile(const QString &defaultProfile="default", bool createNew=true, bool initSettings=true)
Returns the profile from the given root profile location.
static QString resolveProfilesFolder(const QString &basePath=QString())
Resolves the profiles folder for the given path.
User profile contains information about the user profile folders on the machine.
const QString folder() const
The base folder for the user profile.
This class keeps a list of QgsAbstractValidityCheck checks which can be used when performing validity...
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
int open(const QString &path)
Opens the database at the specified file path.
CORE_EXPORT const QStringList files(const QString &zip)
Returns the list of files within a zip file.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:4533
QObject * ABISYM(QgsApplication::mFileOpenEventReceiver)
#define CONN_POOL_MAX_CONCURRENT_CONNS
Q_GLOBAL_STATIC_WITH_ARGS(PalPropertyList, palHiddenProperties,({ QgsPalLayerSettings::PositionX, QgsPalLayerSettings::PositionY, QgsPalLayerSettings::Show, QgsPalLayerSettings::LabelRotation, QgsPalLayerSettings::Family, QgsPalLayerSettings::FontStyle, QgsPalLayerSettings::Size, QgsPalLayerSettings::Bold, QgsPalLayerSettings::Italic, QgsPalLayerSettings::Underline, QgsPalLayerSettings::Color, QgsPalLayerSettings::Strikeout, QgsPalLayerSettings::MultiLineAlignment, QgsPalLayerSettings::BufferSize, QgsPalLayerSettings::BufferDraw, QgsPalLayerSettings::BufferColor, QgsPalLayerSettings::LabelDistance, QgsPalLayerSettings::Hali, QgsPalLayerSettings::Vali, QgsPalLayerSettings::ScaleVisibility, QgsPalLayerSettings::MinScale, QgsPalLayerSettings::MaxScale, QgsPalLayerSettings::AlwaysShow, QgsPalLayerSettings::CalloutDraw, QgsPalLayerSettings::LabelAllParts })) Q_GLOBAL_STATIC_WITH_ARGS(SymbolPropertyList
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugError(str)
Definition: qgslogger.h:38