QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
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 
120 }
121 
123 {
124  return new QgsExternalResourceWidget( parent );
125 }
126 
128 {
129  mLineEdit = qobject_cast<QLineEdit *>( editor );
130  mLabel = qobject_cast<QLabel *>( editor );
131  mQgsWidget = qobject_cast<QgsExternalResourceWidget *>( editor );
132 
133  if ( mLineEdit )
134  {
135  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
136  if ( fle )
137  {
139  }
140  }
141  else
142  mLineEdit = editor->findChild<QLineEdit *>();
143 
144  if ( mQgsWidget )
145  {
147 
148  QVariantMap cfg = config();
149 
150  if ( cfg.contains( QStringLiteral( "UseLink" ) ) )
151  {
152  mQgsWidget->fileWidget()->setUseLink( cfg.value( QStringLiteral( "UseLink" ) ).toBool() );
153  }
154  if ( cfg.contains( QStringLiteral( "FullUrl" ) ) )
155  {
156  mQgsWidget->fileWidget()->setFullUrl( cfg.value( QStringLiteral( "FullUrl" ) ).toBool() );
157  }
158 
159  mPropertyCollection.loadVariant( cfg.value( QStringLiteral( "PropertyCollection" ) ), propertyDefinitions() );
161  {
162  mQgsWidget->setDefaultRoot( cfg.value( QStringLiteral( "DefaultRoot" ) ).toString() );
163  }
164  if ( cfg.contains( QStringLiteral( "StorageMode" ) ) )
165  {
166  mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode )cfg.value( QStringLiteral( "StorageMode" ) ).toInt() );
167  }
168  if ( cfg.contains( QStringLiteral( "RelativeStorage" ) ) )
169  {
170  mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage )cfg.value( QStringLiteral( "RelativeStorage" ) ).toInt() );
171  }
172  if ( cfg.contains( QStringLiteral( "FileWidget" ) ) )
173  {
174  mQgsWidget->setFileWidgetVisible( cfg.value( QStringLiteral( "FileWidget" ) ).toBool() );
175  }
176  if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
177  {
178  mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
179  }
180  if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
181  {
182  mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent )cfg.value( QStringLiteral( "DocumentViewer" ) ).toInt() );
183  }
184  if ( cfg.contains( QStringLiteral( "FileWidgetFilter" ) ) )
185  {
186  mQgsWidget->fileWidget()->setFilter( cfg.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
187  }
188  if ( cfg.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
189  {
190  mQgsWidget->setDocumentViewerHeight( cfg.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt( ) );
191  }
192  if ( cfg.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
193  {
194  mQgsWidget->setDocumentViewerWidth( cfg.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt( ) );
195  }
196  }
197 
198  if ( mLineEdit )
199  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value )
200  {
202  emit valueChanged( value );
204  emit valuesChanged( value );
205  } );
206 
207 }
208 
209 void QgsExternalResourceWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
210 {
211  if ( mLineEdit )
212  {
213  if ( value.isNull() )
214  {
215  mLineEdit->setText( QgsApplication::nullRepresentation() );
216  }
217  else
218  {
219  mLineEdit->setText( value.toString() );
220  }
221  }
222 
223  if ( mLabel )
224  {
225  mLabel->setText( value.toString() );
227  emit valueChanged( value.toString() );
229  emit valuesChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
230  }
231 
232  if ( mQgsWidget )
233  {
234  if ( value.isNull() )
235  {
237  }
238  else
239  {
240  mQgsWidget->setDocumentPath( value.toString() );
241  }
242  }
243 
244 }
245 
247 {
248  if ( mLineEdit )
249  mLineEdit->setReadOnly( !enabled );
250 
251  if ( mQgsWidget )
252  mQgsWidget->setReadOnly( !enabled );
253 }
254 
255 void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus()
256 {
257  if ( mLineEdit )
258  {
259  if ( !constraintResultVisible() )
260  {
261  widget()->setStyleSheet( QString() );
262  }
263  else
264  {
265  switch ( constraintResult() )
266  {
268  mLineEdit->setStyleSheet( QString() );
269  break;
270 
272  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
273  break;
274 
276  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
277  break;
278  }
279  }
280  }
281 }
QgsExternalResourceWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsexternalresourcewidgetwrapper.cpp:127
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
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
QgsExternalResourceWidgetWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Definition: qgsexternalresourcewidgetwrapper.cpp:80
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:191
qgsfilterlineedit.h
QgsFileWidget::GetFile
@ GetFile
Select a single file.
Definition: qgsfilewidget.h:66
QgsFilterLineEdit
Definition: qgsfilterlineedit.h:39
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:331
QgsExternalResourceWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsexternalresourcewidgetwrapper.cpp:122
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:306
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:151
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
Definition: qgsexternalresourcewidget.h:46
QgsEditorWidgetWrapper
Definition: qgseditorwidgetwrapper.h:47
QgsExternalResourceWidget::NoContent
@ NoContent
Definition: qgsexternalresourcewidget.h:69
QgsExternalResourceWidget::DocumentViewerContent
DocumentViewerContent
Definition: qgsexternalresourcewidget.h:67
qgsapplication.h
QgsFileWidget::setUseLink
void setUseLink(bool useLink)
determines if the file path will be shown as a link
Definition: qgsfilewidget.cpp:187
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:752
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:127
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:202
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:121
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:60
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:88
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:294
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1802
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:110
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:76
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
Definition: qgsvectorlayer.h:385
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:64
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:221
QgsFileWidget::setFilter
void setFilter(const QString &filter)
setFilter sets the filter used by the model to filters.
Definition: qgsfilewidget.cpp:130
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:73
QgsFilterLineEdit::setNullValue
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
Definition: qgsfilterlineedit.h:115
QgsFeature
Definition: qgsfeature.h:55
QgsEditorWidgetWrapper::constraintResult
ConstraintResult constraintResult
Definition: qgseditorwidgetwrapper.h:52
QgsFileWidget::setFullUrl
void setFullUrl(bool fullUrl)
determines if the links shows the full path or not
Definition: qgsfilewidget.cpp:201
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:99
QgsExternalResourceWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsexternalresourcewidgetwrapper.cpp:246
QgsPropertyCollection::isActive
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Definition: qgspropertycollection.cpp:258
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:751
QgsExternalResourceWidget::fileWidget
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
Definition: qgsexternalresourcewidget.cpp:78
qgsproject.h
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:521