QGIS API Documentation  3.21.0-Master (5b68dc587e)
qgsapplication.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsapplication.h - 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 #ifndef QGSAPPLICATION_H
16 #define QGSAPPLICATION_H
17 
18 #include "qgis_core.h"
19 #include <QApplication>
20 #include <QEvent>
21 #include <QStringList>
22 #include <QColor>
23 
24 #include <memory>
25 #include "qgis_sip.h"
26 #include "qgsconfig.h"
27 #include "qgssettingsentry.h"
28 #include "qgstranslationcontext.h"
29 
30 class QgsSettingsRegistryCore;
34 class QgsRuntimeProfiler;
35 class QgsTaskManager;
40 class QgsExternalStorageRegistry;
43 class QgsSvgCache;
44 class QgsImageCache;
45 class QgsSourceCache;
53 class QgsMessageLog;
55 class QgsAnnotationRegistry;
56 class QgsUserProfile;
60 class QgsAuthManager;
63 class QTranslator;
64 class QgsCalloutRegistry;
65 class QgsBookmarkManager;
66 class QgsStyleModel;
75 
90 class CORE_EXPORT QgsApplication : public QApplication
91 {
92 
93 #ifdef SIP_RUN
94  % TypeCode
95  // Convert a Python argv list to a conventional C argc count and argv array.
96  static char **qtgui_ArgvToC( PyObject *argvlist, int &argc )
97  {
98  char **argv;
99 
100  argc = PyList_GET_SIZE( argvlist );
101 
102  // Allocate space for two copies of the argument pointers, plus the
103  // terminating NULL.
104  if ( ( argv = ( char ** )sipMalloc( 2 * ( argc + 1 ) * sizeof( char * ) ) ) == NULL )
105  return NULL;
106 
107  // Convert the list.
108  for ( int a = 0; a < argc; ++a )
109  {
110  char *arg;
111  // Get the argument and allocate memory for it.
112  if ( ( arg = PyBytes_AsString( PyList_GET_ITEM( argvlist, a ) ) ) == NULL ||
113  ( argv[a] = ( char * )sipMalloc( strlen( arg ) + 1 ) ) == NULL )
114  return NULL;
115  // Copy the argument and save a pointer to it.
116  strcpy( argv[a], arg );
117  argv[a + argc + 1] = argv[a];
118  }
119 
120  argv[argc + argc + 1] = argv[argc] = NULL;
121 
122  return argv;
123  }
124 
125  // Remove arguments from the Python argv list that have been removed from the
126  // C argv array.
127  static void qtgui_UpdatePyArgv( PyObject *argvlist, int argc, char **argv )
128  {
129  for ( int a = 0, na = 0; a < argc; ++a )
130  {
131  // See if it was removed.
132  if ( argv[na] == argv[a + argc + 1] )
133  ++na;
134  else
135  PyList_SetSlice( argvlist, na, na + 1, NULL );
136  }
137  }
138  % End
139 #endif
140 
141  Q_OBJECT
142 
143  public:
144 
154  {
155  Qt,
157  };
158 
159  static const char *QGIS_ORGANIZATION_NAME;
160  static const char *QGIS_ORGANIZATION_DOMAIN;
161  static const char *QGIS_APPLICATION_NAME;
162 #ifndef SIP_RUN
163  QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" );
164 #else
165  QgsApplication( SIP_PYLIST argv, bool GUIenabled, QString profileFolder = QString(), QString platformName = "desktop" ) / PostHook = __pyQtQAppHook__ / [( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" )];
166  % MethodCode
167  // The Python interface is a list of argument strings that is modified.
168 
169  int argc;
170  char **argv;
171 
172  // Convert the list.
173  if ( ( argv = qtgui_ArgvToC( a0, argc ) ) == NULL )
174  sipIsErr = 1;
175  else
176  {
177  // Create it now the arguments are right.
178  static int nargc = argc;
179 
180  sipCpp = new sipQgsApplication( nargc, argv, a1, *a2, *a3 );
181 
182  // Now modify the original list.
183  qtgui_UpdatePyArgv( a0, argc, argv );
184  }
185  % End
186 #endif
187 
188  ~QgsApplication() override;
189 
195  static QgsApplication *instance();
196 
204  static void init( QString profileFolder = QString() ) SIP_SKIP;
205 
207  bool event( QEvent *event ) override;
208 
210  bool notify( QObject *receiver, QEvent *event ) override;
211 
213  static void setFileOpenEventReceiver( QObject *receiver );
214 
225  static void setThemeName( const QString &themeName );
226 
231  static QString resolvePkgPath();
232 
240  static QString themeName();
241 
249  static void setUITheme( const QString &themeName );
250 
257  static QHash<QString, QString> uiThemes();
258 
260  static QString authorsFilePath();
261 
267  static QString contributorsFilePath();
268 
275  static QString developersMapFilePath();
276 
278  static QString sponsorsFilePath();
279 
281  static QString donorsFilePath();
282 
284  static QString serverResourcesPath();
285 
289  static QString translatorsFilePath();
290 
294  static QString licenceFilePath();
295 
297  static QString i18nPath();
298 
303  static QString metadataPath();
304 
306  static QString qgisMasterDatabaseFilePath();
307 
309  static QString qgisSettingsDirPath();
310 
312  static QString qgisUserDatabaseFilePath();
313 
315  static QString qgisAuthDatabaseFilePath();
316 
318  static QString splashPath();
319 
321  static QString iconsPath();
322 
324  static QString srsDatabaseFilePath();
325 
331  static void setSvgPaths( const QStringList &svgPaths );
332 
334  static QStringList svgPaths();
335 
340  static QStringList layoutTemplatePaths();
341 
343  static QMap<QString, QString> systemEnvVars();
344 
346  static QString prefixPath();
347 
349  static QString pluginPath();
350 
352  static QString pkgDataPath();
353 
355  static QString activeThemePath();
356 
358  static QString defaultThemePath();
359 
364  static QString iconPath( const QString &iconFile );
365 
373  static QIcon getThemeIcon( const QString &name, const QColor &fillColor = QColor(), const QColor &strokeColor = QColor() );
374 
379  enum Cursor
380  {
388  };
389 
396  static QCursor getThemeCursor( Cursor cursor );
397 
406  static QPixmap getThemePixmap( const QString &name, const QColor &foreColor = QColor(), const QColor &backColor = QColor(), int size = 16 );
407 
409  static QString userStylePath();
410 
416  static QRegularExpression shortNameRegularExpression();
417 
423  static QString userLoginName();
424 
430  static QString userFullName();
431 
437  static QString osName();
438 
444  static QString platform();
445 
450  static QString locale();
451 
453  static QString userThemesFolder();
454 
456  static QString defaultStylePath();
457 
459  static QString defaultThemesFolder();
460 
462  static QString libraryPath();
463 
465  static QString libexecPath();
466 
473  static QString qmlImportPath();
474 
476  static void setPrefixPath( const QString &prefixPath, bool useDefaultPaths = false );
477 
479  static void setPluginPath( const QString &pluginPath );
480 
482  static void setPkgDataPath( const QString &pkgDataPath );
483 
485  static void setDefaultSvgPaths( const QStringList &pathList );
486 
488  static void setAuthDatabaseDirPath( const QString &authDbDirPath );
489 
491  static void initQgis();
492 
494  static bool createDatabase( QString *errorMessage = nullptr );
495 
497  static bool createThemeFolder();
498 
500  static void exitQgis();
501 
503  static QString appIconPath();
504 
506  enum endian_t
507  {
508  XDR = 0, // network, or big-endian, byte order
509  NDR = 1 // little-endian byte order
510  };
511 
513  static endian_t endian();
514 
529  static QString reportStyleSheet( QgsApplication::StyleSheetType styleSheetType = QgsApplication::StyleSheetType::Qt );
530 
535  static QString showSettings();
536 
544  static void registerOgrDrivers();
545 
547  static QString absolutePathToRelativePath( const QString &apath, const QString &targetPath );
549  static QString relativePathToAbsolutePath( const QString &rpath, const QString &targetPath );
550 
552  static bool isRunningFromBuildDir() { return ABISYM( mRunningFromBuildDir ); }
553 #if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
554  static QString cfgIntDir() SIP_SKIP;
555 #endif
557  static QString buildSourcePath();
559  static QString buildOutputPath();
560 
566  static void skipGdalDriver( const QString &driver );
567 
573  static void restoreGdalDriver( const QString &driver );
574 
579  static QStringList skippedGdalDrivers();
580 
587  static void applyGdalSkippedDrivers();
588 
593  static void registerGdalDriversFromSettings();
594 
601  static QStringList deferredSkippedGdalDrivers();
602 
609  static void setSkippedGdalDrivers( const QStringList &skippedGdalDrivers,
610  const QStringList &deferredSkippedGdalDrivers );
611 
616  static int maxThreads();
617 
623  static void setMaxThreads( int maxThreads );
624 
630  static QgsTaskManager *taskManager();
631 
636  static QgsSettingsRegistryCore *settingsRegistryCore() SIP_KEEPREFERENCE;
637 
642  static QgsColorSchemeRegistry *colorSchemeRegistry() SIP_KEEPREFERENCE;
643 
648  static QgsPaintEffectRegistry *paintEffectRegistry() SIP_KEEPREFERENCE;
649 
654  static QgsRendererRegistry *rendererRegistry() SIP_KEEPREFERENCE;
655 
661  static QgsRasterRendererRegistry *rasterRendererRegistry() SIP_SKIP;
662 
667  static QgsPointCloudRendererRegistry *pointCloudRendererRegistry() SIP_KEEPREFERENCE;
668 
674  static QgsDataItemProviderRegistry *dataItemProviderRegistry() SIP_KEEPREFERENCE;
675 
682  static QgsCoordinateReferenceSystemRegistry *coordinateReferenceSystemRegistry() SIP_KEEPREFERENCE;
683 
691  static QgsSvgCache *svgCache();
692 
699  static QgsImageCache *imageCache();
700 
706  static QgsSourceCache *sourceCache();
707 
712  static QgsNetworkContentFetcherRegistry *networkContentFetcherRegistry() SIP_KEEPREFERENCE;
713 
718  static QgsValidityCheckRegistry *validityCheckRegistry() SIP_KEEPREFERENCE;
719 
724  static QgsSymbolLayerRegistry *symbolLayerRegistry() SIP_KEEPREFERENCE;
725 
730  static QgsCalloutRegistry *calloutRegistry() SIP_KEEPREFERENCE;
731 
736  static QgsLayoutItemRegistry *layoutItemRegistry() SIP_KEEPREFERENCE;
737 
742  static QgsAnnotationItemRegistry *annotationItemRegistry() SIP_KEEPREFERENCE;
743 
748  static QgsGpsConnectionRegistry *gpsConnectionRegistry() SIP_KEEPREFERENCE;
749 
754  static QgsBabelFormatRegistry *gpsBabelFormatRegistry() SIP_KEEPREFERENCE;
755 
760  static QgsPluginLayerRegistry *pluginLayerRegistry() SIP_KEEPREFERENCE;
761 
766  static QgsClassificationMethodRegistry *classificationMethodRegistry() SIP_KEEPREFERENCE;
767 
772  static QgsBookmarkManager *bookmarkManager();
773 
779  static QgsTileDownloadManager *tileDownloadManager() SIP_SKIP;
780 
785  static QgsRecentStyleHandler *recentStyleHandler() SIP_KEEPREFERENCE;
786 
794  static QgsStyleModel *defaultStyleModel();
795 
800  static QgsMessageLog *messageLog();
801 
808  static QgsAuthManager *authManager();
809 
815  static QgsProcessingRegistry *processingRegistry();
816 
821  static QgsPageSizeRegistry *pageSizeRegistry() SIP_KEEPREFERENCE;
822 
828  static QgsAnnotationRegistry *annotationRegistry() SIP_SKIP;
829 
835  static QgsActionScopeRegistry *actionScopeRegistry() SIP_KEEPREFERENCE;
836 
841  static QgsConnectionRegistry *connectionRegistry();
842 
847  static QgsRuntimeProfiler *profiler();
848 
854  static QgsNumericFormatRegistry *numericFormatRegistry() SIP_KEEPREFERENCE;
855 
859  static QgsFieldFormatterRegistry *fieldFormatterRegistry() SIP_KEEPREFERENCE;
860 
865  static Qgs3DRendererRegistry *renderer3DRegistry() SIP_KEEPREFERENCE;
866 
871  static Qgs3DSymbolRegistry *symbol3DRegistry() SIP_KEEPREFERENCE;
872 
878  static QgsScaleBarRendererRegistry *scaleBarRendererRegistry() SIP_KEEPREFERENCE;
879 
884  static QgsProjectStorageRegistry *projectStorageRegistry() SIP_KEEPREFERENCE;
885 
890  static QgsExternalStorageRegistry *externalStorageRegistry() SIP_KEEPREFERENCE;
891 
899  static QgsLocalizedDataPathRegistry *localizedDataPathRegistry() SIP_KEEPREFERENCE;
900 
909  static QString nullRepresentation();
910 
914  static void setNullRepresentation( const QString &nullRepresentation );
915 
923  static QVariantMap customVariables();
924 
932  static void setCustomVariables( const QVariantMap &customVariables );
933 
939  static void setCustomVariable( const QString &name, const QVariant &value );
940 
950  static int scaleIconSize( int standardSize, bool applyDevicePixelRatio = false );
951 
960  int maxConcurrentConnectionsPerPool() const;
961 
967  static void setTranslation( const QString &translation );
968 
974  QString translation() const;
975 
981  void collectTranslatableObjects( QgsTranslationContext *translationContext );
982 
983 #ifndef SIP_RUN
985  static const inline QgsSettingsEntryString settingsLocaleUserLocale = QgsSettingsEntryString( QStringLiteral( "locale/userLocale" ), QgsSettings::NoSection, QString() );
987  static const inline QgsSettingsEntryBool settingsLocaleOverrideFlag = QgsSettingsEntryBool( QStringLiteral( "locale/overrideFlag" ), QgsSettings::NoSection, false );
989  static const inline QgsSettingsEntryString settingsLocaleGlobalLocale = QgsSettingsEntryString( QStringLiteral( "locale/globalLocale" ), QgsSettings::NoSection, QString() );
991  static const inline QgsSettingsEntryBool settingsLocaleShowGroupSeparator = QgsSettingsEntryBool( QStringLiteral( "locale/showGroupSeparator" ), QgsSettings::NoSection, false );
993  static const inline QgsSettingsEntryStringList settingsSearchPathsForSVG = QgsSettingsEntryStringList( QStringLiteral( "svg/searchPathsForSVG" ), QgsSettings::NoSection, QStringList() );
994 #endif
995 
996 #ifdef SIP_RUN
997  SIP_IF_FEATURE( ANDROID )
998  //dummy method to workaround sip generation issue
999  bool x11EventFilter( XEvent *event );
1000  SIP_END
1001 #endif
1002 
1003  signals:
1005  void preNotify( QObject *receiver, QEvent *event, bool *done ) SIP_SKIP;
1006 
1012 
1013 
1018 
1026 
1027  private:
1028 
1029  static void copyPath( const QString &src, const QString &dst );
1030  static QObject *ABISYM( mFileOpenEventReceiver );
1031 
1032  static bool ABISYM( mInitialized );
1033 
1035  static bool ABISYM( mRunningFromBuildDir );
1036 
1040  static int ABISYM( sMaxThreads );
1041 
1042  QMap<QString, QIcon> mIconCache;
1043  QMap<Cursor, QCursor> mCursorCache;
1044 
1045  QTranslator *mQgisTranslator = nullptr;
1046  QTranslator *mQtTranslator = nullptr;
1047  QTranslator *mQtBaseTranslator = nullptr;
1048 
1049  QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
1050  QgsAuthManager *mAuthManager = nullptr;
1051 
1052  struct ApplicationMembers
1053  {
1054  QgsSettingsRegistryCore *mSettingsRegistryCore = nullptr;
1055  QgsCoordinateReferenceSystemRegistry *mCrsRegistry = nullptr;
1056  Qgs3DRendererRegistry *m3DRendererRegistry = nullptr;
1057  Qgs3DSymbolRegistry *m3DSymbolRegistry = nullptr;
1058  QgsActionScopeRegistry *mActionScopeRegistry = nullptr;
1059  QgsAnnotationRegistry *mAnnotationRegistry = nullptr;
1060  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
1061  QgsLocalizedDataPathRegistry *mLocalizedDataPathRegistry = nullptr;
1062  QgsNumericFormatRegistry *mNumericFormatRegistry = nullptr;
1063  QgsFieldFormatterRegistry *mFieldFormatterRegistry = nullptr;
1064  QgsGpsConnectionRegistry *mGpsConnectionRegistry = nullptr;
1065  QgsBabelFormatRegistry *mGpsBabelFormatRegistry = nullptr;
1066  QgsNetworkContentFetcherRegistry *mNetworkContentFetcherRegistry = nullptr;
1067  QgsScaleBarRendererRegistry *mScaleBarRendererRegistry = nullptr;
1068  QgsValidityCheckRegistry *mValidityCheckRegistry = nullptr;
1069  QgsMessageLog *mMessageLog = nullptr;
1070  QgsPaintEffectRegistry *mPaintEffectRegistry = nullptr;
1071  QgsPluginLayerRegistry *mPluginLayerRegistry = nullptr;
1072  QgsClassificationMethodRegistry *mClassificationMethodRegistry = nullptr;
1073  QgsProcessingRegistry *mProcessingRegistry = nullptr;
1074  QgsConnectionRegistry *mConnectionRegistry = nullptr;
1075  std::unique_ptr<QgsProjectStorageRegistry> mProjectStorageRegistry;
1076  QgsExternalStorageRegistry *mExternalStorageRegistry = nullptr;
1077  QgsPageSizeRegistry *mPageSizeRegistry = nullptr;
1078  QgsRasterRendererRegistry *mRasterRendererRegistry = nullptr;
1079  QgsRendererRegistry *mRendererRegistry = nullptr;
1080  QgsPointCloudRendererRegistry *mPointCloudRendererRegistry = nullptr;
1081  QgsSvgCache *mSvgCache = nullptr;
1082  QgsImageCache *mImageCache = nullptr;
1083  QgsSourceCache *mSourceCache = nullptr;
1084  QgsSymbolLayerRegistry *mSymbolLayerRegistry = nullptr;
1085  QgsCalloutRegistry *mCalloutRegistry = nullptr;
1086  QgsTaskManager *mTaskManager = nullptr;
1087  QgsLayoutItemRegistry *mLayoutItemRegistry = nullptr;
1088  QgsAnnotationItemRegistry *mAnnotationItemRegistry = nullptr;
1089  QgsUserProfileManager *mUserConfigManager = nullptr;
1090  QgsBookmarkManager *mBookmarkManager = nullptr;
1091  QgsTileDownloadManager *mTileDownloadManager = nullptr;
1092  QgsStyleModel *mStyleModel = nullptr;
1093  QgsRecentStyleHandler *mRecentStyleHandler = nullptr;
1094  QString mNullRepresentation;
1095  QStringList mSvgPathCache;
1096  bool mSvgPathCacheValid = false;
1097 
1098  ApplicationMembers();
1099  ~ApplicationMembers();
1100  };
1101 
1102  // Applications members which belong to an instance of QgsApplication
1103  ApplicationMembers *mApplicationMembers = nullptr;
1104  // ... but in case QgsApplication is never instantiated (eg with custom designer widgets), we fall back to static members
1105  static ApplicationMembers *sApplicationMembers;
1106 
1107  static QgsAuthManager *sAuthManager;
1108 
1109  static ApplicationMembers *members();
1110 
1111  static void invalidateCaches();
1112 
1113  friend class TestQgsApplication;
1114 };
1115 
1116 // clazy:excludeall=qstring-allocations
1117 
1118 #endif
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,...
endian_t
Constants for endian-ness.
void customVariablesChanged()
Emitted whenever a custom global variable changes.
StyleSheetType
The StyleSheetType enum represents the stylesheet type that a widget supports.
@ WebBrowser
StyleSheet for Qt GUI widgets (based on QLabel or QTextBrowser), supports basic CSS and Qt extensions...
static const char * QGIS_APPLICATION_NAME
static const char * QGIS_ORGANIZATION_DOMAIN
void preNotify(QObject *receiver, QEvent *event, bool *done)
static const char * QGIS_ORGANIZATION_NAME
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.
void requestForTranslatableObjects(QgsTranslationContext *translationContext)
Emitted when project strings which require translation are being collected for inclusion in a ....
void nullRepresentationChanged()
This string is used to represent the value NULL throughout QGIS.
static bool isRunningFromBuildDir()
Indicates whether running from build directory (not installed)
Singleton offering an interface to manage the authentication configuration database and to utilize co...
A registry for QgsAbstractBabelFormat GPSBabel formats.
Manages storage of a set of bookmarks.
Registry of available callout classes.
This class manages all known classification methods.
Registry of color schemes.
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.
This class keeps a list of data item providers that may add items to the browser tree.
The QgsFieldFormatterRegistry manages registered classes of QgsFieldFormatter.
A class to register / unregister existing GPS connections such that the information is available to a...
A cache for images derived from raster files.
Definition: qgsimagecache.h:97
Registry of available layout item types.
A registry class to hold localized data paths which can be used for basemaps, logos,...
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:40
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...
Registry of storage backends that QgsProject may use.
Registry for raster renderers.
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...
The QgsScaleBarRendererRegistry manages registered scalebar renderers.
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 ...
A cache for images / pictures derived from SVG files.
Definition: qgssvgcache.h:123
Registry of available symbol layer classes.
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.
User profile contains information about the user profile folders on the machine.
This class keeps a list of QgsAbstractValidityCheck checks which can be used when performing validity...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:86
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_IF_FEATURE(feature)
Definition: qgis_sip.h:167
#define SIP_END
Definition: qgis_sip.h:194
QObject * ABISYM(QgsApplication::mFileOpenEventReceiver)