downscale images in button

This commit is contained in:
fourtf 2020-09-26 15:40:43 +02:00
parent d13b7d9768
commit 06cb88f6c2
2 changed files with 20 additions and 1 deletions

View file

@ -9,6 +9,20 @@
#include "util/FunctionEventFilter.hpp" #include "util/FunctionEventFilter.hpp"
namespace chatterino { namespace chatterino {
namespace {
// returns a new resized image or the old one if the size didn't change
auto resizePixmap(const QPixmap &current, const QPixmap resized,
const QSize &size) -> QPixmap
{
if (resized.size() == size)
return resized;
else
return current.scaled(size, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
}
} // namespace
Button::Button(BaseWidget *parent) Button::Button(BaseWidget *parent)
: BaseWidget(parent) : BaseWidget(parent)
@ -30,6 +44,7 @@ void Button::setMouseEffectColor(boost::optional<QColor> color)
void Button::setPixmap(const QPixmap &_pixmap) void Button::setPixmap(const QPixmap &_pixmap)
{ {
this->pixmap_ = _pixmap; this->pixmap_ = _pixmap;
this->resizedPixmap_ = {};
this->update(); this->update();
} }
@ -123,6 +138,9 @@ void Button::paintEvent(QPaintEvent *)
QRect rect = this->rect(); QRect rect = this->rect();
this->resizedPixmap_ =
resizePixmap(this->pixmap_, this->resizedPixmap_, rect.size());
int margin = this->height() < 22 * this->scale() ? 3 : 6; int margin = this->height() < 22 * this->scale() ? 3 : 6;
int s = this->enableMargin_ ? int(margin * this->scale()) : 0; int s = this->enableMargin_ ? int(margin * this->scale()) : 0;
@ -132,7 +150,7 @@ void Button::paintEvent(QPaintEvent *)
rect.moveTop(s); rect.moveTop(s);
rect.setBottom(rect.bottom() - s - s); rect.setBottom(rect.bottom() - s - s);
painter.drawPixmap(rect, this->pixmap_); painter.drawPixmap(rect, this->resizedPixmap_);
painter.setOpacity(1); painter.setOpacity(1);
} }

View file

@ -78,6 +78,7 @@ private:
QColor borderColor_{}; QColor borderColor_{};
QPixmap pixmap_{}; QPixmap pixmap_{};
QPixmap resizedPixmap_{};
Dim dimPixmap_{Dim::Some}; Dim dimPixmap_{Dim::Some};
bool enableMargin_{true}; bool enableMargin_{true};
QPoint mousePos_{}; QPoint mousePos_{};