QGIS API Documentation  2.8.2-Wien
 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 
24 QgsPhotoWidgetWrapper::QgsPhotoWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26  , mPhotoLabel( NULL )
27  , mWebView( NULL )
28  , mLineEdit( NULL )
29  , mButton( NULL )
30 {
31 }
32 
33 void QgsPhotoWidgetWrapper::selectFileName()
34 {
35  if ( mLineEdit )
36  {
37  QString fileName = QFileDialog::getOpenFileName( 0, tr( "Select a picture" ), QFileInfo( mLineEdit->text() ).absolutePath() );
38  if ( !fileName.isNull() )
39  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
40  }
41 }
42 
43 void QgsPhotoWidgetWrapper::loadPixmap( const QString &fileName )
44 {
45  if ( mWebView )
46  {
47  mWebView->setUrl( fileName );
48  }
49 
50  QPixmap pm( fileName );
51  if ( !pm.isNull() && mPhotoLabel )
52  {
53  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
54  if ( size.width() == 0 && size.height() > 0 )
55  {
56  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
57  }
58  else if ( size.width() > 0 && size.height() == 0 )
59  {
60  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
61  }
62 
63  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
64 
65  mPhotoLabel->setPixmap( pm );
66  mPhotoLabel->setMinimumSize( size );
67  }
68 }
69 
71 {
72  QVariant v;
73 
74  if ( mLineEdit )
75  {
76  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
77  v = QVariant( QVariant::String );
78  else
79  v = mLineEdit->text();
80  }
81 
82  return v;
83 }
84 
85 QWidget* QgsPhotoWidgetWrapper::createWidget( QWidget* parent )
86 {
87  QWidget* container = new QWidget( parent );
88  QGridLayout* layout = new QGridLayout( container );
89  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
90  QLabel* label = new QLabel( parent );
91  label->setObjectName( "PhotoLabel" );
92  QPushButton* pb = new QPushButton( tr( "..." ), container );
93  pb->setObjectName( "FileChooserButton" );
94 
95  layout->addWidget( label, 0, 0, 1, 2 );
96  layout->addWidget( le, 1, 0 );
97  layout->addWidget( pb, 1, 1 );
98 
99  container->setLayout( layout );
100 
101  return container;
102 }
103 
104 void QgsPhotoWidgetWrapper::initWidget( QWidget* editor )
105 {
106  QWidget* container;
107 
108  mLineEdit = qobject_cast<QLineEdit*>( editor );
109  mWebView = qobject_cast<QWebView*>( editor );
110 
111  if ( mLineEdit )
112  {
113  container = mLineEdit->parentWidget();
114  }
115  else if ( mWebView )
116  {
117  container = mWebView->parentWidget();
118  mLineEdit = container->findChild<QLineEdit*>();
119  }
120  else
121  {
122  container = editor;
123  mLineEdit = container->findChild<QLineEdit*>();
124  }
125 
126  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
127  if ( !mButton )
128  mButton = container->findChild<QPushButton*>();
129 
130  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
131  if ( !mPhotoLabel )
132  mPhotoLabel = container->findChild<QLabel*>();
133 
134  if ( mButton )
135  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
136 
137  if ( mLineEdit )
138  {
139 
140  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
141  if ( fle )
142  {
143  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
144  }
145 
146  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
147  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
148  }
149 }
150 
151 void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
152 {
153  if ( mLineEdit )
154  {
155  if ( value.isNull() )
156  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
157  else
158  mLineEdit->setText( value.toString() );
159  }
160  else
161  {
162  loadPixmap( value.toString() );
163  }
164 }
165 
167 {
168  if ( mLineEdit )
169  mLineEdit->setEnabled( enabled );
170 
171  if ( mButton )
172  mButton->setEnabled( enabled );
173 }