QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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"
64#include "qgshistoryentry.h"
65
67
68
69#include <QPushButton>
70#include <QToolButton>
71
73{
74 static QgsGui *sInstance( new QgsGui() );
75 return sInstance;
76}
77
79{
80 return instance()->mNative;
81}
82
84{
85 return instance()->mSettingsRegistryGui;
86}
87
89{
90 return instance()->mEditorWidgetRegistry;
91}
92
94{
95 return instance()->mRelationEditorRegistry;
96}
97
99{
100 return instance()->mShapeMapToolRegistry;
101}
102
104{
105 return instance()->mSourceSelectProviderRegistry;
106}
107
109{
110 return instance()->mSubsetStringEditorProviderRegistry;
111}
112
114{
115 return instance()->mProviderSourceWidgetProviderRegistry;
116}
117
119{
120 return instance()->mShortcutsManager;
121}
122
124{
125 return instance()->mLayerTreeEmbeddedWidgetRegistry;
126}
127
129{
130 return instance()->mMapLayerActionRegistry;
131}
132
134{
135 return instance()->mLayoutItemGuiRegistry;
136}
137
139{
140 return instance()->mAnnotationItemGuiRegistry;
141}
142
144{
145 return instance()->mProcessingGuiRegistry;
146}
147
149{
150 return instance()->mNumericFormatGuiRegistry;
151}
152
154{
155 return instance()->mCodeEditorColorSchemeRegistry;
156}
157
158QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
159{
160 return instance()->mProcessingRecentAlgorithmLog;
161}
162
164{
165 return instance()->mDataItemGuiProviderRegistry;
166}
167
169{
170 return instance()->mProjectStorageGuiRegistry;
171}
172
174{
175 return instance()->mProviderGuiRegistry;
176}
177
179{
180 return instance()->mSensorGuiRegistry;
181}
182
184{
185 return instance()->mHistoryProviderRegistry;
186}
187
189{
190 return instance()->mSettingsEditorRegistry;
191}
192
193void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
194{
195 if ( widget->objectName().isEmpty() )
196 {
197 QgsDebugError( QStringLiteral( "WARNING: No object name set. Best for it to be set objectName when using QgsGui::enableAutoGeometryRestore" ) );
198 }
199 instance()->mWidgetStateHelper->registerWidget( widget, key );
200}
201
202QgsWindowManagerInterface *QgsGui::windowManager()
203{
204 return instance()->mWindowManager.get();
205}
206
207void QgsGui::setWindowManager( QgsWindowManagerInterface *manager )
208{
209 instance()->mWindowManager.reset( manager );
210}
211
212QgsGui::HigFlags QgsGui::higFlags()
213{
214 if ( QgsApplication::settingsLocaleUserLocale->value().startsWith( QLatin1String( "en" ) ) )
215 {
217 }
218 else
219 {
220 return QgsGui::HigFlags();
221 }
222}
223
225{
226 delete mProcessingGuiRegistry;
227 delete mDataItemGuiProviderRegistry;
228 delete mProcessingRecentAlgorithmLog;
229 delete mLayoutItemGuiRegistry;
230 delete mAnnotationItemGuiRegistry;
231 delete mLayerTreeEmbeddedWidgetRegistry;
232 delete mEditorWidgetRegistry;
233 delete mMapLayerActionRegistry;
234 delete mSourceSelectProviderRegistry;
235 delete mHistoryProviderRegistry;
236 delete mShortcutsManager;
237 delete mNative;
238 delete mNumericFormatGuiRegistry;
239 delete mWidgetStateHelper;
240 delete mProjectStorageGuiRegistry;
241 delete mProviderGuiRegistry;
242 delete mCodeEditorColorSchemeRegistry;
243 delete mSubsetStringEditorProviderRegistry;
244 delete mProviderSourceWidgetProviderRegistry;
245 delete mShapeMapToolRegistry;
246 delete mRelationEditorRegistry;
247 delete mSettingsRegistryGui;
248 delete mSensorGuiRegistry;
249 delete mSettingsEditorRegistry;
250}
251
252QColor QgsGui::sampleColor( QPoint point )
253{
254 QScreen *screen = findScreenAt( point );
255 if ( ! screen )
256 {
257 return QColor();
258 }
259
260 const int x = point.x() - screen->geometry().left();
261 const int y = point.y() - screen->geometry().top();
262 const QPixmap snappedPixmap = screen->grabWindow( 0, x, y, 1, 1 );
263 const QImage snappedImage = snappedPixmap.toImage();
264 return snappedImage.pixel( 0, 0 );
265}
266
267QScreen *QgsGui::findScreenAt( QPoint point )
268{
269 const QList< QScreen * > screens = QGuiApplication::screens();
270 for ( QScreen *screen : screens )
271 {
272 if ( screen->geometry().contains( point ) )
273 {
274 return screen;
275 }
276 }
277 return nullptr;
278}
279
280QgsGui::QgsGui()
281{
282#ifdef Q_OS_MAC
283 QgsMacNative *macNative = new QgsMacNative();
284 macNative->setIconPath( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) );
285 mNative = macNative;
286#elif defined (Q_OS_WIN)
287#ifndef __MINGW32__
288 mNative = new QgsWinNative();
289#else
290 mNative = new QgsNative();
291#endif
292#elif defined(Q_OS_LINUX)
293 mNative = new QgsLinuxNative();
294#else
295 mNative = new QgsNative();
296#endif
297
298 mSettingsRegistryGui = new QgsSettingsRegistryGui();
299
300 mSettingsEditorRegistry = new QgsSettingsEditorWidgetRegistry();
301
302 mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();
303
304 // provider gui registry initialize QgsProviderRegistry too
305 mSensorGuiRegistry = new QgsSensorGuiRegistry();
306 mSensorGuiRegistry->populate();
307
308 mHistoryProviderRegistry = new QgsHistoryProviderRegistry();
309 mHistoryProviderRegistry->addDefaultProviders();
310
311 mProviderGuiRegistry = new QgsProviderGuiRegistry( QgsApplication::pluginPath() );
312 mProjectStorageGuiRegistry = new QgsProjectStorageGuiRegistry();
313 mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
314 mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
315 mNumericFormatGuiRegistry = new QgsNumericFormatGuiRegistry();
316 mSubsetStringEditorProviderRegistry = new QgsSubsetStringEditorProviderRegistry();
317 mProviderSourceWidgetProviderRegistry = new QgsProviderSourceWidgetProviderRegistry();
318
319 mProjectStorageGuiRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
320 mDataItemGuiProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
321 mSourceSelectProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
322 mSourceSelectProviderRegistry->addProvider( new QgsLayerMetadataSourceSelectProvider() );
323 mSubsetStringEditorProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
324 mProviderSourceWidgetProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
325
326 mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
327 mRelationEditorRegistry = new QgsRelationWidgetRegistry();
328 mShapeMapToolRegistry = new QgsMapToolShapeRegistry();
329 mShortcutsManager = new QgsShortcutsManager();
330 mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
331 mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
332 mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
333
334 mAnnotationItemGuiRegistry = new QgsAnnotationItemGuiRegistry();
335 mAnnotationItemGuiRegistry->addDefaultItems();
336
337 mWidgetStateHelper = new QgsWidgetStateHelper();
338 mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
339 mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
340
341 qRegisterMetaType< QgsHistoryEntry >( "QgsHistoryEntry" );
342}
343
344bool QgsGui::pythonMacroAllowed( void ( *lambda )(), QgsMessageBar *messageBar )
345{
346 const Qgis::PythonMacroMode macroMode = QgsSettings().enumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::Ask );
347
348 switch ( macroMode )
349 {
352 if ( lambda )
353 lambda();
354 return true;
357 if ( messageBar )
358 {
359 messageBar->pushMessage( tr( "Python Macros" ),
360 tr( "Python macros are currently disabled and will not be run" ),
361 Qgis::MessageLevel::Warning );
362 }
363 return false;
365 if ( !lambda )
366 {
367 QMessageBox msgBox( QMessageBox::Information, tr( "Python Macros" ),
368 tr( "Python macros are currently disabled. Do you allow this macro to run?" ) );
369 QAbstractButton *stopSessionButton = msgBox.addButton( tr( "Disable for this Session" ), QMessageBox::DestructiveRole );
370 msgBox.addButton( tr( "No" ), QMessageBox::NoRole );
371 QAbstractButton *yesButton = msgBox.addButton( tr( "Yes" ), QMessageBox::YesRole );
372 msgBox.exec();
373
374 QAbstractButton *clicked = msgBox.clickedButton();
375 if ( clicked == stopSessionButton )
376 {
377 QgsSettings().setEnumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::NotForThisSession );
378 }
379 return clicked == yesButton;
380 }
381 else
382 {
383 // create the notification widget for macros
384 Q_ASSERT( messageBar );
385 if ( messageBar )
386 {
387 QToolButton *btnEnableMacros = new QToolButton();
388 btnEnableMacros->setText( tr( "Enable Macros" ) );
389 btnEnableMacros->setStyleSheet( QStringLiteral( "background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
390 btnEnableMacros->setCursor( Qt::PointingHandCursor );
391 btnEnableMacros->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
392
393 QgsMessageBarItem *macroMsg = new QgsMessageBarItem(
394 tr( "Security warning" ),
395 tr( "Python macros cannot currently be run." ),
396 btnEnableMacros,
397 Qgis::MessageLevel::Warning,
398 0,
399 messageBar );
400
401 connect( btnEnableMacros, &QToolButton::clicked, messageBar, [ = ]()
402 {
403 lambda();
404 messageBar->popWidget( macroMsg );
405 } );
406
407 // display the macros notification widget
408 messageBar->pushItem( macroMsg );
409 }
410
411 return false;
412 }
413 }
414 return false;
415}
416
418void QgsGui::emitOptionsChanged()
419{
420 emit optionsChanged();
421}
PythonMacroMode
Vector layer type flags.
Definition: qgis.h:293
@ 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:98
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition: qgsgui.cpp:88
static QgsProviderSourceWidgetProviderRegistry * sourceWidgetProviderRegistry()
Returns the registry of provider source widget providers.
Definition: qgsgui.cpp:113
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:143
static QgsShortcutsManager * shortcutsManager()
Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
Definition: qgsgui.cpp:118
static void setWindowManager(QgsWindowManagerInterface *manager)
Sets the global window manager.
Definition: qgsgui.cpp:207
void optionsChanged()
This signal is emitted whenever the application options have been changed.
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:344
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:123
static QScreen * findScreenAt(QPoint point)
Returns the screen at the given global point (pixel).
Definition: qgsgui.cpp:267
static QgsAnnotationItemGuiRegistry * annotationItemGuiRegistry()
Returns the global annotation item GUI registry, used for registering the GUI behavior of annotation ...
Definition: qgsgui.cpp:138
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:128
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:72
static QgsProviderGuiRegistry * providerGuiRegistry()
Returns the registry of GUI-related components of data providers.
Definition: qgsgui.cpp:173
static QgsRelationWidgetRegistry * relationWidgetRegistry()
Returns the global relation widget registry, used for managing all known relation widget factories.
Definition: qgsgui.cpp:93
static QgsSensorGuiRegistry * sensorGuiRegistry()
Returns the registry of GUI-related components for sensors.
Definition: qgsgui.cpp:178
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:193
static QgsHistoryProviderRegistry * historyProviderRegistry()
Returns the global history provider registry, used for tracking history providers.
Definition: qgsgui.cpp:183
@ HigMenuTextIsTitleCase
Menu action texts should be title case.
Definition: qgsgui.h:248
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:249
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition: qgsgui.cpp:78
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
Definition: qgsgui.cpp:202
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:163
static QgsProcessingRecentAlgorithmLog * processingRecentAlgorithmLog()
Returns the global processing recent algorithm log, used for tracking recently used processing algori...
Definition: qgsgui.cpp:158
static QgsSubsetStringEditorProviderRegistry * subsetStringEditorProviderRegistry()
Returns the registry of subset string editors of data providers.
Definition: qgsgui.cpp:108
static QgsProjectStorageGuiRegistry * projectStorageGuiRegistry()
Returns the global GUI-related project storage registry.
Definition: qgsgui.cpp:168
static QgsGui::HigFlags higFlags()
Returns the platform's HIG flags.
Definition: qgsgui.cpp:212
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition: qgsgui.cpp:133
static QgsSettingsRegistryGui * settingsRegistryGui()
Returns the gui's settings registry, used for managing gui settings.
Definition: qgsgui.cpp:83
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:103
static QgsCodeEditorColorSchemeRegistry * codeEditorColorSchemeRegistry()
Returns the global code editor color scheme registry, used for registering the color schemes for QgsC...
Definition: qgsgui.cpp:153
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition: qgsgui.cpp:148
static QgsSettingsEditorWidgetRegistry * settingsEditorWidgetRegistry()
Returns the registry of settings editors.
Definition: qgsgui.cpp:188
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition: qgsgui.cpp:252
~QgsGui()
Definition: qgsgui.cpp:224
The QgsHistoryProviderRegistry is a registry for objects which track user history (i....
void addDefaultProviders()
Adds the default history providers to the registry.
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:63
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:314
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:262
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