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