QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
208 QgsGui::QgsGui()
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 }
QgsDataItemGuiProviderRegistry
Definition: qgsdataitemguiproviderregistry.h:37
qgsdataitemguiproviderregistry.h
QgsGui::editorWidgetRegistry
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition: qgsgui.cpp:73
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:247
QgsSourceSelectProviderRegistry
Definition: qgssourceselectproviderregistry.h:41
qgsmessagebaritem.h
QgsGui::sampleColor
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition: qgsgui.cpp:183
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:176
QgsGui::setWindowManager
static void setWindowManager(QgsWindowManagerInterface *manager)
Sets the global window manager.
Definition: qgsgui.cpp:147
QgsGui::mapLayerActionRegistry
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:93
qgsgui.h
Qgis::Warning
@ Warning
Definition: qgis.h:104
QgsGui::providerGuiRegistry
static QgsProviderGuiRegistry * providerGuiRegistry()
Returns the registry of GUI-related components of data providers.
Definition: qgsgui.cpp:128
QgsWidgetStateHelper
Definition: qgswidgetstatehelper_p.h:32
QgsGui::windowManager
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
Definition: qgsgui.cpp:142
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:152
QgsSettings
Definition: qgssettings.h:61
QgsGui::HigDialogTitleIsTitleCase
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:177
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
qgsproviderguiregistry.h
QgsShortcutsManager
Definition: qgsshortcutsmanager.h:37
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:133
qgslayoutviewrubberband.h
qgswidgetstatehelper_p.h
QgsGui::findScreenAt
static QScreen * findScreenAt(QPoint point)
Returns the screen at the given global point (pixel).
Definition: qgsgui.cpp:195
qgsproviderregistry.h
QgsMapLayerActionRegistry
Definition: qgsmaplayeractionregistry.h:145
QgsApplication::iconsPath
static QString iconsPath()
Returns the path to the icons image directory.
Definition: qgsapplication.cpp:993
QgsMessageBarItem
Definition: qgsmessagebaritem.h:38
QgsGui::~QgsGui
~QgsGui()
Definition: qgsgui.cpp:165
QgsGui::projectStorageGuiRegistry
static QgsProjectStorageGuiRegistry * projectStorageGuiRegistry()
Returns the global GUI-related project storage registry.
Definition: qgsgui.cpp:123
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
Definition: qgslayertreeembeddedwidgetregistry.h:70
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:103
QgsMessageBar
Definition: qgsmessagebar.h:60
QgsProviderGuiRegistry
Definition: qgsproviderguiregistry.h:49
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:88
QgsGui::nativePlatformInterface
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition: qgsgui.cpp:68
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:118
qgsprojectstorageguiregistry.h
qgseditorwidgetregistry.h
QgsGui::instance
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:62
QgsProcessingGuiRegistry
The QgsProcessingGuiRegistry is a home for widgets for processing configuration widgets.
Definition: qgsprocessingguiregistry.h:44
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:83
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
QgsNumericFormatGuiRegistry
The QgsNumericFormatGuiRegistry is a home for widgets for configuring QgsNumericFormat objects.
Definition: qgsnumericformatguiregistry.h:55
qgslayoutitemregistry.h
QgsProjectStorageGuiRegistry
Definition: qgsprojectstorageguiregistry.h:44
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:98
QgsEditorWidgetRegistry
Definition: qgseditorwidgetregistry.h:44
QgsGui::numericFormatGuiRegistry
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition: qgsgui.cpp:108
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:136
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:54
QgsApplication::pluginPath
static QString pluginPath()
Returns the path to the application plugin directory.
Definition: qgsapplication.cpp:552
QgsGui::sourceSelectProviderRegistry
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:78
QgsGui::processingRecentAlgorithmLog
static QgsProcessingRecentAlgorithmLog * processingRecentAlgorithmLog()
Returns the global processing recent algorithm log, used for tracking recently used processing algori...
Definition: qgsgui.cpp:113
QgsGui
Definition: qgsgui.h:49
QgsLayoutItemGuiRegistry
Registry of available layout item GUI behavior.
Definition: qgslayoutitemguiregistry.h:331