dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
scaledpixmaplabel.cpp
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  *
25  * Additional note on redistribution: The copyright and license notices above
26  * must be maintained in each individual source file that is a derivative work
27  * of this source file; otherwise redistribution is prohibited.
28  */
29 
30 #include "scaledpixmaplabel.h"
31 
32 
33 ScaledPixmapLabel::ScaledPixmapLabel(QWidget *parent, Qt::WindowFlags f) :
34  QLabel(parent, f)
35 {
36  setScaledContents(false);
37 }
38 
40 {
41  if (fullPixmap.isNull())
42  return -1;
43  return static_cast<int>(static_cast<qreal>(width) * fullPixmap.height() / fullPixmap.width());
44 }
45 
47 {
48  return fullPixmap.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
49 }
50 
52 {
53  if (fullPixmap.isNull())
54  return QSize();
55  int w = std::min(width(), static_cast<int>(fullPixmap.width() / fullPixmap.devicePixelRatio()));
56  return QSize(w, heightForWidth(w));
57 }
58 
59 void ScaledPixmapLabel::resizeEvent(QResizeEvent *event)
60 {
61  Q_UNUSED(event)
62  if (!fullPixmap.isNull())
63  QLabel::setPixmap(scaledPixmap());
64 }
65 
66 void ScaledPixmapLabel::setPixmap(const QPixmap &pixmap)
67 {
68  fullPixmap = pixmap;
69  if (fullPixmap.isNull())
70  QLabel::setPixmap(pixmap);
71  else
72  QLabel::setPixmap(scaledPixmap());
73 }
74 
void setPixmap(const QPixmap &pixmap)
setPixmap Sets the current pixmap
QPixmap scaledPixmap() const
scaledPixmap Scale the pixmap to fit desired size
virtual void resizeEvent(QResizeEvent *event)
virtual QSize sizeHint() const
sizeHint Calculate desired size
ScaledPixmapLabel(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
ScaledPixmapLabel.
virtual int heightForWidth(int width) const
heightForWidth Given a desired width, return the desired height