QGIS API Documentation  3.0.2-Girona (307d082)
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 
26 
27 QgsExternalResourceWidgetWrapper::QgsExternalResourceWidgetWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
28  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
29 
30 {
31 }
32 
34 {
35  if ( mQgsWidget )
36  {
37  return mQgsWidget->documentPath( field().type() );
38  }
39 
40  if ( mLineEdit )
41  {
42  if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QgsApplication::nullRepresentation() )
43  {
44  return QVariant( field().type() );
45  }
46  else
47  {
48  return mLineEdit->text();
49  }
50  }
51 
52  return QVariant( field().type() );
53 }
54 
56 {
57  if ( mLineEdit )
58  {
59  whileBlocking( mLineEdit )->clear();
60  }
61 
62  if ( mLabel )
63  {
64  mLabel->clear();
65  }
66 
67  if ( mQgsWidget )
68  {
69  whileBlocking( mQgsWidget )->setDocumentPath( QString() );
70  }
71 }
72 
74 {
75  return mLineEdit || mLabel || mQgsWidget;
76 }
77 
79 {
80  if ( mQgsWidget && mPropertyCollection.hasActiveProperties() )
81  {
83  expressionContext.setFeature( feature );
84  bool ok = false;
85 
87  {
88  QString path = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::RootPath, expressionContext, QString(), &ok );
89  if ( ok )
90  {
91  mQgsWidget->setDefaultRoot( path );
92  }
93  }
95  {
96  QString dvcString = mPropertyCollection.valueAsString( QgsEditorWidgetWrapper::DocumentViewerContent, expressionContext, QStringLiteral( "NoContent" ), &ok );
97  if ( ok )
98  {
100  if ( dvcString.compare( QLatin1String( "image" ), Qt::CaseInsensitive ) == 0 )
101  {
103  }
104  else if ( dvcString.compare( QLatin1String( "web" ), Qt::CaseInsensitive ) == 0 )
105  {
107  }
108  else
109  {
111  }
112  mQgsWidget->setDocumentViewerContent( dvc );
113  }
114  }
115  }
116 
118 }
119 
121 {
122  return new QgsExternalResourceWidget( parent );
123 }
124 
126 {
127  mLineEdit = qobject_cast<QLineEdit *>( editor );
128  mLabel = qobject_cast<QLabel *>( editor );
129  mQgsWidget = qobject_cast<QgsExternalResourceWidget *>( editor );
130 
131  if ( mLineEdit )
132  {
133  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
134  if ( fle )
135  {
137  }
138  }
139  else
140  mLineEdit = editor->findChild<QLineEdit *>();
141 
142  if ( mQgsWidget )
143  {
144  mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
145 
146  QVariantMap cfg = config();
147 
148  if ( cfg.contains( QStringLiteral( "UseLink" ) ) )
149  {
150  mQgsWidget->fileWidget()->setUseLink( cfg.value( QStringLiteral( "UseLink" ) ).toBool() );
151  }
152  if ( cfg.contains( QStringLiteral( "FullUrl" ) ) )
153  {
154  mQgsWidget->fileWidget()->setFullUrl( cfg.value( QStringLiteral( "FullUrl" ) ).toBool() );
155  }
156 
157  mPropertyCollection.loadVariant( cfg.value( QStringLiteral( "PropertyCollection" ) ), propertyDefinitions() );
159  {
160  mQgsWidget->setDefaultRoot( cfg.value( QStringLiteral( "DefaultRoot" ) ).toString() );
161  }
162  if ( cfg.contains( QStringLiteral( "StorageMode" ) ) )
163  {
164  mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode )cfg.value( QStringLiteral( "StorageMode" ) ).toInt() );
165  }
166  if ( cfg.contains( QStringLiteral( "RelativeStorage" ) ) )
167  {
168  mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage )cfg.value( QStringLiteral( "RelativeStorage" ) ).toInt() );
169  }
170  if ( cfg.contains( QStringLiteral( "FileWidget" ) ) )
171  {
172  mQgsWidget->setFileWidgetVisible( cfg.value( QStringLiteral( "FileWidget" ) ).toBool() );
173  }
174  if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
175  {
176  mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
177  }
178  if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
179  {
180  mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent )cfg.value( QStringLiteral( "DocumentViewer" ) ).toInt() );
181  }
182  if ( cfg.contains( QStringLiteral( "FileWidgetFilter" ) ) )
183  {
184  mQgsWidget->fileWidget()->setFilter( cfg.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
185  }
186  }
187 
188  if ( mLineEdit )
189  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); } );
190 
191 }
192 
194 {
195  if ( mLineEdit )
196  {
197  if ( value.isNull() )
198  {
199  mLineEdit->setText( QgsApplication::nullRepresentation() );
200  }
201  else
202  {
203  mLineEdit->setText( value.toString() );
204  }
205  }
206 
207  if ( mLabel )
208  {
209  mLabel->setText( value.toString() );
210  emit valueChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
211  }
212 
213  if ( mQgsWidget )
214  {
215  if ( value.isNull() )
216  {
218  }
219  else
220  {
221  mQgsWidget->setDocumentPath( value.toString() );
222  }
223  }
224 
225 }
226 
228 {
229  if ( mLineEdit )
230  mLineEdit->setReadOnly( !enabled );
231 
232  if ( mQgsWidget )
233  mQgsWidget->setReadOnly( !enabled );
234 }
235 
236 void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus()
237 {
238  if ( mLineEdit )
239  {
240  if ( !constraintResultVisible() )
241  {
242  widget()->setStyleSheet( QString() );
243  }
244  else
245  {
246  switch ( constraintResult() )
247  {
249  mLineEdit->setStyleSheet( QString() );
250  break;
251 
253  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
254  break;
255 
257  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
258  break;
259  }
260  }
261  }
262 }
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Widget failed at least one soft (non-enforced) constraint.
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
QgsField field() const
Access the field.
QgsExternalResourceWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Manages an editor widget Widget and wrapper share the same parent.
bool valid() const override
Return true if the widget has been properly initialized.
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly ...
Widget failed at least one hard (enforced) constraint.
QVariantMap config() const
Returns the whole config.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
void setDocumentPath(const QVariant &documentPath)
bool constraintResultVisible() const
Getter of constraintResultVisible Defines if the constraint result should be visualized on the widget...
Root path for external resource.
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
QVariant value() const override
Will be used to access the widget&#39;s value.
QgsPropertyCollection mPropertyCollection
Data defined property collection.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer&#39;s project and layer.
Document type for external resource.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
Definition: qgsfilewidget.h:63
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...
Widget passed constraints successfully.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:224
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
Widget to display file path with a push button for an "open file" dialog It can also be used to displ...
ConstraintResult constraintResult() const
Getter of constraintResult It&#39;s the current result of the constraint on the widget influencing it&#39;s v...
bool hasActiveProperties() const override
Returns true if the collection has any active properties, or false if all properties within the colle...
void setValue(const QVariant &value) override
QWidget * widget()
Access the widget managed by this wrapper.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
void setReadOnly(bool readOnly)
defines if the widget is readonly
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
Represents a vector layer which manages a vector based data sets.
void setFeature(const QgsFeature &feature) override
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:74