QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsphotowidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsphotowidgetwrapper.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 
16 #include "qgsphotowidgetwrapper.h"
17 
18 #include <QGridLayout>
19 #include <QFileDialog>
20 #include <QSettings>
21 
22 #include "qgsfilterlineedit.h"
23 
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26  , mPhotoLabel( 0 )
27  , mLineEdit( 0 )
28  , mButton( 0 )
29 {
30 #ifdef WITH_QTWEBKIT
31  mWebView = 0;
32 #endif
33 }
34 
35 void QgsPhotoWidgetWrapper::selectFileName()
36 {
37  if ( mLineEdit )
38  {
39  QString fileName = QFileDialog::getOpenFileName( 0, tr( "Select a picture" ), QFileInfo( mLineEdit->text() ).absolutePath() );
40  if ( !fileName.isNull() )
41  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
42  }
43 }
44 
45 void QgsPhotoWidgetWrapper::loadPixmap( const QString &fileName )
46 {
47 #ifdef WITH_QTWEBKIT
48  if ( mWebView )
49  {
50  mWebView->setUrl( fileName );
51  }
52 #endif
53 
54  QPixmap pm( fileName );
55  if ( !pm.isNull() && mPhotoLabel )
56  {
57  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
58  if ( size.width() == 0 && size.height() > 0 )
59  {
60  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
61  }
62  else if ( size.width() > 0 && size.height() == 0 )
63  {
64  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
65  }
66 
67  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
68 
69  mPhotoLabel->setPixmap( pm );
70  mPhotoLabel->setMinimumSize( size );
71  }
72 }
73 
75 {
76  QVariant v;
77 
78  if ( mLineEdit )
79  {
80  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
81  v = QVariant( QVariant::String );
82  else
83  v = mLineEdit->text();
84  }
85 
86  return v;
87 }
88 
90 {
91  QWidget* container = new QWidget( parent );
92  QGridLayout* layout = new QGridLayout( container );
93  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
94  QLabel* label = new QLabel( parent );
95  label->setObjectName( "PhotoLabel" );
96  QPushButton* pb = new QPushButton( tr( "..." ), container );
97  pb->setObjectName( "FileChooserButton" );
98 
99  layout->addWidget( label, 0, 0, 1, 2 );
100  layout->addWidget( le, 1, 0 );
101  layout->addWidget( pb, 1, 1 );
102 
103  container->setLayout( layout );
104 
105  return container;
106 }
107 
109 {
110  QWidget* container;
111 
112  mLineEdit = qobject_cast<QLineEdit*>( editor );
113 #ifdef WITH_QTWEBKIT
114  mWebView = qobject_cast<QWebView*>( editor );
115 #endif
116 
117  if ( mLineEdit )
118  {
119  container = mLineEdit->parentWidget();
120  }
121 #ifdef WITH_QTWEBKIT
122  else if ( mWebView )
123  {
124  container = mWebView->parentWidget();
125  mLineEdit = container->findChild<QLineEdit*>();
126  }
127 #endif
128  else
129  {
130  container = editor;
131  mLineEdit = container->findChild<QLineEdit*>();
132  }
133 
134  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
135  if ( !mButton )
136  mButton = container->findChild<QPushButton*>();
137 
138  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
139  if ( !mPhotoLabel )
140  mPhotoLabel = container->findChild<QLabel*>();
141 
142  if ( mButton )
143  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
144 
145  if ( mLineEdit )
146  {
147 
148  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
149  if ( fle )
150  {
151  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
152  }
153 
154  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
155  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
156  }
157 }
158 
160 {
161  if ( mLineEdit )
162  {
163  if ( value.isNull() )
164  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
165  else
166  mLineEdit->setText( value.toString() );
167  }
168  else
169  {
170  loadPixmap( value.toString() );
171  }
172 }
173 
175 {
176  if ( mLineEdit )
177  mLineEdit->setEnabled( enabled );
178 
179  if ( mButton )
180  mButton->setEnabled( enabled );
181 }
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.
void setPixmap(const QPixmap &)
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setMinimumSize(const QSize &)
void setEnabled(bool)
void setLayout(QLayout *layout)
bool isNull() const
const QgsEditorWidgetConfig config()
Returns the whole config.
void setObjectName(const QString &name)
Lineedit with builtin clear button.
void setNullValue(QString nullValue)
void setEnabled(bool enabled) override
QVariant value(const QString &key, const QVariant &defaultValue) const
QVariant value() override
Will be used to access the widget's value.
QWidget * parentWidget() const
QgsPhotoWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setValue(const QVariant &value) override
Represents a vector layer which manages a vector based data sets.
QString toString() const
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
T findChild(const QString &name) const