QGIS API Documentation  3.23.0-Master (b5237dafc3)
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 
20 #include <QLayout>
21 
22 QgsActionWidgetWrapper::QgsActionWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
23  : QgsWidgetWrapper( layer, editor, parent )
24 {
25  connect( this, &QgsWidgetWrapper::contextChanged, [ = ]
26  {
27  const bool actionIsVisible {
28  ( context().attributeFormMode() == QgsAttributeEditorContext::Mode::SingleEditMode ) ||
29  ( context().attributeFormMode() == QgsAttributeEditorContext::Mode::AddFeatureMode ) };
30  if ( mActionButton )
31  {
32  mActionButton->setVisible( actionIsVisible );
33  }
34  } );
35 }
36 
38 {
39  mAction = action;
40 }
41 
43 {
44  mFeature = feature;
45 }
46 
48 {
49  if ( valid() && layer() )
50  {
51  mActionButton->setEnabled( !mAction.isEnabledOnlyWhenEditable() || enabled );
52  }
53 }
54 
56 {
57  return mAction.isValid() && mAction.runable();
58 }
59 
60 QWidget *QgsActionWidgetWrapper::createWidget( QWidget *parent )
61 {
62  return new QPushButton( parent );
63 }
64 
65 void QgsActionWidgetWrapper::initWidget( QWidget *editor )
66 {
67 
68  mActionButton = qobject_cast<QPushButton *>( editor );
69 
70  if ( !mActionButton )
71  return;
72 
73  if ( valid() && layer() )
74  {
75  const QString shortTitle { mAction.shortTitle() }; // might be empty
76  const QString description { mAction.name() }; // mandatory
77  const QIcon icon { mAction.icon() }; // might be invalid
78 
79  // Configure push button
80  if ( ! icon.isNull() )
81  {
82  mActionButton->setIcon( icon );
83  mActionButton->setToolTip( description );
84  }
85  else
86  {
87  mActionButton->setText( shortTitle.isEmpty() ? description : shortTitle );
88  if ( ! shortTitle.isEmpty() )
89  {
90  mActionButton->setToolTip( description );
91  }
92  }
93 
94  if ( mAction.isEnabledOnlyWhenEditable() && !layer()->isEditable() )
95  {
96  mActionButton->setEnabled( false );
97  }
98 
99  // Always connect
100  connect( mActionButton, &QPushButton::clicked, this, [ & ]
101  {
102  const QgsAttributeEditorContext attributecontext = context();
103  QgsExpressionContext expressionContext = layer()->createExpressionContext();
104  expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
105  expressionContext.setFeature( mFeature );
106  mAction.run( layer(), mFeature, expressionContext );
107  } );
108 
109  }
110 
111 }
112 
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:35
QString name() const
The name of the action. This may be a longer description.
Definition: qgsaction.h:127
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition: qgsaction.cpp:58
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:150
bool isValid() const
Returns true if this action was a default constructed one.
Definition: qgsaction.h:144
QString shortTitle() const
The short title is used to label user interface elements like buttons.
Definition: qgsaction.h:130
bool isEnabledOnlyWhenEditable() const
Returns whether only enabled in editable mode.
Definition: qgsaction.h:176
This class contains context information for attribute editor widgets.
QString attributeFormModeString() const
Returns given attributeFormMode as string.
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:56
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...
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.