QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsaction.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaction.h - QgsAction
3 
4  ---------------------
5  begin : 18.4.2016
6  copyright : (C) 2016 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSACTION_H
17 #define QGSACTION_H
18 
19 #include "qgis_core.h"
20 #include <QSet>
21 #include <QString>
22 #include <QIcon>
23 #include <QAction>
24 #include <QUuid>
25 
26 #include "qgsexpressioncontext.h"
27 #include <memory>
28 
30 
35 class CORE_EXPORT QgsAction
36 {
37  public:
39  {
42  Mac,
46  };
47 
51  QgsAction() = default;
52 
62 #ifndef SIP_RUN
63  QgsAction( ActionType type, const QString &description, const QString &command, bool capture = false, bool enabledOnlyWhenEditable = false )
64  : mType( type )
65  , mDescription( description )
66  , mCommand( command )
67  , mCaptureOutput( capture )
68  , mId( QUuid::createUuid() )
69  , mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
70  {}
71 #else
72  QgsAction( ActionType type, const QString &description, const QString &command, bool capture = false )
73  : mType( type )
74  , mDescription( description )
75  , mCommand( command )
76  , mCaptureOutput( capture )
77  , mId( QUuid::createUuid() )
78  , mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
79  {}
80 #endif
81 
95 #ifndef SIP_RUN
96  QgsAction( ActionType type, const QString &description, const QString &action, const QString &icon, bool capture, const QString &shortTitle = QString(), const QSet<QString> &actionScopes = QSet<QString>(), const QString &notificationMessage = QString(), bool enabledOnlyWhenEditable = false )
97  : mType( type )
98  , mDescription( description )
99  , mShortTitle( shortTitle )
100  , mIcon( icon )
101  , mCommand( action )
102  , mCaptureOutput( capture )
103  , mActionScopes( actionScopes )
104  , mNotificationMessage( notificationMessage )
105  , mId( QUuid::createUuid() )
106  , mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
107  {}
108 #else
109  QgsAction( ActionType type, const QString &description, const QString &action, const QString &icon, bool capture, const QString &shortTitle = QString(), const QSet<QString> &actionScopes = QSet<QString>(), const QString &notificationMessage = QString() )
110  : mType( type )
111  , mDescription( description )
112  , mShortTitle( shortTitle )
113  , mIcon( icon )
114  , mCommand( action )
115  , mCaptureOutput( capture )
116  , mActionScopes( actionScopes )
117  , mNotificationMessage( notificationMessage )
118  , mId( QUuid::createUuid() )
119  , mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
120  {}
121 #endif
122 
124  QString name() const { return mDescription; }
125 
127  QString shortTitle() const { return mShortTitle; }
128 
134  QUuid id() const { return mId; }
135 
141  bool isValid() const { return !mId.isNull(); }
142 
144  QString iconPath() const { return mIcon; }
145 
147  QIcon icon() const { return QIcon( mIcon ); }
148 
156  QString command() const { return mCommand; }
157 
163  QString notificationMessage() const { return mNotificationMessage; }
164 
166  ActionType type() const { return mType; }
167 
169  bool capture() const { return mCaptureOutput; }
170 
171 
173  bool isEnabledOnlyWhenEditable() const { return mIsEnabledOnlyWhenEditable; }
174 
180  void setEnabledOnlyWhenEditable( bool enable ) { mIsEnabledOnlyWhenEditable = enable; };
181 
182 
184  bool runable() const;
185 
191  void run( QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext ) const;
192 
198  void run( const QgsExpressionContext &expressionContext ) const;
199 
208  QSet<QString> actionScopes() const;
209 
217  void setActionScopes( const QSet<QString> &actionScopes );
218 
225  void readXml( const QDomNode &actionNode );
226 
233  void writeXml( QDomNode &actionsNode ) const;
234 
240  void setExpressionContextScope( const QgsExpressionContextScope &scope );
241 
247  QgsExpressionContextScope expressionContextScope() const;
248 
249  private:
250  ActionType mType = Generic;
251  QString mDescription;
252  QString mShortTitle;
253  QString mIcon;
254  QString mCommand;
255  bool mCaptureOutput = false;
256  QSet<QString> mActionScopes;
257  QString mNotificationMessage;
258  mutable std::shared_ptr<QAction> mAction;
259  QUuid mId;
260  QgsExpressionContextScope mExpressionContextScope;
261  bool mIsEnabledOnlyWhenEditable = false;
262 };
263 
265 
266 #endif // QGSACTION_H
Utility class that encapsulates an action based on vector attributes.
Definition: qgsaction.h:36
QgsAction(ActionType type, const QString &description, const QString &command, bool capture=false, bool enabledOnlyWhenEditable=false)
Create a new QgsAction.
Definition: qgsaction.h:63
void setEnabledOnlyWhenEditable(bool enable)
Set whether the action is only enabled in editable mode.
Definition: qgsaction.h:180
QString notificationMessage() const
Returns the notification message that triggers the action.
Definition: qgsaction.h:163
QString name() const
The name of the action. This may be a longer description.
Definition: qgsaction.h:124
QgsAction()=default
Default constructor.
QIcon icon() const
The icon.
Definition: qgsaction.h:147
QString iconPath() const
The path to the icon.
Definition: qgsaction.h:144
bool isValid() const
Returns true if this action was a default constructed one.
Definition: qgsaction.h:141
ActionType type() const
The action type.
Definition: qgsaction.h:166
QString command() const
Returns the command that is executed by this action.
Definition: qgsaction.h:156
QString shortTitle() const
The short title is used to label user interface elements like buttons.
Definition: qgsaction.h:127
bool isEnabledOnlyWhenEditable() const
Returns whether only enabled in editable mode.
Definition: qgsaction.h:173
bool capture() const
Whether to capture output for display when this action is run.
Definition: qgsaction.h:169
QUuid id() const
Returns a unique id for this action.
Definition: qgsaction.h:134
QgsAction(ActionType type, const QString &description, const QString &action, const QString &icon, bool capture, const QString &shortTitle=QString(), const QSet< QString > &actionScopes=QSet< QString >(), const QString &notificationMessage=QString(), bool enabledOnlyWhenEditable=false)
Create a new QgsAction.
Definition: qgsaction.h:96
@ GenericPython
Definition: qgsaction.h:41
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsMeshTimeSettings)