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