QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
6  email : [email protected]
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 <QPushButton>
19 #include <QSettings>
20 #include <QLabel>
21 
22 #include "qgsproject.h"
24 #include "qgsfilterlineedit.h"
25 #include "qgsapplication.h"
27 
28 
29 QgsExternalResourceWidgetWrapper::QgsExternalResourceWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
30  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
31 
32 {
33 }
34 
36 {
37  if ( mQgsWidget )
38  {
39  return mQgsWidget->documentPath( field().type() );
40  }
41 
42  if ( mLineEdit )
43  {
44  if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QgsApplication::nullRepresentation() )
45  {
46  return QVariant( field().type() );
47  }
48  else
49  {
50  return mLineEdit->text();
51  }
52  }
53 
54  return QVariant( field().type() );
55 }
56 
58 {
59  if ( mLineEdit )
60  {
61  whileBlocking( mLineEdit )->clear();
62  }
63 
64  if ( mLabel )
65  {
66  mLabel->clear();
67  }
68 
69  if ( mQgsWidget )
70  {
71  whileBlocking( mQgsWidget )->setDocumentPath( QString() );
72  }
73 }
74 
76 {
77  return mLineEdit || mLabel || mQgsWidget;
78 }
79 
80 void QgsExternalResourceWidgetWrapper::updateProperties( const QgsFeature &feature )
81 {
82  if ( mQgsWidget && mPropertyCollection.hasActiveProperties() )
83  {
85  expressionContext.setFeature( feature );
86  bool ok = false;
87 
89  {
90  QString path = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::RootPath, expressionContext, QString(), &ok );
91  if ( ok )
92  {
93  mQgsWidget->setDefaultRoot( path );
94  }
95  }
97  {
98  QString dvcString = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::DocumentViewerContent, expressionContext, QStringLiteral( "NoContent" ), &ok );
99  if ( ok )
100  {
102  if ( dvcString.compare( QLatin1String( "image" ), Qt::CaseInsensitive ) == 0 )
103  {
105  }
106  else if ( dvcString.compare( QLatin1String( "web" ), Qt::CaseInsensitive ) == 0 )
107  {
109  }
110  else
111  {
113  }
114  mQgsWidget->setDocumentViewerContent( dvc );
115  }
116  }
117  }
118 }
119 
121 {
122  updateProperties( feature );
124 }
125 
127 {
128  mForm = qobject_cast<QgsAttributeForm *>( parent );
129 
130  if ( mForm )
132 
133  return new QgsExternalResourceWidget( parent );
134 }
135 
137 {
138  mLineEdit = qobject_cast<QLineEdit *>( editor );
139  mLabel = qobject_cast<QLabel *>( editor );
140  mQgsWidget = qobject_cast<QgsExternalResourceWidget *>( editor );
141 
142  if ( mLineEdit )
143  {
144  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
145  if ( fle )
146  {
148  }
149  }
150  else
151  mLineEdit = editor->findChild<QLineEdit *>();
152 
153  if ( mQgsWidget )
154  {
156 
157  QVariantMap cfg = config();
158 
159  if ( cfg.contains( QStringLiteral( "UseLink" ) ) )
160  {
161  mQgsWidget->fileWidget()->setUseLink( cfg.value( QStringLiteral( "UseLink" ) ).toBool() );
162  }
163  if ( cfg.contains( QStringLiteral( "FullUrl" ) ) )
164  {
165  mQgsWidget->fileWidget()->setFullUrl( cfg.value( QStringLiteral( "FullUrl" ) ).toBool() );
166  }
167 
168  mPropertyCollection.loadVariant( cfg.value( QStringLiteral( "PropertyCollection" ) ), propertyDefinitions() );
170  {
171  mQgsWidget->setDefaultRoot( cfg.value( QStringLiteral( "DefaultRoot" ) ).toString() );
172  }
173  if ( cfg.contains( QStringLiteral( "StorageMode" ) ) )
174  {
175  mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode )cfg.value( QStringLiteral( "StorageMode" ) ).toInt() );
176  }
177  if ( cfg.contains( QStringLiteral( "RelativeStorage" ) ) )
178  {
179  mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage )cfg.value( QStringLiteral( "RelativeStorage" ) ).toInt() );
180  }
181  if ( cfg.contains( QStringLiteral( "FileWidget" ) ) )
182  {
183  mQgsWidget->setFileWidgetVisible( cfg.value( QStringLiteral( "FileWidget" ) ).toBool() );
184  }
185  if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
186  {
187  mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
188  }
189  if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
190  {
191  mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent )cfg.value( QStringLiteral( "DocumentViewer" ) ).toInt() );
192  }
193  if ( cfg.contains( QStringLiteral( "FileWidgetFilter" ) ) )
194  {
195  mQgsWidget->fileWidget()->setFilter( cfg.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
196  }
197  if ( cfg.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
198  {
199  mQgsWidget->setDocumentViewerHeight( cfg.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt( ) );
200  }
201  if ( cfg.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
202  {
203  mQgsWidget->setDocumentViewerWidth( cfg.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt( ) );
204  }
205  }
206 
207  if ( mLineEdit )
208  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value )
209  {
211  emit valueChanged( value );
213  emit valuesChanged( value );
214  } );
215 
216 }
217 
218 void QgsExternalResourceWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
219 {
220  if ( mLineEdit )
221  {
222  if ( value.isNull() )
223  {
224  mLineEdit->setText( QgsApplication::nullRepresentation() );
225  }
226  else
227  {
228  mLineEdit->setText( value.toString() );
229  }
230  }
231 
232  if ( mLabel )
233  {
234  mLabel->setText( value.toString() );
236  emit valueChanged( value.toString() );
238  emit valuesChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
239  }
240 
241  if ( mQgsWidget )
242  {
243  if ( value.isNull() )
244  {
246  }
247  else
248  {
249  mQgsWidget->setDocumentPath( value.toString() );
250  }
251  }
252 
253 }
254 
256 {
257  if ( mLineEdit )
258  mLineEdit->setReadOnly( !enabled );
259 
260  if ( mQgsWidget )
261  mQgsWidget->setReadOnly( !enabled );
262 }
263 
264 void QgsExternalResourceWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
265 {
266  Q_UNUSED( newValue );
267  if ( attributeChanged )
268  {
271 
272  if ( documentViewerContentExp.referencedColumns().contains( attribute ) ||
273  rootPathExp.referencedColumns().contains( attribute ) )
274  {
275  QgsFeature feature = mForm->currentFormFeature();
276  updateProperties( feature );
277  }
278  }
279 }
280 
281 void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus()
282 {
283  if ( mLineEdit )
284  {
285  if ( !constraintResultVisible() )
286  {
287  widget()->setStyleSheet( QString() );
288  }
289  else
290  {
291  switch ( constraintResult() )
292  {
294  mLineEdit->setStyleSheet( QString() );
295  break;
296 
298  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
299  break;
300 
302  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
303  break;
304  }
305  }
306  }
307 }
QgsExternalResourceWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsexternalresourcewidgetwrapper.cpp:136
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsexpressioncontextutils.h
QgsWidgetWrapper::DocumentViewerContent
@ DocumentViewerContent
Document type for external resource.
Definition: qgswidgetwrapper.h:79
QgsExternalResourceWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsexternalresourcewidgetwrapper.cpp:75
QgsEditorWidgetWrapper::ConstraintResultFailHard
@ ConstraintResultFailHard
Widget failed at least one hard (enforced) constraint.
Definition: qgseditorwidgetwrapper.h:63
QgsExpression::referencedColumns
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
Definition: qgsexpression.cpp:217
QgsExternalResourceWidgetWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Definition: qgsexternalresourcewidgetwrapper.cpp:120
QgsExternalResourceWidget::setDefaultRoot
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
Definition: qgsexternalresourcewidget.cpp:194
QgsEditorWidgetWrapper::constraintResult
ConstraintResult constraintResult
Definition: qgseditorwidgetwrapper.h:52
qgsfilterlineedit.h
QgsFileWidget::GetFile
@ GetFile
Select a single file.
Definition: qgsfilewidget.h:66
QgsFilterLineEdit
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
Definition: qgsfilterlineedit.h:40
QgsPropertyCollection::loadVariant
bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
Definition: qgspropertycollection.cpp:341
QgsExternalResourceWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsexternalresourcewidgetwrapper.cpp:126
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:307
QgsExternalResourceWidgetWrapper::showIndeterminateState
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Definition: qgsexternalresourcewidgetwrapper.cpp:57
QgsFileWidget::setFileWidgetButtonVisible
void setFileWidgetButtonVisible(bool visible)
determines if the tool button is shown
Definition: qgsfilewidget.cpp:145
QgsWidgetWrapper::RootPath
@ RootPath
Root path for external resource.
Definition: qgswidgetwrapper.h:78
QgsEditorWidgetWrapper::constraintResultVisible
bool constraintResultVisible
Definition: qgseditorwidgetwrapper.h:51
qgsexternalresourcewidgetwrapper.h
QgsWidgetWrapper::widget
QWidget * widget()
Access the widget managed by this wrapper.
Definition: qgswidgetwrapper.cpp:46
QgsExternalResourceWidget
Widget to display file path with a push button for an "open file" dialog It can also be used to displ...
Definition: qgsexternalresourcewidget.h:47
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:48
QgsPropertyCollection::property
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
Definition: qgspropertycollection.cpp:214
QgsProperty::expressionString
QString expressionString() const
Returns the expression used for the property value.
Definition: qgsproperty.cpp:323
QgsExternalResourceWidget::NoContent
@ NoContent
Definition: qgsexternalresourcewidget.h:69
QgsExternalResourceWidget::DocumentViewerContent
DocumentViewerContent
Definition: qgsexternalresourcewidget.h:68
qgsapplication.h
QgsFileWidget::setUseLink
void setUseLink(bool useLink)
determines if the file path will be shown as a link
Definition: qgsfilewidget.cpp:181
QgsEditorWidgetWrapper::valueChanged
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:797
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:130
QgsEditorWidgetWrapper::ConstraintResultPass
@ ConstraintResultPass
Widget passed constraints successfully.
Definition: qgseditorwidgetwrapper.h:62
QgsExternalResourceWidget::setRelativeStorage
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path.
Definition: qgsexternalresourcewidget.cpp:205
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:124
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:61
QgsAttributeForm::currentFormFeature
QgsFeature currentFormFeature() const
Returns the feature that is currently displayed in the form with all the changes received on editing ...
Definition: qgsattributeform.h:84
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:91
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsExternalResourceWidget::setFileWidgetVisible
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
Definition: qgsexternalresourcewidget.cpp:89
QgsPropertyCollection::hasActiveProperties
bool hasActiveProperties() const override
Returns true if the collection has any active properties, or false if all properties within the colle...
Definition: qgspropertycollection.cpp:304
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1851
QgsExternalResourceWidgetWrapper::QgsExternalResourceWidgetWrapper
QgsExternalResourceWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsExternalResourceWidgetWrapper.
Definition: qgsexternalresourcewidgetwrapper.cpp:29
qgsexternalresourcewidget.h
QgsEditorWidgetWrapper::field
QgsField field() const
Access the field.
Definition: qgseditorwidgetwrapper.cpp:39
QgsExternalResourceWidgetWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgsexternalresourcewidgetwrapper.cpp:35
QgsExternalResourceWidget::setDocumentViewerHeight
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
Definition: qgsexternalresourcewidget.cpp:113
QgsExternalResourceWidgetWrapper::widgetValueChanged
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.
Definition: qgsexternalresourcewidgetwrapper.cpp:264
QgsAbstractPropertyCollection::valueAsString
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.
Definition: qgspropertycollection.cpp:42
QgsFileWidget::RelativeStorage
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
QgsWidgetWrapper::propertyDefinitions
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
Definition: qgswidgetwrapper.cpp:22
QgsEditorWidgetWrapper::valuesChanged
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:81
QgsExternalResourceWidget::Image
@ Image
Definition: qgsexternalresourcewidget.h:70
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsExternalResourceWidget::Web
@ Web
Definition: qgsexternalresourcewidget.h:71
QgsFileWidget::StorageMode
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
Definition: qgsfilewidget.h:65
QgsEditorWidgetWrapper::ConstraintResultFailSoft
@ ConstraintResultFailSoft
Widget failed at least one soft (non-enforced) constraint.
Definition: qgseditorwidgetwrapper.h:64
QgsWidgetWrapper::mPropertyCollection
QgsPropertyCollection mPropertyCollection
Data defined property collection.
Definition: qgswidgetwrapper.h:245
QgsFileWidget::setStorageMode
void setStorageMode(QgsFileWidget::StorageMode storageMode)
determines the storage mode (i.e. file or directory)
Definition: qgsfilewidget.cpp:215
QgsFileWidget::setFilter
void setFilter(const QString &filter)
setFilter sets the filter used by the model to filters.
Definition: qgsfilewidget.cpp:124
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:74
QgsAttributeForm::widgetValueChanged
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
QgsFilterLineEdit::setNullValue
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
Definition: qgsfilterlineedit.h:115
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFileWidget::setFullUrl
void setFullUrl(bool fullUrl)
determines if the links shows the full path or not
Definition: qgsfilewidget.cpp:195
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:100
QgsExternalResourceWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsexternalresourcewidgetwrapper.cpp:255
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsPropertyCollection::isActive
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Definition: qgspropertycollection.cpp:268
QgsEditorWidgetWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
Definition: qgseditorwidgetwrapper.cpp:69
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
QgsExternalResourceWidget::fileWidget
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
Definition: qgsexternalresourcewidget.cpp:79
qgsproject.h