mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: Make MaskablePixmapWidget allow to apply style
This commit is contained in:
parent
91dabf11d3
commit
73dcbd2e60
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "maskablepixmapwidget.h"
|
#include "maskablepixmapwidget.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QStyle>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var QPixmap* MaskablePixmapWidget::renderTarget
|
* @var QPixmap* MaskablePixmapWidget::renderTarget
|
||||||
|
@ -26,7 +27,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MaskablePixmapWidget::MaskablePixmapWidget(QWidget* parent, QSize size, QString maskName)
|
MaskablePixmapWidget::MaskablePixmapWidget(QWidget* parent, QSize size, QString maskName)
|
||||||
: QWidget(parent)
|
: QLabel("", parent)
|
||||||
, renderTarget(nullptr)
|
, renderTarget(nullptr)
|
||||||
, maskName(maskName)
|
, maskName(maskName)
|
||||||
, clickable(false)
|
, clickable(false)
|
||||||
|
@ -43,20 +44,25 @@ void MaskablePixmapWidget::setClickable(bool clickable)
|
||||||
{
|
{
|
||||||
this->clickable = clickable;
|
this->clickable = clickable;
|
||||||
|
|
||||||
if (clickable)
|
if (clickable) {
|
||||||
setCursor(Qt::PointingHandCursor);
|
setCursor(Qt::PointingHandCursor);
|
||||||
else
|
} else {
|
||||||
unsetCursor();
|
unsetCursor();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MaskablePixmapWidget::setPixmap(const QPixmap& pmap)
|
void MaskablePixmapWidget::setPixmap(const QPixmap& pmap)
|
||||||
{
|
{
|
||||||
if (!pmap.isNull()) {
|
if (pmap.isNull()) {
|
||||||
unscaled = pmap;
|
return;
|
||||||
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding,
|
|
||||||
Qt::SmoothTransformation);
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unscaled = pmap;
|
||||||
|
pixmap = pmap.scaled(width() - 2, height() - 2,
|
||||||
|
Qt::KeepAspectRatioByExpanding,
|
||||||
|
Qt::SmoothTransformation);
|
||||||
|
updatePixmap();
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap MaskablePixmapWidget::getPixmap() const
|
QPixmap MaskablePixmapWidget::getPixmap() const
|
||||||
|
@ -71,17 +77,35 @@ void MaskablePixmapWidget::setSize(QSize size)
|
||||||
renderTarget = new QPixmap(size);
|
renderTarget = new QPixmap(size);
|
||||||
|
|
||||||
QPixmap pmapMask = QPixmap(maskName);
|
QPixmap pmapMask = QPixmap(maskName);
|
||||||
if (!pmapMask.isNull())
|
if (!pmapMask.isNull()) {
|
||||||
mask = pmapMask.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
mask = pmapMask.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||||
|
}
|
||||||
|
|
||||||
if (!unscaled.isNull()) {
|
if (!unscaled.isNull()) {
|
||||||
pixmap = unscaled.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding,
|
pixmap = unscaled.scaled(width() - 2, height() - 2,
|
||||||
|
Qt::KeepAspectRatioByExpanding,
|
||||||
Qt::SmoothTransformation);
|
Qt::SmoothTransformation);
|
||||||
|
updatePixmap();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MaskablePixmapWidget::paintEvent(QPaintEvent*)
|
|
||||||
|
|
||||||
|
void MaskablePixmapWidget::paintEvent(QPaintEvent* e)
|
||||||
|
{
|
||||||
|
QLabel::setPixmap(*renderTarget);
|
||||||
|
QLabel::paintEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MaskablePixmapWidget::mousePressEvent(QMouseEvent*)
|
||||||
|
{
|
||||||
|
if (clickable) {
|
||||||
|
emit clicked();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MaskablePixmapWidget::updatePixmap()
|
||||||
{
|
{
|
||||||
renderTarget->fill(Qt::transparent);
|
renderTarget->fill(Qt::transparent);
|
||||||
|
|
||||||
|
@ -94,13 +118,4 @@ void MaskablePixmapWidget::paintEvent(QPaintEvent*)
|
||||||
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
||||||
painter.drawPixmap(0, 0, mask);
|
painter.drawPixmap(0, 0, mask);
|
||||||
painter.end();
|
painter.end();
|
||||||
|
|
||||||
painter.begin(this);
|
|
||||||
painter.drawPixmap(0, 0, *renderTarget);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MaskablePixmapWidget::mousePressEvent(QMouseEvent*)
|
|
||||||
{
|
|
||||||
if (clickable)
|
|
||||||
emit clicked();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,14 @@
|
||||||
#ifndef MASKABLEPIXMAPWIDGET_H
|
#ifndef MASKABLEPIXMAPWIDGET_H
|
||||||
#define MASKABLEPIXMAPWIDGET_H
|
#define MASKABLEPIXMAPWIDGET_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QLabel>
|
||||||
|
|
||||||
class MaskablePixmapWidget final : public QWidget
|
class MaskablePixmapWidget final : public QLabel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
MaskablePixmapWidget(QWidget* parent, QSize size, QString maskName = QString());
|
MaskablePixmapWidget(QWidget* parent, QSize size, QString maskName = QString());
|
||||||
~MaskablePixmapWidget();
|
~MaskablePixmapWidget() override;
|
||||||
void autopickBackground();
|
void autopickBackground();
|
||||||
void setClickable(bool clickable);
|
void setClickable(bool clickable);
|
||||||
void setPixmap(const QPixmap& pmap);
|
void setPixmap(const QPixmap& pmap);
|
||||||
|
@ -38,13 +38,15 @@ signals:
|
||||||
void clicked();
|
void clicked();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void paintEvent(QPaintEvent*) final override;
|
void paintEvent(QPaintEvent*) final override;
|
||||||
virtual void mousePressEvent(QMouseEvent*) final override;
|
void mousePressEvent(QMouseEvent*) final override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void updatePixmap();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPixmap pixmap, mask, unscaled;
|
QPixmap pixmap, mask, unscaled;
|
||||||
QPixmap* renderTarget;
|
QPixmap* renderTarget;
|
||||||
QSize size;
|
|
||||||
QString maskName;
|
QString maskName;
|
||||||
bool clickable;
|
bool clickable;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user