Skip to content

Commit a6eb7b6

Browse files
committed
Convert action ids to uuid
1 parent 08d350c commit a6eb7b6

16 files changed

+201
-133
lines changed

python/core/qgsaction.sip

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ class QgsAction
3333
OpenUrl,
3434
};
3535

36+
/**
37+
* Default constructor
38+
*/
39+
QgsAction();
40+
41+
/**
42+
* Create a new QgsAction
43+
*
44+
* @param type The type of this action
45+
* @param description A human readable description string
46+
* @param command The action text. Its interpretation depends on the type
47+
* @param capture If this is set to true, the output will be captured when an action is run
48+
*/
49+
QgsAction( ActionType type, const QString& description, const QString& command, bool capture = false );
50+
51+
/**
52+
* Create a new QgsAction
53+
*
54+
* @param type The type of this action
55+
* @param description A human readable description string
56+
* @param action The action text. Its interpretation depends on the type
57+
* @param icon Path to an icon for this action
58+
* @param capture If this is set to true, the output will be captured when an action is run
59+
* @param shortTitle A short string used to label user interface elements like buttons
60+
* @param actionScopes A set of scopes in which this action will be available
61+
*/
3662
QgsAction( ActionType type, const QString& description, const QString& action, const QString& icon, bool capture, const QString& shortTitle = QString(), const QSet<QString>& actionScopes = QSet<QString>() );
3763

3864
//! The name of the action. This may be a longer description.
@@ -46,14 +72,14 @@ class QgsAction
4672
*
4773
* @note Added in QGIS 3.0
4874
*/
49-
QString id() const { return mShortTitle; }
75+
QUuid id() const;
5076

5177
/**
5278
* Returns true if this action was a default constructed one.
5379
*
5480
* @note Added in QGIS 3.0
5581
*/
56-
bool isValid() const { return !mShortTitle.isNull(); }
82+
bool isValid() const;
5783

5884
//! The path to the icon
5985
QString iconPath() const;
@@ -105,4 +131,20 @@ class QgsAction
105131
* @note Added in QGIS 3.0
106132
*/
107133
void setActionScopes( const QSet<QString>& actionScopes );
134+
135+
/**
136+
* Reads an XML definition from actionNode
137+
* into this object.
138+
*
139+
* @note Added in QGIS 3.0
140+
*/
141+
void readXml( const QDomNode& actionNode );
142+
143+
/**
144+
* Appends an XML definition for this action as a new
145+
* child node to actionsNode.
146+
*
147+
* @note Added in QGIS 3.0
148+
*/
149+
void writeXml(QDomNode& actionsNode ) const;
108150
};

python/core/qgsactionmanager.sip

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class QgsActionManager
4444
* any stdout from the process will be captured and displayed in a
4545
* dialog box.
4646
*/
47-
void addAction( QgsAction::ActionType type, const QString& name, const QString& action, bool capture = false );
47+
QUuid addAction(QgsAction::ActionType type, const QString& name, const QString& command, bool capture = false );
4848

4949
/** Add an action with the given name and action details.
5050
* Will happily have duplicate names and actions. If
5151
* capture is true, when running the action using doAction(),
5252
* any stdout from the process will be captured and displayed in a
5353
* dialog box.
5454
*/
55-
void addAction( QgsAction::ActionType type, const QString& name, const QString& action, const QString& icon, bool capture = false );
55+
QUuid addAction(QgsAction::ActionType type, const QString& name, const QString& command, const QString& icon, bool capture = false );
5656

5757
/**
5858
* Add a new action to this list.
@@ -63,31 +63,30 @@ class QgsActionManager
6363
* field to be used if the action has a $currfield placeholder.
6464
* @note available in python bindings as doActionFeature
6565
*/
66-
void doAction( const QString& actionId,
67-
const QgsFeature &feat,
68-
int defaultValueIndex = 0 ) /PyName=doActionFeature/;
66+
void doAction(const QUuid& actionId, const QgsFeature& feature, int defaultValueIndex = 0 ) /PyName=doActionFeature/;
6967

7068
/** Does the action using the expression engine to replace any embedded expressions
7169
* in the action definition.
7270
* @param actionId action id
7371
* @param feature feature to run action for
7472
* @param context expression context to evalute expressions under
7573
*/
76-
void doAction( const QString& actionId,
77-
const QgsFeature& feature,
78-
const QgsExpressionContext& context );
74+
void doAction( const QUuid& actionId, const QgsFeature& feature, const QgsExpressionContext& context );
7975

8076
//! Removes all actions
8177
void clearActions();
8278

83-
//! List all actions
79+
/**
80+
* Return a list of actions that are available in the given action scope.
81+
* If no action scope is provided, all actions will be returned.
82+
*/
8483
QList<QgsAction> listActions( const QString& actionScope = QString() ) const;
8584

8685
//! Return the layer
8786
QgsVectorLayer* layer() const;
8887

8988
//! Writes the actions out in XML format
90-
bool writeXml( QDomNode& layer_node, QDomDocument& doc ) const;
89+
bool writeXml( QDomNode& layer_node ) const;
9190

9291
//! Reads the actions in in XML format
9392
bool readXml( const QDomNode& layer_node );
@@ -97,15 +96,15 @@ class QgsActionManager
9796
*
9897
* @note Added in QGIS 3.0
9998
*/
100-
QgsAction action( const QString& id );
99+
QgsAction action( const QUuid& id );
101100

102101
/**
103102
* Each scope can have a default action. This will be saved in the project
104103
* file.
105104
*
106105
* @note Added in QGIS 3.0
107106
*/
108-
void setDefaultAction( const QString& actionScope, const QString& actionId );
107+
void setDefaultAction( const QString& actionScope, const QUuid& actionId );
109108

110109
/**
111110
* Each scope can have a default action. This will be saved in the project

python/core/qgsapplication.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
381381
/**
382382
* Returns the action scope registry.
383383
*
384-
* @Note Added in QGIS 3.0
384+
* @note Added in QGIS 3.0
385385
*/
386386
static QgsActionScopeRegistry* actionScopeRegistry();
387387

python/gui/attributetable/qgsdualview.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class QgsAttributeTableAction : QAction
229229
%End
230230

231231
public:
232-
QgsAttributeTableAction( const QString &name, QgsDualView *dualView, const QString& action, const QModelIndex &fieldIdx );
232+
QgsAttributeTableAction( const QString& name, QgsDualView* dualView, const QUuid& action, const QModelIndex& fieldIdx );
233233

234234
public slots:
235235
void execute();

src/app/qgsfeatureaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <QPushButton>
3333
#include <QSettings>
3434

35-
QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *layer, const QString& actionId, int defaultAttr, QObject *parent )
35+
QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *layer, const QUuid& actionId, int defaultAttr, QObject *parent )
3636
: QAction( name, parent )
3737
, mLayer( layer )
3838
, mFeature( &f )

src/app/qgsfeatureaction.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QList>
2323
#include <QPair>
2424
#include <QAction>
25+
#include <QUuid>
2526

2627
class QgsIdentifyResultsDialog;
2728
class QgsVectorLayer;
@@ -33,7 +34,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
3334
Q_OBJECT
3435

3536
public:
36-
QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *vl, const QString& actionId = QString(), int defaultAttr = -1, QObject *parent = nullptr );
37+
QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *vl, const QUuid& actionId = QString(), int defaultAttr = -1, QObject *parent = nullptr );
3738

3839
public slots:
3940
void execute();
@@ -59,7 +60,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
5960

6061
QgsVectorLayer* mLayer;
6162
QgsFeature* mFeature;
62-
QString mActionId;
63+
QUuid mActionId;
6364
int mIdx;
6465

6566
bool mFeatureSaved;

src/core/qgsaction.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,56 @@ void QgsAction::setActionScopes( const QSet<QString>& actionScopes )
9191
{
9292
mActionScopes = actionScopes;
9393
}
94+
95+
void QgsAction::readXml( const QDomNode& actionNode )
96+
{
97+
QDomElement actionElement = actionNode.toElement();
98+
QDomNodeList actionScopeNodes = actionElement.elementsByTagName( "actionScope" );
99+
100+
if ( actionScopeNodes.isEmpty() )
101+
{
102+
mActionScopes
103+
<< QStringLiteral( "Canvas" )
104+
<< QStringLiteral( "Field" )
105+
<< QStringLiteral( "Feature" );
106+
}
107+
else
108+
{
109+
for ( int j = 0; j < actionScopeNodes.length(); ++j )
110+
{
111+
QDomElement actionScopeElem = actionScopeNodes.item( j ).toElement();
112+
mActionScopes << actionScopeElem.attribute( "id" );
113+
}
114+
}
115+
116+
mType = static_cast< QgsAction::ActionType >( actionElement.attributeNode( QStringLiteral( "type" ) ).value().toInt() );
117+
mDescription = actionElement.attributeNode( QStringLiteral( "name" ) ).value();
118+
mCommand = actionElement.attributeNode( QStringLiteral( "action" ) ).value();
119+
mIcon = actionElement.attributeNode( QStringLiteral( "icon" ) ).value();
120+
mCaptureOutput = actionElement.attributeNode( QStringLiteral( "capture" ) ).value().toInt() != 0;
121+
mShortTitle = actionElement.attributeNode( QStringLiteral( "shortTitle" ) ).value();
122+
mId = QUuid( actionElement.attributeNode( QStringLiteral( "id" ) ).value() );
123+
if ( mId.isNull() )
124+
mId = QUuid::createUuid();
125+
}
126+
127+
void QgsAction::writeXml( QDomNode& actionsNode ) const
128+
{
129+
QDomElement actionSetting = actionsNode.ownerDocument().createElement( QStringLiteral( "actionsetting" ) );
130+
actionSetting.setAttribute( QStringLiteral( "type" ), mType );
131+
actionSetting.setAttribute( QStringLiteral( "name" ), mDescription );
132+
actionSetting.setAttribute( QStringLiteral( "shortTitle" ), mShortTitle );
133+
actionSetting.setAttribute( QStringLiteral( "icon" ), mIcon );
134+
actionSetting.setAttribute( QStringLiteral( "action" ), mCommand );
135+
actionSetting.setAttribute( QStringLiteral( "capture" ), mCaptureOutput );
136+
actionSetting.setAttribute( QStringLiteral( "id" ), mId.toString() );
137+
138+
Q_FOREACH ( const QString& scope, mActionScopes )
139+
{
140+
QDomElement actionScopeElem = actionsNode.ownerDocument().createElement( "actionScope" );
141+
actionScopeElem.setAttribute( "id", scope );
142+
actionSetting.appendChild( actionScopeElem );
143+
}
144+
145+
actionsNode.appendChild( actionSetting );
146+
}

src/core/qgsaction.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QString>
2121
#include <QIcon>
2222
#include <QAction>
23+
#include <QUuid>
2324

2425
#include "qgsexpressioncontext.h"
2526

@@ -53,14 +54,15 @@ class CORE_EXPORT QgsAction
5354
*
5455
* @param type The type of this action
5556
* @param description A human readable description string
56-
* @param action The action text. Its interpretation depends on the type
57+
* @param command The action text. Its interpretation depends on the type
5758
* @param capture If this is set to true, the output will be captured when an action is run
5859
*/
59-
QgsAction( ActionType type, const QString& description, const QString& action, bool capture )
60+
QgsAction( ActionType type, const QString& description, const QString& command, bool capture = false )
6061
: mType( type )
6162
, mDescription( description )
62-
, mCommand( action )
63+
, mCommand( command )
6364
, mCaptureOutput( capture )
65+
, mId( QUuid::createUuid() )
6466
{}
6567

6668
/**
@@ -82,6 +84,7 @@ class CORE_EXPORT QgsAction
8284
, mCommand( action )
8385
, mCaptureOutput( capture )
8486
, mActionScopes( actionScopes )
87+
, mId( QUuid::createUuid() )
8588
{}
8689

8790
//! The name of the action. This may be a longer description.
@@ -95,14 +98,14 @@ class CORE_EXPORT QgsAction
9598
*
9699
* @note Added in QGIS 3.0
97100
*/
98-
QString id() const { return mShortTitle; }
101+
QUuid id() const { return mId; }
99102

100103
/**
101104
* Returns true if this action was a default constructed one.
102105
*
103106
* @note Added in QGIS 3.0
104107
*/
105-
bool isValid() const { return !mShortTitle.isNull(); }
108+
bool isValid() const { return !mId.isNull(); }
106109

107110
//! The path to the icon
108111
QString iconPath() const { return mIcon; }
@@ -161,6 +164,22 @@ class CORE_EXPORT QgsAction
161164
*/
162165
void setActionScopes( const QSet<QString>& actionScopes );
163166

167+
/**
168+
* Reads an XML definition from actionNode
169+
* into this object.
170+
*
171+
* @note Added in QGIS 3.0
172+
*/
173+
void readXml( const QDomNode& actionNode );
174+
175+
/**
176+
* Appends an XML definition for this action as a new
177+
* child node to actionsNode.
178+
*
179+
* @note Added in QGIS 3.0
180+
*/
181+
void writeXml( QDomNode& actionsNode ) const;
182+
164183
private:
165184
ActionType mType;
166185
QString mDescription;
@@ -170,6 +189,7 @@ class CORE_EXPORT QgsAction
170189
bool mCaptureOutput;
171190
QSet<QString> mActionScopes;
172191
mutable QSharedPointer<QAction> mAction;
192+
QUuid mId;
173193
};
174194

175195
Q_DECLARE_METATYPE( QgsAction )

0 commit comments

Comments
 (0)