Skip to content

Commit 0c16195

Browse files
committed
Various fixes for photo editor and external resource widgets
- 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
1 parent e571885 commit 0c16195

4 files changed

+60
-10
lines changed

src/gui/editorwidgets/qgsphotowidgetwrapper.cpp

+34-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ void QgsPhotoWidgetWrapper::selectFileName()
5656

5757
void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
5858
{
59+
if ( fileName.isEmpty() )
60+
{
61+
#ifdef WITH_QTWEBKIT
62+
if ( mWebView )
63+
{
64+
mWebView->setUrl( QString() );
65+
}
66+
#endif
67+
clearPicture();
68+
return;
69+
}
70+
5971
QString filePath = fileName;
6072

6173
if ( QUrl( fileName ).isRelative() )
@@ -98,6 +110,24 @@ void QgsPhotoWidgetWrapper::loadPixmap( const QString& fileName )
98110
mPhotoLabel->setPixmap( pm );
99111
}
100112
}
113+
else
114+
{
115+
clearPicture();
116+
}
117+
}
118+
119+
void QgsPhotoWidgetWrapper::clearPicture()
120+
{
121+
if ( mPhotoLabel )
122+
{
123+
mPhotoLabel->clear();
124+
mPhotoLabel->setMinimumSize( QSize( 0, 0 ) );
125+
126+
if ( mPhotoPixmapLabel )
127+
mPhotoPixmapLabel->setPixmap( QPixmap() );
128+
else
129+
mPhotoLabel->setPixmap( QPixmap() );
130+
}
101131
}
102132

103133
QVariant QgsPhotoWidgetWrapper::value() const
@@ -212,7 +242,10 @@ void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
212242
if ( mLineEdit )
213243
{
214244
if ( value.isNull() )
215-
mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
245+
{
246+
whileBlocking( mLineEdit )->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
247+
clearPicture();
248+
}
216249
else
217250
mLineEdit->setText( value.toString() );
218251
}

src/gui/editorwidgets/qgsphotowidgetwrapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class GUI_EXPORT QgsPhotoWidgetWrapper : public QgsEditorWidgetWrapper
7878
QLineEdit* mLineEdit;
7979
//! The button to open the file chooser dialog
8080
QPushButton* mButton;
81+
82+
void clearPicture();
8183
};
8284

8385
#endif // QGSPHOTOWIDGETWRAPPER_H

src/gui/qgsexternalresourcewidget.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ void QgsExternalResourceWidget::updateDocumentViewer()
142142
{
143143
const QPixmap* pm = mPixmapLabel->pixmap();
144144

145-
if ( pm )
145+
if ( !pm || pm->isNull() )
146+
{
147+
mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
148+
}
149+
else
146150
{
147151
QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
148152
if ( size.width() == 0 && size.height() > 0 )
@@ -165,7 +169,7 @@ void QgsExternalResourceWidget::updateDocumentViewer()
165169

166170
void QgsExternalResourceWidget::loadDocument( const QString& path )
167171
{
168-
if ( path.isNull() )
172+
if ( path.isEmpty() )
169173
{
170174
#ifdef WITH_QTWEBKIT
171175
if ( mDocumentViewerContent == Web )
@@ -176,6 +180,7 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
176180
if ( mDocumentViewerContent == Image )
177181
{
178182
mPixmapLabel->clear();
183+
updateDocumentViewer();
179184
}
180185
}
181186

@@ -190,12 +195,8 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
190195
if ( mDocumentViewerContent == Image )
191196
{
192197
QPixmap pm( path );
193-
if ( !pm.isNull() )
194-
{
195-
mPixmapLabel->setPixmap( pm );
196-
197-
updateDocumentViewer();
198-
}
198+
mPixmapLabel->setPixmap( pm );
199+
updateDocumentViewer();
199200
}
200201
}
201202

src/gui/qgspixmaplabel.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,31 @@ QgsPixmapLabel::QgsPixmapLabel( QWidget *parent ) :
2424

2525
void QgsPixmapLabel::setPixmap( const QPixmap & p )
2626
{
27+
bool sizeChanged = ( p.size() != mPixmap.size() );
2728
mPixmap = p;
28-
QLabel::setPixmap( p );
29+
30+
if ( sizeChanged )
31+
{
32+
updateGeometry();
33+
}
34+
35+
QLabel::setPixmap( mPixmap.scaled( this->size(),
36+
Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
2937
}
3038

3139
int QgsPixmapLabel::heightForWidth( int width ) const
3240
{
41+
if ( mPixmap.isNull() )
42+
return 0;
43+
3344
return (( qreal )mPixmap.height()*width ) / mPixmap.width();
3445
}
3546

3647
QSize QgsPixmapLabel::sizeHint() const
3748
{
49+
if ( mPixmap.isNull() )
50+
return QSize( 0, 0 );
51+
3852
int w = this->width();
3953
return QSize( w, heightForWidth( w ) );
4054
}

0 commit comments

Comments
 (0)