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