Added the new class for resize the image if the window is resized

This commit is contained in:
defnax 2020-01-21 16:22:56 +01:00
parent f8f77f122e
commit 27c4503cfb
3 changed files with 101 additions and 0 deletions

View File

@ -470,6 +470,7 @@ HEADERS += rshare.h \
util/RsFile.h \
util/qtthreadsutils.h \
util/ClickableLabel.h \
util/AspectRatioPixmapLabel.h \
gui/profile/ProfileWidget.h \
gui/profile/ProfileManager.h \
gui/profile/StatusMessage.h \
@ -830,6 +831,7 @@ SOURCES += main.cpp \
util/RsFile.cpp \
util/RichTextEdit.cpp \
util/ClickableLabel.cpp \
util/AspectRatioPixmapLabel.cpp \
gui/profile/ProfileWidget.cpp \
gui/profile/StatusMessage.cpp \
gui/profile/ProfileManager.cpp \

View File

@ -0,0 +1,56 @@
/*******************************************************************************
* retroshare-gui/src/util/AspectRatioPixmapLabel.cpp *
* *
* Copyright (C) 2019 Retroshare Team <retroshare.project@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "AspectRatioPixmapLabel.h"
AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) :
QLabel(parent)
{
this->setMinimumSize(1,1);
setScaledContents(false);
}
void AspectRatioPixmapLabel::setPixmap ( const QPixmap & p)
{
pix = p;
QLabel::setPixmap(scaledPixmap());
}
int AspectRatioPixmapLabel::heightForWidth( int width ) const
{
return pix.isNull() ? this->height() : ((qreal)pix.height()*width)/pix.width();
}
QSize AspectRatioPixmapLabel::sizeHint() const
{
int w = this->width();
return QSize( w, heightForWidth(w) );
}
QPixmap AspectRatioPixmapLabel::scaledPixmap() const
{
return pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
void AspectRatioPixmapLabel::resizeEvent(QResizeEvent * e)
{
if(!pix.isNull())
QLabel::setPixmap(scaledPixmap());
}

View File

@ -0,0 +1,43 @@
/*******************************************************************************
* retroshare-gui/src/util/AspectRatioPixmapLabel.h *
* *
* Copyright (C) 2019 Retroshare Team <retroshare.project@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef ASPECTRATIOPIXMAPLABEL_H
#define ASPECTRATIOPIXMAPLABEL_H
#include <QLabel>
#include <QPixmap>
#include <QResizeEvent>
class AspectRatioPixmapLabel : public QLabel
{
Q_OBJECT
public:
explicit AspectRatioPixmapLabel(QWidget *parent = 0);
virtual int heightForWidth( int width ) const;
virtual QSize sizeHint() const;
QPixmap scaledPixmap() const;
public slots:
void setPixmap ( const QPixmap & );
void resizeEvent(QResizeEvent *);
private:
QPixmap pix;
};
#endif // ASPECTRATIOPIXMAPLABEL_H