QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsactionwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsactionwidgetwrapper.cpp - QgsActionWidgetWrapper
3
4 ---------------------
5 begin : 14.8.2021
6 copyright : (C) 2021 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17#include "moc_qgsactionwidgetwrapper.cpp"
19#include "qgsattributeform.h"
20
21#include <QLayout>
22
23QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
24 : QgsWidgetWrapper( layer, editor, parent )
25{
26 connect( this, &QgsWidgetWrapper::contextChanged, [ = ]
27 {
28 const bool actionIsVisible {
31 if ( mActionButton )
32 {
33 mActionButton->setVisible( actionIsVisible );
34 }
35 } );
36}
37
39{
40 mAction = action;
41}
42
44{
45 mFeature = feature;
46}
47
49{
50 if ( valid() && layer() )
51 {
52 mActionButton->setEnabled( !mAction.isEnabledOnlyWhenEditable() || enabled );
53 }
54}
55
57{
58 return mAction.isValid() && mAction.runable();
59}
60
61QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
62{
63 return new QPushButton( parent );
64}
65
67{
68
69 mActionButton = qobject_cast<QPushButton *>( editor );
70
71 if ( !mActionButton )
72 return;
73
74 if ( valid() && layer() )
75 {
76 const QString shortTitle { mAction.shortTitle() }; // might be empty
77 const QString description { mAction.name() }; // mandatory
78 const QIcon icon { mAction.icon() }; // might be invalid
79
80 // Configure push button
81 if ( ! icon.isNull() )
82 {
83 mActionButton->setIcon( icon );
84 mActionButton->setToolTip( description );
85 }
86 else
87 {
88 mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
89 if ( ! shortTitle.isEmpty() )
90 {
91 mActionButton->setToolTip( description );
92 }
93 }
94
95 if ( mAction.isEnabledOnlyWhenEditable() && !layer()->isEditable() )
96 {
97 mActionButton->setEnabled( false );
98 }
99
100 // Always connect
101 connect( mActionButton, &QPushButton::clicked, this, [ & ]
102 {
103 const QgsAttributeEditorContext attributecontext = context();
104 QgsExpressionContext expressionContext = layer()->createExpressionContext();
105 expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
106 expressionContext.setFeature( mFeature );
108 {
109 if ( QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent() ) )
110 {
111 const QString formCode = QStringLiteral( "locals()[\"form\"] = sip.wrapinstance( %1, qgis.gui.QgsAttributeForm )\n" )
112 .arg( ( quint64 ) form );
113 QgsAction action { mAction };
114 action.setCommand( formCode + mAction.command() );
115 action.run( layer(), mFeature, expressionContext );
116 }
117 }
118 else
119 {
120 mAction.run( layer(), mFeature, expressionContext );
121 }
122 } );
123
124 }
125
126}
127
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setFeature(const QgsFeature &feature) override
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setAction(const QgsAction &action)
Sets the action.
void setEnabled(bool enabled) override
bool valid() const override
Returns true if the widget has been properly initialized.
QgsActionWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create an action widget wrapper.
Utility class that encapsulates an action based on vector attributes.
Definition qgsaction.h:37
QString name() const
The name of the action. This may be a longer description.
Definition qgsaction.h:115
Qgis::AttributeActionType type() const
The action type.
Definition qgsaction.h:153
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition qgsaction.cpp:78
void setCommand(const QString &newCommand)
Sets the action command.
bool runable() const
Checks if the action is runable on the current platform.
Definition qgsaction.cpp:41
QIcon icon() const
The icon.
Definition qgsaction.h:136
bool isValid() const
Returns true if this action was a default constructed one.
Definition qgsaction.h:130
QString command() const
Returns the command that is executed by this action.
Definition qgsaction.h:144
QString shortTitle() const
The short title is used to label user interface elements like buttons.
Definition qgsaction.h:118
bool isEnabledOnlyWhenEditable() const
Returns whether only enabled in editable mode.
Definition qgsaction.h:160
This class contains context information for attribute editor widgets.
QString attributeFormModeString() const
Returns given attributeFormMode as string.
@ SingleEditMode
Single edit mode, for editing a single feature.
Mode attributeFormMode() const
Returns current attributeFormMode.
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Represents a vector layer which manages a vector based data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Manages an editor widget Widget and wrapper share the same parent.
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.