QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsattributeform.h"
20#include "qgsgui.h"
21#include "qgsmessagebar.h"
22
23#include <QLayout>
24
25#include "moc_qgsactionwidgetwrapper.cpp"
26
27QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent, QgsMessageBar *messageBar )
28 : QgsWidgetWrapper( layer, editor, parent )
29 , mMessageBar( messageBar )
30{
31 connect( this, &QgsWidgetWrapper::contextChanged, [this] {
32 const bool actionIsVisible {
34 };
35 if ( mActionButton )
36 {
37 mActionButton->setVisible( actionIsVisible );
38 }
39 } );
40}
41
43{
44 mAction = action;
45}
46
48{
49 mFeature = feature;
50}
51
53{
54 if ( valid() && layer() )
55 {
56 mActionButton->setEnabled( !mAction.isEnabledOnlyWhenEditable() || enabled );
57 }
58}
59
61{
62 return mAction.isValid() && mAction.runable();
63}
64
65QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
66{
67 return new QPushButton( parent );
68}
69
71{
72 mActionButton = qobject_cast<QPushButton *>( editor );
73
74 if ( !mActionButton )
75 return;
76
77 if ( valid() && layer() )
78 {
79 const QString shortTitle { mAction.shortTitle() }; // might be empty
80 const QString description { mAction.name() }; // mandatory
81 const QIcon icon { mAction.icon() }; // might be invalid
82
83 // Configure push button
84 if ( !icon.isNull() )
85 {
86 mActionButton->setIcon( icon );
87 mActionButton->setToolTip( description );
88 }
89 else
90 {
91 mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
92 if ( !shortTitle.isEmpty() )
93 {
94 mActionButton->setToolTip( description );
95 }
96 }
97
98 if ( mAction.isEnabledOnlyWhenEditable() && !layer()->isEditable() )
99 {
100 mActionButton->setEnabled( false );
101 }
102
103 // Always connect
104 connect( mActionButton, &QPushButton::clicked, this, [&] {
105 const QgsAttributeEditorContext attributecontext = context();
106 QgsExpressionContext expressionContext = layer()->createExpressionContext();
107 expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
108 expressionContext.setFeature( mFeature );
109 switch ( mAction.type() )
110 {
115 {
116 if ( QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent() ) )
117 {
119 if ( !allowed )
120 {
121 if ( mMessageBar )
122 {
123 mMessageBar->pushMessage(
124 tr( "Security warning" ),
125 tr( "The action contains an embedded script which has been denied execution." ),
127 );
128 }
129 return;
130 }
131
132 const QString formCode = QStringLiteral( "locals()[\"form\"] = sip.wrapinstance( %1, qgis.gui.QgsAttributeForm )\n" )
133 .arg( ( quint64 ) form );
134 QgsAction action { mAction };
135 action.setCommand( formCode + mAction.command() );
136 action.run( layer(), mFeature, expressionContext );
137 }
138 break;
139 }
140
145 {
146 mAction.run( layer(), mFeature, expressionContext );
147 break;
148 }
149 }
150 } );
151 }
152}
@ Mac
MacOS specific.
Definition qgis.h:4678
@ OpenUrl
Open URL action.
Definition qgis.h:4681
@ Unix
Unix specific.
Definition qgis.h:4680
@ SubmitUrlMultipart
POST data to an URL using "multipart/form-data".
Definition qgis.h:4683
@ Windows
Windows specific.
Definition qgis.h:4679
@ SubmitUrlEncoded
POST data to an URL, using "application/x-www-form-urlencoded" or "application/json" if the body is v...
Definition qgis.h:4682
@ Warning
Warning message.
Definition qgis.h:158
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, QgsMessageBar *messageBar=nullptr)
Create an action widget wrapper.
Utility class that encapsulates an action based on vector attributes.
Definition qgsaction.h:37
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.
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.
The attribute form widget for vector layer features.
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
bool isValid() const
Returns the validity of this feature.
static bool allowExecutionOfEmbeddedScripts(QgsProject *project, QgsMessageBar *messageBar=nullptr)
Returns true if python embedded in a project is currently allowed to be loaded.
Definition qgsgui.cpp:394
A bar for displaying non-blocking messages to the user.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Represents a vector layer which manages a vector based dataset.
QgsExpressionContext createExpressionContext() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
QgsWidgetWrapper(QgsVectorLayer *vl, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
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.