Skip to content

Commit

Permalink
Various fixes for photo editor and external resource widgets
Browse files Browse the repository at this point in the history
- Clear picture when changing from a feature with a picture to
a feature with no picture set
- Don't try to load "NULL" as a filename
- Fix calculation of widget size so that widget can collapse
its size to nothing when it doesn't have a picture set
- Avoid incorrect scaling and cropping of pictures
  • Loading branch information
nyalldawson committed Apr 5, 2016
1 parent e571885 commit 0c16195
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
35 changes: 34 additions & 1 deletion src/gui/editorwidgets/qgsphotowidgetwrapper.cpp
Expand Up @@ -56,6 +56,18 @@ void QgsPhotoWidgetWrapper::selectFileName()

void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
{
if ( fileName.isEmpty() )
{
#ifdef WITH_QTWEBKIT
if ( mWebView )
{
mWebView->setUrl( QString() );
}
#endif
clearPicture();
return;
}

QString filePath = fileName;

if ( QUrl( fileName ).isRelative() )
Expand Down Expand Up @@ -98,6 +110,24 @@ void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
mPhotoLabel->setPixmap( pm );
}
}
else
{
clearPicture();
}
}

void QgsPhotoWidgetWrapper::clearPicture()
{
if ( mPhotoLabel )
{
mPhotoLabel->clear();
mPhotoLabel->setMinimumSize( QSize( 0, 0 ) );

if ( mPhotoPixmapLabel )
mPhotoPixmapLabel->setPixmap( QPixmap() );
else
mPhotoLabel->setPixmap( QPixmap() );
}
}

QVariant QgsPhotoWidgetWrapper::value() const
Expand Down Expand Up @@ -212,7 +242,10 @@ void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
if ( mLineEdit )
{
if ( value.isNull() )
mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
{
whileBlocking( mLineEdit )->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
clearPicture();
}
else
mLineEdit->setText( value.toString() );
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsphotowidgetwrapper.h
Expand Up @@ -78,6 +78,8 @@ class GUI_EXPORT QgsPhotoWidgetWrapper : public QgsEditorWidgetWrapper
QLineEdit* mLineEdit;
//! The button to open the file chooser dialog
QPushButton* mButton;

void clearPicture();
};

#endif // QGSPHOTOWIDGETWRAPPER_H
17 changes: 9 additions & 8 deletions src/gui/qgsexternalresourcewidget.cpp
Expand Up @@ -142,7 +142,11 @@ void QgsExternalResourceWidget::updateDocumentViewer()
{
const QPixmap* pm = mPixmapLabel->pixmap();

if ( pm )
if ( !pm || pm->isNull() )
{
mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
}
else
{
QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
if ( size.width() == 0 && size.height() > 0 )
Expand All @@ -165,7 +169,7 @@ void QgsExternalResourceWidget::updateDocumentViewer()

void QgsExternalResourceWidget::loadDocument( const QString& path )
{
if ( path.isNull() )
if ( path.isEmpty() )
{
#ifdef WITH_QTWEBKIT
if ( mDocumentViewerContent == Web )
Expand All @@ -176,6 +180,7 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
if ( mDocumentViewerContent == Image )
{
mPixmapLabel->clear();
updateDocumentViewer();
}
}

Expand All @@ -190,12 +195,8 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
if ( mDocumentViewerContent == Image )
{
QPixmap pm( path );
if ( !pm.isNull() )
{
mPixmapLabel->setPixmap( pm );

updateDocumentViewer();
}
mPixmapLabel->setPixmap( pm );
updateDocumentViewer();
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/gui/qgspixmaplabel.cpp
Expand Up @@ -24,17 +24,31 @@ QgsPixmapLabel::QgsPixmapLabel( QWidget *parent ) :

void QgsPixmapLabel::setPixmap( const QPixmap & p )
{
bool sizeChanged = ( p.size() != mPixmap.size() );
mPixmap = p;
QLabel::setPixmap( p );

if ( sizeChanged )
{
updateGeometry();
}

QLabel::setPixmap( mPixmap.scaled( this->size(),
Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
}

int QgsPixmapLabel::heightForWidth( int width ) const
{
if ( mPixmap.isNull() )
return 0;

return (( qreal )mPixmap.height()*width ) / mPixmap.width();
}

QSize QgsPixmapLabel::sizeHint() const
{
if ( mPixmap.isNull() )
return QSize( 0, 0 );

int w = this->width();
return QSize( w, heightForWidth( w ) );
}
Expand Down

0 comments on commit 0c16195

Please sign in to comment.