Navigation Menu

Skip to content

Commit

Permalink
Tests for form action widget
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 16, 2021
1 parent 889359c commit cd6cd2f
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 56 deletions.
Expand Up @@ -22,28 +22,44 @@ This element will load a layer action onto the form.

QgsAttributeEditorAction( const QgsAction &action, QgsAttributeEditorElement *parent );
%Docstring
Creates a new element which can display a layer action
Creates a new element which can display a layer action.

:param action: The action
:param parent: The parent (used as container)
%End

QgsAttributeEditorAction( const QUuid &uuid, const QString &layerId, QgsAttributeEditorElement *parent );
%Docstring
Creates a new element which can display a layer action
Creates a new element which can display a layer action, this constructor allows
to create a QgsAttributeEditorAction when actions are not yet loaded.

:param uuid: The action UUID
:param layerId: The ID of the layer the action belongs to
:param parent: The parent (used as container)
:param uuid: The action unique identifier (UUID).
:param layerId: The ID of the layer the action belongs to.
:param parent: The parent (used as container).
%End

virtual QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const /Factory/;


const QgsAction &action() const;
%Docstring
Returns the (possibly lazy loaded) action.
%End

void setAction( const QgsAction &newAction );
const QString actionUUID() const;
const QString actionTitle() const;
%Docstring
Set the ``action``.
%End

const QString actionId() const;
%Docstring
Returns the action's unique identifier (UUID).
%End

const QString actionDisplayName() const;
%Docstring
Returns the action short title (if defined) or the action name as a fallback for display.
%End

};

Expand Down
17 changes: 16 additions & 1 deletion python/core/auto_generated/qgsaction.sip.in
Expand Up @@ -49,6 +49,22 @@ Create a new QgsAction
%Docstring
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
:param notificationMessage: A particular message which reception will trigger the action
:param enabledOnlyWhenEditable: if ``True`` then action is only enable in editmode. Not available in Python bindings.
%End

QgsAction( const QUuid &id, 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() );
%Docstring
Create a new QgsAction

:param id: The unique identifier of this action
: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
Expand Down Expand Up @@ -77,7 +93,6 @@ Returns a unique id for this action.
.. versionadded:: 3.0
%End


bool isValid() const;
%Docstring
Returns ``True`` if this action was a default constructed one.
Expand Down
Expand Up @@ -11,7 +11,7 @@
class QgsActionWidgetWrapper : QgsWidgetWrapper
{
%Docstring(signature="appended")
Wraps a button widget to launch a layer action
Wraps a button widget to launch a layer action.

.. versionadded:: 3.22
%End
Expand All @@ -25,22 +25,27 @@ Wraps a button widget to launch a layer action
%Docstring
Create an action widget wrapper

:param layer: The layer on which the feature is
:param layer: The layer on which the feature is.
:param editor: An editor widget. Can be ``None`` if one should be autogenerated.
:param parent: A parent widget
%End

public:

void setAction( const QgsAction &action );
%Docstring
Sets the ``action``.
%End

virtual bool valid() const;

virtual QWidget *createWidget( QWidget *parent );

virtual void initWidget( QWidget *editor );


void setAction( const QgsAction &action );

public slots:

virtual void setFeature( const QgsFeature &feature );


Expand Down
10 changes: 6 additions & 4 deletions src/core/editform/qgsattributeeditoraction.cpp
Expand Up @@ -3,8 +3,8 @@
---------------------
begin : 14.8.2021
copyright : (C) 2021 by ale
email : [your-email-here]
copyright : (C) 2021 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
Expand All @@ -21,6 +21,7 @@
QgsAttributeEditorAction::QgsAttributeEditorAction( const QgsAction &action, QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeAction, action.id().toString(), parent )
, mAction( action )
, mUuid( action.id() )
{}

QgsAttributeEditorAction::QgsAttributeEditorAction( const QUuid &uuid, const QString &layerId, QgsAttributeEditorElement *parent )
Expand All @@ -43,6 +44,7 @@ const QgsAction &QgsAttributeEditorAction::action() const
// Lazy loading
if ( ! mAction.isValid() && ! mUuid.isNull() && ! mLayerId.isEmpty() )
{
// Nested if for clarity
if ( const QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( mLayerId ) ); layer )
{
mAction = layer->actions()->action( mUuid );
Expand All @@ -57,12 +59,12 @@ void QgsAttributeEditorAction::setAction( const QgsAction &newAction )
mAction = newAction;
}

const QString QgsAttributeEditorAction::actionUUID() const
const QString QgsAttributeEditorAction::actionId() const
{
return action().isValid() ? action().id().toString( ) : QString( );
}

const QString QgsAttributeEditorAction::actionTitle() const
const QString QgsAttributeEditorAction::actionDisplayName() const
{
if ( action().isValid() )
{
Expand Down
34 changes: 25 additions & 9 deletions src/core/editform/qgsattributeeditoraction.h
Expand Up @@ -3,8 +3,8 @@
---------------------
begin : 14.8.2021
copyright : (C) 2021 by ale
email : [your-email-here]
copyright : (C) 2021 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
Expand All @@ -30,28 +30,44 @@ class CORE_EXPORT QgsAttributeEditorAction : public QgsAttributeEditorElement
public:

/**
* Creates a new element which can display a layer action
* Creates a new element which can display a layer action.
*
* \param action The action
* \param parent The parent (used as container)
*/
QgsAttributeEditorAction( const QgsAction &action, QgsAttributeEditorElement *parent );

/**
* Creates a new element which can display a layer action
* Creates a new element which can display a layer action, this constructor allows
* to create a QgsAttributeEditorAction when actions are not yet loaded.
*
* \param uuid The action UUID
* \param layerId The ID of the layer the action belongs to
* \param parent The parent (used as container)
* \param uuid The action unique identifier (UUID).
* \param layerId The ID of the layer the action belongs to.
* \param parent The parent (used as container).
*/
QgsAttributeEditorAction( const QUuid &uuid, const QString &layerId, QgsAttributeEditorElement *parent );

QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const override SIP_FACTORY;

/**
* Returns the (possibly lazy loaded) action.
*/
const QgsAction &action() const;

/**
* Set the \a action.
*/
void setAction( const QgsAction &newAction );
const QString actionUUID() const;
const QString actionTitle() const;

/**
* Returns the action's unique identifier (UUID).
*/
const QString actionId() const;

/**
* Returns the action short title (if defined) or the action name as a fallback for display.
*/
const QString actionDisplayName() const;

private:

Expand Down
5 changes: 0 additions & 5 deletions src/core/qgsaction.cpp
Expand Up @@ -27,11 +27,6 @@
#include "qgslogger.h"
#include "qgsexpressioncontextutils.h"

void QgsAction::setId( const QUuid &id )
{
mId = id;
}

bool QgsAction::runable() const
{
return mType == Generic ||
Expand Down
50 changes: 42 additions & 8 deletions src/core/qgsaction.h
Expand Up @@ -119,6 +119,48 @@ class CORE_EXPORT QgsAction
{}
#endif

/**
* Create a new QgsAction
*
* \param id The unique identifier of this action
* \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
* \param notificationMessage A particular message which reception will trigger the action
* \param enabledOnlyWhenEditable if TRUE then action is only enable in editmode. Not available in Python bindings.
*/
#ifndef SIP_RUN
QgsAction( const QUuid &id, 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 )
: mType( type )
, mDescription( description )
, mShortTitle( shortTitle )
, mIcon( icon )
, mCommand( action )
, mCaptureOutput( capture )
, mActionScopes( actionScopes )
, mNotificationMessage( notificationMessage )
, mId( id )
, mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{}
#else
QgsAction( const QUuid &id, 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() )
: mType( type )
, mDescription( description )
, mShortTitle( shortTitle )
, mIcon( icon )
, mCommand( action )
, mCaptureOutput( capture )
, mActionScopes( actionScopes )
, mNotificationMessage( notificationMessage )
, mId( id )
, mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{}
#endif

//! The name of the action. This may be a longer description.
QString name() const { return mDescription; }

Expand All @@ -132,14 +174,6 @@ class CORE_EXPORT QgsAction
*/
QUuid id() const { return mId; }

/**
* Set the unique \a id for this action.
*
* \note not available in Python bindings
* \since QGIS 3.22
*/
void setId( const QUuid &id ) SIP_SKIP;

/**
* Returns TRUE if this action was a default constructed one.
*
Expand Down
31 changes: 26 additions & 5 deletions src/gui/editorwidgets/qgsactionwidgetwrapper.cpp
Expand Up @@ -35,7 +35,7 @@ void QgsActionWidgetWrapper::setFeature( const QgsFeature &feature )

bool QgsActionWidgetWrapper::valid() const
{
return true;
return mAction.isValid() && mAction.runable();
}

QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
Expand All @@ -51,12 +51,33 @@ void QgsActionWidgetWrapper::initWidget( QWidget *editor )
if ( !mActionButton )
return;

if ( mAction.isValid()
&& layer() )
if ( valid() && layer() )
{
if ( mAction.isValid() && mAction.runable() )
const QString shortTitle { mAction.shortTitle() }; // might be empty
const QString description { mAction.name() }; // mandatory
const QIcon icon { mAction.icon() }; // might be invalid

// Configure push button
if ( ! icon.isNull() )
{
mActionButton->setIcon( mAction.icon() );
mActionButton->setToolTip( description );
}
else
{
mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
if ( ! shortTitle.isEmpty() )
{
mActionButton->setToolTip( description );
}
}

if ( mAction.isEnabledOnlyWhenEditable() && ! layer()->isEditable() )
{
mActionButton->setEnabled( false );
}
else
{
mActionButton->setText( mAction.shortTitle() );
connect( mActionButton, &QPushButton::clicked, this, [ & ]
{
const QgsAttributeEditorContext attributecontext = context();
Expand Down
15 changes: 10 additions & 5 deletions src/gui/editorwidgets/qgsactionwidgetwrapper.h
Expand Up @@ -25,7 +25,7 @@

/**
* \ingroup gui
* \brief Wraps a button widget to launch a layer action
* \brief Wraps a button widget to launch a layer action.
* \since QGIS 3.22
*/
class GUI_EXPORT QgsActionWidgetWrapper : public QgsWidgetWrapper
Expand All @@ -37,21 +37,26 @@ class GUI_EXPORT QgsActionWidgetWrapper : public QgsWidgetWrapper
/**
* Create an action widget wrapper
*
* \param layer The layer on which the feature is
* \param layer The layer on which the feature is.
* \param editor An editor widget. Can be NULLPTR if one should be autogenerated.
* \param parent A parent widget
*/
QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent );

// QgsWidgetWrapper interface
public:

/**
* Sets the \a action.
*/
void setAction( const QgsAction &action );

// QgsWidgetWrapper interface
bool valid() const override;
QWidget *createWidget( QWidget *parent ) override;
void initWidget( QWidget *editor ) override;

void setAction( const QgsAction &action );

public slots:

void setFeature( const QgsFeature &feature ) override;

private:
Expand Down

0 comments on commit cd6cd2f

Please sign in to comment.