Skip to content

Commit

Permalink
Simplify code and support copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 17, 2021
1 parent 6de2f2d commit 915349d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 97 deletions.
Expand Up @@ -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

};
Expand Down
35 changes: 6 additions & 29 deletions src/core/editform/qgsattributeeditoraction.cpp
Expand Up @@ -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;
}
Expand All @@ -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" );
Expand All @@ -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;
}
18 changes: 3 additions & 15 deletions src/core/editform/qgsattributeeditoraction.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/editform/qgsattributeeditorelement.cpp
Expand Up @@ -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 )
Expand Down
34 changes: 22 additions & 12 deletions src/gui/editorwidgets/qgsactionwidgetwrapper.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
} );

}

}


4 changes: 3 additions & 1 deletion src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -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 );
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -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() );

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

}


if ( mMode != QgsAttributeEditorContext::SearchMode )
{
QStringList invalidFields, descriptions;
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 11 additions & 3 deletions src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -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;
}

Expand Down
24 changes: 6 additions & 18 deletions tests/src/python/test_qgsattributeeditoraction.py
Expand Up @@ -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__':
Expand Down

0 comments on commit 915349d

Please sign in to comment.