QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsgui.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgui.cpp
3 ----------
4 begin : May 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include <QScreen>
20#include <QMessageBox>
21
22#include "qgsgui.h"
29#ifdef Q_OS_MACX
30#include "qgsmacnative.h"
31#elif defined (Q_OS_WIN)
32#ifndef __MINGW32__
33#include "qgswinnative.h"
34#else
35#include "qgsnative.h"
36#endif
37#elif defined (Q_OS_LINUX)
38#include "qgslinuxnative.h"
39#else
40#include "qgsnative.h"
41#endif
43#include "qgsshortcutsmanager.h"
45#include "qgslogger.h"
48#include "qgssettings.h"
52#include "qgsmessagebar.h"
53#include "qgsmessagebaritem.h"
65#include "qgshistoryentry.h"
66
68
69
70#include <QPushButton>
71#include <QToolButton>
72
74{
75 static QgsGui *sInstance( new QgsGui() );
76 return sInstance;
77}
78
80{
81 return instance()->mNative;
82}
83
85{
86 return instance()->mSettingsRegistryGui;
87}
88
90{
91 return instance()->mEditorWidgetRegistry;
92}
93
95{
96 return instance()->mRelationEditorRegistry;
97}
98
100{
101 return instance()->mShapeMapToolRegistry;
102}
103
105{
106 return instance()->mSourceSelectProviderRegistry;
107}
108
110{
111 return instance()->mSubsetStringEditorProviderRegistry;
112}
113
115{
116 return instance()->mProviderSourceWidgetProviderRegistry;
117}
118
120{
121 return instance()->mShortcutsManager;
122}
123
125{
126 return instance()->mLayerTreeEmbeddedWidgetRegistry;
127}
128
130{
131 return instance()->mMapLayerActionRegistry;
132}
133
135{
136 return instance()->mLayoutItemGuiRegistry;
137}
138
140{
141 return instance()->mAnnotationItemGuiRegistry;
142}
143
145{
146 return instance()->mProcessingGuiRegistry;
147}
148
150{
151 return instance()->mNumericFormatGuiRegistry;
152}
153
155{
156 return instance()->mCodeEditorColorSchemeRegistry;
157}
158
159QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
160{
161 return instance()->mProcessingRecentAlgorithmLog;
162}
163
165{
166 return instance()->mDataItemGuiProviderRegistry;
167}
168
170{
171 return instance()->mProjectStorageGuiRegistry;
172}
173
175{
176 return instance()->mProviderGuiRegistry;
177}
178
180{
181 return instance()->mSensorGuiRegistry;
182}
183
185{
186 return instance()->mHistoryProviderRegistry;
187}
188
190{
191 return instance()->mSettingsEditorRegistry;
192}
193
194void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
195{
196 if ( widget->objectName().isEmpty() )
197 {
198 QgsDebugError( QStringLiteral( "WARNING: No object name set. Best for it to be set objectName when using QgsGui::enableAutoGeometryRestore" ) );
199 }
200 instance()->mWidgetStateHelper->registerWidget( widget, key );
201}
202
203QgsWindowManagerInterface *QgsGui::windowManager()
204{
205 return instance()->mWindowManager.get();
206}
207
209{
210 return instance()->mInputControllerManager;
211}
212
213void QgsGui::setWindowManager( QgsWindowManagerInterface *manager )
214{
215 instance()->mWindowManager.reset( manager );
216}
217
219{
220 if ( QgsApplication::settingsLocaleUserLocale->value().startsWith( QLatin1String( "en" ) ) )
221 {
223 }
224 else
225 {
226 return QgsGui::HigFlags();
227 }
228}
229
231{
232 delete mProcessingGuiRegistry;
233 delete mDataItemGuiProviderRegistry;
234 delete mProcessingRecentAlgorithmLog;
235 delete mLayoutItemGuiRegistry;
236 delete mAnnotationItemGuiRegistry;
237 delete mLayerTreeEmbeddedWidgetRegistry;
238 delete mEditorWidgetRegistry;
239 delete mMapLayerActionRegistry;
240 delete mSourceSelectProviderRegistry;
241 delete mHistoryProviderRegistry;
242 delete mShortcutsManager;
243 delete mNative;
244 delete mNumericFormatGuiRegistry;
245 delete mWidgetStateHelper;
246 delete mProjectStorageGuiRegistry;
247 delete mProviderGuiRegistry;
248 delete mCodeEditorColorSchemeRegistry;
249 delete mSubsetStringEditorProviderRegistry;
250 delete mProviderSourceWidgetProviderRegistry;
251 delete mShapeMapToolRegistry;
252 delete mRelationEditorRegistry;
253 delete mInputControllerManager;
254 delete mSettingsRegistryGui;
255 delete mSensorGuiRegistry;
256 delete mSettingsEditorRegistry;
257}
258
259QColor QgsGui::sampleColor( QPoint point )
260{
261 QScreen *screen = findScreenAt( point );
262 if ( ! screen )
263 {
264 return QColor();
265 }
266
267 const int x = point.x() - screen->geometry().left();
268 const int y = point.y() - screen->geometry().top();
269 const QPixmap snappedPixmap = screen->grabWindow( 0, x, y, 1, 1 );
270 const QImage snappedImage = snappedPixmap.toImage();
271 return snappedImage.pixel( 0, 0 );
272}
273
274QScreen *QgsGui::findScreenAt( QPoint point )
275{
276 const QList< QScreen * > screens = QGuiApplication::screens();
277 for ( QScreen *screen : screens )
278 {
279 if ( screen->geometry().contains( point ) )
280 {
281 return screen;
282 }
283 }
284 return nullptr;
285}
286
287QgsGui::QgsGui()
288{
289#ifdef Q_OS_MAC
290 QgsMacNative *macNative = new QgsMacNative();
291 macNative->setIconPath( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) );
292 mNative = macNative;
293#elif defined (Q_OS_WIN)
294#ifndef __MINGW32__
295 mNative = new QgsWinNative();
296#else
297 mNative = new QgsNative();
298#endif
299#elif defined(Q_OS_LINUX)
300 mNative = new QgsLinuxNative();
301#else
302 mNative = new QgsNative();
303#endif
304
305 mSettingsRegistryGui = new QgsSettingsRegistryGui();
306
307 mSettingsEditorRegistry = new QgsSettingsEditorWidgetRegistry();
308
309 mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();
310
311 // provider gui registry initialize QgsProviderRegistry too
312 mSensorGuiRegistry = new QgsSensorGuiRegistry();
313 mSensorGuiRegistry->populate();
314
315 mHistoryProviderRegistry = new QgsHistoryProviderRegistry();
316 mHistoryProviderRegistry->addDefaultProviders();
317
318 mInputControllerManager = new QgsInputControllerManager();
319
320 mProviderGuiRegistry = new QgsProviderGuiRegistry( QgsApplication::pluginPath() );
321 mProjectStorageGuiRegistry = new QgsProjectStorageGuiRegistry();
322 mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
323 mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
324 mNumericFormatGuiRegistry = new QgsNumericFormatGuiRegistry();
325 mSubsetStringEditorProviderRegistry = new QgsSubsetStringEditorProviderRegistry();
326 mProviderSourceWidgetProviderRegistry = new QgsProviderSourceWidgetProviderRegistry();
327
328 mProjectStorageGuiRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
329 mDataItemGuiProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
330 mSourceSelectProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
331 mSourceSelectProviderRegistry->addProvider( new QgsLayerMetadataSourceSelectProvider() );
332 mSubsetStringEditorProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
333 mProviderSourceWidgetProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
334
335 mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
336 mRelationEditorRegistry = new QgsRelationWidgetRegistry();
337 mShapeMapToolRegistry = new QgsMapToolShapeRegistry();
338 mShortcutsManager = new QgsShortcutsManager();
339 mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
340 mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
341 mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
342
343 mAnnotationItemGuiRegistry = new QgsAnnotationItemGuiRegistry();
344 mAnnotationItemGuiRegistry->addDefaultItems();
345
346 mWidgetStateHelper = new QgsWidgetStateHelper();
347 mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
348 mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
349
350 qRegisterMetaType< QgsHistoryEntry >( "QgsHistoryEntry" );
351}
352
353bool QgsGui::pythonMacroAllowed( void ( *lambda )(), QgsMessageBar *messageBar )
354{
355 const Qgis::PythonMacroMode macroMode = QgsSettings().enumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::Ask );
356
357 switch ( macroMode )
358 {
361 if ( lambda )
362 lambda();
363 return true;
366 if ( messageBar )
367 {
368 messageBar->pushMessage( tr( "Python Macros" ),
369 tr( "Python macros are currently disabled and will not be run" ),
370 Qgis::MessageLevel::Warning );
371 }
372 return false;
374 if ( !lambda )
375 {
376 QMessageBox msgBox( QMessageBox::Information, tr( "Python Macros" ),
377 tr( "Python macros are currently disabled. Do you allow this macro to run?" ) );
378 QAbstractButton *stopSessionButton = msgBox.addButton( tr( "Disable for this Session" ), QMessageBox::DestructiveRole );
379 msgBox.addButton( tr( "No" ), QMessageBox::NoRole );
380 QAbstractButton *yesButton = msgBox.addButton( tr( "Yes" ), QMessageBox::YesRole );
381 msgBox.exec();
382
383 QAbstractButton *clicked = msgBox.clickedButton();
384 if ( clicked == stopSessionButton )
385 {
386 QgsSettings().setEnumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::NotForThisSession );
387 }
388 return clicked == yesButton;
389 }
390 else
391 {
392 // create the notification widget for macros
393 Q_ASSERT( messageBar );
394 if ( messageBar )
395 {
396 QToolButton *btnEnableMacros = new QToolButton();
397 btnEnableMacros->setText( tr( "Enable Macros" ) );
398 btnEnableMacros->setStyleSheet( QStringLiteral( "background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
399 btnEnableMacros->setCursor( Qt::PointingHandCursor );
400 btnEnableMacros->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
401
402 QgsMessageBarItem *macroMsg = new QgsMessageBarItem(
403 tr( "Security warning" ),
404 tr( "Python macros cannot currently be run." ),
405 btnEnableMacros,
406 Qgis::MessageLevel::Warning,
407 0,
408 messageBar );
409
410 connect( btnEnableMacros, &QToolButton::clicked, messageBar, [ = ]()
411 {
412 lambda();
413 messageBar->popWidget( macroMsg );
414 } );
415
416 // display the macros notification widget
417 messageBar->pushItem( macroMsg );
418 }
419
420 return false;
421 }
422 }
423 return false;
424}
425
427void QgsGui::emitOptionsChanged()
428{
429 emit optionsChanged();
430}
PythonMacroMode
Authorisation to run Python Macros.
Definition: qgis.h:321
@ Always
Macros are always run.
@ NotForThisSession
Macros will not be run for this session.
@ Never
Macros are never run.
@ Ask
User is prompt before running.
@ SessionOnly
Only during this session.
Registry of available annotation item GUI behavior.
void addDefaultItems()
Populates the registry with default items.
static QString pluginPath()
Returns the path to the application plugin directory.
static const QgsSettingsEntryString * settingsLocaleUserLocale
Settings entry locale user locale.
static QString iconsPath()
Returns the path to the icons image directory.
A registry of color schemes for use in QgsCodeEditor widgets.
This class keeps a list of data item GUI providers that may affect how QgsDataItems behave within the...
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
This class manages all known edit widget factories.
QgsGui is a singleton class containing various registry and other global members related to GUI class...
Definition: qgsgui.h:60
static QgsMapToolShapeRegistry * mapToolShapeRegistry()
Returns the registry of shape map tools.
Definition: qgsgui.cpp:99
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition: qgsgui.cpp:89
static QgsProviderSourceWidgetProviderRegistry * sourceWidgetProviderRegistry()
Returns the registry of provider source widget providers.
Definition: qgsgui.cpp:114
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:144
static QgsShortcutsManager * shortcutsManager()
Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
Definition: qgsgui.cpp:119
static void setWindowManager(QgsWindowManagerInterface *manager)
Sets the global window manager.
Definition: qgsgui.cpp:213
void optionsChanged()
This signal is emitted whenever the application options have been changed.
static QgsInputControllerManager * inputControllerManager()
Returns the global input controller manager.
Definition: qgsgui.cpp:208
static bool pythonMacroAllowed(void(*lambda)()=nullptr, QgsMessageBar *messageBar=nullptr)
Returns true if python macros are currently allowed to be run If the global option is to ask user,...
Definition: qgsgui.cpp:353
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:124
static QScreen * findScreenAt(QPoint point)
Returns the screen at the given global point (pixel).
Definition: qgsgui.cpp:274
static QgsAnnotationItemGuiRegistry * annotationItemGuiRegistry()
Returns the global annotation item GUI registry, used for registering the GUI behavior of annotation ...
Definition: qgsgui.cpp:139
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:129
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:73
static QgsProviderGuiRegistry * providerGuiRegistry()
Returns the registry of GUI-related components of data providers.
Definition: qgsgui.cpp:174
static QgsRelationWidgetRegistry * relationWidgetRegistry()
Returns the global relation widget registry, used for managing all known relation widget factories.
Definition: qgsgui.cpp:94
static QgsSensorGuiRegistry * sensorGuiRegistry()
Returns the registry of GUI-related components for sensors.
Definition: qgsgui.cpp:179
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:194
static QgsHistoryProviderRegistry * historyProviderRegistry()
Returns the global history provider registry, used for tracking history providers.
Definition: qgsgui.cpp:184
@ HigMenuTextIsTitleCase
Menu action texts should be title case.
Definition: qgsgui.h:254
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:255
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition: qgsgui.cpp:79
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
Definition: qgsgui.cpp:203
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:164
static QgsProcessingRecentAlgorithmLog * processingRecentAlgorithmLog()
Returns the global processing recent algorithm log, used for tracking recently used processing algori...
Definition: qgsgui.cpp:159
QFlags< HigFlag > HigFlags
Definition: qgsgui.h:257
static QgsSubsetStringEditorProviderRegistry * subsetStringEditorProviderRegistry()
Returns the registry of subset string editors of data providers.
Definition: qgsgui.cpp:109
static QgsProjectStorageGuiRegistry * projectStorageGuiRegistry()
Returns the global GUI-related project storage registry.
Definition: qgsgui.cpp:169
static QgsGui::HigFlags higFlags()
Returns the platform's HIG flags.
Definition: qgsgui.cpp:218
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition: qgsgui.cpp:134
static QgsSettingsRegistryGui * settingsRegistryGui()
Returns the gui's settings registry, used for managing gui settings.
Definition: qgsgui.cpp:84
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:104
static QgsCodeEditorColorSchemeRegistry * codeEditorColorSchemeRegistry()
Returns the global code editor color scheme registry, used for registering the color schemes for QgsC...
Definition: qgsgui.cpp:154
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition: qgsgui.cpp:149
static QgsSettingsEditorWidgetRegistry * settingsEditorWidgetRegistry()
Returns the registry of settings editors.
Definition: qgsgui.cpp:189
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition: qgsgui.cpp:259
~QgsGui()
Definition: qgsgui.cpp:230
The QgsHistoryProviderRegistry is a registry for objects which track user history (i....
void addDefaultProviders()
Adds the default history providers to the registry.
Manages input control devices.
Source select provider for layer metadata.
Registry of widgets that may be embedded into layer tree view.
Registry of available layout item GUI behavior.
This class tracks map layer actions.
Keeps track of the registered shape map tools.
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
bool popWidget(QgsMessageBarItem *item)
Remove the specified item from the bar, and display the next most recent one in the stack.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar, after hiding the currently visible one and putting it in a stack.
The QgsNumericFormatGuiRegistry is a home for widgets for configuring QgsNumericFormat objects.
The QgsProcessingGuiRegistry is a home for widgets for processing configuration widgets.
A registry / canonical manager of GUI parts of project storage backends.
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
A registry / canonical manager of GUI parts of data providers.
This class keeps a list of provider source widget providers.
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
Keeps track of the registered relations widgets.
Registry of available sensor GUI behavior.
bool populate()
Populates the registry with standard sensor types.
This class manages editor widgets for settings.
QgsSettingsRegistryGui is used for settings introspection and collects all QgsSettingsEntry instances...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:64
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
Definition: qgssettings.h:315
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:263
Shortcuts manager is a class that contains a list of QActions and QShortcuts that have been registere...
This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDial...
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
void addProvider(QgsSourceSelectProvider *provider)
Add a provider implementation. Takes ownership of the object.
This class keeps a list of subset string editor providers.
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
QgsWidgetStateHelper is a helper class to save and restore the geometry of QWidgets in the applicatio...
void registerWidget(QWidget *widget, const QString &key=QString())
Register a widget to have it geometry state automatically saved and restored.
#define QgsDebugError(str)
Definition: qgslogger.h:38