QGIS API Documentation  2.14.3-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 #include <QUrl>
24 
25 #include "qgsfilterlineedit.h"
26 
28  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
29  , mPhotoLabel( nullptr )
30  , mPhotoPixmapLabel( nullptr )
31  , mLineEdit( nullptr )
32  , mButton( nullptr )
33 {
34 #ifdef WITH_QTWEBKIT
35  mWebView = nullptr;
36 #endif
37 }
38 
39 void QgsPhotoWidgetWrapper::selectFileName()
40 {
41  if ( !mLineEdit )
42  return;
43 
44  QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Select a picture" ), QFileInfo( mLineEdit->text() ).absolutePath() );
45 
46  if ( fileName.isNull() )
47  return;
48 
49  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
50  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
51 
52  if ( filePath.startsWith( projPath ) )
53  filePath = QDir( projPath ).relativeFilePath( filePath );
54 
55  mLineEdit->setText( filePath );
56 }
57 
58 void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
59 {
60  QString filePath = fileName;
61 
62  if ( QUrl( fileName ).isRelative() )
63  filePath = QDir( QgsProject::instance()->fileInfo().absolutePath() ).filePath( fileName );
64 
65 #ifdef WITH_QTWEBKIT
66  if ( mWebView )
67  {
68  mWebView->setUrl( filePath );
69  }
70 #endif
71 
72  QPixmap pm( filePath );
73  if ( !pm.isNull() && mPhotoLabel )
74  {
75  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
76  if ( size.width() == 0 && size.height() > 0 )
77  {
78  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
79  }
80  else if ( size.width() > 0 && size.height() == 0 )
81  {
82  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
83  }
84 
85  if ( mPhotoPixmapLabel )
86  {
87  mPhotoPixmapLabel->setPixmap( pm );
88 
89  if ( size.width() != 0 || size.height() != 0 )
90  {
91  mPhotoPixmapLabel->setMinimumSize( size );
92  mPhotoPixmapLabel->setMaximumSize( size );
93  }
94  }
95  else // mPhotoLabel is checked in the outer if branch
96  {
97  mPhotoLabel->setMinimumSize( size );
98  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
99  mPhotoLabel->setPixmap( pm );
100  }
101  }
102 }
103 
105 {
106  QVariant v;
107 
108  if ( mLineEdit )
109  {
110  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
111  v = QVariant( QVariant::String );
112  else
113  v = mLineEdit->text();
114  }
115 
116  return v;
117 }
118 
120 {
121  QWidget* container = new QWidget( parent );
122  QGridLayout* layout = new QGridLayout( container );
123  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
124  QgsPixmapLabel* label = new QgsPixmapLabel( parent );
125  label->setObjectName( "PhotoLabel" );
126  QPushButton* pb = new QPushButton( tr( "..." ), container );
127  pb->setObjectName( "FileChooserButton" );
128 
129  layout->addWidget( label, 0, 0, 1, 2 );
130  layout->addWidget( le, 1, 0 );
131  layout->addWidget( pb, 1, 1 );
132 
133  container->setLayout( layout );
134 
135  return container;
136 }
137 
139 {
140  QWidget* container;
141 
142  mLineEdit = qobject_cast<QLineEdit*>( editor );
143 #ifdef WITH_QTWEBKIT
144  mWebView = qobject_cast<QWebView*>( editor );
145 #endif
146 
147  if ( mLineEdit )
148  {
149  container = mLineEdit->parentWidget();
150  }
151 #ifdef WITH_QTWEBKIT
152  else if ( mWebView )
153  {
154  container = mWebView->parentWidget();
155  mLineEdit = container->findChild<QLineEdit*>();
156  }
157 #endif
158  else
159  {
160  container = editor;
161  mLineEdit = container->findChild<QLineEdit*>();
162  }
163 
164  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
165  if ( !mButton )
166  mButton = container->findChild<QPushButton*>();
167 
168  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
169  if ( !mPhotoLabel )
170  mPhotoLabel = container->findChild<QLabel*>();
171 
172  mPhotoPixmapLabel = qobject_cast<QgsPixmapLabel*>( mPhotoLabel );
173  if ( mButton )
174  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
175 
176  if ( mLineEdit )
177  {
178 
179  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
180  if ( fle )
181  {
182  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
183  }
184 
185  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
186  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
187  }
188 }
189 
191 {
192 #ifdef WITH_QTWEBKIT
193  return mPhotoLabel || mLineEdit || mButton || mWebView;
194 #else
195  return mPhotoLabel || mLineEdit || mButton;
196 #endif
197 }
198 
200 {
201  if ( mLineEdit )
202  {
203  if ( value.isNull() )
204  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
205  else
206  mLineEdit->setText( value.toString() );
207  }
208  else
209  {
210  loadPixmap( value.toString() );
211  }
212 }
213 
215 {
216  if ( mLineEdit )
217  mLineEdit->setEnabled( enabled );
218 
219  if ( mButton )
220  mButton->setEnabled( enabled );
221 }
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