QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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#include <QString>
29
30#include "moc_qgsexternalresourcewidgetwrapper.cpp"
31
32using namespace Qt::StringLiterals;
33
35 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
36 , mMessageBar( messageBar )
37{}
38
40{
41 if ( mQgsWidget )
42 {
43 return mQgsWidget->documentPath( field().type() );
44 }
45
46 if ( mLineEdit )
47 {
48 if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QgsApplication::nullRepresentation() )
49 {
51 }
52 else
53 {
54 return mLineEdit->text();
55 }
56 }
57
59}
60
62{
63 if ( mLineEdit )
64 {
65 whileBlocking( mLineEdit )->clear();
66 }
67
68 if ( mLabel )
69 {
70 mLabel->clear();
71 }
72
73 if ( mQgsWidget )
74 {
75 whileBlocking( mQgsWidget )->setDocumentPath( QString() );
76 }
77}
78
80{
81 return mLineEdit || mLabel || mQgsWidget;
82}
83
84void QgsExternalResourceWidgetWrapper::updateProperties( const QgsFeature &feature )
85{
86 if ( mQgsWidget && mPropertyCollection.hasActiveProperties() )
87 {
89 expressionContext.setFeature( feature );
90 bool ok = false;
91
93 {
94 const QString path = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::Property::RootPath, expressionContext, QString(), &ok );
95 if ( ok )
96 {
97 mQgsWidget->setDefaultRoot( path );
98 }
99 }
101 {
102 const QString dvcString = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::Property::DocumentViewerContent, expressionContext, u"NoContent"_s, &ok );
103 if ( ok )
104 {
106 if ( dvcString.compare( "image"_L1, Qt::CaseInsensitive ) == 0 )
107 {
109 }
110 else if ( dvcString.compare( "audio"_L1, Qt::CaseInsensitive ) == 0 )
111 {
113 }
114 else if ( dvcString.compare( "video"_L1, Qt::CaseInsensitive ) == 0 )
115 {
117 }
118 else if ( dvcString.compare( "web"_L1, Qt::CaseInsensitive ) == 0 )
119 {
121 }
122 else
123 {
125 }
126 mQgsWidget->setDocumentViewerContent( dvc );
127 }
128 }
129 }
130}
131
133{
134 updateProperties( feature );
136
137 if ( mQgsWidget )
138 {
140 }
141}
142
144{
145 mForm = qobject_cast<QgsAttributeForm *>( parent );
146
147 if ( mForm )
149
150 return new QgsExternalResourceWidget( parent );
151}
152
154{
155 mLineEdit = qobject_cast<QLineEdit *>( editor );
156 mLabel = qobject_cast<QLabel *>( editor );
157 mQgsWidget = qobject_cast<QgsExternalResourceWidget *>( editor );
158
159 if ( mLineEdit )
160 {
161 QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
162 if ( fle )
163 {
165 }
166 }
167 else
168 mLineEdit = editor->findChild<QLineEdit *>();
169
170 if ( mQgsWidget )
171 {
172 mQgsWidget->setMessageBar( mMessageBar );
173
174 mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
175
176 const QVariantMap cfg = config();
177 mPropertyCollection.loadVariant( cfg.value( u"PropertyCollection"_s ), propertyDefinitions() );
178
179 mQgsWidget->setStorageType( cfg.value( u"StorageType"_s ).toString() );
180 mQgsWidget->setStorageAuthConfigId( cfg.value( u"StorageAuthConfigId"_s ).toString() );
181
182 mQgsWidget->fileWidget()->setStorageUrlExpression(
184 : QgsExpression::quotedValue( cfg.value( u"StorageUrl"_s ).toString() )
185 );
186
188
189 if ( cfg.contains( u"UseLink"_s ) )
190 {
191 mQgsWidget->fileWidget()->setUseLink( cfg.value( u"UseLink"_s ).toBool() );
192 }
193 if ( cfg.contains( u"FullUrl"_s ) )
194 {
195 mQgsWidget->fileWidget()->setFullUrl( cfg.value( u"FullUrl"_s ).toBool() );
196 }
197
199 {
200 mQgsWidget->setDefaultRoot( cfg.value( u"DefaultRoot"_s ).toString() );
201 }
202 if ( cfg.contains( u"StorageMode"_s ) )
203 {
204 mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode ) cfg.value( u"StorageMode"_s ).toInt() );
205 }
206 if ( cfg.contains( u"RelativeStorage"_s ) )
207 {
208 mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage ) cfg.value( u"RelativeStorage"_s ).toInt() );
209 }
210 if ( cfg.contains( u"FileWidget"_s ) )
211 {
212 mQgsWidget->setFileWidgetVisible( cfg.value( u"FileWidget"_s ).toBool() );
213 }
214 if ( cfg.contains( u"FileWidgetButton"_s ) )
215 {
216 mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( u"FileWidgetButton"_s ).toBool() );
217 }
218 if ( cfg.contains( u"DocumentViewer"_s ) )
219 {
220 mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent ) cfg.value( u"DocumentViewer"_s ).toInt() );
221 }
222 if ( cfg.contains( u"FileWidgetFilter"_s ) )
223 {
224 mQgsWidget->fileWidget()->setFilter( cfg.value( u"FileWidgetFilter"_s ).toString() );
225 }
226 if ( cfg.contains( u"DocumentViewerHeight"_s ) )
227 {
228 mQgsWidget->setDocumentViewerHeight( cfg.value( u"DocumentViewerHeight"_s ).toInt() );
229 }
230 if ( cfg.contains( u"DocumentViewerWidth"_s ) )
231 {
232 mQgsWidget->setDocumentViewerWidth( cfg.value( u"DocumentViewerWidth"_s ).toInt() );
233 }
234 }
235
236 if ( mLineEdit )
237 connect( mLineEdit, &QLineEdit::textChanged, this, [this]( const QString &value ) {
239 emit valueChanged( value );
241 emit valuesChanged( value );
242 } );
243}
244
245void QgsExternalResourceWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
246{
247 if ( mLineEdit )
248 {
250 {
251 mLineEdit->setText( QgsApplication::nullRepresentation() );
252 }
253 else
254 {
255 mLineEdit->setText( value.toString() );
256 }
257 }
258
259 if ( mLabel )
260 {
261 mLabel->setText( value.toString() );
263 emit valueChanged( value.toString() );
265 emit valuesChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
266 }
267
268 if ( mQgsWidget )
269 {
271 {
272 mQgsWidget->setDocumentPath( QgsApplication::nullRepresentation() );
273 }
274 else
275 {
276 mQgsWidget->setDocumentPath( value.toString() );
277 }
278 }
279}
280
282{
283 if ( mLineEdit )
284 mLineEdit->setReadOnly( !enabled );
285
286 if ( mQgsWidget )
287 mQgsWidget->setReadOnly( !enabled );
288}
289
290void QgsExternalResourceWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
291{
292 Q_UNUSED( newValue );
293 if ( attributeChanged )
294 {
295 const QgsExpression documentViewerContentExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::Property::DocumentViewerContent ).expressionString() );
296 const QgsExpression rootPathExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::Property::RootPath ).expressionString() );
297
298 if ( documentViewerContentExp.referencedColumns().contains( attribute ) || rootPathExp.referencedColumns().contains( attribute ) )
299 {
300 const QgsFeature feature = mForm->currentFormFeature();
301 updateProperties( feature );
302 }
303 }
304}
305
307{
308 if ( mLineEdit )
309 {
311 {
312 widget()->setStyleSheet( QString() );
313 }
314 else
315 {
316 switch ( constraintResult() )
317 {
319 mLineEdit->setStyleSheet( QString() );
320 break;
321
323 mLineEdit->setStyleSheet( u"QgsFilterLineEdit { background-color: #dd7777; }"_s );
324 break;
325
327 mLineEdit->setStyleSheet( u"QgsFilterLineEdit { background-color: #ffd85d; }"_s );
328 break;
329 }
330 }
331 }
332}
333
335{
336 if ( !mQgsWidget || !layer() )
337 return;
338
339 QgsExpressionContext expressionContext( layer()->createExpressionContext() );
340 expressionContext.setFeature( formFeature() );
342 if ( context().parentFormFeature().isValid() )
343 {
344 expressionContext.appendScope( QgsExpressionContextUtils::parentFormScope( context().parentFormFeature() ) );
345 }
346
347 mQgsWidget->fileWidget()->setExpressionContext( expressionContext );
348}
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:60
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:7504
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7503
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6880