QGIS API Documentation  2.9.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  , mLineEdit( NULL )
27  , mPushButton( NULL )
28  , mLabel( NULL )
29 {
30 }
31 
33 {
34  QVariant value;
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 
50 QWidget* QgsFileNameWidgetWrapper::createWidget( QWidget* parent )
51 {
52  QWidget* container = new QWidget( parent );
53  container->setBackgroundRole( QPalette::Window );
54  container->setAutoFillBackground( true );
55 
56  QLineEdit* le = new QgsFilterLineEdit( container );
57  QPushButton* pbn = new QPushButton( tr( "..." ), container );
58  QGridLayout* layout = new QGridLayout();
59 
60  layout->setMargin( 0 );
61  layout->addWidget( le, 0, 0 );
62  layout->addWidget( pbn, 0, 1 );
63 
64  container->setLayout( layout );
65 
66  return container;
67 }
68 
69 void QgsFileNameWidgetWrapper::initWidget( QWidget* editor )
70 {
71  mLineEdit = qobject_cast<QLineEdit*>( editor );
72  if ( !mLineEdit )
73  {
74  mLineEdit = editor->findChild<QLineEdit*>();
75  }
76 
77  mPushButton = editor->findChild<QPushButton*>();
78 
79  if ( mPushButton )
80  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
81 
82  mLabel = qobject_cast<QLabel*>( editor );
83 
84  if ( mLineEdit )
85  {
86  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
87  if ( fle )
88  {
89  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
90  }
91 
92  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
93  }
94 }
95 
96 void QgsFileNameWidgetWrapper::setValue( const QVariant& value )
97 {
98  if ( mLineEdit )
99  {
100  if ( value.isNull() )
101  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
102  else
103  mLineEdit->setText( value.toString() );
104  }
105 
106  if ( mLabel )
107  mLabel->setText( value.toString() );
108 }
109 
110 void QgsFileNameWidgetWrapper::selectFileName()
111 {
112  QString text;
113 
114  if ( mLineEdit )
115  text = mLineEdit->text();
116 
117  if ( mLabel )
118  text = mLabel->text();
119 
120  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
121 
122  if ( fileName.isNull() )
123  return;
124 
125  if ( mLineEdit )
126  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
127 
128  if ( mLabel )
129  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
130 }
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:74
#define tr(sourceText)