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