QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  if ( QgsApplication::settingsLocaleUserLocale.value().startsWith( QLatin1String( "en" ) ) )
178  {
180  }
181  else
182  {
183  return QgsGui::HigFlags();
184  }
185 }
186 
188 {
189  delete mProcessingGuiRegistry;
190  delete mDataItemGuiProviderRegistry;
191  delete mProcessingRecentAlgorithmLog;
192  delete mLayoutItemGuiRegistry;
193  delete mLayerTreeEmbeddedWidgetRegistry;
194  delete mEditorWidgetRegistry;
195  delete mMapLayerActionRegistry;
196  delete mSourceSelectProviderRegistry;
197  delete mShortcutsManager;
198  delete mNative;
199  delete mNumericFormatGuiRegistry;
200  delete mWidgetStateHelper;
201  delete mProjectStorageGuiRegistry;
202  delete mProviderGuiRegistry;
203  delete mCodeEditorColorSchemeRegistry;
204  delete mSubsetStringEditorProviderRegistry;
205  delete mProviderSourceWidgetProviderRegistry;
206  delete mRelationEditorRegistry;
207 }
208 
209 QColor QgsGui::sampleColor( QPoint point )
210 {
211  QScreen *screen = findScreenAt( point );
212  if ( ! screen )
213  {
214  return QColor();
215  }
216  QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
217  QImage snappedImage = snappedPixmap.toImage();
218  return snappedImage.pixel( 0, 0 );
219 }
220 
221 QScreen *QgsGui::findScreenAt( QPoint point )
222 {
223  const QList< QScreen * > screens = QGuiApplication::screens();
224  for ( QScreen *screen : screens )
225  {
226  if ( screen->geometry().contains( point ) )
227  {
228  return screen;
229  }
230  }
231  return nullptr;
232 }
233 
234 QgsGui::QgsGui()
235 {
236 #ifdef Q_OS_MAC
237  QgsMacNative *macNative = new QgsMacNative();
238  macNative->setIconPath( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) );
239  mNative = macNative;
240 #elif defined (Q_OS_WIN)
241 #ifndef __MINGW32__
242  mNative = new QgsWinNative();
243 #else
244  mNative = new QgsNative();
245 #endif
246 #elif defined(Q_OS_LINUX)
247  mNative = new QgsLinuxNative();
248 #else
249  mNative = new QgsNative();
250 #endif
251 
252  mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();
253 
254  // provider gui registry initialize QgsProviderRegistry too
255  mProviderGuiRegistry = new QgsProviderGuiRegistry( QgsApplication::pluginPath() );
256  mProjectStorageGuiRegistry = new QgsProjectStorageGuiRegistry();
257  mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
258  mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
259  mNumericFormatGuiRegistry = new QgsNumericFormatGuiRegistry();
260  mSubsetStringEditorProviderRegistry = new QgsSubsetStringEditorProviderRegistry();
261  mProviderSourceWidgetProviderRegistry = new QgsProviderSourceWidgetProviderRegistry();
262 
263  mProjectStorageGuiRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
264  mDataItemGuiProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
265  mSourceSelectProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
266  mSubsetStringEditorProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
267  mProviderSourceWidgetProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
268 
269  mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
270  mRelationEditorRegistry = new QgsRelationWidgetRegistry();
271  mShortcutsManager = new QgsShortcutsManager();
272  mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
273  mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
274  mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
275  mWidgetStateHelper = new QgsWidgetStateHelper();
276  mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
277  mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
278 }
279 
280 bool QgsGui::pythonMacroAllowed( void ( *lambda )(), QgsMessageBar *messageBar )
281 {
282  Qgis::PythonMacroMode macroMode = QgsSettings().enumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::Ask );
283 
284  switch ( macroMode )
285  {
288  if ( lambda )
289  lambda();
290  return true;
293  if ( messageBar )
294  {
295  messageBar->pushMessage( tr( "Python Macros" ),
296  tr( "Python macros are currently disabled and will not be run" ),
297  Qgis::MessageLevel::Warning );
298  }
299  return false;
301  if ( !lambda )
302  {
303  QMessageBox msgBox( QMessageBox::Information, tr( "Python Macros" ),
304  tr( "Python macros are currently disabled. Do you allow this macro to run?" ) );
305  QAbstractButton *stopSessionButton = msgBox.addButton( tr( "Don't Ask Anymore" ), QMessageBox::DestructiveRole );
306  msgBox.addButton( tr( "No" ), QMessageBox::NoRole );
307  QAbstractButton *yesButton = msgBox.addButton( tr( "Yes" ), QMessageBox::YesRole );
308  msgBox.exec();
309 
310  QAbstractButton *clicked = msgBox.clickedButton();
311  if ( clicked == stopSessionButton )
312  {
313  QgsSettings().setEnumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::NotForThisSession );
314  }
315  return clicked == yesButton;
316  }
317  else
318  {
319  // create the notification widget for macros
320  Q_ASSERT( messageBar );
321  if ( messageBar )
322  {
323  QToolButton *btnEnableMacros = new QToolButton();
324  btnEnableMacros->setText( tr( "Enable Macros" ) );
325  btnEnableMacros->setStyleSheet( QStringLiteral( "background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
326  btnEnableMacros->setCursor( Qt::PointingHandCursor );
327  btnEnableMacros->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
328 
329  QgsMessageBarItem *macroMsg = new QgsMessageBarItem(
330  tr( "Security warning" ),
331  tr( "Python macros cannot currently be run." ),
332  btnEnableMacros,
333  Qgis::MessageLevel::Warning,
334  0,
335  messageBar );
336 
337  connect( btnEnableMacros, &QToolButton::clicked, messageBar, [ = ]()
338  {
339  lambda();
340  messageBar->popWidget( macroMsg );
341  } );
342 
343  // display the macros notification widget
344  messageBar->pushItem( macroMsg );
345  }
346 
347  return false;
348  }
349  }
350  return false;
351 }
352 
354 void QgsGui::emitOptionsChanged()
355 {
356  emit optionsChanged();
357 }
PythonMacroMode
Authorisation to run Python Macros.
Definition: qgis.h:142
@ 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.
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: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:280
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:221
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:209
~QgsGui()
Definition: qgsgui.cpp:187
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