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