QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 if ( !action.isNull() )
152 mActionText->insertText( action );
153}
154
155void QgsAttributeActionPropertiesDialog::insertExpressionOrField()
156{
157 QString selText = mActionText->selectedText();
158
159 // edit the selected expression if there's one
160 if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) )
161 selText = selText.mid( 2, selText.size() - 4 );
162
163 mActionText->insertText( "[%" + mFieldExpression->currentField() + "%]" );
164}
165
166void QgsAttributeActionPropertiesDialog::chooseIcon()
167{
168 const QList<QByteArray> list = QImageWriter::supportedImageFormats();
169 QStringList formatList;
170 const auto constList = list;
171 for ( const QByteArray &format : constList )
172 formatList << QStringLiteral( "*.%1" ).arg( QString( format ) );
173
174 const QString filter = tr( "Images( %1 ); All( *.* )" ).arg( formatList.join( QLatin1Char( ' ' ) ) );
175 const QString icon = QFileDialog::getOpenFileName( this, tr( "Choose Icon…" ), mActionIcon->text(), filter );
176
177 if ( !icon.isNull() )
178 {
179 mActionIcon->setText( icon );
180 mIconPreview->setPixmap( QPixmap( icon ) );
181 }
182}
183
184void QgsAttributeActionPropertiesDialog::updateButtons()
185{
186 if ( mActionName->text().isEmpty() || mActionText->text().isEmpty() )
187 {
188 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
189 }
190 else
191 {
192 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
193 }
194}
195
196void QgsAttributeActionPropertiesDialog::init( const QSet<QString> &actionScopes )
197{
198 const QSet<QgsActionScope> availableActionScopes = QgsApplication::actionScopeRegistry()->actionScopes();
199
200 const auto constAvailableActionScopes = availableActionScopes;
201 for ( const QgsActionScope &scope : constAvailableActionScopes )
202 {
203 QCheckBox *actionScopeCheckBox = new QCheckBox( scope.title() );
204 if ( actionScopes.contains( scope.id() ) )
205 actionScopeCheckBox->setChecked( true );
206 const QStringList variables = scope.expressionContextScope().variableNames();
207
208 QString tooltip = scope.description();
209 if ( !variables.empty() )
210 {
211 tooltip += QLatin1String( "<br><br>" );
212 tooltip += tr( "Additional variables" );
213 tooltip += QLatin1String( "<ul><li>" );
214 tooltip += variables.join( QLatin1String( "</li><li>" ) );
215 tooltip += QLatin1String( "</ul></li>" );
216 }
217 actionScopeCheckBox->setToolTip( tooltip );
218 actionScopeCheckBox->setProperty( "ActionScopeName", scope.id() );
219 mActionScopeCheckBoxes.append( actionScopeCheckBox );
220 mActionScopesGroupBox->layout()->addWidget( actionScopeCheckBox );
221 }
222
223 QgsDistanceArea myDa;
225 myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
226
227 mFieldExpression->setLayer( mLayer );
228 mFieldExpression->setGeomCalculator( myDa );
229 mFieldExpression->registerExpressionContextGenerator( this );
230
231 connect( mBrowseButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::browse );
232 connect( mInsertFieldOrExpression, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::insertExpressionOrField );
233 connect( mActionName, &QLineEdit::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
234 connect( mActionText, &QsciScintilla::textChanged, this, &QgsAttributeActionPropertiesDialog::updateButtons );
235 connect( mChooseIconButton, &QAbstractButton::clicked, this, &QgsAttributeActionPropertiesDialog::chooseIcon );
236
237 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsAttributeActionPropertiesDialog::showHelp );
238
239 updateButtons();
240}
241
242void QgsAttributeActionPropertiesDialog::showHelp()
243{
244 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#actions-properties" ) );
245}
246
247void QgsAttributeActionPropertiesDialog::populateActionTypes()
248{
249 mActionType->addItem( tr( "Generic" ), static_cast< int>( Qgis::AttributeActionType::Generic ) );
250 mActionType->addItem( tr( "Python" ), static_cast< int>( Qgis::AttributeActionType::GenericPython ) );
251 mActionType->addItem( tr( "macOS" ), static_cast< int>( Qgis::AttributeActionType::Mac ) );
252 mActionType->addItem( tr( "Windows" ), static_cast< int>( Qgis::AttributeActionType::Windows ) );
253 mActionType->addItem( tr( "Unix" ), static_cast< int>( Qgis::AttributeActionType::Unix ) );
254 mActionType->addItem( tr( "Open URL" ), static_cast< int>( Qgis::AttributeActionType::OpenUrl ) );
255 mActionType->addItem( tr( "Submit URL (urlencoded or JSON)" ), static_cast< int>( Qgis::AttributeActionType::SubmitUrlEncoded ) );
256 mActionType->addItem( tr( "Submit URL (multipart)" ), static_cast< int>( Qgis::AttributeActionType::SubmitUrlMultipart ) );
257}
AttributeActionType
Attribute action types.
Definition qgis.h:4339
@ 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...