Skip to content
Permalink
Browse files

Scale image if the photo widget size is set to 0/0

  • Loading branch information
m-kuhn committed Sep 7, 2015
1 parent 75df5f2 commit 141242674b5b465d02b8ed107ffe83636588aa4c
@@ -212,6 +212,7 @@ SET(QGIS_GUI_SRCS
qgsnumericsortlistviewitem.cpp
qgsoptionsdialogbase.cpp
qgsowssourceselect.cpp
qgspixmaplabel.cpp
qgspluginmanagerinterface.cpp
qgsprojectbadlayerguihandler.cpp
qgsprojectionselectionwidget.cpp
@@ -333,6 +334,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsnewvectorlayerdialog.h
qgsoptionsdialogbase.h
qgsowssourceselect.h
qgspixmaplabel.h
qgspluginmanagerinterface.h
qgsprevieweffect.h
qgsprojectbadlayerguihandler.h
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsphotowidgetwrapper.h"
#include "qgspixmaplabel.h"
#include "qgsproject.h"

#include <QGridLayout>
@@ -25,6 +26,7 @@
QgsPhotoWidgetWrapper::QgsPhotoWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mPhotoLabel( 0 )
, mPhotoPixmapLabel( 0 )
, mLineEdit( 0 )
, mButton( 0 )
{
@@ -79,10 +81,22 @@ void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
size.setHeight( size.width() * pm.size().height() / pm.size().width() );
}

pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
if ( mPhotoPixmapLabel )
{
mPhotoPixmapLabel->setPixmap( pm );

mPhotoLabel->setPixmap( pm );
mPhotoLabel->setMinimumSize( size );
if ( size.width() != 0 || size.height() != 0 )
{
mPhotoPixmapLabel->setMinimumSize( size );
mPhotoPixmapLabel->setMaximumSize( size );
}
}
else // mPhotoLabel is checked in the outer if branch
{
mPhotoLabel->setMinimumSize( size );
pm = pm.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
mPhotoLabel->setPixmap( pm );
}
}
}

@@ -106,7 +120,7 @@ QWidget* QgsPhotoWidgetWrapper::createWidget( QWidget* parent )
QWidget* container = new QWidget( parent );
QGridLayout* layout = new QGridLayout( container );
QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
QLabel* label = new QLabel( parent );
QgsPixmapLabel* label = new QgsPixmapLabel( parent );
label->setObjectName( "PhotoLabel" );
QPushButton* pb = new QPushButton( tr( "..." ), container );
pb->setObjectName( "FileChooserButton" );
@@ -154,6 +168,7 @@ void QgsPhotoWidgetWrapper::initWidget( QWidget* editor )
if ( !mPhotoLabel )
mPhotoLabel = container->findChild<QLabel*>();

mPhotoPixmapLabel = qobject_cast<QgsPixmapLabel*>( mPhotoLabel );
if ( mButton )
connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );

@@ -17,6 +17,7 @@
#define QGSPHOTOWIDGETWRAPPER_H

#include "qgseditorwidgetwrapper.h"
#include "qgspixmaplabel.h"

#include <QLabel>
#include <QPushButton>
@@ -61,6 +62,9 @@ class GUI_EXPORT QgsPhotoWidgetWrapper : public QgsEditorWidgetWrapper
private:
//! This label is used as a container to display the picture
QLabel* mPhotoLabel;
//! This label is used as a container to display a picture that scales with the dialog layout.
//! It will always point to the same label as mPhotoLabel, but may be NULL if the widget is of type QLabel.
QgsPixmapLabel* mPhotoPixmapLabel;
#ifdef WITH_QTWEBKIT
//! This webview is used as a container to display the picture
QWebView* mWebView;
@@ -0,0 +1,47 @@
/***************************************************************************
----------------------------------------------------
date : 7.9.2015
copyright : (C) 2015 by Matthias Kuhn
email : matthias (at) opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgspixmaplabel.h"


QgsPixmapLabel::QgsPixmapLabel( QWidget *parent ) :
QLabel( parent )
{
this->setMinimumSize( 1, 1 );
}

void QgsPixmapLabel::setPixmap( const QPixmap & p )
{
mPixmap = p;
QLabel::setPixmap( p );
}

int QgsPixmapLabel::heightForWidth( int width ) const
{
return (( qreal )mPixmap.height()*width ) / mPixmap.width();
}

QSize QgsPixmapLabel::sizeHint() const
{
int w = this->width();
return QSize( w, heightForWidth( w ) );
}

void QgsPixmapLabel::resizeEvent( QResizeEvent * e )
{
QLabel::resizeEvent( e );
QLabel::setPixmap( mPixmap.scaled( this->size(),
Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
}
@@ -0,0 +1,53 @@
/***************************************************************************
----------------------------------------------------
date : 7.9.2015
copyright : (C) 2015 by Matthias Kuhn
email : matthias (at) opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSPIXMAPLABEL_H
#define QGSPIXMAPLABEL_H

#include <QLabel>

/**
* @brief The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given
* to the widget by the layout and keeping its aspect ratio.
*/
class QgsPixmapLabel : public QLabel
{
Q_OBJECT

public:
explicit QgsPixmapLabel( QWidget *parent = 0 );
/**
* Calculates the height for the given width.
*
* @param width The width for the widget
* @return An appropriate height
*/
virtual int heightForWidth( int width ) const;

/**
* An optimal size for the widget. Effectively using the height
* determined from the width with the given aspect ratio.
* @return A size hint
*/
virtual QSize sizeHint() const;

public slots:
void setPixmap( const QPixmap & );
void resizeEvent( QResizeEvent * );
private:
QPixmap mPixmap;
};

#endif // QGSPIXMAPLABEL_H

0 comments on commit 1412426

Please sign in to comment.
You can’t perform that action at this time.