QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
91
95
96#include <QDir>
97#include <QFile>
98#include <QFileInfo>
99#include <QFileOpenEvent>
100#include <QMessageBox>
101#include <QPalette>
102#include <QProcess>
103#include <QProcessEnvironment>
104#include <QIcon>
105#include <QPixmap>
106#include <QThreadPool>
107#include <QLocale>
108#include <QStyle>
109#include <QLibraryInfo>
110#include <QStandardPaths>
111#include <QRegularExpression>
112#include <QTextStream>
113#include <QScreen>
114#include <QAuthenticator>
115#include <QRecursiveMutex>
116
117#ifndef Q_OS_WIN
118#include <netinet/in.h>
119#include <pwd.h>
120#else
121#include <winsock.h>
122#include <windows.h>
123#include <lmcons.h>
124#define SECURITY_WIN32
125#include <security.h>
126#ifdef _MSC_VER
127#pragma comment( lib, "Secur32.lib" )
128#endif
129#endif
130
131#include "qgsconfig.h"
132
133#include <gdal.h>
134#include <ogr_api.h>
135#include <cpl_conv.h> // for setting gdal options
136#include <sqlite3.h>
137#include <mutex>
138
139#include <proj.h>
140
141#if defined(Q_OS_LINUX)
142#include <sys/sysinfo.h>
143#endif
144
145#define CONN_POOL_MAX_CONCURRENT_CONNS 4
146
147QObject *ABISYM( QgsApplication::mFileOpenEventReceiver ) = nullptr;
148bool ABISYM( QgsApplication::mInitialized ) = false;
149bool ABISYM( QgsApplication::mRunningFromBuildDir ) = false;
150const char *QgsApplication::QGIS_ORGANIZATION_NAME = "QGIS";
151const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
152const char *QgsApplication::QGIS_APPLICATION_NAME = "QGIS3";
153QgsApplication::ApplicationMembers *QgsApplication::sApplicationMembers = nullptr;
154QgsAuthManager *QgsApplication::sAuthManager = nullptr;
155int ABISYM( QgsApplication::sMaxThreads ) = -1;
156
157Q_GLOBAL_STATIC( QStringList, sFileOpenEventList )
158Q_GLOBAL_STATIC( QString, sPrefixPath )
159Q_GLOBAL_STATIC( QString, sPluginPath )
160Q_GLOBAL_STATIC( QString, sPkgDataPath )
161Q_GLOBAL_STATIC( QString, sLibraryPath )
162Q_GLOBAL_STATIC( QString, sLibexecPath )
163Q_GLOBAL_STATIC( QString, sQmlImportPath )
164Q_GLOBAL_STATIC( QString, sThemeName )
165Q_GLOBAL_STATIC( QString, sProfilePath )
166
167Q_GLOBAL_STATIC( QStringList, sDefaultSvgPaths )
168Q_GLOBAL_STATIC( QgsStringMap, sSystemEnvVars )
169Q_GLOBAL_STATIC( QString, sConfigPath )
170
171Q_GLOBAL_STATIC( QString, sBuildSourcePath )
172#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
173Q_GLOBAL_STATIC( QString, sCfgIntDir )
174#endif
175Q_GLOBAL_STATIC( QString, sBuildOutputPath )
176Q_GLOBAL_STATIC( QStringList, sGdalSkipList )
177Q_GLOBAL_STATIC( QStringList, sDeferredSkippedGdalDrivers )
178Q_GLOBAL_STATIC( QString, sAuthDbDirPath )
179
180Q_GLOBAL_STATIC( QString, sUserName )
181Q_GLOBAL_STATIC( QString, sUserFullName )
182Q_GLOBAL_STATIC_WITH_ARGS( QString, sPlatformName, ( "external" ) )
183Q_GLOBAL_STATIC( QString, sTranslation )
184
185Q_GLOBAL_STATIC( QTemporaryDir, sIconCacheDir )
186
187QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder, const QString &platformName )
188 : QApplication( argc, argv, GUIenabled )
189{
190 *sPlatformName() = platformName;
191
192 if ( *sTranslation() != QLatin1String( "C" ) )
193 {
194 mQgisTranslator = new QTranslator();
195 if ( mQgisTranslator->load( QStringLiteral( "qgis_" ) + *sTranslation(), i18nPath() ) )
196 {
197 installTranslator( mQgisTranslator );
198 }
199 else
200 {
201 QgsDebugMsgLevel( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), *sTranslation() ), 2 );
202 }
203
204 /* Translation file for Qt.
205 * The strings from the QMenuBar context section are used by Qt/Mac to shift
206 * the About, Preferences and Quit items to the Mac Application menu.
207 * These items must be translated identically in both qt_ and qgis_ files.
208 */
209 QString qtTranslationsPath = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
210#ifdef __MINGW32__
211 QString prefix = QDir( QString( "%1/../" ).arg( QApplication::applicationDirPath() ) ).absolutePath();
212 qtTranslationsPath = prefix + qtTranslationsPath.mid( QLibraryInfo::location( QLibraryInfo::PrefixPath ).length() );
213#endif
214
215 mQtTranslator = new QTranslator();
216 if ( mQtTranslator->load( QStringLiteral( "qt_" ) + *sTranslation(), qtTranslationsPath ) )
217 {
218 installTranslator( mQtTranslator );
219 }
220 else
221 {
222 QgsDebugMsgLevel( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
223 }
224
225 mQtBaseTranslator = new QTranslator();
226 if ( mQtBaseTranslator->load( QStringLiteral( "qtbase_" ) + *sTranslation(), qtTranslationsPath ) )
227 {
228 installTranslator( mQtBaseTranslator );
229 }
230 else
231 {
232 QgsDebugMsgLevel( QStringLiteral( "loading of qtbase translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
233 }
234 }
235
236 mApplicationMembers = new ApplicationMembers();
237
238 *sProfilePath() = profileFolder;
239
241}
242
243void QgsApplication::init( QString profileFolder )
244{
245 if ( profileFolder.isEmpty() )
246 {
247 if ( getenv( "QGIS_CUSTOM_CONFIG_PATH" ) )
248 {
249 profileFolder = getenv( "QGIS_CUSTOM_CONFIG_PATH" );
250 }
251 else
252 {
253 profileFolder = QStandardPaths::standardLocations( QStandardPaths::AppDataLocation ).value( 0 );
254 }
255 // This will normally get here for custom scripts that use QgsApplication.
256 // This doesn't get this hit for QGIS Desktop because we setup the profile via main
257 QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( profileFolder );
258 QgsUserProfileManager manager( rootProfileFolder );
259 QgsUserProfile *profile = manager.getProfile();
260 profileFolder = profile->folder();
261 delete profile;
262 }
263
264 *sProfilePath() = profileFolder;
265
266 static std::once_flag sMetaTypesRegistered;
267 std::call_once( sMetaTypesRegistered, []
268 {
269 qRegisterMetaType<QgsGeometry::Error>( "QgsGeometry::Error" );
270 qRegisterMetaType<QgsDatabaseQueryLogEntry>( "QgsDatabaseQueryLogEntry" );
271 qRegisterMetaType<QgsProcessingFeatureSourceDefinition>( "QgsProcessingFeatureSourceDefinition" );
272 qRegisterMetaType<QgsProcessingOutputLayerDefinition>( "QgsProcessingOutputLayerDefinition" );
273 qRegisterMetaType<QgsUnitTypes::LayoutUnit>( "QgsUnitTypes::LayoutUnit" );
274 qRegisterMetaType<QgsUnsetAttributeValue>( "QgsUnsetAttributeValue" );
275 qRegisterMetaType<QgsFeatureId>( "QgsFeatureId" );
276 qRegisterMetaType<QgsFields>( "QgsFields" );
277 qRegisterMetaType<QgsFeatureIds>( "QgsFeatureIds" );
278 qRegisterMetaType<QgsProperty>( "QgsProperty" );
279 qRegisterMetaType<QgsFeatureStoreList>( "QgsFeatureStoreList" );
280 qRegisterMetaType<Qgis::MessageLevel>( "Qgis::MessageLevel" );
281 qRegisterMetaType<Qgis::BrowserItemState>( "Qgis::BrowserItemState" );
282 qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
283 qRegisterMetaType<QgsReferencedPointXY>( "QgsReferencedPointXY" );
284 qRegisterMetaType<QgsReferencedGeometry>( "QgsReferencedGeometry" );
285 qRegisterMetaType<QgsLayoutRenderContext::Flags>( "QgsLayoutRenderContext::Flags" );
286 qRegisterMetaType<QgsStyle::StyleEntity>( "QgsStyle::StyleEntity" );
287 qRegisterMetaType<QgsCoordinateReferenceSystem>( "QgsCoordinateReferenceSystem" );
288 qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" );
289 qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" );
290 qRegisterMetaType<QgsNetworkReplyContent>( "QgsNetworkReplyContent" );
291 qRegisterMetaType<QgsFeature>( "QgsFeature" );
292 qRegisterMetaType<QgsGeometry>( "QgsGeometry" );
293 qRegisterMetaType<QgsInterval>( "QgsInterval" );
294 qRegisterMetaType<QgsRectangle>( "QgsRectangle" );
295 qRegisterMetaType<QgsPointXY>( "QgsPointXY" );
296 qRegisterMetaType<QgsDatumTransform::GridDetails>( "QgsDatumTransform::GridDetails" );
297 qRegisterMetaType<QgsDatumTransform::TransformDetails>( "QgsDatumTransform::TransformDetails" );
298 qRegisterMetaType<QgsNewsFeedParser::Entry>( "QgsNewsFeedParser::Entry" );
299 qRegisterMetaType<QgsRectangle>( "QgsRectangle" );
300 qRegisterMetaType<QgsLocatorResult>( "QgsLocatorResult" );
301 qRegisterMetaType<QgsGradientColorRamp>( "QgsGradientColorRamp" );
302 qRegisterMetaType<QgsProcessingModelChildParameterSource>( "QgsProcessingModelChildParameterSource" );
303#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
304 // Qt6 documentation says these are not needed anymore (https://www.qt.io/blog/whats-new-in-qmetatype-qvariant) #spellok
305 // TODO: when tests can run against Qt6 builds, check for any regressions
306 qRegisterMetaTypeStreamOperators<QgsProcessingModelChildParameterSource>( "QgsProcessingModelChildParameterSource" );
307#endif
308 qRegisterMetaType<QgsRemappingSinkDefinition>( "QgsRemappingSinkDefinition" );
309 qRegisterMetaType<QgsProcessingModelChildDependency>( "QgsProcessingModelChildDependency" );
310 qRegisterMetaType<QgsTextFormat>( "QgsTextFormat" );
311#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
312 QMetaType::registerComparators<QgsProcessingModelChildDependency>();
313 QMetaType::registerEqualsComparator<QgsProcessingFeatureSourceDefinition>();
314 QMetaType::registerEqualsComparator<QgsProperty>();
315 QMetaType::registerEqualsComparator<QgsDateTimeRange>();
316 QMetaType::registerEqualsComparator<QgsDateRange>();
317 QMetaType::registerEqualsComparator<QgsUnsetAttributeValue>();
318#endif
319 qRegisterMetaType<QPainter::CompositionMode>( "QPainter::CompositionMode" );
320 qRegisterMetaType<QgsDateTimeRange>( "QgsDateTimeRange" );
321 qRegisterMetaType<QList<QgsMapLayer *>>( "QList<QgsMapLayer*>" );
322 qRegisterMetaType<QMap<QNetworkRequest::Attribute, QVariant>>( "QMap<QNetworkRequest::Attribute,QVariant>" );
323 qRegisterMetaType<QMap<QNetworkRequest::KnownHeaders, QVariant>>( "QMap<QNetworkRequest::KnownHeaders,QVariant>" );
324 qRegisterMetaType<QList<QNetworkReply::RawHeaderPair>>( "QList<QNetworkReply::RawHeaderPair>" );
325 qRegisterMetaType< QAuthenticator * >( "QAuthenticator*" );
326 } );
327
328 ( void ) resolvePkgPath();
329
330 if ( ABISYM( mRunningFromBuildDir ) )
331 {
332 // we run from source directory - not installed to destination (specified prefix)
333 *sPrefixPath() = QString(); // set invalid path
334#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
335 setPluginPath( *sBuildOutputPath() + '/' + QString( QGIS_PLUGIN_SUBDIR ) + '/' + *sCfgIntDir() );
336#else
337 setPluginPath( *sBuildOutputPath() + '/' + QStringLiteral( QGIS_PLUGIN_SUBDIR ) );
338#endif
339 setPkgDataPath( *sBuildOutputPath() + QStringLiteral( "/data" ) ); // in buildDir/data - used for: doc, resources, svg
340 *sLibraryPath() = *sBuildOutputPath() + '/' + QGIS_LIB_SUBDIR + '/';
341#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
342 *sLibexecPath() = *sBuildOutputPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/' + *sCfgIntDir() + '/';
343#else
344 *sLibexecPath() = *sBuildOutputPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/';
345#endif
346#if defined( HAVE_QUICK )
347 *sQmlImportPath() = *sBuildOutputPath() + '/' + QGIS_QML_SUBDIR + '/';
348#endif
349 }
350 else
351 {
352 char *prefixPath = getenv( "QGIS_PREFIX_PATH" );
353 if ( !prefixPath )
354 {
355 if ( sPrefixPath()->isNull() )
356 {
357#if defined(Q_OS_MACX) || defined(Q_OS_WIN)
358 setPrefixPath( applicationDirPath(), true );
359#elif defined(ANDROID)
360 // this is "/data/data/org.qgis.qgis" in android
361 QDir myDir( QDir::homePath() );
362 myDir.cdUp();
363 QString myPrefix = myDir.absolutePath();
364 setPrefixPath( myPrefix, true );
365#else
366 QDir myDir( applicationDirPath() );
367 // Fix for server which is one level deeper in /usr/lib/cgi-bin
368 if ( applicationDirPath().contains( QStringLiteral( "cgi-bin" ) ) )
369 {
370 myDir.cdUp();
371 }
372 myDir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr
373 QString myPrefix = myDir.absolutePath();
374 setPrefixPath( myPrefix, true );
375#endif
376 }
377 }
378 else
379 {
380 setPrefixPath( prefixPath, true );
381 }
382 }
383
384 *sConfigPath() = profileFolder + '/'; // make sure trailing slash is included
385 *sDefaultSvgPaths() << qgisSettingsDirPath() + QStringLiteral( "svg/" );
386
387 *sAuthDbDirPath() = qgisSettingsDirPath();
388 if ( getenv( "QGIS_AUTH_DB_DIR_PATH" ) )
389 {
390 setAuthDatabaseDirPath( getenv( "QGIS_AUTH_DB_DIR_PATH" ) );
391 }
392
393 // force use of OpenGL renderer for Qt3d.
394 qputenv( "QT3D_RENDERER", "opengl" );
395
396 // store system environment variables passed to application, before they are adjusted
397 QMap<QString, QString> systemEnvVarMap;
398 QString passfile( QStringLiteral( "QGIS_AUTH_PASSWORD_FILE" ) ); // QString, for comparison
399
400 const auto systemEnvironment = QProcessEnvironment::systemEnvironment().toStringList();
401 for ( const QString &varStr : systemEnvironment )
402 {
403 int pos = varStr.indexOf( QLatin1Char( '=' ) );
404 if ( pos == -1 )
405 continue;
406 QString varStrName = varStr.left( pos );
407 QString varStrValue = varStr.mid( pos + 1 );
408 if ( varStrName != passfile )
409 {
410 systemEnvVarMap.insert( varStrName, varStrValue );
411 }
412 }
413 *sSystemEnvVars() = systemEnvVarMap;
414
415 // append local user-writable folder as a proj search path
416 QStringList currentProjSearchPaths = QgsProjUtils::searchPaths();
417 currentProjSearchPaths.append( qgisSettingsDirPath() + QStringLiteral( "proj" ) );
418#ifdef Q_OS_MACX
419 // append bundled proj lib for MacOS
420 QString projLib( QDir::cleanPath( pkgDataPath().append( "/proj" ) ) );
421 if ( QFile::exists( projLib ) )
422 {
423 currentProjSearchPaths.append( projLib );
424 }
425#endif // Q_OS_MACX
426
427 char **newPaths = new char *[currentProjSearchPaths.length()];
428 for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
429 {
430 newPaths[i] = CPLStrdup( currentProjSearchPaths.at( i ).toUtf8().constData() );
431 }
432 proj_context_set_search_paths( nullptr, currentProjSearchPaths.count(), newPaths );
433 for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
434 {
435 CPLFree( newPaths[i] );
436 }
437 delete [] newPaths;
438
439 // allow Qt to search for Qt plugins (e.g. sqldrivers) in our plugin directory
440 QCoreApplication::addLibraryPath( pluginPath() );
441
442 {
443 QgsScopedRuntimeProfile profile( tr( "Load user fonts" ) );
445 }
446
447 // set max. thread count to -1
448 // this should be read from QgsSettings but we don't know where they are at this point
449 // so we read actual value in main.cpp
450 ABISYM( sMaxThreads ) = -1;
451
452 {
453 QgsScopedRuntimeProfile profile( tr( "Load color schemes" ) );
456 }
457
458 {
459 QgsScopedRuntimeProfile profile( tr( "Load bookmarks" ) );
461 }
462
463 // trigger creation of default style
464 QgsStyle *defaultStyle = QgsStyle::defaultStyle();
465 if ( !members()->mStyleModel )
466 members()->mStyleModel = new QgsStyleModel( defaultStyle );
467
468 ABISYM( mInitialized ) = true;
469}
470
472{
473 delete mDataItemProviderRegistry;
474 delete mApplicationMembers;
475 delete mQgisTranslator;
476 delete mQtTranslator;
477 delete mQtBaseTranslator;
478
479 // we do this here as well as in exitQgis() -- it's safe to call as often as we want,
480 // and there's just a *chance* that someone hasn't properly called exitQgis prior to
481 // this destructor...
482 invalidateCaches();
483}
484
485void QgsApplication::invalidateCaches()
486{
487 // invalidate coordinate cache while the PROJ context held by the thread-locale
488 // QgsProjContextStore object is still alive. Otherwise if this later object
489 // is destroyed before the static variables of the cache, we might use freed memory.
493}
494
496{
497 return qobject_cast<QgsApplication *>( QCoreApplication::instance() );
498}
499
500bool QgsApplication::event( QEvent *event )
501{
502 bool done = false;
503 if ( event->type() == QEvent::FileOpen )
504 {
505 // handle FileOpen event (double clicking a file icon in Mac OS X Finder)
506 if ( ABISYM( mFileOpenEventReceiver ) )
507 {
508 // Forward event to main window.
509 done = notify( ABISYM( mFileOpenEventReceiver ), event );
510 }
511 else
512 {
513 // Store filename because receiver has not registered yet.
514 // If QGIS has been launched by double clicking a file icon, FileOpen will be
515 // the first event; the main window is not yet ready to handle the event.
516 sFileOpenEventList()->append( static_cast<QFileOpenEvent *>( event )->file() );
517 done = true;
518 }
519 }
520 else
521 {
522 // pass other events to base class
523 done = QApplication::event( event );
524 }
525 return done;
526}
527
528bool QgsApplication::notify( QObject *receiver, QEvent *event )
529{
530 bool done = false;
531 // Crashes in customization (especially on Mac), if we're not in the main/UI thread, see #5597
532 if ( thread() == receiver->thread() )
533 emit preNotify( receiver, event, &done );
534
535 if ( done )
536 return true;
537
538 // Send event to receiver and catch unhandled exceptions
539 done = true;
540 try
541 {
542 done = QApplication::notify( receiver, event );
543 }
544 catch ( QgsException &e )
545 {
546 qCritical() << "Caught unhandled QgsException: " << e.what();
547 if ( qApp->thread() == QThread::currentThread() )
548 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
549 }
550 catch ( std::exception &e )
551 {
552 qCritical() << "Caught unhandled std::exception: " << e.what();
553 if ( qApp->thread() == QThread::currentThread() )
554 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
555 }
556 catch ( ... )
557 {
558 qCritical() << "Caught unhandled unknown exception";
559 if ( qApp->thread() == QThread::currentThread() )
560 QMessageBox::critical( activeWindow(), tr( "Exception" ), tr( "unknown exception" ) );
561 }
562
563 return done;
564}
565
567{
568 return QgsRuntimeProfiler::threadLocalInstance();
569}
570
572{
573 // Set receiver for FileOpen events
574 ABISYM( mFileOpenEventReceiver ) = receiver;
575 // Propagate any events collected before the receiver has registered.
576 if ( sFileOpenEventList()->count() > 0 )
577 {
578 const QStringList fileOpenEventList = *sFileOpenEventList();
579 for ( const QString &file : fileOpenEventList )
580 {
581 QFileOpenEvent foe( file );
582 QgsApplication::sendEvent( ABISYM( mFileOpenEventReceiver ), &foe );
583 }
584 sFileOpenEventList()->clear();
585 }
586}
587
588void QgsApplication::setPrefixPath( const QString &prefixPath, bool useDefaultPaths )
589{
590 *sPrefixPath() = prefixPath;
591#if defined(Q_OS_WIN)
592 if ( sPrefixPath()->endsWith( "/bin" ) )
593 {
594 sPrefixPath()->chop( 4 );
595 }
596#endif
597 if ( useDefaultPaths && !ABISYM( mRunningFromBuildDir ) )
598 {
599 setPluginPath( *sPrefixPath() + '/' + QStringLiteral( QGIS_PLUGIN_SUBDIR ) );
600 setPkgDataPath( *sPrefixPath() + '/' + QStringLiteral( QGIS_DATA_SUBDIR ) );
601 }
602 *sLibraryPath() = *sPrefixPath() + '/' + QGIS_LIB_SUBDIR + '/';
603 *sLibexecPath() = *sPrefixPath() + '/' + QGIS_LIBEXEC_SUBDIR + '/';
604#if defined( HAVE_QUICK )
605 *sQmlImportPath() = *sPrefixPath() + '/' + QGIS_QML_SUBDIR + '/';
606#endif
607}
608
609void QgsApplication::setPluginPath( const QString &pluginPath )
610{
611 *sPluginPath() = pluginPath;
612}
613
614void QgsApplication::setPkgDataPath( const QString &pkgDataPath )
615{
616 *sPkgDataPath() = pkgDataPath;
617
618 QString mySvgPath = pkgDataPath + QStringLiteral( "/svg/" );
619
620 // avoid duplicate entries
621 if ( !sDefaultSvgPaths()->contains( mySvgPath ) )
622 *sDefaultSvgPaths() << mySvgPath;
623}
624
625void QgsApplication::setDefaultSvgPaths( const QStringList &pathList )
626{
627 *sDefaultSvgPaths() = pathList;
628}
629
630void QgsApplication::setAuthDatabaseDirPath( const QString &authDbDirPath )
631{
632 QFileInfo fi( authDbDirPath );
633 if ( fi.exists() && fi.isDir() && fi.isWritable() )
634 {
635 *sAuthDbDirPath() = fi.canonicalFilePath() + QDir::separator();
636 }
637}
638
640{
641#if 0
642 if ( ABISYM( mRunningFromBuildDir ) )
643 {
644 static bool sOnce = true;
645 if ( sOnce )
646 {
647 QgsMessageLogNotifyBlocker blockNotifications;
648 ( void ) blockNotifications;
649 qWarning( "!!! prefix path was requested, but it is not valid - we do not run from installed path !!!" );
650 }
651 sOnce = false;
652 }
653#endif
654
655 return *sPrefixPath();
656}
658{
659 return *sPluginPath();
660}
661
663{
664 if ( sPkgDataPath()->isNull() )
665 return resolvePkgPath();
666 else
667 return *sPkgDataPath();
668}
669
671{
672 return QStringLiteral( ":/images/themes/default/" );
673}
675{
676 QString usersThemes = userThemesFolder() + QDir::separator() + themeName() + QDir::separator() + "icons/";
677 QDir dir( usersThemes );
678 if ( dir.exists() )
679 {
680 return usersThemes;
681 }
682 else
683 {
684 QString defaultThemes = defaultThemesFolder() + QDir::separator() + themeName() + QDir::separator() + "icons/";
685 return defaultThemes;
686 }
687}
688
690{
691 return iconsPath() + QStringLiteral( "qgis-icon-60x60.png" );
692}
693
695{
696 return ABISYM( sMaxThreads );
697}
698
699QString QgsApplication::iconPath( const QString &iconFile )
700{
701 // try active theme
702 QString path = activeThemePath();
703 if ( QFile::exists( path + iconFile ) )
704 return path + iconFile;
705
706 // use default theme
707 return defaultThemePath() + iconFile;
708}
709
710QIcon QgsApplication::getThemeIcon( const QString &name, const QColor &fillColor, const QColor &strokeColor )
711{
712 const QString cacheKey = ( name.startsWith( '/' ) ? name.mid( 1 ) : name )
713 + ( fillColor.isValid() ? QStringLiteral( "_%1" ).arg( fillColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() )
714 + ( strokeColor.isValid() ? QStringLiteral( "_%1" ).arg( strokeColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() );
715 QgsApplication *app = instance();
716 if ( app && app->mIconCache.contains( cacheKey ) )
717 return app->mIconCache.value( cacheKey );
718
719 QIcon icon;
720 const bool colorBased = fillColor.isValid() || strokeColor.isValid();
721
722 auto iconFromColoredSvg = [ = ]( const QString & path ) -> QIcon
723 {
724 // sizes are unused here!
725 const QByteArray svgContent = QgsApplication::svgCache()->svgContent( path, 16, fillColor, strokeColor, 1, 1 );
726
727 const QString iconPath = sIconCacheDir()->filePath( cacheKey + QStringLiteral( ".svg" ) );
728 QFile f( iconPath );
729 if ( f.open( QFile::WriteOnly | QFile::Truncate ) )
730 {
731 f.write( svgContent );
732 f.close();
733 }
734 else
735 {
736 QgsDebugMsg( QStringLiteral( "Could not create colorized icon svg at %1" ).arg( iconPath ) );
737 return QIcon();
738 }
739
740 return QIcon( f.fileName() );
741 };
742
743 QString preferredPath = activeThemePath() + QDir::separator() + name;
744 QString defaultPath = defaultThemePath() + QDir::separator() + name;
745 if ( QFile::exists( preferredPath ) )
746 {
747 if ( colorBased )
748 {
749 icon = iconFromColoredSvg( preferredPath );
750 }
751 else
752 {
753 icon = QIcon( preferredPath );
754 }
755 }
756 else if ( QFile::exists( defaultPath ) )
757 {
758 //could still return an empty icon if it
759 //doesn't exist in the default theme either!
760 if ( colorBased )
761 {
762 icon = iconFromColoredSvg( defaultPath );
763 }
764 else
765 {
766 icon = QIcon( defaultPath );
767 }
768 }
769 else
770 {
771 icon = QIcon();
772 }
773
774 if ( app )
775 app->mIconCache.insert( cacheKey, icon );
776 return icon;
777}
778
780{
781 QgsApplication *app = instance();
782 if ( app && app->mCursorCache.contains( cursor ) )
783 return app->mCursorCache.value( cursor );
784
785 // All calculations are done on 32x32 icons
786 // Defaults to center, individual cursors may override
787 int activeX = 16;
788 int activeY = 16;
789
790 QString name;
791 switch ( cursor )
792 {
793 case ZoomIn:
794 name = QStringLiteral( "mZoomIn.svg" );
795 activeX = 13;
796 activeY = 13;
797 break;
798 case ZoomOut:
799 name = QStringLiteral( "mZoomOut.svg" );
800 activeX = 13;
801 activeY = 13;
802 break;
803 case Identify:
804 activeX = 3;
805 activeY = 6;
806 name = QStringLiteral( "mIdentify.svg" );
807 break;
808 case CrossHair:
809 name = QStringLiteral( "mCrossHair.svg" );
810 break;
811 case CapturePoint:
812 name = QStringLiteral( "mCapturePoint.svg" );
813 break;
814 case Select:
815 name = QStringLiteral( "mSelect.svg" );
816 activeX = 6;
817 activeY = 6;
818 break;
819 case Sampler:
820 activeX = 5;
821 activeY = 5;
822 name = QStringLiteral( "mSampler.svg" );
823 break;
824 // No default
825 }
826 // It should never get here!
827 Q_ASSERT( ! name.isEmpty( ) );
828
829 QIcon icon = getThemeIcon( QStringLiteral( "cursors" ) + QDir::separator() + name );
830 QCursor cursorIcon;
831 // Check if an icon exists for this cursor (the O.S. default cursor will be used if it does not)
832 if ( ! icon.isNull( ) )
833 {
834 // Apply scaling
835 float scale = Qgis::UI_SCALE_FACTOR * QgsApplication::fontMetrics().height() / 32.0;
836 cursorIcon = QCursor( icon.pixmap( std::ceil( scale * 32 ), std::ceil( scale * 32 ) ), std::ceil( scale * activeX ), std::ceil( scale * activeY ) );
837 }
838 if ( app )
839 app->mCursorCache.insert( cursor, cursorIcon );
840 return cursorIcon;
841}
842
843// TODO: add some caching mechanism ?
844QPixmap QgsApplication::getThemePixmap( const QString &name, const QColor &foreColor, const QColor &backColor, const int size )
845{
846 const QString preferredPath = activeThemePath() + QDir::separator() + name;
847 const QString defaultPath = defaultThemePath() + QDir::separator() + name;
848 const QString path = QFile::exists( preferredPath ) ? preferredPath : defaultPath;
849 if ( foreColor.isValid() || backColor.isValid() )
850 {
851 bool fitsInCache = false;
852 const QImage image = svgCache()->svgAsImage( path, size, backColor, foreColor, 1, 1, fitsInCache );
853 return QPixmap::fromImage( image );
854 }
855
856 return QPixmap( path );
857}
858
859void QgsApplication::setThemeName( const QString &themeName )
860{
861 *sThemeName() = themeName;
862}
863
865{
866 static QString appPath;
867 if ( appPath.isNull() )
868 {
869 if ( QCoreApplication::instance() )
870 {
871 appPath = applicationDirPath();
872 }
873 else
874 {
875 qWarning( "Application path not initialized" );
876 }
877 }
878
879 if ( !appPath.isNull() || getenv( "QGIS_PREFIX_PATH" ) )
880 {
881 QString prefix = getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : appPath;
882
883 // check if QGIS is run from build directory (not the install directory)
884 QFile f;
885 // "/../../.." is for Mac bundled app in build directory
886 static const QStringList paths { QStringList() << QString() << QStringLiteral( "/.." ) << QStringLiteral( "/bin" ) << QStringLiteral( "/../../.." ) };
887 for ( const QString &path : paths )
888 {
889 f.setFileName( prefix + path + "/qgisbuildpath.txt" );
890 if ( f.exists() )
891 break;
892 }
893 if ( f.exists() && f.open( QIODevice::ReadOnly ) )
894 {
895 ABISYM( mRunningFromBuildDir ) = true;
896 *sBuildSourcePath() = f.readLine().trimmed();
897 *sBuildOutputPath() = f.readLine().trimmed();
898 QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 );
899 QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( sBuildSourcePath()->toUtf8().constData() ), 4 );
900 QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( sBuildOutputPath()->toUtf8().constData() ), 4 );
901#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
902#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
903 *sCfgIntDir() = prefix.split( '/', QString::SkipEmptyParts ).last();
904#else
905 *sCfgIntDir() = prefix.split( '/', Qt::SkipEmptyParts ).last();
906#endif
907 qDebug( "- cfg: %s", sCfgIntDir()->toUtf8().constData() );
908#endif
909 }
910 }
911
912 QString prefixPath;
913 if ( getenv( "QGIS_PREFIX_PATH" ) )
914 prefixPath = getenv( "QGIS_PREFIX_PATH" );
915 else
916 {
917#if defined(ANDROID)
918 // this is "/data/data/org.qgis.qgis" in android
919 QDir dir( QDir::homePath() );
920 dir.cdUp();
921 prefixPath = dir.absolutePath();
922#else
923
924#if defined(Q_OS_MACX)
925 prefixPath = appPath;
926#elif defined(Q_OS_WIN)
927 prefixPath = appPath;
928 if ( prefixPath.endsWith( "/bin" ) )
929 prefixPath.chop( 4 );
930#else
931 QDir dir( appPath );
932 // Fix for server which is one level deeper in /usr/lib/cgi-bin
933 if ( appPath.contains( QStringLiteral( "cgi-bin" ) ) )
934 {
935 dir.cdUp();
936 }
937 dir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr
938 prefixPath = dir.absolutePath();
939#endif
940#endif
941 }
942
943 if ( ABISYM( mRunningFromBuildDir ) )
944 return *sBuildOutputPath() + QStringLiteral( "/data" );
945 else
946 return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR );
947}
948
950{
951 return *sThemeName();
952}
953
954void QgsApplication::setUITheme( const QString &themeName )
955{
956 // Loop all style sheets, find matching name, load it.
957 QHash<QString, QString> themes = QgsApplication::uiThemes();
958 if ( themeName == QLatin1String( "default" ) || !themes.contains( themeName ) )
959 {
960 setThemeName( QStringLiteral( "default" ) );
961 qApp->setStyleSheet( QString() );
962 return;
963 }
964
965 QString path = themes.value( themeName );
966 QString stylesheetname = path + "/style.qss";
967
968 QFile file( stylesheetname );
969 QFile variablesfile( path + "/variables.qss" );
970
971 QFileInfo variableInfo( variablesfile );
972
973 if ( !file.open( QIODevice::ReadOnly ) || ( variableInfo.exists() && !variablesfile.open( QIODevice::ReadOnly ) ) )
974 {
975 return;
976 }
977
978 QString styledata = file.readAll();
979 styledata.replace( QLatin1String( "@theme_path" ), path );
980
981 if ( variableInfo.exists() )
982 {
983 QTextStream in( &variablesfile );
984 while ( !in.atEnd() )
985 {
986 QString line = in.readLine();
987 // This is a variable
988 if ( line.startsWith( '@' ) )
989 {
990 int index = line.indexOf( ':' );
991 QString name = line.mid( 0, index );
992 QString value = line.mid( index + 1, line.length() );
993 styledata.replace( name, value );
994 }
995 }
996 variablesfile.close();
997 }
998 file.close();
999
1000 if ( Qgis::UI_SCALE_FACTOR != 1.0 )
1001 {
1002 // apply OS-specific UI scale factor to stylesheet's em values
1003 int index = 0;
1004 const static QRegularExpression regex( QStringLiteral( "(?<=[\\s:])([0-9\\.]+)(?=em)" ) );
1005 QRegularExpressionMatch match = regex.match( styledata, index );
1006 while ( match.hasMatch() )
1007 {
1008 index = match.capturedStart();
1009 styledata.remove( index, match.captured( 0 ).length() );
1010 QString number = QString::number( match.captured( 0 ).toDouble() * Qgis::UI_SCALE_FACTOR );
1011 styledata.insert( index, number );
1012 index += number.length();
1013 match = regex.match( styledata, index );
1014 }
1015 }
1016
1017 qApp->setStyleSheet( styledata );
1018
1019 QFile palettefile( path + "/palette.txt" );
1020 QFileInfo paletteInfo( palettefile );
1021 if ( paletteInfo.exists() && palettefile.open( QIODevice::ReadOnly ) )
1022 {
1023 QPalette pal = qApp->palette();
1024 QTextStream in( &palettefile );
1025 while ( !in.atEnd() )
1026 {
1027 QString line = in.readLine();
1028 QStringList parts = line.split( ':' );
1029 if ( parts.count() == 2 )
1030 {
1031 int role = parts.at( 0 ).trimmed().toInt();
1032 QColor color = QgsSymbolLayerUtils::decodeColor( parts.at( 1 ).trimmed() );
1033 pal.setColor( static_cast< QPalette::ColorRole >( role ), color );
1034 }
1035 }
1036 palettefile.close();
1037 qApp->setPalette( pal );
1038 }
1039
1041}
1042
1043QHash<QString, QString> QgsApplication::uiThemes()
1044{
1045 QStringList paths = QStringList() << userThemesFolder() << defaultThemesFolder();
1046 QHash<QString, QString> mapping;
1047 mapping.insert( QStringLiteral( "default" ), QString() );
1048 const auto constPaths = paths;
1049 for ( const QString &path : constPaths )
1050 {
1051 QDir folder( path );
1052 QFileInfoList styleFiles = folder.entryInfoList( QDir::Dirs | QDir::NoDotAndDotDot );
1053 const auto constStyleFiles = styleFiles;
1054 for ( const QFileInfo &info : constStyleFiles )
1055 {
1056 QFileInfo styleFile( info.absoluteFilePath() + "/style.qss" );
1057 if ( !styleFile.exists() )
1058 continue;
1059
1060 QString name = info.baseName();
1061 QString path = info.absoluteFilePath();
1062 mapping.insert( name, path );
1063 }
1064 }
1065 return mapping;
1066}
1067
1069{
1070 return pkgDataPath() + QStringLiteral( "/doc/AUTHORS" );
1071}
1072
1074{
1075 return pkgDataPath() + QStringLiteral( "/doc/CONTRIBUTORS" );
1076}
1078{
1079 return pkgDataPath() + QStringLiteral( "/doc/developersmap.html" );
1080}
1081
1083{
1084 return pkgDataPath() + QStringLiteral( "/doc/SPONSORS" );
1085}
1086
1088{
1089 return pkgDataPath() + QStringLiteral( "/doc/DONORS" );
1090}
1091
1093{
1094 return pkgDataPath() + QStringLiteral( "/doc/TRANSLATORS" );
1095}
1096
1098{
1099 return pkgDataPath() + QStringLiteral( "/doc/LICENSE" );
1100}
1101
1103{
1104 if ( ABISYM( mRunningFromBuildDir ) )
1105 return *sBuildOutputPath() + QStringLiteral( "/i18n/" );
1106 else
1107 return pkgDataPath() + QStringLiteral( "/i18n/" );
1108}
1109
1111{
1112 return pkgDataPath() + QStringLiteral( "/resources/metadata-ISO/" );
1113}
1114
1116{
1117 return pkgDataPath() + QStringLiteral( "/resources/qgis.db" );
1118}
1119
1121{
1122 return *sConfigPath();
1123}
1124
1126{
1127 return qgisSettingsDirPath() + QStringLiteral( "qgis.db" );
1128}
1129
1131{
1132 return *sAuthDbDirPath() + QStringLiteral( "qgis-auth.db" );
1133}
1134
1136{
1137 return QStringLiteral( ":/images/splash/" );
1138}
1139
1141{
1142 return pkgDataPath() + QStringLiteral( "/images/icons/" );
1143}
1144
1146{
1147 if ( ABISYM( mRunningFromBuildDir ) )
1148 {
1149 QString tempCopy = QDir::tempPath() + "/srs6.db";
1150
1151 if ( !QFile( tempCopy ).exists() )
1152 {
1153 QFile f( buildSourcePath() + "/resources/srs6.db" );
1154 if ( !f.copy( tempCopy ) )
1155 {
1156 qFatal( "Could not create temporary copy" );
1157 }
1158 }
1159
1160 return tempCopy;
1161 }
1162 else
1163 {
1164 return pkgDataPath() + QStringLiteral( "/resources/srs.db" );
1165 }
1166}
1167
1168void QgsApplication::setSvgPaths( const QStringList &svgPaths )
1169{
1171 members()->mSvgPathCacheValid = false;
1172}
1173
1175{
1176 static QReadWriteLock lock;
1177
1179
1180 if ( members()->mSvgPathCacheValid )
1181 {
1182 return members()->mSvgPathCache;
1183 }
1184 else
1185 {
1187 //local directories to search when looking for an SVG with a given basename
1188 //defined by user in options dialog
1189 const QStringList pathList = settingsSearchPathsForSVG.value();
1190
1191 // maintain user set order while stripping duplicates
1192 QStringList paths;
1193 for ( const QString &path : pathList )
1194 {
1195 if ( !paths.contains( path ) )
1196 paths.append( path );
1197 }
1198 for ( const QString &path : std::as_const( *sDefaultSvgPaths() ) )
1199 {
1200 if ( !paths.contains( path ) )
1201 paths.append( path );
1202 }
1203 members()->mSvgPathCache = paths;
1204
1205 return paths;
1206 }
1207}
1208
1210{
1211 //local directories to search when looking for an template with a given basename
1212 //defined by user in options dialog
1214}
1215
1216QMap<QString, QString> QgsApplication::systemEnvVars()
1217{
1218 return *sSystemEnvVars();
1219}
1220
1222{
1223 return qgisSettingsDirPath() + QStringLiteral( "symbology-style.db" );
1224}
1225
1227{
1228 const thread_local QRegularExpression regexp( QRegularExpression::anchoredPattern( QStringLiteral( "^[A-Za-z][A-Za-z0-9\\._-]*" ) ) );
1229 return regexp;
1230}
1231
1233{
1234 if ( !sUserName()->isEmpty() )
1235 return *sUserName();
1236
1237#ifdef _MSC_VER
1238 TCHAR name [ UNLEN + 1 ];
1239 DWORD size = UNLEN + 1;
1240
1241 if ( GetUserName( ( TCHAR * )name, &size ) )
1242 {
1243#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1244 *sUserName() = QString::fromLocal8Bit( name );
1245#else
1246 *sUserName() = QString::fromWCharArray( name );
1247#endif
1248 }
1249
1250
1251#elif QT_CONFIG(process)
1252 QProcess process;
1253
1254 process.start( QStringLiteral( "whoami" ), QStringList() );
1255 process.waitForFinished();
1256 *sUserName() = process.readAllStandardOutput().trimmed();
1257#endif
1258
1259 if ( !sUserName()->isEmpty() )
1260 return *sUserName();
1261
1262 //backup plan - use environment variables
1263 *sUserName() = qgetenv( "USER" );
1264 if ( !sUserName()->isEmpty() )
1265 return *sUserName();
1266
1267 //last resort
1268 *sUserName() = qgetenv( "USERNAME" );
1269 return *sUserName();
1270}
1271
1273{
1274 if ( !sUserFullName()->isEmpty() )
1275 return *sUserFullName();
1276
1277#ifdef _MSC_VER
1278 TCHAR name [ UNLEN + 1 ];
1279 DWORD size = UNLEN + 1;
1280
1281 //note - this only works for accounts connected to domain
1282 if ( GetUserNameEx( NameDisplay, ( TCHAR * )name, &size ) )
1283 {
1284#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1285 *sUserFullName() = QString::fromLocal8Bit( name );
1286#else
1287 *sUserFullName() = QString::fromWCharArray( name );
1288#endif
1289 }
1290
1291 //fall back to login name
1292 if ( sUserFullName()->isEmpty() )
1293 *sUserFullName() = userLoginName();
1294#elif defined(Q_OS_ANDROID) || defined(__MINGW32__)
1295 *sUserFullName() = QStringLiteral( "Not available" );
1296#else
1297 struct passwd *p = getpwuid( getuid() );
1298
1299 if ( p )
1300 {
1301 QString gecosName = QString( p->pw_gecos );
1302 *sUserFullName() = gecosName.left( gecosName.indexOf( ',', 0 ) );
1303 }
1304
1305#endif
1306
1307 return *sUserFullName();
1308}
1309
1311{
1312#if defined(Q_OS_ANDROID)
1313 return QLatin1String( "android" );
1314#elif defined(Q_OS_MAC)
1315 return QLatin1String( "osx" );
1316#elif defined(Q_OS_WIN)
1317 return QLatin1String( "windows" );
1318#elif defined(Q_OS_LINUX)
1319 return QStringLiteral( "linux" );
1320#elif defined(Q_OS_FREEBSD)
1321 return QStringLiteral( "freebsd" );
1322#elif defined(Q_OS_OPENBSD)
1323 return QStringLiteral( "openbsd" );
1324#elif defined(Q_OS_NETBSD)
1325 return QStringLiteral( "netbsd" );
1326#elif defined(Q_OS_UNIX)
1327 return QLatin1String( "unix" );
1328#else
1329 return QLatin1String( "unknown" );
1330#endif
1331}
1332
1334{
1335#if defined(Q_OS_ANDROID)
1336 return -1;
1337#elif defined(Q_OS_MAC)
1338 return -1;
1339#elif defined(Q_OS_WIN)
1340 MEMORYSTATUSEX memoryStatus;
1341 ZeroMemory( &memoryStatus, sizeof( MEMORYSTATUSEX ) );
1342 memoryStatus.dwLength = sizeof( MEMORYSTATUSEX );
1343 if ( GlobalMemoryStatusEx( &memoryStatus ) )
1344 {
1345 return memoryStatus.ullTotalPhys / ( 1024 * 1024 );
1346 }
1347 else
1348 {
1349 return -1;
1350 }
1351#elif defined(Q_OS_LINUX)
1352 constexpr int megabyte = 1024 * 1024;
1353 struct sysinfo si;
1354 sysinfo( &si );
1355 return si.totalram / megabyte;
1356#elif defined(Q_OS_FREEBSD)
1357 return -1;
1358#elif defined(Q_OS_OPENBSD)
1359 return -1;
1360#elif defined(Q_OS_NETBSD)
1361 return -1;
1362#elif defined(Q_OS_UNIX)
1363 return -1;
1364#else
1365 return -1;
1366#endif
1367}
1368
1370{
1371 return *sPlatformName();
1372}
1373
1375{
1377 {
1379 // don't differentiate en_US and en_GB
1380 if ( locale.startsWith( QLatin1String( "en" ), Qt::CaseInsensitive ) )
1381 {
1382 return locale.left( 2 );
1383 }
1384
1385 return locale;
1386 }
1387 else
1388 {
1389 return QLocale().name().left( 2 );
1390 }
1391}
1392
1393void QgsApplication::setLocale( const QLocale &locale )
1394{
1395 QLocale::setDefault( locale );
1396 emit instance()->localeChanged();
1397}
1398
1400{
1401 return qgisSettingsDirPath() + QStringLiteral( "/themes" );
1402}
1403
1405{
1406 return pkgDataPath() + QStringLiteral( "/resources/symbology-style.xml" );
1407}
1408
1410{
1411 return pkgDataPath() + QStringLiteral( "/resources/themes" );
1412}
1413
1415{
1416 return pkgDataPath() + QStringLiteral( "/resources/server/" );
1417}
1418
1420{
1421 return *sLibraryPath();
1422}
1423
1425{
1426 return *sLibexecPath();
1427}
1428
1430{
1431 return *sQmlImportPath();
1432}
1433
1435{
1436 return ( htonl( 1 ) == 1 ) ? XDR : NDR;
1437}
1438
1440{
1441 if ( !ABISYM( mInitialized ) && QgsApplication::instance() )
1442 {
1443 init( *sProfilePath() );
1444 }
1445
1446 // set the provider plugin path (this creates provider registry)
1448
1449 // create data item provider registry
1451
1452 // create project instance if doesn't exist
1454
1455 // Initialize authentication manager and connect to database
1457
1458 // Make sure we have a NAM created on the main thread.
1459 // Note that this might call QgsApplication::authManager to
1460 // setup the proxy configuration that's why it needs to be
1461 // called after the QgsAuthManager instance has been created
1463
1464}
1465
1467{
1468 if ( auto *lInstance = instance() )
1469 {
1470 if ( !lInstance->mAuthManager )
1471 {
1472 lInstance->mAuthManager = QgsAuthManager::instance();
1473 }
1474 return lInstance->mAuthManager;
1475 }
1476 else
1477 {
1478 // no QgsApplication instance
1479 if ( !sAuthManager )
1480 sAuthManager = QgsAuthManager::instance();
1481 return sAuthManager;
1482 }
1483}
1484
1485
1487{
1488 // make sure all threads are done before exiting
1489 QThreadPool::globalInstance()->waitForDone();
1490
1491 // don't create to delete
1492 if ( auto *lInstance = instance() )
1493 delete lInstance->mAuthManager;
1494 else
1495 delete sAuthManager;
1496
1497 //Ensure that all remaining deleteLater QObjects are actually deleted before we exit.
1498 //This isn't strictly necessary (since we're exiting anyway) but doing so prevents a lot of
1499 //LeakSanitiser noise which hides real issues
1500 QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
1501
1502 //delete all registered functions from expression engine (see above comment)
1504
1505 // avoid creating instance just to delete it!
1506 if ( QgsProject::sProject )
1507 delete QgsProject::instance();
1508
1509 // avoid creating instance just to delete it!
1510 if ( QgsProviderRegistry::exists() )
1512
1513 invalidateCaches();
1514
1516
1517 // tear-down GDAL/OGR
1518 OGRCleanupAll();
1519 GDALDestroyDriverManager();
1520}
1521
1523{
1524 QString myEnvironmentVar( getenv( "QGIS_PREFIX_PATH" ) );
1525 QString myState = tr( "Application state:\n"
1526 "QGIS_PREFIX_PATH env var:\t\t%1\n"
1527 "Prefix:\t\t%2\n"
1528 "Plugin Path:\t\t%3\n"
1529 "Package Data Path:\t%4\n"
1530 "Active Theme Name:\t%5\n"
1531 "Active Theme Path:\t%6\n"
1532 "Default Theme Path:\t%7\n"
1533 "SVG Search Paths:\t%8\n"
1534 "User DB Path:\t%9\n"
1535 "Auth DB Path:\t%10\n" )
1536 .arg( myEnvironmentVar,
1537 prefixPath(),
1538 pluginPath(),
1539 pkgDataPath(),
1540 themeName(),
1543 svgPaths().join( tr( "\n\t\t", "match indentation of application state" ) ),
1545 .arg( qgisAuthDatabaseFilePath() );
1546 return myState;
1547}
1548
1550{
1551 //
1552 // Make the style sheet desktop preferences aware by using qapplication
1553 // palette as a basis for colors where appropriate
1554 //
1555 // QColor myColor1 = palette().highlight().color();
1556 QColor myColor1( Qt::lightGray );
1557 QColor myColor2 = myColor1;
1558 myColor2 = myColor2.lighter( 110 ); //10% lighter
1559 QString myStyle;
1560 myStyle = QStringLiteral( ".overview{"
1561 " font: 1.82em;"
1562 " font-weight: bold;"
1563 "}"
1564 "body{"
1565 " background: white;"
1566 " color: black;"
1567 " font-family: 'Lato', 'Open Sans', 'Lucida Grande', 'Segoe UI', 'Arial', sans-serif;"
1568 " width: 100%;"
1569 "}"
1570 "h1{ background-color: #F6F6F6;"
1571 " color: #589632; " // from http://qgis.org/en/site/getinvolved/styleguide.html
1572 " font-size: x-large; "
1573 " font-weight: normal;"
1574 " background: none;"
1575 " padding: 0.75em 0 0;"
1576 " margin: 0;"
1577 " line-height: 3em;"
1578 "}"
1579 "h2{ background-color: #F6F6F6;"
1580 " color: #589632; " // from http://qgis.org/en/site/getinvolved/styleguide.html
1581 " font-size: medium; "
1582 " font-weight: normal;"
1583 " background: none;"
1584 " padding: 0.75em 0 0;"
1585 " margin: 0;"
1586 " line-height: 1.1em;"
1587 "}"
1588 "h3{ background-color: #F6F6F6;"
1589 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1590 " font-weight: bold;"
1591 " font-size: large;"
1592 " text-align: left;"
1593 " border-bottom: 5px solid #DCEB5C;"
1594 "}"
1595 "h4{ background-color: #F6F6F6;"
1596 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1597 " font-weight: bold;"
1598 " font-size: medium;"
1599 " text-align: left;"
1600 "}"
1601 "h5{ background-color: #F6F6F6;"
1602 " color: #93b023;" // from http://qgis.org/en/site/getinvolved/styleguide.html
1603 " font-weight: bold;"
1604 " font-size: small;"
1605 " text-align: left;"
1606 "}"
1607 "a{ color: #729FCF;"
1608 " font-family: arial,sans-serif;"
1609 "}"
1610 "label{ background-color: #FFFFCC;"
1611 " border: 1px solid black;"
1612 " margin: 1px;"
1613 " padding: 0px 3px; "
1614 " font-size: small;"
1615 "}"
1616 "th .strong {"
1617 " font-weight: bold;"
1618 "}"
1619 "hr {"
1620 " border: 0;"
1621 " height: 0;"
1622 " border-top: 1px solid black;"
1623 "}"
1624 ".list-view .highlight {"
1625 " text-align: left;"
1626 " border: 0px;"
1627 " width: 20%;"
1628 " padding-right: 15px;"
1629 " padding-left: 20px;"
1630 " font-weight: bold;"
1631 "}"
1632 ".tabular-view .odd-row {"
1633 " background-color: #f9f9f9;"
1634 "}"
1635 ".section {"
1636 " font-weight: bold;"
1637 " padding-top:25px;"
1638 "}" );
1639
1640 // We have some subtle differences between Qt based style and QWebKit style
1641 switch ( styleSheetType )
1642 {
1643 case StyleSheetType::Qt:
1644 myStyle += QStringLiteral(
1645 ".tabular-view{ "
1646 " border-collapse: collapse;"
1647 " width: 95%;"
1648 "}"
1649 ".tabular-view th, .tabular-view td { "
1650 " border:1px solid black;"
1651 "}" );
1652 break;
1653
1654 case StyleSheetType::WebBrowser:
1655 myStyle += QStringLiteral(
1656 "body { "
1657 " margin: auto;"
1658 " width: 97%;"
1659 "}"
1660 "table.tabular-view, table.list-view { "
1661 " border-collapse: collapse;"
1662 " table-layout:fixed;"
1663 " width: 100% !important;"
1664 " font-size: 90%;"
1665 "}"
1666 // Override
1667 "h1 { "
1668 " line-height: inherit;"
1669 "}"
1670 "td, th {"
1671 " word-wrap: break-word; "
1672 " vertical-align: top;"
1673 "}"
1674 // Set first column width
1675 ".list-view th:first-child, .list-view td:first-child {"
1676 " width: 20%;"
1677 "}"
1678 ".list-view.highlight { "
1679 " padding-left: inherit; "
1680 "}"
1681 // Set first column width for inner tables
1682 ".tabular-view th:first-child, .tabular-view td:first-child { "
1683 " width: 20%; "
1684 "}"
1685 // Makes titles bg stand up
1686 ".tabular-view th.strong { "
1687 " background-color: #eee; "
1688 "}"
1689 // Give some visual appearance to those ugly nested tables
1690 ".tabular-view th, .tabular-view td { "
1691 " border: 1px solid #eee;"
1692 "}"
1693 );
1694 break;
1695 }
1696
1697 return myStyle;
1698}
1699
1701{
1702 if ( 0 >= OGRGetDriverCount() )
1703 {
1704 OGRRegisterAll();
1705 }
1706}
1707
1708QString QgsApplication::absolutePathToRelativePath( const QString &aPath, const QString &targetPath )
1709{
1710 QString aPathUrl = aPath;
1711 QString tPathUrl = targetPath;
1712#if defined( Q_OS_WIN )
1713 const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
1714
1715 aPathUrl.replace( '\\', '/' );
1716 if ( aPathUrl.startsWith( "//" ) )
1717 {
1718 // keep UNC prefix
1719 aPathUrl = "\\\\" + aPathUrl.mid( 2 );
1720 }
1721
1722 tPathUrl.replace( '\\', '/' );
1723 if ( tPathUrl.startsWith( "//" ) )
1724 {
1725 // keep UNC prefix
1726 tPathUrl = "\\\\" + tPathUrl.mid( 2 );
1727 }
1728#else
1729 const Qt::CaseSensitivity cs = Qt::CaseSensitive;
1730#endif
1731
1732#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
1733 QStringList targetElems = tPathUrl.split( '/', QString::SkipEmptyParts );
1734 QStringList aPathElems = aPathUrl.split( '/', QString::SkipEmptyParts );
1735#else
1736 QStringList targetElems = tPathUrl.split( '/', Qt::SkipEmptyParts );
1737 QStringList aPathElems = aPathUrl.split( '/', Qt::SkipEmptyParts );
1738#endif
1739
1740 targetElems.removeAll( QStringLiteral( "." ) );
1741 aPathElems.removeAll( QStringLiteral( "." ) );
1742
1743 // remove common part
1744 int n = 0;
1745 while ( !aPathElems.isEmpty() &&
1746 !targetElems.isEmpty() &&
1747 aPathElems[0].compare( targetElems[0], cs ) == 0 )
1748 {
1749 aPathElems.removeFirst();
1750 targetElems.removeFirst();
1751 n++;
1752 }
1753
1754 if ( n == 0 )
1755 {
1756 // no common parts; might not even be a file
1757 return aPathUrl;
1758 }
1759
1760 if ( !targetElems.isEmpty() )
1761 {
1762 // go up to the common directory
1763 for ( int i = 0; i < targetElems.size(); i++ )
1764 {
1765 aPathElems.insert( 0, QStringLiteral( ".." ) );
1766 }
1767 }
1768 else
1769 {
1770 // let it start with . nevertheless,
1771 // so relative path always start with either ./ or ../
1772 aPathElems.insert( 0, QStringLiteral( "." ) );
1773 }
1774
1775 return aPathElems.join( QLatin1Char( '/' ) );
1776}
1777
1778QString QgsApplication::relativePathToAbsolutePath( const QString &rpath, const QString &targetPath )
1779{
1780 // relative path should always start with ./ or ../
1781 if ( !rpath.startsWith( QLatin1String( "./" ) ) && !rpath.startsWith( QLatin1String( "../" ) ) )
1782 {
1783 return rpath;
1784 }
1785
1786 QString rPathUrl = rpath;
1787 QString targetPathUrl = targetPath;
1788
1789#if defined(Q_OS_WIN)
1790 rPathUrl.replace( '\\', '/' );
1791 targetPathUrl.replace( '\\', '/' );
1792
1793 bool uncPath = targetPathUrl.startsWith( "//" );
1794#endif
1795
1796#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
1797 QStringList srcElems = rPathUrl.split( '/', QString::SkipEmptyParts );
1798 QStringList targetElems = targetPathUrl.split( '/', QString::SkipEmptyParts );
1799#else
1800 QStringList srcElems = rPathUrl.split( '/', Qt::SkipEmptyParts );
1801 QStringList targetElems = targetPathUrl.split( '/', Qt::SkipEmptyParts );
1802#endif
1803
1804#if defined(Q_OS_WIN)
1805 if ( uncPath )
1806 {
1807 targetElems.insert( 0, "" );
1808 targetElems.insert( 0, "" );
1809 }
1810#endif
1811
1812 // append source path elements
1813 targetElems << srcElems;
1814 targetElems.removeAll( QStringLiteral( "." ) );
1815
1816 // resolve ..
1817 int pos;
1818 while ( ( pos = targetElems.indexOf( QLatin1String( ".." ) ) ) > 0 )
1819 {
1820 // remove preceding element and ..
1821 targetElems.removeAt( pos - 1 );
1822 targetElems.removeAt( pos - 1 );
1823 }
1824
1825#if !defined(Q_OS_WIN)
1826 // make path absolute
1827 targetElems.prepend( QString() );
1828#endif
1829
1830 return targetElems.join( QLatin1Char( '/' ) );
1831}
1832
1834{
1835 return *sBuildSourcePath();
1836}
1837
1839{
1840 return *sBuildOutputPath();
1841}
1842
1843#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
1844QString QgsApplication::cfgIntDir()
1845{
1846 return *sCfgIntDir();
1847}
1848#endif
1849
1850void QgsApplication::skipGdalDriver( const QString &driver )
1851{
1852 if ( sGdalSkipList()->contains( driver ) || driver.isEmpty() )
1853 {
1854 return;
1855 }
1856 *sGdalSkipList() << driver;
1858}
1859
1860void QgsApplication::restoreGdalDriver( const QString &driver )
1861{
1862 if ( !sGdalSkipList()->contains( driver ) )
1863 {
1864 return;
1865 }
1866 int myPos = sGdalSkipList()->indexOf( driver );
1867 if ( myPos >= 0 )
1868 {
1869 sGdalSkipList()->removeAt( myPos );
1870 }
1872}
1873
1875{
1876 return *sGdalSkipList();
1877}
1878
1879void QgsApplication::setSkippedGdalDrivers( const QStringList &skippedGdalDrivers,
1880 const QStringList &deferredSkippedGdalDrivers )
1881{
1882 *sGdalSkipList() = skippedGdalDrivers;
1883 *sDeferredSkippedGdalDrivers() = deferredSkippedGdalDrivers;
1884
1885 QgsSettings settings;
1886 settings.setValue( QStringLiteral( "gdal/skipDrivers" ), skippedGdalDrivers.join( QLatin1Char( ',' ) ) );
1887
1889}
1890
1892{
1893 QgsSettings settings;
1894 QString joinedList, delimiter;
1895 if ( settings.contains( QStringLiteral( "gdal/skipDrivers" ) ) )
1896 {
1897 joinedList = settings.value( QStringLiteral( "gdal/skipDrivers" ), QString() ).toString();
1898 delimiter = QStringLiteral( "," );
1899 }
1900 else
1901 {
1902 joinedList = settings.value( QStringLiteral( "gdal/skipList" ), QString() ).toString();
1903 delimiter = QStringLiteral( " " );
1904 }
1905 QStringList myList;
1906 if ( !joinedList.isEmpty() )
1907 {
1908 myList = joinedList.split( delimiter );
1909 }
1910 *sGdalSkipList() = myList;
1912}
1913
1915{
1916 return *sDeferredSkippedGdalDrivers();
1917}
1918
1920{
1921 sGdalSkipList()->removeDuplicates();
1922 QStringList realDisabledDriverList;
1923 for ( const auto &driverName : *sGdalSkipList() )
1924 {
1925 if ( !sDeferredSkippedGdalDrivers()->contains( driverName ) )
1926 realDisabledDriverList << driverName;
1927 }
1928 QString myDriverList = realDisabledDriverList.join( ',' );
1929 QgsDebugMsgLevel( QStringLiteral( "Gdal Skipped driver list set to:" ), 2 );
1930 QgsDebugMsgLevel( myDriverList, 2 );
1931 CPLSetConfigOption( "GDAL_SKIP", myDriverList.toUtf8() );
1932 GDALAllRegister(); //to update driver list and skip missing ones
1933}
1934
1936{
1937 QString folder = userThemesFolder();
1938 QDir myDir( folder );
1939 if ( !myDir.exists() )
1940 {
1941 myDir.mkpath( folder );
1942 }
1943
1944 return true;
1945}
1946
1947void QgsApplication::copyPath( const QString &src, const QString &dst )
1948{
1949 QDir dir( src );
1950 if ( ! dir.exists() )
1951 return;
1952
1953 const auto subDirectories = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot );
1954 for ( const QString &d : subDirectories )
1955 {
1956 QString dst_path = dst + QDir::separator() + d;
1957 dir.mkpath( dst_path );
1958 copyPath( src + QDir::separator() + d, dst_path );
1959 }
1960
1961 const auto files = dir.entryList( QDir::Files );
1962 for ( const QString &f : files )
1963 {
1964 QFile::copy( src + QDir::separator() + f, dst + QDir::separator() + f );
1965 }
1966}
1967
1969{
1970 //read values from QgsSettings
1971 QgsSettings settings;
1972
1973 QVariantMap variables;
1974
1975 //check if settings contains any variables
1976 settings.beginGroup( "variables" );
1977 QStringList childKeys = settings.childKeys();
1978 for ( QStringList::const_iterator it = childKeys.constBegin(); it != childKeys.constEnd(); ++it )
1979 {
1980 QString name = *it;
1981 variables.insert( name, settings.value( name ) );
1982 }
1983
1984 return variables;
1985}
1986
1987void QgsApplication::setCustomVariables( const QVariantMap &variables )
1988{
1989 QgsSettings settings;
1990
1991 QVariantMap::const_iterator it = variables.constBegin();
1992 settings.beginGroup( "variables" );
1993 settings.remove( "" );
1994 for ( ; it != variables.constEnd(); ++it )
1995 {
1996 settings.setValue( it.key(), it.value() );
1997 }
1998
2000}
2001
2002void QgsApplication::setCustomVariable( const QString &name, const QVariant &value )
2003{
2004 // save variable to settings
2005 QgsSettings settings;
2006
2007 settings.setValue( QStringLiteral( "variables/" ) + name, value );
2008
2010}
2011
2012int QgsApplication::scaleIconSize( int standardSize, bool applyDevicePixelRatio )
2013{
2014 QFontMetrics fm( ( QFont() ) );
2015 const double scale = 1.1 * standardSize / 24;
2016 int scaledIconSize = static_cast< int >( std::floor( std::max( Qgis::UI_SCALE_FACTOR * fm.height() * scale, static_cast< double >( standardSize ) ) ) );
2017 if ( applyDevicePixelRatio )
2018 {
2019 if ( QWidget *activeWindow = QApplication::activeWindow() )
2020 scaledIconSize *= ( activeWindow->screen() ? QApplication::activeWindow()->screen()->devicePixelRatio() : 1 );
2021 }
2022 return scaledIconSize;
2023}
2024
2026{
2028}
2029
2030void QgsApplication::setTranslation( const QString &translation )
2031{
2032 *sTranslation() = translation;
2033}
2034
2036{
2037 return *sTranslation();
2038}
2039
2041{
2042 emit requestForTranslatableObjects( translationContext );
2043}
2044
2046{
2047 ApplicationMembers *appMembers = members();
2048 if ( appMembers->mNullRepresentation.isNull() )
2049 {
2050 appMembers->mNullRepresentation = QgsSettings().value( QStringLiteral( "qgis/nullValue" ), QStringLiteral( "NULL" ) ).toString();
2051 }
2052 return appMembers->mNullRepresentation;
2053}
2054
2055void QgsApplication::setNullRepresentation( const QString &nullRepresentation )
2056{
2057 ApplicationMembers *appMembers = members();
2058 if ( !appMembers || appMembers->mNullRepresentation == nullRepresentation )
2059 return;
2060
2061 appMembers->mNullRepresentation = nullRepresentation;
2062 QgsSettings().setValue( QStringLiteral( "qgis/nullValue" ), nullRepresentation );
2063
2064 QgsApplication *app = instance();
2065 if ( app )
2066 emit app->nullRepresentationChanged();
2067}
2068
2070{
2071 return members()->mActionScopeRegistry;
2072}
2073
2074bool QgsApplication::createDatabase( QString *errorMessage )
2075{
2076 // set a working directory up for gdal to write .aux.xml files into
2077 // for cases where the raster dir is read only to the user
2078 // if the env var is already set it will be used preferentially
2079 QString myPamPath = qgisSettingsDirPath() + QStringLiteral( "gdal_pam/" );
2080 QDir myDir( myPamPath );
2081 if ( !myDir.exists() )
2082 {
2083 myDir.mkpath( myPamPath ); //fail silently
2084 }
2085
2086#if defined(Q_OS_WIN)
2087 CPLSetConfigOption( "GDAL_PAM_PROXY_DIR", myPamPath.toUtf8() );
2088#else
2089 //under other OS's we use an environment var so the user can
2090 //override the path if he likes
2091 int myChangeFlag = 0; //whether we want to force the env var to change
2092 setenv( "GDAL_PAM_PROXY_DIR", myPamPath.toUtf8(), myChangeFlag );
2093#endif
2094
2095 // Check qgis.db and make private copy if necessary
2096 QFile qgisPrivateDbFile( QgsApplication::qgisUserDatabaseFilePath() );
2097
2098 // first we look for ~/.qgis/qgis.db
2099 if ( !qgisPrivateDbFile.exists() )
2100 {
2101 // if it doesn't exist we copy it in from the global resources dir
2102 QString qgisMasterDbFileName = QgsApplication::qgisMasterDatabaseFilePath();
2103 QFile masterFile( qgisMasterDbFileName );
2104
2105 // Must be sure there is destination directory ~/.qgis
2106 QDir().mkpath( QgsApplication::qgisSettingsDirPath() );
2107
2108 //now copy the master file into the users .qgis dir
2109 bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );
2110
2111 if ( !isDbFileCopied )
2112 {
2113 if ( errorMessage )
2114 {
2115 *errorMessage = tr( "[ERROR] Can not make qgis.db private copy" );
2116 }
2117 return false;
2118 }
2119
2120 QFile::Permissions perms = QFile( qgisPrivateDbFile.fileName() ).permissions();
2121 if ( !( perms & QFile::WriteOwner ) )
2122 {
2123 if ( !qgisPrivateDbFile.setPermissions( perms | QFile::WriteOwner ) )
2124 {
2125 if ( errorMessage )
2126 {
2127 *errorMessage = tr( "Can not make '%1' user writable" ).arg( qgisPrivateDbFile.fileName() );
2128 }
2129 return false;
2130 }
2131 }
2132 }
2133 else
2134 {
2135 // migrate if necessary
2137 if ( database.open( QgsApplication::qgisUserDatabaseFilePath() ) != SQLITE_OK )
2138 {
2139 if ( errorMessage )
2140 {
2141 *errorMessage = tr( "Could not open qgis.db" );
2142 }
2143 return false;
2144 }
2145
2146 char *errmsg = nullptr;
2147 int res = sqlite3_exec( database.get(), "SELECT srs_id FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2148 if ( res != SQLITE_OK )
2149 {
2150 sqlite3_free( errmsg );
2151
2152 // qgis.db is missing tbl_srs, create it
2153 if ( sqlite3_exec( database.get(),
2154 "DROP INDEX IF EXISTS idx_srsauthid;"
2155 "CREATE TABLE tbl_srs ("
2156 "srs_id INTEGER PRIMARY KEY,"
2157 "description text NOT NULL,"
2158 "projection_acronym text NOT NULL,"
2159 "ellipsoid_acronym NOT NULL,"
2160 "parameters text NOT NULL,"
2161 "srid integer,"
2162 "auth_name varchar,"
2163 "auth_id varchar,"
2164 "is_geo integer NOT NULL,"
2165 "deprecated boolean,"
2166 "wkt text);"
2167 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2168 {
2169 if ( errorMessage )
2170 {
2171 *errorMessage = tr( "Creation of missing tbl_srs in the private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2172 }
2173 sqlite3_free( errmsg );
2174 return false;
2175 }
2176 }
2177 else
2178 {
2179 // test if wkt column exists in database
2180 res = sqlite3_exec( database.get(), "SELECT wkt FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2181 if ( res != SQLITE_OK )
2182 {
2183 // need to add wkt column
2184 sqlite3_free( errmsg );
2185 if ( sqlite3_exec( database.get(),
2186 "DROP INDEX IF EXISTS idx_srsauthid;"
2187 "DROP TABLE IF EXISTS tbl_srs_bak;"
2188 "ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
2189 "CREATE TABLE tbl_srs ("
2190 "srs_id INTEGER PRIMARY KEY,"
2191 "description text NOT NULL,"
2192 "projection_acronym text NOT NULL,"
2193 "ellipsoid_acronym NOT NULL,"
2194 "parameters text NOT NULL,"
2195 "srid integer,"
2196 "auth_name varchar,"
2197 "auth_id varchar,"
2198 "is_geo integer NOT NULL,"
2199 "deprecated boolean,"
2200 "wkt text);"
2201 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
2202 "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;"
2203 "DROP TABLE tbl_srs_bak", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2204 {
2205 if ( errorMessage )
2206 {
2207 *errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2208 }
2209 sqlite3_free( errmsg );
2210 return false;
2211 }
2212 }
2213 }
2214
2215 res = sqlite3_exec( database.get(), "SELECT acronym FROM tbl_projection LIMIT 0", nullptr, nullptr, &errmsg );
2216 if ( res != SQLITE_OK )
2217 {
2218 sqlite3_free( errmsg );
2219
2220 // qgis.db is missing tbl_projection, create it
2221 if ( sqlite3_exec( database.get(),
2222 "CREATE TABLE tbl_projection ("
2223 "acronym varchar(20) NOT NULL PRIMARY KEY,"
2224 "name varchar(255) NOT NULL default '',"
2225 "notes varchar(255) NOT NULL default '',"
2226 "parameters varchar(255) NOT NULL default ''"
2227 ")", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2228 {
2229 if ( errorMessage )
2230 {
2231 *errorMessage = tr( "Creation of missing tbl_projection in the private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2232 }
2233 sqlite3_free( errmsg );
2234 return false;
2235 }
2236 }
2237
2238 res = sqlite3_exec( database.get(), "SELECT epsg FROM tbl_srs LIMIT 0", nullptr, nullptr, &errmsg );
2239 if ( res == SQLITE_OK )
2240 {
2241 // epsg column exists => need migration
2242 if ( sqlite3_exec( database.get(),
2243 "DROP INDEX IF EXISTS idx_srsauthid;"
2244 "DROP TABLE IF EXISTS tbl_srs_bak;"
2245 "ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
2246 "CREATE TABLE tbl_srs ("
2247 "srs_id INTEGER PRIMARY KEY,"
2248 "description text NOT NULL,"
2249 "projection_acronym text NOT NULL,"
2250 "ellipsoid_acronym NOT NULL,"
2251 "parameters text NOT NULL,"
2252 "srid integer,"
2253 "auth_name varchar,"
2254 "auth_id varchar,"
2255 "is_geo integer NOT NULL,"
2256 "deprecated boolean,"
2257 "wkt text);"
2258 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
2259 "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;"
2260 "DROP TABLE tbl_srs_bak", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2261 {
2262 if ( errorMessage )
2263 {
2264 *errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2265 }
2266 sqlite3_free( errmsg );
2267 return false;
2268 }
2269 }
2270 else
2271 {
2272 sqlite3_free( errmsg );
2273 }
2274
2275 if ( sqlite3_exec( database.get(), "DROP VIEW vw_srs", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2276 {
2277 QgsDebugMsg( QStringLiteral( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
2278 }
2279
2280 if ( sqlite3_exec( database.get(),
2281 "CREATE VIEW vw_srs AS"
2282 " SELECT"
2283 " a.description AS description"
2284 ",a.srs_id AS srs_id"
2285 ",a.is_geo AS is_geo"
2286 ",coalesce(b.name,a.projection_acronym) AS name"
2287 ",a.parameters AS parameters"
2288 ",a.auth_name AS auth_name"
2289 ",a.auth_id AS auth_id"
2290 ",a.deprecated AS deprecated"
2291 " FROM tbl_srs a"
2292 " LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
2293 " ORDER BY coalesce(b.name,a.projection_acronym),a.description", nullptr, nullptr, &errmsg ) != SQLITE_OK )
2294 {
2295 if ( errorMessage )
2296 {
2297 *errorMessage = tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
2298 }
2299 sqlite3_free( errmsg );
2300 return false;
2301 }
2302 }
2303 return true;
2304}
2305
2306void QgsApplication::setMaxThreads( int maxThreads )
2307{
2308 QgsDebugMsgLevel( QStringLiteral( "maxThreads: %1" ).arg( maxThreads ), 2 );
2309
2310 // make sure value is between 1 and #cores, if not set to -1 (use #cores)
2311 // 0 could be used to disable any parallel processing
2312 if ( maxThreads < 1 || maxThreads > QThread::idealThreadCount() )
2313 maxThreads = -1;
2314
2315 // save value
2316 ABISYM( sMaxThreads ) = maxThreads;
2317
2318 // if -1 use #cores
2319 if ( maxThreads == -1 )
2320 maxThreads = QThread::idealThreadCount();
2321
2322 // set max thread count in QThreadPool
2323 QThreadPool::globalInstance()->setMaxThreadCount( maxThreads );
2324 QgsDebugMsgLevel( QStringLiteral( "set QThreadPool max thread count to %1" ).arg( QThreadPool::globalInstance()->maxThreadCount() ), 2 );
2325}
2326
2328{
2329 return members()->mTaskManager;
2330}
2331
2333{
2334 return members()->mSettingsRegistryCore;
2335}
2336
2338{
2339 return members()->mColorSchemeRegistry;
2340}
2341
2343{
2344 return members()->mPaintEffectRegistry;
2345}
2346
2348{
2349 return members()->mRendererRegistry;
2350}
2351
2353{
2354 return members()->mRasterRendererRegistry;
2355}
2356
2358{
2359 return members()->mPointCloudRendererRegistry;
2360}
2361
2363{
2364 if ( auto *lInstance = instance() )
2365 {
2366 if ( !instance()->mDataItemProviderRegistry )
2367 {
2368 lInstance->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
2369 }
2370 return lInstance->mDataItemProviderRegistry;
2371 }
2372 else
2373 {
2374 // no QgsApplication instance
2375 static QgsDataItemProviderRegistry *sDataItemProviderRegistry = nullptr;
2376 if ( !sDataItemProviderRegistry )
2377 sDataItemProviderRegistry = new QgsDataItemProviderRegistry();
2378 return sDataItemProviderRegistry;
2379 }
2380}
2381
2383{
2384 return members()->mCrsRegistry;
2385}
2386
2388{
2389 return members()->mSvgCache;
2390}
2391
2393{
2394 return members()->mImageCache;
2395}
2396
2398{
2399 return members()->mSourceCache;
2400}
2401
2403{
2404 return members()->mNetworkContentFetcherRegistry;
2405}
2406
2408{
2409 return members()->mValidityCheckRegistry;
2410}
2411
2413{
2414 return members()->mSymbolLayerRegistry;
2415}
2416
2418{
2419 return members()->mCalloutRegistry;
2420}
2421
2423{
2424 return members()->mLayoutItemRegistry;
2425}
2426
2428{
2429 return members()->mAnnotationItemRegistry;
2430}
2431
2433{
2434 return members()->mGpsConnectionRegistry;
2435}
2436
2438{
2439 return members()->mGpsBabelFormatRegistry;
2440}
2441
2443{
2444 return members()->mPluginLayerRegistry;
2445}
2446
2448{
2449 return members()->mClassificationMethodRegistry;
2450}
2451
2453{
2454 return members()->mBookmarkManager;
2455}
2456
2458{
2459 return members()->mTileDownloadManager;
2460}
2461
2463{
2464 return members()->mRecentStyleHandler;
2465}
2466
2468{
2469 return members()->mQueryLogger;
2470}
2471
2473{
2474 return members()->mStyleModel;
2475}
2476
2478{
2479 return members()->mFontManager;
2480}
2481
2483{
2484 return members()->mMessageLog;
2485}
2486
2488{
2489 return members()->mProcessingRegistry;
2490}
2491
2493{
2494 return members()->mConnectionRegistry;
2495}
2496
2498{
2499 return members()->mLayerMetadataProviderRegistry;
2500}
2501
2503{
2504 return members()->mPageSizeRegistry;
2505}
2506
2508{
2509 return members()->mAnnotationRegistry;
2510}
2511
2513{
2514 return members()->mNumericFormatRegistry;
2515}
2516
2518{
2519 return members()->mFieldFormatterRegistry;
2520}
2521
2523{
2524 return members()->m3DRendererRegistry;
2525}
2526
2528{
2529 return members()->m3DSymbolRegistry;
2530}
2531
2533{
2534 return members()->mScaleBarRendererRegistry;
2535}
2536
2538{
2539 return members()->mProjectStorageRegistry;
2540}
2541
2543{
2544 return members()->mExternalStorageRegistry;
2545}
2546
2548{
2549 return members()->mLocalizedDataPathRegistry;
2550}
2551
2552QgsApplication::ApplicationMembers::ApplicationMembers()
2553{
2554 // don't use initializer lists or scoped pointers - as more objects are added here we
2555 // will need to be careful with the order of creation/destruction
2556 mSettingsRegistryCore = new QgsSettingsRegistryCore();
2557 mLocalizedDataPathRegistry = new QgsLocalizedDataPathRegistry();
2558 mMessageLog = new QgsMessageLog();
2559 QgsRuntimeProfiler *profiler = QgsRuntimeProfiler::threadLocalInstance();
2560
2561 {
2562 profiler->start( tr( "Create query logger" ) );
2563 mQueryLogger = new QgsDatabaseQueryLog();
2564 profiler->end();
2565 }
2566 {
2567 profiler->start( tr( "Setup coordinate reference system registry" ) );
2568 mCrsRegistry = new QgsCoordinateReferenceSystemRegistry();
2569 profiler->end();
2570 }
2571 {
2572 profiler->start( tr( "Create connection registry" ) );
2573 mConnectionRegistry = new QgsConnectionRegistry();
2574 profiler->end();
2575 }
2576 {
2577 profiler->start( tr( "Create project storage registry" ) );
2578 mProjectStorageRegistry = new QgsProjectStorageRegistry();
2579 profiler->end();
2580 }
2581 {
2582 profiler->start( tr( "Create metadata provider registry" ) );
2583 mLayerMetadataProviderRegistry = new QgsLayerMetadataProviderRegistry();
2584 profiler->end();
2585 }
2586 {
2587 profiler->start( tr( "Create font manager" ) );
2588 mFontManager = new QgsFontManager();
2589 profiler->end();
2590 }
2591 {
2592 profiler->start( tr( "Setup task manager" ) );
2593 mTaskManager = new QgsTaskManager();
2594 profiler->end();
2595 }
2596 {
2597 profiler->start( tr( "Setup action scope registry" ) );
2598 mActionScopeRegistry = new QgsActionScopeRegistry();
2599 profiler->end();
2600 }
2601 {
2602 profiler->start( tr( "Setup numeric formats" ) );
2603 mNumericFormatRegistry = new QgsNumericFormatRegistry();
2604 profiler->end();
2605 }
2606 {
2607 profiler->start( tr( "Setup field formats" ) );
2608 mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
2609 profiler->end();
2610 }
2611 {
2612 profiler->start( tr( "Setup SVG cache" ) );
2613 mSvgCache = new QgsSvgCache();
2614 profiler->end();
2615 }
2616 {
2617 profiler->start( tr( "Setup image cache" ) );
2618 mImageCache = new QgsImageCache();
2619 profiler->end();
2620 }
2621 {
2622 profiler->start( tr( "Setup source cache" ) );
2623 mSourceCache = new QgsSourceCache();
2624 profiler->end();
2625 }
2626 {
2627 profiler->start( tr( "Setup color scheme registry" ) );
2628 mColorSchemeRegistry = new QgsColorSchemeRegistry();
2629 profiler->end();
2630 }
2631 {
2632 profiler->start( tr( "Setup paint effect" ) );
2633 mPaintEffectRegistry = new QgsPaintEffectRegistry();
2634 profiler->end();
2635 }
2636 {
2637 profiler->start( tr( "Setup symbol layer registry" ) );
2638 mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
2639 profiler->end();
2640 }
2641 {
2642 profiler->start( tr( "Recent style handler" ) );
2643 mRecentStyleHandler = new QgsRecentStyleHandler();
2644 profiler->end();
2645 }
2646 {
2647 profiler->start( tr( "Setup callout registry" ) );
2648 mCalloutRegistry = new QgsCalloutRegistry();
2649 profiler->end();
2650 }
2651 {
2652 profiler->start( tr( "Setup renderer registry" ) );
2653 mRendererRegistry = new QgsRendererRegistry();
2654 profiler->end();
2655 }
2656 {
2657 profiler->start( tr( "Setup raster renderer registry" ) );
2658 mRasterRendererRegistry = new QgsRasterRendererRegistry();
2659 profiler->end();
2660 }
2661 {
2662 profiler->start( tr( "Setup point cloud renderer registry" ) );
2663 mPointCloudRendererRegistry = new QgsPointCloudRendererRegistry();
2664 profiler->end();
2665 }
2666 {
2667 profiler->start( tr( "Setup GPS registry" ) );
2668 mGpsConnectionRegistry = new QgsGpsConnectionRegistry();
2669 profiler->end();
2670 }
2671 {
2672 profiler->start( tr( "Setup GPSBabel format registry" ) );
2673 mGpsBabelFormatRegistry = new QgsBabelFormatRegistry();
2674 profiler->end();
2675 }
2676 {
2677 profiler->start( tr( "Setup plugin layer registry" ) );
2678 mPluginLayerRegistry = new QgsPluginLayerRegistry();
2679 profiler->end();
2680 }
2681 {
2682 profiler->start( tr( "Setup Processing registry" ) );
2683 mProcessingRegistry = new QgsProcessingRegistry();
2684 profiler->end();
2685 }
2686 mPageSizeRegistry = new QgsPageSizeRegistry();
2687 {
2688 profiler->start( tr( "Setup layout item registry" ) );
2689 mLayoutItemRegistry = new QgsLayoutItemRegistry();
2690 mLayoutItemRegistry->populate();
2691 profiler->end();
2692 }
2693 {
2694 profiler->start( tr( "Setup annotation registry" ) );
2695 mAnnotationRegistry = new QgsAnnotationRegistry();
2696 profiler->end();
2697 }
2698 {
2699 profiler->start( tr( "Setup annotation item registry" ) );
2700 mAnnotationItemRegistry = new QgsAnnotationItemRegistry();
2701 mAnnotationItemRegistry->populate();
2702 profiler->end();
2703 }
2704 {
2705 profiler->start( tr( "Setup 3D symbol registry" ) );
2706 m3DSymbolRegistry = new Qgs3DSymbolRegistry();
2707 profiler->end();
2708 }
2709 {
2710 profiler->start( tr( "Setup 3D renderer registry" ) );
2711 m3DRendererRegistry = new Qgs3DRendererRegistry();
2712 profiler->end();
2713 }
2714 {
2715 profiler->start( tr( "Setup project storage registry" ) );
2716 mProjectStorageRegistry = new QgsProjectStorageRegistry();
2717 profiler->end();
2718 }
2719 {
2720 profiler->start( tr( "Setup layer metadata provider registry" ) );
2721 mLayerMetadataProviderRegistry = new QgsLayerMetadataProviderRegistry();
2722 profiler->end();
2723 }
2724 {
2725 profiler->start( tr( "Setup external storage registry" ) );
2726 mExternalStorageRegistry = new QgsExternalStorageRegistry();
2727 profiler->end();
2728 }
2729 {
2730 profiler->start( tr( "Setup network content cache" ) );
2731 mNetworkContentFetcherRegistry = new QgsNetworkContentFetcherRegistry();
2732 profiler->end();
2733 }
2734 {
2735 profiler->start( tr( "Setup layout check registry" ) );
2736 mValidityCheckRegistry = new QgsValidityCheckRegistry();
2737 profiler->end();
2738 }
2739 {
2740 profiler->start( tr( "Setup classification registry" ) );
2741 mClassificationMethodRegistry = new QgsClassificationMethodRegistry();
2742 profiler->end();
2743 }
2744 {
2745 profiler->start( tr( "Setup bookmark manager" ) );
2746 mBookmarkManager = new QgsBookmarkManager( nullptr );
2747 profiler->end();
2748 }
2749 {
2750 profiler->start( tr( "Setup tile download manager" ) );
2751 mTileDownloadManager = new QgsTileDownloadManager();
2752 profiler->end();
2753 }
2754 {
2755 profiler->start( tr( "Setup scalebar registry" ) );
2756 mScaleBarRendererRegistry = new QgsScaleBarRendererRegistry();
2757 profiler->end();
2758 }
2759}
2760
2761QgsApplication::ApplicationMembers::~ApplicationMembers()
2762{
2763 delete mStyleModel;
2764 delete mTileDownloadManager;
2765 delete mScaleBarRendererRegistry;
2766 delete mValidityCheckRegistry;
2767 delete mActionScopeRegistry;
2768 delete m3DRendererRegistry;
2769 delete m3DSymbolRegistry;
2770 delete mAnnotationRegistry;
2771 delete mColorSchemeRegistry;
2772 delete mFieldFormatterRegistry;
2773 delete mGpsConnectionRegistry;
2774 delete mGpsBabelFormatRegistry;
2775 delete mMessageLog;
2776 delete mPaintEffectRegistry;
2777 delete mPluginLayerRegistry;
2778 delete mProcessingRegistry;
2779 delete mPageSizeRegistry;
2780 delete mAnnotationItemRegistry;
2781 delete mLayoutItemRegistry;
2782 delete mPointCloudRendererRegistry;
2783 delete mRasterRendererRegistry;
2784 delete mRendererRegistry;
2785 delete mSvgCache;
2786 delete mImageCache;
2787 delete mSourceCache;
2788 delete mCalloutRegistry;
2789 delete mRecentStyleHandler;
2790 delete mSymbolLayerRegistry;
2791 delete mExternalStorageRegistry;
2792 delete mTaskManager;
2793 delete mNetworkContentFetcherRegistry;
2794 delete mClassificationMethodRegistry;
2795 delete mNumericFormatRegistry;
2796 delete mBookmarkManager;
2797 delete mConnectionRegistry;
2798 delete mProjectStorageRegistry;
2799 delete mLayerMetadataProviderRegistry;
2800 delete mFontManager;
2801 delete mLocalizedDataPathRegistry;
2802 delete mCrsRegistry;
2803 delete mQueryLogger;
2804 delete mSettingsRegistryCore;
2805}
2806
2807QgsApplication::ApplicationMembers *QgsApplication::members()
2808{
2809 if ( auto *lInstance = instance() )
2810 {
2811 return lInstance->mApplicationMembers;
2812 }
2813 else
2814 {
2815 static QRecursiveMutex sMemberMutex;
2816 QMutexLocker lock( &sMemberMutex );
2817 if ( !sApplicationMembers )
2818 sApplicationMembers = new ApplicationMembers();
2819 return sApplicationMembers;
2820 }
2821}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:2304
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 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 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 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 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 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 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 QgsSettingsEntryBool settingsLocaleOverrideFlag
Settings entry locale override flag.
static const char * QGIS_ORGANIZATION_NAME
static QString contributorsFilePath()
Returns the path to the contributors file.
static const QgsSettingsEntryStringList settingsSearchPathsForSVG
Settings entry search path for SVG.
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 QString userFullName()
Returns the user's operating system login account full display name.
static 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.
static const QgsSettingsEntryString settingsLocaleUserLocale
Settings entry locale user locale.
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 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:48
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:663
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:477
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.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
QgsSettingsRegistryCore is used for settings introspection and collects all QgsSettingsEntry instance...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
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:90
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.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:3022
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 QgsDebugMsg(str)
Definition: qgslogger.h:38