QGIS API Documentation  2.5.0-Master
 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 
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 
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  QPixmap pm( fileName );
42  if ( !pm.isNull() && mPhotoLabel )
43  {
44  QSize size( config( "Width" ).toInt(), config( "Height" ).toInt() );
45  if ( size.width() == 0 && size.height() > 0 )
46  {
47  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
48  }
49  else if ( size.width() > 0 && size.height() == 0 )
50  {
51  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
52  }
53 
54  pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
55 
56 
57  mPhotoLabel->setPixmap( pm );
58  mPhotoLabel->setMinimumSize( size );
59  }
60 }
61 
63 {
64  QVariant v;
65 
66  if ( mLineEdit )
67  v = mLineEdit->text();
68 
69  return v;
70 }
71 
72 QWidget* QgsPhotoWidgetWrapper::createWidget( QWidget* parent )
73 {
74  QWidget* container = new QWidget( parent );
75  QGridLayout* layout = new QGridLayout( container );
76  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
77  QLabel* label = new QLabel( parent );
78  label->setObjectName( "PhotoLabel" );
79  QPushButton* pb = new QPushButton( tr( "..." ), container );
80  pb->setObjectName( "FileChooserButton" );
81 
82  layout->addWidget( label, 0, 0, 1, 2 );
83  layout->addWidget( le, 1, 0 );
84  layout->addWidget( pb, 1, 1 );
85 
86  container->setLayout( layout );
87 
88  return container;
89 }
90 
91 void QgsPhotoWidgetWrapper::initWidget( QWidget* editor )
92 {
93  QWidget* container;
94 
95  mLineEdit = qobject_cast<QLineEdit*>( editor );
96 
97  if ( mLineEdit )
98  container = qobject_cast<QWidget*>( mLineEdit->parent() );
99  else
100  {
101  container = editor;
102  mLineEdit = container->findChild<QLineEdit*>();
103  }
104 
105  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
106  if ( !mButton )
107  mButton = container->findChild<QPushButton*>();
108 
109  mPhotoLabel = container->findChild<QLabel*>( "PhotoLabel" );
110  if ( !mPhotoLabel )
111  mPhotoLabel = container->findChild<QLabel*>();
112 
113  if ( mButton )
114  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
115 
116  if ( mLineEdit )
117  {
118  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
119  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadPixmap( QString ) ) );
120  }
121 }
122 
123 void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
124 {
125  if ( mLineEdit )
126  mLineEdit->setText( value.toString() );
127 
128 
129 }
130 
132 {
133  if ( mLineEdit )
134  mLineEdit->setEnabled( enabled );
135 
136  if ( mButton )
137  mButton->setEnabled( enabled );
138 }
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.
QLabel * mPhotoLabel
This label is used as a container to display the picture.
Lineedit with builtin clear button.
QVariant value()
Will be used to access the widget's value.
void setValue(const QVariant &value)
QLineEdit * mLineEdit
The line edit containing the path to the picture.
QPushButton * mButton
The button to open the file chooser dialog.
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
void loadPixmap(const QString &fileName)
#define tr(sourceText)