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