QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsexternalresourcewidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexternalresourcewidgetwrapper.cpp
3 --------------------------------------
4 begin : 16.12.2015
5 copyright : (C) 2015 by Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsapplication.h"
22#include "qgsfilterlineedit.h"
23#include "qgsproject.h"
24
25#include <QLabel>
26#include <QPushButton>
27#include <QSettings>
28
29#include "moc_qgsexternalresourcewidgetwrapper.cpp"
30
32 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
33 , mMessageBar( messageBar )
34{
35}
36
38{
39 if ( mQgsWidget )
40 {
41 return mQgsWidget->documentPath( field().type() );
42 }
43
44 if ( mLineEdit )
45 {
46 if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QgsApplication::nullRepresentation() )
47 {
49 }
50 else
51 {
52 return mLineEdit->text();
53 }
54 }
55
57}
58
60{
61 if ( mLineEdit )
62 {
63 whileBlocking( mLineEdit )->clear();
64 }
65
66 if ( mLabel )
67 {
68 mLabel->clear();
69 }
70
71 if ( mQgsWidget )
72 {
73 whileBlocking( mQgsWidget )->setDocumentPath( QString() );
74 }
75}
76
78{
79 return mLineEdit || mLabel || mQgsWidget;
80}
81
82void QgsExternalResourceWidgetWrapper::updateProperties( const QgsFeature &feature )
83{
84 if ( mQgsWidget && mPropertyCollection.hasActiveProperties() )
85 {
87 expressionContext.setFeature( feature );
88 bool ok = false;
89
91 {
92 const QString path = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::Property::RootPath, expressionContext, QString(), &ok );
93 if ( ok )
94 {
95 mQgsWidget->setDefaultRoot( path );
96 }
97 }
99 {
100 const QString dvcString = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::Property::DocumentViewerContent, expressionContext, QStringLiteral( "NoContent" ), &ok );
101 if ( ok )
102 {
104 if ( dvcString.compare( QLatin1String( "image" ), Qt::CaseInsensitive ) == 0 )
105 {
107 }
108 else if ( dvcString.compare( QLatin1String( "audio" ), Qt::CaseInsensitive ) == 0 )
109 {
111 }
112 else if ( dvcString.compare( QLatin1String( "video" ), Qt::CaseInsensitive ) == 0 )
113 {
115 }
116 else if ( dvcString.compare( QLatin1String( "web" ), Qt::CaseInsensitive ) == 0 )
117 {
119 }
120 else
121 {
123 }
124 mQgsWidget->setDocumentViewerContent( dvc );
125 }
126 }
127 }
128}
129
131{
132 updateProperties( feature );
134
135 if ( mQgsWidget )
136 {
138 }
139}
140
142{
143 mForm = qobject_cast<QgsAttributeForm *>( parent );
144
145 if ( mForm )
147
148 return new QgsExternalResourceWidget( parent );
149}
150
152{
153 mLineEdit = qobject_cast<QLineEdit *>( editor );
154 mLabel = qobject_cast<QLabel *>( editor );
155 mQgsWidget = qobject_cast<QgsExternalResourceWidget *>( editor );
156
157 if ( mLineEdit )
158 {
159 QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
160 if ( fle )
161 {
163 }
164 }
165 else
166 mLineEdit = editor->findChild<QLineEdit *>();
167
168 if ( mQgsWidget )
169 {
170 mQgsWidget->setMessageBar( mMessageBar );
171
172 mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
173
174 const QVariantMap cfg = config();
175 mPropertyCollection.loadVariant( cfg.value( QStringLiteral( "PropertyCollection" ) ), propertyDefinitions() );
176
177 mQgsWidget->setStorageType( cfg.value( QStringLiteral( "StorageType" ) ).toString() );
178 mQgsWidget->setStorageAuthConfigId( cfg.value( QStringLiteral( "StorageAuthConfigId" ) ).toString() );
179
180 mQgsWidget->fileWidget()->setStorageUrlExpression( mPropertyCollection.isActive( QgsWidgetWrapper::Property::StorageUrl ) ? mPropertyCollection.property( QgsWidgetWrapper::Property::StorageUrl ).asExpression() : QgsExpression::quotedValue( cfg.value( QStringLiteral( "StorageUrl" ) ).toString() ) );
181
183
184 if ( cfg.contains( QStringLiteral( "UseLink" ) ) )
185 {
186 mQgsWidget->fileWidget()->setUseLink( cfg.value( QStringLiteral( "UseLink" ) ).toBool() );
187 }
188 if ( cfg.contains( QStringLiteral( "FullUrl" ) ) )
189 {
190 mQgsWidget->fileWidget()->setFullUrl( cfg.value( QStringLiteral( "FullUrl" ) ).toBool() );
191 }
192
194 {
195 mQgsWidget->setDefaultRoot( cfg.value( QStringLiteral( "DefaultRoot" ) ).toString() );
196 }
197 if ( cfg.contains( QStringLiteral( "StorageMode" ) ) )
198 {
199 mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode ) cfg.value( QStringLiteral( "StorageMode" ) ).toInt() );
200 }
201 if ( cfg.contains( QStringLiteral( "RelativeStorage" ) ) )
202 {
203 mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage ) cfg.value( QStringLiteral( "RelativeStorage" ) ).toInt() );
204 }
205 if ( cfg.contains( QStringLiteral( "FileWidget" ) ) )
206 {
207 mQgsWidget->setFileWidgetVisible( cfg.value( QStringLiteral( "FileWidget" ) ).toBool() );
208 }
209 if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
210 {
211 mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
212 }
213 if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
214 {
215 mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent ) cfg.value( QStringLiteral( "DocumentViewer" ) ).toInt() );
216 }
217 if ( cfg.contains( QStringLiteral( "FileWidgetFilter" ) ) )
218 {
219 mQgsWidget->fileWidget()->setFilter( cfg.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
220 }
221 if ( cfg.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
222 {
223 mQgsWidget->setDocumentViewerHeight( cfg.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt() );
224 }
225 if ( cfg.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
226 {
227 mQgsWidget->setDocumentViewerWidth( cfg.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt() );
228 }
229 }
230
231 if ( mLineEdit )
232 connect( mLineEdit, &QLineEdit::textChanged, this, [this]( const QString &value ) {
234 emit valueChanged( value );
236 emit valuesChanged( value );
237 } );
238}
239
240void QgsExternalResourceWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
241{
242 if ( mLineEdit )
243 {
245 {
246 mLineEdit->setText( QgsApplication::nullRepresentation() );
247 }
248 else
249 {
250 mLineEdit->setText( value.toString() );
251 }
252 }
253
254 if ( mLabel )
255 {
256 mLabel->setText( value.toString() );
258 emit valueChanged( value.toString() );
260 emit valuesChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
261 }
262
263 if ( mQgsWidget )
264 {
266 {
267 mQgsWidget->setDocumentPath( QgsApplication::nullRepresentation() );
268 }
269 else
270 {
271 mQgsWidget->setDocumentPath( value.toString() );
272 }
273 }
274}
275
277{
278 if ( mLineEdit )
279 mLineEdit->setReadOnly( !enabled );
280
281 if ( mQgsWidget )
282 mQgsWidget->setReadOnly( !enabled );
283}
284
285void QgsExternalResourceWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
286{
287 Q_UNUSED( newValue );
288 if ( attributeChanged )
289 {
290 const QgsExpression documentViewerContentExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::Property::DocumentViewerContent ).expressionString() );
291 const QgsExpression rootPathExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::Property::RootPath ).expressionString() );
292
293 if ( documentViewerContentExp.referencedColumns().contains( attribute ) || rootPathExp.referencedColumns().contains( attribute ) )
294 {
295 const QgsFeature feature = mForm->currentFormFeature();
296 updateProperties( feature );
297 }
298 }
299}
300
302{
303 if ( mLineEdit )
304 {
306 {
307 widget()->setStyleSheet( QString() );
308 }
309 else
310 {
311 switch ( constraintResult() )
312 {
314 mLineEdit->setStyleSheet( QString() );
315 break;
316
318 mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
319 break;
320
322 mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
323 break;
324 }
325 }
326 }
327}
328
330{
331 if ( !mQgsWidget || !layer() )
332 return;
333
334 QgsExpressionContext expressionContext( layer()->createExpressionContext() );
335 expressionContext.setFeature( formFeature() );
337 if ( context().parentFormFeature().isValid() )
338 {
339 expressionContext.appendScope( QgsExpressionContextUtils::parentFormScope( context().parentFormFeature() ) );
340 }
341
342 mQgsWidget->fileWidget()->setExpressionContext( expressionContext );
343}
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
QgsFeature formFeature() const
The feature currently being edited, in its current state.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
virtual void updateConstraintWidgetStatus()
This should update the widget with a visual cue if a constraint status changed.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QgsField field() const
Access the field.
@ ConstraintResultFailSoft
Widget failed at least one soft (non-enforced) constraint.
@ ConstraintResultPass
Widget passed constraints successfully.
@ ConstraintResultFailHard
Widget failed at least one hard (enforced) constraint.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
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.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
void updateFileWidgetExpressionContext()
Update file widget current expression context according to layer, feature, and parent feature.
void setFeature(const QgsFeature &feature) override
bool valid() const override
Returns true if the widget has been properly initialized.
QVariant value() const override
Will be used to access the widget's value.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void widgetValueChanged(const QString &attribute, const QVariant &newValue, bool attributeChanged)
Will be called when a value in the current edited form or table row changes.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsExternalResourceWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QgsMessageBar *messageBar=nullptr, QWidget *parent=nullptr)
Constructor for QgsExternalResourceWidgetWrapper.
Widget to display file path with a push button for an "open file" dialog It can also be used to displ...
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
@ GetFile
Select a single file.
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
A bar for displaying non-blocking messages to the user.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
QWidget * widget()
Access the widget managed by this wrapper.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QgsPropertyCollection mPropertyCollection
Data defined property collection.
QVariantMap config() const
Returns the whole config.
@ DocumentViewerContent
Document type for external resource.
@ StorageUrl
Storage URL for external resource.
@ RootPath
Root path for external resource.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511