QGIS API Documentation  2.12.0-Lyon
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( NULL )
28  , mPushButton( NULL )
29  , mLabel( NULL )
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  QWidget* container = new QWidget( parent );
59  container->setBackgroundRole( QPalette::Window );
60  container->setAutoFillBackground( true );
61 
62  QLineEdit* le = new QgsFilterLineEdit( container );
63  QPushButton* pbn = new QPushButton( tr( "..." ), container );
64  QGridLayout* layout = new QGridLayout();
65 
66  layout->setMargin( 0 );
67  layout->addWidget( le, 0, 0 );
68  layout->addWidget( pbn, 0, 1 );
69 
70  container->setLayout( layout );
71 
72  return container;
73 }
74 
76 {
77  mLineEdit = qobject_cast<QLineEdit*>( editor );
78  if ( !mLineEdit )
79  {
80  mLineEdit = editor->findChild<QLineEdit*>();
81  }
82 
83  mPushButton = editor->findChild<QPushButton*>();
84 
85  if ( mPushButton )
86  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
87 
88  mLabel = qobject_cast<QLabel*>( editor );
89 
90  if ( mLineEdit )
91  {
92  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
93  if ( fle )
94  {
95  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
96  }
97 
98  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
99  }
100 }
101 
103 {
104  if ( mLineEdit )
105  {
106  if ( value.isNull() )
107  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
108  else
109  mLineEdit->setText( value.toString() );
110  }
111 
112  if ( mLabel )
113  mLabel->setText( value.toString() );
114 }
115 
116 void QgsFileNameWidgetWrapper::selectFileName()
117 {
118  QString text;
119 
120  if ( mLineEdit )
121  text = mLineEdit->text();
122 
123  if ( mLabel )
124  text = mLabel->text();
125 
126  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
127 
128  if ( fileName.isNull() )
129  return;
130 
131  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
132  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
133 
134  if ( filePath.startsWith( projPath ) )
135  filePath = QDir( projPath ).relativeFilePath( filePath );
136 
137  if ( mLineEdit )
138  mLineEdit->setText( fileName );
139 
140  if ( mLabel )
141  mLineEdit->setText( fileName );
142 }
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()
Access the field.
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const
bool valid() override
Return true if the widget has been properly initialized.
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 startsWith(const QString &s, Qt::CaseSensitivity cs) const
Lineedit with builtin clear button.
void setValue(const QVariant &value) override
void setMargin(int margin)
QVariant value() override
Will be used to access the widget's value.
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:353
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)
Represents a vector layer which manages a vector based data sets.
QString toString() const
QgsFileNameWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:77
T findChild(const QString &name) const