1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

MaskablePixmapWidget: fixed drawing routine

This commit is contained in:
krepa098 2014-09-27 15:39:17 +02:00
parent 4d24bd53f2
commit 97b5599651
2 changed files with 8 additions and 11 deletions

View File

@ -16,20 +16,17 @@
#include "maskablepixmapwidget.h" #include "maskablepixmapwidget.h"
#include <QPainter> #include <QPainter>
#include <QPaintEvent>
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName) MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName)
: QWidget(parent) : QWidget(parent)
{ {
setMinimumSize(size); setFixedSize(size);
setMaximumSize(size); mask = QPixmap(maskName).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
mask = QPixmap(maskName).scaled(maximumSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
} }
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap) void MaskablePixmapWidget::setPixmap(const QPixmap &pmap)
{ {
pixmap = pmap.scaled(maximumSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation); pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
} }
QPixmap MaskablePixmapWidget::getPixmap() const QPixmap MaskablePixmapWidget::getPixmap() const
@ -37,20 +34,20 @@ QPixmap MaskablePixmapWidget::getPixmap() const
return pixmap; return pixmap;
} }
void MaskablePixmapWidget::paintEvent(QPaintEvent *ev) void MaskablePixmapWidget::paintEvent(QPaintEvent *)
{ {
QPixmap tmp(ev->rect().size()); QPixmap tmp(width(), height());
tmp.fill(Qt::transparent); tmp.fill(Qt::transparent);
QPoint offset((ev->rect().size().width()-pixmap.size().width())/2,(ev->rect().size().height()-pixmap.size().height())/2); QPoint offset((width() - pixmap.size().width())/2,(height() - pixmap.size().height())/2); // centering the pixmap
QPainter painter(&tmp); QPainter painter(&tmp);
painter.setCompositionMode(QPainter::CompositionMode_Source); painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.drawPixmap(offset,pixmap); painter.drawPixmap(offset,pixmap);
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.begin(this);
painter.drawPixmap(0,0,tmp); painter.drawPixmap(0,0,tmp);
} }

View File

@ -32,7 +32,7 @@ signals:
void clicked(); void clicked();
protected: protected:
virtual void paintEvent(QPaintEvent *ev); virtual void paintEvent(QPaintEvent *);
virtual void mousePressEvent(QMouseEvent *); virtual void mousePressEvent(QMouseEvent *);
private: private: