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