QGIS API Documentation  2.14.0-Essen
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 at opengis 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 #include "qgspixmaplabel.h"
18 #include "qgsproject.h"
19 
20 #include <QGridLayout>
21 #include <QFileDialog>
22 #include <QSettings>
23 
24 #include "qgsfilterlineedit.h"
25 
27  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
28  , mPhotoLabel( nullptr )
29  , mPhotoPixmapLabel( nullptr )
30  , mLineEdit( nullptr )
31  , mButton( nullptr )
32 {
33 #ifdef WITH_QTWEBKIT
34  mWebView = nullptr;
35 #endif
36 }
37 
38 void QgsPhotoWidgetWrapper::selectFileName()
39 {
40  if ( !mLineEdit )
41  return;
42 
43  QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Select a picture" ), QFileInfo( mLineEdit->text() ).absolutePath() );
44 
45  if ( fileName.isNull() )
46  return;
47 
48  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
49  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
50 
51  if ( filePath.startsWith( projPath ) )
52  filePath = QDir( projPath ).relativeFilePath( filePath );
53 
54  mLineEdit->setText( filePath );
55 }
56 
57 void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
58 {
59  QString filePath = fileName;
60 
61  if ( QUrl( fileName ).isRelative() )
62  filePath = QDir( QgsProject::instance()->fileInfo().absolutePath() ).filePath( fileName );
63 
64 #ifdef WITH_QTWEBKIT
65  if ( mWebView )
66  {
67  mWebView->setUrl( filePath );
68  }
69 #endif
70 
71  QPixmap pm( filePath );
72  if ( !pm.isNull() && mPhotoLabel )
73  {
74  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
75  if ( size.width() == 0 && size.height() > 0 )
76  {
77  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
78  }
79  else if ( size.width() > 0 && size.height() == 0 )
80  {
81  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
82  }
83 
84  if ( mPhotoPixmapLabel )
85  {
86  mPhotoPixmapLabel->setPixmap( pm );
87 
88  if ( size.width() != 0 || size.height() != 0 )
89  {
90  mPhotoPixmapLabel->setMinimumSize( size );
91  mPhotoPixmapLabel->setMaximumSize( size );
92  }
93  }
94  else // mPhotoLabel is checked in the outer if branch
95  {
96  mPhotoLabel->setMinimumSize( size );
97  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
98  mPhotoLabel->setPixmap( pm );
99  }
100  }
101 }
102 
104 {
105  QVariant v;
106 
107  if ( mLineEdit )
108  {
109  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
110  v = QVariant( QVariant::String );
111  else
112  v = mLineEdit->text();
113  }
114 
115  return v;
116 }
117 
119 {
120  QWidget* container = new QWidget( parent );
121  QGridLayout* layout = new QGridLayout( container );
122  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
123  QgsPixmapLabel* label = new QgsPixmapLabel( parent );
124  label->setObjectName( "PhotoLabel" );
125  QPushButton* pb = new QPushButton( tr( "..." ), container );
126  pb->setObjectName( "FileChooserButton" );
127 
128  layout->addWidget( label, 0, 0, 1, 2 );
129  layout->addWidget( le, 1, 0 );
130  layout->addWidget( pb, 1, 1 );
131 
132  container->setLayout( layout );
133 
134  return container;
135 }
136 
138 {
139  QWidget* container;
140 
141  mLineEdit = qobject_cast<QLineEdit*>( editor );
142 #ifdef WITH_QTWEBKIT
143  mWebView = qobject_cast<QWebView*>( editor );
144 #endif
145 
146  if ( mLineEdit )
147  {
148  container = mLineEdit->parentWidget();
149  }
150 #ifdef WITH_QTWEBKIT
151  else if ( mWebView )
152  {
153  container = mWebView->parentWidget();
154  mLineEdit = container->findChild<QLineEdit*>();
155  }
156 #endif
157  else
158  {
159  container = editor;
160  mLineEdit = container->findChild<QLineEdit*>();
161  }
162 
163  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
164  if ( !mButton )
165  mButton = container->findChild<QPushButton*>();
166 
167  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
168  if ( !mPhotoLabel )
169  mPhotoLabel = container->findChild<QLabel*>();
170 
171  mPhotoPixmapLabel = qobject_cast<QgsPixmapLabel*>( mPhotoLabel );
172  if ( mButton )
173  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
174 
175  if ( mLineEdit )
176  {
177 
178  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
179  if ( fle )
180  {
181  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
182  }
183 
184  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
185  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
186  }
187 }
188 
190 {
191 #ifdef WITH_QTWEBKIT
192  return mPhotoLabel || mLineEdit || mButton || mWebView;
193 #else
194  return mPhotoLabel || mLineEdit || mButton;
195 #endif
196 }
197 
199 {
200  if ( mLineEdit )
201  {
202  if ( value.isNull() )
203  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
204  else
205  mLineEdit->setText( value.toString() );
206  }
207  else
208  {
209  loadPixmap( value.toString() );
210  }
211 }
212 
214 {
215  if ( mLineEdit )
216  mLineEdit->setEnabled( enabled );
217 
218  if ( mButton )
219  mButton->setEnabled( enabled );
220 }
QSize size() const
QString toNativeSeparators(const QString &pathName)
void setHeight(int height)
QString relativeFilePath(const QString &fileName) const
void setPixmap(const QPixmap &)
int width() const
void valueChanged()
Will call the value() method to determine the emitted value.
QVariant value() const override
Will be used to access the widget&#39;s 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 filePath(const QString &fileName) const
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.
QgsPhotoWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
void setMinimumSize(const QSize &)
void setEnabled(bool)
void setLayout(QLayout *layout)
bool isNull() const
void setWidth(int width)
void setObjectName(const QString &name)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Lineedit with builtin clear button.
QPixmap scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const
void setEnabled(bool enabled) override
bool isNull() const
void setMaximumSize(const QSize &)
bool valid() const override
Return true if the widget has been properly initialized.
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
QWidget * parentWidget() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
int height() const
QgsEditorWidgetConfig config() const
Returns the whole config.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
void setNullValue(const QString &nullValue)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setValue(const QVariant &value) override
QObject * parent() const
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.
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...
T findChild(const QString &name) const