Skip to content
Permalink
Browse files
Simplify code and support copy/paste
  • Loading branch information
elpaso committed Aug 17, 2021
1 parent 6de2f2d commit 915349dee0929f81660f6a59b4a7d5427979f47f
@@ -28,37 +28,26 @@ Creates a new element which can display a layer action.
:param parent: The parent (used as container)
%End

QgsAttributeEditorAction( const QUuid &uuid, const QString &layerId, QgsAttributeEditorElement *parent );
QgsAttributeEditorAction( const QUuid &uuid, QgsAttributeEditorElement *parent );
%Docstring
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 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;
const QgsAction &action( const QgsVectorLayer *layer ) const;
%Docstring
Returns the (possibly lazy loaded) action.
Returns the (possibly lazy loaded) action for the given ``layer``.
%End

void setAction( const QgsAction &newAction );
%Docstring
Set the action to ``newAction``.
%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

};
@@ -24,31 +24,25 @@ QgsAttributeEditorAction::QgsAttributeEditorAction( const QgsAction &action, Qgs
, mUuid( action.id() )
{}

QgsAttributeEditorAction::QgsAttributeEditorAction( const QUuid &uuid, const QString &layerId, QgsAttributeEditorElement *parent )
QgsAttributeEditorAction::QgsAttributeEditorAction( const QUuid &uuid, QgsAttributeEditorElement *parent )
: QgsAttributeEditorAction( QgsAction(), parent )
{
mUuid = uuid;
mLayerId = layerId;
}

QgsAttributeEditorElement *QgsAttributeEditorAction::clone( QgsAttributeEditorElement *parent ) const
{
QgsAttributeEditorAction *element = new QgsAttributeEditorAction( mAction, parent );
element->mLayerId = mLayerId;
element->mUuid = mUuid;
return element;
}

const QgsAction &QgsAttributeEditorAction::action() const
const QgsAction &QgsAttributeEditorAction::action( const QgsVectorLayer *layer ) const
{
// Lazy loading
if ( ! mAction.isValid() && ! mUuid.isNull() && ! mLayerId.isEmpty() )
if ( ! mAction.isValid() && ! mUuid.isNull() && layer )
{
// Nested if for clarity
if ( const QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( mLayerId ) ); layer )
{
mAction = layer->actions()->action( mUuid );
}
mAction = layer->actions()->action( mUuid );
}
return mAction;
}
@@ -59,23 +53,6 @@ void QgsAttributeEditorAction::setAction( const QgsAction &newAction )
mAction = newAction;
}

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

const QString QgsAttributeEditorAction::actionDisplayName() const
{
if ( action().isValid() )
{
return action().shortTitle().isEmpty() ? action().name() : action().shortTitle();
}
else
{
return QString();
}
}

QString QgsAttributeEditorAction::typeIdentifier() const
{
return QStringLiteral( "attributeEditorAction" );
@@ -84,13 +61,13 @@ QString QgsAttributeEditorAction::typeIdentifier() const
void QgsAttributeEditorAction::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
{
Q_UNUSED( doc )
elem.setAttribute( QStringLiteral( "ActionUUID" ), mAction.id().toString() );
elem.setAttribute( QStringLiteral( "ActionUUID" ), mUuid.toString() );
}

void QgsAttributeEditorAction::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )
mUuid = element.attribute( QStringLiteral( "ActionUUID" ) );
mLayerId = layerId;
}
@@ -42,39 +42,27 @@ class CORE_EXPORT QgsAttributeEditorAction : public QgsAttributeEditorElement
* to create a QgsAttributeEditorAction when actions are not yet loaded.
*
* \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 );
QgsAttributeEditorAction( const QUuid &uuid, QgsAttributeEditorElement *parent );

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

/**
* Returns the (possibly lazy loaded) action.
* Returns the (possibly lazy loaded) action for the given \a layer.
*/
const QgsAction &action() const;
const QgsAction &action( const QgsVectorLayer *layer ) const;

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

/**
* 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:

// Lazy loaded
mutable QgsAction mAction;
QUuid mUuid;
QString mLayerId;

// QgsAttributeEditorElement interface
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
@@ -76,7 +76,7 @@ QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement
}
else if ( element.tagName() == QLatin1String( "attributeEditorAction" ) )
{
newElement = new QgsAttributeEditorAction( element.attribute( QStringLiteral( "name" ) ), layerId, parent );
newElement = new QgsAttributeEditorAction( element.attribute( QStringLiteral( "name" ) ), parent );
}

if ( newElement )
@@ -17,10 +17,21 @@
#include "qgsactionmanager.h"
#include "qgsexpressioncontextutils.h"

#include <QLayout>

QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( layer, editor, parent )
{

connect( this, &QgsWidgetWrapper::contextChanged, [ = ]
{
const bool actionIsVisible {
( context().attributeFormMode() == QgsAttributeEditorContext::Mode::SingleEditMode ) ||
( context().attributeFormMode() == QgsAttributeEditorContext::Mode::AddFeatureMode ) };
if ( mActionButton )
{
mActionButton->setVisible( actionIsVisible );
}
} );
}

void QgsActionWidgetWrapper::setAction( const QgsAction &action )
@@ -84,19 +95,18 @@ void QgsActionWidgetWrapper::initWidget( QWidget *editor )
{
mActionButton->setEnabled( false );
}
else

// Always connect
connect( mActionButton, &QPushButton::clicked, this, [ & ]
{
connect( mActionButton, &QPushButton::clicked, this, [ & ]
{
const QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
expressionContext.setFeature( mFeature );
mAction.run( layer(), mFeature, expressionContext );
} );
}
const QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
expressionContext.setFeature( mFeature );
mAction.run( layer(), mFeature, expressionContext );
} );

}

}


@@ -444,7 +444,9 @@ void QgsValueRelationWidgetWrapper::showIndeterminateState()
{
for ( int i = 0; i < nofColumns; ++i )
{
whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
QgsSignalBlocker<QTableWidget> blocker( mTableWidget );
if ( mTableWidget->item( j, i ) )
mTableWidget->item( j, i )->setCheckState( Qt::PartiallyChecked );
}
}
}
@@ -1349,7 +1349,8 @@ void QgsAttributeForm::synchronizeState()
for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{

if ( QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww ); eww )
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
{
QgsAttributeFormEditorWidget *formWidget = mFormEditorWidgets.value( eww->fieldIdx() );

@@ -1367,8 +1368,10 @@ void QgsAttributeForm::synchronizeState()
{
ww->setEnabled( isEditable );
}

}


if ( mMode != QgsAttributeEditorContext::SearchMode )
{
QStringList invalidFields, descriptions;
@@ -2018,13 +2021,12 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
break;

QgsActionWidgetWrapper *actionWrapper = new QgsActionWidgetWrapper( mLayer, nullptr, this );
actionWrapper->setAction( elementDef->action() );
actionWrapper->setAction( elementDef->action( vl ) );
context.setAttributeFormMode( mMode );
actionWrapper->setContext( context );

mWidgets.append( actionWrapper );

newWidgetInfo.widget = actionWrapper->widget();
newWidgetInfo.showLabel = false;

break;
}
@@ -434,9 +434,17 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
case QgsAttributeEditorElement::AeTypeAction:
{
const QgsAttributeEditorAction *actionEditor = static_cast<const QgsAttributeEditorAction *>( widgetDef );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Action, actionEditor->actionId(), actionEditor->actionDisplayName() );
itemData.setShowLabel( widgetDef->showLabel() );
newWidget = tree->addItem( parent, itemData );
const QgsAction action { actionEditor->action( mLayer ) };
if ( action.isValid() )
{
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Action, action.id().toString(), action.shortTitle().isEmpty() ? action.name() : action.shortTitle() );
itemData.setShowLabel( widgetDef->showLabel() );
newWidget = tree->addItem( parent, itemData );
}
else
{
QgsDebugMsg( QStringLiteral( "Invalid form action" ) );
}
break;
}

@@ -64,33 +64,21 @@ def testEditorActionCtor(self):

parent = QgsAttributeEditorContainer('container', None)
editor_action = QgsAttributeEditorAction(self.action1, parent)
self.assertEqual(QUuid(editor_action.actionId()), self.action_id1)
self.assertEqual(editor_action.action().id(), self.action1.id())
self.assertEqual(QUuid(editor_action.action(self.layer).id()), self.action_id1)
self.assertEqual(editor_action.action(self.layer).id(), self.action1.id())

# Lazy load
editor_action = QgsAttributeEditorAction(self.action1.id(), self.layer.id(), parent)
self.assertEqual(QUuid(editor_action.actionId()), self.action_id1)
self.assertEqual(editor_action.action().id(), self.action1.id())

def testDisplayName(self):

parent = QgsAttributeEditorContainer('container', None)
editor_action = QgsAttributeEditorAction(self.action1, parent)
self.assertEqual(editor_action.actionDisplayName(), 'Test Action 1 Short Title')

editor_action = QgsAttributeEditorAction(self.action2, parent)
self.assertEqual(editor_action.actionDisplayName(), 'Test Action 2 Short Title')

editor_action = QgsAttributeEditorAction(self.action3, parent)
self.assertEqual(editor_action.actionDisplayName(), 'Test Action 3 Desc')
editor_action = QgsAttributeEditorAction(self.action1.id(), parent)
self.assertEqual(QUuid(editor_action.action(self.layer).id()), self.action_id1)
self.assertEqual(editor_action.action(self.layer).id(), self.action1.id())

def testSetAction(self):

parent = QgsAttributeEditorContainer('container', None)
editor_action = QgsAttributeEditorAction(self.action1, parent)

editor_action.setAction(self.action2)
self.assertEqual(QUuid(editor_action.actionId()), self.action_id2)
self.assertEqual(QUuid(editor_action.action(self.layer).id()), self.action_id2)


if __name__ == '__main__':

0 comments on commit 915349d

Please sign in to comment.