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