QGIS API Documentation  3.25.0-Master (dec16ba68b)
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 
18 
20  : QLabel( parent )
21 {
22 }
23 
24 void QgsPixmapLabel::setPixmap( const QPixmap &p )
25 {
26  const bool sizeChanged = ( p.size() != mPixmap.size() );
27  mPixmap = p;
28 
29  if ( mPixmap.isNull() )
30  this->setMinimumHeight( 0 );
31  else
32  this->setMinimumHeight( PIXMAP_MINIMUM_HEIGHT );
33 
34  if ( sizeChanged )
35  {
36  updateGeometry();
37  }
38 
39  QLabel::setPixmap( mPixmap.scaled( this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
40 }
41 
42 int QgsPixmapLabel::heightForWidth( int width ) const
43 {
44  if ( mPixmap.isNull() )
45  return 0;
46 
47  return ( ( qreal )mPixmap.height() * width ) / mPixmap.width();
48 }
49 
51 {
52  if ( mPixmap.isNull() )
53  return QSize( 0, 0 );
54 
55  const int w = this->width();
56  return QSize( w, heightForWidth( w ) );
57 }
58 
59 void QgsPixmapLabel::resizeEvent( QResizeEvent *e )
60 {
61  QLabel::resizeEvent( e );
62  if ( !mPixmap.isNull() )
63  {
64  // Avoid infinite resize loop by setting a pixmap that'll always have a width and height less or equal to the label size
65  QLabel::setPixmap( mPixmap.scaled( this->size() -= QSize( 1, 1 ), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
66  }
67 }
68 
70 {
71  mPixmap = QPixmap();
72  QLabel::clear();
73  this->setMinimumHeight( 0 );
74 }
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.