QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 dot kuhn at gmx 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 
20 #include <QFileDialog>
21 #include <QSettings>
22 #include <QGridLayout>
23 
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26  , mLineEdit( NULL )
27  , mPushButton( NULL )
28  , mLabel( NULL )
29 {
30 }
31 
33 {
35 
36  if ( mLineEdit )
37  {
38  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
39  value = QVariant( field().type() );
40  else
41  value = mLineEdit->text();
42  }
43 
44  if ( mLabel )
45  value = mLabel->text();
46 
47  return value;
48 }
49 
51 {
52  return mLineEdit || mLabel;
53 }
54 
56 {
57  QWidget* container = new QWidget( parent );
58  container->setBackgroundRole( QPalette::Window );
59  container->setAutoFillBackground( true );
60 
61  QLineEdit* le = new QgsFilterLineEdit( container );
62  QPushButton* pbn = new QPushButton( tr( "..." ), container );
63  QGridLayout* layout = new QGridLayout();
64 
65  layout->setMargin( 0 );
66  layout->addWidget( le, 0, 0 );
67  layout->addWidget( pbn, 0, 1 );
68 
69  container->setLayout( layout );
70 
71  return container;
72 }
73 
75 {
76  mLineEdit = qobject_cast<QLineEdit*>( editor );
77  if ( !mLineEdit )
78  {
79  mLineEdit = editor->findChild<QLineEdit*>();
80  }
81 
82  mPushButton = editor->findChild<QPushButton*>();
83 
84  if ( mPushButton )
85  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
86 
87  mLabel = qobject_cast<QLabel*>( editor );
88 
89  if ( mLineEdit )
90  {
91  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
92  if ( fle )
93  {
94  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
95  }
96 
97  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
98  }
99 }
100 
102 {
103  if ( mLineEdit )
104  {
105  if ( value.isNull() )
106  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
107  else
108  mLineEdit->setText( value.toString() );
109  }
110 
111  if ( mLabel )
112  mLabel->setText( value.toString() );
113 }
114 
115 void QgsFileNameWidgetWrapper::selectFileName()
116 {
117  QString text;
118 
119  if ( mLineEdit )
120  text = mLineEdit->text();
121 
122  if ( mLabel )
123  text = mLabel->text();
124 
125  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
126 
127  if ( fileName.isNull() )
128  return;
129 
130  if ( mLineEdit )
131  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
132 
133  if ( mLabel )
134  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
135 }
QString toNativeSeparators(const QString &pathName)
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.
Lineedit with builtin clear button.
void setValue(const QVariant &value) override
void setNullValue(QString nullValue)
void setMargin(int margin)
QVariant value() override
Will be used to access the widget's value.
QVariant value(const QString &key, const QVariant &defaultValue) const
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)
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:74
T findChild(const QString &name) const