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