Skip to content

Commit

Permalink
Scale image if the photo widget size is set to 0/0
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 7, 2015
1 parent 75df5f2 commit 1412426
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -212,6 +212,7 @@ SET(QGIS_GUI_SRCS
qgsnumericsortlistviewitem.cpp
qgsoptionsdialogbase.cpp
qgsowssourceselect.cpp
qgspixmaplabel.cpp
qgspluginmanagerinterface.cpp
qgsprojectbadlayerguihandler.cpp
qgsprojectionselectionwidget.cpp
Expand Down Expand Up @@ -333,6 +334,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsnewvectorlayerdialog.h
qgsoptionsdialogbase.h
qgsowssourceselect.h
qgspixmaplabel.h
qgspluginmanagerinterface.h
qgsprevieweffect.h
qgsprojectbadlayerguihandler.h
Expand Down
23 changes: 19 additions & 4 deletions src/gui/editorwidgets/qgsphotowidgetwrapper.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

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

#include <QGridLayout>
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 );
}
}
}

Expand All @@ -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" );
Expand Down Expand Up @@ -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() ) );

Expand Down
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsphotowidgetwrapper.h
Expand Up @@ -17,6 +17,7 @@
#define QGSPHOTOWIDGETWRAPPER_H

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

#include <QLabel>
#include <QPushButton>
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/gui/qgspixmaplabel.cpp
@@ -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 ) );
}
53 changes: 53 additions & 0 deletions src/gui/qgspixmaplabel.h
@@ -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.