QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
25
26#include "moc_qgsactionwidgetwrapper.cpp"
27
28using namespace Qt::StringLiterals;
29
30QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent, QgsMessageBar *messageBar )
31 : QgsWidgetWrapper( layer, editor, parent )
32 , mMessageBar( messageBar )
33{
34 connect( this, &QgsWidgetWrapper::contextChanged, [this] {
35 const bool actionIsVisible {
37 };
38 if ( mActionButton )
39 {
40 mActionButton->setVisible( actionIsVisible );
41 }
42 } );
43}
44
46{
47 mAction = action;
48}
49
51{
52 mFeature = feature;
53}
54
56{
57 if ( valid() && layer() )
58 {
59 mActionButton->setEnabled( !mAction.isEnabledOnlyWhenEditable() || enabled );
60 }
61}
62
64{
65 return mAction.isValid() && mAction.runable();
66}
67
68QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
69{
70 return new QPushButton( parent );
71}
72
74{
75 mActionButton = qobject_cast<QPushButton *>( editor );
76
77 if ( !mActionButton )
78 return;
79
80 if ( valid() && layer() )
81 {
82 const QString shortTitle { mAction.shortTitle() }; // might be empty
83 const QString description { mAction.name() }; // mandatory
84 const QIcon icon { mAction.icon() }; // might be invalid
85
86 // Configure push button
87 if ( !icon.isNull() )
88 {
89 mActionButton->setIcon( icon );
90 mActionButton->setToolTip( description );
91 }
92 else
93 {
94 mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
95 if ( !shortTitle.isEmpty() )
96 {
97 mActionButton->setToolTip( description );
98 }
99 }
100
101 if ( mAction.isEnabledOnlyWhenEditable() && !layer()->isEditable() )
102 {
103 mActionButton->setEnabled( false );
104 }
105
106 // Always connect
107 connect( mActionButton, &QPushButton::clicked, this, [&] {
108 const QgsAttributeEditorContext attributecontext = context();
109 QgsExpressionContext expressionContext = layer()->createExpressionContext();
110 expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
111 expressionContext.setFeature( mFeature );
112 switch ( mAction.type() )
113 {
118 {
119 if ( QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent() ) )
120 {
122 if ( !allowed )
123 {
124 if ( mMessageBar )
125 {
126 mMessageBar->pushMessage(
127 tr( "Security warning" ),
128 tr( "The action contains an embedded script which has been denied execution." ),
130 );
131 }
132 return;
133 }
134
135 const QString formCode = u"locals()[\"form\"] = sip.wrapinstance( %1, qgis.gui.QgsAttributeForm )\n"_s
136 .arg( ( quint64 ) form );
137 QgsAction action { mAction };
138 action.setCommand( formCode + mAction.command() );
139 action.run( layer(), mFeature, expressionContext );
140 }
141 break;
142 }
143
148 {
149 mAction.run( layer(), mFeature, expressionContext );
150 break;
151 }
152 }
153 } );
154 }
155}
@ Mac
MacOS specific.
Definition qgis.h:4750
@ OpenUrl
Open URL action.
Definition qgis.h:4753
@ Unix
Unix specific.
Definition qgis.h:4752
@ SubmitUrlMultipart
POST data to an URL using "multipart/form-data".
Definition qgis.h:4755
@ Windows
Windows specific.
Definition qgis.h:4751
@ SubmitUrlEncoded
POST data to an URL, using "application/x-www-form-urlencoded" or "application/json" if the body is v...
Definition qgis.h:4754
@ Warning
Warning message.
Definition qgis.h:161
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:38
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition qgsaction.cpp:81
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:60
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:397
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.