QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
24 QgsFileNameWidgetWrapper::QgsFileNameWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 {
27 }
28 
30 {
31  QVariant value;
32 
33  if ( mLineEdit )
34  {
35  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
36  value = QVariant( field().type() );
37  else
38  value = mLineEdit->text();
39  }
40 
41  if ( mLabel )
42  value = mLabel->text();
43 
44  return value;
45 }
46 
47 QWidget* QgsFileNameWidgetWrapper::createWidget( QWidget* parent )
48 {
49  QWidget* container = new QWidget( parent );
50  container->setBackgroundRole( QPalette::Window );
51  container->setAutoFillBackground( true );
52 
53  QLineEdit* le = new QgsFilterLineEdit( container );
54  QPushButton* pbn = new QPushButton( tr( "..." ), container );
55  QGridLayout* layout = new QGridLayout();
56 
57  layout->setMargin( 0 );
58  layout->addWidget( le, 0, 0 );
59  layout->addWidget( pbn, 0, 1 );
60 
61  container->setLayout( layout );
62 
63  return container;
64 }
65 
66 void QgsFileNameWidgetWrapper::initWidget( QWidget* editor )
67 {
68  mLineEdit = qobject_cast<QLineEdit*>( editor );
69  if ( !mLineEdit )
70  {
71  mLineEdit = editor->findChild<QLineEdit*>();
72  }
73 
74  mPushButton = editor->findChild<QPushButton*>();
75 
76  if ( mPushButton )
77  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
78 
79  mLabel = qobject_cast<QLabel*>( editor );
80 
81  if ( mLineEdit )
82  {
83  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
84  if ( fle )
85  {
86  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
87  }
88 
89  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
90  }
91 }
92 
93 void QgsFileNameWidgetWrapper::setValue( const QVariant& value )
94 {
95  if ( mLineEdit )
96  {
97  if ( value.isNull() )
98  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
99  else
100  mLineEdit->setText( value.toString() );
101  }
102 
103  if ( mLabel )
104  mLabel->setText( value.toString() );
105 }
106 
107 void QgsFileNameWidgetWrapper::selectFileName()
108 {
109  QString text;
110 
111  if ( mLineEdit )
112  text = mLineEdit->text();
113 
114  if ( mLabel )
115  text = mLabel->text();
116 
117  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
118 
119  if ( fileName.isNull() )
120  return;
121 
122  if ( mLineEdit )
123  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
124 
125  if ( mLabel )
126  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
127 }
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()
Access the field.
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)
QVariant value() override
Will be used to access the widget's value.
Represents a vector layer which manages a vector based data sets.
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:64
#define tr(sourceText)