QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsattributeactionpropertiesdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeactionpropertiesdialog.cpp - QgsAttributeActionPropertiesDialog
3
4 ---------------------
5 begin : 18.4.2016
6 copyright : (C) 2016 by Matthias Kuhn
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
19#include "qgsproject.h"
20#include "qgsvectorlayer.h"
21#include "qgsapplication.h"
23#include "qgsactionscope.h"
25
26#include <QComboBox>
27#include <QLineEdit>
28#include <QPlainTextEdit>
29#include <QCheckBox>
30#include <QFileDialog>
31#include <QImageWriter>
32
33QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( Qgis::AttributeActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet<QString> &actionScopes, const QString &notificationMessage, bool isEnabledOnlyWhenEditable, QgsVectorLayer *layer, QWidget *parent )
34 : QDialog( parent )
35 , mLayer( layer )
36{
37 setupUi( this );
38
39 populateActionTypes();
40
41 mActionType->setCurrentIndex( mActionType->findData( static_cast< int >( type ) ) );
42 mActionName->setText( description );
43 mShortTitle->setText( shortTitle );
44 mActionIcon->setText( iconPath );
45 mIconPreview->setPixmap( QPixmap( iconPath ) );
46 mActionText->setText( actionText );
47 mCaptureOutput->setChecked( capture );
48 mNotificationMessage->setText( notificationMessage );
49 mIsEnabledOnlyWhenEditable->setChecked( isEnabledOnlyWhenEditable );
50
51 init( actionScopes );
52}
53
55 : QDialog( parent )
56 , mLayer( layer )
57{
58 setupUi( this );
59
60 populateActionTypes();
61
62 QSet<QString> defaultActionScopes;
63 defaultActionScopes << QStringLiteral( "Canvas" )
64 << QStringLiteral( "FieldSpecific" )
65 << QStringLiteral( "Feature" )
66 << QStringLiteral( "FeatureForm" );
67
68 init( defaultActionScopes );
69}
70
72{
73 return static_cast<Qgis::AttributeActionType>( mActionType->currentData().toInt() );
74}
75
77{
78 return mActionName->text();
79}
80
82{
83 return mShortTitle->text();
84}
85
87{
88 return mActionIcon->text();
89}
90
92{
93 return mActionText->text();
94}
95
97{
98 QSet<QString> actionScopes;
99
100 const auto constMActionScopeCheckBoxes = mActionScopeCheckBoxes;
101 for ( QCheckBox *cb : constMActionScopeCheckBoxes )
102 {
103 if ( cb->isChecked() )
104 actionScopes.insert( cb->property( "ActionScopeName" ).toString() );
105 }
106
107 return actionScopes;
108}
109
111{
112 return mNotificationMessage->text();
113}
114
116{
117 return mIsEnabledOnlyWhenEditable->isChecked();
118}
119
121{
122 return mCaptureOutput->isChecked();
123}
124
126{
128
129 const auto constMActionScopeCheckBoxes = mActionScopeCheckBoxes;
130 for ( QCheckBox *cb : constMActionScopeCheckBoxes )
131 {
132 if ( cb->isChecked() )
133 {
134 const QgsActionScope actionScope = QgsApplication::actionScopeRegistry()->actionScope( cb->property( "ActionScopeName" ).toString() );
135 context.appendScope( new QgsExpressionContextScope( actionScope.expressionContextScope() ) );
136 }
137 }
138
140
141 return context;
142}
143
144void QgsAttributeActionPropertiesDialog::browse()
145{
146 // Popup a file browser and place the results into the action widget
147 const QString action = QFileDialog::getOpenFileName(
148 this, tr( "Select an action", "File dialog window title" ), QDir::homePath() );
149
150 if ( !action.isNull() )
151 mActionText->insertText( action );
152}
153
154void QgsAttributeActionPropertiesDialog::insertExpressionOrField()
155{
156 QString selText = mActionText->selectedText();
157
158 // edit the selected expression if there's one
159 if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) )
160 selText = selText.mid( 2, selText.size() - 4 );
161
162 mActionText->insertText( "[%" + mFieldExpression->currentField() + "%]" );
163}
164
165void QgsAttributeActionPropertiesDialog::chooseIcon()
166{
167 const QList<QByteArray> list = QImageWriter::supportedImageFormats();
168 QStringList formatList;
169 const auto constList = list;
170 for ( const QByteArray &format : constList )
171 formatList << QStringLiteral( "*.%1" ).arg( QString( format ) );
172
173 const QString filter = tr( "Images( %1 ); All( *.* )" ).arg( formatList.join( QLatin1Char( ' ' ) ) );
174 const QString icon = QFileDialog::getOpenFileName( this, tr( "Choose Icon…" ), mActionIcon->text(), filter );
175
176 if ( !icon.isNull() )
177 {
178 mActionIcon->setText( icon );
179 mIconPreview->setPixmap( QPixmap( icon ) );
180 }
181}
182
183void QgsAttributeActionPropertiesDialog::updateButtons()
184{
185 if ( mActionName->text().isEmpty() || mActionText->text().isEmpty() )
186 {
187 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
188 }
189 else
190 {
191 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
192 }
193}
194
195void QgsAttributeActionPropertiesDialog::init( const QSet<QString> &actionScopes )
196{
197 const QSet<QgsActionScope> availableActionScopes = QgsApplication::actionScopeRegistry()->actionScopes();
198
199 const auto constAvailableActionScopes = availableActionScopes;
200 for ( const QgsActionScope &scope : constAvailableActionScopes )
201 {
202 QCheckBox *actionScopeCheckBox = new QCheckBox( scope.title() );
203 if ( actionScopes.contains( scope.id() ) )
204 actionScopeCheckBox->setChecked( true );
205 const QStringList variables = scope.expressionContextScope().variableNames();
206
207 QString tooltip = scope.description();
208 if ( !variables.empty() )
209 {
210 tooltip += QLatin1String( "<br><br>" );
211 tooltip += tr( "Additional variables" );
212 tooltip += QLatin1String( "<ul><li>" );
213 tooltip += variables.join( QLatin1String( "</li><li>" ) );
214 tooltip += QLatin1String( "</ul></li>" );
215 }
216 actionScopeCheckBox->setToolTip( tooltip );
217 actionScopeCheckBox->setProperty( "ActionScopeName", scope.id() );
218 mActionScopeCheckBoxes.append( actionScopeCheckBox );
219 mActionScopesGroupBox->layout()->addWidget( actionScopeCheckBox );
220 }
221
222 QgsDistanceArea myDa;
224 myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
225
226 mFieldExpression->setLayer( mLayer );
227 mFieldExpression->setGeomCalculator( myDa );
228 mFieldExpression->registerExpressionContextGenerator( this );
229
230 connect( mBrowseButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::browse );
231 connect( mInsertFieldOrExpression, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::insertExpressionOrField );
232 connect( mActionName, &QLineEdit::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
233 connect( mActionText, &QsciScintilla::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
234 connect( mChooseIconButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::chooseIcon );
235
236 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsAttributeActionPropertiesDialog::showHelp );
237
238 updateButtons();
239}
240
241void QgsAttributeActionPropertiesDialog::showHelp()
242{
243 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#actions-properties" ) );
244}
245
246void QgsAttributeActionPropertiesDialog::populateActionTypes()
247{
248 mActionType->addItem( tr( "Generic" ), static_cast< int>( Qgis::AttributeActionType::Generic ) );
249 mActionType->addItem( tr( "Python" ), static_cast< int>( Qgis::AttributeActionType::GenericPython ) );
250 mActionType->addItem( tr( "macOS" ), static_cast< int>( Qgis::AttributeActionType::Mac ) );
251 mActionType->addItem( tr( "Windows" ), static_cast< int>( Qgis::AttributeActionType::Windows ) );
252 mActionType->addItem( tr( "Unix" ), static_cast< int>( Qgis::AttributeActionType::Unix ) );
253 mActionType->addItem( tr( "Open URL" ), static_cast< int>( Qgis::AttributeActionType::OpenUrl ) );
254 mActionType->addItem( tr( "Submit URL (urlencoded or JSON)" ), static_cast< int>( Qgis::AttributeActionType::SubmitUrlEncoded ) );
255 mActionType->addItem( tr( "Submit URL (multipart)" ), static_cast< int>( Qgis::AttributeActionType::SubmitUrlMultipart ) );
256}
AttributeActionType
Map layer action flags.
Definition: qgis.h:3062
@ Mac
MacOS specific.
@ OpenUrl
Open URL action.
@ SubmitUrlMultipart
POST data to an URL using "multipart/form-data".
@ Windows
Windows specific.
@ SubmitUrlEncoded
POST data to an URL, using "application/x-www-form-urlencoded" or "application/json" if the body is v...
QgsActionScope actionScope(const QString &id)
Gets an action scope by its id.
QSet< QgsActionScope > actionScopes
An action scope defines a "place" for an action to be shown and may add additional expression variabl...
QgsExpressionContextScope expressionContextScope() const
An expression scope may offer additional variables for an action scope.
static QgsActionScopeRegistry * actionScopeRegistry()
Returns the action scope registry.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsAttributeActionPropertiesDialog(Qgis::AttributeActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet< QString > &actionScopes, const QString &notificationMessage, bool isEnabledOnlyWhenEditable, QgsVectorLayer *layer, QWidget *parent=nullptr)
Constructor for QgsAttributeActionPropertiesDialog.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:38
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:484
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:113
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...