QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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"
59 
60 
62 {
63  static QgsGui *sInstance( new QgsGui() );
64  return sInstance;
65 }
66 
68 {
69  return instance()->mNative;
70 }
71 
73 {
74  return instance()->mEditorWidgetRegistry;
75 }
76 
78 {
79  return instance()->mSourceSelectProviderRegistry;
80 }
81 
83 {
84  return instance()->mShortcutsManager;
85 }
86 
88 {
89  return instance()->mLayerTreeEmbeddedWidgetRegistry;
90 }
91 
93 {
94  return instance()->mMapLayerActionRegistry;
95 }
96 
98 {
99  return instance()->mLayoutItemGuiRegistry;
100 }
101 
103 {
104  return instance()->mProcessingGuiRegistry;
105 }
106 
107 QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
108 {
109  return instance()->mProcessingRecentAlgorithmLog;
110 }
111 
113 {
114  return instance()->mDataItemGuiProviderRegistry;
115 }
116 
118 {
119  return instance()->mProjectStorageGuiRegistry;
120 }
121 
123 {
124  return instance()->mProviderGuiRegistry;
125 }
126 
127 void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
128 {
129  if ( widget->objectName().isEmpty() )
130  {
131  QgsDebugMsg( QStringLiteral( "WARNING: No object name set. Best for it to be set objectName when using QgsGui::enableAutoGeometryRestore" ) );
132  }
133  instance()->mWidgetStateHelper->registerWidget( widget, key );
134 }
135 
136 QgsWindowManagerInterface *QgsGui::windowManager()
137 {
138  return instance()->mWindowManager.get();
139 }
140 
141 void QgsGui::setWindowManager( QgsWindowManagerInterface *manager )
142 {
143  instance()->mWindowManager.reset( manager );
144 }
145 
146 QgsGui::HigFlags QgsGui::higFlags()
147 {
148  QgsSettings settings;
149  if ( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString().startsWith( QLatin1String( "en" ) ) )
150  {
152  }
153  else
154  {
155  return nullptr;
156  }
157 }
158 
160 {
161  delete mProcessingGuiRegistry;
162  delete mDataItemGuiProviderRegistry;
163  delete mProcessingRecentAlgorithmLog;
164  delete mLayoutItemGuiRegistry;
165  delete mLayerTreeEmbeddedWidgetRegistry;
166  delete mEditorWidgetRegistry;
167  delete mMapLayerActionRegistry;
168  delete mSourceSelectProviderRegistry;
169  delete mShortcutsManager;
170  delete mNative;
171  delete mWidgetStateHelper;
172  delete mProjectStorageGuiRegistry;
173  delete mProviderGuiRegistry;
174 }
175 
176 QColor QgsGui::sampleColor( QPoint point )
177 {
178  QScreen *screen = findScreenAt( point );
179  if ( ! screen )
180  {
181  return QColor();
182  }
183  QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
184  QImage snappedImage = snappedPixmap.toImage();
185  return snappedImage.pixel( 0, 0 );
186 }
187 
188 QScreen *QgsGui::findScreenAt( QPoint point )
189 {
190  const QList< QScreen * > screens = QGuiApplication::screens();
191  for ( QScreen *screen : screens )
192  {
193  if ( screen->geometry().contains( point ) )
194  {
195  return screen;
196  }
197  }
198  return nullptr;
199 }
200 
202 {
203 #ifdef Q_OS_MAC
204  QgsMacNative *macNative = new QgsMacNative();
205  macNative->setIconPath( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) );
206  mNative = macNative;
207 #elif defined (Q_OS_WIN)
208 #ifndef __MINGW32__
209  mNative = new QgsWinNative();
210 #else
211  mNative = new QgsNative();
212 #endif
213 #elif defined(Q_OS_LINUX)
214  mNative = new QgsLinuxNative();
215 #else
216  mNative = new QgsNative();
217 #endif
218 
219  // provider gui registry initialize QgsProviderRegistry too
220  mProviderGuiRegistry = new QgsProviderGuiRegistry( QgsApplication::pluginPath() );
221  mProjectStorageGuiRegistry = new QgsProjectStorageGuiRegistry();
222  mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
223  mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
224 
225  mProjectStorageGuiRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
226  mDataItemGuiProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
227  mSourceSelectProviderRegistry->initializeFromProviderGuiRegistry( mProviderGuiRegistry );
228 
229  mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
230  mShortcutsManager = new QgsShortcutsManager();
231  mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
232  mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
233  mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
234  mWidgetStateHelper = new QgsWidgetStateHelper();
235  mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
236  mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
237 }
238 
239 bool QgsGui::pythonMacroAllowed( void ( *lambda )(), QgsMessageBar *messageBar )
240 {
241  Qgis::PythonMacroMode macroMode = QgsSettings().enumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::Ask );
242 
243  switch ( macroMode )
244  {
245  case Qgis::PythonMacroMode::SessionOnly:
246  case Qgis::PythonMacroMode::Always:
247  if ( lambda )
248  lambda();
249  return true;
250  case Qgis::PythonMacroMode::Never:
251  case Qgis::PythonMacroMode::NotForThisSession:
252  if ( messageBar )
253  {
254  messageBar->pushMessage( tr( "Python Macros" ),
255  tr( "Python macros are currently disabled and will not be run" ),
256  Qgis::MessageLevel::Warning );
257  }
258  return false;
259  case Qgis::PythonMacroMode::Ask:
260  if ( !lambda )
261  {
262  QMessageBox msgBox( QMessageBox::Information, tr( "Python Macros" ),
263  tr( "Python macros are currently disabled. Do you allow this macro to run?" ) );
264  QAbstractButton *stopSessionButton = msgBox.addButton( tr( "Don't Ask Anymore" ), QMessageBox::DestructiveRole );
265  msgBox.addButton( tr( "No" ), QMessageBox::NoRole );
266  QAbstractButton *yesButton = msgBox.addButton( tr( "Yes" ), QMessageBox::YesRole );
267  msgBox.exec();
268 
269  QAbstractButton *clicked = msgBox.clickedButton();
270  if ( clicked == stopSessionButton )
271  {
272  QgsSettings().setEnumValue( QStringLiteral( "qgis/enableMacros" ), Qgis::PythonMacroMode::NotForThisSession );
273  }
274  return clicked == yesButton;
275  }
276  else
277  {
278  // create the notification widget for macros
279  Q_ASSERT( messageBar );
280  if ( messageBar )
281  {
282  QToolButton *btnEnableMacros = new QToolButton();
283  btnEnableMacros->setText( tr( "Enable Macros" ) );
284  btnEnableMacros->setStyleSheet( QStringLiteral( "background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
285  btnEnableMacros->setCursor( Qt::PointingHandCursor );
286  btnEnableMacros->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
287 
288  QgsMessageBarItem *macroMsg = new QgsMessageBarItem(
289  tr( "Security warning" ),
290  tr( "Python macros cannot currently be run." ),
291  btnEnableMacros,
293  0,
294  messageBar );
295 
296  connect( btnEnableMacros, &QToolButton::clicked, messageBar, [ = ]()
297  {
298  lambda();
299  messageBar->popWidget( macroMsg );
300  } );
301 
302  // display the macros notification widget
303  messageBar->pushItem( macroMsg );
304  }
305 
306  return false;
307  }
308  }
309  return false;
310 }
static QgsProviderGuiRegistry * providerGuiRegistry()
Returns the registry of GUI-related components of data providers.
Definition: qgsgui.cpp:122
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:102
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
Definition: qgsgui.cpp:136
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:170
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS&#39;s underlying public inte...
Definition: qgsgui.cpp:67
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:48
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:112
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:77
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:239
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:159
Registry of available layout item GUI behavior.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:61
static QgsProjectStorageGuiRegistry * projectStorageGuiRegistry()
Returns the global GUI-related project storage registry.
Definition: qgsgui.cpp:117
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:82
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:97
static QString pluginPath()
Returns the path to the application plugin directory.
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:87
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories...
Definition: qgsgui.cpp:72
static QgsProcessingRecentAlgorithmLog * processingRecentAlgorithmLog()
Returns the global processing recent algorithm log, used for tracking recently used processing algori...
Definition: qgsgui.cpp:107
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:169
static void setWindowManager(QgsWindowManagerInterface *manager)
Sets the global window manager.
Definition: qgsgui.cpp:141
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:296
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:146
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:127
PythonMacroMode
Authorisation to run Python Macros.
Definition: qgis.h:102
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:244
static QScreen * findScreenAt(QPoint point)
Returns the screen at the given global point (pixel).
Definition: qgsgui.cpp:188
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:92
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition: qgsgui.cpp:176
A registry / canonical manager of GUI parts of data providers.