QGIS API Documentation  2.15.0-Master (972fc9f)
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 
26  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
27  , mLineEdit( nullptr )
28  , mPushButton( nullptr )
29  , mLabel( nullptr )
30 {
31 }
32 
34 {
36 
37  if ( mLineEdit )
38  {
39  if ( mLineEdit->text() == QSettings().value( "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 
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 
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( "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( "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( bool constraintValid )
156 {
157  if ( mLineEdit )
158  {
159  if ( constraintValid )
160  mLineEdit->setStyleSheet( QString() );
161  else
162  {
163  mLineEdit->setStyleSheet( "QgsFilterLineEdit { background-color: #dd7777; }" );
164  }
165  }
166 }
void setStyleSheet(const QString &styleSheet)
QString toNativeSeparators(const QString &pathName)
QString relativeFilePath(const QString &fileName) const
void valueChanged()
Will call the value() method to determine the emitted value.
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
Manages an editor widget Widget and wrapper share the same parent.
QgsField field() const
Access the field.
void clear()
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const
void setLayout(QLayout *layout)
bool isNull() const
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.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Lineedit with builtin clear button.
void setValue(const QVariant &value) override
void setMargin(int margin)
QString cleanPath(const QString &path)
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:332
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
QVariant value() const override
Will be used to access the widget&#39;s value.
void setAutoFillBackground(bool enabled)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
void setBackgroundRole(QPalette::ColorRole role)
void setNullValue(const QString &nullValue)
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.
QgsFileNameWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
QString toString() const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89
T findChild(const QString &name) const