QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  ***************************************************************************/
16 #include "qgsactionwidgetwrapper.h"
17 #include "qgsactionmanager.h"
19 #include "qgspythonrunner.h"
20 #include "qgsattributeform.h"
21 
22 #include <QLayout>
23 
24 QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
25  : QgsWidgetWrapper( layer, editor, parent )
26 {
27  connect( this, &QgsWidgetWrapper::contextChanged, [ = ]
28  {
29  const bool actionIsVisible {
30  ( context().attributeFormMode() == QgsAttributeEditorContext::Mode::SingleEditMode ) ||
31  ( context().attributeFormMode() == QgsAttributeEditorContext::Mode::AddFeatureMode ) };
32  if ( mActionButton )
33  {
34  mActionButton->setVisible( actionIsVisible );
35  }
36  } );
37 }
38 
40 {
41  mAction = action;
42 }
43 
45 {
46  mFeature = feature;
47 }
48 
50 {
51  if ( valid() && layer() )
52  {
53  mActionButton->setEnabled( !mAction.isEnabledOnlyWhenEditable() || enabled );
54  }
55 }
56 
58 {
59  return mAction.isValid() && mAction.runable();
60 }
61 
62 QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
63 {
64  return new QPushButton( parent );
65 }
66 
67 void QgsActionWidgetWrapper::initWidget( QWidget *editor )
68 {
69 
70  mActionButton = qobject_cast<QPushButton *>( editor );
71 
72  if ( !mActionButton )
73  return;
74 
75  if ( valid() && layer() )
76  {
77  const QString shortTitle { mAction.shortTitle() }; // might be empty
78  const QString description { mAction.name() }; // mandatory
79  const QIcon icon { mAction.icon() }; // might be invalid
80 
81  // Configure push button
82  if ( ! icon.isNull() )
83  {
84  mActionButton->setIcon( icon );
85  mActionButton->setToolTip( description );
86  }
87  else
88  {
89  mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
90  if ( ! shortTitle.isEmpty() )
91  {
92  mActionButton->setToolTip( description );
93  }
94  }
95 
96  if ( mAction.isEnabledOnlyWhenEditable() && !layer()->isEditable() )
97  {
98  mActionButton->setEnabled( false );
99  }
100 
101  // Always connect
102  connect( mActionButton, &QPushButton::clicked, this, [ & ]
103  {
104  const QgsAttributeEditorContext attributecontext = context();
105  QgsExpressionContext expressionContext = layer()->createExpressionContext();
106  expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
107  expressionContext.setFeature( mFeature );
108  if ( mAction.type() == QgsAction::ActionType::GenericPython )
109  {
110  if ( QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent() ) )
111  {
112  const QString formCode = QStringLiteral( "locals()[\"form\"] = sip.wrapinstance( %1, qgis.gui.QgsAttributeForm )\n" )
113  .arg( ( quint64 ) form );
114  QgsAction action { mAction };
115  action.setCommand( formCode + mAction.command() );
116  action.run( layer(), mFeature, expressionContext );
117  }
118  }
119  else
120  {
121  mAction.run( layer(), mFeature, expressionContext );
122  }
123  } );
124 
125  }
126 
127 }
128 
QgsAction::run
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition: qgsaction.cpp:59
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgsexpressioncontextutils.h
qgsattributeform.h
QgsWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgswidgetwrapper.h:52
QgsAction::name
QString name() const
The name of the action. This may be a longer description.
Definition: qgsaction.h:127
QgsActionWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsactionwidgetwrapper.cpp:62
qgsactionwidgetwrapper.h
QgsExpressionContextUtils::formScope
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...
Definition: qgsexpressioncontextutils.cpp:270
QgsAction::shortTitle
QString shortTitle() const
The short title is used to label user interface elements like buttons.
Definition: qgsaction.h:130
QgsAction::isEnabledOnlyWhenEditable
bool isEnabledOnlyWhenEditable() const
Returns whether only enabled in editable mode.
Definition: qgsaction.h:176
QgsWidgetWrapper::context
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
Definition: qgswidgetwrapper.cpp:87
QgsAction::isValid
bool isValid() const
Returns true if this action was a default constructed one.
Definition: qgsaction.h:144
QgsWidgetWrapper::contextChanged
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
QgsAction::type
ActionType type() const
The action type.
Definition: qgsaction.h:169
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:92
qgsactionmanager.h
QgsAttributeEditorContext::attributeFormMode
Mode attributeFormMode() const
Returns current attributeFormMode.
Definition: qgsattributeeditorcontext.h:264
QgsAction::icon
QIcon icon() const
The icon.
Definition: qgsaction.h:150
QgsActionWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsactionwidgetwrapper.cpp:49
QgsActionWidgetWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Definition: qgsactionwidgetwrapper.cpp:44
QgsAction
Utility class that encapsulates an action based on vector attributes.
Definition: qgsaction.h:34
QgsActionWidgetWrapper::setAction
void setAction(const QgsAction &action)
Sets the action.
Definition: qgsactionwidgetwrapper.cpp:39
QgsActionWidgetWrapper::QgsActionWidgetWrapper
QgsActionWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create an action widget wrapper.
Definition: qgsactionwidgetwrapper.cpp:24
QgsAction::runable
bool runable() const
Checks if the action is runable on the current platform.
Definition: qgsaction.cpp:42
QgsAction::setCommand
void setCommand(const QString &newCommand)
Sets the action command.
Definition: qgsaction.cpp:240
QgsActionWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsactionwidgetwrapper.cpp:67
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsAttributeForm
Definition: qgsattributeform.h:44
QgsAction::command
QString command() const
Returns the command that is executed by this action.
Definition: qgsaction.h:159
QgsVectorLayer::createExpressionContext
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsvectorlayer.cpp:5203
QgsAttributeEditorContext
This class contains context information for attribute editor widgets. It will be passed to embedded w...
Definition: qgsattributeeditorcontext.h:40
qgspythonrunner.h
QgsActionWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsactionwidgetwrapper.cpp:57
QgsAttributeEditorContext::attributeFormModeString
QString attributeFormModeString() const
Returns given attributeFormMode as string.
Definition: qgsattributeeditorcontext.h:276
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525