QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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"
60 
61 
63 {
64  static QgsGui *sInstance( new QgsGui() );
65  return sInstance;
66 }
67 
69 {
70  return instance()->mNative;
71 }
72 
74 {
75  return instance()->mEditorWidgetRegistry;
76 }
77 
79 {
80  return instance()->mSourceSelectProviderRegistry;
81 }
82 
84 {
85  return instance()->mShortcutsManager;
86 }
87 
89 {
90  return instance()->mLayerTreeEmbeddedWidgetRegistry;
91 }
92 
94 {
95  return instance()->mMapLayerActionRegistry;
96 }
97 
99 {
100  return instance()->mLayoutItemGuiRegistry;
101 }
102 
104 {
105  return instance()->mProcessingGuiRegistry;
106 }
107 
109 {
110  return instance()->mNumericFormatGuiRegistry;
111 }
112 
113 QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
114 {
115  return instance()->mProcessingRecentAlgorithmLog;
116 }
117 
119 {
120  return instance()->mDataItemGuiProviderRegistry;
121 }
122 
124 {
125  return instance()->mProjectStorageGuiRegistry;
126 }
127 
129 {
130  return instance()->mProviderGuiRegistry;
131 }
132 
133 void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
134 {
135  if ( widget->objectName().isEmpty() )
136  {
137  QgsDebugMsg( QStringLiteral( "WARNING: No object name set. Best for it to be set objectName when using QgsGui::enableAutoGeometryRestore" ) );
138  }
139  instance()->mWidgetStateHelper->registerWidget( widget, key );
140 }
141 
142 QgsWindowManagerInterface *QgsGui::windowManager()
143 {
144  return instance()->mWindowManager.get();
145 }
146 
147 void QgsGui::setWindowManager( QgsWindowManagerInterface *manager )
148 {
149  instance()->mWindowManager.reset( manager );
150 }
151 
152 QgsGui::HigFlags QgsGui::higFlags()
153 {
154  QgsSettings settings;
155  if ( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString().startsWith( QLatin1String( "en" ) ) )
156  {
158  }
159  else
160  {
161  return nullptr;
162  }
163 }
164 
166 {
167  delete mProcessingGuiRegistry;
168  delete mDataItemGuiProviderRegistry;
169  delete mProcessingRecentAlgorithmLog;
170  delete mLayoutItemGuiRegistry;
171  delete mLayerTreeEmbeddedWidgetRegistry;
172  delete mEditorWidgetRegistry;
173  delete mMapLayerActionRegistry;
174  delete mSourceSelectProviderRegistry;
175  delete mShortcutsManager;
176  delete mNative;
177  delete mNumericFormatGuiRegistry;
178  delete mWidgetStateHelper;
179  delete mProjectStorageGuiRegistry;
180  delete mProviderGuiRegistry;
181 }
182 
183 QColor QgsGui::sampleColor( QPoint point )
184 {
185  QScreen *screen = findScreenAt( point );
186  if ( ! screen )
187  {
188  return QColor();
189  }
190  QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
191  QImage snappedImage = snappedPixmap.toImage();
192  return snappedImage.pixel( 0, 0 );
193 }
194 
195 QScreen *QgsGui::findScreenAt( QPoint point )
196 {
197  const QList< QScreen * > screens = QGuiApplication::screens();
198  for ( QScreen *screen : screens )
199  {
200  if ( screen->geometry().contains( point ) )
201  {
202  return screen;
203  }
204  }
205  return nullptr;
206 }
207 
209 {
210 #ifdef Q_OS_MAC
211  QgsMacNative *macNative = new QgsMacNative();
212  macNative->setIconPath( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) );
213  mNative = macNative;
214 #elif defined (Q_OS_WIN)
215 #ifndef __MINGW32__
216  mNative = new QgsWinNative();
217 #else
218  mNative = new QgsNative();
219 #endif
220 #elif defined(Q_OS_LINUX)
221  mNative = new QgsLinuxNative();
222 #else
223  mNative = new QgsNative();
224 #endif
225 
226  // provider gui registry initialize QgsProviderRegistry too
227  mProviderGuiRegistry = new QgsProviderGuiRegistry( QgsApplication::pluginPath() );
228  mProjectStorageGuiRegistry = new QgsProjectStorageGuiRegistry();
229  mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
230  mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
231  mNumericFormatGuiRegistry = new QgsNumericFormatGuiRegistry();
232 
233  mProjectStorageGuiRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
234  mDataItemGuiProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
235  mSourceSelectProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
236 
237  mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
238  mShortcutsManager = new QgsShortcutsManager();
239  mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
240  mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
241  mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
242  mWidgetStateHelper = new QgsWidgetStateHelper();
243  mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
244  mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
245 }
246 
247 bool QgsGui::pythonMacroAllowed( void ( *lambda )(), QgsMessageBar *messageBar )
248 {
249  Qgis::PythonMacroMode macroMode = QgsSettings().enumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::Ask );
250 
251  switch ( macroMode )
252  {
253  case Qgis::PythonMacroMode::SessionOnly:
254  case Qgis::PythonMacroMode::Always:
255  if ( lambda )
256  lambda();
257  return true;
258  case Qgis::PythonMacroMode::Never:
259  case Qgis::PythonMacroMode::NotForThisSession:
260  if ( messageBar )
261  {
262  messageBar->pushMessage( tr( "Python Macros" ),
263  tr( "Python macros are currently disabled and will not be run" ),
264  Qgis::MessageLevel::Warning );
265  }
266  return false;
267  case Qgis::PythonMacroMode::Ask:
268  if ( !lambda )
269  {
270  QMessageBox msgBox( QMessageBox::Information, tr( "Python Macros" ),
271  tr( "Python macros are currently disabled. Do you allow this macro to run?" ) );
272  QAbstractButton *stopSessionButton = msgBox.addButton( tr( "Don't Ask Anymore" ), QMessageBox::DestructiveRole );
273  msgBox.addButton( tr( "No" ), QMessageBox::NoRole );
274  QAbstractButton *yesButton = msgBox.addButton( tr( "Yes" ), QMessageBox::YesRole );
275  msgBox.exec();
276 
277  QAbstractButton *clicked = msgBox.clickedButton();
278  if ( clicked == stopSessionButton )
279  {
280  QgsSettings().setEnumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::NotForThisSession );
281  }
282  return clicked == yesButton;
283  }
284  else
285  {
286  // create the notification widget for macros
287  Q_ASSERT( messageBar );
288  if ( messageBar )
289  {
290  QToolButton *btnEnableMacros = new QToolButton();
291  btnEnableMacros->setText( tr( "Enable Macros" ) );
292  btnEnableMacros->setStyleSheet( QStringLiteral( "background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
293  btnEnableMacros->setCursor( Qt::PointingHandCursor );
294  btnEnableMacros->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
295 
296  QgsMessageBarItem *macroMsg = new QgsMessageBarItem(
297  tr( "Security warning" ),
298  tr( "Python macros cannot currently be run." ),
299  btnEnableMacros,
301  0,
302  messageBar );
303 
304  connect( btnEnableMacros, &QToolButton::clicked, messageBar, [ = ]()
305  {
306  lambda();
307  messageBar->popWidget( macroMsg );
308  } );
309 
310  // display the macros notification widget
311  messageBar->pushItem( macroMsg );
312  }
313 
314  return false;
315  }
316  }
317  return false;
318 }
static QgsProviderGuiRegistry * providerGuiRegistry()
Returns the registry of GUI-related components of data providers.
Definition: qgsgui.cpp:128
The QgsNumericFormatGuiRegistry is a home for widgets for configuring QgsNumericFormat objects...
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:103
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
Definition: qgsgui.cpp:142
A registry / canonical manager of GUI parts of project storage backends.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
Dialog titles should be title case.
Definition: qgsgui.h:177
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS&#39;s underlying public inte...
Definition: qgsgui.cpp:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
This class manages all known edit widget factories.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsGui is a singleton class containing various registry and other global members related to GUI class...
Definition: qgsgui.h:49
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:118
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:78
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
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:247
Shortcuts manager is a class that contains a list of QActions and QShortcuts that have been registere...
The QgsProcessingGuiRegistry is a home for widgets for processing configuration widgets.
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
~QgsGui()
Definition: qgsgui.cpp:165
Registry of available layout item GUI behavior.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:62
static QgsProjectStorageGuiRegistry * projectStorageGuiRegistry()
Returns the global GUI-related project storage registry.
Definition: qgsgui.cpp:123
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
QgsGui(const QgsGui &other)=delete
QgsGui cannot be copied.
static QgsShortcutsManager * shortcutsManager()
Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
Definition: qgsgui.cpp:83
void initializeFromProviderGuiRegistry(QgsProviderGuiRegistry *providerGuiRegistry)
Initializes the registry.
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items...
Definition: qgsgui.cpp:98
static QString pluginPath()
Returns the path to the application plugin directory.
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition: qgsgui.cpp:108
bool popWidget(QgsMessageBarItem *item)
Remove the passed widget from the bar (if previously added), then display the next one in the stack i...
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:88
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories...
Definition: qgsgui.cpp:73
static QgsProcessingRecentAlgorithmLog * processingRecentAlgorithmLog()
Returns the global processing recent algorithm log, used for tracking recently used processing algori...
Definition: qgsgui.cpp:113
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:88
Registry of widgets that may be embedded into layer tree view.
This class tracks map layer actions.
This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDial...
This class keeps a list of data item GUI providers that may affect how QgsDataItems behave within the...
Menu action texts should be title case.
Definition: qgsgui.h:176
static void setWindowManager(QgsWindowManagerInterface *manager)
Sets the global window manager.
Definition: qgsgui.cpp:147
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:297
QgsWidgetStateHelper is a helper class to save and restore the geometry of QWidgets in the applicatio...
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar after hiding the currently visible one and putting it in a stack...
static QgsGui::HigFlags higFlags()
Returns the platform&#39;s HIG flags.
Definition: qgsgui.cpp:152
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:133
PythonMacroMode
Authorisation to run Python Macros.
Definition: qgis.h:123
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:245
static QScreen * findScreenAt(QPoint point)
Returns the screen at the given global point (pixel).
Definition: qgsgui.cpp:195
static QString iconsPath()
Returns the path to the icons image directory.
void registerWidget(QWidget *widget, const QString &key=QString())
Register a widget to have it geometry state automatically saved and restored.
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:93
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition: qgsgui.cpp:183
A registry / canonical manager of GUI parts of data providers.