QGIS API Documentation  3.21.0-Master (af9a72eda6)
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;
52 class QgsMessageLog;
54 class QgsAnnotationRegistry;
55 class QgsUserProfile;
59 class QgsAuthManager;
62 class QTranslator;
63 class QgsCalloutRegistry;
64 class QgsBookmarkManager;
65 class QgsStyleModel;
73 
88 class CORE_EXPORT QgsApplication : public QApplication
89 {
90 
91 #ifdef SIP_RUN
92  % TypeCode
93  // Convert a Python argv list to a conventional C argc count and argv array.
94  static char **qtgui_ArgvToC( PyObject *argvlist, int &argc )
95  {
96  char **argv;
97 
98  argc = PyList_GET_SIZE( argvlist );
99 
100  // Allocate space for two copies of the argument pointers, plus the
101  // terminating NULL.
102  if ( ( argv = ( char ** )sipMalloc( 2 * ( argc + 1 ) * sizeof( char * ) ) ) == NULL )
103  return NULL;
104 
105  // Convert the list.
106  for ( int a = 0; a < argc; ++a )
107  {
108  char *arg;
109  // Get the argument and allocate memory for it.
110  if ( ( arg = PyBytes_AsString( PyList_GET_ITEM( argvlist, a ) ) ) == NULL ||
111  ( argv[a] = ( char * )sipMalloc( strlen( arg ) + 1 ) ) == NULL )
112  return NULL;
113  // Copy the argument and save a pointer to it.
114  strcpy( argv[a], arg );
115  argv[a + argc + 1] = argv[a];
116  }
117 
118  argv[argc + argc + 1] = argv[argc] = NULL;
119 
120  return argv;
121  }
122 
123  // Remove arguments from the Python argv list that have been removed from the
124  // C argv array.
125  static void qtgui_UpdatePyArgv( PyObject *argvlist, int argc, char **argv )
126  {
127  for ( int a = 0, na = 0; a < argc; ++a )
128  {
129  // See if it was removed.
130  if ( argv[na] == argv[a + argc + 1] )
131  ++na;
132  else
133  PyList_SetSlice( argvlist, na, na + 1, NULL );
134  }
135  }
136  % End
137 #endif
138 
139  Q_OBJECT
140 
141  public:
142 
152  {
153  Qt,
155  };
156 
157  static const char *QGIS_ORGANIZATION_NAME;
158  static const char *QGIS_ORGANIZATION_DOMAIN;
159  static const char *QGIS_APPLICATION_NAME;
160 #ifndef SIP_RUN
161  QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" );
162 #else
163  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" )];
164  % MethodCode
165  // The Python interface is a list of argument strings that is modified.
166 
167  int argc;
168  char **argv;
169 
170  // Convert the list.
171  if ( ( argv = qtgui_ArgvToC( a0, argc ) ) == NULL )
172  sipIsErr = 1;
173  else
174  {
175  // Create it now the arguments are right.
176  static int nargc = argc;
177 
178  sipCpp = new sipQgsApplication( nargc, argv, a1, *a2, *a3 );
179 
180  // Now modify the original list.
181  qtgui_UpdatePyArgv( a0, argc, argv );
182  }
183  % End
184 #endif
185 
186  ~QgsApplication() override;
187 
193  static QgsApplication *instance();
194 
202  static void init( QString profileFolder = QString() ) SIP_SKIP;
203 
205  bool event( QEvent *event ) override;
206 
208  bool notify( QObject *receiver, QEvent *event ) override;
209 
211  static void setFileOpenEventReceiver( QObject *receiver );
212 
223  static void setThemeName( const QString &themeName );
224 
229  static QString resolvePkgPath();
230 
238  static QString themeName();
239 
247  static void setUITheme( const QString &themeName );
248 
255  static QHash<QString, QString> uiThemes();
256 
258  static QString authorsFilePath();
259 
265  static QString contributorsFilePath();
266 
273  static QString developersMapFilePath();
274 
276  static QString sponsorsFilePath();
277 
279  static QString donorsFilePath();
280 
282  static QString serverResourcesPath();
283 
287  static QString translatorsFilePath();
288 
292  static QString licenceFilePath();
293 
295  static QString i18nPath();
296 
301  static QString metadataPath();
302 
304  static QString qgisMasterDatabaseFilePath();
305 
307  static QString qgisSettingsDirPath();
308 
310  static QString qgisUserDatabaseFilePath();
311 
313  static QString qgisAuthDatabaseFilePath();
314 
316  static QString splashPath();
317 
319  static QString iconsPath();
320 
322  static QString srsDatabaseFilePath();
323 
329  static void setSvgPaths( const QStringList &svgPaths );
330 
332  static QStringList svgPaths();
333 
338  static QStringList layoutTemplatePaths();
339 
341  static QMap<QString, QString> systemEnvVars();
342 
344  static QString prefixPath();
345 
347  static QString pluginPath();
348 
350  static QString pkgDataPath();
351 
353  static QString activeThemePath();
354 
356  static QString defaultThemePath();
357 
362  static QString iconPath( const QString &iconFile );
363 
371  static QIcon getThemeIcon( const QString &name, const QColor &fillColor = QColor(), const QColor &strokeColor = QColor() );
372 
377  enum Cursor
378  {
386  };
387 
394  static QCursor getThemeCursor( Cursor cursor );
395 
404  static QPixmap getThemePixmap( const QString &name, const QColor &foreColor = QColor(), const QColor &backColor = QColor(), int size = 16 );
405 
407  static QString userStylePath();
408 
414  static QRegularExpression shortNameRegularExpression();
415 
421  static QString userLoginName();
422 
428  static QString userFullName();
429 
435  static QString osName();
436 
442  static QString platform();
443 
448  static QString locale();
449 
451  static QString userThemesFolder();
452 
454  static QString defaultStylePath();
455 
457  static QString defaultThemesFolder();
458 
460  static QString libraryPath();
461 
463  static QString libexecPath();
464 
471  static QString qmlImportPath();
472 
474  static void setPrefixPath( const QString &prefixPath, bool useDefaultPaths = false );
475 
477  static void setPluginPath( const QString &pluginPath );
478 
480  static void setPkgDataPath( const QString &pkgDataPath );
481 
483  static void setDefaultSvgPaths( const QStringList &pathList );
484 
486  static void setAuthDatabaseDirPath( const QString &authDbDirPath );
487 
489  static void initQgis();
490 
492  static bool createDatabase( QString *errorMessage = nullptr );
493 
495  static bool createThemeFolder();
496 
498  static void exitQgis();
499 
501  static QString appIconPath();
502 
504  enum endian_t
505  {
506  XDR = 0, // network, or big-endian, byte order
507  NDR = 1 // little-endian byte order
508  };
509 
511  static endian_t endian();
512 
527  static QString reportStyleSheet( QgsApplication::StyleSheetType styleSheetType = QgsApplication::StyleSheetType::Qt );
528 
533  static QString showSettings();
534 
542  static void registerOgrDrivers();
543 
545  static QString absolutePathToRelativePath( const QString &apath, const QString &targetPath );
547  static QString relativePathToAbsolutePath( const QString &rpath, const QString &targetPath );
548 
550  static bool isRunningFromBuildDir() { return ABISYM( mRunningFromBuildDir ); }
551 #if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
552  static QString cfgIntDir() SIP_SKIP;
553 #endif
555  static QString buildSourcePath();
557  static QString buildOutputPath();
558 
564  static void skipGdalDriver( const QString &driver );
565 
571  static void restoreGdalDriver( const QString &driver );
572 
577  static QStringList skippedGdalDrivers();
578 
585  static void applyGdalSkippedDrivers();
586 
591  static void registerGdalDriversFromSettings();
592 
599  static QStringList deferredSkippedGdalDrivers();
600 
607  static void setSkippedGdalDrivers( const QStringList &skippedGdalDrivers,
608  const QStringList &deferredSkippedGdalDrivers );
609 
614  static int maxThreads();
615 
621  static void setMaxThreads( int maxThreads );
622 
628  static QgsTaskManager *taskManager();
629 
634  static QgsSettingsRegistryCore *settingsRegistryCore() SIP_KEEPREFERENCE;
635 
640  static QgsColorSchemeRegistry *colorSchemeRegistry() SIP_KEEPREFERENCE;
641 
646  static QgsPaintEffectRegistry *paintEffectRegistry() SIP_KEEPREFERENCE;
647 
652  static QgsRendererRegistry *rendererRegistry() SIP_KEEPREFERENCE;
653 
659  static QgsRasterRendererRegistry *rasterRendererRegistry() SIP_SKIP;
660 
665  static QgsPointCloudRendererRegistry *pointCloudRendererRegistry() SIP_KEEPREFERENCE;
666 
672  static QgsDataItemProviderRegistry *dataItemProviderRegistry() SIP_KEEPREFERENCE;
673 
680  static QgsCoordinateReferenceSystemRegistry *coordinateReferenceSystemRegistry() SIP_KEEPREFERENCE;
681 
689  static QgsSvgCache *svgCache();
690 
697  static QgsImageCache *imageCache();
698 
704  static QgsSourceCache *sourceCache();
705 
710  static QgsNetworkContentFetcherRegistry *networkContentFetcherRegistry() SIP_KEEPREFERENCE;
711 
716  static QgsValidityCheckRegistry *validityCheckRegistry() SIP_KEEPREFERENCE;
717 
722  static QgsSymbolLayerRegistry *symbolLayerRegistry() SIP_KEEPREFERENCE;
723 
728  static QgsCalloutRegistry *calloutRegistry() SIP_KEEPREFERENCE;
729 
734  static QgsLayoutItemRegistry *layoutItemRegistry() SIP_KEEPREFERENCE;
735 
740  static QgsAnnotationItemRegistry *annotationItemRegistry() SIP_KEEPREFERENCE;
741 
746  static QgsGpsConnectionRegistry *gpsConnectionRegistry() SIP_KEEPREFERENCE;
747 
752  static QgsPluginLayerRegistry *pluginLayerRegistry() SIP_KEEPREFERENCE;
753 
758  static QgsClassificationMethodRegistry *classificationMethodRegistry() SIP_KEEPREFERENCE;
759 
764  static QgsBookmarkManager *bookmarkManager();
765 
771  static QgsTileDownloadManager *tileDownloadManager() SIP_SKIP;
772 
780  static QgsStyleModel *defaultStyleModel();
781 
786  static QgsMessageLog *messageLog();
787 
794  static QgsAuthManager *authManager();
795 
801  static QgsProcessingRegistry *processingRegistry();
802 
807  static QgsPageSizeRegistry *pageSizeRegistry() SIP_KEEPREFERENCE;
808 
814  static QgsAnnotationRegistry *annotationRegistry() SIP_SKIP;
815 
821  static QgsActionScopeRegistry *actionScopeRegistry() SIP_KEEPREFERENCE;
822 
827  static QgsConnectionRegistry *connectionRegistry();
828 
833  static QgsRuntimeProfiler *profiler();
834 
840  static QgsNumericFormatRegistry *numericFormatRegistry() SIP_KEEPREFERENCE;
841 
845  static QgsFieldFormatterRegistry *fieldFormatterRegistry() SIP_KEEPREFERENCE;
846 
851  static Qgs3DRendererRegistry *renderer3DRegistry() SIP_KEEPREFERENCE;
852 
857  static Qgs3DSymbolRegistry *symbol3DRegistry() SIP_KEEPREFERENCE;
858 
864  static QgsScaleBarRendererRegistry *scaleBarRendererRegistry() SIP_KEEPREFERENCE;
865 
870  static QgsProjectStorageRegistry *projectStorageRegistry() SIP_KEEPREFERENCE;
871 
876  static QgsExternalStorageRegistry *externalStorageRegistry() SIP_KEEPREFERENCE;
877 
885  static QgsLocalizedDataPathRegistry *localizedDataPathRegistry() SIP_KEEPREFERENCE;
886 
895  static QString nullRepresentation();
896 
900  static void setNullRepresentation( const QString &nullRepresentation );
901 
909  static QVariantMap customVariables();
910 
918  static void setCustomVariables( const QVariantMap &customVariables );
919 
925  static void setCustomVariable( const QString &name, const QVariant &value );
926 
936  static int scaleIconSize( int standardSize, bool applyDevicePixelRatio = false );
937 
946  int maxConcurrentConnectionsPerPool() const;
947 
953  static void setTranslation( const QString &translation );
954 
960  QString translation() const;
961 
967  void collectTranslatableObjects( QgsTranslationContext *translationContext );
968 
969 #ifndef SIP_RUN
971  static const inline QgsSettingsEntryString settingsLocaleUserLocale = QgsSettingsEntryString( QStringLiteral( "locale/userLocale" ), QgsSettings::NoSection, QString() );
973  static const inline QgsSettingsEntryBool settingsLocaleOverrideFlag = QgsSettingsEntryBool( QStringLiteral( "locale/overrideFlag" ), QgsSettings::NoSection, false );
975  static const inline QgsSettingsEntryString settingsLocaleGlobalLocale = QgsSettingsEntryString( QStringLiteral( "locale/globalLocale" ), QgsSettings::NoSection, QString() );
977  static const inline QgsSettingsEntryBool settingsLocaleShowGroupSeparator = QgsSettingsEntryBool( QStringLiteral( "locale/showGroupSeparator" ), QgsSettings::NoSection, false );
979  static const inline QgsSettingsEntryStringList settingsSearchPathsForSVG = QgsSettingsEntryStringList( QStringLiteral( "svg/searchPathsForSVG" ), QgsSettings::NoSection, QStringList() );
980 #endif
981 
982 #ifdef SIP_RUN
983  SIP_IF_FEATURE( ANDROID )
984  //dummy method to workaround sip generation issue
985  bool x11EventFilter( XEvent *event );
986  SIP_END
987 #endif
988 
989  signals:
991  void preNotify( QObject *receiver, QEvent *event, bool *done ) SIP_SKIP;
992 
998 
999 
1004 
1012 
1013  private:
1014 
1015  static void copyPath( const QString &src, const QString &dst );
1016  static QObject *ABISYM( mFileOpenEventReceiver );
1017 
1018  static bool ABISYM( mInitialized );
1019 
1021  static bool ABISYM( mRunningFromBuildDir );
1022 
1026  static int ABISYM( sMaxThreads );
1027 
1028  QMap<QString, QIcon> mIconCache;
1029  QMap<Cursor, QCursor> mCursorCache;
1030 
1031  QTranslator *mQgisTranslator = nullptr;
1032  QTranslator *mQtTranslator = nullptr;
1033  QTranslator *mQtBaseTranslator = nullptr;
1034 
1035  QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
1036  QgsAuthManager *mAuthManager = nullptr;
1037 
1038  struct ApplicationMembers
1039  {
1040  QgsSettingsRegistryCore *mSettingsRegistryCore = nullptr;
1041  QgsCoordinateReferenceSystemRegistry *mCrsRegistry = nullptr;
1042  Qgs3DRendererRegistry *m3DRendererRegistry = nullptr;
1043  Qgs3DSymbolRegistry *m3DSymbolRegistry = nullptr;
1044  QgsActionScopeRegistry *mActionScopeRegistry = nullptr;
1045  QgsAnnotationRegistry *mAnnotationRegistry = nullptr;
1046  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
1047  QgsLocalizedDataPathRegistry *mLocalizedDataPathRegistry = nullptr;
1048  QgsNumericFormatRegistry *mNumericFormatRegistry = nullptr;
1049  QgsFieldFormatterRegistry *mFieldFormatterRegistry = nullptr;
1050  QgsGpsConnectionRegistry *mGpsConnectionRegistry = nullptr;
1051  QgsNetworkContentFetcherRegistry *mNetworkContentFetcherRegistry = nullptr;
1052  QgsScaleBarRendererRegistry *mScaleBarRendererRegistry = nullptr;
1053  QgsValidityCheckRegistry *mValidityCheckRegistry = nullptr;
1054  QgsMessageLog *mMessageLog = nullptr;
1055  QgsPaintEffectRegistry *mPaintEffectRegistry = nullptr;
1056  QgsPluginLayerRegistry *mPluginLayerRegistry = nullptr;
1057  QgsClassificationMethodRegistry *mClassificationMethodRegistry = nullptr;
1058  QgsProcessingRegistry *mProcessingRegistry = nullptr;
1059  QgsConnectionRegistry *mConnectionRegistry = nullptr;
1060  std::unique_ptr<QgsProjectStorageRegistry> mProjectStorageRegistry;
1061  QgsExternalStorageRegistry *mExternalStorageRegistry = nullptr;
1062  QgsPageSizeRegistry *mPageSizeRegistry = nullptr;
1063  QgsRasterRendererRegistry *mRasterRendererRegistry = nullptr;
1064  QgsRendererRegistry *mRendererRegistry = nullptr;
1065  QgsPointCloudRendererRegistry *mPointCloudRendererRegistry = nullptr;
1066  QgsSvgCache *mSvgCache = nullptr;
1067  QgsImageCache *mImageCache = nullptr;
1068  QgsSourceCache *mSourceCache = nullptr;
1069  QgsSymbolLayerRegistry *mSymbolLayerRegistry = nullptr;
1070  QgsCalloutRegistry *mCalloutRegistry = nullptr;
1071  QgsTaskManager *mTaskManager = nullptr;
1072  QgsLayoutItemRegistry *mLayoutItemRegistry = nullptr;
1073  QgsAnnotationItemRegistry *mAnnotationItemRegistry = nullptr;
1074  QgsUserProfileManager *mUserConfigManager = nullptr;
1075  QgsBookmarkManager *mBookmarkManager = nullptr;
1076  QgsTileDownloadManager *mTileDownloadManager = nullptr;
1077  QgsStyleModel *mStyleModel = nullptr;
1078  QString mNullRepresentation;
1079  QStringList mSvgPathCache;
1080  bool mSvgPathCacheValid = false;
1081 
1082  ApplicationMembers();
1083  ~ApplicationMembers();
1084  };
1085 
1086  // Applications members which belong to an instance of QgsApplication
1087  ApplicationMembers *mApplicationMembers = nullptr;
1088  // ... but in case QgsApplication is never instantiated (eg with custom designer widgets), we fall back to static members
1089  static ApplicationMembers *sApplicationMembers;
1090 
1091  static QgsAuthManager *sAuthManager;
1092 
1093  static ApplicationMembers *members();
1094 
1095  static void invalidateCaches();
1096 
1097  friend class TestQgsApplication;
1098 };
1099 
1100 // clazy:excludeall=qstring-allocations
1101 
1102 #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...
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.
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)