QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
18#include "moc_qgsattributeactionpropertiesdialog.cpp"
20#include "qgsproject.h"
21#include "qgsvectorlayer.h"
22#include "qgsapplication.h"
24#include "qgsactionscope.h"
26
27#include <QComboBox>
28#include <QLineEdit>
29#include <QPlainTextEdit>
30#include <QCheckBox>
31#include <QFileDialog>
32#include <QImageWriter>
33
34QgsAttributeActionPropertiesDialog::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 )
35 : QDialog( parent )
36 , mLayer( layer )
37{
38 setupUi( this );
39
40 populateActionTypes();
41
42 mActionType->setCurrentIndex( mActionType->findData( static_cast<int>( type ) ) );
43 mActionName->setText( description );
44 mShortTitle->setText( shortTitle );
45 mActionIcon->setText( iconPath );
46 mIconPreview->setPixmap( QPixmap( iconPath ) );
47 mActionText->setText( actionText );
48 mCaptureOutput->setChecked( capture );
49 mNotificationMessage->setText( notificationMessage );
50 mIsEnabledOnlyWhenEditable->setChecked( isEnabledOnlyWhenEditable );
51
52 init( actionScopes );
53}
54
56 : QDialog( parent )
57 , mLayer( layer )
58{
59 setupUi( this );
60
61 populateActionTypes();
62
63 QSet<QString> defaultActionScopes;
64 defaultActionScopes << QStringLiteral( "Canvas" )
65 << QStringLiteral( "FieldSpecific" )
66 << QStringLiteral( "Feature" )
67 << QStringLiteral( "FeatureForm" );
68
69 init( defaultActionScopes );
70}
71
73{
74 return static_cast<Qgis::AttributeActionType>( mActionType->currentData().toInt() );
75}
76
78{
79 return mActionName->text();
80}
81
83{
84 return mShortTitle->text();
85}
86
88{
89 return mActionIcon->text();
90}
91
93{
94 return mActionText->text();
95}
96
98{
99 QSet<QString> actionScopes;
100
101 const auto constMActionScopeCheckBoxes = mActionScopeCheckBoxes;
102 for ( QCheckBox *cb : constMActionScopeCheckBoxes )
103 {
104 if ( cb->isChecked() )
105 actionScopes.insert( cb->property( "ActionScopeName" ).toString() );
106 }
107
108 return actionScopes;
109}
110
112{
113 return mNotificationMessage->text();
114}
115
117{
118 return mIsEnabledOnlyWhenEditable->isChecked();
119}
120
122{
123 return mCaptureOutput->isChecked();
124}
125
127{
129
130 const auto constMActionScopeCheckBoxes = mActionScopeCheckBoxes;
131 for ( QCheckBox *cb : constMActionScopeCheckBoxes )
132 {
133 if ( cb->isChecked() )
134 {
135 const QgsActionScope actionScope = QgsApplication::actionScopeRegistry()->actionScope( cb->property( "ActionScopeName" ).toString() );
136 context.appendScope( new QgsExpressionContextScope( actionScope.expressionContextScope() ) );
137 }
138 }
139
141
142 return context;
143}
144
145void QgsAttributeActionPropertiesDialog::browse()
146{
147 // Popup a file browser and place the results into the action widget
148 const QString action = QFileDialog::getOpenFileName(
149 this, tr( "Select an action", "File dialog window title" ), QDir::homePath()
150 );
151
152 if ( !action.isNull() )
153 mActionText->insertText( action );
154}
155
156void QgsAttributeActionPropertiesDialog::insertExpressionOrField()
157{
158 QString selText = mActionText->selectedText();
159
160 // edit the selected expression if there's one
161 if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) )
162 selText = selText.mid( 2, selText.size() - 4 );
163
164 mActionText->insertText( "[%" + mFieldExpression->currentField() + "%]" );
165}
166
167void QgsAttributeActionPropertiesDialog::chooseIcon()
168{
169 const QList<QByteArray> list = QImageWriter::supportedImageFormats();
170 QStringList formatList;
171 const auto constList = list;
172 for ( const QByteArray &format : constList )
173 formatList << QStringLiteral( "*.%1" ).arg( QString( format ) );
174
175 const QString filter = tr( "Images( %1 ); All( *.* )" ).arg( formatList.join( QLatin1Char( ' ' ) ) );
176 const QString icon = QFileDialog::getOpenFileName( this, tr( "Choose Icon…" ), mActionIcon->text(), filter );
177
178 if ( !icon.isNull() )
179 {
180 mActionIcon->setText( icon );
181 mIconPreview->setPixmap( QPixmap( icon ) );
182 }
183}
184
185void QgsAttributeActionPropertiesDialog::updateButtons()
186{
187 if ( mActionName->text().isEmpty() || mActionText->text().isEmpty() )
188 {
189 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
190 }
191 else
192 {
193 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
194 }
195}
196
197void QgsAttributeActionPropertiesDialog::init( const QSet<QString> &actionScopes )
198{
199 const QSet<QgsActionScope> availableActionScopes = QgsApplication::actionScopeRegistry()->actionScopes();
200
201 const auto constAvailableActionScopes = availableActionScopes;
202 for ( const QgsActionScope &scope : constAvailableActionScopes )
203 {
204 QCheckBox *actionScopeCheckBox = new QCheckBox( scope.title() );
205 if ( actionScopes.contains( scope.id() ) )
206 actionScopeCheckBox->setChecked( true );
207 const QStringList variables = scope.expressionContextScope().variableNames();
208
209 QString tooltip = scope.description();
210 if ( !variables.empty() )
211 {
212 tooltip += QLatin1String( "<br><br>" );
213 tooltip += tr( "Additional variables" );
214 tooltip += QLatin1String( "<ul><li>" );
215 tooltip += variables.join( QLatin1String( "</li><li>" ) );
216 tooltip += QLatin1String( "</ul></li>" );
217 }
218 actionScopeCheckBox->setToolTip( tooltip );
219 actionScopeCheckBox->setProperty( "ActionScopeName", scope.id() );
220 mActionScopeCheckBoxes.append( actionScopeCheckBox );
221 mActionScopesGroupBox->layout()->addWidget( actionScopeCheckBox );
222 }
223
224 QgsDistanceArea myDa;
226 myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
227
228 mFieldExpression->setLayer( mLayer );
229 mFieldExpression->setGeomCalculator( myDa );
230 mFieldExpression->registerExpressionContextGenerator( this );
231
232 connect( mBrowseButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::browse );
233 connect( mInsertFieldOrExpression, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::insertExpressionOrField );
234 connect( mActionName, &QLineEdit::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
235 connect( mActionText, &QsciScintilla::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
236 connect( mChooseIconButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::chooseIcon );
237
238 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsAttributeActionPropertiesDialog::showHelp );
239
240 updateButtons();
241}
242
243void QgsAttributeActionPropertiesDialog::showHelp()
244{
245 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#actions-properties" ) );
246}
247
248void QgsAttributeActionPropertiesDialog::populateActionTypes()
249{
250 mActionType->addItem( tr( "Generic" ), static_cast<int>( Qgis::AttributeActionType::Generic ) );
251 mActionType->addItem( tr( "Python" ), static_cast<int>( Qgis::AttributeActionType::GenericPython ) );
252 mActionType->addItem( tr( "macOS" ), static_cast<int>( Qgis::AttributeActionType::Mac ) );
253 mActionType->addItem( tr( "Windows" ), static_cast<int>( Qgis::AttributeActionType::Windows ) );
254 mActionType->addItem( tr( "Unix" ), static_cast<int>( Qgis::AttributeActionType::Unix ) );
255 mActionType->addItem( tr( "Open URL" ), static_cast<int>( Qgis::AttributeActionType::OpenUrl ) );
256 mActionType->addItem( tr( "Submit URL (urlencoded or JSON)" ), static_cast<int>( Qgis::AttributeActionType::SubmitUrlEncoded ) );
257 mActionType->addItem( tr( "Submit URL (multipart)" ), static_cast<int>( Qgis::AttributeActionType::SubmitUrlMultipart ) );
258}
AttributeActionType
Attribute action types.
Definition qgis.h:4384
@ 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
Returns the expression context scope for the 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:39
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
static QgsProject * instance()
Returns the QgsProject singleton instance.
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...