QGIS API Documentation  2.14.0-Essen
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  return mLineEdit || mLabel || mQgsWidget;
60 }
61 
63 {
64  return new QgsExternalResourceWidget( parent );
65 }
66 
68 {
69  mLineEdit = qobject_cast<QLineEdit*>( editor );
70  mLabel = qobject_cast<QLabel*>( editor );
71  mQgsWidget = qobject_cast<QgsExternalResourceWidget*>( editor );
72 
73  if ( mLineEdit )
74  {
75  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
76  if ( fle )
77  {
78  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
79  }
80  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
81  }
82 
83  if ( mQgsWidget )
84  {
85  mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
86  if ( config().contains( "UseLink" ) )
87  {
88  mQgsWidget->fileWidget()->setUseLink( config( "UseLink" ).toBool() );
89  }
90  if ( config().contains( "FullUrl" ) )
91  {
92  mQgsWidget->fileWidget()->setFullUrl( config( "FullUrl" ).toBool() );
93  }
94  if ( config().contains( "DefaultRoot" ) )
95  {
96  mQgsWidget->fileWidget()->setDefaultRoot( config( "DefaultRoot" ).toString() );
97  }
98  if ( config().contains( "StorageMode" ) )
99  {
100  mQgsWidget->fileWidget()->setStorageMode(( QgsFileWidget::StorageMode )config( "StorageMode" ).toInt() );
101  }
102  if ( config().contains( "RelativeStorage" ) )
103  {
104  mQgsWidget->fileWidget()->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
105  }
106  if ( config().contains( "FileWidget" ) )
107  {
108  mQgsWidget->setFileWidgetVisible( config( "FileWidget" ).toBool() );
109  }
110  if ( config().contains( "FileWidgetButton" ) )
111  {
112  mQgsWidget->fileWidget()->setFileWidgetButtonVisible( config( "FileWidgetButton" ).toBool() );
113  }
114  if ( config().contains( "DocumentViewer" ) )
115  {
116  mQgsWidget->setDocumentViewerContent(( QgsExternalResourceWidget::DocumentViewerContent )config( "DocumentViewer" ).toInt() );
117  }
118  if ( config().contains( "FileWidgetFilter" ) )
119  {
120  mQgsWidget->fileWidget()->setFilter( config( "FileWidgetFilter" ).toString() );
121  }
122  }
123 }
124 
126 {
127  if ( mLineEdit )
128  {
129  if ( value.isNull() )
130  {
131  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
132  }
133  else
134  {
135  mLineEdit->setText( value.toString() );
136  }
137  }
138 
139  if ( mLabel )
140  {
141  mLabel->setText( value.toString() ) ;
142  valueChanged( value.toString() ); // emit signal that value has changed, do not do it for other widgets
143  }
144 
145  if ( mQgsWidget )
146  {
147  if ( value.isNull() )
148  {
149  mQgsWidget->setDocumentPath( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
150  }
151  else
152  {
153  mQgsWidget->setDocumentPath( value.toString() );
154  }
155  }
156 
157 }
158 
160 {
161  if ( mLineEdit )
162  mLineEdit->setReadOnly( !enabled );
163 
164  if ( mQgsWidget )
165  mQgsWidget->setReadOnly( !enabled );
166 }
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void valueChanged()
Will call the value() method to determine the emitted value.
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.
QgsField field() const
Access the field.
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
Lineedit with builtin clear button.
void setText(const QString &)
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
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
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...
QgsEditorWidgetConfig config() const
Returns the whole config.
void setValue(const QVariant &value) override
void setNullValue(const QString &nullValue)
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