QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgspixmaplabel.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 ----------------------------------------------------
4 date : 7.9.2015
5 copyright : (C) 2015 by Matthias Kuhn
6 email : matthias (at) opengis.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 "qgspixmaplabel.h"
17#include "moc_qgspixmaplabel.cpp"
18
19
21 : QLabel( parent )
22{
23}
24
25void QgsPixmapLabel::setPixmap( const QPixmap &p )
26{
27 const bool sizeChanged = ( p.size() != mPixmap.size() );
28 mPixmap = p;
29
30 if ( mPixmap.isNull() )
31 this->setMinimumHeight( 0 );
32 else
33 this->setMinimumHeight( PIXMAP_MINIMUM_HEIGHT );
34
35 if ( sizeChanged )
36 {
37 updateGeometry();
38 }
39
40 QLabel::setPixmap( mPixmap.scaled( this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
41}
42
43int QgsPixmapLabel::heightForWidth( int width ) const
44{
45 if ( mPixmap.isNull() )
46 return 0;
47
48 return ( ( qreal ) mPixmap.height() * width ) / mPixmap.width();
49}
50
52{
53 if ( mPixmap.isNull() )
54 return QSize( 0, 0 );
55
56 const int w = this->width();
57 return QSize( w, heightForWidth( w ) );
58}
59
60void QgsPixmapLabel::resizeEvent( QResizeEvent *e )
61{
62 QLabel::resizeEvent( e );
63 if ( !mPixmap.isNull() )
64 {
65 // Avoid infinite resize loop by setting a pixmap that'll always have a width and height less or equal to the label size
66 QLabel::setPixmap( mPixmap.scaled( this->size() -= QSize( 1, 1 ), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
67 }
68}
69
71{
72 mPixmap = QPixmap();
73 QLabel::clear();
74 this->setMinimumHeight( 0 );
75}
void clear()
Clears any label contents.
int heightForWidth(int width) const override
Calculates the height for the given width.
QSize sizeHint() const override
An optimal size for the widget.
void resizeEvent(QResizeEvent *) override
void setPixmap(const QPixmap &)
QgsPixmapLabel(QWidget *parent=nullptr)
Constructor for QgsPixmapLabel.