QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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(),
36  Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
37 }
38 
39 int QgsPixmapLabel::heightForWidth( int width ) const
40 {
41  if ( mPixmap.isNull() )
42  return 0;
43 
44  return ( ( qreal )mPixmap.height() * width ) / mPixmap.width();
45 }
46 
48 {
49  if ( mPixmap.isNull() )
50  return QSize( 0, 0 );
51 
52  int w = this->width();
53  return QSize( w, heightForWidth( w ) );
54 }
55 
56 void QgsPixmapLabel::resizeEvent( QResizeEvent *e )
57 {
58  QLabel::resizeEvent( e );
59  QLabel::setPixmap( mPixmap.scaled( this->size(),
60  Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
61 }
QSize sizeHint() const override
An optimal size for the widget.
void setPixmap(const QPixmap &)
int heightForWidth(int width) const override
Calculates the height for the given width.
void resizeEvent(QResizeEvent *) override
QgsPixmapLabel(QWidget *parent=nullptr)
Constructor for QgsPixmapLabel.