QGIS API Documentation  2.99.0-Master (08ee180)
qgsfilenamewidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfilenamewidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
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 "qgsfilterlineedit.h"
19 #include "qgsproject.h"
20 
21 #include <QFileDialog>
22 #include <QSettings>
23 #include <QGridLayout>
24 
25 QgsFileNameWidgetWrapper::QgsFileNameWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
26  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
27  , mLineEdit( nullptr )
28  , mPushButton( nullptr )
29  , mLabel( nullptr )
30 {
31 }
32 
34 {
35  QVariant value;
36 
37  if ( mLineEdit )
38  {
39  if ( mLineEdit->text() == QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() )
40  value = QVariant( field().type() );
41  else
42  value = mLineEdit->text();
43  }
44 
45  if ( mLabel )
46  value = mLabel->text();
47 
48  return value;
49 }
50 
52 {
53  return mLineEdit || mLabel;
54 }
55 
57 {
58  if ( mLineEdit )
59  {
60  whileBlocking( mLineEdit )->clear();
61  }
62 
63  if ( mLabel )
64  mLabel->clear();
65 }
66 
67 QWidget* QgsFileNameWidgetWrapper::createWidget( QWidget* parent )
68 {
69  QWidget* container = new QWidget( parent );
70  container->setBackgroundRole( QPalette::Window );
71  container->setAutoFillBackground( true );
72 
73  QLineEdit* le = new QgsFilterLineEdit();
74  QPushButton* pbn = new QPushButton( tr( "..." ) );
75  QGridLayout* layout = new QGridLayout();
76 
77  layout->setMargin( 0 );
78  layout->addWidget( le, 0, 0 );
79  layout->addWidget( pbn, 0, 1 );
80 
81  container->setLayout( layout );
82 
83  return container;
84 }
85 
86 void QgsFileNameWidgetWrapper::initWidget( QWidget* editor )
87 {
88  mLineEdit = qobject_cast<QLineEdit*>( editor );
89  if ( !mLineEdit )
90  {
91  mLineEdit = editor->findChild<QLineEdit*>();
92  }
93 
94  mPushButton = editor->findChild<QPushButton*>();
95 
96  if ( mPushButton )
97  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
98 
99  mLabel = qobject_cast<QLabel*>( editor );
100 
101  if ( mLineEdit )
102  {
103  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
104  if ( fle )
105  {
106  fle->setNullValue( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
107  }
108 
109  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
110  }
111 }
112 
114 {
115  if ( mLineEdit )
116  {
117  if ( value.isNull() )
118  mLineEdit->setText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
119  else
120  mLineEdit->setText( value.toString() );
121  }
122 
123  if ( mLabel )
124  mLabel->setText( value.toString() );
125 }
126 
127 void QgsFileNameWidgetWrapper::selectFileName()
128 {
129  QString text;
130 
131  if ( mLineEdit )
132  text = mLineEdit->text();
133 
134  if ( mLabel )
135  text = mLabel->text();
136 
137  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
138 
139  if ( fileName.isNull() )
140  return;
141 
142  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
143  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
144 
145  if ( filePath.startsWith( projPath ) )
146  filePath = QDir( projPath ).relativeFilePath( filePath );
147 
148  if ( mLineEdit )
149  mLineEdit->setText( fileName );
150 
151  if ( mLabel )
152  mLineEdit->setText( fileName );
153 }
154 
155 void QgsFileNameWidgetWrapper::updateConstraintWidgetStatus( ConstraintResult status )
156 {
157  if ( mLineEdit )
158  {
159  switch ( status )
160  {
162  mLineEdit->setStyleSheet( QString() );
163  break;
164 
166  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
167  break;
168 
170  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
171  break;
172  }
173  }
174 }
Widget failed at least one soft (non-enforced) constraint.
void valueChanged()
Will call the value() method to determine the emitted value.
Manages an editor widget Widget and wrapper share the same parent.
QgsField field() const
Access the field.
Widget failed at least one hard (enforced) constraint.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
bool valid() const override
Return true if the widget has been properly initialized.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setValue(const QVariant &value) override
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:176
ConstraintResult
Result of constraint checks.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:348
QVariant value() const override
Will be used to access the widget&#39;s value.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
Represents a vector layer which manages a vector based data sets.
QgsFileNameWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:98