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