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