QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
24 QgsPhotoWidgetWrapper::QgsPhotoWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 {
27 }
28 
29 void QgsPhotoWidgetWrapper::selectFileName()
30 {
31  if ( mLineEdit )
32  {
33  QString fileName = QFileDialog::getOpenFileName( 0, tr( "Select a picture" ), QFileInfo( mLineEdit->text() ).absolutePath() );
34  if ( !fileName.isNull() )
35  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
36  }
37 }
38 
39 void QgsPhotoWidgetWrapper::loadPixmap( const QString &fileName )
40 {
41  if ( mWebView )
42  {
43  mWebView->setUrl( fileName );
44  }
45 
46  QPixmap pm( fileName );
47  if ( !pm.isNull() && mPhotoLabel )
48  {
49  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
50  if ( size.width() == 0 && size.height() > 0 )
51  {
52  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
53  }
54  else if ( size.width() > 0 && size.height() == 0 )
55  {
56  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
57  }
58 
59  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
60 
61  mPhotoLabel->setPixmap( pm );
62  mPhotoLabel->setMinimumSize( size );
63  }
64 }
65 
67 {
68  QVariant v;
69 
70  if ( mLineEdit )
71  {
72  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
73  v = QVariant( QVariant::String );
74  else
75  v = mLineEdit->text();
76  }
77 
78  return v;
79 }
80 
81 QWidget* QgsPhotoWidgetWrapper::createWidget( QWidget* parent )
82 {
83  QWidget* container = new QWidget( parent );
84  QGridLayout* layout = new QGridLayout( container );
85  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
86  QLabel* label = new QLabel( parent );
87  label->setObjectName( "PhotoLabel" );
88  QPushButton* pb = new QPushButton( tr( "..." ), container );
89  pb->setObjectName( "FileChooserButton" );
90 
91  layout->addWidget( label, 0, 0, 1, 2 );
92  layout->addWidget( le, 1, 0 );
93  layout->addWidget( pb, 1, 1 );
94 
95  container->setLayout( layout );
96 
97  return container;
98 }
99 
100 void QgsPhotoWidgetWrapper::initWidget( QWidget* editor )
101 {
102  QWidget* container;
103 
104  mLineEdit = qobject_cast<QLineEdit*>( editor );
105  mWebView = qobject_cast<QWebView*>( editor );
106 
107  if ( mLineEdit )
108  {
109  container = mLineEdit->parentWidget();
110  }
111  else if ( mWebView )
112  {
113  container = mWebView->parentWidget();
114  mLineEdit = container->findChild<QLineEdit*>();
115  }
116  else
117  {
118  container = editor;
119  mLineEdit = container->findChild<QLineEdit*>();
120  }
121 
122  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
123  if ( !mButton )
124  mButton = container->findChild<QPushButton*>();
125 
126  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
127  if ( !mPhotoLabel )
128  mPhotoLabel = container->findChild<QLabel*>();
129 
130  if ( mButton )
131  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
132 
133  if ( mLineEdit )
134  {
135 
136  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
137  if ( fle )
138  {
139  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
140  }
141 
142  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
143  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
144  }
145 }
146 
147 void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
148 {
149  if ( mLineEdit )
150  {
151  if ( value.isNull() )
152  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
153  else
154  mLineEdit->setText( value.toString() );
155  }
156  else
157  {
158  loadPixmap( value.toString() );
159  }
160 }
161 
163 {
164  if ( mLineEdit )
165  mLineEdit->setEnabled( enabled );
166 
167  if ( mButton )
168  mButton->setEnabled( enabled );
169 }
void valueChanged()
Will call the value() method to determine the emitted value.
Manages an editor widget Widget and wrapper share the same parent.
void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
const QgsEditorWidgetConfig config()
Returns the whole config.
Lineedit with builtin clear button.
void setNullValue(QString nullValue)
QVariant value()
Will be used to access the widget's value.
void setValue(const QVariant &value)
QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
QgsPhotoWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
Represents a vector layer which manages a vector based data sets.
double size
Definition: qgssvgcache.cpp:77
#define tr(sourceText)