Skip to content

Commit

Permalink
Convert action ids to uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 16, 2016
1 parent 08d350c commit a6eb7b6
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 133 deletions.
46 changes: 44 additions & 2 deletions python/core/qgsaction.sip
Expand Up @@ -33,6 +33,32 @@ class QgsAction
OpenUrl,
};

/**
* Default constructor
*/
QgsAction();

/**
* Create a new QgsAction
*
* @param type The type of this action
* @param description A human readable description string
* @param command The action text. Its interpretation depends on the type
* @param capture If this is set to true, the output will be captured when an action is run
*/
QgsAction( ActionType type, const QString& description, const QString& command, bool capture = false );

/**
* Create a new QgsAction
*
* @param type The type of this action
* @param description A human readable description string
* @param action The action text. Its interpretation depends on the type
* @param icon Path to an icon for this action
* @param capture If this is set to true, the output will be captured when an action is run
* @param shortTitle A short string used to label user interface elements like buttons
* @param actionScopes A set of scopes in which this action will be available
*/
QgsAction( ActionType type, const QString& description, const QString& action, const QString& icon, bool capture, const QString& shortTitle = QString(), const QSet<QString>& actionScopes = QSet<QString>() );

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

/**
* Returns true if this action was a default constructed one.
*
* @note Added in QGIS 3.0
*/
bool isValid() const { return !mShortTitle.isNull(); }
bool isValid() const;

//! The path to the icon
QString iconPath() const;
Expand Down Expand Up @@ -105,4 +131,20 @@ class QgsAction
* @note Added in QGIS 3.0
*/
void setActionScopes( const QSet<QString>& actionScopes );

/**
* Reads an XML definition from actionNode
* into this object.
*
* @note Added in QGIS 3.0
*/
void readXml( const QDomNode& actionNode );

/**
* Appends an XML definition for this action as a new
* child node to actionsNode.
*
* @note Added in QGIS 3.0
*/
void writeXml(QDomNode& actionsNode ) const;
};
23 changes: 11 additions & 12 deletions python/core/qgsactionmanager.sip
Expand Up @@ -44,15 +44,15 @@ class QgsActionManager
* any stdout from the process will be captured and displayed in a
* dialog box.
*/
void addAction( QgsAction::ActionType type, const QString& name, const QString& action, bool capture = false );
QUuid addAction(QgsAction::ActionType type, const QString& name, const QString& command, bool capture = false );

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

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

/** Does the action using the expression engine to replace any embedded expressions
* in the action definition.
* @param actionId action id
* @param feature feature to run action for
* @param context expression context to evalute expressions under
*/
void doAction( const QString& actionId,
const QgsFeature& feature,
const QgsExpressionContext& context );
void doAction( const QUuid& actionId, const QgsFeature& feature, const QgsExpressionContext& context );

//! Removes all actions
void clearActions();

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

//! Return the layer
QgsVectorLayer* layer() const;

//! Writes the actions out in XML format
bool writeXml( QDomNode& layer_node, QDomDocument& doc ) const;
bool writeXml( QDomNode& layer_node ) const;

//! Reads the actions in in XML format
bool readXml( const QDomNode& layer_node );
Expand All @@ -97,15 +96,15 @@ class QgsActionManager
*
* @note Added in QGIS 3.0
*/
QgsAction action( const QString& id );
QgsAction action( const QUuid& id );

/**
* Each scope can have a default action. This will be saved in the project
* file.
*
* @note Added in QGIS 3.0
*/
void setDefaultAction( const QString& actionScope, const QString& actionId );
void setDefaultAction( const QString& actionScope, const QUuid& actionId );

/**
* Each scope can have a default action. This will be saved in the project
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsapplication.sip
Expand Up @@ -381,7 +381,7 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
/**
* Returns the action scope registry.
*
* @Note Added in QGIS 3.0
* @note Added in QGIS 3.0
*/
static QgsActionScopeRegistry* actionScopeRegistry();

Expand Down
2 changes: 1 addition & 1 deletion python/gui/attributetable/qgsdualview.sip
Expand Up @@ -229,7 +229,7 @@ class QgsAttributeTableAction : QAction
%End

public:
QgsAttributeTableAction( const QString &name, QgsDualView *dualView, const QString& action, const QModelIndex &fieldIdx );
QgsAttributeTableAction( const QString& name, QgsDualView* dualView, const QUuid& action, const QModelIndex& fieldIdx );

public slots:
void execute();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfeatureaction.cpp
Expand Up @@ -32,7 +32,7 @@
#include <QPushButton>
#include <QSettings>

QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *layer, const QString& actionId, int defaultAttr, QObject *parent )
QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *layer, const QUuid& actionId, int defaultAttr, QObject *parent )
: QAction( name, parent )
, mLayer( layer )
, mFeature( &f )
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgsfeatureaction.h
Expand Up @@ -22,6 +22,7 @@
#include <QList>
#include <QPair>
#include <QAction>
#include <QUuid>

class QgsIdentifyResultsDialog;
class QgsVectorLayer;
Expand All @@ -33,7 +34,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
Q_OBJECT

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

public slots:
void execute();
Expand All @@ -59,7 +60,7 @@ class APP_EXPORT QgsFeatureAction : public QAction

QgsVectorLayer* mLayer;
QgsFeature* mFeature;
QString mActionId;
QUuid mActionId;
int mIdx;

bool mFeatureSaved;
Expand Down
53 changes: 53 additions & 0 deletions src/core/qgsaction.cpp
Expand Up @@ -91,3 +91,56 @@ void QgsAction::setActionScopes( const QSet<QString>& actionScopes )
{
mActionScopes = actionScopes;
}

void QgsAction::readXml( const QDomNode& actionNode )
{
QDomElement actionElement = actionNode.toElement();
QDomNodeList actionScopeNodes = actionElement.elementsByTagName( "actionScope" );

if ( actionScopeNodes.isEmpty() )
{
mActionScopes
<< QStringLiteral( "Canvas" )
<< QStringLiteral( "Field" )
<< QStringLiteral( "Feature" );
}
else
{
for ( int j = 0; j < actionScopeNodes.length(); ++j )
{
QDomElement actionScopeElem = actionScopeNodes.item( j ).toElement();
mActionScopes << actionScopeElem.attribute( "id" );
}
}

mType = static_cast< QgsAction::ActionType >( actionElement.attributeNode( QStringLiteral( "type" ) ).value().toInt() );
mDescription = actionElement.attributeNode( QStringLiteral( "name" ) ).value();
mCommand = actionElement.attributeNode( QStringLiteral( "action" ) ).value();
mIcon = actionElement.attributeNode( QStringLiteral( "icon" ) ).value();
mCaptureOutput = actionElement.attributeNode( QStringLiteral( "capture" ) ).value().toInt() != 0;
mShortTitle = actionElement.attributeNode( QStringLiteral( "shortTitle" ) ).value();
mId = QUuid( actionElement.attributeNode( QStringLiteral( "id" ) ).value() );
if ( mId.isNull() )
mId = QUuid::createUuid();
}

void QgsAction::writeXml( QDomNode& actionsNode ) const
{
QDomElement actionSetting = actionsNode.ownerDocument().createElement( QStringLiteral( "actionsetting" ) );
actionSetting.setAttribute( QStringLiteral( "type" ), mType );
actionSetting.setAttribute( QStringLiteral( "name" ), mDescription );
actionSetting.setAttribute( QStringLiteral( "shortTitle" ), mShortTitle );
actionSetting.setAttribute( QStringLiteral( "icon" ), mIcon );
actionSetting.setAttribute( QStringLiteral( "action" ), mCommand );
actionSetting.setAttribute( QStringLiteral( "capture" ), mCaptureOutput );
actionSetting.setAttribute( QStringLiteral( "id" ), mId.toString() );

Q_FOREACH ( const QString& scope, mActionScopes )
{
QDomElement actionScopeElem = actionsNode.ownerDocument().createElement( "actionScope" );
actionScopeElem.setAttribute( "id", scope );
actionSetting.appendChild( actionScopeElem );
}

actionsNode.appendChild( actionSetting );
}
30 changes: 25 additions & 5 deletions src/core/qgsaction.h
Expand Up @@ -20,6 +20,7 @@
#include <QString>
#include <QIcon>
#include <QAction>
#include <QUuid>

#include "qgsexpressioncontext.h"

Expand Down Expand Up @@ -53,14 +54,15 @@ class CORE_EXPORT QgsAction
*
* @param type The type of this action
* @param description A human readable description string
* @param action The action text. Its interpretation depends on the type
* @param command The action text. Its interpretation depends on the type
* @param capture If this is set to true, the output will be captured when an action is run
*/
QgsAction( ActionType type, const QString& description, const QString& action, bool capture )
QgsAction( ActionType type, const QString& description, const QString& command, bool capture = false )
: mType( type )
, mDescription( description )
, mCommand( action )
, mCommand( command )
, mCaptureOutput( capture )
, mId( QUuid::createUuid() )
{}

/**
Expand All @@ -82,6 +84,7 @@ class CORE_EXPORT QgsAction
, mCommand( action )
, mCaptureOutput( capture )
, mActionScopes( actionScopes )
, mId( QUuid::createUuid() )
{}

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

/**
* Returns true if this action was a default constructed one.
*
* @note Added in QGIS 3.0
*/
bool isValid() const { return !mShortTitle.isNull(); }
bool isValid() const { return !mId.isNull(); }

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

/**
* Reads an XML definition from actionNode
* into this object.
*
* @note Added in QGIS 3.0
*/
void readXml( const QDomNode& actionNode );

/**
* Appends an XML definition for this action as a new
* child node to actionsNode.
*
* @note Added in QGIS 3.0
*/
void writeXml( QDomNode& actionsNode ) const;

private:
ActionType mType;
QString mDescription;
Expand All @@ -170,6 +189,7 @@ class CORE_EXPORT QgsAction
bool mCaptureOutput;
QSet<QString> mActionScopes;
mutable QSharedPointer<QAction> mAction;
QUuid mId;
};

Q_DECLARE_METATYPE( QgsAction )
Expand Down

0 comments on commit a6eb7b6

Please sign in to comment.