QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
255 {
256  if ( mLineEdit )
257  mLineEdit->setReadOnly( !enabled );
258 
259  if ( mQgsWidget )
260  mQgsWidget->setReadOnly( !enabled );
261 }
262 
263 void QgsExternalResourceWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
264 {
265  Q_UNUSED( newValue );
266  if ( attributeChanged )
267  {
270 
271  if ( documentViewerContentExp.referencedColumns().contains( attribute ) ||
272  rootPathExp.referencedColumns().contains( attribute ) )
273  {
274  QgsFeature feature = mForm->currentFormFeature();
275  updateProperties( feature );
276  }
277  }
278 }
279 
280 void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus()
281 {
282  if ( mLineEdit )
283  {
284  if ( !constraintResultVisible() )
285  {
286  widget()->setStyleSheet( QString() );
287  }
288  else
289  {
290  switch ( constraintResult() )
291  {
293  mLineEdit->setStyleSheet( QString() );
294  break;
295 
297  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
298  break;
299 
301  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
302  break;
303  }
304  }
305  }
306 }
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()
This string is used to represent the value NULL throughout QGIS.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
QgsFeature currentFormFeature() const
Returns the feature that is currently displayed in the form with all the changes received on editing ...
Manages an editor widget Widget and wrapper share the same parent.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
ConstraintResult constraintResult
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 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...
Class for parsing and evaluation of expressions (formerly called "search strings").
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
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.
QgsExternalResourceWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsExternalResourceWidgetWrapper.
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.
Widget to display file path with a push button for an "open file" dialog It can also be used to displ...
void setDocumentPath(const QVariant &documentPath)
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path.
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
void setReadOnly(bool readOnly)
defines if the widget is readonly
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
Definition: qgsfilewidget.h:65
@ GetFile
Select a single file.
Definition: qgsfilewidget.h:66
void setFullUrl(bool fullUrl)
determines if the links shows the full path or not
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
void setStorageMode(QgsFileWidget::StorageMode storageMode)
determines the storage mode (i.e. file or directory)
void setUseLink(bool useLink)
determines if the file path will be shown as a link
void setFileWidgetButtonVisible(bool visible)
determines if the tool button is shown
void setFilter(const QString &filter)
setFilter sets the filter used by the model to filters.
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.
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
bool hasActiveProperties() const override
Returns true if the collection has any active properties, or false if all properties within the colle...
QString expressionString() const
Returns the expression used for the property value.
Represents a vector layer which manages a vector based data sets.
QWidget * widget()
Access the widget managed by this wrapper.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
@ DocumentViewerContent
Document type for external resource.
@ RootPath
Root path for external resource.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QgsPropertyCollection mPropertyCollection
Data defined property collection.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:1080
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1079
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:537