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