QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
24 #include "qgsfilterlineedit.h"
25 
26 
28  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
29  , mLineEdit( nullptr )
30  , mLabel( nullptr )
31  , mQgsWidget( nullptr )
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() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
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  return new QgsExternalResourceWidget( parent );
83 }
84 
86 {
87  mLineEdit = qobject_cast<QLineEdit*>( editor );
88  mLabel = qobject_cast<QLabel*>( editor );
89  mQgsWidget = qobject_cast<QgsExternalResourceWidget*>( editor );
90 
91  if ( mLineEdit )
92  {
93  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
94  if ( fle )
95  {
96  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
97  }
98  }
99  else
100  mLineEdit = editor->findChild<QLineEdit*>();
101 
102  if ( mQgsWidget )
103  {
104  mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
105  if ( config().contains( "UseLink" ) )
106  {
107  mQgsWidget->fileWidget()->setUseLink( config( "UseLink" ).toBool() );
108  }
109  if ( config().contains( "FullUrl" ) )
110  {
111  mQgsWidget->fileWidget()->setFullUrl( config( "FullUrl" ).toBool() );
112  }
113  if ( config().contains( "DefaultRoot" ) )
114  {
115  mQgsWidget->setDefaultRoot( config( "DefaultRoot" ).toString() );
116  }
117  if ( config().contains( "StorageMode" ) )
118  {
119  mQgsWidget->fileWidget()->setStorageMode(( QgsFileWidget::StorageMode )config( "StorageMode" ).toInt() );
120  }
121  if ( config().contains( "RelativeStorage" ) )
122  {
123  mQgsWidget->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
124  }
125  if ( config().contains( "FileWidget" ) )
126  {
127  mQgsWidget->setFileWidgetVisible( config( "FileWidget" ).toBool() );
128  }
129  if ( config().contains( "FileWidgetButton" ) )
130  {
131  mQgsWidget->fileWidget()->setFileWidgetButtonVisible( config( "FileWidgetButton" ).toBool() );
132  }
133  if ( config().contains( "DocumentViewer" ) )
134  {
135  mQgsWidget->setDocumentViewerContent(( QgsExternalResourceWidget::DocumentViewerContent )config( "DocumentViewer" ).toInt() );
136  }
137  if ( config().contains( "FileWidgetFilter" ) )
138  {
139  mQgsWidget->fileWidget()->setFilter( config( "FileWidgetFilter" ).toString() );
140  }
141  }
142 
143  if ( mLineEdit )
144  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
145 
146 }
147 
149 {
150  if ( mLineEdit )
151  {
152  if ( value.isNull() )
153  {
154  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
155  }
156  else
157  {
158  mLineEdit->setText( value.toString() );
159  }
160  }
161 
162  if ( mLabel )
163  {
164  mLabel->setText( value.toString() ) ;
165  valueChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
166  }
167 
168  if ( mQgsWidget )
169  {
170  if ( value.isNull() )
171  {
172  mQgsWidget->setDocumentPath( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
173  }
174  else
175  {
176  mQgsWidget->setDocumentPath( value.toString() );
177  }
178  }
179 
180 }
181 
183 {
184  if ( mLineEdit )
185  mLineEdit->setReadOnly( !enabled );
186 
187  if ( mQgsWidget )
188  mQgsWidget->setReadOnly( !enabled );
189 }
190 
191 void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus( bool constraintValid )
192 {
193  if ( mLineEdit )
194  {
195  if ( constraintValid )
196  mLineEdit->setStyleSheet( QString() );
197  else
198  mLineEdit->setStyleSheet( "QgsFilterLineEdit { background-color: #dd7777; }" );
199  }
200 }
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setStyleSheet(const QString &styleSheet)
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
QgsExternalResourceWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
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 clear()
void setDocumentPath(const QVariant &documentPath)
void setReadOnly(bool)
QVariant value() const override
Will be used to access the widget&#39;s value.
bool isNull() const
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setText(const QString &)
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:47
QVariant value(const QString &key, const QVariant &defaultValue) const
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:333
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Widget to display file path with a push button for an "open file" dialog It can also be used to displ...
void setValue(const QVariant &value) override
QgsEditorWidgetConfig config() const
Returns the whole config.
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
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString toString() const
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:56
T findChild(const QString &name) const