QGIS API Documentation  2.5.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 <QGridLayout>
22 
23 QgsFileNameWidgetWrapper::QgsFileNameWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
24  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
25 {
26 }
27 
29 {
30  QVariant value;
31 
32  if ( mLineEdit )
33  value = mLineEdit->text();
34 
35  if ( mLabel )
36  value = mLabel->text();
37 
38  return value;
39 }
40 
41 QWidget* QgsFileNameWidgetWrapper::createWidget( QWidget* parent )
42 {
43  QWidget* container = new QWidget( parent );
44  container->setBackgroundRole( QPalette::Window );
45  container->setAutoFillBackground( true );
46 
47  QLineEdit* le = new QgsFilterLineEdit( container );
48  QPushButton* pbn = new QPushButton( tr( "..." ), container );
49  QGridLayout* layout = new QGridLayout();
50 
51  layout->setMargin( 0 );
52  layout->addWidget( le, 0, 0 );
53  layout->addWidget( pbn, 0, 1 );
54 
55  container->setLayout( layout );
56 
57  return container;
58 }
59 
60 void QgsFileNameWidgetWrapper::initWidget( QWidget* editor )
61 {
62  mLineEdit = qobject_cast<QLineEdit*>( editor );
63  if ( !mLineEdit )
64  {
65  mLineEdit = editor->findChild<QLineEdit*>();
66  }
67 
68  mPushButton = editor->findChild<QPushButton*>();
69 
70  if ( mPushButton )
71  connect( mPushButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
72 
73  mLabel = qobject_cast<QLabel*>( editor );
74 
75  if ( mLineEdit )
76  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
77 }
78 
79 void QgsFileNameWidgetWrapper::setValue( const QVariant& value )
80 {
81  if ( mLineEdit )
82  mLineEdit->setText( value.toString() );
83 
84  if ( mLabel )
85  mLabel->setText( value.toString() );
86 }
87 
89 {
90  QString text;
91 
92  if ( mLineEdit )
93  text = mLineEdit->text();
94 
95  if ( mLabel )
96  text = mLabel->text();
97 
98  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
99 
100  if ( fileName.isNull() )
101  return;
102 
103  if ( mLineEdit )
104  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
105 
106  if ( mLabel )
107  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
108 }
QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
void valueChanged()
Will call the value() method to determine the emitted value.
void setValue(const QVariant &value)
Manages an editor widget Widget and wrapper share the same parent.
QVariant value()
Will be used to access the widget's value.
Lineedit with builtin clear button.
void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
Represents a vector layer which manages a vector based data sets.
QgsFileNameWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
#define tr(sourceText)